diff --git a/p2p/protocol/identify/id.go b/p2p/protocol/identify/id.go index 6c07dbdc8e..72dd019dc9 100644 --- a/p2p/protocol/identify/id.go +++ b/p2p/protocol/identify/id.go @@ -996,7 +996,7 @@ func filterAddrs(addrs []ma.Multiaddr, remote ma.Multiaddr) []ma.Multiaddr { return addrs } if manet.IsPrivateAddr(remote) { - return ma.FilterAddrs(addrs, func(a ma.Multiaddr) bool { return !manet.IsIPLoopback(a) }) + return ma.FilterAddrs(addrs, func(a ma.Multiaddr) bool { return !manet.IsThinWaist(a) || !manet.IsIPLoopback(a) }) } - return ma.FilterAddrs(addrs, manet.IsPublicAddr) + return ma.FilterAddrs(addrs, func(a ma.Multiaddr) bool { return !manet.IsThinWaist(a) || manet.IsPublicAddr(a) }) } diff --git a/p2p/protocol/identify/id_glass_test.go b/p2p/protocol/identify/id_glass_test.go index 777cef01ed..21f76405b3 100644 --- a/p2p/protocol/identify/id_glass_test.go +++ b/p2p/protocol/identify/id_glass_test.go @@ -12,6 +12,7 @@ import ( recordPb "github.com/libp2p/go-libp2p/core/record/pb" blhost "github.com/libp2p/go-libp2p/p2p/host/blank" swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing" + ma "github.com/multiformats/go-multiaddr" "google.golang.org/protobuf/proto" "github.com/stretchr/testify/assert" @@ -173,3 +174,34 @@ func TestInvalidSignedPeerRecord(t *testing.T) { require.True(t, ok) require.Nil(t, cab.GetPeerRecord(h2.ID())) } + +func TestIncomingAddrFilter(t *testing.T) { + lhAddr := ma.StringCast("/ip4/127.0.0.1/udp/123/quic-v1") + privAddr := ma.StringCast("/ip4/192.168.1.101/tcp/123") + pubAddr := ma.StringCast("/ip6/2::1/udp/123/quic-v1") + dnsAddr := ma.StringCast("/dns/example.com/udp/123/quic-v1") + tests := []struct { + output []ma.Multiaddr + remote ma.Multiaddr + }{ + { + output: []ma.Multiaddr{lhAddr, privAddr, pubAddr, dnsAddr}, + remote: lhAddr, + }, + { + output: []ma.Multiaddr{privAddr, pubAddr, dnsAddr}, + remote: privAddr, + }, + { + output: []ma.Multiaddr{pubAddr, dnsAddr}, + remote: pubAddr, + }, + } + for _, tc := range tests { + t.Run(fmt.Sprintf("remote:%s", tc.remote), func(t *testing.T) { + input := []ma.Multiaddr{lhAddr, privAddr, pubAddr, dnsAddr} + got := filterAddrs(input, tc.remote) + require.ElementsMatch(t, tc.output, got) + }) + } +}