Skip to content

Commit

Permalink
Add https to http websocket override
Browse files Browse the repository at this point in the history
  • Loading branch information
Damin Lukawski authored and elazarl committed Sep 9, 2024
1 parent 8b0c205 commit 6741dbf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion https.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,12 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
}
if isWebSocketRequest(req) {
ctx.Logf("Request looks like websocket upgrade.")
proxy.serveWebsocketTLS(ctx, w, req, tlsConfig, rawClientTls)
if req.URL.Scheme == "http" {
ctx.Logf("Enforced HTTP websocket forwarding over TLS")
proxy.serveWebsocketHttpOverTLS(ctx, w, req, rawClientTls)
} else {
proxy.serveWebsocketTLS(ctx, w, req, tlsConfig, rawClientTls)
}
return
}
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS(ctx *ProxyCtx, w http.ResponseWr
proxy.proxyWebsocket(ctx, targetConn, clientConn)
}

func (proxy *ProxyHttpServer) serveWebsocketHttpOverTLS(ctx *ProxyCtx, w http.ResponseWriter, req *http.Request, clientConn *tls.Conn) {
targetURL := url.URL{Scheme: "ws", Host: req.URL.Host, Path: req.URL.Path}

// Connect to upstream
targetConn, err := proxy.connectDial(ctx, "tcp", targetURL.Host)
if err != nil {
ctx.Warnf("Error dialing target site: %v", err)
return
}
defer targetConn.Close()

// Perform handshake
if err := proxy.websocketHandshake(ctx, req, targetConn, clientConn); err != nil {
ctx.Warnf("Websocket handshake error: %v", err)
return
}

// Proxy wss connection
proxy.proxyWebsocket(ctx, targetConn, clientConn)
}

func (proxy *ProxyHttpServer) serveWebsocket(ctx *ProxyCtx, w http.ResponseWriter, req *http.Request) {
targetURL := url.URL{Scheme: "ws", Host: req.URL.Host, Path: req.URL.Path}

Expand Down

0 comments on commit 6741dbf

Please sign in to comment.