From 0d842e7bcdedefb004d435875346776f381ed0f8 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Fri, 27 Dec 2024 13:01:34 +0100 Subject: [PATCH 1/3] Add default websocket port if it's missing --- websocket.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/websocket.go b/websocket.go index 07c73a3b..c394b7a7 100644 --- a/websocket.go +++ b/websocket.go @@ -4,6 +4,7 @@ import ( "bufio" "crypto/tls" "io" + "net" "net/http" "net/url" "strings" @@ -32,7 +33,13 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS( tlsConfig *tls.Config, clientConn *tls.Conn, ) { - targetURL := url.URL{Scheme: "wss", Host: req.URL.Host, Path: req.URL.Path} + // Port is optional in req.URL.Host, in this case SplitHostPort returns + // an error, and we add the default port + host, 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) @@ -58,7 +65,13 @@ func (proxy *ProxyHttpServer) serveWebsocketHttpOverTLS( req *http.Request, clientConn *tls.Conn, ) { - targetURL := url.URL{Scheme: "ws", Host: req.URL.Host, Path: req.URL.Path} + // Port is optional in req.URL.Host, in this case SplitHostPort returns + // an error, and we add the default port + host, 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) From 606639fa43618afc16ac54901e27de09b0ed98fe Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Fri, 27 Dec 2024 13:12:43 +0100 Subject: [PATCH 2/3] Fix host if we already had the port --- websocket.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/websocket.go b/websocket.go index c394b7a7..64bc7405 100644 --- a/websocket.go +++ b/websocket.go @@ -38,6 +38,9 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS( host, port, err := net.SplitHostPort(req.URL.Host) if err != nil || port == "" { host = net.JoinHostPort(req.URL.Host, "443") + } else { + // We already had a port, just use it + host = req.URL.Host } targetURL := url.URL{Scheme: "wss", Host: host, Path: req.URL.Path} @@ -70,6 +73,9 @@ func (proxy *ProxyHttpServer) serveWebsocketHttpOverTLS( host, port, err := net.SplitHostPort(req.URL.Host) if err != nil || port == "" { host = net.JoinHostPort(req.URL.Host, "80") + } else { + // We already had a port, just use it + host = req.URL.Host } targetURL := url.URL{Scheme: "ws", Host: host, Path: req.URL.Path} From 15d9bb4043b79b1df10258465c60f44b88f19430 Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Fri, 27 Dec 2024 13:19:42 +0100 Subject: [PATCH 3/3] Fix ineffective assignment --- websocket.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/websocket.go b/websocket.go index 64bc7405..97108ee5 100644 --- a/websocket.go +++ b/websocket.go @@ -33,14 +33,12 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS( tlsConfig *tls.Config, clientConn *tls.Conn, ) { + host := req.URL.Host // Port is optional in req.URL.Host, in this case SplitHostPort returns // an error, and we add the default port - host, port, err := net.SplitHostPort(req.URL.Host) + _, port, err := net.SplitHostPort(req.URL.Host) if err != nil || port == "" { host = net.JoinHostPort(req.URL.Host, "443") - } else { - // We already had a port, just use it - host = req.URL.Host } targetURL := url.URL{Scheme: "wss", Host: host, Path: req.URL.Path} @@ -68,14 +66,12 @@ func (proxy *ProxyHttpServer) serveWebsocketHttpOverTLS( req *http.Request, clientConn *tls.Conn, ) { + host := req.URL.Host // Port is optional in req.URL.Host, in this case SplitHostPort returns // an error, and we add the default port - host, port, err := net.SplitHostPort(req.URL.Host) + _, port, err := net.SplitHostPort(req.URL.Host) if err != nil || port == "" { host = net.JoinHostPort(req.URL.Host, "80") - } else { - // We already had a port, just use it - host = req.URL.Host } targetURL := url.URL{Scheme: "ws", Host: host, Path: req.URL.Path}