Add support for multiple vhosts

This commit is contained in:
rtm516 2023-12-24 13:15:41 +00:00
parent 90870e9238
commit bdbe73b461
No known key found for this signature in database
GPG key ID: 331715B8B007C67A
4 changed files with 50 additions and 30 deletions

View file

@ -108,6 +108,12 @@ public class GeyserConnect implements Extension {
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

View file

@ -45,6 +45,8 @@ import org.geysermc.geyser.util.DimensionUtils;
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,14 +94,20 @@ public class PacketHandler extends UpstreamPacketHandler {
// 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 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 = domain.replaceFirst("\\." + vhost.baseDomain() + "$", "").split("\\._");
String[] domainParts = target.split("\\._");
for (int i = 0; i < domainParts.length; i++) {
String part = domainParts[i];
if (i == 0) {
@ -126,6 +134,7 @@ public class PacketHandler extends UpstreamPacketHandler {
return PacketSignal.HANDLED;
}
}
}
// Handle normal connections
if (session.getPlayerEntity().getGeyserId() == packet.getRuntimeEntityId()) {

View file

@ -27,7 +27,9 @@ package org.geysermc.connect.extension.config;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public record VirtualHostSection(
boolean enabled,
@JsonProperty("base-domain") String baseDomain) {
@JsonProperty("domains") List<String> domains) {
}

View file

@ -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