Guest User

Untitled

a guest
Jul 1st, 2014
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 6.84 KB | None | 0 0
  1. // Anti Server Flood v2 script by Amit_B
  2. #include "a_samp.inc"
  3. #undef MAX_PLAYERS
  4. #define MAX_PLAYERS 1000 // sa-mp player limit - do not change
  5. #define TIMER 20 // maximum slots for bots attack, increase only if 500 bots are connecting per less than ten second
  6. #define LOADING_UPDATE 10 // percent to log on blocked ips loading
  7. #define BLOCKFILE "antiflood.txt" // blocked ips file name
  8. #define SERVERIP "" // server ip. leave blank if you have 'bind' defined in your server.cfg
  9. #define DEBUG
  10. enum e_Connection { IP[MAX_PLAYERS], IPs, Next, Timer, bool:Used }
  11. new ConnectionPacks[TIMER][e_Connection], Player[MAX_PLAYERS][2], str[32],
  12.     lastTickCount = 0, serverIP[16], MAX_PLAYERS_ = 0, BlockedIP[MAX_PLAYERS][2],
  13.     BlockedIPs = 0;
  14. public OnFilterScriptInit()
  15. {
  16.     print("Anti server flood script v2.0 - loading");
  17.     MAX_PLAYERS_ = GetMaxPlayers();
  18.     if(strlen(SERVERIP) > 0) format(serverIP,sizeof(serverIP),SERVERIP);
  19.     else GetServerVarAsString("bind",serverIP,sizeof(serverIP));
  20.     for(new i = 0; i < MAX_PLAYERS_; i++) Player[i] = {-1,-1};
  21.     if(fexist(BLOCKFILE))
  22.     {
  23.         new File:fh = fopen(BLOCKFILE,io_read);
  24.         if(fh)
  25.         {
  26.             new string[20], l, fl = flength(fh), c = 0, j = 0;
  27.             const est = 16;
  28.             printf("Blocking estimated count of %d IPs",fl / est);
  29.             while(fread(fh,string))
  30.             {
  31.                 if((l = strlen(string)) < 9 || string[0] == '#') continue;
  32.                 for(j = 1; j <= 2; j++) if(string[l-j] == '\n' || string[l-j] == '\r') string[l-j] = 0;
  33.                 BlockIpAddress(string,0);
  34.                 if(++c % ((fl / est) / LOADING_UPDATE) == 0) printf("Blocked %d of %d (estimated count)",c,fl / est);
  35.             }
  36.             fclose(fh);
  37.         }
  38.     }
  39.     SetTimer("ClosingConnections",1000,1);
  40.     print("Anti server flood script v2.0 - loaded");
  41.     return 1;
  42. }
  43. public OnPlayerConnect(playerid)
  44. {
  45.     Remove(playerid);
  46.     return 1;
  47. }
  48. public OnPlayerDisconnect(playerid,reason)
  49. {
  50.     Remove(playerid);
  51.     return 1;
  52. }
  53. public OnPlayerCommandText(playerid,cmdtext[])
  54. {
  55.     if(!strcmp(cmdtext,"/unblockip",true) && IsPlayerAdmin(playerid))
  56.     {
  57.         UnBlockIpAddress(cmdtext[11]);
  58.         SendClientMessage(playerid,-1,"IP unblocked. Now you should remove it's line from \"" BLOCKFILE "\".");
  59.         return 1;
  60.     }
  61.     return 0;
  62. }
  63. public OnIncomingConnection(playerid,ip_address[],port)
  64. {
  65.     if(!strcmp(ip_address,serverIP)) return 1; // prevent NPCs from getting blocked
  66.     if(GetTickCount()-lastTickCount <= 50)
  67.     {
  68.         new cur = -1;
  69.         for(new i = 0; i < TIMER && cur == -1; i++) if(!ConnectionPacks[i][Used]) cur = i;
  70.         #if defined DEBUG
  71.             printf("debug: inserting %s into a pack (%d)",ip_address,cur);
  72.         #endif
  73.         if(cur > -1) if(ConnectionPacks[cur][Next] != -1)
  74.         {
  75.             new place = -1;
  76.             if(ConnectionPacks[cur][Next] == ConnectionPacks[cur][IPs]) ConnectionPacks[cur][IPs]++, place = ConnectionPacks[cur][Next]++;
  77.             else
  78.             {
  79.                 place = ConnectionPacks[cur][Next];
  80.                 for(new i = place; i < MAX_PLAYERS_ && place == -1; i++) if(ConnectionPacks[cur][IP][i] == 0) place = i;
  81.             }
  82.             if(place == -1 || place == MAX_PLAYERS_)
  83.             {
  84.                 ConnectionPacks[cur][Next] = -1;
  85.                 #if defined DEBUG
  86.                     printf("debug: was unable to place %s in pack %d at place %d",ip_address,cur,place);
  87.                 #endif
  88.             }
  89.             else
  90.             {
  91.                 do ConnectionPacks[cur][IP][place] = 1 + random(99999);
  92.                 while existproperty(.value=prfrmt(ConnectionPacks[cur][IP][place]));
  93.                 setproperty(.value=prfrmt(ConnectionPacks[cur][IP][place]),.string=ip_address);
  94.                 Player[playerid][0] = cur, Player[playerid][1] = place;
  95.                 #if defined DEBUG
  96.                     printf("debug: placed %s in pack %d at place %d",ip_address,cur,place);
  97.                 #endif
  98.             }
  99.             if(ConnectionPacks[cur][IPs] > 5)
  100.             {
  101.                 if(ConnectionPacks[cur][Timer] != 0) KillTimer(ConnectionPacks[cur][Timer]);
  102.                 ConnectionPacks[cur][Timer] = SetTimerEx("Timeout",1000,0,"ii",cur,false);
  103.             }
  104.         }
  105.     }
  106.     lastTickCount = GetTickCount();
  107.     return 1;
  108. }
  109. forward Timeout(pack,bool:block);
  110. public Timeout(pack,bool:block)
  111. {
  112.     if(block)
  113.     {
  114.         new File:fh = fopen(BLOCKFILE,fexist(BLOCKFILE) ? io_append : io_write);
  115.         if(fh)
  116.         {
  117.             for(new i = 0, val[16]; i < ConnectionPacks[pack][IPs]; i++)
  118.             {
  119.                 getproperty(.value=prfrmt(ConnectionPacks[pack][IP][i]),.string=val);
  120.                 strunpack(val,val,sizeof(val));
  121.                 BlockIpAddress(val,0);
  122.                 deleteproperty(.value=prfrmt(ConnectionPacks[pack][IP][i]));
  123.                 CloseConnection(val);
  124.                 format(str,sizeof(str),"%s\r\n",val);
  125.                 fwrite(fh,str);
  126.                 ConnectionPacks[pack][IP][i] = 0;
  127.             }
  128.             fclose(fh);
  129.             printf("Blocked %d new IPs",ConnectionPacks[pack][IPs]);
  130.             ConnectionPacks[pack][Used] = false, ConnectionPacks[pack][IPs] = 0, ConnectionPacks[pack][Next] = 0;
  131.         }
  132.     }
  133.     else
  134.     {
  135.         ConnectionPacks[pack][Next] = -1, ConnectionPacks[pack][Used] = true, ConnectionPacks[pack][Timer] = 0;
  136.         SetTimerEx("Timeout",TIMER*1000,0,"ii",pack,true);
  137.         #if defined DEBUG
  138.             printf("debug: timer has stopped for pack %d, blocking in " #TIMER "s",pack);
  139.         #endif
  140.     }
  141. }
  142. forward ClosingConnections();
  143. public ClosingConnections()
  144. {
  145.     new bool:unb = false;
  146.     for(new i = 0, val[32]; i < BlockedIPs; i++) if(BlockedIP[i][0] > 0 && BlockedIP[i][1] > 0)
  147.     {
  148.         if(!--BlockedIP[i][1])
  149.         {
  150.             if(existproperty(.value = prfrmt(BlockedIP[i][0],true)))
  151.             {
  152.                 getproperty(.value=prfrmt(prfrmt(BlockedIP[i][0],true)),.string=val);
  153.                 strunpack(val,val,sizeof(val));
  154.                 SendRconCommand((format(val,sizeof(val),"unbanip %s",val), val));
  155.                 deleteproperty(.value=prfrmt(prfrmt(BlockedIP[i][0],true)));
  156.                 if(!unb) unb = true;
  157.             }
  158.             for(new j = i; j < BlockedIPs && j < MAX_PLAYERS; j++) BlockedIP[j][0] = BlockedIP[j+1][0], BlockedIP[j][1] = BlockedIP[j+1][1];
  159.             BlockedIP[--BlockedIPs] = {0,0};
  160.         }
  161.     }
  162.     if(unb) SendRconCommand("reloadbans");
  163. }
  164. stock Remove(playerid)
  165. {
  166.     if(Player[playerid][0] != -1 && Player[playerid][1] != -1)
  167.     {
  168.         if(existproperty(.value = prfrmt(ConnectionPacks[Player[playerid][0]][IP][Player[playerid][1]]))) deleteproperty(.value=prfrmt(ConnectionPacks[Player[playerid][0]][IP][Player[playerid][1]]));
  169.         ConnectionPacks[Player[playerid][0]][IP][Player[playerid][1]] = 0, ConnectionPacks[Player[playerid][0]][Next] = Player[playerid][1];
  170.         #if defined DEBUG
  171.             printf("debug: removed place %d from %d as it's recognized as a real player",Player[playerid][0],Player[playerid][1]);
  172.         #endif
  173.         Player[playerid][0] = -1, Player[playerid][1] = -1;
  174.     }
  175. }
  176. stock CloseConnection(ip[])
  177. {
  178.     SendRconCommand((format(str,sizeof(str),"banip %s",ip), str));
  179.     do BlockedIP[BlockedIPs][0] = 1 + random(99999);
  180.     while existproperty(.value=prfrmt(BlockedIP[BlockedIPs][0],true));
  181.     setproperty(.value=prfrmt(BlockedIP[BlockedIPs][0],true),.string=ip);
  182.     BlockedIP[BlockedIPs][1] = 20, BlockedIPs++;
  183. }
  184. stock prfrmt(num,bool = false) return hash((format(str,sizeof(str),"%cip%d",bool ? 'b' : 'f',num), str));
  185. stock hash(prop[])
  186. {
  187.     new s[2] = {1,0};
  188.     for(new i = 0, l = strlen(prop); i < l; i++) s[0] = (s[0] + prop[i]) % 65521, s[1] = (s[1] + s[0]) % 65521;
  189.     return (s[1] << 16) + s[0];
  190. }
Advertisement
Add Comment
Please, Sign In to add comment