p2p/nat: fix to ensure the newly assigned port from AddAnyPortMapping is returned

This commit is contained in:
john 2025-03-25 19:41:12 +09:00
parent a14b8eca04
commit 7befd0ef47

View file

@ -95,13 +95,19 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li
return uint16(extport), nil 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) p, err := n.addAnyPortMapping(protocol, extport, intport, ip, desc, lifetimeS)
if err == nil { if err == nil {
extport = int(p) newExtport = p
} }
return err 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) { func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.IP, desc string, lifetimeS uint32) (uint16, error) {