mirror of
https://github.com/ViaVersion/VIAaaS.git
synced 2025-01-08 19:38:36 +01:00
ported address parser to java, fix `_ot
`
This commit is contained in:
parent
188b6e5cc1
commit
e6e621950c
111
src/main/java/com/viaversion/aas/util/AddressParser.java
Normal file
111
src/main/java/com/viaversion/aas/util/AddressParser.java
Normal file
@ -0,0 +1,111 @@
|
||||
package com.viaversion.aas.util;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.primitives.Ints;
|
||||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||
import kotlin.text.StringsKt;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class AddressParser {
|
||||
public Integer protocol;
|
||||
public String viaSuffix;
|
||||
public String serverAddress;
|
||||
public String viaOptions;
|
||||
public Integer port;
|
||||
public String username;
|
||||
public Boolean online;
|
||||
|
||||
public AddressParser parse(String address, String viaHostName) {
|
||||
address = StringsKt.removeSuffix(address, ".");
|
||||
String suffixRemoved = StringsKt.removeSuffix(address, viaHostName);
|
||||
|
||||
if (suffixRemoved.equals(address)) {
|
||||
serverAddress = address;
|
||||
return this;
|
||||
}
|
||||
|
||||
boolean stopOptions = false;
|
||||
List<String> optionsParts = new ArrayList<>();
|
||||
List<String> serverParts = new ArrayList<>();
|
||||
|
||||
for (String part : Lists.reverse(Arrays.asList(suffixRemoved.split(Pattern.quote("."))))) {
|
||||
if (!stopOptions && parseOption(part)) {
|
||||
optionsParts.add(part);
|
||||
continue;
|
||||
}
|
||||
stopOptions = true;
|
||||
serverParts.add(part);
|
||||
}
|
||||
|
||||
serverAddress = String.join(".", Lists.reverse(serverParts));
|
||||
viaOptions = String.join(".", Lists.reverse(optionsParts));
|
||||
viaSuffix = viaHostName;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean parseOption(String part) {
|
||||
String option;
|
||||
if (part.length() < 2) {
|
||||
return false;
|
||||
} else if (part.startsWith("_")) {
|
||||
option = String.valueOf(part.charAt(1));
|
||||
} else if (part.charAt(1) == '_') {
|
||||
option = String.valueOf(part.charAt(0));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
String arg = part.substring(2);
|
||||
switch (option) {
|
||||
case "o": {
|
||||
parseOnlineMode(arg);
|
||||
break;
|
||||
}
|
||||
case "p": {
|
||||
parsePort(arg);
|
||||
break;
|
||||
}
|
||||
case "u": {
|
||||
parseUsername(arg);
|
||||
break;
|
||||
}
|
||||
case "v": {
|
||||
parseProtocol(arg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void parsePort(String arg) {
|
||||
port = Ints.tryParse(arg);
|
||||
}
|
||||
|
||||
public void parseUsername(String arg) {
|
||||
if (arg.length() > 16) throw new IllegalArgumentException("Invalid username");
|
||||
username = arg;
|
||||
}
|
||||
|
||||
public void parseOnlineMode(String arg) {
|
||||
online = null;
|
||||
if (StringsKt.startsWith(arg, "t", true)) {
|
||||
online = true;
|
||||
} else if (StringsKt.startsWith(arg, "f", true)) {
|
||||
online = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void parseProtocol(String arg) {
|
||||
protocol = Ints.tryParse(arg);
|
||||
if (protocol == null) {
|
||||
ProtocolVersion ver = ProtocolVersion.getClosest(arg.replace("_", "."));
|
||||
if (ver != null) protocol = ver.getVersion();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,87 +0,0 @@
|
||||
package com.viaversion.aas
|
||||
|
||||
import com.viaversion.aas.util.StacklessException
|
||||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion
|
||||
|
||||
class VIAaaSAddress {
|
||||
var serverAddress: String? = null
|
||||
var viaSuffix: String? = null
|
||||
var viaOptions: String? = null
|
||||
var protocol: Int? = null
|
||||
var port: Int? = null
|
||||
var online: Boolean? = null
|
||||
var username: String? = null
|
||||
fun parse(rawAddress: String, viaHostName: String): VIAaaSAddress {
|
||||
val address = rawAddress.removeSuffix(".")
|
||||
val suffixRemoved = address.removeSuffix(".$viaHostName")
|
||||
|
||||
if (suffixRemoved == address) {
|
||||
serverAddress = address
|
||||
return this
|
||||
}
|
||||
|
||||
var stopOptions = false
|
||||
val optionsParts = arrayListOf<String>()
|
||||
val serverParts = arrayListOf<String>()
|
||||
|
||||
for (part in suffixRemoved.split('.').asReversed()) {
|
||||
if (!stopOptions && parseOption(part)) {
|
||||
optionsParts.add(part)
|
||||
continue
|
||||
}
|
||||
stopOptions = true
|
||||
serverParts.add(part)
|
||||
}
|
||||
|
||||
serverAddress = serverParts.asReversed().joinToString(".")
|
||||
viaOptions = optionsParts.asReversed().joinToString(".")
|
||||
viaSuffix = viaHostName
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
fun parseOption(part: String): Boolean {
|
||||
val option = when {
|
||||
part.length < 2 -> null
|
||||
part.startsWith("_") -> part[1]
|
||||
part[1] == '_' -> part[0]
|
||||
else -> null
|
||||
}?.toString() ?: return false
|
||||
|
||||
val arg = part.substring(2)
|
||||
when (option.lowercase()) {
|
||||
"p" -> parsePort(arg)
|
||||
"o" -> parseOnlineMode(arg)
|
||||
"v" -> parseProtocol(arg)
|
||||
"u" -> parseUsername(arg)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
fun parsePort(arg: String) {
|
||||
port = arg.toInt()
|
||||
}
|
||||
|
||||
fun parseUsername(arg: String) {
|
||||
if (arg.length > 16) throw StacklessException("Invalid username")
|
||||
username = arg
|
||||
}
|
||||
|
||||
fun parseOnlineMode(arg: String) {
|
||||
online = when {
|
||||
arg.startsWith("t", ignoreCase = true) -> true
|
||||
arg.startsWith("f", ignoreCase = true) -> false
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
fun parseProtocol(arg: String) {
|
||||
try {
|
||||
protocol = arg.toInt()
|
||||
} catch (e: NumberFormatException) {
|
||||
ProtocolVersion.getClosest(arg.replace("_", "."))?.also {
|
||||
protocol = it.version
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -4,13 +4,13 @@ import com.google.common.cache.CacheBuilder
|
||||
import com.google.common.cache.CacheLoader
|
||||
import com.google.common.net.HostAndPort
|
||||
import com.google.common.util.concurrent.RateLimiter
|
||||
import com.viaversion.aas.VIAaaSAddress
|
||||
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.mcLogger
|
||||
import com.viaversion.aas.setAutoRead
|
||||
import com.viaversion.aas.util.AddressParser
|
||||
import com.viaversion.aas.util.StacklessException
|
||||
import com.viaversion.viaversion.api.protocol.packet.State
|
||||
import io.netty.channel.ChannelHandlerContext
|
||||
@ -63,7 +63,7 @@ class HandshakeState : ConnectionState {
|
||||
val virtualHostNoExtra = packet.address.substringBefore(0.toChar())
|
||||
|
||||
val parsed = VIAaaSConfig.hostName.map {
|
||||
VIAaaSAddress().parse(virtualHostNoExtra, it)
|
||||
AddressParser().parse(virtualHostNoExtra, it)
|
||||
}.sortedBy {
|
||||
it.viaSuffix == null
|
||||
}.first()
|
||||
|
@ -210,7 +210,6 @@ class LoginState : ConnectionState {
|
||||
frontName = loginStart.username
|
||||
backName = backName ?: frontName
|
||||
|
||||
handler.data.frontChannel.setAutoRead(false)
|
||||
handler.coroutineScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
if (frontOnline != null) {
|
||||
|
Loading…
Reference in New Issue
Block a user