From bdbe73b461c04114bfe813689b7ee46f816b9abc Mon Sep 17 00:00:00 2001 From: rtm516 Date: Sun, 24 Dec 2023 13:15:41 +0000 Subject: [PATCH] Add support for multiple vhosts --- .../connect/extension/GeyserConnect.java | 6 ++ .../connect/extension/PacketHandler.java | 63 +++++++++++-------- .../extension/config/VirtualHostSection.java | 4 +- src/main/resources/config.yml | 7 ++- 4 files changed, 50 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/geysermc/connect/extension/GeyserConnect.java b/src/main/java/org/geysermc/connect/extension/GeyserConnect.java index be5dec6..55fb065 100644 --- a/src/main/java/org/geysermc/connect/extension/GeyserConnect.java +++ b/src/main/java/org/geysermc/connect/extension/GeyserConnect.java @@ -108,6 +108,12 @@ public void onPostInitialize(GeyserPostInitializeEvent event) { if (geyserInstance.getConfig().isPassthroughMotd() || geyserInstance.getConfig().isPassthroughPlayerCounts()) { this.logger().warning("Either `passthrough-motd` or `passthrough-player-counts` is enabled in the config, this will likely produce errors"); } + + // If we are using floodgate then disable the extension + if (geyserInstance.getConfig().getRemote().authType() == AuthType.FLOODGATE) { + this.logger().error("auth-type set to floodgate in the config, this will break GeyserConnect. Disabling!"); + this.disable(); + } } @Subscribe diff --git a/src/main/java/org/geysermc/connect/extension/PacketHandler.java b/src/main/java/org/geysermc/connect/extension/PacketHandler.java index 130c7fd..aeb93b9 100644 --- a/src/main/java/org/geysermc/connect/extension/PacketHandler.java +++ b/src/main/java/org/geysermc/connect/extension/PacketHandler.java @@ -45,6 +45,8 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; +import java.util.stream.Collectors; public class PacketHandler extends UpstreamPacketHandler { @@ -92,38 +94,45 @@ public PacketSignal handle(SetLocalPlayerAsInitializedPacket packet) { // Handle the virtual host if specified VirtualHostSection vhost = geyserConnect.config().vhost(); if (vhost.enabled()) { - String domain = session.getClientData().getServerAddress().split(":")[0]; - if (!domain.equals(vhost.baseDomain()) && domain.endsWith("." + vhost.baseDomain())) { - String address = ""; - int port = 25565; - boolean online = true; - - // Parse the address used - String[] domainParts = domain.replaceFirst("\\." + vhost.baseDomain() + "$", "").split("\\._"); - for (int i = 0; i < domainParts.length; i++) { - String part = domainParts[i]; - if (i == 0) { - address = part; - } else if (part.startsWith("p")) { - port = Integer.parseInt(part.substring(1)); - } else if (part.startsWith("o")) { - online = false; + String domain = session.getClientData().getServerAddress(); + + // Build the regex matcher for the vhosts + Pattern regex = Pattern.compile("\\.?(" + vhost.domains().stream().map(Pattern::quote).collect(Collectors.joining("|")) + ")(:[0-9]+)?$"); + + if (regex.matcher(domain).find()) { + String target = domain.replaceAll(regex.pattern(), "").strip(); + if (!target.isEmpty()) { + String address = ""; + int port = 25565; + boolean online = true; + + // Parse the address used + String[] domainParts = target.split("\\._"); + for (int i = 0; i < domainParts.length; i++) { + String part = domainParts[i]; + if (i == 0) { + address = part; + } else if (part.startsWith("p")) { + port = Integer.parseInt(part.substring(1)); + } else if (part.startsWith("o")) { + online = false; + } } - } - // They didn't specify an address so disconnect them - if (address.startsWith("_")) { - session.disconnect("disconnectionScreen.invalidIP"); - return PacketSignal.HANDLED; - } + // They didn't specify an address so disconnect them + if (address.startsWith("_")) { + session.disconnect("disconnectionScreen.invalidIP"); + return PacketSignal.HANDLED; + } - // Log the virtual host usage - geyserConnect.logger().info(Utils.displayName(session) + " is using virtualhost: " + address + ":" + port + (!online ? " (offline)" : "")); + // Log the virtual host usage + geyserConnect.logger().info(Utils.displayName(session) + " is using virtualhost: " + address + ":" + port + (!online ? " (offline)" : "")); - // Send the player to the wanted server - Utils.sendToServer(session, originalPacketHandler, new Server(address, port, online, false, null, null, null)); + // Send the player to the wanted server + Utils.sendToServer(session, originalPacketHandler, new Server(address, port, online, false, null, null, null)); - return PacketSignal.HANDLED; + return PacketSignal.HANDLED; + } } } diff --git a/src/main/java/org/geysermc/connect/extension/config/VirtualHostSection.java b/src/main/java/org/geysermc/connect/extension/config/VirtualHostSection.java index 4aa1a60..b28f6fe 100644 --- a/src/main/java/org/geysermc/connect/extension/config/VirtualHostSection.java +++ b/src/main/java/org/geysermc/connect/extension/config/VirtualHostSection.java @@ -27,7 +27,9 @@ import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; + public record VirtualHostSection( boolean enabled, - @JsonProperty("base-domain") String baseDomain) { + @JsonProperty("domains") List domains) { } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 74a29db..d73f267 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -96,5 +96,8 @@ vhost: # Should this be enabled enabled: false - # The base domain pointing to the server - base-domain: example.com + # The domains pointing to the server + domains: + - example.com + - eu.example.com + - us.example.com