feat(Core/Logging): Changed format of packet log file from .bin to .pkt (#5949)

This commit is contained in:
Kargatum
2021-06-17 21:51:45 +07:00
committed by GitHub
parent 9b5b229085
commit 2ba258a88b
3 changed files with 86 additions and 19 deletions

View File

@@ -4,14 +4,42 @@
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*/
#include "ByteBuffer.h"
#include "Config.h"
#include "PacketLog.h"
#include "Config.h"
#include "Timer.h"
#include "WorldPacket.h"
#pragma pack(push, 1)
// Packet logging structures in PKT 3.1 format
struct LogHeader
{
char Signature[3];
uint16 FormatVersion;
uint8 SnifferId;
uint32 Build;
char Locale[4];
uint8 SessionKey[40];
uint32 SniffStartUnixtime;
uint32 SniffStartTicks;
uint32 OptionalDataSize;
};
struct PacketHeader
{
char Direction[4];
uint32 ConnectionId;
uint32 ArrivalTicks;
uint32 OptionalDataSize;
uint32 Length;
uint32 Opcode;
};
#pragma pack(pop)
PacketLog::PacketLog() : _file(nullptr)
{
Initialize();
std::call_once(_initializeFlag, &PacketLog::Initialize, this);
}
PacketLog::~PacketLog()
@@ -38,20 +66,41 @@ void PacketLog::Initialize()
std::string logname = sConfigMgr->GetOption<std::string>("PacketLogFile", "");
if (!logname.empty())
{
_file = fopen((logsDir + logname).c_str(), "wb");
LogHeader header;
header.Signature[0] = 'P'; header.Signature[1] = 'K'; header.Signature[2] = 'T';
header.FormatVersion = 0x0301;
header.SnifferId = 'T';
header.Build = 12340;
header.Locale[0] = 'e'; header.Locale[1] = 'n'; header.Locale[2] = 'U'; header.Locale[3] = 'S';
std::memset(header.SessionKey, 0, sizeof(header.SessionKey));
header.SniffStartUnixtime = time(nullptr);
header.SniffStartTicks = getMSTime();
header.OptionalDataSize = 0;
fwrite(&header, sizeof(header), 1, _file);
}
}
void PacketLog::LogPacket(WorldPacket const& packet, Direction direction)
{
ByteBuffer data(4 + 4 + 4 + 1 + packet.size());
data << int32(packet.GetOpcode());
data << int32(packet.size());
data << uint32(time(nullptr));
data << uint8(direction);
std::lock_guard<std::mutex> lock(_logPacketLock);
for (uint32 i = 0; i < packet.size(); i++)
data << packet[i];
PacketHeader header;
*reinterpret_cast<uint32*>(header.Direction) = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
header.ConnectionId = 0;
header.ArrivalTicks = getMSTime();
header.OptionalDataSize = 0;
header.Length = packet.size() + 4;
header.Opcode = packet.GetOpcode();
fwrite(&header, sizeof(header), 1, _file);
if (!packet.empty())
{
fwrite(packet.contents(), 1, packet.size(), _file);
}
fwrite(data.contents(), 1, data.size(), _file);
fflush(_file);
}

View File

@@ -8,6 +8,7 @@
#define ACORE_PACKETLOG_H
#include "Common.h"
#include <mutex>
enum Direction
{
@@ -22,6 +23,8 @@ class PacketLog
private:
PacketLog();
~PacketLog();
std::mutex _logPacketLock;
std::once_flag _initializeFlag;
public:
static PacketLog* instance();