Skip to content

Commit 3521c90

Browse files
committed
fix: 修正ktlint
1 parent 2bf93a7 commit 3521c90

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

src/main/kotlin/com/bangbang93/openbmclapi/agent/nat/NatMapper.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,3 @@ data class MappingHandle(
3434
val ttlSeconds: Int,
3535
val description: String,
3636
)
37-
38-

src/main/kotlin/com/bangbang93/openbmclapi/agent/nat/NatService.kt

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ class NatService(
1919
fun startIfEnabled(): InetAddress? {
2020
if (!config.enableUpnp) return null
2121

22-
val mapped = mapper.map(
23-
privatePort = config.port,
24-
publicPort = config.clusterPublicPort,
25-
protocol = Protocol.TCP,
26-
ttlSeconds = 3600,
27-
description = "openbmclapi",
28-
)
22+
val mapped =
23+
mapper.map(
24+
privatePort = config.port,
25+
publicPort = config.clusterPublicPort,
26+
protocol = Protocol.TCP,
27+
ttlSeconds = 3600,
28+
description = "openbmclapi",
29+
)
2930
handle = mapped
3031
external = mapper.externalIp()
3132

@@ -34,12 +35,17 @@ class NatService(
3435
logger.info { "UPnP/NAT 端口映射成功,外网 IP: ${ip.hostAddress}" }
3536

3637
// 关闭时清理端口映射
37-
Runtime.getRuntime().addShutdownHook(Thread({
38-
try {
39-
handle?.let { mapper.unmap(it) }
40-
} catch (_: Exception) {
41-
}
42-
}, "nat-unmap"))
38+
Runtime.getRuntime().addShutdownHook(
39+
Thread(
40+
{
41+
try {
42+
handle?.let { mapper.unmap(it) }
43+
} catch (_: Exception) {
44+
}
45+
},
46+
"nat-unmap",
47+
),
48+
)
4349

4450
return ip
4551
}

src/main/kotlin/com/bangbang93/openbmclapi/agent/nat/WeUpnpNatMapper.kt

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.bangbang93.openbmclapi.agent.nat
22

33
import io.github.oshai.kotlinlogging.KotlinLogging
4-
import java.net.InetAddress
54
import org.bitlet.weupnp.GatewayDevice
65
import org.bitlet.weupnp.GatewayDiscover
6+
import java.net.InetAddress
77

88
private val logger = KotlinLogging.logger {}
99

@@ -22,10 +22,25 @@ class WeUpnpNatMapper : NatMapper {
2222
val gw = discover.validGateway ?: error("未发现可用的 UPnP IGD 网关")
2323
device = gw
2424

25-
val ok = when (protocol) {
26-
Protocol.TCP -> gw.addPortMapping(publicPort, privatePort, gw.localAddress.hostAddress, "TCP", description)
27-
Protocol.UDP -> gw.addPortMapping(publicPort, privatePort, gw.localAddress.hostAddress, "UDP", description)
28-
}
25+
val ok =
26+
when (protocol) {
27+
Protocol.TCP ->
28+
gw.addPortMapping(
29+
publicPort,
30+
privatePort,
31+
gw.localAddress.hostAddress,
32+
"TCP",
33+
description,
34+
)
35+
Protocol.UDP ->
36+
gw.addPortMapping(
37+
publicPort,
38+
privatePort,
39+
gw.localAddress.hostAddress,
40+
"UDP",
41+
description,
42+
)
43+
}
2944
if (!ok) error("UPnP 端口映射失败: $protocol $publicPort->$privatePort")
3045

3146
logger.info { "UPnP 映射成功: $protocol $publicPort->$privatePort" }
@@ -37,8 +52,22 @@ class WeUpnpNatMapper : NatMapper {
3752
return try {
3853
val dev = device ?: return false
3954
when (handle.protocol) {
40-
Protocol.TCP -> dev.addPortMapping(handle.publicPort, handle.privatePort, dev.localAddress.hostAddress, "TCP", handle.description)
41-
Protocol.UDP -> dev.addPortMapping(handle.publicPort, handle.privatePort, dev.localAddress.hostAddress, "UDP", handle.description)
55+
Protocol.TCP ->
56+
dev.addPortMapping(
57+
handle.publicPort,
58+
handle.privatePort,
59+
dev.localAddress.hostAddress,
60+
"TCP",
61+
handle.description,
62+
)
63+
Protocol.UDP ->
64+
dev.addPortMapping(
65+
handle.publicPort,
66+
handle.privatePort,
67+
dev.localAddress.hostAddress,
68+
"UDP",
69+
handle.description,
70+
)
4271
}
4372
true
4473
} catch (e: Exception) {

0 commit comments

Comments
 (0)