From 1e178cfe2ac4f542a0005fefbe4c015ae34b2ed9 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Sun, 26 Aug 2018 18:18:02 -0400 Subject: [PATCH] Properly block connecting to Velocity with legacy forwarding. I also took the opportunity to improve the disconnect handling for Disconnect messages from the server. --- .../connection/client/ConnectedPlayer.java | 6 ++++- .../client/HandshakeSessionHandler.java | 25 +++++++++++++------ .../proxy/protocol/packet/Handshake.java | 2 +- 3 files changed, 23 insertions(+), 10 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 3c0789cd4..b6089120e 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 @@ -208,7 +208,11 @@ public class ConnectedPlayer implements MinecraftConnectionAssociation, Player { } else { logger.error("{}: disconnected while connecting to {}: {}", this, info.getName(), plainTextReason); } - handleConnectionException(info, disconnectReason); + handleConnectionException(info, TextComponent.builder() + .content("Unable to connect to " + info.getName() + ": ") + .color(TextColor.RED) + .append(disconnectReason) + .build()); } public void handleConnectionException(ServerInfo info, Component disconnectReason) { 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 58ce7bff5..d88fc0f9f 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 @@ -55,18 +55,27 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { case StateRegistry.LOGIN_ID: connection.setState(StateRegistry.LOGIN); connection.setProtocolVersion(handshake.getProtocolVersion()); + if (!ProtocolConstants.isSupported(handshake.getProtocolVersion())) { connection.closeWith(Disconnect.create(TranslatableComponent.of("multiplayer.disconnect.outdated_client"))); return; - } else { - InetAddress address = ((InetSocketAddress) connection.getChannel().remoteAddress()).getAddress(); - if (!VelocityServer.getServer().getIpAttemptLimiter().attempt(address)) { - connection.closeWith(Disconnect.create(TextComponent.of("You are logging in too fast, try again later."))); - return; - } - VelocityServer.getServer().getEventManager().fireAndForget(new ConnectionHandshakeEvent(ic)); - connection.setSessionHandler(new LoginSessionHandler(connection, ic)); } + + InetAddress address = ((InetSocketAddress) connection.getChannel().remoteAddress()).getAddress(); + if (!VelocityServer.getServer().getIpAttemptLimiter().attempt(address)) { + connection.closeWith(Disconnect.create(TextComponent.of("You are logging in too fast, try again later."))); + return; + } + + // Make sure legacy forwarding is not in use on this connection. Make sure that we do _not_ reject Forge, + // although Velocity does not yet support Forge. + if (handshake.getServerAddress().contains("\0") && !handshake.getServerAddress().endsWith("\0FML\0")) { + connection.closeWith(Disconnect.create(TextComponent.of("Running Velocity behind Velocity is unsupported."))); + return; + } + + VelocityServer.getServer().getEventManager().fireAndForget(new ConnectionHandshakeEvent(ic)); + connection.setSessionHandler(new LoginSessionHandler(connection, ic)); break; default: throw new IllegalArgumentException("Invalid state " + handshake.getNextStatus()); diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java index 2e49d1a98..dec9ecb4a 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/packet/Handshake.java @@ -56,7 +56,7 @@ public class Handshake implements MinecraftPacket { @Override public void decode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { this.protocolVersion = ProtocolUtils.readVarInt(buf); - this.serverAddress = ProtocolUtils.readString(buf, 255); + this.serverAddress = ProtocolUtils.readString(buf); this.port = buf.readUnsignedShort(); this.nextStatus = ProtocolUtils.readVarInt(buf); }