Make hex dump more readable, match more packet names

This commit is contained in:
Dan Mulloy 2017-03-08 09:08:29 -05:00
parent 9289825d87
commit b30628f6db

View File

@ -16,6 +16,7 @@
*/ */
package com.comphenix.protocol; package com.comphenix.protocol;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
@ -29,6 +30,7 @@ import java.util.logging.Level;
import java.util.logging.LogRecord; import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.apache.commons.io.HexDump;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor; 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.PacketEvent;
import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.injector.netty.WirePacket; import com.comphenix.protocol.injector.netty.WirePacket;
import com.google.common.base.Charsets;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
/** /**
* Logs packets to a given stream * Logs packets to a given stream
@ -97,7 +97,22 @@ public class PacketLogging implements CommandExecutor, PacketListener {
int id = Integer.parseInt(args[2]); int id = Integer.parseInt(args[2]);
type = PacketType.findCurrent(protocol, pSender, id); type = PacketType.findCurrent(protocol, pSender, id);
} catch (NumberFormatException ex) { } 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) { } catch (IllegalArgumentException ex) {
sender.sendMessage(ChatColor.RED + "Unknown packet: " + PacketType.format(protocol, pSender, args[2])); sender.sendMessage(ChatColor.RED + "Unknown packet: " + PacketType.format(protocol, pSender, args[2]));
@ -176,9 +191,23 @@ public class PacketLogging implements CommandExecutor, PacketListener {
log(event); 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) { private void log(PacketEvent event) {
ByteBuf buffer = WirePacket.bufferFromPacket(event.getPacket()); String hexDump;
String hexDump = ByteBufUtil.hexDump(buffer);
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) { if (location == LogLocation.FILE) {
fileLogger.log(Level.INFO, event.getPacketType() + ":"); fileLogger.log(Level.INFO, event.getPacketType() + ":");