papermc/patches/server/0273-Handle-Large-Packets-disconnecting-client.patch

136 lines
7 KiB
Diff
Raw Normal View History

2021-06-11 12:02:28 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Tue, 27 Nov 2018 21:18:06 -0500
Subject: [PATCH] Handle Large Packets disconnecting client
If a players inventory is too big to send in a single packet,
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
index 02b3f5c67b47a098f7fe15ddba0df6cb586a9ae5..157f055df00faf3a7870df8109e84fdb12f55964 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/Connection.java
+++ b/src/main/java/net/minecraft/network/Connection.java
2023-09-22 22:33:14 +00:00
@@ -156,6 +156,22 @@ public class Connection extends SimpleChannelInboundHandler<Packet<?>> {
2021-06-11 12:02:28 +00:00
}
public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) {
+ // Paper start - Handle large packets disconnecting client
+ if (throwable instanceof io.netty.handler.codec.EncoderException && throwable.getCause() instanceof PacketEncoder.PacketTooLargeException packetTooLargeException) {
2023-09-21 22:26:51 +00:00
+ final Packet<?> packet = packetTooLargeException.getPacket();
+ final io.netty.util.Attribute<ConnectionProtocol.CodecData<?>> codecDataAttribute = channelhandlercontext.channel().attr(packetTooLargeException.codecKey);
+ if (packet.packetTooLarge(this)) {
+ ProtocolSwapHandler.swapProtocolIfNeeded(codecDataAttribute, packet);
+ return;
2023-09-21 22:26:51 +00:00
+ } else if (packet.isSkippable()) {
+ Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause());
2023-09-21 22:26:51 +00:00
+ ProtocolSwapHandler.swapProtocolIfNeeded(codecDataAttribute, packet);
2021-06-11 12:02:28 +00:00
+ return;
+ } else {
+ throwable = throwable.getCause();
+ }
+ }
+ // Paper end - Handle large packets disconnecting client
2021-06-11 12:02:28 +00:00
if (throwable instanceof SkipPacketException) {
Connection.LOGGER.debug("Skipping packet due to errors", throwable.getCause());
} else {
diff --git a/src/main/java/net/minecraft/network/PacketEncoder.java b/src/main/java/net/minecraft/network/PacketEncoder.java
index 7070d093c80033b61a9c8495bc56153d986b6b03..61f05f34ca33837c643f2915e753ec3935a38314 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/PacketEncoder.java
+++ b/src/main/java/net/minecraft/network/PacketEncoder.java
2023-09-21 22:26:51 +00:00
@@ -41,7 +41,7 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
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);
2021-06-11 12:02:28 +00:00
}
2023-09-21 22:26:51 +00:00
@@ -54,10 +54,35 @@ public class PacketEncoder extends MessageToByteEncoder<Packet<?>> {
throw var13;
} finally {
+ // Paper start - Handle large packets disconnecting client
2023-09-21 22:26:51 +00:00
+ int packetLength = friendlyByteBuf.readableBytes();
+ if (packetLength > MAX_PACKET_SIZE) {
+ throw new PacketTooLargeException(packet, this.codecKey, packetLength);
+ }
+ // Paper end - Handle large packets disconnecting client
2023-09-21 22:26:51 +00:00
ProtocolSwapHandler.swapProtocolIfNeeded(attribute, packet);
2021-06-11 12:02:28 +00:00
}
2023-09-21 22:26:51 +00:00
2021-06-11 12:02:28 +00:00
}
}
}
+
+ // Paper start
+ private static int MAX_PACKET_SIZE = 8388608;
2021-06-11 12:02:28 +00:00
+
+ public static class PacketTooLargeException extends RuntimeException {
+ private final Packet<?> packet;
2023-09-21 22:26:51 +00:00
+ public final AttributeKey<ConnectionProtocol.CodecData<?>> codecKey;
2021-06-11 12:02:28 +00:00
+
2023-09-21 22:26:51 +00:00
+ PacketTooLargeException(Packet<?> packet, AttributeKey<ConnectionProtocol.CodecData<?>> codecKey, int packetLength) {
2021-06-11 12:02:28 +00:00
+ super("PacketTooLarge - " + packet.getClass().getSimpleName() + " is " + packetLength + ". Max is " + MAX_PACKET_SIZE);
+ this.packet = packet;
2023-09-21 22:26:51 +00:00
+ this.codecKey = codecKey;
2021-06-11 12:02:28 +00:00
+ }
+
+ public Packet<?> getPacket() {
2023-09-21 22:26:51 +00:00
+ return this.packet;
2021-06-11 12:02:28 +00:00
+ }
+ }
+ // Paper end
}
diff --git a/src/main/java/net/minecraft/network/protocol/Packet.java b/src/main/java/net/minecraft/network/protocol/Packet.java
2023-09-22 17:59:56 +00:00
index 700418bb0c9fbed3f161611881b1e222248ca4eb..cc658a61065d5c0021a4b88fa58b40211b94f8ec 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/protocol/Packet.java
+++ b/src/main/java/net/minecraft/network/protocol/Packet.java
2023-09-21 22:26:51 +00:00
@@ -10,6 +10,12 @@ public interface Packet<T extends PacketListener> {
2021-06-11 12:02:28 +00:00
void handle(T listener);
+ // Paper start
2021-06-13 08:26:58 +00:00
+ default boolean packetTooLarge(net.minecraft.network.Connection manager) {
2021-06-11 12:02:28 +00:00
+ return false;
+ }
+ // Paper end
+
default boolean isSkippable() {
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
2023-09-22 17:59:56 +00:00
index 0e076173033278587df2b5dfbd01cc9005651eb5..dbd8b9b09b82c1b75e8be9dc7416d9f0863c8c87 100644
2021-06-11 12:02:28 +00:00
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundContainerSetContentPacket.java
2021-07-07 06:52:40 +00:00
@@ -31,6 +31,16 @@ public class ClientboundContainerSetContentPacket implements Packet<ClientGamePa
this.carriedItem = buf.readItem();
}
2021-06-11 12:02:28 +00:00
+ // Paper start
2021-06-11 12:02:28 +00:00
+ @Override
2021-06-13 08:26:58 +00:00
+ public boolean packetTooLarge(net.minecraft.network.Connection manager) {
2021-06-11 12:02:28 +00:00
+ for (int i = 0 ; i < this.items.size() ; i++) {
2021-07-07 06:52:40 +00:00
+ manager.send(new ClientboundContainerSetSlotPacket(this.containerId, this.stateId, i, this.items.get(i)));
2021-06-11 12:02:28 +00:00
+ }
+ return true;
+ }
+ // Paper end
2021-06-13 08:26:58 +00:00
+
2021-07-07 06:52:40 +00:00
@Override
public void write(FriendlyByteBuf buf) {
buf.writeByte(this.containerId);
2021-11-24 00:20:31 +00:00
diff --git a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
2023-09-22 17:59:56 +00:00
index 932cca4d957c1fc212b7a514ea23e8dc7ab5b9d9..f47eeb70661661610ef1a96dd9da67785825c126 100644
2021-11-24 00:20:31 +00:00
--- a/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
+++ b/src/main/java/net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java
@@ -50,7 +50,7 @@ public class ClientboundLevelChunkPacketData {
throw new RuntimeException("Can't read heightmap in packet for [" + x + ", " + z + "]");
2021-06-11 12:02:28 +00:00
} else {
2021-06-13 08:26:58 +00:00
int i = buf.readVarInt();
- if (i > 2097152) {
+ if (i > 2097152) { // Paper - diff on change - if this changes, update PacketEncoder
throw new RuntimeException("Chunk Packet trying to allocate too much memory on read.");
} else {
this.buffer = new byte[i];