From 61cd7b0b4424f643e494a3821e282c706c3a0b37 Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Fri, 2 Jul 2021 18:07:30 -0300 Subject: [PATCH] fix backend address with non socks5 proxy --- .../com/viaversion/aas/handler/MinecraftHandler.kt | 9 ++++----- src/main/kotlin/com/viaversion/aas/handler/Util.kt | 12 +++++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt b/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt index c26d46e..86ab5a8 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt @@ -7,7 +7,7 @@ import com.viaversion.viaversion.exception.CancelCodecException import io.netty.channel.Channel import io.netty.channel.ChannelHandlerContext import io.netty.channel.SimpleChannelInboundHandler -import io.netty.handler.proxy.Socks5ProxyHandler +import io.netty.handler.proxy.ProxyHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.cancel @@ -24,13 +24,12 @@ class MinecraftHandler( val coroutineScope = CoroutineScope(Dispatchers.Default) override fun channelRead0(ctx: ChannelHandlerContext, packet: Packet) { - if (ctx.channel().isActive) { - data.state.handlePacket(this, ctx, packet) - } + if (!ctx.channel().isActive) return + data.state.handlePacket(this, ctx, packet) } override fun channelActive(ctx: ChannelHandlerContext) { - endRemoteAddress = ctx.channel().pipeline().get(Socks5ProxyHandler::class.java)?.destinationAddress() + endRemoteAddress = (ctx.channel().pipeline().get("proxy") as? ProxyHandler)?.destinationAddress() ?: ctx.channel().remoteAddress() } diff --git a/src/main/kotlin/com/viaversion/aas/handler/Util.kt b/src/main/kotlin/com/viaversion/aas/handler/Util.kt index ad93c0a..15a48c3 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/Util.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/Util.kt @@ -1,8 +1,8 @@ package com.viaversion.aas.handler import com.viaversion.aas.AspirinServer -import com.viaversion.aas.config.VIAaaSConfig import com.viaversion.aas.codec.packet.Packet +import com.viaversion.aas.config.VIAaaSConfig import com.viaversion.aas.readRemainingBytes import com.viaversion.aas.send import com.viaversion.viaversion.api.protocol.version.ProtocolVersion @@ -27,11 +27,13 @@ fun addProxyHandler(pipe: ChannelPipeline) { val socket = InetSocketAddress(AspirinServer.dnsResolver.resolve(proxyUri.host).get(), proxyUri.port) val user = proxyUri.userInfo?.substringBefore(':') val pass = proxyUri.userInfo?.substringAfter(':') - when (proxyUri.scheme) { - "socks5" -> pipe.addFirst(Socks5ProxyHandler(socket, user, pass)) - "socks4" -> pipe.addFirst(Socks4ProxyHandler(socket, user)) - "http" -> pipe.addFirst(if (user != null) HttpProxyHandler(socket, user, pass) else HttpProxyHandler(socket)) + val handler = when (proxyUri.scheme) { + "socks5" -> Socks5ProxyHandler(socket, user, pass) + "socks4" -> Socks4ProxyHandler(socket, user) + "http" -> if (user != null) HttpProxyHandler(socket, user, pass) else HttpProxyHandler(socket) + else -> null } + if (handler != null) pipe.addFirst("proxy", handler) } }