Make Tailscale work properly.

This commit is contained in:
2023-03-19 15:56:25 -07:00
parent 1035c83166
commit 59fbea0a37

View File

@ -5,6 +5,7 @@ import gay.pizza.tailscale.core.TailscaleConn
import gay.pizza.tailscale.core.TailscaleListener import gay.pizza.tailscale.core.TailscaleListener
import org.bukkit.Server import org.bukkit.Server
import java.net.InetSocketAddress import java.net.InetSocketAddress
import java.net.StandardSocketOptions
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.channels.ClosedChannelException import java.nio.channels.ClosedChannelException
import java.nio.channels.ReadableByteChannel import java.nio.channels.ReadableByteChannel
@ -27,25 +28,28 @@ class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) {
} }
fun handleServerConnection(conn: TailscaleConn) { fun handleServerConnection(conn: TailscaleConn) {
val socketChannel = SocketChannel.open(InetSocketAddress("127.0.0.1", server.port)) val socketChannel = SocketChannel.open()
val connChannel = conn.openReadWriteChannel() 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() { fun closeAll() {
socketChannel.close() socketChannel.close()
connChannel.close() readChannel.close()
writeChannel.close()
} }
fun startCopyThread(name: String, from: ReadableByteChannel, to: WritableByteChannel) { fun startCopyThread(name: String, from: ReadableByteChannel, to: WritableByteChannel) {
val thread = Thread { val thread = Thread {
try { try {
while (from.isOpen && to.isOpen) { while (true) {
val buffer = ByteBuffer.allocate(2048) val buffer = ByteBuffer.allocate(2048)
val size = from.read(buffer) val size = from.read(buffer)
if (size < 0) { if (size < 0) {
break break
} else { } else {
buffer.flip() buffer.flip()
val array = buffer.array()
to.write(buffer) to.write(buffer)
} }
buffer.clear() buffer.clear()
@ -60,8 +64,8 @@ class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) {
thread.start() thread.start()
} }
startCopyThread("Tailscale to Socket Pipe", connChannel, socketChannel) startCopyThread("Tailscale to Socket Pipe", readChannel, socketChannel)
startCopyThread("Socket to Tailscale Pipe", socketChannel, connChannel) startCopyThread("Socket to Tailscale Pipe", socketChannel, writeChannel)
} }
fun close() { fun close() {