From 9b0d30e5aee16e210996d6f06a39a620a6dc06b4 Mon Sep 17 00:00:00 2001 From: ProtoByter Date: Fri, 11 Nov 2022 20:54:40 +0000 Subject: [PATCH] Fix stupid mistake --- .../openproximitychat/common/UPnPManager.kt | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/common/src/main/kotlin/org/muellerssoftware/openproximitychat/common/UPnPManager.kt b/common/src/main/kotlin/org/muellerssoftware/openproximitychat/common/UPnPManager.kt index 812d689..4b1fa6d 100644 --- a/common/src/main/kotlin/org/muellerssoftware/openproximitychat/common/UPnPManager.kt +++ b/common/src/main/kotlin/org/muellerssoftware/openproximitychat/common/UPnPManager.kt @@ -1,13 +1,24 @@ +@file:OptIn(DelicateCoroutinesApi::class) package org.muellerssoftware.openproximitychat.common +import kotlinx.coroutines.* import org.bitlet.weupnp.GatewayDevice import org.bitlet.weupnp.GatewayDiscover object UPnPManager { var gateway: GatewayDevice? = null val mappedPorts = mutableMapOf() + var discoveryJob: Job = Job() init { + runBlocking { + discoveryJob = GlobalScope.launch { + startGatewayDiscover() + } + } + } + + fun startGatewayDiscover() { val gatewayDiscoverer = GatewayDiscover() for (i in 0..9) { Logging.info("Searching for UPnP gateway... (Attempt ${i + 1})") @@ -23,6 +34,11 @@ object UPnPManager { } else { Logging.info("UPnP gateway found: ${gateway!!.friendlyName}") + val ports = mappedPorts.toMutableMap() + mappedPorts.clear() + ports.map { + mapPort(it.key, it.value) + } } } @@ -31,7 +47,11 @@ object UPnPManager { } fun mapPort(internalPort: Int, externalPort: Int) { - val succeeded = gateway?.addPortMapping(externalPort, internalPort, gateway!!.localAddress.hostAddress, "UDP", "OPCFabric mapped port") + if (gateway == null) { + mappedPorts.put(internalPort, externalPort) + } + + val succeeded = gateway?.addPortMapping(externalPort, internalPort, gateway!!.localAddress.hostAddress, "UDP", "OpenProximityChat mapped port") if (succeeded == true) { mappedPorts.put(internalPort, externalPort) }