Able to log incoming and outgoing packets

This commit is contained in:
P0nk
2021-07-13 21:50:19 +02:00
parent 3de827553e
commit fc694f1b0f
2 changed files with 72 additions and 2 deletions

View File

@@ -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<Void> {
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<Void> {
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<Void> {
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 ? "<UnknownPacket> " : "";
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);
}
}

View File

@@ -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<OutPacket> {
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<OutPacket> {
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<OutPacket> {
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 ? "<UnknownPacket> " : "";
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();
}
}