From 7befd0ef47872e9e71f85b48a865ff4b47684495 Mon Sep 17 00:00:00 2001 From: john Date: Tue, 25 Mar 2025 19:41:12 +0900 Subject: [PATCH] p2p/nat: fix to ensure the newly assigned port from AddAnyPortMapping is returned --- p2p/nat/natupnp.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/p2p/nat/natupnp.go b/p2p/nat/natupnp.go index f1bb955892..204b09f757 100644 --- a/p2p/nat/natupnp.go +++ b/p2p/nat/natupnp.go @@ -95,13 +95,19 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li return uint16(extport), nil } - return uint16(extport), n.withRateLimit(func() error { + var newExtport uint16 + err = n.withRateLimit(func() error { p, err := n.addAnyPortMapping(protocol, extport, intport, ip, desc, lifetimeS) if err == nil { - extport = int(p) + newExtport = p } return err }) + + if err != nil { + return uint16(extport), err + } + return newExtport, nil } func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.IP, desc string, lifetimeS uint32) (uint16, error) {