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
This commit is contained in:
MangosArentLiterature 2021-06-09 23:32:55 -05:00
parent 555b4a0cbf
commit a561d3eb47
3 changed files with 18 additions and 103 deletions

View File

@ -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<bool, QString> isHDIDBanned(QString hdid);
/**
* @brief Gets the ID number of a given ban.

View File

@ -51,82 +51,26 @@ std::pair<bool, QString> DBManager::isIPBanned(QString ipid)
else return {false, nullptr};
}
bool DBManager::isHDIDBanned(QString hdid)
std::pair<bool, QString> 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;

View File

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