From 3a502a1ecc950a2b9731331c3d366d82de7a981d Mon Sep 17 00:00:00 2001 From: Kleissner Date: Fri, 7 May 2021 01:05:02 +0200 Subject: [PATCH] Minor UPnP improvement: Run discovery of each network in parallel. --- Network UPnP.go | 21 +++++++++++---------- Network.go | 4 ++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/Network UPnP.go b/Network UPnP.go index 7938b44..d3029bc 100644 --- a/Network UPnP.go +++ b/Network UPnP.go @@ -4,7 +4,6 @@ Copyright: 2021 Peernet s.r.o. Author: Peter Kleissner Currently only supports IPv4 networks. -TODO: Limit mapping to X hours. Auto remap upon expiration. */ package core @@ -60,15 +59,6 @@ func (network *Network) upnpAuto() { return } - // Only allow 1 UPnP worker at a time. - upnpMutex.Lock() - defer upnpMutex.Unlock() - - // If there is already a running UPnP on the adapter, skip. - if _, ok := upnpListInterfaces[network.GetAdapterName()]; ok { - return - } - nat, err := upnp.Discover(network.address.IP) if err != nil { return @@ -83,6 +73,15 @@ func (network *Network) upnpAuto() { network.ipExternal = externalIP + // Only allow 1 UPnP worker at a time for registering the adapter. + upnpMutex.Lock() + defer upnpMutex.Unlock() + + // If there is already a running UPnP on the adapter, skip. + if _, ok := upnpListInterfaces[network.GetAdapterName()]; ok { + return + } + if err := network.upnpTryPortForward(); err != nil { return } @@ -163,3 +162,5 @@ func (network *Network) upnpValidate() (err error) { // TODO: Send special message which validates the UPnP mapping return nil } + +// TODO: Function to check if there is an existing port forward diff --git a/Network.go b/Network.go index 4ca99ea..cb8472c 100644 --- a/Network.go +++ b/Network.go @@ -241,8 +241,8 @@ func GetNetworks(networkType int) (networks []*Network) { } // GetListen returns connectivity information -func (network *Network) GetListen() (listen *net.UDPAddr, multicastIPv6 net.IP, broadcastIPv4 []net.IP, portExternal uint16) { - return network.address, network.multicastIP, network.broadcastIPv4, network.portExternal +func (network *Network) GetListen() (listen *net.UDPAddr, multicastIPv6 net.IP, broadcastIPv4 []net.IP, ipExternal net.IP, portExternal uint16) { + return network.address, network.multicastIP, network.broadcastIPv4, network.ipExternal, network.portExternal } // GetAdapterName returns the adapter name, if available