2023-02-02 00:19:27 +01:00
# pragma semicolon 1
# define PLUGIN_AUTHOR "jenz"
# define g_dLength 400
# define PLUGIN_VERSION "1.0"
# pragma newdecls required
# include <sourcemod>
# include <clientprefs>
# include <cstrike>
# include <sdktools>
int validate_state [ MAXPLAYERS + 1 ] ;
Database g_dDatabase ;
2023-02-03 01:44:45 +01:00
Database g_hDatabase_sourceban ;
2023-02-03 11:38:47 +01:00
Handle g_hOnReportBanPostForward ;
2023-02-02 00:19:27 +01:00
public Plugin myinfo =
{
name = " jenz ban detector " ,
author = PLUGIN_AUTHOR ,
description = " my ban detector maybe catches you " ,
version = PLUGIN_VERSION ,
url = " www.unloze.com "
} ;
2023-02-03 11:38:47 +01:00
public APLRes AskPluginLoad2 ( Handle myself , bool late , char [ ] error , int err_max )
{
RegPluginLibrary ( " jenz_ban_detector " ) ;
return APLRes_Success ;
}
2023-02-02 00:19:27 +01:00
public void OnPluginStart ( )
{
2023-02-03 11:38:47 +01:00
g_hOnReportBanPostForward = CreateGlobalForward ( " BanDetectorPost " , ET_Ignore , Param_Cell , Param_String ) ;
2023-02-02 00:19:27 +01:00
if ( ! g_dDatabase )
{
Database . Connect ( SQL_OnDatabaseConnect , " jenz_ban_detector " ) ;
}
for ( int i = 0 ; i < MaxClients ; i + + )
{
if ( IsValidClient ( i ) )
{
validate_state [ i ] = 0 ;
}
}
CreateTimer ( 10.0 , start_checks , _ , TIMER_REPEAT ) ;
}
public void SQL_checkEntries ( int client )
{
char sQuery [ g_dLength ] ;
char sSID [ MAX_NAME_LENGTH ] ;
char sIP [ MAX_NAME_LENGTH ] ;
char sName [ MAX_NAME_LENGTH ] ;
GetClientName ( client , sName , sizeof ( sName ) ) ;
int size2 = 2 * strlen ( sName ) + 1 ;
char [ ] sEscapedName = new char [ size2 + 1 ] ;
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);
2023-02-03 01:44:45 +01:00
g_dDatabase . Query ( SQL_UpdateEntry , sQuery , client , DBPrio_High ) ;
2023-02-02 00:19:27 +01:00
}
public void SQL_OnDatabaseConnect ( Database db , const char [ ] error , any data )
{
if ( ! db | | strlen ( error ) )
{
LogError ( " Database error: %s " , error ) ;
return ;
}
g_dDatabase = db ;
}
2023-02-03 01:44:45 +01:00
public void SQL_OnDatabaseConnect_sb ( Database db , const char [ ] error , any data )
{
if ( ! db | | strlen ( error ) )
{
LogError ( " Database error: %s " , error ) ;
return ;
}
g_hDatabase_sourceban = db ;
}
public void SQL_UpdateEntry ( Database db , DBResultSet results , const char [ ] error , int client )
2023-02-02 00:19:27 +01:00
{
if ( ! db | | strlen ( error ) )
{
LogError ( " Database error: %s " , error ) ;
return ;
}
2023-02-03 01:44:45 +01:00
if ( IsValidClient ( client ) )
{
char sQuery [ g_dLength ] ;
char sSID [ MAX_NAME_LENGTH ] ;
GetClientAuthId ( client , AuthId_Steam2 , sSID , sizeof ( sSID ) ) ;
2023-02-03 11:38:47 +01:00
Format ( sQuery , sizeof ( sQuery ) , " SELECT fingerprint FROM `ban_detector` where steamid = '%s' limit 1 " , sSID ) ;
2023-02-03 01:44:45 +01:00
//PrintToChatAll("sQuery: %s", sQuery);
g_dDatabase . Query ( SQL_FindFingerPrints , sQuery , client , DBPrio_High ) ;
}
delete results ;
}
public void SQL_FindFingerPrints ( Database db , DBResultSet results , const char [ ] error , int client )
{
if ( ! db | | strlen ( error ) )
{
LogError ( " Database error: %s " , error ) ;
return ;
}
char fingerprint [ 1024 ] ;
if ( results . RowCount & & 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 ) ;
//PrintToChatAll("sQuery: %s", sQuery);
g_dDatabase . Query ( SQL_checkSourcebans , sQuery , client , DBPrio_High ) ;
}
delete results ;
}
public void SQL_checkSourcebans ( Database db , DBResultSet results , const char [ ] error , int client )
{
if ( ! db | | strlen ( error ) )
{
LogError ( " Database error: %s " , error ) ;
return ;
}
while ( results . RowCount > 0 & & results . FetchRow ( ) )
{
char sSID [ MAX_NAME_LENGTH ] ;
char sIP [ MAX_NAME_LENGTH ] ;
results . FetchString ( 0 , sSID , sizeof ( sSID ) ) ;
results . FetchString ( 1 , sIP , sizeof ( sIP ) ) ;
char sql_statement [ 512 ] ;
// + 3600 for one hour to accomdate timezone difference
Format ( sql_statement , sizeof ( sql_statement ) , " select authid, ip from sb_bans where (ip = '%s' or authid = '%s') 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 ) ;
}
delete results ;
}
public void sql_select_sb_bans ( Database db , DBResultSet results , const char [ ] error , int client )
{
if ( ! db | | strlen ( error ) )
{
LogError ( " Database error: %s " , error ) ;
return ;
}
if ( ! IsValidClient ( client ) )
{
delete results ;
return ;
}
if ( results . RowCount & & results . FetchRow ( ) )
{
char sSID [ MAX_NAME_LENGTH ] ;
char sIP [ MAX_NAME_LENGTH ] ;
results . FetchString ( 0 , sSID , sizeof ( sSID ) ) ;
results . FetchString ( 1 , sIP , sizeof ( sIP ) ) ;
2023-02-03 11:38:47 +01:00
Call_StartForward ( g_hOnReportBanPostForward ) ;
Call_PushCell ( client ) ;
2023-02-03 01:44:45 +01:00
if ( strlen ( sSID ) = = 0 )
{
2023-02-03 11:38:47 +01:00
// use IP instead if no steamID
Call_PushString ( sIP ) ;
2023-02-03 01:44:45 +01:00
}
2023-02-03 11:38:47 +01:00
else
{
//found steamID
Call_PushString ( sSID ) ;
}
Call_Finish ( ) ;
2023-02-03 01:44:45 +01:00
}
delete results ;
2023-02-02 00:19:27 +01:00
}
public Action start_checks ( Handle hTimer )
{
if ( ! g_dDatabase )
{
Database . Connect ( SQL_OnDatabaseConnect , " jenz_ban_detector " ) ;
}
else
{
2023-02-03 01:44:45 +01:00
if ( ! g_hDatabase_sourceban )
{
Database . Connect ( SQL_OnDatabaseConnect_sb , " sourcebans " ) ;
}
else
2023-02-02 00:19:27 +01:00
{
2023-02-03 01:44:45 +01:00
for ( int i = 0 ; i < MaxClients ; i + + )
2023-02-02 00:19:27 +01:00
{
2023-02-03 01:44:45 +01:00
if ( IsValidClient ( i ) & & validate_state [ i ] = = 0 )
{
validate_state [ i ] = - 1 ;
SQL_checkEntries ( i ) ;
}
2023-02-02 00:19:27 +01:00
}
}
}
}
public void OnClientDisconnect ( int client )
{
validate_state [ client ] = - 1 ;
}
public void OnClientPostAdminCheck ( int client )
{
validate_state [ client ] = - 1 ;
CreateTimer ( 10.0 , make_db_entry , client ) ;
}
public Action make_db_entry ( Handle hTimer , int client )
{
if ( IsValidClient ( client ) )
{
validate_state [ client ] = 0 ;
}
}
stock bool IsValidClient ( int client )
{
if ( client > 0 & & client < = MaxClients & & IsClientConnected ( client ) & & IsClientInGame ( client ) & & ! IsFakeClient ( client ) )
return true ;
return false ;
}