Compare commits
2 Commits
18bbb93873
...
9b0d30e5ae
Author | SHA1 | Date | |
---|---|---|---|
|
9b0d30e5ae | ||
|
02cbad1bff |
@ -1,65 +1,53 @@
|
||||
@file:OptIn(DelicateCoroutinesApi::class)
|
||||
package org.muellerssoftware.openproximitychat.common
|
||||
|
||||
import org.bitlet.weupnp.GatewayDevice
|
||||
import org.bitlet.weupnp.GatewayDiscover
|
||||
import kotlinx.coroutines.*
|
||||
import org.slf4j.Logger
|
||||
|
||||
object UPnPManager {
|
||||
var gateway: GatewayDevice? = null
|
||||
val mappedPorts = mutableMapOf<Int, Int>()
|
||||
var discoveryJob: Job = Job()
|
||||
|
||||
init {
|
||||
runBlocking {
|
||||
discoveryJob = GlobalScope.launch {
|
||||
startGatewayDiscover()
|
||||
}
|
||||
}
|
||||
object Logging {
|
||||
enum class LogType {
|
||||
Debug,
|
||||
Info,
|
||||
Error
|
||||
}
|
||||
|
||||
suspend fun startGatewayDiscover() {
|
||||
val gatewayDiscoverer = GatewayDiscover()
|
||||
for (i in 0..9) {
|
||||
Logging.info("Searching for UPnP gateway... (Attempt ${i + 1})")
|
||||
gatewayDiscoverer.discover()
|
||||
if (gatewayDiscoverer.getValidGateway() != null) {
|
||||
gateway = gatewayDiscoverer.validGateway
|
||||
break
|
||||
var logger: Logger? = null
|
||||
set(value) {
|
||||
for (log in backLog) {
|
||||
when (log.first) {
|
||||
LogType.Debug -> value?.debug(log.second)
|
||||
LogType.Info -> value?.info(log.second)
|
||||
LogType.Error -> value?.error(log.second)
|
||||
}
|
||||
}
|
||||
|
||||
field = value
|
||||
}
|
||||
|
||||
if (gateway == null) {
|
||||
Logging.error("No UPnP gateway found!")
|
||||
var backLog = mutableListOf<Pair<LogType, String>>()
|
||||
|
||||
fun info(message: String) {
|
||||
if (logger != null) {
|
||||
logger!!.info(message)
|
||||
}
|
||||
else {
|
||||
Logging.info("UPnP gateway found: ${gateway!!.friendlyName}")
|
||||
val ports = mappedPorts.toMutableMap()
|
||||
mappedPorts.clear()
|
||||
ports.map {
|
||||
mapPort(it.key, it.value)
|
||||
}
|
||||
backLog.add(Pair(LogType.Info, message))
|
||||
}
|
||||
}
|
||||
|
||||
fun mapPort(port: Int) {
|
||||
mapPort(port, port)
|
||||
}
|
||||
|
||||
fun mapPort(internalPort: Int, externalPort: Int) {
|
||||
if (gateway == null) {
|
||||
mappedPorts.put(internalPort, externalPort)
|
||||
fun error(message: String) {
|
||||
if (logger != null) {
|
||||
logger!!.error(message)
|
||||
}
|
||||
|
||||
val succeeded = gateway?.addPortMapping(externalPort, internalPort, gateway!!.localAddress.hostAddress, "UDP", "OpenProximityChat mapped port")
|
||||
if (succeeded == true) {
|
||||
mappedPorts.put(internalPort, externalPort)
|
||||
else {
|
||||
backLog.add(Pair(LogType.Error, message))
|
||||
}
|
||||
}
|
||||
|
||||
fun unMapAll() {
|
||||
mappedPorts.map {
|
||||
gateway?.deletePortMapping(it.value, "UDP")
|
||||
fun debug(message: String) {
|
||||
if (logger != null) {
|
||||
logger!!.debug(message)
|
||||
}
|
||||
else {
|
||||
backLog.add(Pair(LogType.Debug, message))
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Int, Int>()
|
||||
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user