Log packets in dedicated handlers instead of in encoder/decoder
This commit is contained in:
@@ -30,6 +30,8 @@ import io.netty.handler.timeout.IdleStateEvent;
|
||||
import net.MaplePacketHandler;
|
||||
import net.PacketProcessor;
|
||||
import net.netty.InvalidPacketHeaderException;
|
||||
import net.packet.logging.LoggingUtil;
|
||||
import net.packet.logging.PacketLogger;
|
||||
import net.packet.ByteBufOutPacket;
|
||||
import net.packet.InPacket;
|
||||
import net.packet.OutPacket;
|
||||
@@ -80,7 +82,6 @@ import java.util.concurrent.locks.Lock;
|
||||
|
||||
public class MapleClient extends ChannelInboundHandlerAdapter {
|
||||
private static final Logger log = LoggerFactory.getLogger(MapleClient.class);
|
||||
private static final Set<Short> ignoredDebugRecvPackets = Set.of((short) 167, (short) 197, (short) 89, (short) 91, (short) 41, (short) 188, (short) 107);
|
||||
|
||||
public static final int LOGIN_NOTLOGGEDIN = 0;
|
||||
public static final int LOGIN_SERVER_TRANSITION = 1;
|
||||
@@ -181,7 +182,7 @@ public class MapleClient extends ChannelInboundHandlerAdapter {
|
||||
short opcode = packet.readShort();
|
||||
final MaplePacketHandler handler = packetProcessor.getHandler(opcode);
|
||||
|
||||
if (YamlConfig.config.server.USE_DEBUG_SHOW_RCVD_PACKET && !ignoredDebugRecvPackets.contains(opcode)) {
|
||||
if (YamlConfig.config.server.USE_DEBUG_SHOW_RCVD_PACKET && !LoggingUtil.isIgnoredRecvPacket(opcode)) {
|
||||
log.debug("Received packet id {}", opcode);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,16 @@
|
||||
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.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) {
|
||||
@@ -38,10 +31,6 @@ public class PacketDecoder extends ReplayingDecoder<Void> {
|
||||
receiveCypher.crypt(packet);
|
||||
MapleCustomEncryption.decryptData(packet);
|
||||
out.add(new ByteBufInPacket(Unpooled.wrappedBuffer(packet)));
|
||||
|
||||
if (LOG_PACKETS){
|
||||
logPacket(packet);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,27 +46,4 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,13 @@
|
||||
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.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) {
|
||||
@@ -28,10 +19,6 @@ public class PacketEncoder extends MessageToByteEncoder<OutPacket> {
|
||||
byte[] packet = in.getBytes();
|
||||
out.writeBytes(getEncodedHeader(packet.length));
|
||||
|
||||
if (LOG_PACKETS) {
|
||||
logPacket(packet);
|
||||
}
|
||||
|
||||
MapleCustomEncryption.encryptData(packet);
|
||||
sendCypher.crypt(packet);
|
||||
out.writeBytes(packet);
|
||||
@@ -40,27 +27,4 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
package net.netty;
|
||||
|
||||
import client.MapleClient;
|
||||
import config.YamlConfig;
|
||||
import constants.net.ServerConstants;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import net.packet.logging.InPacketLogger;
|
||||
import net.packet.logging.OutPacketLogger;
|
||||
import tools.MaplePacketCreator;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
public abstract class ServerChannelInitializer extends ChannelInitializer<SocketChannel> {
|
||||
private static final int IDLE_TIME_SECONDS = 30;
|
||||
private static final boolean LOG_PACKETS = YamlConfig.config.server.USE_DEBUG_SHOW_PACKET;
|
||||
private static final ChannelHandler sendPacketLogger = new OutPacketLogger();
|
||||
private static final ChannelHandler receivePacketLogger = new InPacketLogger();
|
||||
|
||||
static final AtomicLong sessionId = new AtomicLong(7777);
|
||||
|
||||
void initPipeline(SocketChannel socketChannel, MapleClient client) {
|
||||
@@ -31,5 +39,10 @@ public abstract class ServerChannelInitializer extends ChannelInitializer<Socket
|
||||
pipeline.addLast("IdleStateHandler", new IdleStateHandler(0, 0, IDLE_TIME_SECONDS));
|
||||
pipeline.addLast("PacketCodec", new PacketCodec(ClientCyphers.of(sendIv, recvIv)));
|
||||
pipeline.addLast("MapleClient", client);
|
||||
|
||||
if (LOG_PACKETS) {
|
||||
pipeline.addBefore("MapleClient", "SendPacketLogger", sendPacketLogger);
|
||||
pipeline.addBefore("MapleClient", "ReceivePacketLogger", receivePacketLogger);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
47
src/main/java/net/packet/logging/InPacketLogger.java
Normal file
47
src/main/java/net/packet/logging/InPacketLogger.java
Normal file
@@ -0,0 +1,47 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import constants.net.OpcodeConstants;
|
||||
import io.netty.channel.ChannelHandler.Sharable;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import net.packet.InPacket;
|
||||
import net.packet.Packet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tools.HexTool;
|
||||
|
||||
@Sharable
|
||||
public class InPacketLogger extends ChannelInboundHandlerAdapter implements PacketLogger {
|
||||
private static final Logger log = LoggerFactory.getLogger(InPacketLogger.class);
|
||||
private static final int LOG_CONTENT_THRESHOLD = 3_000;
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) {
|
||||
if (msg instanceof InPacket packet) {
|
||||
log(packet);
|
||||
}
|
||||
|
||||
ctx.fireChannelRead(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Packet packet) {
|
||||
final byte[] content = packet.getBytes();
|
||||
final int packetLength = content.length;
|
||||
|
||||
if (packetLength <= LOG_CONTENT_THRESHOLD) {
|
||||
final short opcode = LoggingUtil.readFirstShort(content);
|
||||
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(content), HexTool.toStringFromAscii(content));
|
||||
} else {
|
||||
log.debug(HexTool.toString(new byte[]{content[0], content[1]}) + "...");
|
||||
}
|
||||
}
|
||||
|
||||
private String getRecvOpcodeName(short opcode) {
|
||||
return OpcodeConstants.recvOpcodeNames.get((int) opcode);
|
||||
}
|
||||
}
|
||||
17
src/main/java/net/packet/logging/LoggingUtil.java
Normal file
17
src/main/java/net/packet/logging/LoggingUtil.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class LoggingUtil {
|
||||
private static final Set<Short> ignoredDebugRecvPackets = Set.of((short) 167, (short) 197, (short) 89, (short) 91, (short) 41, (short) 188, (short) 107);
|
||||
|
||||
public static short readFirstShort(byte[] bytes) {
|
||||
return Unpooled.wrappedBuffer(bytes).readShortLE();
|
||||
}
|
||||
|
||||
public static boolean isIgnoredRecvPacket(short opcode) {
|
||||
return ignoredDebugRecvPackets.contains(opcode);
|
||||
}
|
||||
}
|
||||
48
src/main/java/net/packet/logging/OutPacketLogger.java
Normal file
48
src/main/java/net/packet/logging/OutPacketLogger.java
Normal file
@@ -0,0 +1,48 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import constants.net.OpcodeConstants;
|
||||
import io.netty.channel.ChannelHandler.Sharable;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelOutboundHandlerAdapter;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import net.packet.OutPacket;
|
||||
import net.packet.Packet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tools.HexTool;
|
||||
|
||||
@Sharable
|
||||
public class OutPacketLogger extends ChannelOutboundHandlerAdapter implements PacketLogger {
|
||||
private static final Logger log = LoggerFactory.getLogger(OutPacketLogger.class);
|
||||
private static final int LOG_CONTENT_THRESHOLD = 50_000;
|
||||
|
||||
@Override
|
||||
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
|
||||
if (msg instanceof OutPacket packet) {
|
||||
log(packet);
|
||||
}
|
||||
|
||||
ctx.write(msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Packet packet) {
|
||||
final byte[] content = packet.getBytes();
|
||||
final int packetLength = content.length;
|
||||
|
||||
if (packetLength <= LOG_CONTENT_THRESHOLD) {
|
||||
final short opcode = LoggingUtil.readFirstShort(content);
|
||||
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(content), HexTool.toStringFromAscii(content));
|
||||
} else {
|
||||
log.info(HexTool.toString(new byte[]{content[0], content[1]}) + " ...");
|
||||
}
|
||||
}
|
||||
|
||||
private String getSendOpcodeName(short opcode) {
|
||||
return OpcodeConstants.sendOpcodeNames.get((int) opcode);
|
||||
}
|
||||
}
|
||||
7
src/main/java/net/packet/logging/PacketLogger.java
Normal file
7
src/main/java/net/packet/logging/PacketLogger.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package net.packet.logging;
|
||||
|
||||
import net.packet.Packet;
|
||||
|
||||
public interface PacketLogger {
|
||||
void log(Packet packet);
|
||||
}
|
||||
@@ -5,8 +5,11 @@
|
||||
</Properties>
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
|
||||
<PatternLayout>
|
||||
<Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Pattern>
|
||||
</PatternLayout>
|
||||
</Console>
|
||||
|
||||
<RollingFile name="File"
|
||||
fileName="logs/${filename}.log"
|
||||
filePattern="logs/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/${filename}-%d{yyyy-MM-dd_HH-mm-ss}-%i.log">
|
||||
|
||||
Reference in New Issue
Block a user