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

fix: websocket transport should send host header #1829

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion p2p/transport/websocket/addrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,27 @@ func parseMultiaddr(maddr ma.Multiaddr) (*url.URL, error) {

type parsedWebsocketMultiaddr struct {
isWSS bool
// sni is the SNI value for the TLS handshake
// sni is the SNI value for the TLS handshake, and for setting HTTP Host header
sni *ma.Component
// the rest of the multiaddr before the /tls/sni/example.com/ws or /ws or /wss
restMultiaddr ma.Multiaddr
}

func (pwma *parsedWebsocketMultiaddr) toMultiaddr() ma.Multiaddr {
if !pwma.isWSS {
if pwma.sni == nil {
return pwma.restMultiaddr.Encapsulate(wsComponent)
}
return pwma.restMultiaddr.Encapsulate(pwma.sni).Encapsulate(wsComponent)
}

if pwma.sni == nil {
return pwma.restMultiaddr.Encapsulate(tlsComponent).Encapsulate(wsComponent)
}

return pwma.restMultiaddr.Encapsulate(tlsComponent).Encapsulate(pwma.sni).Encapsulate(wsComponent)
}

func parseWebsocketMultiaddr(a ma.Multiaddr) (parsedWebsocketMultiaddr, error) {
out := parsedWebsocketMultiaddr{}
// First check if we have a WSS component. If so we'll canonicalize it into a /tls/ws
Expand Down
12 changes: 0 additions & 12 deletions p2p/transport/websocket/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ type listener struct {
incoming chan *Conn
}

func (pwma *parsedWebsocketMultiaddr) toMultiaddr() ma.Multiaddr {
if !pwma.isWSS {
return pwma.restMultiaddr.Encapsulate(wsComponent)
}

if pwma.sni == nil {
return pwma.restMultiaddr.Encapsulate(tlsComponent).Encapsulate(wsComponent)
}

return pwma.restMultiaddr.Encapsulate(tlsComponent).Encapsulate(pwma.sni).Encapsulate(wsComponent)
}

// newListener creates a new listener from a raw net.Listener.
// tlsConf may be nil (for unencrypted websockets).
func newListener(a ma.Multiaddr, tlsConf *tls.Config) (*listener, error) {
Expand Down
34 changes: 23 additions & 11 deletions p2p/transport/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package websocket
import (
"context"
"crypto/tls"
"net"
"net/http"
"time"

Expand Down Expand Up @@ -125,11 +126,6 @@ func (t *WebsocketTransport) Resolve(ctx context.Context, maddr ma.Multiaddr) ([
return nil, err
}

if !parsed.isWSS {
// No /tls/ws component, this isn't a secure websocket multiaddr. We can just return it here
return []ma.Multiaddr{maddr}, nil
}

if parsed.sni == nil {
var err error
// We don't have an sni component, we'll use dns/dnsaddr
Expand Down Expand Up @@ -174,14 +170,30 @@ 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}
if isWss {
sni := ""
sni, err = raddr.ValueForProtocol(ma.P_SNI)
if err != nil {
sni = ""

sni := ""
sni, err = raddr.ValueForProtocol(ma.P_SNI)
if err != nil {
sni = ""
}

host := wsurl.Host

var dialer ws.Dialer
if sni == "" {
dialer = ws.Dialer{HandshakeTimeout: 30 * time.Second}
} else {
dialer = ws.Dialer{
HandshakeTimeout: 30 * time.Second,
NetDial: func(network, address string) (net.Conn, error) {
tcpAddr, _ := net.ResolveTCPAddr(network, host)
return net.DialTCP("tcp", nil, tcpAddr)
},
}
wsurl.Host = sni + ":" + wsurl.Port()
}

if isWss {
if sni != "" {
copytlsClientConf := t.tlsClientConf.Clone()
copytlsClientConf.ServerName = sni
Expand Down
2 changes: 2 additions & 0 deletions p2p/transport/websocket/websocket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ func TestWriteZero(t *testing.T) {
func TestResolveMultiaddr(t *testing.T) {
// map[unresolved]resolved
testCases := map[string]string{
"/ip4/1.2.3.4/tcp/1234/ws": "/ip4/1.2.3.4/tcp/1234/ws",
"/dns4/example.com/tcp/1234/ws": "/dns4/example.com/tcp/1234/sni/example.com/ws",
"/dns4/example.com/tcp/1234/wss": "/dns4/example.com/tcp/1234/tls/sni/example.com/ws",
"/dns6/example.com/tcp/1234/wss": "/dns6/example.com/tcp/1234/tls/sni/example.com/ws",
"/dnsaddr/example.com/tcp/1234/wss": "/dnsaddr/example.com/tcp/1234/tls/sni/example.com/ws",
Expand Down