Move UPnP fields into networks context

This commit is contained in:
Kleissner
2021-12-29 05:50:33 +01:00
parent 5d598c13f6
commit 9a9a4919e8
2 changed files with 12 additions and 12 deletions

View File

@@ -11,17 +11,13 @@ package core
import (
"math/rand"
"net"
"sync"
"time"
"github.com/PeernetOfficial/core/upnp"
)
var upnpListInterfaces map[string]struct{}
var upnpMutex sync.RWMutex
func (nets *Networks) startUPnP() {
upnpListInterfaces = make(map[string]struct{})
nets.upnpListInterfaces = make(map[string]struct{})
for _, cidr := range []string{
"10.0.0.0/8", // RFC1918
@@ -103,11 +99,11 @@ func (network *Network) upnpAuto() {
network.ipExternal = externalIP
// Only allow 1 UPnP worker at a time for registering the adapter.
upnpMutex.Lock()
defer upnpMutex.Unlock()
network.networkGroup.upnpMutex.Lock()
defer network.networkGroup.upnpMutex.Unlock()
// If there is already a running UPnP on the adapter, skip.
if _, ok := upnpListInterfaces[network.GetAdapterName()]; ok {
if _, ok := network.networkGroup.upnpListInterfaces[network.GetAdapterName()]; ok {
return
}
@@ -115,7 +111,7 @@ func (network *Network) upnpAuto() {
return
}
upnpListInterfaces[network.GetAdapterName()] = struct{}{}
network.networkGroup.upnpListInterfaces[network.GetAdapterName()] = struct{}{}
go network.upnpMonitorPortForward()
}
@@ -157,9 +153,9 @@ monitorLoop:
ticker.Stop()
upnpMutex.Lock()
delete(upnpListInterfaces, network.GetAdapterName())
upnpMutex.Unlock()
network.networkGroup.upnpMutex.Lock()
delete(network.networkGroup.upnpListInterfaces, network.GetAdapterName())
network.networkGroup.upnpMutex.Unlock()
}
func (network *Network) upnpTryPortForward() (err error) {

View File

@@ -36,6 +36,10 @@ type Networks struct {
// localFirewall indicates if a local firewall may drop unsolicited incoming packets
localFirewall bool
// UPnP data
upnpListInterfaces map[string]struct{}
upnpMutex sync.RWMutex
// backend
backend *Backend
}