add ban durations (only works for hdid atm)

This commit is contained in:
scatterflower 2021-03-13 13:25:09 -06:00
parent f45099e47b
commit 6a38e50c4f
3 changed files with 72 additions and 15 deletions

View File

@ -19,6 +19,7 @@
#define BAN_MANAGER_H
#include <QDebug>
#include <QDateTime>
#include <QHostAddress>
#include <QMessageAuthenticationCode>
#include <QString>
@ -37,8 +38,10 @@ public:
QString getBanReason(QHostAddress ip);
QString getBanReason(QString hdid);
long long getBanDuration(QString hdid);
long long getBanDuration(QHostAddress hdid);
void addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason);
void addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason, long long duration);
bool createUser(QString username, QString salt, QString password, unsigned long long acl);
unsigned long long getACL(QString moderator_name);

View File

@ -115,16 +115,21 @@ void AOClient::cmdBan(int argc, QStringList argv)
if (unquoted_args.length() > 1)
duration = unquoted_args.at(1);
long long duration_seconds = 0;
if (duration == "perma")
duration_seconds = -2;
else
duration_seconds = parseTime(duration);
qDebug() << "Reason: " << reason;
qDebug() << "Duration: " << duration;
return;
if (duration_seconds == -1) {
sendServerMessage("Invalid time format. Format example: 1h30m");
return;
}
QString target_ipid = argv[0];
QHostAddress ip;
QString hdid;
unsigned long time = QDateTime::currentDateTime().toTime_t();
//QString reason = argv[1];
unsigned long time = QDateTime::currentDateTime().toSecsSinceEpoch();
bool ban_logged = false;
for (AOClient* client : server->clients) {
@ -132,8 +137,8 @@ void AOClient::cmdBan(int argc, QStringList argv)
if (!ban_logged) {
ip = client->remote_ip;
hdid = client->hwid;
server->db_manager->addBan(target_ipid, ip, hdid, time, reason);
sendServerMessage("Banned user with ipid " + target_ipid + " for reason: " + reason);
server->db_manager->addBan(target_ipid, ip, hdid, time, reason, duration_seconds);
sendServerMessage("Banned user with ipid " + target_ipid + " for " + duration + ". Reason: " + reason);
ban_logged = true;
}
client->sendPacket("KB", {reason});

View File

@ -24,26 +24,46 @@ DBManager::DBManager() :
db.setDatabaseName("config/akashi.db");
if (!db.open())
qCritical() << "Database Error:" << db.lastError();
QSqlQuery create_ban_table("CREATE TABLE IF NOT EXISTS bans ('ID' INTEGER, 'IPID' TEXT, 'HDID' TEXT, 'IP' TEXT, 'TIME' INTEGER, 'REASON' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))");
QSqlQuery create_ban_table("CREATE TABLE IF NOT EXISTS bans ('ID' INTEGER, 'IPID' TEXT, 'HDID' TEXT, 'IP' TEXT, 'TIME' INTEGER, 'REASON' TEXT, 'DURATION' INTEGER, PRIMARY KEY('ID' AUTOINCREMENT))");
QSqlQuery create_user_table("CREATE TABLE IF NOT EXISTS users ('ID' INTEGER, 'USERNAME' TEXT, 'SALT' TEXT, 'PASSWORD' TEXT, 'ACL' TEXT, PRIMARY KEY('ID' AUTOINCREMENT))");
}
bool DBManager::isIPBanned(QHostAddress ip)
{
QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE IP = ?");
query.prepare("SELECT DURATION FROM BANS WHERE IP = ? ORDER BY TIME DESC");
query.addBindValue(ip.toString());
query.exec();
return query.first();
if (query.first()) {
long long duration = getBanDuration(ip);
long long ban_time = query.value(0).toLongLong();
if (duration == -2)
return true;
long long current_time = QDateTime::currentDateTime().toSecsSinceEpoch();
if (ban_time + duration > current_time)
return true;
else return false;
}
else return false;
}
bool DBManager::isHDIDBanned(QString hdid)
{
QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE HDID = ?");
query.prepare("SELECT TIME FROM BANS WHERE HDID = ? ORDER BY TIME DESC");
query.addBindValue(hdid);
query.exec();
return query.first();
if (query.first()) {
long long duration = getBanDuration(hdid);
long long ban_time = query.value(0).toLongLong();
if (duration == -2)
return true;
long long current_time = QDateTime::currentDateTime().toSecsSinceEpoch();
if (ban_time + duration > current_time)
return true;
else return false;
}
else return false;
}
QString DBManager::getBanReason(QHostAddress ip)
@ -74,15 +94,44 @@ QString DBManager::getBanReason(QString hdid)
}
}
void DBManager::addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason)
long long DBManager::getBanDuration(QString hdid)
{
QSqlQuery query;
query.prepare("INSERT INTO BANS(IPID, HDID, IP, TIME, REASON) VALUES(?, ?, ?, ?, ?)");
query.prepare("SELECT DURATION FROM BANS WHERE HDID = ?");
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 = ?");
query.addBindValue(ip.toString());
query.exec();
if (query.first()) {
return query.value(0).toLongLong();
}
else {
return -1;
}
}
void DBManager::addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason, long long duration)
{
QSqlQuery query;
query.prepare("INSERT INTO BANS(IPID, HDID, IP, TIME, REASON, DURATION) VALUES(?, ?, ?, ?, ?, ?)");
query.addBindValue(ipid);
query.addBindValue(hdid);
query.addBindValue(ip.toString());
query.addBindValue(QString::number(time));
query.addBindValue(reason);
query.addBindValue(duration);
if (!query.exec())
qDebug() << "SQL Error:" << query.lastError().text();
}