use BanInfo for inserting bans, send ban id and duration with KB packet

This commit is contained in:
in1tiate 2021-03-14 13:28:59 -05:00
parent b5779b1aa5
commit b562b77be9
3 changed files with 56 additions and 23 deletions

View File

@ -39,7 +39,9 @@ public:
QString getBanReason(QHostAddress ip); QString getBanReason(QHostAddress ip);
QString getBanReason(QString hdid); QString getBanReason(QString hdid);
long long getBanDuration(QString hdid); long long getBanDuration(QString hdid);
long long getBanDuration(QHostAddress hdid); long long getBanDuration(QHostAddress ip);
int getBanID(QString hdid);
int getBanID(QHostAddress ip);
struct BanInfo { struct BanInfo {
QString ipid; QString ipid;
@ -51,7 +53,7 @@ public:
}; };
QList<BanInfo> getRecentBans(); QList<BanInfo> getRecentBans();
void addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason, long long duration); void addBan(BanInfo ban);
bool createUser(QString username, QString salt, QString password, unsigned long long acl); bool createUser(QString username, QString salt, QString password, unsigned long long acl);
unsigned long long getACL(QString moderator_name); unsigned long long getACL(QString moderator_name);

View File

@ -95,6 +95,8 @@ void AOClient::cmdBan(int argc, QStringList argv)
args_str += " " + argv[i]; args_str += " " + argv[i];
} }
DBManager::BanInfo ban;
QRegularExpression quoteMatcher("['\"](.+?)[\"']"); QRegularExpression quoteMatcher("['\"](.+?)[\"']");
QRegularExpressionMatchIterator matches = quoteMatcher.globalMatch(args_str); QRegularExpressionMatchIterator matches = quoteMatcher.globalMatch(args_str);
QList<QString> unquoted_args; QList<QString> unquoted_args;
@ -103,7 +105,6 @@ void AOClient::cmdBan(int argc, QStringList argv)
unquoted_args.append(match.captured(1)); unquoted_args.append(match.captured(1));
} }
QString reason;
QString duration = "perma"; QString duration = "perma";
if (unquoted_args.length() < 1) { if (unquoted_args.length() < 1) {
@ -111,7 +112,7 @@ void AOClient::cmdBan(int argc, QStringList argv)
return; return;
} }
reason = unquoted_args.at(0); ban.reason = unquoted_args.at(0);
if (unquoted_args.length() > 1) if (unquoted_args.length() > 1)
duration = unquoted_args.at(1); duration = unquoted_args.at(1);
@ -126,34 +127,34 @@ void AOClient::cmdBan(int argc, QStringList argv)
return; return;
} }
QString target_ipid = argv[0]; ban.duration = duration_seconds;
QHostAddress ip;
QString hdid; ban.ipid = argv[0];
unsigned long time = QDateTime::currentDateTime().toSecsSinceEpoch(); ban.time = QDateTime::currentDateTime().toSecsSinceEpoch();
bool ban_logged = false; bool ban_logged = false;
int kick_counter = 0; int kick_counter = 0;
if (argc > 2) { if (argc > 2) {
for (int i = 2; i < argv.length(); i++) { for (int i = 2; i < argv.length(); i++) {
reason += " " + argv[i]; ban.reason += " " + argv[i];
} }
} }
for (AOClient* client : server->getClientsByIpid(target_ipid)) { for (AOClient* client : server->getClientsByIpid(ban.ipid)) {
if (!ban_logged) { if (!ban_logged) {
ip = client->remote_ip; ban.ip = client->remote_ip;
hdid = client->hwid; ban.hdid = client->hwid;
server->db_manager->addBan(target_ipid, ip, hdid, time, reason, duration_seconds); server->db_manager->addBan(ban);
sendServerMessage("Banned user with ipid " + target_ipid + " for reason: " + reason); sendServerMessage("Banned user with ipid " + ban.ipid + " for reason: " + ban.reason);
ban_logged = true; ban_logged = true;
} }
client->sendPacket("KB", {reason}); client->sendPacket("KB", {ban.reason + "\nID: " + QString::number(server->db_manager->getBanID(ban.ip)) + "\nUntil: " + QDateTime::fromSecsSinceEpoch(ban.time).addSecs(ban.duration).toString("dd.MM.yyyy, hh:mm")});
client->socket->close(); client->socket->close();
kick_counter++; kick_counter++;
} }
if (kick_counter > 1) if (kick_counter > 1)
sendServerMessage("Kicked " + QString::number(kick_counter) + " clients with matching ipids"); sendServerMessage("Kicked " + QString::number(kick_counter) + " clients with matching ipids.");
if (!ban_logged) if (!ban_logged)
sendServerMessage("User with ipid not found!"); sendServerMessage("User with ipid not found!");
} }

View File

@ -122,6 +122,36 @@ long long DBManager::getBanDuration(QHostAddress ip)
} }
} }
int DBManager::getBanID(QString hdid)
{
QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE HDID = ?");
query.addBindValue(hdid);
query.exec();
if (query.first()) {
return query.value(0).toInt();
}
else {
return -1;
}
}
int DBManager::getBanID(QHostAddress ip)
{
QSqlQuery query;
query.prepare("SELECT ID FROM BANS WHERE IP = ?");
query.addBindValue(ip.toString());
query.exec();
if (query.first()) {
return query.value(0).toInt();
}
else {
return -1;
}
}
QList<DBManager::BanInfo> DBManager::getRecentBans() QList<DBManager::BanInfo> DBManager::getRecentBans()
{ {
QList<BanInfo> return_list; QList<BanInfo> return_list;
@ -143,16 +173,16 @@ QList<DBManager::BanInfo> DBManager::getRecentBans()
return return_list; return return_list;
} }
void DBManager::addBan(QString ipid, QHostAddress ip, QString hdid, unsigned long time, QString reason, long long duration) void DBManager::addBan(BanInfo ban)
{ {
QSqlQuery query; QSqlQuery query;
query.prepare("INSERT INTO BANS(IPID, HDID, IP, TIME, REASON, DURATION) VALUES(?, ?, ?, ?, ?, ?)"); query.prepare("INSERT INTO BANS(IPID, HDID, IP, TIME, REASON, DURATION) VALUES(?, ?, ?, ?, ?, ?)");
query.addBindValue(ipid); query.addBindValue(ban.ipid);
query.addBindValue(hdid); query.addBindValue(ban.hdid);
query.addBindValue(ip.toString()); query.addBindValue(ban.ip.toString());
query.addBindValue(QString::number(time)); query.addBindValue(QString::number(ban.time));
query.addBindValue(reason); query.addBindValue(ban.reason);
query.addBindValue(duration); query.addBindValue(ban.duration);
if (!query.exec()) if (!query.exec())
qDebug() << "SQL Error:" << query.lastError().text(); qDebug() << "SQL Error:" << query.lastError().text();
} }