Minor UPnP improvement: Run discovery of each network in parallel.

This commit is contained in:
Kleissner
2021-05-07 01:05:02 +02:00
parent 5f8f299799
commit 3a502a1ecc
2 changed files with 13 additions and 12 deletions

View File

@@ -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

View File

@@ -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