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

Per server IP & profile forwarding configuration #1166

Closed
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2018-2021 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.proxy.config;

/**
* Represents what kind of forwarding is configured.
*/
public enum PlayerInfoForwarding {
/**
* Forward player IPs and UUIDs in a format supported by the BungeeGuard
* plugin. Use this if you run servers using Minecraft 1.12 or lower, and are"
* unable to implement network level firewalling (on a shared host).
*/
BUNGEEGUARD,

/**
* Forwarding mode will be picked from Velocity's configuration.
*/
DEFAULT,

/**
* No forwarding will be done. All players will appear to be connecting from the
* proxy and will have offline-mode UUIDs.
*/
NONE,

/**
* Forward player IPs and UUIDs in a BungeeCord-compatible format. Use this if
* you run servers using Minecraft 1.12 or lower.
*/
LEGACY,

/**
* Forward player IPs and UUIDs as part of the login process using Velocity's
* native forwarding. Only applicable for Minecraft 1.13 or higher.
*/
MODERN,
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public interface ProxyConfig {
*/
Map<String, String> getServers();

/**
* Get a Map of all servers registered in <code>velocity.toml</code> with all details.
* This method does <strong>not</strong> return all the servers currently in memory,
* although in most cases it does. For a view of all registered servers,
* see {@link ProxyServer#getAllServers()}.
*
* @return registered servers map
*/
Map<String, ServerConnectionInfo> getServersDetailed();

/**
* Get the order of servers that players will be connected to.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2018-2021 Velocity Contributors
*
* The Velocity API is licensed under the terms of the MIT License. For more details,
* reference the LICENSE file in the api top-level directory.
*/

package com.velocitypowered.api.proxy.config;

import java.util.Objects;

/**
* Exposes certain server information.
*/
public final class ServerConnectionInfo {
private final String address;
private final PlayerInfoForwarding forwarding;

public ServerConnectionInfo(String address, PlayerInfoForwarding forwarding) {
this.address = address;
this.forwarding = forwarding;
}

public String getAddress() {
return address;
}

public PlayerInfoForwarding getForwarding() {
return forwarding;
}

public static ServerConnectionInfo of(String address, PlayerInfoForwarding forwarding) {
return new ServerConnectionInfo(address, forwarding);
}

@Override
public int hashCode() {
return Objects.hash(address, forwarding);
}

@Override
public String toString() {
if (forwarding == PlayerInfoForwarding.DEFAULT) {
return "{ address = \"" + address + "\" }";
} else {
return "{ address = \"" + address + "\", forwarding = \"" + forwarding.name() + "\" }";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.velocitypowered.api.proxy.server;

import com.google.common.base.Preconditions;
import com.velocitypowered.api.proxy.config.PlayerInfoForwarding;
import java.net.InetSocketAddress;
import java.util.Objects;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -20,16 +21,21 @@ public final class ServerInfo implements Comparable<ServerInfo> {

private final String name;
private final InetSocketAddress address;
private final PlayerInfoForwarding playerInfoForwarding;

/**
* Creates a new ServerInfo object.
*
* @param name the name for the server
* @param address the address of the server to connect to
* @param playerInfoForwarding the player info forwarding mode this connection will use
*/
public ServerInfo(String name, InetSocketAddress address) {
public ServerInfo(String name, InetSocketAddress address,
PlayerInfoForwarding playerInfoForwarding) {
this.name = Preconditions.checkNotNull(name, "name");
this.address = Preconditions.checkNotNull(address, "address");
this.playerInfoForwarding = Preconditions.checkNotNull(playerInfoForwarding,
"playerInfoForwarding");
}

public final String getName() {
Expand All @@ -40,11 +46,16 @@ public final InetSocketAddress getAddress() {
return address;
}

public final PlayerInfoForwarding getPlayerInfoForwarding() {
return playerInfoForwarding;
}

@Override
public String toString() {
return "ServerInfo{"
+ "name='" + name + '\''
+ ", address=" + address
+ ", playerInfoForwarding=" + playerInfoForwarding
+ '}';
}

Expand All @@ -58,12 +69,13 @@ public final boolean equals(@Nullable Object o) {
}
ServerInfo that = (ServerInfo) o;
return Objects.equals(name, that.name)
&& Objects.equals(address, that.address);
&& Objects.equals(address, that.address)
&& Objects.equals(playerInfoForwarding, that.playerInfoForwarding);
}

@Override
public final int hashCode() {
return Objects.hash(name, address);
return Objects.hash(name, address, playerInfoForwarding);
}

@Override
Expand Down
14 changes: 10 additions & 4 deletions proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.velocitypowered.api.plugin.PluginManager;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.proxy.config.ServerConnectionInfo;
import com.velocitypowered.api.proxy.player.ResourcePackInfo;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import com.velocitypowered.api.proxy.server.ServerInfo;
Expand Down Expand Up @@ -233,8 +234,11 @@ void start() {

this.doStartupConfigLoad();

for (Map.Entry<String, String> entry : configuration.getServers().entrySet()) {
servers.register(new ServerInfo(entry.getKey(), AddressUtil.parseAddress(entry.getValue())));
for (Map.Entry<String, ServerConnectionInfo> entry : configuration
.getServersDetailed().entrySet()) {
ServerConnectionInfo pair = entry.getValue();
InetSocketAddress serverAddress = AddressUtil.parseAddress(pair.getAddress());
servers.register(new ServerInfo(entry.getKey(), serverAddress, pair.getForwarding()));
}

ipAttemptLimiter = Ratelimiters.createWithMilliseconds(configuration.getLoginRatelimit());
Expand Down Expand Up @@ -411,9 +415,11 @@ public boolean reloadConfiguration() throws IOException {
// Re-register servers. If a server is being replaced, make sure to note what players need to
// move back to a fallback server.
Collection<ConnectedPlayer> evacuate = new ArrayList<>();
for (Map.Entry<String, String> entry : newConfiguration.getServers().entrySet()) {
for (Map.Entry<String, ServerConnectionInfo> entry : newConfiguration
.getServersDetailed().entrySet()) {
ServerInfo newInfo =
new ServerInfo(entry.getKey(), AddressUtil.parseAddress(entry.getValue()));
new ServerInfo(entry.getKey(), AddressUtil.parseAddress(entry.getValue().getAddress()),
entry.getValue().getForwarding());
Optional<RegisteredServer> rs = servers.getServer(entry.getKey());
if (!rs.isPresent()) {
servers.register(newInfo);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.annotations.Expose;
import com.velocitypowered.api.proxy.config.PlayerInfoForwarding;
import com.velocitypowered.api.proxy.config.ProxyConfig;
import com.velocitypowered.api.proxy.config.ServerConnectionInfo;
import com.velocitypowered.api.util.Favicon;
import com.velocitypowered.proxy.util.AddressUtil;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand Down Expand Up @@ -149,6 +152,11 @@ public boolean validate() {
}

switch (playerInfoForwardingMode) {
case DEFAULT:
logger.error("DEFAULT forwarding mode is not a valid option as a default forwarding mode "
+ "setting");
valid = false;
break;
case NONE:
logger.warn("Player info forwarding is disabled! All players will appear to be connecting "
+ "from the proxy and will have offline-mode UUIDs.");
Expand All @@ -168,9 +176,9 @@ public boolean validate() {
logger.warn("You don't have any servers configured.");
}

for (Map.Entry<String, String> entry : servers.getServers().entrySet()) {
for (Map.Entry<String, ServerConnectionInfo> entry : servers.getServers().entrySet()) {
try {
AddressUtil.parseAddress(entry.getValue());
AddressUtil.parseAddress(entry.getValue().getAddress());
} catch (IllegalArgumentException e) {
logger.error("Server {} does not have a valid IP address.", entry.getKey(), e);
valid = false;
Expand Down Expand Up @@ -300,6 +308,11 @@ public byte[] getForwardingSecret() {

@Override
public Map<String, String> getServers() {
return Maps.transformValues(servers.getServers(), ServerConnectionInfo::getAddress);
}

@Override
public Map<String, ServerConnectionInfo> getServersDetailed() {
return servers.getServers();
}

Expand Down Expand Up @@ -610,10 +623,10 @@ public boolean isOnlineModeKickExistingPlayers() {

private static class Servers {

private Map<String, String> servers = ImmutableMap.of(
"lobby", "127.0.0.1:30066",
"factions", "127.0.0.1:30067",
"minigames", "127.0.0.1:30068"
private Map<String, ServerConnectionInfo> servers = ImmutableMap.of(
"lobby", ServerConnectionInfo.of("127.0.0.1:30066", PlayerInfoForwarding.DEFAULT),
"factions", ServerConnectionInfo.of("127.0.0.1:30067", PlayerInfoForwarding.DEFAULT),
"minigames", ServerConnectionInfo.of("127.0.0.1:30068", PlayerInfoForwarding.DEFAULT)
);
private List<String> attemptConnectionOrder = ImmutableList.of("lobby");

Expand All @@ -622,10 +635,22 @@ private Servers() {

private Servers(CommentedConfig config) {
if (config != null) {
Map<String, String> servers = new HashMap<>();
Map<String, ServerConnectionInfo> servers = new HashMap<>();
for (UnmodifiableConfig.Entry entry : config.entrySet()) {
if (entry.getValue() instanceof String) {
servers.put(cleanServerName(entry.getKey()), entry.getValue());
ServerConnectionInfo info = ServerConnectionInfo.of(
(String) entry.getValue(), PlayerInfoForwarding.DEFAULT
);
servers.put(cleanServerName(entry.getKey()), info);
} else if (entry.getValue() instanceof CommentedConfig) {
CommentedConfig serverTable = entry.getValue();
ServerConnectionInfo info = ServerConnectionInfo.of(
serverTable.getOrElse("address", ""),
PlayerInfoForwarding
.valueOf(serverTable.getOrElse("forwarding", "default")
.toUpperCase(Locale.ROOT))
);
servers.put(cleanServerName(entry.getKey()), info);
} else {
if (!entry.getKey().equalsIgnoreCase("try")) {
throw new IllegalArgumentException(
Expand All @@ -638,16 +663,17 @@ private Servers(CommentedConfig config) {
}
}

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

private Map<String, String> getServers() {
private Map<String, ServerConnectionInfo> getServers() {
return servers;
}

public void setServers(Map<String, String> servers) {
public void setServers(Map<String, ServerConnectionInfo> servers) {
this.servers = servers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package com.velocitypowered.proxy.connection;

import com.velocitypowered.api.proxy.config.PlayerInfoForwarding;
import com.velocitypowered.api.util.GameProfile;
import com.velocitypowered.proxy.config.PlayerInfoForwarding;
import com.velocitypowered.proxy.connection.backend.BackendConnectionPhase;
import com.velocitypowered.proxy.connection.client.ClientConnectionPhase;

Expand Down
Loading