Ban System
#1

I have a decent and perfectly working ban system.
The problem is... It only bans the player with the name but not the IP itself.

How can i make a ban system which bans the IP too (storing them on a file text). For instence

Код:
127.0.0.1
192.168.1.1
If i am unbanned, 192.168.1.1 will be removed but the original content of .txt will remain the same (except the IP of the unbanned user is gone/wiped off).

I am using SQLite to do these stuffs.

PAWN Code:
Don't mind the indentation, the PAWN tag fucks it up. Apologize to my poor habit of using huge amount of useless string sizes.

pawn Код:
// OnFilterScriptInit

db_query(Database,
"CREATE TABLE IF NOT EXISTS `bans` (`username` TEXT, `ip` TEXT, `banby` TEXT, `banreason` TEXT, `banwhen` TEXT)");

// OnPlayerConnect

new
    bQuery[128+90],
    reason[128],
    admin[128],
    when[128],
    jip[20],
    jname[24],
    DBResult:jResult
;
format(bQuery, 600, "SELECT * FROM `bans` WHERE `username` = '%s'", GetName(playerid));
jResult = db_query(Database, bQuery);

if(db_num_rows(jResult))
{
    db_get_field_assoc(jResult, "username", jname, 24);
    db_get_field_assoc(jResult, "ip", jip, 20);
    db_get_field_assoc(jResult, "banby", admin, 128);
    db_get_field_assoc(jResult, "banreason", reason, 128);
    db_get_field_assoc(jResult, "banwhen", when, 128);

    ShowBanEx(playerid, jname, admin, reason, jip, when);

    KickDelay(playerid);
    return 1;
}
db_free_result(jResult);

// Stocks

stock BanAccountEx(name[], ip[], admin[] = "Server AC", reason[] = "None") // bans the offline player
{
    new
        Query[500],
        DBResult:result,
        ban_hr, ban_min, ban_sec, ban_month, ban_days, ban_years, when[128]
    ;

    gettime(ban_hr, ban_min, ban_sec);
    getdate(ban_years, ban_month, ban_days);

    format(when, 128, "%02d/%02d/%d %02d:%02d:%02d", ban_month, ban_days, ban_years, ban_hr, ban_min, ban_sec);

    format(Query, 500, "INSERT INTO `bans` (`username`, `ip`, `banby`, `banreason`, `banwhen`) VALUES ('%s', '%s', '%s', '%s', '%s')", DB_Escape(name), DB_Escape(ip), DB_Escape(admin), DB_Escape(reason), DB_Escape(when));
    result = db_query(Database, Query);

    db_free_result(result);
    return 1;
}

stock BanAccount(playerid, admin[] = "Server AC", reason[] = "None") // online player function
{
    new
        Query[500],
        DBResult:result,
        ban_hr, ban_min, ban_sec, ban_month, ban_days, ban_years, when[128]
    ;

    gettime(ban_hr, ban_min, ban_sec);
    getdate(ban_years, ban_month, ban_days);

    format(when, 128, "%02d/%02d/%d %02d:%02d:%02d", ban_month, ban_days, ban_years, ban_hr, ban_min, ban_sec);

    format(Query, 500, "INSERT INTO `bans` (`username`, `ip`, `banby`, `banreason`, `banwhen`) VALUES ('%s', '%s', '%s', '%s', '%s')", DB_Escape(GetName(playerid)), DB_Escape(User[playerid][accountIP]), DB_Escape(admin), DB_Escape(reason), DB_Escape(when));
    result = db_query(Database, Query);

    db_free_result(result);
   
    KickDelay(playerid);
    return 1;
}

