Browse Source

fix(nat): use the right addresses when nat port mapping

We switched `listenAddrs` from the _interface_ addresses to the raw addresses we
were listening on. Unfortunately, we needed the resolved addresses later in the
function when resolving port mappings.
pull/966/head
Steven Allen 4 years ago
parent
commit
1180b6fa1e
  1. 25
      p2p/host/basic/basic_host.go

25
p2p/host/basic/basic_host.go

@ -752,22 +752,24 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
localIPv6Addr := h.localIPv6Addr
h.lipMu.RUnlock()
var finalAddrs []ma.Multiaddr
ifaceAddrs := []ma.Multiaddr{manet.IP4Loopback, manet.IP6Loopback}
if localIPv4Addr != nil {
ifaceAddrs = append(ifaceAddrs, localIPv4Addr)
}
if localIPv6Addr != nil {
ifaceAddrs = append(ifaceAddrs, localIPv6Addr)
}
// Iterate over all _unresolved_ listen addresses, resolving our primary
// interface only to avoid advertising too many addresses.
listenAddrs := h.Network().ListenAddresses()
var finalAddrs []ma.Multiaddr
for _, addr := range listenAddrs {
if !manet.IsIPUnspecified(addr) {
finalAddrs = append(finalAddrs, addr)
continue
}
ifaceAddrs := []ma.Multiaddr{manet.IP4Loopback, manet.IP6Loopback}
if localIPv4Addr != nil {
ifaceAddrs = append(ifaceAddrs, localIPv4Addr)
}
if localIPv6Addr != nil {
ifaceAddrs = append(ifaceAddrs, localIPv6Addr)
}
resolved, err := addrutil.ResolveUnspecifiedAddress(addr, ifaceAddrs)
if err == nil {
for _, r := range resolved {
@ -848,8 +850,9 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
continue
}
if !ip.IsGlobalUnicast() {
// We only map global unicast ports.
if !ip.IsGlobalUnicast() && !ip.IsUnspecified() {
// We only map global unicast & unspecified addresses ports.
// Not broadcast, multicast, etc.
continue
}

Loading…
Cancel
Save