Skip to content

Commit

Permalink
added port to websocket connections
Browse files Browse the repository at this point in the history
  • Loading branch information
M41KL-N41TT committed Feb 26, 2024
1 parent e27fa9c commit d8f369c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/elazarl/goproxy

require github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2
go 1.22.0

require github.com/elazarl/goproxy/ext v0.0.0-20231117061959-7cc037d33fb5
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2 h1:dWB6v3RcOy03t/bUadywsbyrQwCqZeNIEX6M1OtSZOM=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/elazarl/goproxy/ext v0.0.0-20231117061959-7cc037d33fb5 h1:iGoePcl8bIDJxxRAL2Q4E4Rt35z5m917RJb8lAvdrQw=
github.com/elazarl/goproxy/ext v0.0.0-20231117061959-7cc037d33fb5/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
13 changes: 11 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 All @@ -26,7 +27,11 @@ func isWebSocketRequest(r *http.Request) bool {
}

func (proxy *ProxyHttpServer) serveWebsocketTLS(ctx *ProxyCtx, w http.ResponseWriter, req *http.Request, tlsConfig *tls.Config, clientConn *tls.Conn) {
targetURL := url.URL{Scheme: "wss", Host: req.URL.Host, Path: req.URL.Path}
host, port, _ := net.SplitHostPort(req.URL.Host)
if port == "" {
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 @@ -47,7 +52,11 @@ func (proxy *ProxyHttpServer) serveWebsocketTLS(ctx *ProxyCtx, w http.ResponseWr
}

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}
host, port, _ := net.SplitHostPort(req.URL.Host)
if port == "" {
host += ":80"
}
targetURL := url.URL{Scheme: "ws", Host: host, Path: req.URL.Path}

targetConn, err := proxy.connectDial(ctx, "tcp", targetURL.Host)
if err != nil {
Expand Down

0 comments on commit d8f369c

Please sign in to comment.