2012-08-17 10:36:48 +02:00
|
|
|
/*
|
2020-01-02 06:44:10 +01:00
|
|
|
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
|
2012-08-17 10:36:48 +02:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
* option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
|
* more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "PacketLog.h"
|
|
|
|
|
#include "Config.h"
|
2017-12-10 12:39:37 +01:00
|
|
|
#include "GameTime.h"
|
2018-01-06 01:21:59 +01:00
|
|
|
#include "IpAddress.h"
|
2024-08-18 18:59:58 +02:00
|
|
|
#include "RealmList.h"
|
2014-07-20 02:54:12 +02:00
|
|
|
#include "Timer.h"
|
2017-05-18 23:52:58 +02:00
|
|
|
#include "WorldPacket.h"
|
2012-08-17 10:36:48 +02:00
|
|
|
|
2014-07-19 13:30:51 +02:00
|
|
|
#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
|
|
|
|
|
{
|
2014-07-30 03:44:30 +01:00
|
|
|
// used to uniquely identify a connection
|
|
|
|
|
struct OptionalData
|
|
|
|
|
{
|
|
|
|
|
uint8 SocketIPBytes[16];
|
|
|
|
|
uint32 SocketPort;
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-23 13:22:03 +02:00
|
|
|
uint32 Direction;
|
2014-07-19 13:30:51 +02:00
|
|
|
uint32 ConnectionId;
|
|
|
|
|
uint32 ArrivalTicks;
|
|
|
|
|
uint32 OptionalDataSize;
|
|
|
|
|
uint32 Length;
|
2014-07-30 03:44:30 +01:00
|
|
|
OptionalData OptionalData;
|
2014-07-19 13:30:51 +02:00
|
|
|
uint32 Opcode;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
2020-08-14 17:06:03 +02:00
|
|
|
PacketLog::PacketLog() : _file(nullptr)
|
2012-08-17 10:36:48 +02:00
|
|
|
{
|
2014-08-02 23:38:25 +01:00
|
|
|
std::call_once(_initializeFlag, &PacketLog::Initialize, this);
|
2012-08-17 10:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PacketLog::~PacketLog()
|
|
|
|
|
{
|
|
|
|
|
if (_file)
|
|
|
|
|
fclose(_file);
|
|
|
|
|
|
2020-08-14 17:06:03 +02:00
|
|
|
_file = nullptr;
|
2012-08-17 10:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
2016-03-11 17:11:05 +01:00
|
|
|
PacketLog* PacketLog::instance()
|
|
|
|
|
{
|
|
|
|
|
static PacketLog instance;
|
|
|
|
|
return &instance;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-17 10:36:48 +02:00
|
|
|
void PacketLog::Initialize()
|
|
|
|
|
{
|
2013-07-15 17:31:44 +02:00
|
|
|
std::string logsDir = sConfigMgr->GetStringDefault("LogsDir", "");
|
2012-08-17 10:36:48 +02:00
|
|
|
|
|
|
|
|
if (!logsDir.empty())
|
2014-07-19 13:30:51 +02:00
|
|
|
if ((logsDir.at(logsDir.length() - 1) != '/') && (logsDir.at(logsDir.length() - 1) != '\\'))
|
2012-08-17 10:36:48 +02:00
|
|
|
logsDir.push_back('/');
|
|
|
|
|
|
2013-07-15 17:31:44 +02:00
|
|
|
std::string logname = sConfigMgr->GetStringDefault("PacketLogFile", "");
|
2012-08-17 10:36:48 +02:00
|
|
|
if (!logname.empty())
|
2014-07-19 13:30:51 +02:00
|
|
|
{
|
2012-08-17 10:36:48 +02:00
|
|
|
_file = fopen((logsDir + logname).c_str(), "wb");
|
2014-07-19 13:30:51 +02:00
|
|
|
|
2014-09-14 16:34:15 +02:00
|
|
|
if (CanLogPacket())
|
2017-05-18 23:52:58 +02:00
|
|
|
{
|
|
|
|
|
LogHeader header;
|
|
|
|
|
header.Signature[0] = 'P'; header.Signature[1] = 'K'; header.Signature[2] = 'T';
|
|
|
|
|
header.FormatVersion = 0x0301;
|
|
|
|
|
header.SnifferId = 'T';
|
2024-08-18 18:59:58 +02:00
|
|
|
if (std::shared_ptr<Realm const> currentRealm = sRealmList->GetCurrentRealm())
|
|
|
|
|
header.Build = currentRealm->Build;
|
|
|
|
|
else
|
|
|
|
|
header.Build = 0;
|
2017-05-18 23:52:58 +02:00
|
|
|
header.Locale[0] = 'e'; header.Locale[1] = 'n'; header.Locale[2] = 'U'; header.Locale[3] = 'S';
|
|
|
|
|
std::memset(header.SessionKey, 0, sizeof(header.SessionKey));
|
2017-12-10 12:39:37 +01:00
|
|
|
header.SniffStartUnixtime = GameTime::GetGameTime();
|
2017-05-18 23:52:58 +02:00
|
|
|
header.SniffStartTicks = getMSTime();
|
|
|
|
|
header.OptionalDataSize = 0;
|
|
|
|
|
|
2014-09-14 16:34:15 +02:00
|
|
|
fwrite(&header, sizeof(header), 1, _file);
|
2017-05-18 23:52:58 +02:00
|
|
|
}
|
2014-07-19 13:30:51 +02:00
|
|
|
}
|
2012-08-17 10:36:48 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-09 20:48:13 +01:00
|
|
|
void PacketLog::LogPacket(WorldPacket const& packet, Direction direction, boost::asio::ip::address const& addr, uint16 port, ConnectionType connectionType)
|
2012-08-17 10:36:48 +02:00
|
|
|
{
|
2014-08-02 23:38:25 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_logPacketLock);
|
|
|
|
|
|
2014-07-19 13:30:51 +02:00
|
|
|
PacketHeader header;
|
2016-04-23 13:22:03 +02:00
|
|
|
header.Direction = direction == CLIENT_TO_SERVER ? 0x47534d43 : 0x47534d53;
|
2014-11-09 20:48:13 +01:00
|
|
|
header.ConnectionId = connectionType;
|
2014-07-19 13:30:51 +02:00
|
|
|
header.ArrivalTicks = getMSTime();
|
2014-07-30 03:44:30 +01:00
|
|
|
|
|
|
|
|
header.OptionalDataSize = sizeof(header.OptionalData);
|
|
|
|
|
memset(header.OptionalData.SocketIPBytes, 0, sizeof(header.OptionalData.SocketIPBytes));
|
|
|
|
|
if (addr.is_v4())
|
|
|
|
|
{
|
|
|
|
|
auto bytes = addr.to_v4().to_bytes();
|
|
|
|
|
memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
|
|
|
|
|
}
|
|
|
|
|
else if (addr.is_v6())
|
|
|
|
|
{
|
|
|
|
|
auto bytes = addr.to_v6().to_bytes();
|
|
|
|
|
memcpy(header.OptionalData.SocketIPBytes, bytes.data(), bytes.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header.OptionalData.SocketPort = port;
|
2019-11-21 20:40:59 +01:00
|
|
|
std::size_t size = packet.size();
|
|
|
|
|
if (direction == CLIENT_TO_SERVER)
|
2024-10-30 14:40:36 +01:00
|
|
|
size -= 4;
|
2019-11-21 20:40:59 +01:00
|
|
|
|
|
|
|
|
header.Length = size + sizeof(header.Opcode);
|
2014-07-19 13:30:51 +02:00
|
|
|
header.Opcode = packet.GetOpcode();
|
2012-08-17 10:36:48 +02:00
|
|
|
|
2014-07-19 13:30:51 +02:00
|
|
|
fwrite(&header, sizeof(header), 1, _file);
|
2019-11-21 20:40:59 +01:00
|
|
|
if (size)
|
2019-07-12 20:42:49 +02:00
|
|
|
{
|
2025-05-15 19:18:41 +02:00
|
|
|
uint8 const* data = packet.data();
|
2019-07-12 20:42:49 +02:00
|
|
|
if (direction == CLIENT_TO_SERVER)
|
2024-10-30 14:40:36 +01:00
|
|
|
data += 4;
|
2019-09-08 01:24:24 +02:00
|
|
|
fwrite(data, 1, size, _file);
|
2019-07-12 20:42:49 +02:00
|
|
|
}
|
2012-08-17 10:36:48 +02:00
|
|
|
|
|
|
|
|
fflush(_file);
|
|
|
|
|
}
|