Restore Handle Large Packets Disconnecting Client Patch

This commit is contained in:
Shane Freeder 2024-04-27 15:06:52 +01:00
parent e600a4de22
commit 4f09300744

View file

@ -17,13 +17,12 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ // Paper start - Handle large packets disconnecting client + // Paper start - Handle large packets disconnecting client
+ if (throwable instanceof io.netty.handler.codec.EncoderException && throwable.getCause() instanceof PacketEncoder.PacketTooLargeException packetTooLargeException) { + if (throwable instanceof io.netty.handler.codec.EncoderException && throwable.getCause() instanceof PacketEncoder.PacketTooLargeException packetTooLargeException) {
+ final Packet<?> packet = packetTooLargeException.getPacket(); + final Packet<?> packet = packetTooLargeException.getPacket();
+ final io.netty.util.Attribute<ConnectionProtocol.CodecData<?>> codecDataAttribute = channelhandlercontext.channel().attr(packetTooLargeException.codecKey);
+ if (packet.packetTooLarge(this)) { + if (packet.packetTooLarge(this)) {
+ ProtocolSwapHandler.swapProtocolIfNeeded(codecDataAttribute, packet); + ProtocolSwapHandler.handleOutboundTerminalPacket(channelhandlercontext, packet);
+ return; + return;
+ } else if (packet.isSkippable()) { + } else if (packet.isSkippable()) {
+ Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause()); + Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause());
+ ProtocolSwapHandler.swapProtocolIfNeeded(codecDataAttribute, packet); + ProtocolSwapHandler.handleOutboundTerminalPacket(channelhandlercontext, packet);
+ return; + return;
+ } else { + } else {
+ throwable = throwable.getCause(); + throwable = throwable.getCause();
@ -37,42 +36,32 @@ diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/j
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/network/PacketEncoder.java --- a/src/main/java/net/minecraft/network/PacketEncoder.java
+++ b/src/main/java/net/minecraft/network/PacketEncoder.java +++ b/src/main/java/net/minecraft/network/PacketEncoder.java
@@ -0,0 +0,0 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> { @@ -0,0 +0,0 @@ public class PacketEncoder<T extends PacketListener> extends MessageToByteEncode
int j = friendlyByteBuf.writerIndex();
packet.write(friendlyByteBuf);
int k = friendlyByteBuf.writerIndex() - j;
- if (k > 8388608) {
+ if (false && k > 8388608) { // Paper - Handle large packets disconnecting client; disable
throw new IllegalArgumentException("Packet too big (is " + k + ", should be less than 8388608): " + packet);
}
@@ -0,0 +0,0 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> { throw var9;
throw var13;
} finally { } finally {
+ // Paper start - Handle large packets disconnecting client + // Paper start - Handle large packets disconnecting client
+ int packetLength = friendlyByteBuf.readableBytes(); + int packetLength = byteBuf.readableBytes();
+ if (packetLength > MAX_PACKET_SIZE) { + if (packetLength > MAX_PACKET_SIZE || (packetLength > MAX_FINAL_PACKET_SIZE && packet.hasLargePacketFallback())) {
+ throw new PacketTooLargeException(packet, this.codecKey, packetLength); + throw new PacketTooLargeException(packet, packetLength);
+ } + }
+ // Paper end - Handle large packets disconnecting client + // Paper end - Handle large packets disconnecting client
ProtocolSwapHandler.swapProtocolIfNeeded(attribute, packet); ProtocolSwapHandler.handleOutboundTerminalPacket(channelHandlerContext, packet);
}
}
} }
} }
+ +
+ // Paper start + // Paper start
+ private static int MAX_PACKET_SIZE = 8388608; + // packet size is encoded into 3-byte varint
+ private static final int MAX_FINAL_PACKET_SIZE = (1 << 21) - 1;
+ // Vanilla Max size for the encoder (before compression)
+ private static final int MAX_PACKET_SIZE = 8388608;
+ +
+ public static class PacketTooLargeException extends RuntimeException { + public static class PacketTooLargeException extends RuntimeException {
+ private final Packet<?> packet; + private final Packet<?> packet;
+ public final AttributeKey<ConnectionProtocol.CodecData<?>> codecKey;
+ +
+ PacketTooLargeException(Packet<?> packet, AttributeKey<ConnectionProtocol.CodecData<?>> codecKey, int packetLength) { + PacketTooLargeException(Packet<?> packet, int packetLength) {
+ super("PacketTooLarge - " + packet.getClass().getSimpleName() + " is " + packetLength + ". Max is " + MAX_PACKET_SIZE); + super("PacketTooLarge - " + packet.getClass().getSimpleName() + " is " + packetLength + ". Max is " + MAX_PACKET_SIZE);
+ this.packet = packet; + this.packet = packet;
+ this.codecKey = codecKey;
+ } + }
+ +
+ public Packet<?> getPacket() { + public Packet<?> getPacket() {
@ -90,6 +79,13 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
void handle(T listener); void handle(T listener);
+ // Paper start + // Paper start
+ default boolean hasLargePacketFallback() {
+ return false;
+ }
+
+ /**
+ * override {@link #hasLargePacketFallback()} to return true when overriding in subclasses
+ */
+ default boolean packetTooLarge(net.minecraft.network.Connection manager) { + default boolean packetTooLarge(net.minecraft.network.Connection manager) {
+ return false; + return false;
+ } + }
@ -103,10 +99,15 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java +++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
@@ -0,0 +0,0 @@ public class ClientboundContainerSetContentPacket implements Packet<ClientGamePa @@ -0,0 +0,0 @@ public class ClientboundContainerSetContentPacket implements Packet<ClientGamePa
this.carriedItem = buf.readItem(); this.carriedItem = ItemStack.OPTIONAL_STREAM_CODEC.decode(buf);
} }
+ // Paper start + // Paper start - Handle large packets disconnecting client
+ @Override
+ public boolean hasLargePacketFallback() {
+ return true;
+ }
+
+ @Override + @Override
+ public boolean packetTooLarge(net.minecraft.network.Connection manager) { + public boolean packetTooLarge(net.minecraft.network.Connection manager) {
+ for (int i = 0 ; i < this.items.size() ; i++) { + for (int i = 0 ; i < this.items.size() ; i++) {
@ -114,11 +115,11 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
+ } + }
+ return true; + return true;
+ } + }
+ // Paper end + // Paper end - Handle large packets disconnecting client
+ +
@Override private void write(RegistryFriendlyByteBuf buf) {
public void write(FriendlyByteBuf buf) {
buf.writeByte(this.containerId); buf.writeByte(this.containerId);
buf.writeVarInt(this.stateId);
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java