From 2262ceb1d18772bcfc8103b9e911e174dcfdb6a8 Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Sun, 26 Mar 2023 21:21:04 -0700 Subject: [PATCH] Implement usage of the new Tailscale channel library. --- .../foundation/chaos/FoundationChaosPlugin.kt | 2 +- .../core/features/persist/XodusExtensions.kt | 2 +- foundation-tailscale/build.gradle.kts | 1 + .../tailscale/TailscaleProxyServer.kt | 44 ++++--------------- settings.gradle.kts | 3 +- 5 files changed, 13 insertions(+), 39 deletions(-) diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt index f582082..0caeb45 100644 --- a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt @@ -18,7 +18,7 @@ class FoundationChaosPlugin : BaseFoundationPlugin() { config = loadConfigurationWithDefault( foundation, ChaosConfig.serializer(), - "heimdall.yaml" + "chaos.yaml" ) registerCommandExecutor("chaos", ChaosToggleCommand()) } diff --git a/foundation-core/src/main/kotlin/gay/pizza/foundation/core/features/persist/XodusExtensions.kt b/foundation-core/src/main/kotlin/gay/pizza/foundation/core/features/persist/XodusExtensions.kt index e961c04..aa4a4d9 100644 --- a/foundation-core/src/main/kotlin/gay/pizza/foundation/core/features/persist/XodusExtensions.kt +++ b/foundation-core/src/main/kotlin/gay/pizza/foundation/core/features/persist/XodusExtensions.kt @@ -2,6 +2,6 @@ package gay.pizza.foundation.core.features.persist import jetbrains.exodus.entitystore.Entity -fun > Entity.setAllProperties(vararg entries: Pair) = entries.forEach { entry -> +fun > Entity.setAllProperties(vararg entries: Pair): Unit = entries.forEach { entry -> setProperty(entry.first, entry.second) } diff --git a/foundation-tailscale/build.gradle.kts b/foundation-tailscale/build.gradle.kts index ba52419..c1e7e4b 100644 --- a/foundation-tailscale/build.gradle.kts +++ b/foundation-tailscale/build.gradle.kts @@ -13,6 +13,7 @@ dependencies { implementation(project(":common-plugin")) compileOnly(project(":foundation-shared")) implementation(libs.tailscale) + implementation(libs.tailscale.channel) } concreteItem { diff --git a/foundation-tailscale/src/main/kotlin/gay/pizza/foundation/tailscale/TailscaleProxyServer.kt b/foundation-tailscale/src/main/kotlin/gay/pizza/foundation/tailscale/TailscaleProxyServer.kt index 74fc08d..f9d9e06 100644 --- a/foundation-tailscale/src/main/kotlin/gay/pizza/foundation/tailscale/TailscaleProxyServer.kt +++ b/foundation-tailscale/src/main/kotlin/gay/pizza/foundation/tailscale/TailscaleProxyServer.kt @@ -1,16 +1,13 @@ package gay.pizza.foundation.tailscale +import gay.pizza.tailscale.channel.ChannelCopier import gay.pizza.tailscale.core.Tailscale import gay.pizza.tailscale.core.TailscaleConn import gay.pizza.tailscale.core.TailscaleListener import org.bukkit.Server import java.net.InetSocketAddress import java.net.StandardSocketOptions -import java.nio.ByteBuffer -import java.nio.channels.ClosedChannelException -import java.nio.channels.ReadableByteChannel import java.nio.channels.SocketChannel -import java.nio.channels.WritableByteChannel class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) { private var minecraftServerListener: TailscaleListener? = null @@ -34,38 +31,13 @@ class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) { val readChannel = conn.openReadChannel() val writeChannel = conn.openWriteChannel() - fun closeAll() { - socketChannel.close() - readChannel.close() - writeChannel.close() - } - - fun startCopyThread(name: String, from: ReadableByteChannel, to: WritableByteChannel) { - val thread = Thread { - try { - while (true) { - val buffer = ByteBuffer.allocate(2048) - val size = from.read(buffer) - if (size < 0) { - break - } else { - buffer.flip() - to.write(buffer) - } - buffer.clear() - } - } catch (_: ClosedChannelException) { - } finally { - closeAll() - } - } - - thread.name = name - thread.start() - } - - startCopyThread("Tailscale to Socket Pipe", readChannel, socketChannel) - startCopyThread("Socket to Tailscale Pipe", socketChannel, writeChannel) + val closeHandler = { socketChannel.close() } + val tailscaleSocketCopier = ChannelCopier(readChannel, socketChannel) + tailscaleSocketCopier.spawnCopyThread( + "Tailscale to Socket Copier", onClose = closeHandler) + val socketTailscaleCopier = ChannelCopier(socketChannel, writeChannel) + socketTailscaleCopier.spawnCopyThread( + "Socket to Tailscale Copier", onClose = closeHandler) } fun close() { diff --git a/settings.gradle.kts b/settings.gradle.kts index b94303b..3caa643 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -73,7 +73,7 @@ dependencyResolutionManagement { version("postgresql", "42.5.3") version("exposed", "0.41.1") version("hikaricp", "5.0.1") - version("libtailscale", "0.1.2-SNAPSHOT") + version("libtailscale", "0.1.4-SNAPSHOT") library("clikt", "com.github.ajalt.clikt", "clikt").versionRef("clikt") library("xodus-core", "org.jetbrains.xodus", "xodus-openAPI").versionRef("xodus") @@ -93,6 +93,7 @@ dependencyResolutionManagement { library("exposed-java-time", "org.jetbrains.exposed", "exposed-java-time").versionRef("exposed") library("hikaricp", "com.zaxxer", "HikariCP").versionRef("hikaricp") library("tailscale", "gay.pizza.tailscale", "tailscale").versionRef("libtailscale") + library("tailscale-channel", "gay.pizza.tailscale", "tailscale-channel").versionRef("libtailscale") } } }