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.
@@ -752,14 +752,6 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
localIPv6Addr := h.localIPv6Addr
h.lipMu.RUnlock()
- var finalAddrs []ma.Multiaddr
- listenAddrs := h.Network().ListenAddresses()
- 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)
@@ -768,6 +760,16 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
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
+ }
+
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
}
