From f37bace7affe6ca8e3c3a85b27f3f9ad33ffe003 Mon Sep 17 00:00:00 2001 From: creeper123123321 <7974274+creeper123123321@users.noreply.github.com> Date: Sun, 7 Feb 2021 12:00:25 -0300 Subject: [PATCH] viaaas address code cleanup --- .../creeper123123321/viaaas/CloudHandler.kt | 4 +- .../github/creeper123123321/viaaas/VIAaaS.kt | 114 +++++++++--------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/CloudHandler.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/CloudHandler.kt index d33e8d4..08d55c5 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/CloudHandler.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/CloudHandler.kt @@ -133,7 +133,7 @@ class HandshakeState : MinecraftConnectionState { val backProto = parsed.protocol ?: 47 // todo autodetection val hadHostname = parsed.viaSuffix != null - packet.address = parsed.realAddress!! + packet.address = parsed.serverAddress!! packet.port = parsed.port ?: if (VIAaaSConfig.defaultBackendPort == -1) { packet.port } else { @@ -142,7 +142,7 @@ class HandshakeState : MinecraftConnectionState { handler.data.backVer = backProto handler.data.frontOnline = parsed.online - handler.data.backName = parsed.altUsername + handler.data.backName = parsed.username val playerAddr = handler.data.frontHandler.remoteAddress mcLogger.info("Connecting $playerAddr (${handler.data.frontVer}) -> ${packet.address}:${packet.port} ($backProto)") diff --git a/src/main/kotlin/com/github/creeper123123321/viaaas/VIAaaS.kt b/src/main/kotlin/com/github/creeper123123321/viaaas/VIAaaS.kt index 52857ba..8d32ca6 100644 --- a/src/main/kotlin/com/github/creeper123123321/viaaas/VIAaaS.kt +++ b/src/main/kotlin/com/github/creeper123123321/viaaas/VIAaaS.kt @@ -294,68 +294,68 @@ object VIAaaSConfig : Config(File("config/viaaas.yml")) { } class VIAaaSAddress { - var protocol: Int? = null + var serverAddress: String? = null var viaSuffix: String? = null - var realAddress: String? = null + var viaOptions: String? = null + var protocol: Int? = null var port: Int? = null - var online = true - var altUsername: String? = null + var online: Boolean? = null + var username: String? = null fun parse(address: String, viaHostName: String): VIAaaSAddress { - val parts = address.split('.') - var foundDomain = false - var foundOptions = false - val ourParts = StringBuilder() - val realAddrBuilder = StringBuilder() - for (i in parts.indices.reversed()) { - val part = parts[i] - var realAddrPart = false - if (foundDomain) { - if (!foundOptions) { - if (part.startsWith("_")) { - val arg = part.substring(2) - when { - part.startsWith("_p", ignoreCase = true) -> port = arg.toInt() - part.startsWith("_o", ignoreCase = true) -> online = arg.toBoolean() - part.startsWith("_v", ignoreCase = true) -> { - try { - protocol = arg.toInt() - } catch (e: NumberFormatException) { - val closest = ProtocolVersion.getClosest(arg.replace("_", ".")) - if (closest != null) { - protocol = closest.version - } - } - } - part.startsWith("_u", ignoreCase = true) -> { - if (arg.length > 16) throw IllegalArgumentException("Invalid alt username") - altUsername = arg - } - } - } else { - foundOptions = true - } - } - if (foundOptions) { - realAddrPart = true - } - } else if (parts.filterIndexed { a, _ -> a >= i } - .joinToString(".").equals(viaHostName, ignoreCase = true)) { - foundDomain = true - } - if (realAddrPart) { - realAddrBuilder.insert(0, "$part.") + val suffixRemoved = address.removeSuffix(".$viaHostName") + + if (suffixRemoved == address) { + serverAddress = address + return this + } + + var endOfOptions = false + val optionsList = arrayListOf() + serverAddress = suffixRemoved.split('.').asReversed().filter { + if (endOfOptions || !parseOption(it)) { + endOfOptions = true + true } else { - ourParts.insert(0, "$part.") + optionsList.add(it) + false } - } - val realAddr = realAddrBuilder.toString().replace("\\.$".toRegex(), "") - val suffix = ourParts.toString().replace("\\.$".toRegex(), "") - if (realAddr.isEmpty()) { - realAddress = address - } else { - realAddress = realAddr - viaSuffix = suffix - } + }.asReversed().joinToString(".") + + viaOptions = optionsList.asReversed().joinToString(".") + + viaSuffix = viaHostName + return this } + + fun parseOption(part: String): Boolean { + if (part.startsWith("_")) { + val arg = part.substring(2) + when { + part.startsWith("_p", ignoreCase = true) -> port = arg.toInt() + part.startsWith("_o", ignoreCase = true) -> { + online = when { + arg.startsWith("t", ignoreCase = true) -> true + arg.startsWith("f", ignoreCase = true) -> false + else -> null + } + } + part.startsWith("_v", ignoreCase = true) -> { + try { + protocol = arg.toInt() + } catch (e: NumberFormatException) { + ProtocolVersion.getClosest(arg.replace("_", "."))?.also { + protocol = it.version + } + } + } + part.startsWith("_u", ignoreCase = true) -> { + if (arg.length > 16) throw IllegalArgumentException("Invalid username") + username = arg + } + } + return true + } + return false + } }