Skip to content

Commit

Permalink
identify: filter received addresses based on the node's remote address
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jun 1, 2023
1 parent 8d77135 commit cf0ca1b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion p2p/protocol/identify/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ func (ids *idService) consumeMessage(mes *pb.Identify, c network.Conn, isPush bo
log.Debugf("error adding signed addrs to peerstore: %v", err)
}
} else {
ids.Host.Peerstore().AddAddrs(p, lmaddrs, ttl)
ids.Host.Peerstore().AddAddrs(p, filterAddrs(lmaddrs, c.RemoteMultiaddr()), ttl)
}

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

func (nn *netNotifiee) Listen(n network.Network, a ma.Multiaddr) {}
func (nn *netNotifiee) ListenClose(n network.Network, a ma.Multiaddr) {}

// filterAddrs filters the address slice based on the remove multiaddr:
// * if it's a localhost address, no filtering is applied
// * if it's a local network address, all localhost addresses are filtered out
// * if it's a public address, all localhost and local network addresses are filtered out
func filterAddrs(addrs []ma.Multiaddr, remote ma.Multiaddr) []ma.Multiaddr {
if manet.IsIPLoopback(remote) {
return addrs
}
if manet.IsPrivateAddr(remote) {
return ma.FilterAddrs(addrs, func(a ma.Multiaddr) bool { return !manet.IsIPLoopback(a) })
}
return ma.FilterAddrs(addrs, manet.IsPublicAddr)
}

0 comments on commit cf0ca1b

Please # to comment.