From 942908341a0ecfce5f910c05beec7378d5f71313 Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Fri, 2 Jul 2021 19:45:52 -0300 Subject: [PATCH] fix fallback when using non-ipv6 compatible proxy --- .../com/viaversion/aas/handler/MinecraftHandler.kt | 13 +++++++++++-- .../kotlin/com/viaversion/aas/handler/state/Util.kt | 5 ++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt b/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt index 86ab5a8..01715f9 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/MinecraftHandler.kt @@ -7,6 +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.ProxyConnectException import io.netty.handler.proxy.ProxyHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -34,8 +35,10 @@ class MinecraftHandler( } override fun channelInactive(ctx: ChannelHandlerContext) { - other?.close() - data.state.onInactivated(this) + if (!failedProxy(ctx)) { + other?.close() + data.state.onInactivated(this) + } coroutineScope.cancel() } @@ -47,7 +50,13 @@ class MinecraftHandler( other?.setAutoRead(ctx.channel().isWritable) } + private fun failedProxy(ctx: ChannelHandlerContext): Boolean { + // proxy connect future fails are handled in another part + return (ctx.channel().pipeline().get("proxy") as? ProxyHandler)?.connectFuture()?.isSuccess == false + } + override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) { + if (cause is ProxyConnectException && failedProxy(ctx)) return if (cause is CancelCodecException) return if (cause is ClosedChannelException) return mcLogger.debug("Exception: ", cause) diff --git a/src/main/kotlin/com/viaversion/aas/handler/state/Util.kt b/src/main/kotlin/com/viaversion/aas/handler/state/Util.kt index fff5cda..1c9faa4 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/state/Util.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/state/Util.kt @@ -16,6 +16,7 @@ import io.netty.bootstrap.Bootstrap import io.netty.channel.Channel import io.netty.channel.ChannelOption import io.netty.channel.socket.SocketChannel +import io.netty.handler.proxy.ProxyHandler import io.netty.resolver.NoopAddressResolverGroup import kotlinx.coroutines.future.await import kotlinx.coroutines.withTimeoutOrNull @@ -41,6 +42,7 @@ private suspend fun createBackChannel( .connect(socketAddr) .also { it.suspendAwait() } .channel() + (channel.pipeline().get("proxy") as? ProxyHandler)?.connectFuture()?.suspendAwait() mcLogger.info("+ ${handler.endRemoteAddress} -> $socketAddr") handler.data.backChannel = channel as SocketChannel @@ -69,7 +71,8 @@ private suspend fun autoDetectVersion(handler: MinecraftHandler, socketAddr: Ine if (detectedProtocol != null && detectedProtocol.version !in arrayOf(-1, -2) - && ProtocolVersion.isRegistered(detectedProtocol.version)) { + && ProtocolVersion.isRegistered(detectedProtocol.version) + ) { handler.data.backServerVer = detectedProtocol.version } else { handler.data.backServerVer = 47 // fallback 1.8