net/portmapper: return correct upnp port

Previously, this was incorrectly returning the internal port, and using that with the external
exposed IP when it did not use WANIPConnection2. In the case when we must provide a port, we
return it instead.

Noticed this while implementing the integration test for upnp.

Signed-off-by: julianknodt <julianknodt@gmail.com>
pull/2483/head
julianknodt 3 years ago committed by Julian Knodt
parent a5fb8e0731
commit 3a4201e773

@ -7,6 +7,7 @@ package portmapper
import ( import (
"context" "context"
"fmt" "fmt"
"math/rand"
"net/url" "net/url"
"time" "time"
@ -86,6 +87,8 @@ const tsPortMappingDesc = "tailscale-portmap"
// addAnyPortMapping abstracts over different UPnP client connections, calling the available // addAnyPortMapping abstracts over different UPnP client connections, calling the available
// AddAnyPortMapping call if available for WAN IP connection v2, otherwise defaulting to the old // AddAnyPortMapping call if available for WAN IP connection v2, otherwise defaulting to the old
// behavior of calling AddPortMapping with port = 0 to specify a wildcard port. // behavior of calling AddPortMapping with port = 0 to specify a wildcard port.
// It returns the new external port (which may not be identical to the external port specified),
// or an error.
func addAnyPortMapping( func addAnyPortMapping(
ctx context.Context, ctx context.Context,
upnp upnpClient, upnp upnpClient,
@ -107,6 +110,9 @@ func addAnyPortMapping(
uint32(leaseDuration.Seconds()), uint32(leaseDuration.Seconds()),
) )
} }
for externalPort == 0 {
externalPort = uint16(rand.Intn(65535))
}
err = upnp.AddPortMapping( err = upnp.AddPortMapping(
ctx, ctx,
"", "",
@ -118,7 +124,7 @@ func addAnyPortMapping(
tsPortMappingDesc, tsPortMappingDesc,
uint32(leaseDuration.Seconds()), uint32(leaseDuration.Seconds()),
) )
return internalPort, err return externalPort, err
} }
// getUPnPClients gets a client for interfacing with UPnP, ignoring the underlying protocol for // getUPnPClients gets a client for interfacing with UPnP, ignoring the underlying protocol for

Loading…
Cancel
Save