Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting forwarding mode separately for different servers #1357

Open
wants to merge 6 commits into
base: dev/3.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class VelocityConfiguration implements ProxyConfig {
@Expose
private boolean preventClientProxyConnections = false;
@Expose
private PlayerInfoForwarding playerInfoForwardingMode = PlayerInfoForwarding.NONE;
private PlayerInfoForwarding defaultForwardingMode = PlayerInfoForwarding.NONE;
private byte[] forwardingSecret = generateRandomString(12).getBytes(StandardCharsets.UTF_8);
@Expose
private boolean announceForge = false;
Expand Down Expand Up @@ -103,7 +103,7 @@ private VelocityConfiguration(Servers servers, ForcedHosts forcedHosts, Advanced

private VelocityConfiguration(String bind, String motd, int showMaxPlayers, boolean onlineMode,
boolean preventClientProxyConnections, boolean announceForge,
PlayerInfoForwarding playerInfoForwardingMode, byte[] forwardingSecret,
PlayerInfoForwarding defaultForwardingMode, byte[] forwardingSecret,
boolean onlineModeKickExistingPlayers, PingPassthroughMode pingPassthrough,
boolean enablePlayerAddressLogging, Servers servers, ForcedHosts forcedHosts,
Advanced advanced, Query query, Metrics metrics, boolean forceKeyAuthentication) {
Expand All @@ -113,7 +113,7 @@ private VelocityConfiguration(String bind, String motd, int showMaxPlayers, bool
this.onlineMode = onlineMode;
this.preventClientProxyConnections = preventClientProxyConnections;
this.announceForge = announceForge;
this.playerInfoForwardingMode = playerInfoForwardingMode;
this.defaultForwardingMode = defaultForwardingMode;
this.forwardingSecret = forwardingSecret;
this.onlineModeKickExistingPlayers = onlineModeKickExistingPlayers;
this.pingPassthrough = pingPassthrough;
Expand Down Expand Up @@ -151,22 +151,41 @@ public boolean validate() {
+ "receive any support!");
}

switch (playerInfoForwardingMode) {
boolean requireForwardingSecret = false;
for (Map.Entry<String, PlayerInfoForwarding> entry : servers.getServerForwardingModes().entrySet()) {
switch (entry.getValue()) {
case NONE:
logger.warn("Player info forwarding is disabled for {}!"
+ " All players will appear to be connecting from the proxy and will have offline-mode UUIDs.", entry.getKey());
break;
case MODERN:
case BUNGEEGUARD:
requireForwardingSecret = true;
break;
default:
break;
}
}

switch (defaultForwardingMode) {
case NONE:
logger.warn("Player info forwarding is disabled! All players will appear to be connecting "
logger.warn("Player info forwarding is disabled by default! All players will appear to be connecting "
+ "from the proxy and will have offline-mode UUIDs.");
break;
case MODERN:
case BUNGEEGUARD:
if (forwardingSecret == null || forwardingSecret.length == 0) {
logger.error("You don't have a forwarding secret set. This is required for security.");
valid = false;
}
requireForwardingSecret = true;
break;
default:
break;
}

if (requireForwardingSecret && (forwardingSecret == null || forwardingSecret.length == 0)) {
logger.error("You don't have a forwarding secret set. This is required for security. "
+ "See https://docs.papermc.io/velocity/player-information-forwarding#configuring-modern-forwarding for more details.");
valid = false;
}

if (servers.getServers().isEmpty()) {
logger.warn("You don't have any servers configured.");
}
Expand Down Expand Up @@ -293,8 +312,12 @@ public boolean shouldPreventClientProxyConnections() {
return preventClientProxyConnections;
}

public PlayerInfoForwarding getPlayerInfoForwardingMode() {
return playerInfoForwardingMode;
public PlayerInfoForwarding getDefaultForwardingMode() {
return defaultForwardingMode;
}

public PlayerInfoForwarding getServerForwardingMode(String server) {
return servers.getServerForwardingModes().getOrDefault(server, defaultForwardingMode);
}

public byte[] getForwardingSecret() {
Expand Down Expand Up @@ -414,7 +437,7 @@ public String toString() {
.add("motd", motd)
.add("showMaxPlayers", showMaxPlayers)
.add("onlineMode", onlineMode)
.add("playerInfoForwardingMode", playerInfoForwardingMode)
.add("defaultForwardingMode", defaultForwardingMode)
.add("forwardingSecret", forwardingSecret)
.add("announceForge", announceForge)
.add("servers", servers)
Expand Down Expand Up @@ -572,16 +595,33 @@ private static class Servers {
"minigames", "127.0.0.1:30068"
);
private List<String> attemptConnectionOrder = ImmutableList.of("lobby");
private Map<String, PlayerInfoForwarding> serverForwardingModes = ImmutableMap.of();

private Servers() {
}

private Servers(CommentedConfig config) {
if (config != null) {
Map<String, String> servers = new HashMap<>();
Map<String, PlayerInfoForwarding> serverForwardingModes = new HashMap<>();
for (UnmodifiableConfig.Entry entry : config.entrySet()) {
if (entry.getValue() instanceof String) {
servers.put(cleanServerName(entry.getKey()), entry.getValue());
} else if (entry.getValue() instanceof UnmodifiableConfig) {
UnmodifiableConfig unmodifiableConfig = entry.getValue();
String name = entry.getKey();

String address = unmodifiableConfig.get("address");
if (address == null) {
throw new IllegalArgumentException("Server " + name + " doesn't have an address!");
}

PlayerInfoForwarding mode = unmodifiableConfig.getEnum("forwarding-mode", PlayerInfoForwarding.class);
if (mode != null) {
serverForwardingModes.put(name, mode);
}

servers.put(name, address);
} else {
if (!entry.getKey().equalsIgnoreCase("try")) {
throw new IllegalArgumentException(
Expand All @@ -591,12 +631,14 @@ private Servers(CommentedConfig config) {
}
this.servers = ImmutableMap.copyOf(servers);
this.attemptConnectionOrder = config.getOrElse("try", attemptConnectionOrder);
this.serverForwardingModes = ImmutableMap.copyOf(serverForwardingModes);
}
}

private Servers(Map<String, String> servers, List<String> attemptConnectionOrder) {
private Servers(Map<String, String> servers, List<String> attemptConnectionOrder, Map<String, PlayerInfoForwarding> serverForwardingModes) {
this.servers = servers;
this.attemptConnectionOrder = attemptConnectionOrder;
this.serverForwardingModes = serverForwardingModes;
}

private Map<String, String> getServers() {
Expand All @@ -615,6 +657,14 @@ public void setAttemptConnectionOrder(List<String> attemptConnectionOrder) {
this.attemptConnectionOrder = attemptConnectionOrder;
}

public Map<String, PlayerInfoForwarding> getServerForwardingModes() {
return serverForwardingModes;
}

public void setServerForwardingModes(Map<String, PlayerInfoForwarding> serverForwardingModes) {
this.serverForwardingModes = serverForwardingModes;
}

/**
* TOML requires keys to match a regex of {@code [A-Za-z0-9_-]} unless it is wrapped in quotes;
* however, the TOML parser returns the key with the quotes so we need to clean the server name
Expand All @@ -632,6 +682,7 @@ public String toString() {
return "Servers{"
+ "servers=" + servers
+ ", attemptConnectionOrder=" + attemptConnectionOrder
+ ", serverForwardingModes=" + serverForwardingModes
+ '}';
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public boolean handle(EncryptionRequestPacket packet) {
public boolean handle(LoginPluginMessagePacket packet) {
MinecraftConnection mc = serverConn.ensureConnected();
VelocityConfiguration configuration = server.getConfiguration();
if (configuration.getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN
if (configuration.getServerForwardingMode(serverConn.getServerInfo().getName()) == PlayerInfoForwarding.MODERN
&& packet.getChannel().equals(PlayerDataForwarding.CHANNEL)) {

int requestedForwardingVersion = PlayerDataForwarding.MODERN_DEFAULT;
Expand Down Expand Up @@ -143,7 +143,8 @@ public boolean handle(SetCompressionPacket packet) {

@Override
public boolean handle(ServerLoginSuccessPacket packet) {
if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN && !informationForwarded) {
if (server.getConfiguration().getServerForwardingMode(serverConn.getServerInfo().getName()) == PlayerInfoForwarding.MODERN
&& !informationForwarded) {
resultFuture.complete(ConnectionRequestResults.forDisconnect(MODERN_IP_FORWARDING_FAILURE, serverConn.getServer()));
serverConn.disconnect();
return true;
Expand Down Expand Up @@ -202,7 +203,7 @@ public void exception(Throwable throwable) {

@Override
public void disconnected() {
if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.LEGACY) {
if (server.getConfiguration().getServerForwardingMode(serverConn.getServerInfo().getName()) == PlayerInfoForwarding.LEGACY) {
resultFuture.completeExceptionally(new QuietRuntimeException(
"""
The connection to the remote server was unexpectedly closed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private String createBungeeGuardForwardingAddress(byte[] forwardingSecret) {

private void startHandshake() {
final MinecraftConnection mc = ensureConnected();
PlayerInfoForwarding forwardingMode = server.getConfiguration().getPlayerInfoForwardingMode();
PlayerInfoForwarding forwardingMode = server.getConfiguration().getServerForwardingMode(registeredServer.getServerInfo().getName());

// Initiate the handshake.
ProtocolVersion protocolVersion = proxyPlayer.getConnection().getProtocolVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class AuthSessionHandler implements MinecraftSessionHandler {
public void activated() {
// Some connection types may need to alter the game profile.
profile = mcConnection.getType().addGameProfileTokensIfRequired(profile,
server.getConfiguration().getPlayerInfoForwardingMode());
server.getConfiguration().getDefaultForwardingMode());
GameProfileRequestEvent profileRequestEvent = new GameProfileRequestEvent(inbound, profile,
onlineMode);
final GameProfile finalProfile = profile;
Expand Down Expand Up @@ -139,7 +139,7 @@ private void startLoginCompletion(ConnectedPlayer player) {
}
VelocityConfiguration configuration = server.getConfiguration();
UUID playerUniqueId = player.getUniqueId();
if (configuration.getPlayerInfoForwardingMode() == PlayerInfoForwarding.NONE) {
if (configuration.getDefaultForwardingMode() == PlayerInfoForwarding.NONE) {
playerUniqueId = UuidUtils.generateOfflinePlayerUuid(player.getUsername());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private void handleLogin(HandshakePacket handshake, InitialInboundConnection ic)

// If the proxy is configured for modern forwarding, we must deny connections from 1.12.2
// and lower, otherwise IP information will never get forwarded.
if (server.getConfiguration().getPlayerInfoForwardingMode() == PlayerInfoForwarding.MODERN
if (server.getConfiguration().getDefaultForwardingMode() == PlayerInfoForwarding.MODERN
&& handshake.getProtocolVersion().lessThan(ProtocolVersion.MINECRAFT_1_13)) {
// Bump connection into correct protocol state so that we can send the disconnect packet.
connection.setState(StateRegistry.LOGIN);
Expand Down
5 changes: 4 additions & 1 deletion proxy/src/main/resources/default-velocity.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ forwarding-secret-file = "forwarding.secret"

# Announce whether or not your server supports Forge. If you run a modded server, we
# suggest turning this on.
#
#
# If your network runs one modpack consistently, consider using ping-passthrough = "mods"
# instead for a nicer display in the server list.
announce-forge = false
Expand Down Expand Up @@ -76,6 +76,9 @@ lobby = "127.0.0.1:30066"
factions = "127.0.0.1:30067"
minigames = "127.0.0.1:30068"

# If you need a different forwarding mode, specify it like this
modern = { address = "127.0.0.1:30069", forwarding-mode = "MODERN" }

# In what order we should try servers when a player logs in or is kicked from a server.
try = [
"lobby"
Expand Down