From 73381f97ef02f534680a607b4c5f800b62da1661 Mon Sep 17 00:00:00 2001 From: scatterflower Date: Thu, 8 Oct 2020 00:19:13 -0500 Subject: [PATCH] some logging improvements --- include/logger.h | 8 ++++- src/area_data.cpp | 2 +- src/commands.cpp | 6 ++-- src/logger.cpp | 74 ++++++++++++++++++++++++++--------------------- src/packets.cpp | 3 +- src/server.cpp | 7 ++--- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/include/logger.h b/include/logger.h index d86ab6c..fe21ff7 100644 --- a/include/logger.h +++ b/include/logger.h @@ -20,6 +20,7 @@ #include "include/aoclient.h" #include "include/aopacket.h" +#include "include/area_data.h" #include #include @@ -28,21 +29,26 @@ #include class AOClient; +class AreaData; class Logger { public: - Logger(int p_max_length); + Logger(int p_max_length, AreaData* p_area); void logIC(AOClient* client, AOPacket* packet); void logOOC(AOClient* client, AOPacket* packet); void logModcall(AOClient* client, AOPacket* packet); + void logCmd(AOClient* client, AOPacket* packet, QString cmd, QStringList args); + void logLogin(AOClient* client, bool success, QString modname); void flush(); private: + QString buildEntry(AOClient* client, QString type, QString message); void addEntry(QString entry); int max_length; QQueue buffer; + AreaData* area; }; #endif // LOGGER_H diff --git a/src/area_data.cpp b/src/area_data.cpp index 5ddcc48..63b57aa 100644 --- a/src/area_data.cpp +++ b/src/area_data.cpp @@ -40,5 +40,5 @@ AreaData::AreaData(QStringList characters, QString p_name, int p_index) int log_size = config_ini.value("logbuffer", 50).toInt(); if (log_size == 0) log_size = 500; - logger = new Logger(log_size); + logger = new Logger(log_size, this); } diff --git a/src/commands.cpp b/src/commands.cpp index 07b9887..ed631eb 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -44,8 +44,8 @@ void AOClient::cmdLogin(int argc, QStringList argv) authenticated = true; } else { sendServerMessage("Incorrect password."); - return; } + server->areas.value(current_area)->logger->logLogin(this, authenticated, "moderator"); } else { if (argc < 2) { @@ -57,11 +57,13 @@ void AOClient::cmdLogin(int argc, QStringList argv) if (server->db_manager->authenticate(username, password)) { moderator_name = username; authenticated = true; - sendServerMessage("Logged in as " + username); + sendServerMessage("Logged in as a moderator."); + sendServerMessage("Welcome, " + username); } else { sendServerMessage("Incorrect password."); } + server->areas.value(current_area)->logger->logLogin(this, authenticated, username); } } diff --git a/src/logger.cpp b/src/logger.cpp index 848a455..3dd5cf9 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -17,63 +17,68 @@ ////////////////////////////////////////////////////////////////////////////////////// #include "include/logger.h" -Logger::Logger(int p_max_length) +Logger::Logger(int p_max_length, AreaData* p_area) { + area = p_area; max_length = p_max_length; } void Logger::logIC(AOClient *client, AOPacket *packet) { - // TODO: copy pasted code - QString time = QDateTime::currentDateTime().toString("ddd MMMM d yyyy | hh:mm:ss"); - QString area_name = client->getServer()->area_names.value(client->current_area); - QString char_name = client->current_char; - QString ipid = client->getIpid(); QString message = packet->contents[4]; - - QString log_entry = QStringLiteral("[%1][%2][IC] %3(%4): %5\n") - .arg(time) - .arg(area_name) - .arg(char_name) - .arg(ipid) - .arg(message); - addEntry(log_entry); + addEntry(buildEntry(client, "IC", message)); } void Logger::logOOC(AOClient* client, AOPacket* packet) { - // TODO: copy pasted code - QString time = QDateTime::currentDateTime().toString("ddd MMMM d yyyy | hh:mm:ss"); - QString area_name = client->getServer()->area_names.value(client->current_area); - QString char_name = client->current_char; - QString ipid = client->getIpid(); QString message = packet->contents[1]; - - QString log_entry = QStringLiteral("[%1][%2][OOC] %3(%4): %5\n") - .arg(time) - .arg(area_name) - .arg(char_name) - .arg(ipid) - .arg(message); - addEntry(log_entry); + addEntry(buildEntry(client, "OOC", message)); } void Logger::logModcall(AOClient* client, AOPacket* packet) { - // TODO: copy pasted code + QString message = packet->contents[0]; + addEntry(buildEntry(client, "MODCALL", message)); +} + +void Logger::logCmd(AOClient *client, AOPacket *packet, QString cmd, QStringList args) +{ + // Some commands contain sensitive data, like passwords + // These must be filtered out + if (cmd == "login") { + addEntry(buildEntry(client, "LOGIN", "Attempted login")); + } + else if (cmd == "rootpass") { + addEntry(buildEntry(client, "USERS", "Root password created")); + } + else if (cmd == "adduser") { + addEntry(buildEntry(client, "USERS", "Added user " + args[0])); + } + else + logOOC(client, packet); +} + +void Logger::logLogin(AOClient *client, bool success, QString modname) +{ + QString message = success ? "Logged in as " + modname : "Failed to log in as " + modname; + addEntry(buildEntry(client, "LOGIN", message)); +} + +QString Logger::buildEntry(AOClient *client, QString type, QString message) +{ QString time = QDateTime::currentDateTime().toString("ddd MMMM d yyyy | hh:mm:ss"); - QString area_name = client->getServer()->area_names.value(client->current_area); + QString area_name = area->name; QString char_name = client->current_char; QString ipid = client->getIpid(); - QString message = packet->contents[0]; - QString log_entry = QStringLiteral("[%1][%2][MODCALL] %3(%4): %5\n") + QString log_entry = QStringLiteral("[%1][%2][%6] %3(%4): %5\n") .arg(time) .arg(area_name) .arg(char_name) .arg(ipid) - .arg(message); - addEntry(log_entry); + .arg(message) + .arg(type); + return log_entry; } void Logger::addEntry(QString entry) @@ -89,6 +94,9 @@ void Logger::addEntry(QString entry) void Logger::flush() { + // raiden suggested this, but idk if i want to use it + // QString time = QDateTime::currentDateTime().toString("ddd mm/dd/yy hh:mm:ss"); + // QString filename = QStringLiteral("reports/%1/%2.log").arg(area->name).arg(time); QFile logfile("config/server.log"); if (logfile.open(QIODevice::WriteOnly | QIODevice::Append)) { QTextStream file_stream(&logfile); diff --git a/src/packets.cpp b/src/packets.cpp index b584bba..a103916 100644 --- a/src/packets.cpp +++ b/src/packets.cpp @@ -159,13 +159,14 @@ void AOClient::pktOocChat(AreaData* area, int argc, QStringList argv, AOPacket p command = command.right(command.length() - 1); cmd_argv.removeFirst(); int cmd_argc = cmd_argv.length(); + area->logger->logCmd(this, &packet, command, cmd_argv); handleCommand(command, cmd_argc, cmd_argv); } else { // TODO: zalgo strip server->broadcast(packet, current_area); + area->logger->logOOC(this, &packet); } - area->logger->logOOC(this, &packet); } void AOClient::pktPing(AreaData* area, int argc, QStringList argv, AOPacket packet) diff --git a/src/server.cpp b/src/server.cpp index b307398..690a4f2 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -110,10 +110,9 @@ void Server::clientConnected() }); connect(socket, &QTcpSocket::readyRead, client, &AOClient::clientData); - AOPacket decryptor( - "decryptor", {"NOENCRYPT"}); // This is the infamous workaround for - // tsuserver4. It should disable fantacrypt - // completely in any client 2.4.3 or newer + AOPacket decryptor("decryptor", {"NOENCRYPT"}); // This is the infamous workaround for + // tsuserver4. It should disable fantacrypt + // completely in any client 2.4.3 or newer client->sendPacket(decryptor); #ifdef NET_DEBUG qDebug() << client->remote_ip.toString() << "connected";