diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java index 84720131f..cf935ff41 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/ClientPlaySessionHandler.java @@ -86,7 +86,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { @Override public boolean handle(ClientSettings packet) { player.setPlayerSettings(packet); - return false; // will forward onto the handleGeneric below, which will write the packet to the remote server + return false; // will forward onto the server } @Override @@ -164,8 +164,7 @@ public class ClientPlaySessionHandler implements MinecraftSessionHandler { List actuallyRegistered = new ArrayList<>(); List channels = PluginMessageUtil.getChannels(packet); for (String channel : channels) { - if (knownChannels.size() >= MAX_PLUGIN_CHANNELS && - !knownChannels.contains(channel)) { + if (knownChannels.size() >= MAX_PLUGIN_CHANNELS && !knownChannels.contains(channel)) { throw new IllegalStateException("Too many plugin message channels registered"); } if (knownChannels.add(channel)) { 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 a3950b0c4..a2d2e64b5 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 @@ -184,8 +184,8 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { connection.write(pkt); return; } else { - // Due to issues with action bar packets, we'll need to convert the text message into a legacy message - // and then inject the legacy text into a component... yuck! + // Due to issues with action bar packets, we'll need to convert the text message into a + // legacy message and then inject the legacy text into a component... yuck! JsonObject object = new JsonObject(); object.addProperty("text", ComponentSerializers.LEGACY.serialize(component)); json = VelocityServer.GSON.toJson(object); @@ -396,49 +396,6 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return server.getServer(toTryName); } - private Optional checkServer(RegisteredServer server) { - Preconditions - .checkState(server instanceof VelocityRegisteredServer, "Not a valid Velocity server."); - if (connectionInFlight != null) { - return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS); - } - if (connectedServer != null && connectedServer.getServer().equals(server)) { - return Optional.of(ConnectionRequestBuilder.Status.ALREADY_CONNECTED); - } - return Optional.empty(); - } - - private CompletableFuture connect( - ConnectionRequestBuilderImpl request) { - Optional initialCheck = checkServer(request.getServer()); - if (initialCheck.isPresent()) { - return CompletableFuture - .completedFuture(ConnectionRequestResults.plainResult(initialCheck.get())); - } - - // Otherwise, initiate the connection. - ServerPreConnectEvent event = new ServerPreConnectEvent(this, request.getServer()); - return server.getEventManager().fire(event) - .thenCompose(newEvent -> { - Optional connectTo = newEvent.getResult().getServer(); - if (!connectTo.isPresent()) { - return CompletableFuture.completedFuture( - ConnectionRequestResults - .plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED) - ); - } - - RegisteredServer rs = connectTo.get(); - Optional lastCheck = checkServer(rs); - if (lastCheck.isPresent()) { - return CompletableFuture - .completedFuture(ConnectionRequestResults.plainResult(lastCheck.get())); - } - return new VelocityServerConnection((VelocityRegisteredServer) rs, this, server) - .connect(); - }); - } - public void setConnectedServer(@Nullable VelocityServerConnection serverConnection) { this.connectedServer = serverConnection; this.tryIndex = 0; // reset since we got connected to a server @@ -515,20 +472,58 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { private class ConnectionRequestBuilderImpl implements ConnectionRequestBuilder { - private final RegisteredServer server; + private final RegisteredServer toConnect; - ConnectionRequestBuilderImpl(RegisteredServer server) { - this.server = Preconditions.checkNotNull(server, "info"); + ConnectionRequestBuilderImpl(RegisteredServer toConnect) { + this.toConnect = Preconditions.checkNotNull(toConnect, "info"); } @Override public RegisteredServer getServer() { - return server; + return toConnect; + } + + private Optional checkServer(RegisteredServer server) { + Preconditions + .checkState(server instanceof VelocityRegisteredServer, "Not a valid Velocity server."); + if (connectionInFlight != null) { + return Optional.of(ConnectionRequestBuilder.Status.CONNECTION_IN_PROGRESS); + } + if (connectedServer != null && connectedServer.getServer().equals(server)) { + return Optional.of(ConnectionRequestBuilder.Status.ALREADY_CONNECTED); + } + return Optional.empty(); } @Override public CompletableFuture connect() { - return ConnectedPlayer.this.connect(this); + Optional initialCheck = checkServer(toConnect); + if (initialCheck.isPresent()) { + return CompletableFuture + .completedFuture(ConnectionRequestResults.plainResult(initialCheck.get())); + } + + // Otherwise, initiate the connection. + ServerPreConnectEvent event = new ServerPreConnectEvent(ConnectedPlayer.this, toConnect); + return server.getEventManager().fire(event) + .thenCompose(newEvent -> { + Optional connectTo = newEvent.getResult().getServer(); + if (!connectTo.isPresent()) { + return CompletableFuture.completedFuture( + ConnectionRequestResults + .plainResult(ConnectionRequestBuilder.Status.CONNECTION_CANCELLED) + ); + } + + RegisteredServer rs = connectTo.get(); + Optional lastCheck = checkServer(rs); + if (lastCheck.isPresent()) { + return CompletableFuture + .completedFuture(ConnectionRequestResults.plainResult(lastCheck.get())); + } + return new VelocityServerConnection((VelocityRegisteredServer) toConnect, + ConnectedPlayer.this, server).connect(); + }); } @Override @@ -536,7 +531,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { return connect() .whenCompleteAsync((status, throwable) -> { if (throwable != null) { - handleConnectionException(server, throwable); + handleConnectionException(toConnect, throwable); return; } @@ -551,7 +546,7 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { // Ignored; the plugin probably already handled this. break; case SERVER_DISCONNECTED: - handleConnectionException(server, Disconnect.create(status.getReason() + handleConnectionException(toConnect, Disconnect.create(status.getReason() .orElse(ConnectionMessages.INTERNAL_SERVER_CONNECTION_ERROR))); break; default: diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java index da3a13259..fc92cc90e 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java @@ -95,22 +95,22 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { return true; } - // Determine if we're using Forge (1.8 to 1.12, may not be the case in 1.13) and store that in the connection + // Determine if we're using Forge (1.8 to 1.12, may not be the case in 1.13). boolean isForge = handshake.getServerAddress().endsWith("\0FML\0"); connection.setLegacyForge(isForge); - // Make sure legacy forwarding is not in use on this connection. Make sure that we do _not_ reject Forge + // Make sure legacy forwarding is not in use on this connection. Make sure that we do _not_ + // reject Forge. if (handshake.getServerAddress().contains("\0") && !isForge) { connection.closeWith(Disconnect .create(TextComponent.of("Running Velocity behind Velocity is unsupported."))); return true; } - // 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 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 - && handshake.getProtocolVersion() < - ProtocolConstants.MINECRAFT_1_13) { + && handshake.getProtocolVersion() < ProtocolConstants.MINECRAFT_1_13) { connection.closeWith(Disconnect .create(TextComponent.of("This server is only compatible with 1.13 and above."))); return true; diff --git a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java index fa5afd1c6..9c4961fc0 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/connection/client/LoginSessionHandler.java @@ -133,8 +133,8 @@ public class LoginSessionHandler implements MinecraftSessionHandler { return; } - // Go ahead and enable encryption. Once the client sends EncryptionResponse, encryption is - // enabled. + // Go ahead and enable encryption. Once the client sends EncryptionResponse, encryption + // is enabled. try { inbound.enableEncryption(decryptedSharedSecret); } catch (GeneralSecurityException e) { @@ -146,8 +146,7 @@ public class LoginSessionHandler implements MinecraftSessionHandler { initializePlayer( VelocityServer.GSON.fromJson(profileResponse.getBody(), GameProfile.class), true); } else if (profileResponse.getCode() == 204) { - // Apparently an offline-mode user logged onto this online-mode proxy. The client has enabled - // encryption, so we need to do that as well. + // Apparently an offline-mode user logged onto this online-mode proxy. logger.warn("An offline-mode client ({} from {}) tried to connect!", login.getUsername(), playerIp); inbound.closeWith(Disconnect.create(TextComponent diff --git a/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java b/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java index dd79addde..af012a817 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/network/http/NettyHttpClient.java @@ -41,8 +41,8 @@ public class NettyHttpClient { @Override public void channelAcquired(Channel channel) throws Exception { - // We don't do anything special when acquiring channels. The channel handler cleans up after - // each connection is used. + // We don't do anything special when acquiring channels. The channel handler cleans up + // after each connection is used. } @Override @@ -59,6 +59,11 @@ public class NettyHttpClient { }; } + /** + * Attempts an HTTP GET request to the specified URL. + * @param url the URL to fetch + * @return a future representing the response + */ public CompletableFuture get(URL url) { String host = url.getHost(); int port = url.getPort();