SOCKS5 proxy, implement #100

This commit is contained in:
creeper123123321 2021-03-25 20:58:49 -03:00
parent e73175b7c5
commit 14e5ae62bc
5 changed files with 33 additions and 12 deletions

View File

@ -40,4 +40,7 @@ object VIAaaSConfig : Config(File("config/viaaas.yml")) {
val rateLimitWs: Double get() = this.getDouble("rate-limit-ws", 1.0) val rateLimitWs: Double get() = this.getDouble("rate-limit-ws", 1.0)
val rateLimitConnectionMc: Double get() = this.getDouble("rate-limit-connection-mc", 10.0) val rateLimitConnectionMc: Double get() = this.getDouble("rate-limit-connection-mc", 10.0)
val listeningWsLimit: Int get() = this.getInt("listening-ws-limit", 16) val listeningWsLimit: Int get() = this.getInt("listening-ws-limit", 16)
val backendSocks5ProxyAddress: String?
get() = this.getString("backend-socks5-proxy-address", "")!!.let { if (it.isEmpty()) null else it }
val backendSocks5ProxyPort: Int get() = this.getInt("backend-socks5-proxy-port", 9050)
} }

View File

@ -14,7 +14,7 @@ class BackEndInit(val connectionData: ConnectionData) : ChannelInitializer<Chann
override fun initChannel(ch: Channel) { override fun initChannel(ch: Channel) {
val user = UserConnection(ch, true) val user = UserConnection(ch, true)
ProtocolPipeline(user) ProtocolPipeline(user)
ch.pipeline() ch.pipeline().also { addSocks5(it) }
// "crypto" // "crypto"
.addLast("frame", FrameCodec()) .addLast("frame", FrameCodec())
// compress // compress

View File

@ -1,11 +1,22 @@
package com.github.creeper123123321.viaaas.handler package com.github.creeper123123321.viaaas.handler
import com.github.creeper123123321.viaaas.config.VIAaaSConfig
import com.github.creeper123123321.viaaas.packet.Packet import com.github.creeper123123321.viaaas.packet.Packet
import com.github.creeper123123321.viaaas.send import com.github.creeper123123321.viaaas.send
import io.netty.channel.ChannelPipeline
import io.netty.handler.proxy.Socks5ProxyHandler
import us.myles.ViaVersion.api.protocol.ProtocolVersion import us.myles.ViaVersion.api.protocol.ProtocolVersion
import java.net.InetSocketAddress
fun forward(handler: MinecraftHandler, packet: Packet, flush: Boolean = false) { fun forward(handler: MinecraftHandler, packet: Packet, flush: Boolean = false) {
send(handler.other!!, packet, flush) send(handler.other!!, packet, flush)
} }
fun is1_7(handler: MinecraftHandler) = handler.data.frontVer!! <= ProtocolVersion.v1_7_6.version fun is1_7(handler: MinecraftHandler) = handler.data.frontVer!! <= ProtocolVersion.v1_7_6.version
fun addSocks5(pipe: ChannelPipeline) {
val addr = VIAaaSConfig.backendSocks5ProxyAddress
if (addr != null) {
pipe.addFirst(Socks5ProxyHandler(InetSocketAddress(addr, VIAaaSConfig.backendSocks5ProxyPort)))
}
}

View File

@ -6,23 +6,26 @@ import com.github.creeper123123321.viaaas.codec.FrameCodec
import com.github.creeper123123321.viaaas.codec.MinecraftCodec import com.github.creeper123123321.viaaas.codec.MinecraftCodec
import com.github.creeper123123321.viaaas.handler.ConnectionData import com.github.creeper123123321.viaaas.handler.ConnectionData
import com.github.creeper123123321.viaaas.handler.MinecraftHandler import com.github.creeper123123321.viaaas.handler.MinecraftHandler
import com.github.creeper123123321.viaaas.packet.handshake.Handshake import com.github.creeper123123321.viaaas.handler.addSocks5
import com.github.creeper123123321.viaaas.mcLogger import com.github.creeper123123321.viaaas.mcLogger
import com.github.creeper123123321.viaaas.packet.handshake.Handshake
import com.github.creeper123123321.viaaas.packet.status.StatusRequest import com.github.creeper123123321.viaaas.packet.status.StatusRequest
import com.github.creeper123123321.viaaas.send import com.github.creeper123123321.viaaas.send
import java.util.concurrent.CompletableFuture
import java.net.InetSocketAddress
import us.myles.ViaVersion.api.protocol.ProtocolVersion
import io.netty.handler.timeout.ReadTimeoutHandler
import io.netty.channel.socket.nio.NioSocketChannel
import com.google.common.cache.CacheLoader
import java.util.concurrent.TimeUnit
import com.google.common.cache.CacheBuilder import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import io.netty.bootstrap.Bootstrap import io.netty.bootstrap.Bootstrap
import io.netty.channel.* import io.netty.channel.Channel
import io.netty.channel.ChannelFuture
import io.netty.channel.ChannelInitializer
import io.netty.channel.ChannelOption
import io.netty.handler.timeout.ReadTimeoutHandler
import io.netty.util.concurrent.Future import io.netty.util.concurrent.Future
import us.myles.ViaVersion.api.protocol.ProtocolVersion
import us.myles.ViaVersion.packets.State import us.myles.ViaVersion.packets.State
import java.net.InetSocketAddress
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ExecutionException import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
object ProtocolDetector { object ProtocolDetector {
private val SERVER_VER = CacheBuilder.newBuilder() private val SERVER_VER = CacheBuilder.newBuilder()
@ -38,7 +41,7 @@ object ProtocolDetector {
.handler(object : ChannelInitializer<Channel>() { .handler(object : ChannelInitializer<Channel>() {
override fun initChannel(channel: Channel) { override fun initChannel(channel: Channel) {
val data = ConnectionData(channel, state = ProtocolDetectionState(future), frontVer = -1) val data = ConnectionData(channel, state = ProtocolDetectionState(future), frontVer = -1)
channel.pipeline() channel.pipeline().also { addSocks5(it) }
.addLast("timeout", ReadTimeoutHandler(30, TimeUnit.SECONDS)) .addLast("timeout", ReadTimeoutHandler(30, TimeUnit.SECONDS))
.addLast("frame", FrameCodec()) .addLast("frame", FrameCodec())
.addLast("mc", MinecraftCodec()) .addLast("mc", MinecraftCodec())

View File

@ -12,6 +12,10 @@ port: 25565
bind-address: localhost bind-address: localhost
# Use Netty native transport for Minecraft connections when available. # Use Netty native transport for Minecraft connections when available.
native-transport-mc: true native-transport-mc: true
# Address of SOCKS5 proxy used for connecting to backend servers. Empty to disable.
backend-socks5-proxy-address: ""
# Port of SOCKS5 proxy used for connecting to backend servers.
backend-socks5-proxy-port: 9050
# #
###### ######
# Crypto # Crypto