mirror of
https://github.com/ipfs/kubo.git
synced 2026-02-27 05:17:49 +08:00
p2p/proto/identify: use observed listen addrs
This commit finally makes use of the sent observed addrs. If the connection's local address is from one of our listen addrs, then the remote's observed addr is its natted mapping, which is useful to us. For now, we add it directly to our address book. (a future commit should make addressbook addresses expire)
This commit is contained in:
parent
289f24207e
commit
e59b88dace
@ -163,7 +163,9 @@ func (ids *IDService) consumeMessage(mes *pb.Identify, c inet.Conn) {
|
||||
p := c.RemotePeer()
|
||||
|
||||
// mes.Protocols
|
||||
|
||||
// mes.ObservedAddr
|
||||
ids.consumeObservedAddress(mes.GetObservedAddr(), c)
|
||||
|
||||
// mes.ListenAddrs
|
||||
laddrs := mes.GetListenAddrs()
|
||||
@ -208,3 +210,44 @@ func (ids *IDService) IdentifyWait(c inet.Conn) <-chan struct{} {
|
||||
close(ch)
|
||||
return ch
|
||||
}
|
||||
|
||||
func (ids *IDService) consumeObservedAddress(observed []byte, c inet.Conn) {
|
||||
if observed == nil {
|
||||
return
|
||||
}
|
||||
|
||||
maddr, err := ma.NewMultiaddrBytes(observed)
|
||||
if err != nil {
|
||||
log.Debugf("error parsing received observed addr for %s: %s", c, err)
|
||||
return
|
||||
}
|
||||
|
||||
// we should only use ObservedAddr when our connection's LocalAddr is one
|
||||
// of our ListenAddrs. If we Dial out using an ephemeral addr, knowing that
|
||||
// address's external mapping is not very useful because the port will not be
|
||||
// the same as the listen addr.
|
||||
ifaceaddrs, err := ids.Host.Network().InterfaceListenAddresses()
|
||||
if err != nil {
|
||||
log.Infof("failed to get interface listen addrs", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Debugf("identify identifying observed multiaddr: %s %s", c.LocalMultiaddr(), ifaceaddrs)
|
||||
if !addrInAddrs(c.LocalMultiaddr(), ifaceaddrs) {
|
||||
// not in our list
|
||||
return
|
||||
}
|
||||
|
||||
// ok! we have the observed version of one of our ListenAddresses!
|
||||
log.Debugf("added own observed listen addr: %s --> %s", c.LocalMultiaddr(), maddr)
|
||||
ids.Host.Peerstore().AddAddress(ids.Host.ID(), maddr)
|
||||
}
|
||||
|
||||
func addrInAddrs(a ma.Multiaddr, as []ma.Multiaddr) bool {
|
||||
for _, b := range as {
|
||||
if a.Equal(b) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user