Skip to content

Commit cf0ca1b

Browse files
identify: filter received addresses based on the node's remote address
1 parent 8d77135 commit cf0ca1b

File tree

1 file changed

+15
-1
lines changed
  • p2p/protocol/identify

1 file changed

+15
-1
lines changed

p2p/protocol/identify/id.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ func (ids *idService) consumeMessage(mes *pb.Identify, c network.Conn, isPush bo
769769
log.Debugf("error adding signed addrs to peerstore: %v", err)
770770
}
771771
} else {
772-
ids.Host.Peerstore().AddAddrs(p, lmaddrs, ttl)
772+
ids.Host.Peerstore().AddAddrs(p, filterAddrs(lmaddrs, c.RemoteMultiaddr()), ttl)
773773
}
774774

775775
// Finally, expire all temporary addrs.
@@ -959,3 +959,17 @@ func (nn *netNotifiee) Disconnected(_ network.Network, c network.Conn) {
959959

960960
func (nn *netNotifiee) Listen(n network.Network, a ma.Multiaddr) {}
961961
func (nn *netNotifiee) ListenClose(n network.Network, a ma.Multiaddr) {}
962+
963+
// filterAddrs filters the address slice based on the remove multiaddr:
964+
// * if it's a localhost address, no filtering is applied
965+
// * if it's a local network address, all localhost addresses are filtered out
966+
// * if it's a public address, all localhost and local network addresses are filtered out
967+
func filterAddrs(addrs []ma.Multiaddr, remote ma.Multiaddr) []ma.Multiaddr {
968+
if manet.IsIPLoopback(remote) {
969+
return addrs
970+
}
971+
if manet.IsPrivateAddr(remote) {
972+
return ma.FilterAddrs(addrs, func(a ma.Multiaddr) bool { return !manet.IsIPLoopback(a) })
973+
}
974+
return ma.FilterAddrs(addrs, manet.IsPublicAddr)
975+
}

0 commit comments

Comments
 (0)