Skip to content

Commit

Permalink
Add support for multiple vhosts
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Dec 24, 2023
1 parent 90870e9 commit bdbe73b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 36 additions & 27 deletions src/main/java/org/geysermc/connect/extension/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> domains) {
}
7 changes: 5 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bdbe73b

Please sign in to comment.