Skip to content

Commit

Permalink
swarm: remove localhost udp ports check
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Jun 5, 2023
1 parent 6f27081 commit 505b66e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 61 deletions.
25 changes: 0 additions & 25 deletions p2p/net/swarm/swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,20 +448,8 @@ func (s *Swarm) filterKnownUndialables(p peer.ID, addrs []ma.Multiaddr) []ma.Mul
})
}

// Make a map of udp ports we are listening on to filter peers web transport addresses
ourLocalHostUDPPorts := make(map[string]bool, 2)
for _, a := range ourAddrs {
if !manet.IsIPLoopback(a) {
continue
}
if p, err := a.ValueForProtocol(ma.P_UDP); err == nil {
ourLocalHostUDPPorts[p] = true
}
}

return ma.FilterAddrs(addrs,
func(addr ma.Multiaddr) bool { return !ma.Contains(ourAddrs, addr) },
func(addr ma.Multiaddr) bool { return checkLocalHostUDPAddrs(addr, ourLocalHostUDPPorts) },
s.canDial,
// TODO: Consider allowing link-local addresses
func(addr ma.Multiaddr) bool { return !manet.IsIP6LinkLocal(addr) },
Expand Down Expand Up @@ -558,16 +546,3 @@ func isRelayAddr(addr ma.Multiaddr) bool {
_, err := addr.ValueForProtocol(ma.P_CIRCUIT)
return err == nil
}

// checkLocalHostUDPAddrs returns false for addresses that have the same localhost port
// as the one we are listening on
// This is useful for filtering out peer's localhost webtransport addresses.
func checkLocalHostUDPAddrs(addr ma.Multiaddr, ourUDPPorts map[string]bool) bool {
if !manet.IsIPLoopback(addr) {
return true
}
if p, err := addr.ValueForProtocol(ma.P_UDP); err == nil {
return !ourUDPPorts[p]
}
return true
}
36 changes: 0 additions & 36 deletions p2p/net/swarm/swarm_dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ import (
"github.com/libp2p/go-libp2p/core/test"
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
quic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/quicreuse"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/libp2p/go-libp2p/p2p/transport/websocket"
webtransport "github.com/libp2p/go-libp2p/p2p/transport/webtransport"

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
Expand Down Expand Up @@ -241,36 +238,3 @@ func TestAddrResolutionRecursive(t *testing.T) {
require.Len(t, addrs2, 1)
require.Contains(t, addrs2, addr1)
}

func TestLocalHostWebTransportRemoved(t *testing.T) {
resolver, err := madns.NewResolver()
if err != nil {
t.Fatal(err)
}

s := newTestSwarmWithResolver(t, resolver)
p, err := test.RandPeerID()
if err != nil {
t.Error(err)
}
reuse, err := quicreuse.NewConnManager([32]byte{})
require.NoError(t, err)
defer reuse.Close()

quicTr, err := quic.NewTransport(s.Peerstore().PrivKey(s.LocalPeer()), reuse, nil, nil, nil)
require.NoError(t, err)
require.NoError(t, s.AddTransport(quicTr))

webtransportTr, err := webtransport.New(s.Peerstore().PrivKey(s.LocalPeer()), nil, reuse, nil, nil)
require.NoError(t, err)
s.AddTransport(webtransportTr)

err = s.AddListenAddr(ma.StringCast("/ip4/127.0.0.1/udp/10000/quic-v1/"))
require.NoError(t, err)

res := s.filterKnownUndialables(p, []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/udp/10000/quic-v1/webtransport")})
if len(res) != 0 {
t.Errorf("failed to filter localhost webtransport address")
}
s.Close()
}

0 comments on commit 505b66e

Please sign in to comment.