From a561d3eb47eb3e56eac2314bce01b5ec7af8be85 Mon Sep 17 00:00:00 2001 From: MangosArentLiterature <58055358+MangosArentLiterature@users.noreply.github.com> Date: Wed, 9 Jun 2021 23:32:55 -0500 Subject: [PATCH] Mirror isIPBanned() changes to isHDIDBanned() this also removes getBanReason and getBanDuration as they are now no longer needed also minor clean up this websocket ip ban or whatever --- core/include/db_manager.h | 36 +++---------------- core/src/db_manager.cpp | 74 +++++---------------------------------- core/src/packets.cpp | 11 +++--- 3 files changed, 18 insertions(+), 103 deletions(-) diff --git a/core/include/db_manager.h b/core/include/db_manager.h index 1f340ee..1bc71e7 100644 --- a/core/include/db_manager.h +++ b/core/include/db_manager.h @@ -71,39 +71,11 @@ public: * * @param hdid The hardware ID to check if it is banned. * - * @return True if the query could return at least one such record. + * @return A pair of values: + * * First, a `bool` that is true if the query could return at least one such record. + * * Then, a `QString` that is the reason for the ban. */ - bool isHDIDBanned(QString hdid); - - /** - * @brief Returns the reason the given IP address was banned with. - * - * @param ip The IP address whose ban reason needs to be returned. - * - * @return The ban reason if the IP address is actually banned, - * or `"Ban reason not found."` if the IP address is not actually banned. - */ - QString getBanReason(QHostAddress ip); - - /** - * @overload - */ - QString getBanReason(QString hdid); - - /** - * @brief Returns the reason the given IP address was banned with. - * - * @param ip The IP address whose ban duration to get. - * - * @return The ban duration if the IP address is actually banned, - * or `-1` if the IP address is not actually banned. - */ - long long getBanDuration(QHostAddress ip); - - /** - * @overload - */ - long long getBanDuration(QString hdid); + std::pair isHDIDBanned(QString hdid); /** * @brief Gets the ID number of a given ban. diff --git a/core/src/db_manager.cpp b/core/src/db_manager.cpp index 3cd4fd7..0ce30aa 100644 --- a/core/src/db_manager.cpp +++ b/core/src/db_manager.cpp @@ -51,82 +51,26 @@ std::pair DBManager::isIPBanned(QString ipid) else return {false, nullptr}; } -bool DBManager::isHDIDBanned(QString hdid) +std::pair DBManager::isHDIDBanned(QString hdid) { QSqlQuery query; - query.prepare("SELECT TIME FROM BANS WHERE HDID = ? ORDER BY TIME DESC"); + query.prepare("SELECT * FROM BANS WHERE HDID = ? ORDER BY TIME DESC"); query.addBindValue(hdid); query.exec(); if (query.first()) { - long long duration = getBanDuration(hdid); - long long ban_time = query.value(0).toLongLong(); + long long ban_time = query.value(4).toLongLong(); + QString reason = query.value(5).toString(); + long long duration = query.value(6).toLongLong(); if (duration == -2) - return true; + return {true, reason}; long long current_time = QDateTime::currentDateTime().toSecsSinceEpoch(); if (ban_time + duration > current_time) - return true; - else return false; + return {true, reason}; + else return {false, nullptr}; } - else return false; + else return {false, nullptr}; } -QString DBManager::getBanReason(QHostAddress ip) -{ - QSqlQuery query; - query.prepare("SELECT REASON FROM BANS WHERE IP = ? ORDER BY TIME DESC"); - query.addBindValue(ip.toString()); - query.exec(); - if (query.first()) { - return query.value(0).toString(); - } - else { - return "Ban reason not found."; - } -} - -QString DBManager::getBanReason(QString hdid) -{ - QSqlQuery query; - query.prepare("SELECT REASON FROM BANS WHERE HDID = ? ORDER BY TIME DESC"); - query.addBindValue(hdid); - query.exec(); - if (query.first()) { - return query.value(0).toString(); - } - else { - return "Ban reason not found."; - } -} - -long long DBManager::getBanDuration(QString hdid) -{ - QSqlQuery query; - query.prepare("SELECT DURATION FROM BANS WHERE HDID = ? ORDER BY TIME DESC"); - query.addBindValue(hdid); - query.exec(); - if (query.first()) { - return query.value(0).toLongLong(); - } - else { - return -1; - } -} - -long long DBManager::getBanDuration(QHostAddress ip) -{ - QSqlQuery query; - query.prepare("SELECT DURATION FROM BANS WHERE IP = ? ORDER BY TIME DESC"); - query.addBindValue(ip.toString()); - query.exec(); - if (query.first()) { - return query.value(0).toLongLong(); - } - else { - return -1; - } -} - - int DBManager::getBanID(QString hdid) { QSqlQuery query; diff --git a/core/src/packets.cpp b/core/src/packets.cpp index 0f62caa..392b3dc 100644 --- a/core/src/packets.cpp +++ b/core/src/packets.cpp @@ -27,8 +27,9 @@ void AOClient::pktDefault(AreaData* area, int argc, QStringList argv, AOPacket p void AOClient::pktHardwareId(AreaData* area, int argc, QStringList argv, AOPacket packet) { hwid = argv[0]; - if(server->db_manager->isHDIDBanned(hwid)) { - sendPacket("BD", {server->db_manager->getBanReason(hwid) + "\nBan ID: " + QString::number(server->db_manager->getBanID(hwid))}); + auto ban = server->db_manager->isHDIDBanned(hwid); + if (ban.first) { + sendPacket("BD", {ban.second + "\nBan ID: " + QString::number(server->db_manager->getBanID(hwid))}); socket->close(); return; } @@ -318,10 +319,8 @@ void AOClient::pktWebSocketIp(AreaData* area, int argc, QStringList argv, AOPack calculateIpid(); if (remote_ip.isLoopback()) { auto ban = server->db_manager->isIPBanned(ipid); - bool is_banned = ban.first; - if(is_banned) { - QString reason = ban.second; - sendPacket("BD", {reason}); + if (ban.first) { + sendPacket("BD", {ban.second}); socket->close(); return; }