some logging improvements

This commit is contained in:
scatterflower 2020-10-08 00:19:13 -05:00
parent 0a29665927
commit 73381f97ef
6 changed files with 58 additions and 42 deletions

View File

@ -20,6 +20,7 @@
#include "include/aoclient.h"
#include "include/aopacket.h"
#include "include/area_data.h"
#include <QFile>
#include <QDebug>
@ -28,21 +29,26 @@
#include <QDateTime>
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<QString> buffer;
AreaData* area;
};
#endif // LOGGER_H

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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)

View File

@ -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";