Skip to content

Commit

Permalink
Support SOCKS proxy with wss
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Jan 14, 2025
1 parent 4651a0d commit f198023
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions p2p/transport/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ func (t *WebsocketTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (ma
return nil, err
}
isWss := wsurl.Scheme == "wss"
dialer := ws.Dialer{HandshakeTimeout: 30 * time.Second}
dialer := ws.Dialer{
HandshakeTimeout: 30 * time.Second,
// Inherit the default proxy behavior
Proxy: ws.DefaultDialer.Proxy,
}
if isWss {
sni := ""
sni, err = raddr.ValueForProtocol(ma.P_SNI)
Expand All @@ -204,16 +208,22 @@ func (t *WebsocketTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (ma
copytlsClientConf.ServerName = sni
dialer.TLSClientConfig = copytlsClientConf
ipAddr := wsurl.Host
// Setting the NetDial because we already have the resolved IP address, so we don't want to do another resolution.
// We set the `.Host` to the sni field so that the host header gets properly set.
wsurl.Host = sni + ":" + wsurl.Port()
// Setting the NetDial because we already have the resolved IP address, so we can avoid another resolution.
dialer.NetDial = func(network, address string) (net.Conn, error) {
tcpAddr, err := net.ResolveTCPAddr(network, ipAddr)
var tcpAddr *net.TCPAddr
var err error
if address == wsurl.Host {
tcpAddr, err = net.ResolveTCPAddr(network, ipAddr) // Use our already resolved IP address
} else {
tcpAddr, err = net.ResolveTCPAddr(network, address)
}
if err != nil {
return nil, err
}
return net.DialTCP("tcp", nil, tcpAddr)
}
wsurl.Host = sni + ":" + wsurl.Port()
} else {
dialer.TLSClientConfig = t.tlsClientConf
}
Expand Down

0 comments on commit f198023

Please sign in to comment.