Skip to content

Commit

Permalink
identify: don't filter dns addresses based on remote addr type
Browse files Browse the repository at this point in the history
  • Loading branch information
sukunrt committed Sep 3, 2023
1 parent 9f14eb7 commit 7b6f8c4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
}
32 changes: 32 additions & 0 deletions p2p/protocol/identify/id_glass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 7b6f8c4

Please sign in to comment.