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

Handle https to http override in TLS MitM websocket connection #505

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion https.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,12 @@ func (proxy *ProxyHttpServer) handleHttps(w http.ResponseWriter, r *http.Request
if resp == nil {
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