From f16ff65933f7dcf08300e3a2b4b8e48fcd138f24 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Wed, 1 Jul 2020 13:22:00 +0200 Subject: [PATCH] Move handshake exceptions filter to exceptionCaught handling --- .../bukkit/handlers/BukkitDecodeHandler.java | 5 +- .../bukkit/handlers/BukkitEncodeHandler.java | 5 +- .../ViaVersion/api/protocol/Protocol.java | 3 +- .../protocols/base/BaseProtocol.java | 92 ++++++++----------- 4 files changed, 47 insertions(+), 58 deletions(-) diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java index 8243bfdd9..3930581f9 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitDecodeHandler.java @@ -3,11 +3,13 @@ package us.myles.ViaVersion.bukkit.handlers; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.exception.CancelCodecException; import us.myles.ViaVersion.exception.CancelDecoderException; import us.myles.ViaVersion.exception.InformativeException; +import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.util.PipelineUtil; import java.lang.reflect.InvocationTargetException; @@ -57,7 +59,8 @@ public class BukkitDecodeHandler extends ByteToMessageDecoder { if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; // ProtocolLib compat super.exceptionCaught(ctx, cause); - if (!NMSUtil.isDebugPropertySet() && PipelineUtil.containsCause(cause, InformativeException.class)) { + if (!NMSUtil.isDebugPropertySet() && PipelineUtil.containsCause(cause, InformativeException.class) + && (info.getProtocolInfo().getState() != State.HANDSHAKE || Via.getManager().isDebug())) { cause.printStackTrace(); // Print if CB doesn't already do it } } diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitEncodeHandler.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitEncodeHandler.java index 41e73c357..875583b8d 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitEncodeHandler.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitEncodeHandler.java @@ -3,6 +3,7 @@ package us.myles.ViaVersion.bukkit.handlers; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; +import us.myles.ViaVersion.api.Via; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.bukkit.util.NMSUtil; import us.myles.ViaVersion.exception.CancelCodecException; @@ -10,6 +11,7 @@ import us.myles.ViaVersion.exception.CancelEncoderException; import us.myles.ViaVersion.exception.InformativeException; import us.myles.ViaVersion.handlers.ChannelHandlerContextWrapper; import us.myles.ViaVersion.handlers.ViaHandler; +import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.util.PipelineUtil; import java.lang.reflect.Field; @@ -69,7 +71,8 @@ public class BukkitEncodeHandler extends MessageToByteEncoder implements ViaHand if (PipelineUtil.containsCause(cause, CancelCodecException.class)) return; // ProtocolLib compat super.exceptionCaught(ctx, cause); - if (!NMSUtil.isDebugPropertySet() && PipelineUtil.containsCause(cause, InformativeException.class)) { + if (!NMSUtil.isDebugPropertySet() && PipelineUtil.containsCause(cause, InformativeException.class) + && (info.getProtocolInfo().getState() != State.HANDSHAKE || Via.getManager().isDebug())) { cause.printStackTrace(); // Print if CB doesn't already do it } } diff --git a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java index 0c78d098c..280836879 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java +++ b/common/src/main/java/us/myles/ViaVersion/api/protocol/Protocol.java @@ -418,7 +418,8 @@ public abstract class Protocol { + 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); + // Ensure the server has a version provider + if (Via.getManager().getProviders().get(VersionProvider.class) == null) { + wrapper.user().setActive(false); + return; + } + // Choose the pipe + int protocol = Via.getManager().getProviders().get(VersionProvider.class).getServerProtocol(wrapper.user()); + info.setServerProtocolVersion(protocol); + List> protocols = null; + + // Only allow newer clients or (1.9.2 on 1.9.4 server if the server supports it) + if (info.getProtocolVersion() >= protocol || Via.getPlatform().isOldClientsAllowed()) { + protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocol); } - 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); - // Ensure the server has a version provider - if (Via.getManager().getProviders().get(VersionProvider.class) == null) { - wrapper.user().setActive(false); - return; + ProtocolPipeline pipeline = wrapper.user().getProtocolInfo().getPipeline(); + if (protocols != null) { + for (Pair prot : protocols) { + pipeline.add(prot.getValue()); + // Ensure mapping data has already been loaded + ProtocolRegistry.completeMappingDataLoading(prot.getValue().getClass()); } - // Choose the pipe - int protocol = Via.getManager().getProviders().get(VersionProvider.class).getServerProtocol(wrapper.user()); - info.setServerProtocolVersion(protocol); - List> protocols = null; + wrapper.set(Type.VAR_INT, 0, protocol); + } - // Only allow newer clients or (1.9.2 on 1.9.4 server if the server supports it) - if (info.getProtocolVersion() >= protocol || Via.getPlatform().isOldClientsAllowed()) { - protocols = ProtocolRegistry.getProtocolPath(info.getProtocolVersion(), protocol); - } + // Add Base Protocol + pipeline.add(ProtocolRegistry.getBaseProtocol(protocol)); - ProtocolPipeline pipeline = wrapper.user().getProtocolInfo().getPipeline(); - if (protocols != null) { - for (Pair prot : protocols) { - pipeline.add(prot.getValue()); - // Ensure mapping data has already been loaded - ProtocolRegistry.completeMappingDataLoading(prot.getValue().getClass()); - } - wrapper.set(Type.VAR_INT, 0, protocol); - } - - // Add Base Protocol - pipeline.add(ProtocolRegistry.getBaseProtocol(protocol)); - - // Change state - if (state == 1) { - info.setState(State.STATUS); - } - if (state == 2) { - info.setState(State.LOGIN); - } + // Change state + if (state == 1) { + info.setState(State.STATUS); + } + if (state == 2) { + info.setState(State.LOGIN); } }); }