mirror of
				https://github.com/GayPizzaSpecifications/foundation.git
				synced 2025-11-04 03:39:37 +00:00 
			
		
		
		
	Add option to use /proc/self/fd for Tailscale connections.
This commit is contained in:
		@ -9,5 +9,6 @@ data class TailscaleConfig(
 | 
				
			|||||||
  val controlUrl: String? = null,
 | 
					  val controlUrl: String? = null,
 | 
				
			||||||
  val authKey: String? = null,
 | 
					  val authKey: String? = null,
 | 
				
			||||||
  val tailscalePath: String? = null,
 | 
					  val tailscalePath: String? = null,
 | 
				
			||||||
  val ephemeral: Boolean = false
 | 
					  val ephemeral: Boolean = false,
 | 
				
			||||||
 | 
					  val useProcSelfFd: Boolean = false
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
				
			|||||||
@ -12,6 +12,7 @@ class TailscaleController(val server: Server, val config: TailscaleConfig) {
 | 
				
			|||||||
    if (!config.enabled) {
 | 
					    if (!config.enabled) {
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    tailscale.useProcSelfFd = config.useProcSelfFd
 | 
				
			||||||
    tailscale.hostname = config.hostname
 | 
					    tailscale.hostname = config.hostname
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (config.controlUrl != null) {
 | 
					    if (config.controlUrl != null) {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,16 +1,13 @@
 | 
				
			|||||||
package gay.pizza.foundation.tailscale
 | 
					package gay.pizza.foundation.tailscale
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import gay.pizza.tailscale.channel.ChannelCopier
 | 
				
			||||||
import gay.pizza.tailscale.core.Tailscale
 | 
					import gay.pizza.tailscale.core.Tailscale
 | 
				
			||||||
import gay.pizza.tailscale.core.TailscaleConn
 | 
					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.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.SocketChannel
 | 
				
			||||||
import java.nio.channels.WritableByteChannel
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) {
 | 
					class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) {
 | 
				
			||||||
  private var minecraftServerListener: TailscaleListener? = null
 | 
					  private var minecraftServerListener: TailscaleListener? = null
 | 
				
			||||||
@ -34,38 +31,13 @@ class TailscaleProxyServer(val server: Server, val tailscale: Tailscale) {
 | 
				
			|||||||
    val readChannel = conn.openReadChannel()
 | 
					    val readChannel = conn.openReadChannel()
 | 
				
			||||||
    val writeChannel = conn.openWriteChannel()
 | 
					    val writeChannel = conn.openWriteChannel()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun closeAll() {
 | 
					    val closeHandler = { socketChannel.close() }
 | 
				
			||||||
      socketChannel.close()
 | 
					    val tailscaleSocketCopier = ChannelCopier(readChannel, socketChannel)
 | 
				
			||||||
      readChannel.close()
 | 
					    tailscaleSocketCopier.spawnCopyThread(
 | 
				
			||||||
      writeChannel.close()
 | 
					      "Tailscale to Socket Copier", onClose = closeHandler)
 | 
				
			||||||
    }
 | 
					    val socketTailscaleCopier = ChannelCopier(socketChannel, writeChannel)
 | 
				
			||||||
 | 
					    socketTailscaleCopier.spawnCopyThread(
 | 
				
			||||||
    fun startCopyThread(name: String, from: ReadableByteChannel, to: WritableByteChannel) {
 | 
					      "Socket to Tailscale Copier", onClose = closeHandler)
 | 
				
			||||||
      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)
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  fun close() {
 | 
					  fun close() {
 | 
				
			||||||
 | 
				
			|||||||
@ -73,7 +73,7 @@ dependencyResolutionManagement {
 | 
				
			|||||||
      version("postgresql", "42.5.3")
 | 
					      version("postgresql", "42.5.3")
 | 
				
			||||||
      version("exposed", "0.41.1")
 | 
					      version("exposed", "0.41.1")
 | 
				
			||||||
      version("hikaricp", "5.0.1")
 | 
					      version("hikaricp", "5.0.1")
 | 
				
			||||||
      version("libtailscale", "0.1.4-SNAPSHOT")
 | 
					      version("libtailscale", "0.1.5-SNAPSHOT")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      library("clikt", "com.github.ajalt.clikt", "clikt").versionRef("clikt")
 | 
					      library("clikt", "com.github.ajalt.clikt", "clikt").versionRef("clikt")
 | 
				
			||||||
      library("xodus-core", "org.jetbrains.xodus", "xodus-openAPI").versionRef("xodus")
 | 
					      library("xodus-core", "org.jetbrains.xodus", "xodus-openAPI").versionRef("xodus")
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user