From 31cc5288f50597cc076fca44cd751481225404a9 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 26 Jul 2018 02:42:17 -0400 Subject: [PATCH] Cleanup, renames, check protocol version of client on handshake. --- .../proxy/connection/client/ConnectedPlayer.java | 11 +++-------- .../client/HandshakeSessionHandler.java | 10 +++++++++- .../proxy/protocol/ProtocolConstants.java | 10 ++++++++++ .../proxy/protocol/packets/Disconnect.java | 15 +++++++++++++++ .../proxy/util/EncryptionUtils.java | 2 +- .../proxy/util/EncryptionUtilsTest.java | 4 ++-- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java b/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java index 6f1b0de36..a7b6053f7 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java +++ b/src/main/java/com/velocitypowered/proxy/connection/client/ConnectedPlayer.java @@ -42,8 +42,7 @@ public class ConnectedPlayer { public void handleConnectionException(ServerInfo info, Throwable throwable) { String error = ThrowableUtils.briefDescription(throwable); - Disconnect disconnect = new Disconnect(); - disconnect.setReason(ComponentSerializers.JSON.serialize(TextComponent.of(error, TextColor.RED))); + Disconnect disconnect = Disconnect.create(TextComponent.of(error, TextColor.RED)); handleConnectionException(info, disconnect); } @@ -56,9 +55,7 @@ public class ConnectedPlayer { if (connectedServer == null) { // The player isn't yet connected to a server - we should disconnect them. - Disconnect d = new Disconnect(); - d.setReason(ComponentSerializers.JSON.serialize(component)); - connection.closeWith(d); + connection.closeWith(Disconnect.create(component)); } else { Chat chat = new Chat(); chat.setMessage(ComponentSerializers.JSON.serialize(component)); @@ -71,8 +68,6 @@ public class ConnectedPlayer { } public void close(TextComponent reason) { - Disconnect disconnect = new Disconnect(); - disconnect.setReason(ComponentSerializers.JSON.serialize(reason)); - connection.closeWith(disconnect); + connection.closeWith(Disconnect.create(reason)); } } diff --git a/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java b/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java index 6a2852505..a6c9d387a 100644 --- a/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java +++ b/src/main/java/com/velocitypowered/proxy/connection/client/HandshakeSessionHandler.java @@ -2,10 +2,13 @@ package com.velocitypowered.proxy.connection.client; import com.google.common.base.Preconditions; import com.velocitypowered.proxy.protocol.MinecraftPacket; +import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.StateRegistry; +import com.velocitypowered.proxy.protocol.packets.Disconnect; import com.velocitypowered.proxy.protocol.packets.Handshake; import com.velocitypowered.proxy.connection.MinecraftConnection; import com.velocitypowered.proxy.connection.MinecraftSessionHandler; +import net.kyori.text.TextComponent; public class HandshakeSessionHandler implements MinecraftSessionHandler { private final MinecraftConnection connection; @@ -30,7 +33,12 @@ public class HandshakeSessionHandler implements MinecraftSessionHandler { break; case 2: connection.setState(StateRegistry.LOGIN); - connection.setSessionHandler(new LoginSessionHandler(connection)); + if (!ProtocolConstants.isSupported(handshake.getProtocolVersion())) { + connection.closeWith(Disconnect.create(TextComponent.of("Unsupported client"))); + return; + } else { + connection.setSessionHandler(new LoginSessionHandler(connection)); + } break; default: throw new IllegalArgumentException("Invalid state " + handshake.getNextStatus()); diff --git a/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java b/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java index 57d6fe9ee..3201ac14d 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/ProtocolConstants.java @@ -1,8 +1,18 @@ package com.velocitypowered.proxy.protocol; +import java.util.Arrays; + public enum ProtocolConstants { ; public static final int MINECRAFT_1_12 = 340; + private static final int[] SUPPORTED_VERSIONS = new int[] { + MINECRAFT_1_12 + }; + + public static boolean isSupported(int version) { + return Arrays.binarySearch(SUPPORTED_VERSIONS, version) >= 0; + } + public enum Direction { TO_SERVER, TO_CLIENT diff --git a/src/main/java/com/velocitypowered/proxy/protocol/packets/Disconnect.java b/src/main/java/com/velocitypowered/proxy/protocol/packets/Disconnect.java index 9c7600843..e85f6adcb 100644 --- a/src/main/java/com/velocitypowered/proxy/protocol/packets/Disconnect.java +++ b/src/main/java/com/velocitypowered/proxy/protocol/packets/Disconnect.java @@ -1,13 +1,23 @@ package com.velocitypowered.proxy.protocol.packets; +import com.google.common.base.Preconditions; import com.velocitypowered.proxy.protocol.ProtocolConstants; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; import io.netty.buffer.ByteBuf; +import net.kyori.text.TextComponent; +import net.kyori.text.serializer.ComponentSerializers; public class Disconnect implements MinecraftPacket { private String reason; + public Disconnect() { + } + + public Disconnect(String reason) { + this.reason = reason; + } + public String getReason() { return reason; } @@ -32,4 +42,9 @@ public class Disconnect implements MinecraftPacket { public void encode(ByteBuf buf, ProtocolConstants.Direction direction, int protocolVersion) { ProtocolUtils.writeString(buf, reason); } + + public static Disconnect create(TextComponent component) { + Preconditions.checkNotNull(component, "component"); + return new Disconnect(ComponentSerializers.JSON.serialize(component)); + } } diff --git a/src/main/java/com/velocitypowered/proxy/util/EncryptionUtils.java b/src/main/java/com/velocitypowered/proxy/util/EncryptionUtils.java index 0746ea4d7..a98a58bf9 100644 --- a/src/main/java/com/velocitypowered/proxy/util/EncryptionUtils.java +++ b/src/main/java/com/velocitypowered/proxy/util/EncryptionUtils.java @@ -3,7 +3,7 @@ package com.velocitypowered.proxy.util; import java.math.BigInteger; public enum EncryptionUtils { ; - public static String twoComplementsSha1Digest(byte[] digest) { + public static String twosComplementSha1Digest(byte[] digest) { return new BigInteger(digest).toString(16); } } diff --git a/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java b/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java index 7f2e93933..5f0c53602 100644 --- a/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java +++ b/src/test/java/com/velocitypowered/proxy/util/EncryptionUtilsTest.java @@ -9,7 +9,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; class EncryptionUtilsTest { @Test - void twoComplementsSha1Digest() throws Exception { + void twosComplementSha1Digest() throws Exception { String notchHash = hexDigest("Notch"); assertEquals("4ed1f46bbe04bc756bcb17c0c7ce3e4632f06a48", notchHash); @@ -21,6 +21,6 @@ class EncryptionUtilsTest { MessageDigest digest = MessageDigest.getInstance("SHA-1"); digest.update(str.getBytes(StandardCharsets.UTF_8)); byte[] digested = digest.digest(); - return EncryptionUtils.twoComplementsSha1Digest(digested); + return EncryptionUtils.twosComplementSha1Digest(digested); } }