Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default websocket port if it's missing #606

Merged
merged 3 commits into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading