|
16 | 16 | */
|
17 | 17 | package com.comphenix.protocol;
|
18 | 18 |
|
| 19 | +import java.io.ByteArrayOutputStream; |
19 | 20 | import java.io.File;
|
20 | 21 | import java.io.IOException;
|
21 | 22 | import java.text.MessageFormat;
|
|
29 | 30 | import java.util.logging.LogRecord;
|
30 | 31 | import java.util.logging.Logger;
|
31 | 32 |
|
| 33 | +import org.apache.commons.io.HexDump; |
32 | 34 | import org.bukkit.ChatColor;
|
33 | 35 | import org.bukkit.command.Command;
|
34 | 36 | import org.bukkit.command.CommandExecutor;
|
|
41 | 43 | import com.comphenix.protocol.events.PacketEvent;
|
42 | 44 | import com.comphenix.protocol.events.PacketListener;
|
43 | 45 | import com.comphenix.protocol.injector.netty.WirePacket;
|
44 |
| - |
45 |
| -import io.netty.buffer.ByteBuf; |
46 |
| -import io.netty.buffer.ByteBufUtil; |
| 46 | +import com.google.common.base.Charsets; |
47 | 47 |
|
48 | 48 | /**
|
49 | 49 | * Logs packets to a given stream
|
@@ -73,66 +73,89 @@ public class PacketLogging implements CommandExecutor, PacketListener {
|
73 | 73 | public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
74 | 74 | PacketType type = null;
|
75 | 75 |
|
76 |
| - if (args.length > 2) { |
77 |
| - Protocol protocol; |
| 76 | + try { |
| 77 | + if (args.length > 2) { |
| 78 | + Protocol protocol; |
78 | 79 |
|
79 |
| - try { |
80 |
| - protocol = Protocol.valueOf(args[0].toUpperCase()); |
81 |
| - } catch (IllegalArgumentException ex) { |
82 |
| - sender.sendMessage(ChatColor.RED + "Unknown protocol " + args[0]); |
83 |
| - return true; |
84 |
| - } |
| 80 | + try { |
| 81 | + protocol = Protocol.valueOf(args[0].toUpperCase()); |
| 82 | + } catch (IllegalArgumentException ex) { |
| 83 | + sender.sendMessage(ChatColor.RED + "Unknown protocol " + args[0]); |
| 84 | + return true; |
| 85 | + } |
85 | 86 |
|
86 |
| - Sender pSender; |
| 87 | + Sender pSender; |
87 | 88 |
|
88 |
| - try { |
89 |
| - pSender = Sender.valueOf(args[1].toUpperCase()); |
90 |
| - } catch (IllegalArgumentException ex) { |
91 |
| - sender.sendMessage(ChatColor.RED + "Unknown sender: " + args[1]); |
92 |
| - return true; |
93 |
| - } |
| 89 | + try { |
| 90 | + pSender = Sender.valueOf(args[1].toUpperCase()); |
| 91 | + } catch (IllegalArgumentException ex) { |
| 92 | + sender.sendMessage(ChatColor.RED + "Unknown sender: " + args[1]); |
| 93 | + return true; |
| 94 | + } |
94 | 95 |
|
95 |
| - try { |
96 | 96 | try {
|
97 |
| - int id = Integer.parseInt(args[2]); |
98 |
| - type = PacketType.findCurrent(protocol, pSender, id); |
99 |
| - } catch (NumberFormatException ex) { |
100 |
| - type = PacketType.findCurrent(protocol, pSender, args[2]); |
| 97 | + try { |
| 98 | + int id = Integer.parseInt(args[2]); |
| 99 | + type = PacketType.findCurrent(protocol, pSender, id); |
| 100 | + } catch (NumberFormatException ex) { |
| 101 | + String name = args[2]; |
| 102 | + outer: for (PacketType packet : PacketType.values()) { |
| 103 | + if (packet.getProtocol() == protocol && packet.getSender() == pSender) { |
| 104 | + if (packet.name().equalsIgnoreCase(name)) { |
| 105 | + type = packet; |
| 106 | + break outer; |
| 107 | + } |
| 108 | + for (String className : packet.getClassNames()) { |
| 109 | + if (className.equalsIgnoreCase(name)) { |
| 110 | + type = packet; |
| 111 | + break outer; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } catch (IllegalArgumentException ex) { |
| 118 | + type = null; |
101 | 119 | }
|
102 |
| - } catch (IllegalArgumentException ex) { |
103 |
| - sender.sendMessage(ChatColor.RED + "Unknown packet: " + PacketType.format(protocol, pSender, args[2])); |
104 |
| - return true; |
105 |
| - } |
106 | 120 |
|
107 |
| - if (args.length > 3) { |
108 |
| - if (args[3].equalsIgnoreCase("console")) { |
109 |
| - this.location = LogLocation.CONSOLE; |
110 |
| - } else { |
111 |
| - this.location = LogLocation.FILE; |
| 121 | + if (type == null) { |
| 122 | + sender.sendMessage(ChatColor.RED + "Unknown packet: " + args[2]); |
| 123 | + return true; |
112 | 124 | }
|
113 |
| - } |
114 | 125 |
|
115 |
| - if (pSender == Sender.CLIENT) { |
116 |
| - if (receivingTypes.contains(type)) { |
117 |
| - receivingTypes.remove(type); |
118 |
| - } else { |
119 |
| - receivingTypes.add(type); |
| 126 | + if (args.length > 3) { |
| 127 | + if (args[3].equalsIgnoreCase("console")) { |
| 128 | + this.location = LogLocation.CONSOLE; |
| 129 | + } else { |
| 130 | + this.location = LogLocation.FILE; |
| 131 | + } |
120 | 132 | }
|
121 |
| - } else { |
122 |
| - if (sendingTypes.contains(type)) { |
123 |
| - sendingTypes.remove(type); |
| 133 | + |
| 134 | + if (pSender == Sender.CLIENT) { |
| 135 | + if (receivingTypes.contains(type)) { |
| 136 | + receivingTypes.remove(type); |
| 137 | + } else { |
| 138 | + receivingTypes.add(type); |
| 139 | + } |
124 | 140 | } else {
|
125 |
| - sendingTypes.add(type); |
| 141 | + if (sendingTypes.contains(type)) { |
| 142 | + sendingTypes.remove(type); |
| 143 | + } else { |
| 144 | + sendingTypes.add(type); |
| 145 | + } |
126 | 146 | }
|
| 147 | + |
| 148 | + startLogging(); |
| 149 | + sender.sendMessage(ChatColor.GREEN + "Now logging " + type.getPacketClass().getSimpleName()); |
| 150 | + return true; |
127 | 151 | }
|
128 | 152 |
|
129 |
| - startLogging(); |
130 |
| - sender.sendMessage(ChatColor.GREEN + "Now logging " + type.getPacketClass().getSimpleName()); |
| 153 | + sender.sendMessage(ChatColor.RED + "Invalid syntax: /packetlog <protocol> <sender> <packet> [location]"); |
| 154 | + return true; |
| 155 | + } catch (Throwable ex) { |
| 156 | + sender.sendMessage(ChatColor.RED + "Failed to parse command: " + ex.toString()); |
131 | 157 | return true;
|
132 | 158 | }
|
133 |
| - |
134 |
| - sender.sendMessage(ChatColor.RED + "Invalid syntax: /packetlog <protocol> <sender> <packet> [location]"); |
135 |
| - return true; |
136 | 159 | }
|
137 | 160 |
|
138 | 161 | private void startLogging() {
|
@@ -176,18 +199,34 @@ public void onPacketReceiving(PacketEvent event) {
|
176 | 199 | log(event);
|
177 | 200 | }
|
178 | 201 |
|
| 202 | + private static String hexDump(byte[] bytes) throws IOException { |
| 203 | + try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { |
| 204 | + HexDump.dump(bytes, 0, output, 0); |
| 205 | + return new String(output.toByteArray(), Charsets.UTF_8); |
| 206 | + } |
| 207 | + } |
| 208 | + |
179 | 209 | private void log(PacketEvent event) {
|
180 |
| - ByteBuf buffer = WirePacket.bufferFromPacket(event.getPacket()); |
181 |
| - String hexDump = ByteBufUtil.hexDump(buffer); |
182 |
| - |
183 |
| - if (location == LogLocation.FILE) { |
184 |
| - fileLogger.log(Level.INFO, event.getPacketType() + ":"); |
185 |
| - fileLogger.log(Level.INFO, hexDump); |
186 |
| - fileLogger.log(Level.INFO, ""); |
187 |
| - } else { |
188 |
| - System.out.println(event.getPacketType() + ":"); |
189 |
| - System.out.println(hexDump); |
190 |
| - System.out.println(); |
| 210 | + try { |
| 211 | + WirePacket packet = WirePacket.fromPacket(event.getPacket()); |
| 212 | + String hexDump = hexDump(packet.getBytes()); |
| 213 | + |
| 214 | + if (location == LogLocation.FILE) { |
| 215 | + fileLogger.log(Level.INFO, event.getPacketType() + ":"); |
| 216 | + fileLogger.log(Level.INFO, hexDump); |
| 217 | + fileLogger.log(Level.INFO, ""); |
| 218 | + } else { |
| 219 | + System.out.println(event.getPacketType() + ":"); |
| 220 | + System.out.println(hexDump); |
| 221 | + System.out.println(); |
| 222 | + } |
| 223 | + } catch (Throwable ex) { |
| 224 | + plugin.getLogger().log(Level.WARNING, "Failed to log packet " + event.getPacketType() + ":", ex); |
| 225 | + plugin.getLogger().log(Level.WARNING, "Clearing packet logger..."); |
| 226 | + |
| 227 | + sendingTypes.clear(); |
| 228 | + receivingTypes.clear(); |
| 229 | + startLogging(); |
191 | 230 | }
|
192 | 231 | }
|
193 | 232 |
|
|
0 commit comments