From d5931ccb7c18e6d75c2090d587b2f538307b21cb Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Sun, 9 Jul 2023 09:31:11 -0300 Subject: [PATCH] fix protocol detection when ipv6 fails --- src/main/kotlin/com/viaversion/aas/VIAaaS.kt | 4 +++- .../com/viaversion/aas/handler/ConnectionData.kt | 1 + .../viaversion/aas/handler/state/HandshakeState.kt | 4 +++- .../com/viaversion/aas/handler/state/LoginState.kt | 1 + .../kotlin/com/viaversion/aas/handler/state/Util.kt | 13 +++++-------- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/viaversion/aas/VIAaaS.kt b/src/main/kotlin/com/viaversion/aas/VIAaaS.kt index 1390f6f..b2094b9 100644 --- a/src/main/kotlin/com/viaversion/aas/VIAaaS.kt +++ b/src/main/kotlin/com/viaversion/aas/VIAaaS.kt @@ -53,6 +53,8 @@ private fun printSplash() { println("VIAaaS ${AspirinServer.version}") } +val autoProtocolId = -2 + private fun initVia() { AspirinPlatform.initVia { AspirinRewind().init() @@ -61,7 +63,7 @@ private fun initVia() { AspirinLegacy().init() } - ProtocolVersion.register(-2, "AUTO") + ProtocolVersion.register(autoProtocolId, "AUTO") registerAspirinProtocols() } diff --git a/src/main/kotlin/com/viaversion/aas/handler/ConnectionData.kt b/src/main/kotlin/com/viaversion/aas/handler/ConnectionData.kt index ce0b436..4052727 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/ConnectionData.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/ConnectionData.kt @@ -12,6 +12,7 @@ class ConnectionData( var state: ConnectionState = HandshakeState(), var frontVer: Int? = null, var backServerVer: Int? = null, + var autoDetectProtocol: Boolean = false ) { val frontHandler get() = frontChannel.pipeline()[MinecraftHandler::class.java] val backHandler get() = backChannel?.pipeline()?.get(MinecraftHandler::class.java) diff --git a/src/main/kotlin/com/viaversion/aas/handler/state/HandshakeState.kt b/src/main/kotlin/com/viaversion/aas/handler/state/HandshakeState.kt index 848ce6d..2e18b5a 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/state/HandshakeState.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/state/HandshakeState.kt @@ -8,6 +8,7 @@ import com.viaversion.aas.codec.packet.Packet import com.viaversion.aas.codec.packet.handshake.Handshake import com.viaversion.aas.config.VIAaaSConfig import com.viaversion.aas.handler.MinecraftHandler +import com.viaversion.aas.autoProtocolId import com.viaversion.aas.mcLogger import com.viaversion.aas.util.AddressParser import com.viaversion.aas.util.StacklessException @@ -72,7 +73,7 @@ class HandshakeState : ConnectionState { } } - val backProto = parsed.protocol ?: -2 + val backProto = parsed.protocol ?: autoProtocolId val backAddress = parsed.serverAddress!! val port = parsed.port ?: VIAaaSConfig.defaultBackendPort ?: virtualPort @@ -83,6 +84,7 @@ class HandshakeState : ConnectionState { val addressFromWeb = VIAaaSConfig.hostName.any { parsed.serverAddress.equals(it, ignoreCase = true) } handler.data.backServerVer = backProto + if (backProto == autoProtocolId) handler.data.autoDetectProtocol = true (handler.data.state as? LoginState)?.also { it.frontOnline = frontOnline it.backName = parsed.username diff --git a/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt b/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt index db1ca7c..6b9c3a5 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/state/LoginState.kt @@ -266,6 +266,7 @@ class LoginState : ConnectionState { val info = AspirinServer.viaWebServer.requestAddressInfo(frontName).await() backAddress = info.backHostAndPort handler.data.backServerVer = info.backVersion + if (info.backVersion == autoProtocolId) handler.data.autoDetectProtocol = true frontOnline = info.frontOnline info.backName?.also { backName = info.backName } } 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 0125763..9b8f716 100644 --- a/src/main/kotlin/com/viaversion/aas/handler/state/Util.kt +++ b/src/main/kotlin/com/viaversion/aas/handler/state/Util.kt @@ -59,7 +59,7 @@ private suspend fun createBackChannel( mcLogger.debug("+ {} {} -> {}", state.name[0], handler.endRemoteAddress, socketAddr) } handler.data.backChannel = channel as SocketChannel - + val packet = Handshake() packet.nextState = state packet.protocolId = handler.data.frontVer!! @@ -74,7 +74,7 @@ private suspend fun createBackChannel( } private suspend fun autoDetectVersion(handler: MinecraftHandler, socketAddr: InetSocketAddress) { - if (handler.data.backServerVer == -2) { // Auto + if (handler.data.autoDetectProtocol) { // Auto var detectedProtocol: ProtocolVersion? = null try { detectedProtocol = withTimeout(10_000) { @@ -85,14 +85,10 @@ private suspend fun autoDetectVersion(handler: MinecraftHandler, socketAddr: Ine mcLogger.debug("Stacktrace: ", e) } - if (detectedProtocol != null + handler.data.backServerVer = if (detectedProtocol != null && detectedProtocol.version !in arrayOf(-1, -2) && ProtocolVersion.isRegistered(detectedProtocol.version) - ) { - handler.data.backServerVer = detectedProtocol.version - } else { - handler.data.backServerVer = 47 // fallback 1.8 - } + ) detectedProtocol.version else 47 // fallback 1.8 } } @@ -136,6 +132,7 @@ private suspend fun resolveBackendAddresses(hostAndPort: HostAndPort): List listOf(InetSocketAddress.createUnresolved(removedEndDot, srvResolved.port)) + else -> AspirinServer.dnsResolver .resolveAll(srvResolved.host) .suspendAwait()