From 8df4467392e1136c14edcfeae0031c120aecb789 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Mon, 17 Feb 2020 19:34:22 -0500 Subject: [PATCH] Upon connection exception, discard all incoming packets instead --- .../protocol/netty/MinecraftDecoder.java | 40 +++++++++++++++---- .../proxy/util/except/QuietException.java | 17 ++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 proxy/src/main/java/com/velocitypowered/proxy/util/except/QuietException.java diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java index a6f616941..0204aefb3 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftDecoder.java @@ -5,6 +5,7 @@ import com.velocitypowered.api.network.ProtocolVersion; import com.velocitypowered.proxy.protocol.MinecraftPacket; import com.velocitypowered.proxy.protocol.ProtocolUtils; import com.velocitypowered.proxy.protocol.StateRegistry; +import com.velocitypowered.proxy.util.except.QuietException; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.CorruptedFrameException; @@ -13,6 +14,11 @@ import java.util.List; public class MinecraftDecoder extends MessageToMessageDecoder { + private static final boolean DEBUG = Boolean.getBoolean("velocity.packet-decode-logging"); + private static final QuietException DECODE_FAILED = + new QuietException("A packet did not decode successfully (invalid data). If you are a " + + "developer, launch Velocity with -Dvelocity.packet-decode-logging=true to see more."); + private final ProtocolUtils.Direction direction; private StateRegistry state; private StateRegistry.PacketRegistry.ProtocolRegistry registry; @@ -46,21 +52,39 @@ public class MinecraftDecoder extends MessageToMessageDecoder { try { packet.decode(msg, direction, registry.version); } catch (Exception e) { - throw new CorruptedFrameException( - "Error decoding " + packet.getClass() + " Direction " + direction - + " Protocol " + registry.version + " State " + state + " ID " + Integer - .toHexString(packetId), e); + throw handleDecodeFailure(e, packet, packetId); } + if (msg.isReadable()) { - throw new CorruptedFrameException( - "Did not read full packet for " + packet.getClass() + " Direction " + direction - + " Protocol " + registry.version + " State " + state + " ID " + Integer - .toHexString(packetId)); + throw handleNotReadEnough(packet, packetId); } out.add(packet); } } + private Exception handleNotReadEnough(MinecraftPacket packet, int packetId) { + if (DEBUG) { + return new CorruptedFrameException("Did not read full packet for " + packet.getClass() + " " + + getExtraConnectionDetail(packetId)); + } else { + return DECODE_FAILED; + } + } + + private Exception handleDecodeFailure(Exception cause, MinecraftPacket packet, int packetId) { + if (DEBUG) { + return new CorruptedFrameException( + "Error decoding " + packet.getClass() + " " + getExtraConnectionDetail(packetId), cause); + } else { + return DECODE_FAILED; + } + } + + private String getExtraConnectionDetail(int packetId) { + return "Direction " + direction + " Protocol " + registry.version + " State " + state + + " ID " + Integer.toHexString(packetId); + } + public void setProtocolVersion(ProtocolVersion protocolVersion) { this.registry = direction.getProtocolRegistry(state, protocolVersion); } diff --git a/proxy/src/main/java/com/velocitypowered/proxy/util/except/QuietException.java b/proxy/src/main/java/com/velocitypowered/proxy/util/except/QuietException.java new file mode 100644 index 000000000..d16a00724 --- /dev/null +++ b/proxy/src/main/java/com/velocitypowered/proxy/util/except/QuietException.java @@ -0,0 +1,17 @@ +package com.velocitypowered.proxy.util.except; + +/** + * A special-purpose exception thrown when we want to indicate an error condition but do not want + * to see a large stack trace in logs. + */ +public class QuietException extends RuntimeException { + + public QuietException(String message) { + super(message); + } + + @Override + public synchronized Throwable fillInStackTrace() { + return this; + } +}