From fc694f1b0f9c998969b0e53989e12114c1c7e67e Mon Sep 17 00:00:00 2001 From: P0nk Date: Tue, 13 Jul 2021 21:50:19 +0200 Subject: [PATCH] Able to log incoming and outgoing packets --- src/main/java/net/netty/PacketDecoder.java | 37 ++++++++++++++++++++-- src/main/java/net/netty/PacketEncoder.java | 37 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/netty/PacketDecoder.java b/src/main/java/net/netty/PacketDecoder.java index 79cc0fafb5..0c150f2527 100644 --- a/src/main/java/net/netty/PacketDecoder.java +++ b/src/main/java/net/netty/PacketDecoder.java @@ -1,16 +1,23 @@ package net.netty; +import config.YamlConfig; +import constants.net.OpcodeConstants; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ReplayingDecoder; import net.mina.MapleCustomEncryption; import net.packet.ByteBufInPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tools.HexTool; import tools.MapleAESOFB; import java.util.List; public class PacketDecoder extends ReplayingDecoder { + private static final Logger log = LoggerFactory.getLogger(PacketDecoder.class); + private static final boolean LOG_PACKETS = YamlConfig.config.server.USE_DEBUG_SHOW_PACKET; private final MapleAESOFB receiveCypher; public PacketDecoder(MapleAESOFB receiveCypher) { @@ -25,13 +32,16 @@ public class PacketDecoder extends ReplayingDecoder { throw new InvalidPacketHeaderException("Attempted to decode a packet with an invalid header", header); } - int packetLength = decodePacketLength(header); + final int packetLength = decodePacketLength(header); byte[] packet = new byte[packetLength]; in.readBytes(packet); receiveCypher.crypt(packet); MapleCustomEncryption.decryptData(packet); out.add(new ByteBufInPacket(Unpooled.wrappedBuffer(packet))); - // TODO conditionally log the packet + + if (LOG_PACKETS){ + logPacket(packet); + } } /** @@ -47,4 +57,27 @@ public class PacketDecoder extends ReplayingDecoder { length = ((length << 8) & 0xFF00) | ((length >>> 8) & 0xFF); return length; } + + private void logPacket(byte[] packet) { + final int packetLength = packet.length; + + if (packetLength <= 3000) { + final int opcode = readFirstShort(packet); + final String opcodeHex = Integer.toHexString(opcode).toUpperCase(); + final String opcodeName = getRecvOpcodeName(opcode); + final String prefix = opcodeName == null ? " " : ""; + log.info("{}ClientSend:{} [{}] ({}) - hex:{} - text:{}", prefix, opcodeName, opcodeHex, packetLength, + HexTool.toString(packet), HexTool.toStringFromAscii(packet)); + } else { + log.debug(HexTool.toString(new byte[]{packet[0], packet[1]}) + "..."); + } + } + + private static int readFirstShort(byte[] bytes) { + return new ByteBufInPacket(Unpooled.wrappedBuffer(bytes)).readShort(); + } + + private String getRecvOpcodeName(int opcode) { + return OpcodeConstants.recvOpcodeNames.get(opcode); + } } diff --git a/src/main/java/net/netty/PacketEncoder.java b/src/main/java/net/netty/PacketEncoder.java index 3d424822c9..2e6de8e4d7 100644 --- a/src/main/java/net/netty/PacketEncoder.java +++ b/src/main/java/net/netty/PacketEncoder.java @@ -1,13 +1,22 @@ package net.netty; +import config.YamlConfig; +import constants.net.OpcodeConstants; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToByteEncoder; import net.mina.MapleCustomEncryption; +import net.packet.ByteBufInPacket; import net.packet.OutPacket; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tools.HexTool; import tools.MapleAESOFB; public class PacketEncoder extends MessageToByteEncoder { + private static final Logger log = LoggerFactory.getLogger(PacketEncoder.class); + private static final boolean LOG_PACKETS = YamlConfig.config.server.USE_DEBUG_SHOW_PACKET; private final MapleAESOFB sendCypher; public PacketEncoder(MapleAESOFB sendCypher) { @@ -18,6 +27,11 @@ public class PacketEncoder extends MessageToByteEncoder { protected void encode(ChannelHandlerContext ctx, OutPacket in, ByteBuf out) { byte[] packet = in.getBytes(); out.writeBytes(getEncodedHeader(packet.length)); + + if (LOG_PACKETS) { + logPacket(packet); + } + MapleCustomEncryption.encryptData(packet); sendCypher.crypt(packet); out.writeBytes(packet); @@ -26,4 +40,27 @@ public class PacketEncoder extends MessageToByteEncoder { private byte[] getEncodedHeader(int length) { return sendCypher.getPacketHeader(length); } + + private void logPacket(byte[] packet) { + final int packetLength = packet.length; + + if (packetLength <= 50000) { + final int opcode = readFirstShort(packet); + String opcodeHex = Integer.toHexString(opcode).toUpperCase(); + String opcodeName = getSendOpcodeName(opcode); + String prefix = opcodeName == null ? " " : ""; + log.info("{}ServerSend:{} [{}] ({}) - hex:{} - text:{}", prefix, opcodeName, opcodeHex, packetLength, + HexTool.toString(packet), HexTool.toStringFromAscii(packet)); + } else { + log.info(HexTool.toString(new byte[]{packet[0], packet[1]}) + " ..."); + } + } + + private String getSendOpcodeName(int opcode) { + return OpcodeConstants.sendOpcodeNames.get(opcode); + } + + private static int readFirstShort(byte[] bytes) { + return new ByteBufInPacket(Unpooled.wrappedBuffer(bytes)).readShort(); + } }