From 59fbea0a377a73d7c5fc3389031022397031554e Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Sun, 19 Mar 2023 15:56:25 -0700 Subject: [PATCH] Make Tailscale work properly. --- .../tailscale/TailscaleProxyServer.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) 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 32f4a2a..74fc08d 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 @@ -5,6 +5,7 @@ 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 @@ -27,25 +28,28 @@ class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) { } fun handleServerConnection(conn: TailscaleConn) { - val socketChannel = SocketChannel.open(InetSocketAddress("127.0.0.1", server.port)) - val connChannel = conn.openReadWriteChannel() + val socketChannel = SocketChannel.open() + socketChannel.setOption(StandardSocketOptions.TCP_NODELAY, true) + socketChannel.connect(InetSocketAddress("127.0.0.1", server.port)) + val readChannel = conn.openReadChannel() + val writeChannel = conn.openWriteChannel() fun closeAll() { socketChannel.close() - connChannel.close() + readChannel.close() + writeChannel.close() } fun startCopyThread(name: String, from: ReadableByteChannel, to: WritableByteChannel) { val thread = Thread { try { - while (from.isOpen && to.isOpen) { + while (true) { val buffer = ByteBuffer.allocate(2048) val size = from.read(buffer) if (size < 0) { break } else { buffer.flip() - val array = buffer.array() to.write(buffer) } buffer.clear() @@ -60,8 +64,8 @@ class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) { thread.start() } - startCopyThread("Tailscale to Socket Pipe", connChannel, socketChannel) - startCopyThread("Socket to Tailscale Pipe", socketChannel, connChannel) + startCopyThread("Tailscale to Socket Pipe", readChannel, socketChannel) + startCopyThread("Socket to Tailscale Pipe", socketChannel, writeChannel) } fun close() {