From 5ec87c9d7650ae21faca9b7b3cc7ac1629870d24 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Mon, 20 Mar 2017 21:42:46 -0400 Subject: [PATCH] Fix issues with logging custom payload packets --- .../protocol/injector/netty/WirePacket.java | 35 +++++++++++++++---- .../com/comphenix/protocol/PacketLogging.java | 13 ++++--- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/injector/netty/WirePacket.java b/modules/API/src/main/java/com/comphenix/protocol/injector/netty/WirePacket.java index 1a5d8e48..83254c20 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/injector/netty/WirePacket.java +++ b/modules/API/src/main/java/com/comphenix/protocol/injector/netty/WirePacket.java @@ -150,29 +150,52 @@ public class WirePacket { */ public static WirePacket fromPacket(PacketContainer packet) { int id = packet.getType().getCurrentId(); - return new WirePacket(id, getBytes(bufferFromPacket(packet))); + return new WirePacket(id, bytesFromPacket(packet)); } /** - * Creates a ByteBuf from an existing PacketContainer containing all the + * Creates a byte array from an existing PacketContainer containing all the * bytes from that packet * * @param packet Existing packet * @return The ByteBuf */ - public static ByteBuf bufferFromPacket(PacketContainer packet) { + public static byte[] bytesFromPacket(PacketContainer packet) { checkNotNull(packet, "packet cannot be null!"); - + ByteBuf buffer = PacketContainer.createPacketBuffer(); + ByteBuf store = PacketContainer.createPacketBuffer(); + + // Read the bytes once Method write = MinecraftMethods.getPacketWriteByteBufMethod(); try { write.invoke(packet.getHandle(), buffer); } catch (ReflectiveOperationException ex) { - throw new RuntimeException("Failed to serialize packet contents.", ex); + throw new RuntimeException("Failed to read packet contents.", ex); } - return buffer; + byte[] bytes = getBytes(buffer); + + // Rewrite them to the packet to avoid issues with certain packets + if (packet.getType() == PacketType.Play.Server.CUSTOM_PAYLOAD + || packet.getType() == PacketType.Play.Client.CUSTOM_PAYLOAD) { + // Make a copy of the array before writing + byte[] ret = Arrays.copyOf(bytes, bytes.length); + store.writeBytes(bytes); + + Method read = MinecraftMethods.getPacketReadByteBufMethod(); + + try { + read.invoke(packet.getHandle(), store); + } catch (ReflectiveOperationException ex) { + throw new RuntimeException("Failed to rewrite packet contents.", ex); + } + + return ret; + } + + return bytes; } /** diff --git a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java index fcb1f539..bf471777 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java @@ -94,10 +94,10 @@ public class PacketLogging implements CommandExecutor, PacketListener { } try { - try { + try { // Try IDs first int id = Integer.parseInt(args[2]); type = PacketType.findCurrent(protocol, pSender, id); - } catch (NumberFormatException ex) { + } catch (NumberFormatException ex) { // Check packet names String name = args[2]; outer: for (PacketType packet : PacketType.values()) { if (packet.getProtocol() == protocol && packet.getSender() == pSender) { @@ -114,7 +114,7 @@ public class PacketLogging implements CommandExecutor, PacketListener { } } } - } catch (IllegalArgumentException ex) { + } catch (IllegalArgumentException ex) { // RIP type = null; } @@ -168,6 +168,7 @@ public class PacketLogging implements CommandExecutor, PacketListener { this.sendingWhitelist = ListeningWhitelist.newBuilder().types(sendingTypes).build(); this.receivingWhitelist = ListeningWhitelist.newBuilder().types(receivingTypes).build(); + // Setup the file logger if it hasn't been already if (location == LogLocation.FILE && fileLogger == null) { fileLogger = Logger.getLogger("ProtocolLib-FileLogging"); @@ -199,6 +200,8 @@ public class PacketLogging implements CommandExecutor, PacketListener { log(event); } + // Here's where the magic happens + private static String hexDump(byte[] bytes) throws IOException { try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { HexDump.dump(bytes, 0, output, 0); @@ -208,8 +211,8 @@ public class PacketLogging implements CommandExecutor, PacketListener { private void log(PacketEvent event) { try { - WirePacket packet = WirePacket.fromPacket(event.getPacket()); - String hexDump = hexDump(packet.getBytes()); + byte[] bytes = WirePacket.bytesFromPacket(event.getPacket()); + String hexDump = hexDump(bytes); if (location == LogLocation.FILE) { fileLogger.log(Level.INFO, event.getPacketType() + ":");