forked from git-mirrors/GeyserConnect
Add support for multiple vhosts
This commit is contained in:
parent
90870e9238
commit
bdbe73b461
4 changed files with 50 additions and 30 deletions
|
@ -108,6 +108,12 @@ public class GeyserConnect implements Extension {
|
||||||
if (geyserInstance.getConfig().isPassthroughMotd() || geyserInstance.getConfig().isPassthroughPlayerCounts()) {
|
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");
|
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
|
@Subscribe
|
||||||
|
|
|
@ -45,6 +45,8 @@ import org.geysermc.geyser.util.DimensionUtils;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class PacketHandler extends UpstreamPacketHandler {
|
public class PacketHandler extends UpstreamPacketHandler {
|
||||||
|
|
||||||
|
@ -92,38 +94,45 @@ public class PacketHandler extends UpstreamPacketHandler {
|
||||||
// Handle the virtual host if specified
|
// Handle the virtual host if specified
|
||||||
VirtualHostSection vhost = geyserConnect.config().vhost();
|
VirtualHostSection vhost = geyserConnect.config().vhost();
|
||||||
if (vhost.enabled()) {
|
if (vhost.enabled()) {
|
||||||
String domain = session.getClientData().getServerAddress().split(":")[0];
|
String domain = session.getClientData().getServerAddress();
|
||||||
if (!domain.equals(vhost.baseDomain()) && domain.endsWith("." + vhost.baseDomain())) {
|
|
||||||
String address = "";
|
|
||||||
int port = 25565;
|
|
||||||
boolean online = true;
|
|
||||||
|
|
||||||
// Parse the address used
|
// Build the regex matcher for the vhosts
|
||||||
String[] domainParts = domain.replaceFirst("\\." + vhost.baseDomain() + "$", "").split("\\._");
|
Pattern regex = Pattern.compile("\\.?(" + vhost.domains().stream().map(Pattern::quote).collect(Collectors.joining("|")) + ")(:[0-9]+)?$");
|
||||||
for (int i = 0; i < domainParts.length; i++) {
|
|
||||||
String part = domainParts[i];
|
if (regex.matcher(domain).find()) {
|
||||||
if (i == 0) {
|
String target = domain.replaceAll(regex.pattern(), "").strip();
|
||||||
address = part;
|
if (!target.isEmpty()) {
|
||||||
} else if (part.startsWith("p")) {
|
String address = "";
|
||||||
port = Integer.parseInt(part.substring(1));
|
int port = 25565;
|
||||||
} else if (part.startsWith("o")) {
|
boolean online = true;
|
||||||
online = false;
|
|
||||||
|
// 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
|
// They didn't specify an address so disconnect them
|
||||||
if (address.startsWith("_")) {
|
if (address.startsWith("_")) {
|
||||||
session.disconnect("disconnectionScreen.invalidIP");
|
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)" : ""));
|
||||||
|
|
||||||
|
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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));
|
|
||||||
|
|
||||||
return PacketSignal.HANDLED;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,9 @@ package org.geysermc.connect.extension.config;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public record VirtualHostSection(
|
public record VirtualHostSection(
|
||||||
boolean enabled,
|
boolean enabled,
|
||||||
@JsonProperty("base-domain") String baseDomain) {
|
@JsonProperty("domains") List<String> domains) {
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,5 +96,8 @@ vhost:
|
||||||
# Should this be enabled
|
# Should this be enabled
|
||||||
enabled: false
|
enabled: false
|
||||||
|
|
||||||
# The base domain pointing to the server
|
# The domains pointing to the server
|
||||||
base-domain: example.com
|
domains:
|
||||||
|
- example.com
|
||||||
|
- eu.example.com
|
||||||
|
- us.example.com
|
||||||
|
|
Loading…
Add table
Reference in a new issue