doPortMapping(): don't set the external IP

because some IGDs can't handle it
This commit is contained in:
Ștefan Talpalaru 2019-12-12 01:57:05 +01:00
parent e0e49b1711
commit 655fc43751
No known key found for this signature in database
GPG Key ID: CBF7934204F1B6F9
1 changed files with 6 additions and 6 deletions

View File

@ -27,7 +27,6 @@ var
upnp {.threadvar.}: Miniupnp upnp {.threadvar.}: Miniupnp
npmp {.threadvar.}: NatPmp npmp {.threadvar.}: NatPmp
strategy = NatNone strategy = NatNone
externalIP {.threadvar.}: IPAddress
internalTcpPort: Port internalTcpPort: Port
externalTcpPort: Port externalTcpPort: Port
internalUdpPort: Port internalUdpPort: Port
@ -39,6 +38,8 @@ logScope:
## Also does threadvar initialisation. ## Also does threadvar initialisation.
## Must be called before redirectPorts() in each thread. ## Must be called before redirectPorts() in each thread.
proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress] = proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress] =
var externalIP: IPAddress
if natStrategy == NatAny or natStrategy == NatUpnp: if natStrategy == NatAny or natStrategy == NatUpnp:
upnp = newMiniupnp() upnp = newMiniupnp()
upnp.discoverDelay = UPNP_TIMEOUT upnp.discoverDelay = UPNP_TIMEOUT
@ -107,8 +108,7 @@ proc doPortMapping(tcpPort, udpPort: Port, description: string): Option[(Port, P
internalHost = upnp.lanAddr, internalHost = upnp.lanAddr,
internalPort = $port, internalPort = $port,
desc = description, desc = description,
leaseDuration = 0, leaseDuration = 0)
externalIP = $externalIP)
if pmres.isErr: if pmres.isErr:
error "UPnP port mapping", msg = pmres.error error "UPnP port mapping", msg = pmres.error
return return
@ -161,14 +161,14 @@ proc repeatPortMapping(args: PortMappingArgs) {.thread.} =
# We can't use copies of Miniupnp and NatPmp objects in this thread, because they share # We can't use copies of Miniupnp and NatPmp objects in this thread, because they share
# C pointers with other instances that have already been garbage collected, so # C pointers with other instances that have already been garbage collected, so
# we use threadvars instead and initialise them again with getExternalIP(). # we use threadvars instead and initialise them again with getExternalIP(),
# even though we don't need the external IP's value.
let ipres = getExternalIP(strategy, quiet = true) let ipres = getExternalIP(strategy, quiet = true)
if ipres.isSome: if ipres.isSome:
externalIP = ipres.get()
while true: while true:
# we're being silly here with this channel polling because we can't # we're being silly here with this channel polling because we can't
# select on Nim channels like on Go ones # select on Nim channels like on Go ones
let (dataAvailable, data) = natCloseChan.tryRecv() let (dataAvailable, _) = natCloseChan.tryRecv()
if dataAvailable: if dataAvailable:
return return
else: else: