From b30628f6dbc68936696182a41fa32b71e9113033 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Wed, 8 Mar 2017 09:08:29 -0500 Subject: [PATCH 1/3] Make hex dump more readable, match more packet names --- .../com/comphenix/protocol/PacketLogging.java | 41 ++++++++++++++++--- 1 file changed, 35 insertions(+), 6 deletions(-) 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 db117126..765a659b 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java @@ -16,6 +16,7 @@ */ package com.comphenix.protocol; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.text.MessageFormat; @@ -29,6 +30,7 @@ import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import org.apache.commons.io.HexDump; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -41,9 +43,7 @@ import com.comphenix.protocol.events.ListeningWhitelist; import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.injector.netty.WirePacket; - -import io.netty.buffer.ByteBuf; -import io.netty.buffer.ByteBufUtil; +import com.google.common.base.Charsets; /** * Logs packets to a given stream @@ -97,7 +97,22 @@ public class PacketLogging implements CommandExecutor, PacketListener { int id = Integer.parseInt(args[2]); type = PacketType.findCurrent(protocol, pSender, id); } catch (NumberFormatException ex) { - type = PacketType.findCurrent(protocol, pSender, args[2]); + String name = args[2]; + outer: for (PacketType packet : PacketType.values()) { + if (packet.getProtocol() == protocol && + packet.getSender() == pSender) { + if (packet.name().equalsIgnoreCase(name)) { + type = packet; + break outer; + } + for (String className : packet.getClassNames()) { + if (className.equalsIgnoreCase(name)) { + type = packet; + break outer; + } + } + } + } } } catch (IllegalArgumentException ex) { sender.sendMessage(ChatColor.RED + "Unknown packet: " + PacketType.format(protocol, pSender, args[2])); @@ -176,9 +191,23 @@ public class PacketLogging implements CommandExecutor, PacketListener { log(event); } + private static String hexDump(byte[] bytes) throws IOException { + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { + HexDump.dump(bytes, 0, output, 0); + return new String(output.toByteArray(), Charsets.UTF_8); + } + } + private void log(PacketEvent event) { - ByteBuf buffer = WirePacket.bufferFromPacket(event.getPacket()); - String hexDump = ByteBufUtil.hexDump(buffer); + String hexDump; + + try { + WirePacket packet = WirePacket.fromPacket(event.getPacket()); + hexDump = hexDump(packet.getBytes()); + } catch (Throwable ex) { + fileLogger.log(Level.WARNING, "Failed to dump packet: " + ex.toString()); + return; + } if (location == LogLocation.FILE) { fileLogger.log(Level.INFO, event.getPacketType() + ":"); From 73ce01bbde0d3778348aec269720824f7f6f73d9 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Thu, 9 Mar 2017 10:53:06 -0500 Subject: [PATCH 2/3] Make packet logging more robust --- .../com/comphenix/protocol/PacketLogging.java | 162 ++++++++++-------- 1 file changed, 86 insertions(+), 76 deletions(-) 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 765a659b..fcb1f539 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/PacketLogging.java @@ -73,81 +73,89 @@ public class PacketLogging implements CommandExecutor, PacketListener { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { PacketType type = null; - if (args.length > 2) { - Protocol protocol; + try { + if (args.length > 2) { + Protocol protocol; - try { - protocol = Protocol.valueOf(args[0].toUpperCase()); - } catch (IllegalArgumentException ex) { - sender.sendMessage(ChatColor.RED + "Unknown protocol " + args[0]); - return true; - } - - Sender pSender; - - try { - pSender = Sender.valueOf(args[1].toUpperCase()); - } catch (IllegalArgumentException ex) { - sender.sendMessage(ChatColor.RED + "Unknown sender: " + args[1]); - return true; - } - - try { try { - int id = Integer.parseInt(args[2]); - type = PacketType.findCurrent(protocol, pSender, id); - } catch (NumberFormatException ex) { - String name = args[2]; - outer: for (PacketType packet : PacketType.values()) { - if (packet.getProtocol() == protocol && - packet.getSender() == pSender) { - if (packet.name().equalsIgnoreCase(name)) { - type = packet; - break outer; - } - for (String className : packet.getClassNames()) { - if (className.equalsIgnoreCase(name)) { + protocol = Protocol.valueOf(args[0].toUpperCase()); + } catch (IllegalArgumentException ex) { + sender.sendMessage(ChatColor.RED + "Unknown protocol " + args[0]); + return true; + } + + Sender pSender; + + try { + pSender = Sender.valueOf(args[1].toUpperCase()); + } catch (IllegalArgumentException ex) { + sender.sendMessage(ChatColor.RED + "Unknown sender: " + args[1]); + return true; + } + + try { + try { + int id = Integer.parseInt(args[2]); + type = PacketType.findCurrent(protocol, pSender, id); + } catch (NumberFormatException ex) { + String name = args[2]; + outer: for (PacketType packet : PacketType.values()) { + if (packet.getProtocol() == protocol && packet.getSender() == pSender) { + if (packet.name().equalsIgnoreCase(name)) { type = packet; break outer; } + for (String className : packet.getClassNames()) { + if (className.equalsIgnoreCase(name)) { + type = packet; + break outer; + } + } } } } + } catch (IllegalArgumentException ex) { + type = null; } - } catch (IllegalArgumentException ex) { - sender.sendMessage(ChatColor.RED + "Unknown packet: " + PacketType.format(protocol, pSender, args[2])); + + if (type == null) { + sender.sendMessage(ChatColor.RED + "Unknown packet: " + args[2]); + return true; + } + + if (args.length > 3) { + if (args[3].equalsIgnoreCase("console")) { + this.location = LogLocation.CONSOLE; + } else { + this.location = LogLocation.FILE; + } + } + + if (pSender == Sender.CLIENT) { + if (receivingTypes.contains(type)) { + receivingTypes.remove(type); + } else { + receivingTypes.add(type); + } + } else { + if (sendingTypes.contains(type)) { + sendingTypes.remove(type); + } else { + sendingTypes.add(type); + } + } + + startLogging(); + sender.sendMessage(ChatColor.GREEN + "Now logging " + type.getPacketClass().getSimpleName()); return true; } - if (args.length > 3) { - if (args[3].equalsIgnoreCase("console")) { - this.location = LogLocation.CONSOLE; - } else { - this.location = LogLocation.FILE; - } - } - - if (pSender == Sender.CLIENT) { - if (receivingTypes.contains(type)) { - receivingTypes.remove(type); - } else { - receivingTypes.add(type); - } - } else { - if (sendingTypes.contains(type)) { - sendingTypes.remove(type); - } else { - sendingTypes.add(type); - } - } - - startLogging(); - sender.sendMessage(ChatColor.GREEN + "Now logging " + type.getPacketClass().getSimpleName()); + sender.sendMessage(ChatColor.RED + "Invalid syntax: /packetlog [location]"); + return true; + } catch (Throwable ex) { + sender.sendMessage(ChatColor.RED + "Failed to parse command: " + ex.toString()); return true; } - - sender.sendMessage(ChatColor.RED + "Invalid syntax: /packetlog [location]"); - return true; } private void startLogging() { @@ -199,24 +207,26 @@ public class PacketLogging implements CommandExecutor, PacketListener { } private void log(PacketEvent event) { - String hexDump; - try { WirePacket packet = WirePacket.fromPacket(event.getPacket()); - hexDump = hexDump(packet.getBytes()); - } catch (Throwable ex) { - fileLogger.log(Level.WARNING, "Failed to dump packet: " + ex.toString()); - return; - } + String hexDump = hexDump(packet.getBytes()); - if (location == LogLocation.FILE) { - fileLogger.log(Level.INFO, event.getPacketType() + ":"); - fileLogger.log(Level.INFO, hexDump); - fileLogger.log(Level.INFO, ""); - } else { - System.out.println(event.getPacketType() + ":"); - System.out.println(hexDump); - System.out.println(); + if (location == LogLocation.FILE) { + fileLogger.log(Level.INFO, event.getPacketType() + ":"); + fileLogger.log(Level.INFO, hexDump); + fileLogger.log(Level.INFO, ""); + } else { + System.out.println(event.getPacketType() + ":"); + System.out.println(hexDump); + System.out.println(); + } + } catch (Throwable ex) { + plugin.getLogger().log(Level.WARNING, "Failed to log packet " + event.getPacketType() + ":", ex); + plugin.getLogger().log(Level.WARNING, "Clearing packet logger..."); + + sendingTypes.clear(); + receivingTypes.clear(); + startLogging(); } } From aaf1af8e414dbf743ad155883e8439d8a8530c77 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Sat, 18 Mar 2017 19:29:16 -0400 Subject: [PATCH 3/3] Don't worry if we can't determine the Java version --- .../main/java/com/comphenix/protocol/utility/Util.java | 10 +++++++--- .../main/java/com/comphenix/protocol/ProtocolLib.java | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/modules/API/src/main/java/com/comphenix/protocol/utility/Util.java b/modules/API/src/main/java/com/comphenix/protocol/utility/Util.java index ed36fce1..50b2e5e8 100644 --- a/modules/API/src/main/java/com/comphenix/protocol/utility/Util.java +++ b/modules/API/src/main/java/com/comphenix/protocol/utility/Util.java @@ -63,10 +63,14 @@ public class Util { /** * Gets the currently running major Java version. - * @return The version + * @return The version or -1 if it could not be found */ public static int getJavaVersion() { - String version = Runtime.class.getPackage().getSpecificationVersion(); - return (int) (Double.valueOf(version) * 10 % 10); + try { + String version = Runtime.class.getPackage().getSpecificationVersion(); + return (int) (Double.parseDouble(version) * 10 % 10); + } catch (Throwable ex) { + return -1; + } } } diff --git a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java index b40ced09..4c2772d1 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java @@ -156,7 +156,7 @@ public class ProtocolLib extends JavaPlugin { ProtocolLogger.init(this); int java = Util.getJavaVersion(); - if (java < 8 && !getConfig().getBoolean("ignoreJava", false)) { + if (java != -1 && java < 8 && !getConfig().getBoolean("ignoreJava", false)) { logger.warning("Detected outdated Java version: Java " + java); logger.warning("It is recommended that you update to Java 8 as soon as possible."); logger.warning("Future versions of ProtocolLib many not support Java " + java + ".");