stock ShowBanEx(playerid, player[], admin[] = "Server AC", reason[] = "None", ip[], when[] = "01/01/1970 00:00:00")
{
    new string[256], string2[1500];

    for(new i=0; i<100; i++)
    {
        SendClientMessage(playerid, -1, " ");
    }

    format(string, sizeof string, "Sorry %s, but you are banned from this server.", GetName(playerid));
    SendClientMessage(playerid, COLOR_JACKPOT, string);
    SendClientMessage(playerid, -1, "Please read the dialouge on your screen regarding the ban details.");

    strcat(string2, ""white"");
    strcat(string2, "Information regardless of the ban:\n\n");
    format(string, 256, ""red"Name: "white"%s\n", player);
    strcat(string2, string);
    format(string, 256, ""red"Banned By: "white"%s\n", admin);
    strcat(string2, string);
    format(string, 256, ""red"Reason: "white"%s\n", reason);
    strcat(string2, string);
    format(string, 256, ""red"IP Address: "white"%s\n", ip);
    strcat(string2, string);
    format(string, 256, ""red"Date: "white"%s\n\n", when);
    strcat(string2, string);
    strcat(string2, ""lightblue"Your current information (server-client):\n\n");
    format(string, 256, ""red"Name: "white"%s\n", GetName(playerid));
    strcat(string2, string);
    format(string, 256, ""red"IP Address: "white"%s\n\n", User[playerid][accountIP]);
    strcat(string2, string);
    strcat(string2, ""white"");
    strcat(string2, ""orange"You can appeal this ban by heading to our forums."white"\n");
    strcat(string2, "Make sure to take a picture of this by pressing "green"F8"white".");

    ShowPlayerDialog(playerid, DIALOG_NULL, DIALOG_STYLE_MSGBOX, ""red"Banned!", string2, "Close", "");
    return 1;
}

stock ShowBan(playerid, admin[] = "Server AC", reason[] = "None", when[] = "01/01/1970 00:00:00")
{
    new string[256], string2[1500];

    for(new i=0; i<100; i++)
    {
        SendClientMessage(playerid, -1, " ");
    }

    format(string, sizeof string, "Sorry %s, but you are banned from this server.", GetName(playerid));
    SendClientMessage(playerid, COLOR_JACKPOT, string);
    SendClientMessage(playerid, -1, "Please read the dialouge on your screen regarding the ban details.");

    strcat(string2, ""white"");
    strcat(string2, "Information regardless of the ban:\n\n");
    format(string, 256, ""red"Name: "white"%s\n", GetName(playerid));
    strcat(string2, string);
    format(string, 256, ""red"Banned By: "white"%s\n", admin);
    strcat(string2, string);
    format(string, 256, ""red"Reason: "white"%s\n", reason);
    strcat(string2, string);
    format(string, 256, ""red"IP Address: "white"%s\n", User[playerid][accountIP]);
    strcat(string2, string);
    format(string, 256, ""red"Date: "white"%s\n\n", when);
    strcat(string2, string);
    strcat(string2, ""white"");
    strcat(string2, ""orange"You can appeal this ban by heading to our forums."white"\n");
    strcat(string2, "Make sure to take a picture of this by pressing "green"F8"white".");

    ShowPlayerDialog(playerid, DIALOG_NULL, DIALOG_STYLE_MSGBOX, ""red"Banned!", string2, "Close", "");
    return 1;
}
Reply
#2

why not use Mysql account registration and making a simple new table,
Write a ban code, and fetching the bans from the database to your website, so you can simple view all bans in a schedule?
Reply
#3

I am not that good with MySQL, let's just say i can create a registration but i am just causing more troubles to myself (MySQL causes a server lag on my server months ago for misusing it's capability and functions.)

I wanted to start off with SQLite by the way.
Reply
#4

Why don't you use RCON's banip / unbanip?
just do
PHP код:
// banning ip:
new IP[16], tmpstr[64];
GetPlayerIp(playeridIPsizeof(IP));
format(tmpstrsizeof(tmpstr), "banip %s"IP);
SendRconCommand(tmpstr);
// unbanning ip, you have to restore/retrieve player's IP from the datatable.
new tmpstr[64];
// create your ip store variable, store ip in it and then format tmpstr with banned ip
format(tmpstrsizeof(tmpstr), "unbanip %s"IpVariable);
SendRconCommand(tmpstr); 
Sorry if I misunderstood your question.

