Restore Handle Large Packets Disconnecting Client Patch

This commit is contained in:
Shane Freeder 2024-04-27 15:06:52 +01:00
parent f85d423535
commit 5731f346e2
No known key found for this signature in database
GPG Key ID: A3F61EA5A085289C

View File

@ -7,23 +7,22 @@ If a players inventory is too big to send in a single packet,
split the inventory set into multiple packets instead. split the inventory set into multiple packets instead.
diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java diff --git a/src/main/java/net/minecraft/network/Connection.java b/src/main/java/net/minecraft/network/Connection.java
index 02b3f5c67b47a098f7fe15ddba0df6cb586a9ae5..157f055df00faf3a7870df8109e84fdb12f55964 100644 index 7fb162fa031fd76aa9a94f5fdaa3e32ceb9b9abc..58d28b6c1cc7da7d786f78308db971f7502ad844 100644
--- a/src/main/java/net/minecraft/network/Connection.java --- a/src/main/java/net/minecraft/network/Connection.java
+++ b/src/main/java/net/minecraft/network/Connection.java +++ b/src/main/java/net/minecraft/network/Connection.java
@@ -156,6 +156,22 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> { @@ -180,6 +180,21 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
} }
public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) { public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) {
+ // 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();
@ -34,45 +33,35 @@ index 02b3f5c67b47a098f7fe15ddba0df6cb586a9ae5..157f055df00faf3a7870df8109e84fdb
Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause()); Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause());
} else { } else {
diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/java/net/minecraft/network/PacketEncoder.java diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/java/net/minecraft/network/PacketEncoder.java
index 0d80fcee1831af59b06c4d00dc713bd4dad947fc..061eada043325142d33a0cec02e9e484d14a7fca 100644 index 046bfc212b640de174b300e7a05cc30bb3cac93e..af3ec112e142a2c91c46882dad6180b18f39eec2 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
@@ -41,7 +41,7 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> { @@ -40,7 +40,33 @@ 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);
}
@@ -54,9 +54,34 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> { throw var9;
} finally {
throw var13; + // Paper start - Handle large packets disconnecting client
} finally { + int packetLength = byteBuf.readableBytes();
+ // Paper start - Handle large packets disconnecting client + if (packetLength > MAX_PACKET_SIZE || (packetLength > MAX_FINAL_PACKET_SIZE && packet.hasLargePacketFallback())) {
+ int packetLength = friendlyByteBuf.readableBytes(); + throw new PacketTooLargeException(packet, packetLength);
+ if (packetLength > MAX_PACKET_SIZE) { + }
+ throw new PacketTooLargeException(packet, this.codecKey, packetLength); + // Paper end - Handle large packets disconnecting client
+ } ProtocolSwapHandler.handleOutboundTerminalPacket(channelHandlerContext, packet);
+ // Paper end - Handle large packets disconnecting client
ProtocolSwapHandler.swapProtocolIfNeeded(attribute, 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() {
@ -82,14 +71,21 @@ index 0d80fcee1831af59b06c4d00dc713bd4dad947fc..061eada043325142d33a0cec02e9e484
+ // Paper end + // Paper end
} }
diff --git a/src/main/java/net/minecraft/network/protocol/Packet.java b/src/main/java/net/minecraft/network/protocol/Packet.java diff --git a/src/main/java/net/minecraft/network/protocol/Packet.java b/src/main/java/net/minecraft/network/protocol/Packet.java
index 700418bb0c9fbed3f161611881b1e222248ca4eb..cc658a61065d5c0021a4b88fa58b40211b94f8ec 100644 index 5ee2ba1225fb7e4f02152b45adeb66f79ed1650d..c9d283b7fc9ede79dc6cbc39dfc9e7ae986a6a47 100644
--- a/src/main/java/net/minecraft/network/protocol/Packet.java --- a/src/main/java/net/minecraft/network/protocol/Packet.java
+++ b/src/main/java/net/minecraft/network/protocol/Packet.java +++ b/src/main/java/net/minecraft/network/protocol/Packet.java
@@ -10,6 +10,12 @@ public interface Packet<T extends PacketListener> { @@ -11,6 +11,19 @@ public interface Packet<T extends PacketListener> {
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;
+ } + }
@ -99,14 +95,19 @@ index 700418bb0c9fbed3f161611881b1e222248ca4eb..cc658a61065d5c0021a4b88fa58b4021
return false; return false;
} }
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
index 6206d4d71dfe95b454b22f5b3055623638e145c0..6765175c98d52e5cbc191e88e0d545a05606dfd4 100644 index 7e555ece0555b3d2a983ab2c39c5e7ec23fc7e88..8cca2ac616a2c80268c96b9f95e33f834a0fc8fd 100644
--- 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
@@ -31,6 +31,16 @@ public class ClientboundContainerSetContentPacket implements Packet<ClientGamePa @@ -36,6 +36,21 @@ 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,16 +115,16 @@ index 6206d4d71dfe95b454b22f5b3055623638e145c0..6765175c98d52e5cbc191e88e0d545a0
+ } + }
+ 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 dc657312889da4fc3222a6981223a01406b77deb..a44a82d2d5ed4d675dc1a184d5b6b935fda575dd 100644 index 2501fbaf497d226051800c53d60a39bbc80db91c..0a8d07bf68b0ceabd13c70196d357fce79dcc2c3 100644
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java --- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java +++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
@@ -49,7 +49,7 @@ public class ClientboundLevelChunkPacketData { @@ -80,7 +80,7 @@ public class ClientboundLevelChunkPacketData {
throw new RuntimeException("Can't read heightmap in packet for [" + x + ", " + z + "]"); throw new RuntimeException("Can't read heightmap in packet for [" + x + ", " + z + "]");
} else { } else {
int i = buf.readVarInt(); int i = buf.readVarInt();