From b99b79f44a5263a1422a050ee06df6f8ee0f3cec Mon Sep 17 00:00:00 2001 From: KennyTV Date: Wed, 1 Jul 2020 09:11:46 +0200 Subject: [PATCH] Only print handshake errors when debug is enabled The exceptions only now showed up with the new print handling, but 100% of the printed cases have just been invalid packets, which we can (and should) just ignore. Fixes #1854 --- .../ViaVersion/api/rewriters/TagRewriter.java | 2 +- .../protocols/base/BaseProtocol.java | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java index 2f9a017c9..0773b50e9 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java +++ b/common/src/main/java/us/myles/ViaVersion/api/rewriters/TagRewriter.java @@ -88,7 +88,7 @@ public class TagRewriter { } // Send new tags if present - if (newTags != null) { + if (newTags != null && !newTags.isEmpty()) { for (TagData tag : newTags) { wrapper.write(Type.STRING, tag.identifier); wrapper.write(Type.VAR_INT_ARRAY_PRIMITIVE, tag.entries); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java index 139640bd8..c3199c187 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java @@ -27,16 +27,27 @@ public class BaseProtocol extends SimpleProtocol { registerIncoming(State.HANDSHAKE, 0x00, 0x00, new PacketRemapper() { @Override public void registerMap() { - // select right protocol - map(Type.VAR_INT); // 0 - Client Protocol Version - map(Type.STRING); // 1 - Server Address - map(Type.UNSIGNED_SHORT); // 2 - Server Port - map(Type.VAR_INT); // 3 - Next State handler(new PacketHandler() { @Override public void handle(PacketWrapper wrapper) throws Exception { - int protVer = wrapper.get(Type.VAR_INT, 0); - int state = wrapper.get(Type.VAR_INT, 1); + try { + handleWithException(wrapper); + } catch (Exception e) { + // Only throw exceptions here when debug is enabled + // The handling has proven to be correct, but often receives invalid packets as the first packet of a connection + if (Via.getManager().isDebug()) { + throw e; + } else { + wrapper.cancel(); + } + } + } + + private void handleWithException(PacketWrapper wrapper) throws Exception { + int protVer = wrapper.passthrough(Type.VAR_INT); + wrapper.passthrough(Type.STRING); // Server Address + wrapper.passthrough(Type.UNSIGNED_SHORT); // Server Port + int state = wrapper.passthrough(Type.VAR_INT); ProtocolInfo info = wrapper.user().getProtocolInfo(); info.setProtocolVersion(protVer);