fix fallback when using non-ipv6 compatible proxy

This commit is contained in:
creeper123123321 2021-07-02 19:45:52 -03:00
parent 61cd7b0b44
commit 942908341a
2 changed files with 15 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import com.viaversion.viaversion.exception.CancelCodecException
import io.netty.channel.Channel import io.netty.channel.Channel
import io.netty.channel.ChannelHandlerContext import io.netty.channel.ChannelHandlerContext
import io.netty.channel.SimpleChannelInboundHandler import io.netty.channel.SimpleChannelInboundHandler
import io.netty.handler.proxy.ProxyConnectException
import io.netty.handler.proxy.ProxyHandler import io.netty.handler.proxy.ProxyHandler
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -34,8 +35,10 @@ class MinecraftHandler(
} }
override fun channelInactive(ctx: ChannelHandlerContext) { override fun channelInactive(ctx: ChannelHandlerContext) {
if (!failedProxy(ctx)) {
other?.close() other?.close()
data.state.onInactivated(this) data.state.onInactivated(this)
}
coroutineScope.cancel() coroutineScope.cancel()
} }
@ -47,7 +50,13 @@ class MinecraftHandler(
other?.setAutoRead(ctx.channel().isWritable) 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) { override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) {
if (cause is ProxyConnectException && failedProxy(ctx)) return
if (cause is CancelCodecException) return if (cause is CancelCodecException) return
if (cause is ClosedChannelException) return if (cause is ClosedChannelException) return
mcLogger.debug("Exception: ", cause) mcLogger.debug("Exception: ", cause)

View File

@ -16,6 +16,7 @@ import io.netty.bootstrap.Bootstrap
import io.netty.channel.Channel import io.netty.channel.Channel
import io.netty.channel.ChannelOption import io.netty.channel.ChannelOption
import io.netty.channel.socket.SocketChannel import io.netty.channel.socket.SocketChannel
import io.netty.handler.proxy.ProxyHandler
import io.netty.resolver.NoopAddressResolverGroup import io.netty.resolver.NoopAddressResolverGroup
import kotlinx.coroutines.future.await import kotlinx.coroutines.future.await
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
@ -41,6 +42,7 @@ private suspend fun createBackChannel(
.connect(socketAddr) .connect(socketAddr)
.also { it.suspendAwait() } .also { it.suspendAwait() }
.channel() .channel()
(channel.pipeline().get("proxy") as? ProxyHandler)?.connectFuture()?.suspendAwait()
mcLogger.info("+ ${handler.endRemoteAddress} -> $socketAddr") mcLogger.info("+ ${handler.endRemoteAddress} -> $socketAddr")
handler.data.backChannel = channel as SocketChannel handler.data.backChannel = channel as SocketChannel
@ -69,7 +71,8 @@ private suspend fun autoDetectVersion(handler: MinecraftHandler, socketAddr: Ine
if (detectedProtocol != null if (detectedProtocol != null
&& detectedProtocol.version !in arrayOf(-1, -2) && detectedProtocol.version !in arrayOf(-1, -2)
&& ProtocolVersion.isRegistered(detectedProtocol.version)) { && ProtocolVersion.isRegistered(detectedProtocol.version)
) {
handler.data.backServerVer = detectedProtocol.version handler.data.backServerVer = detectedProtocol.version
} else { } else {
handler.data.backServerVer = 47 // fallback 1.8 handler.data.backServerVer = 47 // fallback 1.8