From 8bf3b99b101d87e1841e0a95d597d99f572bb3b5 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 10 Aug 2018 23:40:26 -0400 Subject: [PATCH] Make sure the client has time to respond to the player info packet. Apparently, Minecraft 1.13 can take a little too long to respond to Velocity's player info forwarding packet. This especially noticeable in offline mode: by the time the client does respond, Velocity has already completed the login process and tried connecting to the server (it is very quick under offline mode). Noticed by Leymooo. --- .../client/LoginSessionHandler.java | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) 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 d08351d14..594b8ce26 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 @@ -41,39 +41,33 @@ public class LoginSessionHandler implements MinecraftSessionHandler { this.inbound = Preconditions.checkNotNull(inbound, "inbound"); } - @Override - public void activated() { - if (inbound.getProtocolVersion() >= ProtocolConstants.MINECRAFT_1_13) { - LoginPluginMessage message = new LoginPluginMessage(); - playerInfoId = ThreadLocalRandom.current().nextInt(); - message.setId(playerInfoId); - message.setChannel(VelocityConstants.VELOCITY_IP_FORWARDING_CHANNEL); - message.setData(Unpooled.EMPTY_BUFFER); - inbound.write(message); - } - } - @Override public void handle(MinecraftPacket packet) { if (packet instanceof LoginPluginResponse) { LoginPluginResponse lpr = (LoginPluginResponse) packet; - if (lpr.getId() == playerInfoId && lpr.isSuccess()) { - // Uh oh, someone's trying to run Velocity behind Velocity. We don't want that happening. - inbound.closeWith(Disconnect.create( - TextComponent.of("Running Velocity behind Velocity isn't supported.", TextColor.RED) - )); + if (lpr.getId() == playerInfoId) { + if (lpr.isSuccess()) { + // Uh oh, someone's trying to run Velocity behind Velocity. We don't want that happening. + inbound.closeWith(Disconnect.create( + TextComponent.of("Running Velocity behind Velocity isn't supported.", TextColor.RED) + )); + } else { + // Proceed with the regular login process. + initiateLogin(); + } } } else if (packet instanceof ServerLogin) { this.login = (ServerLogin) packet; - if (VelocityServer.getServer().getConfiguration().isOnlineMode()) { - // Request encryption. - EncryptionRequest request = generateRequest(); - this.verify = Arrays.copyOf(request.getVerifyToken(), 4); - inbound.write(request); + if (inbound.getProtocolVersion() >= ProtocolConstants.MINECRAFT_1_13) { + LoginPluginMessage message = new LoginPluginMessage(); + playerInfoId = ThreadLocalRandom.current().nextInt(); + message.setId(playerInfoId); + message.setChannel(VelocityConstants.VELOCITY_IP_FORWARDING_CHANNEL); + message.setData(Unpooled.EMPTY_BUFFER); + inbound.write(message); } else { - // Offline-mode, don't try to request encryption. - handleSuccessfulLogin(GameProfile.forOfflinePlayer(login.getUsername())); + initiateLogin(); } } else if (packet instanceof EncryptionResponse) { try { @@ -119,6 +113,18 @@ public class LoginSessionHandler implements MinecraftSessionHandler { } } + private void initiateLogin() { + if (VelocityServer.getServer().getConfiguration().isOnlineMode()) { + // Request encryption. + EncryptionRequest request = generateRequest(); + this.verify = Arrays.copyOf(request.getVerifyToken(), 4); + inbound.write(request); + } else { + // Offline-mode, don't try to request encryption. + handleSuccessfulLogin(GameProfile.forOfflinePlayer(login.getUsername())); + } + } + private EncryptionRequest generateRequest() { byte[] verify = new byte[4]; ThreadLocalRandom.current().nextBytes(verify);