3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-06 00:00:47 +01:00

Ability to specify servers from the command line (#1445)

Dieser Commit ist enthalten in:
Jackson 2024-10-21 11:09:55 +11:00 committet von GitHub
Ursprung 1e6396d6c2
Commit d4e89dbdda
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
2 geänderte Dateien mit 62 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -17,11 +17,18 @@
package com.velocitypowered.proxy;
import com.velocitypowered.api.proxy.server.ServerInfo;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.List;
import com.velocitypowered.proxy.util.AddressUtil;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import joptsimple.ValueConversionException;
import joptsimple.ValueConverter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -35,6 +42,8 @@ public final class ProxyOptions {
private final boolean help;
private final @Nullable Integer port;
private final @Nullable Boolean haproxy;
private final boolean ignoreConfigServers;
private final List<ServerInfo> servers;
ProxyOptions(final String[] args) {
final OptionParser parser = new OptionParser();
@ -49,11 +58,20 @@ public final class ProxyOptions {
"Choose whether to enable haproxy protocol. "
+ "The configuration haproxy protocol will be ignored.")
.withRequiredArg().ofType(Boolean.class);
final OptionSpec<ServerInfo> servers = parser.accepts("add-server",
"Define a server mapping. "
+ "You must ensure that server name is not also registered in the config or use --ignore-config-servers.")
.withRequiredArg().withValuesConvertedBy(new ServerInfoConverter());
final OptionSpec<Void> ignoreConfigServers = parser.accepts("ignore-config-servers",
"Skip registering servers from the config file. "
+ "Useful in dynamic setups or with the --add-server flag.");
final OptionSet set = parser.parse(args);
this.help = set.has(help);
this.port = port.value(set);
this.haproxy = haproxy.value(set);
this.servers = servers.values(set);
this.ignoreConfigServers = set.has(ignoreConfigServers);
if (this.help) {
try {
@ -75,4 +93,40 @@ public final class ProxyOptions {
public @Nullable Boolean isHaproxy() {
return this.haproxy;
}
public boolean isIgnoreConfigServers() {
return this.ignoreConfigServers;
}
public List<ServerInfo> getServers() {
return this.servers;
}
private static class ServerInfoConverter implements ValueConverter<ServerInfo> {
@Override
public ServerInfo convert(String s) {
String[] split = s.split(":", 2);
if (split.length < 2) {
throw new ValueConversionException("Invalid server format. Use <name>:<address>");
}
InetSocketAddress address;
try {
address = AddressUtil.parseAddress(split[1]);
} catch (IllegalStateException e) {
throw new ValueConversionException("Invalid hostname for server flag with name: " + split[0]);
}
return new ServerInfo(split[0], address);
}
@Override
public Class<? extends ServerInfo> valueType() {
return ServerInfo.class;
}
@Override
public String valuePattern() {
return "name>:<address";
}
}
}

Datei anzeigen

@ -275,9 +275,15 @@ public class VelocityServer implements ProxyServer, ForwardingAudience {
this.doStartupConfigLoad();
for (ServerInfo cliServer : options.getServers()) {
servers.register(cliServer);
}
if (!options.isIgnoreConfigServers()) {
for (Map.Entry<String, String> entry : configuration.getServers().entrySet()) {
servers.register(new ServerInfo(entry.getKey(), AddressUtil.parseAddress(entry.getValue())));
}
}
ipAttemptLimiter = Ratelimiters.createWithMilliseconds(configuration.getLoginRatelimit());
loadPlugins();