EDIT:

And this thing can be done by MySQL / SQLite, by creating a table, storing banned IPs on it (a table with only IP column) ex:
PHP код:
INSERT INTO bannedip VALUES ('127.0.0.1'); 
and unbanning ip:
PHP код:
DELETE FROM `bannedipsWHERE `bannedip` = '127.0.0.1'
to check if player's banned:
PHP код:
SELECT `bannedipFROM `bannedipsWHERE `bannedip` = '127.0.0.1' LIMIT 1// mysql query / sql query.
if(rows)
{
    
// player's banned

Reply
#5

Thank you for your answer. But i am creating my own custom ban system which means the default ban system wouldn't be used or involved at all. (None of the ban functions will be used, just the code, and the Kick Function).
Reply
#6

Read my reply again, I edited it.
Reply
#7

Oh sweet! I am gonna try that out, thank you!
Reply
#8

One last question, How can i use the IP for strfind (ex: 192.168.1.1, separating the .1.1 but checks for 192.16

I wanted to do this because most of the time, users comes back with a dynamic changing IP, this trick works most of the time (it also prevent these changing IP techniques, i have seen this technique somewhere)
Reply
#9

i've made ban system on sqlite before 2 months or more .-., here is it >
PHP код:
#include a_samp
#include zcmd
#define red             0xFF0000AA
#define COLOR_Silver 0xCACACAAA
#define _DB_    "bansystem.db"
new banned[MAX_PLAYERS];
new 
DB:Database;
public 
OnFilterScriptInit()
{
    
Database db_open(_DB_);
    
db_query(Database,
    
"CREATE TABLE IF NOT EXISTS `banneds` (`name` TEXT, `ip` TEXT, `date` TEXT, `time` TEXT, `reason` TEXT, `bannedby` TEXT)");
    
db_query(Database,
    
"CREATE TABLE IF NOT EXISTS `bannedips` (`ip` TEXT)");
    print(
"Loading Ban System "_DB_" loading...");
    return 
1;
}
public 
OnFilterScriptExit() {
      print(
"Loading Ban System "_DB_" closing...");
    return 
db_close(Database);
}
public 
OnPlayerConnect(playerid) {
    
banned[playerid] = 0;
    new 
Query[250],str[350],ip1[128],name1[MAX_PLAYER_NAME];
    new 
string[50],content[50],savingstring[6][128];
    
GetPlayerName(playerid,name1,sizeof(name1));  GetPlayerIp(playeridip1sizeof(ip1));
    new 
DBResult:Result;
    
format(Querysizeof(Query), "SELECT * FROM `bannes` WHERE `name` = '%s'"name1);
    
Result db_query(DatabaseQuery);
    if(
db_num_rows(Result))
    {
    
db_get_field_assoc(Result"banned"savingstring[0], 10);
    
db_get_field_assoc(Result"date"savingstring[1], 10);
    
db_get_field_assoc(Result"time"savingstring[2], 10);
    
db_get_field_assoc(Result"bannedby"savingstring[3], 10);
    
db_get_field_assoc(Result"name"savingstring[4], 10);
    
db_get_field_assoc(Result"reason"savingstring[5], 10);
    
banned[playerid] = strval(savingstring[0]);
    if(
banned[playerid] == 1) {
    
format(strsizeof(str),"{C0C0C0}Your Account's currently Banned.\n\n{00FFFF}In-Game Name: {33FF33}%s\n{00FFFF}Administrator: {33FF33}%s\n{00FFFF}Date: {33FF33}%s\n{00FFFF}Reason: {33FF33}%s\n{C0C0C0}* if you think that you're banned wrongfully,\nfeel free to make unban appeal on our forum: forum",savingstring[4],savingstring[3],content,savingstring[5]);
    
ShowPlayerDialog(playerid801DIALOG_STYLE_MSGBOX"{FF0000}You Are Banned!"str"Close","");
    return 
SetTimerEx("kickplayer"500false"i"playerid);
    } if(!
db_num_rows(Result)) {
    
db_free_result(Result);
    
format(Querysizeof(Query), "SELECT * FROM bannedips WHERE ip = '%s'"ip1);
    
Result db_query(DatabaseQuery);
    if(
db_num_rows(Result)) {
    
format(string,sizeof(string),"%s has been Auto Kicked | Reason: Banned IP |",name1);
    
SendClientMessageToAll(COLOR_Silver,string);
    return 
SetTimerEx("kickplayer"500false"i"playerid);
    } if(!
db_num_rows(Result)) {
    new 
datum[50],hour1,minute1,second1,year1,month1,day1,tijd[50];
    
getdate(year1month1day1); gettime(hour1,minute1,second1);
    
format(datumsizeof(datum), "%d-%d-%d"year1month1day1); format(tijdsizeof(tijd), "%d:%d:%d"hour1minute1second1);
    
format(Querysizeof(Query),
    
"INSERT INTO `banneds`(`name`, `ip`, `date`, `time`, `bannedby`, `banned`) VALUES ('%s','%s','%s','%s',0,0)",name1ip1datumtijd);
    
db_query(DatabaseQuery);
    
db_free_result(Result);
    print(
"Hi");
      }
    }
      }
    return 
1;
 }
forward kickplayer(playerid);
public 
kickplayer(playerid) {
    return 
Kick(playerid);
}
public 
OnPlayerDisconnect(playerid) {
    new 
Query[250],DBResult:Result,ip1[128], datum[50],hour1,minute1,second1,year1,month1,day1,tijd[50],name1[MAX_PLAYER_NAME];
    
getdate(year1month1day1); GetPlayerName(playerid,name1,sizeof(name1)); gettime(hour1,minute1,second1);
    
format(datumsizeof(datum), "%d-%d-%d"year1month1day1); format(tijdsizeof(tijd), "%d:%d:%d"hour1minute1second1);
    
GetPlayerIp(playeridip1sizeof(ip1));
    
format(Querysizeof(Query),"SELECT * FROM banneds WHERE name = '%s'"name1);
    
Result db_query(DatabaseQuery);
    if(
db_num_rows(Result)) {
    if(
banned[playerid] == 0) {
    
format(Querysizeof(Query),"UPDATE `banneds` SET `ip` = '%s',`date` = '%s',`time` = '%s',`bannedby` = None, `banned` = 0 WHERE `name` = '%s'"ip1,datum,tijd,name1);
    
db_query(DatabaseQuery);
    
db_free_result(Result);
    return 
banned[playerid] = 0;
     }
    }
    return 
1;
 }
CMD:ban(playerid,params[]) {
    if(
IsPlayerAdmin(playerid)) {
            new 
strdate[128],strtime[128], tmp[256], tmp2[256], Index;        tmp strtok(params,Index), tmp2 strtok(params,Index);
            if(!
strlen(params)) return SendClientMessage(playeridred"USAGE: /ban [playerid] [reason]");
            if(!
strlen(tmp2)) return SendClientMessage(playeridred"ERROR: You must give a reason");
              new 
player1playername[MAX_PLAYER_NAME], ip[128], adminname[MAX_PLAYER_NAME], string[128];
            
player1 strval(tmp);
             
//if(IsPlayerConnected(player1) && player1 != INVALID_PLAYER_ID && player1 != playerid && !IsPlayerAdmin(player1)) {
                
GetPlayerName(player1playernamesizeof(playername)); GetPlayerName(playeridadminnamesizeof(adminname)); GetPlayerIp(playerid,ip,sizeof(ip));
                new 
year,month,day,hour,minuite,secondgetdate(yearmonthday); gettime(hour,minuite,second);
                
format(string,sizeof(string),"|- %s has been banned by Administrator %s [Reason: %s] [Date: %d/%d/%d] [Time: %d:%d] -|",playername,adminname,params[2],day,month,year,hour,minuite);
                
SendClientMessageToAll(COLOR_Silver,string);
                print(string);
                
format(strdatesizeof(strdate), "%d-%d-%d ",year,month,day);
                
format(strtime,sizeof(strtime),"%d:%d:%d",hour,minuite,second);
                new 
Query[250],DBResult:Result;
                
format(Querysizeof(Query),"SELECT * FROM banneds WHERE name = '%s'"playername);
                
Result db_query(DatabaseQuery);
                if(
db_num_rows(Result)) {
                
format(Querysizeof(Query),"UPDATE `banneds` SET `ip` = '%s',`date` = '%s',`time` = '%s',`bannedby` = '%s', `banned` = '%d',`reason` = '%s' WHERE `name` = '%s'"ip,strdate,strtime,adminname,1,params[2],playername);
                
db_query(DatabaseQuery);
                
format(Querysizeof(Query),"INSERT INTO `bannedips`(`ip`) VALUES ('%s')",ip);
                
db_query(DatabaseQuery);
                
db_free_result(Result);
                }
                return 
SetTimerEx("kickplayer"500false"i"playerid);
             
//} else return SendClientMessage(playerid, red, "Player is not connected or is yourself or is the highest level admin");
    
} else return SendClientMessage(playerid,red,"ERROR: You are not a high enough level to use this command");
}
CMD:unban(playerid,params[]) {
     if(
IsPlayerAdmin(playerid)) {
     if(
isnull(params)) return SendClientMessage(playerid,-1,"Usage: /unban [playername]");
     new 
Query[250],DBResult:Result,savingstring[50],savingstring1[50],unban,string[128];
     
format(Querysizeof(Query), "SELECT * FROM banneds WHERE name = '%s'"params);
     
Result db_query(DatabaseQuery);
     if(
db_num_rows(Result)) {
     
     
db_get_field_assoc(Result"banned"savingstring10);
     
db_get_field_assoc(Result"ip"savingstring110);
     
unban strval(savingstring);
//     if(unban == 1) {
     
format(Querysizeof(Query),"UPDATE `banneds` SET `banned` = 0 WHERE `name` = '%s'"params);
     
db_query(DatabaseQuery);
     
format(Querysizeof(Query),"DELETE FROM `bannedips` WHERE `IP` = '%s'"savingstring1);
     
db_query(DatabaseQuery);
     
format(string,sizeof(string),"%s successfully has been unbanned",params);
     
SendClientMessage(playerid,0x00FFFFAA,string);
     
db_free_result(Result);
         
//} else return SendClientMessage(playerid,-1,"ERROR: This Player is not banned");
      
} else return SendClientMessage(playerid,-1,"ERROR: Player Name Not Exist on Database");
     } else return 
SendClientMessage(playerid,red,"ERROR: You are not a high enough level to use this command");
     return 
1;
}
 
strtok(const string[], &index)
{
    new 
length strlen(string);
    while ((
index length) && (string[index] <= ' '))
    {
        
index++;
    }
    new 
offset index;
    new 
result[20];
    while ((
index length) && (string[index] > ' ') && ((index offset) < (sizeof(result) - 1)))
    {
        
result[index offset] = string[index];
        
index++;
    }
    
result[index offset] = EOS;
    return 
result;

i don't know if there are an bug but i don't think there're commands are: /ban id reason , /unban name
Reply
#10

Hello jlait thank you for helping me out, however i wanted to read the IP with the format of xxx.xxx instead of the whole IP (I am not entirely sure if you used strfind or what so ever on that code, however the ip ban system only works on one IP, so the next time they change their IP for example 192.172.82.1, i banned that IP, they can simply change to 192.172.82.5 and ban evade again)

I am not entirely sure how your system works, Reply Back.
Reply


Forum Jump:


Users browsing this thread: