Advertisement
Guest User

CIDR Ban

a guest
Jun 26th, 2013
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 9.87 KB | None | 0 0
  1. /***                                                    ***
  2.  * CIDR Ban - Ban CIDR Ranges                             *
  3.  ***                                                    ***
  4.  * @Author  Rafael 'R@f' Keramidas <rafael@keramid.as>    *
  5.  * @Date    12th June 2013                                *
  6.  * @Version 1.0                                           *
  7.  * @Licence MIT License                                   *
  8.  * @Comment The Regex Plugin is required                  *
  9.  *                                                        *
  10.  * Copyright (C) 2013 Rafael Keramidas                    *
  11.  *                                                        *
  12.  * Permission is hereby granted, free of charge, to any   *
  13.  * person obtaining a copy of this software and           *
  14.  * associated documentation files (the "Software"), to    *
  15.  * deal in the Software without restriction, including    *
  16.  * without limitation the rights to use, copy, modify,    *
  17.  * merge, publish, distribute, sublicense, and/or sell    *
  18.  * copies of the Software, and to permit persons to whom  *
  19.  * the Software is furnished to do so, subject to the     *
  20.  * following conditions:                                  *
  21.  *                                                        *
  22.  * The above copyright notice and this permission notice  *
  23.  * shall be included in all copies or substantial         *
  24.  * portions of the Software.                              *
  25.  *                                                        *
  26.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF  *
  27.  * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT        *
  28.  * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS  *
  29.  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO    *
  30.  * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE *
  31.  * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN  *
  32.  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      *
  33.  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
  34.  * USE OR OTHER DEALINGS IN THE SOFTWARE.                 *
  35.  ***                                                    ***/
  36.  
  37. #include <a_samp>
  38. #include <a_http>
  39. #include <regex>
  40. #include <cidr>
  41.  
  42. #define BANLIST         "cidr-banlist.txt"
  43.  
  44. #define KickPlayer(%0)  SetTimerEx("KickTimer", 600, 0, "i", %0)
  45.  
  46. #define COLOR_RED       0xAA3333AA
  47. #define COLOR_YELLOW    0xFFFF00AA
  48. #define COLOR_GREEN     0x33AA33AA
  49.  
  50. /*############################################################################*/
  51.  
  52. public OnFilterScriptInit()
  53. {
  54.     print("\n######################################");
  55.     print("#                     #");
  56.     print("# CIDR Ban By Rafael 'R@f' Keramidas #");
  57.     print("#                     #");
  58.     print("######################################\n");
  59.     return true;
  60. }
  61.  
  62. /*############################################################################*/
  63.  
  64. public OnPlayerConnect(playerid)
  65. {
  66.     new
  67.         sPlayerName[MAX_PLAYER_NAME],
  68.         sPlayerIP[20];
  69.  
  70.     GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
  71.     GetPlayerIp(playerid, sPlayerIP, sizeof(sPlayerIP));
  72.  
  73.     /* Check if the player is range banned */
  74.     if(IsRangeBanned(sPlayerIP))
  75.     {
  76.         SendClientMessage(playerid, COLOR_RED, "Your IP is range banned from this server.");
  77.         printf("%s (%s) is range banned from the server.", sPlayerName, sPlayerIP);
  78.         KickPlayer(playerid);
  79.     }
  80.     return true;
  81. }
  82.  
  83. /*############################################################################*/
  84.  
  85. public OnPlayerCommandText(playerid, cmdtext[])
  86. {
  87.     new
  88.         sMsg[128],
  89.         sCmd[128],
  90.         sCIDRRange[32],
  91.         sStartIP[32],
  92.         sEndIP[32],
  93.         iIndex;
  94.  
  95.     sCmd = strtok(cmdtext, iIndex);
  96.  
  97.     /**
  98.      * /bancidr - Ban a CIDR range.
  99.      *
  100.      * @description     Will add the CIDR range to the range ban file and kick all
  101.      *                  current players that are in this range.
  102.      * @param 1         CIDR range (for example: 192.168.1.0/24).
  103.      **/
  104.     if(strcmp(sCmd, "/bancidr", true) == 0)
  105.     {
  106.         /* Check if the player is admin */
  107.         if(!IsPlayerAdmin(playerid))
  108.             return SendClientMessage(playerid, COLOR_RED, "ERROR: You must be admin to use this command !");
  109.  
  110.         sCIDRRange = strtok(cmdtext, iIndex);
  111.         /* Check if the CIDR parameter is empty */
  112.         if(!strlen(sCIDRRange))
  113.             return SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /bancidr [CIDR RANGE] - EXAMPLE: /bancidr 192.168.1.0/24");
  114.  
  115.         /* Check if the CIDR range is valid */
  116.         if(!IsValidCIDR(sCIDRRange))
  117.             return SendClientMessage(playerid, COLOR_RED, "ERROR: Invalid CIDR !");
  118.  
  119.         /* Show a message to the admin */
  120.         format(sMsg, sizeof(sMsg), "The range %s has been banned successfully.", sCIDRRange);
  121.         SendClientMessage(playerid, COLOR_GREEN, sMsg);
  122.  
  123.         /* Ban the range and kick the players in this range from the server */
  124.         BanCIDRRange(sCIDRRange);
  125.         BanCurrentPlayersInRange(sCIDRRange);
  126.        
  127.         return true;
  128.     }
  129.  
  130.     /**
  131.      * /getcidr - Get a CIDR range from two IPs.
  132.      *
  133.      * @description     Get the CIDR range by giving two IPs (start and ending IP).
  134.      * @param 1         Starting IP
  135.      * @param 2         Ending IP
  136.      **/
  137.     if(strcmp(sCmd, "/getcidr", true) == 0)
  138.     {
  139.         if(!IsPlayerAdmin(playerid))
  140.             return SendClientMessage(playerid, COLOR_RED, "ERROR: You must be admin to use this command !");
  141.  
  142.         sStartIP = strtok(cmdtext, iIndex);
  143.         sEndIP = strtok(cmdtext, iIndex);
  144.         /* Check if one of the IP parameters is empty */
  145.         if(!strlen(sStartIP) || !strlen(sEndIP))
  146.             return SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /getcidr [START IP] [END IP] - EXAMPLE: /getcidr 192.168.1.0 192.168.1.42");
  147.  
  148.         /* Check if the IP range is valid */
  149.         if(!IsValidIP(sStartIP) || !IsValidIP(sEndIP))
  150.             return SendClientMessage(playerid, COLOR_RED, "ERROR: Invalid IP!");
  151.  
  152.         /* Send a HTTP request to get the CIDR ranges */
  153.         format(sMsg, sizeof(sMsg), "rafael.keramid.as/samp/cidrcalc.php?startip=%s&endip=%s", sStartIP, sEndIP);
  154.         HTTP(playerid, HTTP_GET, sMsg, "", "ShowCIDRRange");
  155.        
  156.         return true;
  157.     }
  158.     return false;
  159. }
  160.  
  161. /*############################################################################*/
  162.  
  163. /* Strtok - Not by me. */
  164. strtok(const string[], &index)
  165. {
  166.     new length = strlen(string);
  167.     while ((index < length) && (string[index] <= ' '))
  168.     {
  169.         index++;
  170.     }
  171.  
  172.     new offset = index;
  173.     new result[20];
  174.     while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
  175.     {
  176.         result[index - offset] = string[index];
  177.         index++;
  178.     }
  179.     result[index - offset] = EOS;
  180.     return result;
  181. }
  182.  
  183. /*############################################################################*/
  184.  
  185. /* CIDR validation regexp */
  186. stock IsValidCIDR(const sCIDR[])
  187. {
  188.     static
  189.         RegEx:rCIDR;
  190.  
  191.     if(!rCIDR)
  192.         rCIDR = regex_build("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(/(3[012]|[12]?[0-9]))");
  193.  
  194.     return regex_match_exid(sCIDR, rCIDR);
  195. }
  196.  
  197. /*############################################################################*/
  198.  
  199. /* IP validation regexp */
  200. stock IsValidIP(const sIP[])
  201. {
  202.     static
  203.         RegEx:rIP;
  204.  
  205.     if(!rIP)
  206.         rIP = regex_build("(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)");
  207.  
  208.     return regex_match_exid(sIP, rIP);
  209. }
  210.  
  211. /*############################################################################*/
  212.  
  213. /* Check if an IP is in the CIDR ban file */
  214. stock IsRangeBanned(sClientIP[]) {
  215.     new
  216.         bool:bBanned = false,
  217.         sBanCIDR[24],
  218.         File:fBanList = fopen(BANLIST, io_readwrite);
  219.  
  220.     while(fread(fBanList, sBanCIDR))
  221.     {
  222.         RemoveNewLine(sBanCIDR);
  223.         if(IsValidCIDR(sBanCIDR))
  224.             if(cidr_match(sClientIP, sBanCIDR))
  225.                 bBanned = true;
  226.     }
  227.  
  228.     fclose(fBanList);
  229.     return bBanned;
  230. }
  231.  
  232. /*############################################################################*/
  233.  
  234. /* Add a CIDR range to the ban file */
  235. stock BanCIDRRange(sCIDR[])
  236. {
  237.     new
  238.         sBanCIDR[24],
  239.         File:fBanList = fopen(BANLIST, io_append);
  240.  
  241.     if(fBanList)
  242.     {
  243.         format(sBanCIDR, sizeof(sBanCIDR), "%s\r\n", sCIDR);
  244.         fwrite(fBanList, sBanCIDR);
  245.         fclose(fBanList);
  246.     }
  247. }
  248.  
  249. /*############################################################################*/
  250.  
  251. /* Kick all current players in the CIDR range from the server */
  252. stock BanCurrentPlayersInRange(sCIDR[])
  253. {
  254.     new
  255.         sPlayerIP[20];
  256.  
  257.     for(new i = 0; i < MAX_PLAYERS; i++)
  258.     {
  259.         if(IsPlayerConnected(i))
  260.         {
  261.             GetPlayerIp(i, sPlayerIP, sizeof(sPlayerIP));
  262.             if(cidr_match(sPlayerIP, sCIDR))
  263.             {
  264.                 SendClientMessage(i, COLOR_RED, "You have been range banned from this server.");
  265.                 KickPlayer(i);
  266.             }
  267.         }
  268.     }
  269. }
  270.  
  271. /*############################################################################*/
  272.  
  273. /* Returns an array with multiple CIDR ranges that matches the two parameters */
  274. stock RangeToCIDR(sStartIP[], sEndIP[])
  275. {
  276.     new
  277.         iStartIP = 0;
  278. }
  279.  
  280. /*############################################################################*/
  281.  
  282. /* Remove all new line chars (\n and \r) */
  283. stock RemoveNewLine(sString[])
  284. {
  285.     new
  286.         i = 0;
  287.  
  288.     while(sString[i] != 0)
  289.     {
  290.         if(sString[i] == '\n' || sString[i] == '\r')
  291.             sString[i] = 0;
  292.  
  293.         else
  294.             i++;
  295.     }
  296. }
  297.  
  298. /*############################################################################*/
  299.  
  300. /* Handle the HTTP response */
  301. forward ShowCIDRRange(index, response_code, data[]);
  302. public ShowCIDRRange(index, response_code, data[])
  303. {
  304.     new
  305.         sBuffer[128];
  306.  
  307.     if(response_code == 200)
  308.     {
  309.         format(sBuffer, sizeof(sBuffer), "CIDR Range(s) : %s", data);
  310.         SendClientMessage(index, COLOR_GREEN, sBuffer);
  311.     }
  312. }
  313.  
  314. /*############################################################################*/
  315.  
  316. /* For the kick timer */
  317. forward KickTimer(playerid);
  318. public KickTimer(playerid)
  319. {
  320.     Kick(playerid);
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement