From 0a9c1f1394665f8cdb7df503d084fffbeb63da55 Mon Sep 17 00:00:00 2001 From: jenz Date: Tue, 7 Feb 2023 20:45:15 +0100 Subject: [PATCH] updated db create script and updated mysql queries --- jenz_ban_detector/mysql/create_db.sql | 18 +++++++--- .../scripting/jenz_ban_detector.sp | 36 ++++++++++++++----- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/jenz_ban_detector/mysql/create_db.sql b/jenz_ban_detector/mysql/create_db.sql index 907a63b2..4c838568 100644 --- a/jenz_ban_detector/mysql/create_db.sql +++ b/jenz_ban_detector/mysql/create_db.sql @@ -1,13 +1,21 @@ CREATE TABLE `ban_detector` ( `fingerprint` varchar(512) NOT NULL, `ip` varchar(64) NOT NULL, - `steamid` varchar(64) DEFAULT NULL, - `name` varchar(128) DEFAULT NULL, - `created_on` datetime DEFAULT CURRENT_TIMESTAMP - PRIMARY KEY (`fingerprint`,`ip`) -); + `created_on` datetime DEFAULT CURRENT_TIMESTAMP, + `ID` int AUTO_INCREMENT, + PRIMARY KEY (`fingerprint`,`ip`), + KEY `ID` (`ID`) +) +CREATE TABLE `ban_detector_steamids` ( + `steamid` varchar(64) DEFAULT NULL, + `name` varchar(128) DEFAULT NULL, + `ID` int NOT NULL, + FOREIGN KEY (`ID`) REFERENCES ban_detector(ID) + ON DELETE CASCADE +) + --usefull read query select bd2.* from ban_detector bd2 inner join diff --git a/jenz_ban_detector/scripting/jenz_ban_detector.sp b/jenz_ban_detector/scripting/jenz_ban_detector.sp index 7b589107..5b089e85 100644 --- a/jenz_ban_detector/scripting/jenz_ban_detector.sp +++ b/jenz_ban_detector/scripting/jenz_ban_detector.sp @@ -13,6 +13,7 @@ int validate_state [MAXPLAYERS + 1]; Database g_dDatabase; Database g_hDatabase_sourceban; Handle g_hOnReportBanPostForward; +bool g_bReportedClientBanAvoiding[MAXPLAYERS + 1]; public Plugin myinfo = { @@ -41,12 +42,13 @@ public void OnPluginStart() if (IsValidClient(i)) { validate_state[i] = 0; + g_bReportedClientBanAvoiding[i] = false; } } CreateTimer(10.0, start_checks, _, TIMER_REPEAT); } -public void SQL_checkEntries(int client) +public void SQL_addEntry(int client) { char sQuery[g_dLength]; char sSID[MAX_NAME_LENGTH]; @@ -58,9 +60,7 @@ public void SQL_checkEntries(int client) GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); g_dDatabase.Escape(sName, sEscapedName, size2 + 1); GetClientIP(client, sIP, sizeof(sIP)); - - Format(sQuery, sizeof(sQuery), "UPDATE `ban_detector` SET steamid='%s', name='%s' WHERE ip='%s'; ", sSID, sEscapedName, sIP); - //PrintToChatAll("sQuery: %s", sQuery); + Format(sQuery, sizeof(sQuery), "insert into `ban_detector_steamids` (`steamid`, `name`, `ID`) SELECT '%s', '%s', bd.ID from `ban_detector` bd where bd.ip = '%s' and not exists (select bds.ID from `ban_detector_steamids` bds where bds.ID = bd.ID and bds.steamid = '%s')", sSID, sEscapedName, sIP, sSID); g_dDatabase.Query(SQL_UpdateEntry, sQuery, client, DBPrio_High); } @@ -96,12 +96,18 @@ public void SQL_UpdateEntry(Database db, DBResultSet results, const char[] error { char sQuery[g_dLength]; char sSID[MAX_NAME_LENGTH]; + char sIP[MAX_NAME_LENGTH]; + GetClientIP(client, sIP, sizeof(sIP)); GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); - Format(sQuery, sizeof(sQuery), "SELECT fingerprint FROM `ban_detector` where steamid = '%s' limit 1", sSID); + Format(sQuery, sizeof(sQuery), "SELECT bd.fingerprint FROM `ban_detector` bd inner join `ban_detector_steamids` bds on bd.ID = bds.ID where bds.steamid = '%s' or bd.ip = '%s'", sSID, sIP); //PrintToChatAll("sQuery: %s", sQuery); g_dDatabase.Query(SQL_FindFingerPrints, sQuery, client, DBPrio_High); } delete results; + if (IsValidClient(client)) + { + g_bReportedClientBanAvoiding[client] = false; + } } public void SQL_FindFingerPrints(Database db, DBResultSet results, const char[] error, int client) @@ -112,15 +118,19 @@ public void SQL_FindFingerPrints(Database db, DBResultSet results, const char[] return; } char fingerprint[1024]; - if (results.RowCount && results.FetchRow()) + while (results.RowCount > 0 && results.FetchRow()) { results.FetchString(0, fingerprint, sizeof(fingerprint)); char sQuery[1024]; char[] sEscapedFingerPrint = new char[1024]; g_dDatabase.Escape(fingerprint, sEscapedFingerPrint, 1024); - Format(sQuery, sizeof(sQuery), "select steamid, ip from ban_detector where fingerprint = '%s'", sEscapedFingerPrint); + Format(sQuery, sizeof(sQuery), "select steamid, ip from ban_detector bd inner join ban_detector_steamids bds on bd.ID = bds.ID where fingerprint = '%s'", sEscapedFingerPrint); //PrintToChatAll("sQuery: %s", sQuery); g_dDatabase.Query(SQL_checkSourcebans, sQuery, client, DBPrio_High); + if (IsValidClient(client) && g_bReportedClientBanAvoiding[client]) + { + break; + } } delete results; } @@ -145,6 +155,10 @@ public void SQL_checkSourcebans(Database db, DBResultSet results, const char[] e // + 3600 for one hour to accomdate timezone difference Format(sql_statement, sizeof(sql_statement), "select authid, ip from sb_bans where ((ip = '%s' and ip is not null and ip != '') or (authid = '%s' and authid is not null and authid =! '')) and (RemoveType != 'U' or RemoveType is NULL) and (ends > UNIX_TIMESTAMP() + 3600 or ends = created) order by created desc limit 1", sIP, sSID); g_hDatabase_sourceban.Query(sql_select_sb_bans, sql_statement, client, DBPrio_High); + if (IsValidClient(client) && g_bReportedClientBanAvoiding[client]) + { + break; + } } delete results; } @@ -163,8 +177,9 @@ public void sql_select_sb_bans(Database db, DBResultSet results, const char[] er return; } - if (results.RowCount && results.FetchRow()) + if (results.RowCount && results.FetchRow() && IsValidClient(client) && !g_bReportedClientBanAvoiding[client]) { + g_bReportedClientBanAvoiding[client] = true; char sSID[MAX_NAME_LENGTH]; char sIP[MAX_NAME_LENGTH]; results.FetchString(0, sSID, sizeof(sSID)); @@ -182,6 +197,7 @@ public void sql_select_sb_bans(Database db, DBResultSet results, const char[] er Call_PushString(sSID); } Call_Finish(); + // TODO in the future: just add a sourceban ban here on the client in the future for auto bans. should just be 30 minute bans } delete results; } @@ -205,7 +221,7 @@ public Action start_checks(Handle hTimer) if (IsValidClient(i) && validate_state[i] == 0) { validate_state[i] = -1; - SQL_checkEntries(i); + SQL_addEntry(i); } } } @@ -215,12 +231,14 @@ public Action start_checks(Handle hTimer) public void OnClientDisconnect(int client) { validate_state[client] = -1; + g_bReportedClientBanAvoiding[client] = false; } public void OnClientPostAdminCheck(int client) { validate_state[client] = -1; CreateTimer(10.0, make_db_entry, client); + g_bReportedClientBanAvoiding[client] = false; } public Action make_db_entry(Handle hTimer, int client)