Skip to content

Commit ebc628b

Browse files
committed
autonatv2: fix normalization for websocket addrs
1. Convert /wss to /tls/ws 2. Ignore the sni for comparison because there's no sni information on `conn.LocalAddr()`
1 parent ee94b93 commit ebc628b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

p2p/protocol/autonatv2/autonat_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,24 @@ func TestAreAddrsConsistency(t *testing.T) {
636636
dialAddr: ma.StringCast("/ip4/1.2.3.4/udp/123/quic-v1/"),
637637
success: false,
638638
},
639+
{
640+
name: "wss",
641+
dialAddr: ma.StringCast("/dns/lib.p2p/tcp/1/wss"),
642+
localAddr: ma.StringCast("/ip4/1.2.3.4/tcp/1/tls/ws"),
643+
success: true,
644+
},
645+
{
646+
name: "tls-sni",
647+
localAddr: ma.StringCast("/ip4/1.2.3.4/tcp/1/wss"),
648+
dialAddr: ma.StringCast("/ip4/1.2.3.4/tcp/1/tls/sni/abc.xyz/ws"),
649+
success: true,
650+
},
651+
{
652+
name: "only p2p",
653+
localAddr: ma.StringCast("/p2p/QmYo41GybvrXk8y8Xnm1P7pfA4YEXCpfnLyzgRPnNbG35e"),
654+
dialAddr: ma.StringCast("/p2p/QmYo41GybvrXk8y8Xnm1P7pfA4YEXCpfnLyzgRPnNbG35e"),
655+
success: true,
656+
},
639657
}
640658
for _, tc := range tests {
641659
t.Run(tc.name, func(t *testing.T) {

p2p/protocol/autonatv2/client.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,40 @@ func (ac *client) handleDialBack(s network.Stream) {
325325
}
326326
}
327327

328+
var tlsWSAddr = ma.StringCast("/tls/ws")
329+
328330
// normalizeMultiaddr returns a multiaddr suitable for equality checks.
329-
// it removes trailing certhashes.
331+
// it removes trailing certhashes and p2p components, removes sni components,
332+
// and translates /wss to /tls/ws.
333+
// Remove sni components because there's no way for us to verify whether the
334+
// correct sni was dialled by the remote host as the LocalAddr on the websocket conn
335+
// doesn't have sni information.
336+
// Note: This is used for comparing two addresses where both the addresses are
337+
// controlled by the host not by a remote node.
330338
func normalizeMultiaddr(addr ma.Multiaddr) ma.Multiaddr {
331339
addr = removeTrailing(addr, ma.P_P2P)
332340
addr = removeTrailing(addr, ma.P_CERTHASH)
341+
342+
for i, c := range addr {
343+
if c.Code() == ma.P_WSS {
344+
na := make(ma.Multiaddr, 0, len(addr)+1)
345+
na = append(na, addr[:i]...)
346+
na = append(na, tlsWSAddr...)
347+
na = append(na, addr[i+1:]...)
348+
addr = na
349+
break // only do this once; there shouldn't be two /wss components anyway
350+
}
351+
}
352+
353+
for i, c := range addr {
354+
if c.Code() == ma.P_SNI {
355+
na := make(ma.Multiaddr, 0, len(addr)-1)
356+
na = append(na, addr[:i]...)
357+
na = append(na, addr[i+1:]...)
358+
addr = na
359+
break // only do this once; there shouldn't be two /sni components anyway
360+
}
361+
}
333362
return addr
334363
}
335364

0 commit comments

Comments
 (0)