From 505b66e431a15f5955ccb8d4647e8c0dbea628b0 Mon Sep 17 00:00:00 2001 From: sukun Date: Mon, 5 Jun 2023 20:22:10 +0530 Subject: [PATCH] swarm: remove localhost udp ports check --- p2p/net/swarm/swarm_dial.go | 25 ---------------------- p2p/net/swarm/swarm_dial_test.go | 36 -------------------------------- 2 files changed, 61 deletions(-) diff --git a/p2p/net/swarm/swarm_dial.go b/p2p/net/swarm/swarm_dial.go index 7aa2befe7a..c5b3e7e320 100644 --- a/p2p/net/swarm/swarm_dial.go +++ b/p2p/net/swarm/swarm_dial.go @@ -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) }, @@ -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 -} diff --git a/p2p/net/swarm/swarm_dial_test.go b/p2p/net/swarm/swarm_dial_test.go index ce60701875..c0d1c78f6e 100644 --- a/p2p/net/swarm/swarm_dial_test.go +++ b/p2p/net/swarm/swarm_dial_test.go @@ -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" @@ -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() -}