From 73ce01bbde0d3778348aec269720824f7f6f73d9 Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Thu, 9 Mar 2017 10:53:06 -0500 Subject: [PATCH] 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(); } }