Waterfall/fire-pit/0042-Dump-a-packet-s-raw-hex-if-entity-rewriting-fails.patch

297 lines
12 KiB
Diff
Raw Normal View History

From 3f7b0d3a64c7e0b7909bb3394954bfa2501ad8fb Mon Sep 17 00:00:00 2001
From: Techcable <Techcable@techcable.net>
Date: Fri, 7 Apr 2017 20:19:15 -0700
Subject: [PATCH] Dump a packet's raw hex if entity rewriting fails.
diff --git a/proxy/src/main/java/io/github/waterfallmc/waterfall/utils/BufUtils.java b/proxy/src/main/java/io/github/waterfallmc/waterfall/utils/BufUtils.java
new file mode 100644
index 00000000..c21a707a
--- /dev/null
+++ b/proxy/src/main/java/io/github/waterfallmc/waterfall/utils/BufUtils.java
@@ -0,0 +1,40 @@
+package io.github.waterfallmc.waterfall.utils;
+
+import java.util.function.Function;
+
+import io.netty.buffer.ByteBuf;
+import net.md_5.bungee.protocol.DefinedPacket;
+
+public final class BufUtils {
+ private BufUtils() {}
+
+ /**
+ * Read a varint from the ByteBuf without changing the index.
+ */
+ public static int peekVarInt(ByteBuf buf) {
+ return peek(buf, DefinedPacket::readVarInt);
+ }
+
+ /**
+ * Read values from the byte buf, then reset it's indexes to the old value.
+ *
+ * This effectively 'peeks' at values ahead of the index.
+ * This is preferable to `resetWriterIndex` and `resetReaderIndex`,
+ * since it doesn't rely on the buffer's mark.
+ *
+ * @param buf the buffer to peek from
+ * @param func the function that should peek values
+ * @return the value returned from the function
+ * @throws Throwable any exception thrown by the callable
+ */
+ public static <R> R peek(ByteBuf buf, Function<ByteBuf, R> func) {
+ int oldReaderIndex = buf.readerIndex();
+ int oldWriterIndex = buf.writerIndex();
+ try {
+ return func.apply(buf);
+ } finally {
+ buf.readerIndex(oldReaderIndex);
+ buf.writerIndex(oldWriterIndex);
+ }
+ }
+}
diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java
index d98a8056..736bd599 100644
--- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java
+++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap.java
@@ -10,6 +10,12 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.md_5.bungee.protocol.DefinedPacket;
import net.md_5.bungee.protocol.ProtocolConstants;
+// Waterfall start
+import io.github.waterfallmc.waterfall.utils.BufUtils;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.handler.codec.DecoderException;
+import net.md_5.bungee.protocol.BadPacketException;
+// Waterfall end
/**
* Class to rewrite integers within packets.
@@ -66,13 +72,51 @@ public abstract class EntityMap
}
}
- public void rewriteServerbound(ByteBuf packet, int oldId, int newId)
+ // Waterfall start - wrap rewrite calls to provide descriptive error messages
+ public final void rewriteServerbound(ByteBuf packet, int oldId, int newId) {
+ Integer packetId = null;
+ ByteBuf originalPacket = packet.copy(); // EntityMap modifies in place, so we need a copy :(
+ try {
+ packetId = BufUtils.peekVarInt(packet);
+ rewriteServerbound0(packet, oldId, newId);
+ } catch (BadPacketException | IndexOutOfBoundsException e) {
+ String packetType = packetId != null ? "id " + Integer.toHexString(packetId) : "unknown";
+ throw new DecoderException(
+ "Error rewriting packet "
+ + packetType
+ + " with contents:\n"
+ + ByteBufUtil.prettyHexDump(originalPacket),
+ e
+ );
+ }
+ }
+
+ public final void rewriteClientbound(ByteBuf packet, int oldId, int newId) {
+ Integer packetId = null;
+ ByteBuf originalPacket = packet.copy(); // EntityMap modifies in place, so we need a copy :(
+ try {
+ packetId = BufUtils.peekVarInt(packet);
+ rewriteClientbound0(packet, oldId, newId);
+ } catch (BadPacketException | IndexOutOfBoundsException e) {
+ String packetType = packetId != null ? "id " + Integer.toHexString(packetId) : "unknown";
+ throw new DecoderException(
+ "Error rewriting packet "
+ + packetType
+ + " with contents:\n"
+ + ByteBufUtil.prettyHexDump(originalPacket),
+ e
+ );
+ }
+ }
+
+ protected void rewriteServerbound0(ByteBuf packet, int oldId, int newId)
{
rewrite( packet, oldId, newId, serverboundInts, serverboundVarInts );
}
- public void rewriteClientbound(ByteBuf packet, int oldId, int newId)
+ protected void rewriteClientbound0(ByteBuf packet, int oldId, int newId)
{
+ // Waterfall end
rewrite( packet, oldId, newId, clientboundInts, clientboundVarInts );
}
diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_10.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_10.java
index a89ea2e9..0de5ce25 100644
--- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_10.java
+++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_10.java
@@ -49,9 +49,11 @@ class EntityMap_1_10 extends EntityMap
@Override
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
- public void rewriteClientbound(ByteBuf packet, int oldId, int newId)
+ // Waterfall start - rewriteClientbound -> rewriteClientbound0
+ protected void rewriteClientbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteClientbound( packet, oldId, newId );
+ super.rewriteClientbound0( packet, oldId, newId );
+ // Waterfall end
// Special cases
int readerIndex = packet.readerIndex();
@@ -151,10 +153,13 @@ class EntityMap_1_10 extends EntityMap
packet.readerIndex( readerIndex );
}
+ // Waterfall start - rewriteServerbound -> rewriteServerbound0
@Override
- public void rewriteServerbound(ByteBuf packet, int oldId, int newId)
+ public void rewriteServerbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteServerbound( packet, oldId, newId );
+ super.rewriteServerbound0( packet, oldId, newId );
+ // Waterfall end
+
// Special cases
int readerIndex = packet.readerIndex();
int packetId = DefinedPacket.readVarInt( packet );
diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_11.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_11.java
index 260da17e..8a746116 100644
--- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_11.java
+++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_11.java
@@ -49,9 +49,11 @@ class EntityMap_1_11 extends EntityMap
@Override
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
- public void rewriteClientbound(ByteBuf packet, int oldId, int newId)
+ // Waterfall start - rewriteClientbound -> rewriteClientbound0
+ protected void rewriteClientbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteClientbound( packet, oldId, newId );
+ super.rewriteClientbound0( packet, oldId, newId );
+ // Waterfall end
// Special cases
int readerIndex = packet.readerIndex();
@@ -155,10 +157,13 @@ class EntityMap_1_11 extends EntityMap
packet.readerIndex( readerIndex );
}
+ // Waterfall start - rewriteServerbound -> rewriteServerbound0
@Override
- public void rewriteServerbound(ByteBuf packet, int oldId, int newId)
+ public void rewriteServerbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteServerbound( packet, oldId, newId );
+ super.rewriteServerbound0( packet, oldId, newId );
+ // Waterfall end
+
// Special cases
int readerIndex = packet.readerIndex();
int packetId = DefinedPacket.readVarInt( packet );
diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_8.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_8.java
index 4229fefa..23f29a4b 100644
--- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_8.java
+++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_8.java
@@ -49,9 +49,11 @@ class EntityMap_1_8 extends EntityMap
@Override
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
- public void rewriteClientbound(ByteBuf packet, int oldId, int newId)
+ // Waterfall start - rewriteClientbound -> rewriteClientbound0
+ protected void rewriteClientbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteClientbound( packet, oldId, newId );
+ super.rewriteClientbound0( packet, oldId, newId );
+ // Waterfall end
//Special cases
int readerIndex = packet.readerIndex();
@@ -149,10 +151,13 @@ class EntityMap_1_8 extends EntityMap
packet.readerIndex( readerIndex );
}
+ // Waterfall start - rewriteServerbound -> rewriteServerbound0
@Override
- public void rewriteServerbound(ByteBuf packet, int oldId, int newId)
+ public void rewriteServerbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteServerbound( packet, oldId, newId );
+ super.rewriteServerbound0( packet, oldId, newId );
+ // Waterfall end
+
//Special cases
int readerIndex = packet.readerIndex();
int packetId = DefinedPacket.readVarInt( packet );
diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9.java
index 19d96776..80075eb4 100644
--- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9.java
+++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9.java
@@ -49,9 +49,11 @@ class EntityMap_1_9 extends EntityMap
@Override
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
- public void rewriteClientbound(ByteBuf packet, int oldId, int newId)
+ // Waterfall start - rewriteClientbound -> rewriteClientbound0
+ protected void rewriteClientbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteClientbound( packet, oldId, newId );
+ super.rewriteClientbound0( packet, oldId, newId );
+ // Waterfall end
// Special cases
int readerIndex = packet.readerIndex();
@@ -151,10 +153,13 @@ class EntityMap_1_9 extends EntityMap
packet.readerIndex( readerIndex );
}
+ // Waterfall start - rewriteServerbound -> rewriteServerbound0
@Override
- public void rewriteServerbound(ByteBuf packet, int oldId, int newId)
+ public void rewriteServerbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteServerbound( packet, oldId, newId );
+ super.rewriteServerbound0( packet, oldId, newId );
+ // Waterfall end
+
// Special cases
int readerIndex = packet.readerIndex();
int packetId = DefinedPacket.readVarInt( packet );
diff --git a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9_4.java b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9_4.java
index 412ab4ed..ae5ce7f0 100644
--- a/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9_4.java
+++ b/proxy/src/main/java/net/md_5/bungee/entitymap/EntityMap_1_9_4.java
@@ -49,9 +49,11 @@ class EntityMap_1_9_4 extends EntityMap
@Override
@SuppressFBWarnings("DLS_DEAD_LOCAL_STORE")
- public void rewriteClientbound(ByteBuf packet, int oldId, int newId)
+ // Waterfall start - rewriteClientbound -> rewriteClientbound0
+ protected void rewriteClientbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteClientbound( packet, oldId, newId );
+ super.rewriteClientbound0( packet, oldId, newId );
+ // Waterfall end
// Special cases
int readerIndex = packet.readerIndex();
@@ -151,10 +153,13 @@ class EntityMap_1_9_4 extends EntityMap
packet.readerIndex( readerIndex );
}
+ // Waterfall start - rewriteServerbound -> rewriteServerbound0
@Override
- public void rewriteServerbound(ByteBuf packet, int oldId, int newId)
+ public void rewriteServerbound0(ByteBuf packet, int oldId, int newId)
{
- super.rewriteServerbound( packet, oldId, newId );
+ super.rewriteServerbound0( packet, oldId, newId );
+ // Waterfall end
+
// Special cases
int readerIndex = packet.readerIndex();
int packetId = DefinedPacket.readVarInt( packet );
--
2.12.2