Skip to content

Commit

Permalink
Add default websocket port if it's missing (#606)
Browse files Browse the repository at this point in the history
* Add default websocket port if it's missing

* Fix host if we already had the port

* Fix ineffective assignment
  • Loading branch information
ErikPelli authored Dec 27, 2024
1 parent b229734 commit 020a007
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"crypto/tls"
"io"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -32,7 +33,14 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS(
tlsConfig *tls.Config,
clientConn *tls.Conn,
) {
targetURL := url.URL{Scheme: "wss", Host: req.URL.Host, Path: req.URL.Path}
host := req.URL.Host
// Port is optional in req.URL.Host, in this case SplitHostPort returns
// an error, and we add the default port
_, port, err := net.SplitHostPort(req.URL.Host)
if err != nil || port == "" {
host = net.JoinHostPort(req.URL.Host, "443")
}
targetURL := url.URL{Scheme: "wss", Host: host, Path: req.URL.Path}

// Connect to upstream
targetConn, err := tls.Dial("tcp", targetURL.Host, tlsConfig)
Expand All @@ -58,7 +66,14 @@ func (proxy *ProxyHttpServer) serveWebsocketHttpOverTLS(
req *http.Request,
clientConn *tls.Conn,
) {
targetURL := url.URL{Scheme: "ws", Host: req.URL.Host, Path: req.URL.Path}
host := req.URL.Host
// Port is optional in req.URL.Host, in this case SplitHostPort returns
// an error, and we add the default port
_, port, err := net.SplitHostPort(req.URL.Host)
if err != nil || port == "" {
host = net.JoinHostPort(req.URL.Host, "80")
}
targetURL := url.URL{Scheme: "ws", Host: host, Path: req.URL.Path}

// Connect to upstream
targetConn, err := proxy.connectDial(ctx, "tcp", targetURL.Host)
Expand Down

0 comments on commit 020a007

Please sign in to comment.