diff --git a/p2p/transport/websocket/websocket.go b/p2p/transport/websocket/websocket.go index e24cb88c6d..6c5799d46e 100644 --- a/p2p/transport/websocket/websocket.go +++ b/p2p/transport/websocket/websocket.go @@ -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) @@ -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 }