Add support for URI-compliant wildcard-domain-handling syntax

This commit is contained in:
Kamesuta 2025-01-12 18:16:02 +09:00
parent 73c470c3da
commit 625cbf147e
No known key found for this signature in database
GPG key ID: 62D73B99C24685B6
2 changed files with 42 additions and 13 deletions
src/main/java/net/raphimc/viaproxy
protocoltranslator/viaproxy
proxy/client2proxy

View file

@ -168,7 +168,7 @@ public class ViaProxyConfig {
@Description({
"Allows clients to specify a target server and version using wildcard domains.",
"none: No wildcard domain handling.",
"public: Public wildcard domain handling. Intended for usage by external clients. (Example: address_port_version.viaproxy.127.0.0.1.nip.io)",
"public: Public wildcard domain handling. Intended for usage by external clients. (Example: address.<address>.port.<port>.version.<version>.viaproxy.127.0.0.1.nip.io)",
"internal: Internal wildcard domain handling. Intended for local usage by custom clients. (Example: original-handshake-address\\7address:port\\7version\\7classic-mppass)"
})
private WildcardDomainHandling wildcardDomainHandling = WildcardDomainHandling.NONE;

View file

@ -61,10 +61,15 @@ import java.nio.channels.UnresolvedAddressException;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Client2ProxyHandler extends SimpleChannelInboundHandler<Packet> {
private static final Pattern ADDRESS_SYNTAX_PATTERN = Pattern.compile(
"^address\\.(.+?)\\.port\\.(\\d+?)\\.version\\.(.+?)$"
);
private ProxyConnection proxyConnection;
@Override
@ -140,21 +145,45 @@ public class Client2ProxyHandler extends SimpleChannelInboundHandler<Packet> {
if (!handshakeParts[0].toLowerCase().contains(".viaproxy.")) {
throw new IllegalArgumentException();
}
// Extract the portion before ".viaproxy."
final String addressData = handshakeParts[0].substring(0, handshakeParts[0].toLowerCase().lastIndexOf(".viaproxy."));
final ArrayHelper arrayHelper = ArrayHelper.instanceOf(addressData.split(Pattern.quote("_")));
if (arrayHelper.getLength() < 3) {
throw new IllegalArgumentException();
if (addressData.startsWith("address.")) {
// Handle the explicit syntax: address.<ip>.port.<port>.version.<version>.viaproxy.hostname
final Matcher matcher = ADDRESS_SYNTAX_PATTERN.matcher(addressData);
if (!matcher.matches()) {
throw new IllegalArgumentException();
}
final String connectAddress = matcher.group(1);
final int connectPort = Integer.parseInt(matcher.group(2));
final String versionString = matcher.group(3);
serverVersion = ProtocolVersionUtil.fromNameLenient(versionString);
if (serverVersion == null) {
this.proxyConnection.kickClient("§cWrong domain syntax!\n§cUnknown server version.");
}
serverAddress = AddressUtil.parse(connectAddress + ":" + connectPort, serverVersion);
} else {
// Handle the _ syntax: <ip>_<port>_<version>.viaproxy.hostname
final ArrayHelper arrayHelper = ArrayHelper.instanceOf(addressData.split(Pattern.quote("_")));
if (arrayHelper.getLength() < 3) {
throw new IllegalArgumentException();
}
final String versionString = arrayHelper.get(arrayHelper.getLength() - 1);
serverVersion = ProtocolVersionUtil.fromNameLenient(versionString);
if (serverVersion == null) {
this.proxyConnection.kickClient("§cWrong domain syntax!\n§cUnknown server version.");
}
final String connectAddress = arrayHelper.getAsString(0, arrayHelper.getLength() - 3, "_");
final int connectPort = arrayHelper.getInteger(arrayHelper.getLength() - 2);
serverAddress = AddressUtil.parse(connectAddress + ":" + connectPort, serverVersion);
}
final String versionString = arrayHelper.get(arrayHelper.getLength() - 1);
serverVersion = ProtocolVersionUtil.fromNameLenient(versionString);
if (serverVersion == null) {
this.proxyConnection.kickClient("§cWrong domain syntax!\n§cUnknown server version.");
}
final String connectAddress = arrayHelper.getAsString(0, arrayHelper.getLength() - 3, "_");
final int connectPort = arrayHelper.getInteger(arrayHelper.getLength() - 2);
serverAddress = AddressUtil.parse(connectAddress + ":" + connectPort, serverVersion);
} catch (IllegalArgumentException e) {
this.proxyConnection.kickClient("§cWrong domain syntax! §6Please use:\n§7address_port_version.viaproxy.hostname");
this.proxyConnection.kickClient("§cWrong domain syntax! §6Please use:\n§7address.<ip>.port.<port>.version.<version>.viaproxy.hostname");
}
} else if (ViaProxy.getConfig().getWildcardDomainHandling() == ViaProxyConfig.WildcardDomainHandling.INTERNAL) {
final ArrayHelper arrayHelper = ArrayHelper.instanceOf(handshakeParts[0].split("\7"));