From 7da847bfac6fce73e0de4caed8b187c94dcc04d0 Mon Sep 17 00:00:00 2001 From: kashike Date: Tue, 20 Nov 2018 21:48:11 -0800 Subject: [PATCH 01/10] Allow specifying bind port via command line adds #140 --- proxy/build.gradle | 1 + .../velocitypowered/proxy/ProxyOptions.java | 44 +++++++++++++++++++ .../com/velocitypowered/proxy/Velocity.java | 8 +++- .../velocitypowered/proxy/VelocityServer.java | 13 +++++- 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java diff --git a/proxy/build.gradle b/proxy/build.gradle index 1901a221b..140e50405 100644 --- a/proxy/build.gradle +++ b/proxy/build.gradle @@ -38,6 +38,7 @@ dependencies { compile "org.apache.logging.log4j:log4j-core:${log4jVersion}" compile "org.apache.logging.log4j:log4j-slf4j-impl:${log4jVersion}" + compile 'net.sf.jopt-simple:jopt-simple:5.0.4' // command-line options compile 'net.minecrell:terminalconsoleappender:1.1.1' runtime 'net.java.dev.jna:jna:4.5.2' // Needed for JLine runtime 'com.lmax:disruptor:3.4.2' // Async loggers diff --git a/proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java b/proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java new file mode 100644 index 000000000..b2a12358d --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java @@ -0,0 +1,44 @@ +package com.velocitypowered.proxy; + +import joptsimple.OptionParser; +import joptsimple.OptionSet; +import joptsimple.OptionSpec; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.checkerframework.checker.nullness.qual.Nullable; + +import java.io.IOException; +import java.util.Arrays; + +public final class ProxyOptions { + private static final Logger logger = LogManager.getLogger(ProxyOptions.class); + private final boolean help; + private final @Nullable Integer port; + + ProxyOptions(final String[] args) { + final OptionParser parser = new OptionParser(); + + final OptionSpec help = parser.acceptsAll(Arrays.asList("h", "help"), "Print help").forHelp(); + final OptionSpec port = parser.acceptsAll(Arrays.asList("p", "port"), "Specify the bind port to be used. The configuration bind port will be ignored.").withRequiredArg().ofType(Integer.class); + final OptionSet set = parser.parse(args); + + this.help = set.has(help); + this.port = port.value(set); + + if (this.help) { + try { + parser.printHelpOn(System.out); + } catch (final IOException e) { + logger.error("Could not print help", e); + } + } + } + + boolean isHelp() { + return this.help; + } + + public @Nullable Integer getPort() { + return this.port; + } +} diff --git a/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java b/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java index 0d3b64c15..8774c0b96 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/Velocity.java @@ -17,7 +17,13 @@ public class Velocity { public static void main(String... args) { long startTime = System.currentTimeMillis(); - VelocityServer server = new VelocityServer(); + final ProxyOptions options = new ProxyOptions(args); + + if (options.isHelp()) { + return; + } + + VelocityServer server = new VelocityServer(options); server.start(); Runtime.getRuntime().addShutdownHook(new Thread(() -> server.shutdown(false), "Shutdown thread")); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java index 52a850e53..339199141 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/VelocityServer.java @@ -70,6 +70,7 @@ public class VelocityServer implements ProxyServer { .registerTypeHierarchyAdapter(GameProfile.class, new GameProfileSerializer()) .create(); + private final ProxyOptions options; private @MonotonicNonNull ConnectionManager cm; private @MonotonicNonNull VelocityConfiguration configuration; private @MonotonicNonNull NettyHttpClient httpClient; @@ -88,6 +89,10 @@ public class VelocityServer implements ProxyServer { private @MonotonicNonNull VelocityScheduler scheduler; private final VelocityChannelRegistrar channelRegistrar = new VelocityChannelRegistrar(); + public VelocityServer(final ProxyOptions options) { + this.options = options; + } + public KeyPair getServerKeyPair() { if (serverKeyPair == null) { throw new AssertionError(); @@ -181,7 +186,13 @@ public class VelocityServer implements ProxyServer { // init console permissions after plugins are loaded console.setupPermissions(); - this.cm.bind(configuration.getBind()); + final Integer port = this.options.getPort(); + if (port != null) { + logger.debug("Overriding bind port to {} from command line option", port); + this.cm.bind(new InetSocketAddress(configuration.getBind().getHostString(), port)); + } else { + this.cm.bind(configuration.getBind()); + } if (configuration.isQueryEnabled()) { this.cm.queryBind(configuration.getBind().getHostString(), configuration.getQueryPort()); From 9dad1a7020b7ecc86fb3340a40555fad393315d3 Mon Sep 17 00:00:00 2001 From: Lars Artmann Date: Thu, 22 Nov 2018 05:54:05 +0100 Subject: [PATCH 02/10] Removed a unnecessary check In "hasPermission" is the removed check already included --- .../com/velocitypowered/proxy/command/ShutdownCommand.java | 5 ----- 1 file changed, 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/ShutdownCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/ShutdownCommand.java index e3e8d5d6d..ba94083c8 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/ShutdownCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/ShutdownCommand.java @@ -17,11 +17,6 @@ public class ShutdownCommand implements Command { @Override public void execute(CommandSource source, String @NonNull [] args) { - if (source != server.getConsoleCommandSource()) { - source - .sendMessage(TextComponent.of("You are not allowed to use this command.", TextColor.RED)); - return; - } server.shutdown(true); } From f24102328220ab0705f1a90dfe98fad54f5955a1 Mon Sep 17 00:00:00 2001 From: Lars Artmann Date: Thu, 22 Nov 2018 22:51:19 +0100 Subject: [PATCH 03/10] Bug Fix #146 --- .../velocitypowered/proxy/command/VelocityCommandManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java index 20fe7097a..a7e2e69af 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommandManager.java @@ -23,7 +23,7 @@ public class VelocityCommandManager implements CommandManager { Preconditions.checkNotNull(command, "executor"); for (int i = 0, length = aliases.length; i < length; i++) { final String alias = aliases[i]; - Preconditions.checkNotNull(aliases, "alias at index %s", i); + Preconditions.checkNotNull(alias, "alias at index %s", i); this.commands.put(alias.toLowerCase(Locale.ENGLISH), command); } } From 40e7ef784fc83d95a9bcb214949071cf62a7459a Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 22 Nov 2018 19:29:02 -0500 Subject: [PATCH 04/10] Enhance /velocity command. Closes #145 --- .../com/velocitypowered/proxy/command/VelocityCommand.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java index d5c9f7a85..a6449a5e4 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/command/VelocityCommand.java @@ -92,7 +92,7 @@ public class VelocityCommand implements Command { @Override public boolean hasPermission(CommandSource source, String @NonNull [] args) { if (args.length == 0) { - return true; + return subcommands.values().stream().anyMatch(e -> e.hasPermission(source, args)); } Command command = subcommands.get(args[0].toLowerCase(Locale.US)); if (command == null) { @@ -113,6 +113,11 @@ public class VelocityCommand implements Command { @Override public void execute(CommandSource source, String @NonNull [] args) { + if (args.length != 0) { + source.sendMessage(TextComponent.of("/velocity version", TextColor.RED)); + return; + } + ProxyVersion version = server.getVersion(); TextComponent velocity = TextComponent.builder(version.getName() + " ") From 81d8c8374ef311d4cbf6c0f0db1ff0826b5fe250 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 23 Nov 2018 03:23:45 -0500 Subject: [PATCH 05/10] Fix up Checkstyle styles from PR --- .../java/com/velocitypowered/proxy/ProxyOptions.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java b/proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java index b2a12358d..30a617b9f 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/ProxyOptions.java @@ -1,5 +1,7 @@ package com.velocitypowered.proxy; +import java.io.IOException; +import java.util.Arrays; import joptsimple.OptionParser; import joptsimple.OptionSet; import joptsimple.OptionSpec; @@ -7,9 +9,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.checkerframework.checker.nullness.qual.Nullable; -import java.io.IOException; -import java.util.Arrays; - public final class ProxyOptions { private static final Logger logger = LogManager.getLogger(ProxyOptions.class); private final boolean help; @@ -18,8 +17,11 @@ public final class ProxyOptions { ProxyOptions(final String[] args) { final OptionParser parser = new OptionParser(); - final OptionSpec help = parser.acceptsAll(Arrays.asList("h", "help"), "Print help").forHelp(); - final OptionSpec port = parser.acceptsAll(Arrays.asList("p", "port"), "Specify the bind port to be used. The configuration bind port will be ignored.").withRequiredArg().ofType(Integer.class); + final OptionSpec help = parser.acceptsAll(Arrays.asList("h", "help"), "Print help") + .forHelp(); + final OptionSpec port = parser.acceptsAll(Arrays.asList("p", "port"), + "Specify the bind port to be used. The configuration bind port will be ignored.") + .withRequiredArg().ofType(Integer.class); final OptionSet set = parser.parse(args); this.help = set.has(help); From e2ad6b740a8f6ae8afc58f061c652eb40359c9db Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 23 Nov 2018 15:37:17 -0500 Subject: [PATCH 06/10] Do not allow connections till JoinGame is received. --- .../proxy/connection/client/ConnectedPlayer.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index e06cdacfa..b3ffe49be 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -481,7 +481,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { private Optional checkServer(RegisteredServer server) { Preconditions .checkState(server instanceof VelocityRegisteredServer, "Not a valid Velocity server."); - if (connectionInFlight != null) { + if (connectionInFlight != null || (connectedServer != null + && !connectedServer.hasCompletedJoin())) { return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS); } if (connectedServer != null && connectedServer.getServer().equals(server)) { From 436c1624bc57a892102995279b8caca8aa650c64 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 23 Nov 2018 18:25:35 -0500 Subject: [PATCH 07/10] Make sure to mark the in-flight connection. --- .../proxy/connection/client/ConnectedPlayer.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index b3ffe49be..c99a5764a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -517,8 +517,12 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return CompletableFuture .completedFuture(ConnectionRequestResults.plainResult(lastCheck.get())); } - return new VelocityServerConnection((VelocityRegisteredServer) rs, - ConnectedPlayer.this, server).connect(); + + VelocityRegisteredServer vrs = (VelocityRegisteredServer) rs; + VelocityServerConnection con = new VelocityServerConnection(vrs, ConnectedPlayer.this, + server); + connectionInFlight = con; + return con.connect(); }); } From d9be210fe31b0147e0987d73326b9f842370c9c7 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 23 Nov 2018 18:54:53 -0500 Subject: [PATCH 08/10] Fix this up --- .../proxy/connection/backend/VelocityServerConnection.java | 3 +++ .../proxy/connection/client/ConnectedPlayer.java | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java index aff20854c..47e18c9b1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/VelocityServerConnection.java @@ -94,6 +94,9 @@ public class VelocityServerConnection implements MinecraftConnectionAssociation, new LoginSessionHandler(server, VelocityServerConnection.this, result)); startHandshake(); } else { + // We need to remember to reset the in-flight connection to allow connect() to work + // properly. + proxyPlayer.resetInFlightConnection(); result.completeExceptionally(future.cause()); } }); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index c99a5764a..328e5c2aa 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -286,6 +286,10 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return connectedServer; } + public void resetInFlightConnection() { + connectionInFlight = null; + } + public void handleConnectionException(RegisteredServer server, Throwable throwable) { if (throwable == null) { throw new NullPointerException("throwable"); From 97725ed90698bf3d91e035b216404267540eacb5 Mon Sep 17 00:00:00 2001 From: js6pak <35262707+js6pak@users.noreply.github.com> Date: Sat, 24 Nov 2018 22:36:03 +0100 Subject: [PATCH 09/10] Fix GS4 k, v section Add "plugins" key before plugins list --- .../velocitypowered/proxy/protocol/netty/GS4QueryHandler.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java index f329b7f31..2695298a1 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/GS4QueryHandler.java @@ -264,6 +264,8 @@ public class GS4QueryHandler extends SimpleChannelInboundHandler return; } + writeString(buf, "plugins"); + StringBuilder pluginsString = new StringBuilder(); pluginsString.append(serverVersion).append(':').append(' '); Iterator iterator = plugins.iterator(); From bb4945b51338de72d0a94ecb628bc3756e03870a Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sat, 24 Nov 2018 20:12:29 -0500 Subject: [PATCH 10/10] Fix awkward disconnection issues. --- .../proxy/connection/backend/BackendPlaySessionHandler.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java index 3be1bfeba..04f03f764 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/backend/BackendPlaySessionHandler.java @@ -160,8 +160,7 @@ public class BackendPlaySessionHandler implements MinecraftSessionHandler { public void disconnected() { serverConn.getServer().removePlayer(serverConn.getPlayer()); if (!serverConn.isGracefulDisconnect()) { - serverConn.getPlayer().handleConnectionException(serverConn.getServer(), Disconnect.create( - ConnectionMessages.UNEXPECTED_DISCONNECT)); + serverConn.getPlayer().disconnect(ConnectionMessages.UNEXPECTED_DISCONNECT); } }