Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*** ***
- * CIDR Ban - Ban CIDR Ranges *
- *** ***
- * @Author Rafael 'R@f' Keramidas <rafael@keramid.as> *
- * @Date 12th June 2013 *
- * @Version 1.0 *
- * @Licence MIT License *
- * @Comment The Regex Plugin is required *
- * *
- * Copyright (C) 2013 Rafael Keramidas *
- * *
- * Permission is hereby granted, free of charge, to any *
- * person obtaining a copy of this software and *
- * associated documentation files (the "Software"), to *
- * deal in the Software without restriction, including *
- * without limitation the rights to use, copy, modify, *
- * merge, publish, distribute, sublicense, and/or sell *
- * copies of the Software, and to permit persons to whom *
- * the Software is furnished to do so, subject to the *
- * following conditions: *
- * *
- * The above copyright notice and this permission notice *
- * shall be included in all copies or substantial *
- * portions of the Software. *
- * *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF *
- * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT *
- * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS *
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO *
- * EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE *
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN *
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
- * USE OR OTHER DEALINGS IN THE SOFTWARE. *
- *** ***/
- #include <a_samp>
- #include <a_http>
- #include <regex>
- #include <cidr>
- #define BANLIST "cidr-banlist.txt"
- #define KickPlayer(%0) SetTimerEx("KickTimer", 600, 0, "i", %0)
- #define COLOR_RED 0xAA3333AA
- #define COLOR_YELLOW 0xFFFF00AA
- #define COLOR_GREEN 0x33AA33AA
- /*############################################################################*/
- public OnFilterScriptInit()
- {
- print("\n######################################");
- print("# #");
- print("# CIDR Ban By Rafael 'R@f' Keramidas #");
- print("# #");
- print("######################################\n");
- return true;
- }
- /*############################################################################*/
- public OnPlayerConnect(playerid)
- {
- new
- sPlayerName[MAX_PLAYER_NAME],
- sPlayerIP[20];
- GetPlayerName(playerid, sPlayerName, sizeof(sPlayerName));
- GetPlayerIp(playerid, sPlayerIP, sizeof(sPlayerIP));
- /* Check if the player is range banned */
- if(IsRangeBanned(sPlayerIP))
- {
- SendClientMessage(playerid, COLOR_RED, "Your IP is range banned from this server.");
- printf("%s (%s) is range banned from the server.", sPlayerName, sPlayerIP);
- KickPlayer(playerid);
- }
- return true;
- }
- /*############################################################################*/
- public OnPlayerCommandText(playerid, cmdtext[])
- {
- new
- sMsg[128],
- sCmd[128],
- sCIDRRange[32],
- sStartIP[32],
- sEndIP[32],
- iIndex;
- sCmd = strtok(cmdtext, iIndex);
- /**
- * /bancidr - Ban a CIDR range.
- *
- * @description Will add the CIDR range to the range ban file and kick all
- * current players that are in this range.
- * @param 1 CIDR range (for example: 192.168.1.0/24).
- **/
- if(strcmp(sCmd, "/bancidr", true) == 0)
- {
- /* Check if the player is admin */
- if(!IsPlayerAdmin(playerid))
- return SendClientMessage(playerid, COLOR_RED, "ERROR: You must be admin to use this command !");
- sCIDRRange = strtok(cmdtext, iIndex);
- /* Check if the CIDR parameter is empty */
- if(!strlen(sCIDRRange))
- return SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /bancidr [CIDR RANGE] - EXAMPLE: /bancidr 192.168.1.0/24");
- /* Check if the CIDR range is valid */
- if(!IsValidCIDR(sCIDRRange))
- return SendClientMessage(playerid, COLOR_RED, "ERROR: Invalid CIDR !");
- /* Show a message to the admin */
- format(sMsg, sizeof(sMsg), "The range %s has been banned successfully.", sCIDRRange);
- SendClientMessage(playerid, COLOR_GREEN, sMsg);
- /* Ban the range and kick the players in this range from the server */
- BanCIDRRange(sCIDRRange);
- BanCurrentPlayersInRange(sCIDRRange);
- return true;
- }
- /**
- * /getcidr - Get a CIDR range from two IPs.
- *
- * @description Get the CIDR range by giving two IPs (start and ending IP).
- * @param 1 Starting IP
- * @param 2 Ending IP
- **/
- if(strcmp(sCmd, "/getcidr", true) == 0)
- {
- if(!IsPlayerAdmin(playerid))
- return SendClientMessage(playerid, COLOR_RED, "ERROR: You must be admin to use this command !");
- sStartIP = strtok(cmdtext, iIndex);
- sEndIP = strtok(cmdtext, iIndex);
- /* Check if one of the IP parameters is empty */
- if(!strlen(sStartIP) || !strlen(sEndIP))
- return SendClientMessage(playerid, COLOR_YELLOW, "USAGE: /getcidr [START IP] [END IP] - EXAMPLE: /getcidr 192.168.1.0 192.168.1.42");
- /* Check if the IP range is valid */
- if(!IsValidIP(sStartIP) || !IsValidIP(sEndIP))
- return SendClientMessage(playerid, COLOR_RED, "ERROR: Invalid IP!");
- /* Send a HTTP request to get the CIDR ranges */
- format(sMsg, sizeof(sMsg), "rafael.keramid.as/samp/cidrcalc.php?startip=%s&endip=%s", sStartIP, sEndIP);
- HTTP(playerid, HTTP_GET, sMsg, "", "ShowCIDRRange");
- return true;
- }
- return false;
- }
- /*############################################################################*/
- /* Strtok - Not by me. */
- 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;
- }
- /*############################################################################*/
- /* CIDR validation regexp */
- stock IsValidCIDR(const sCIDR[])
- {
- static
- RegEx:rCIDR;
- if(!rCIDR)
- 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]))");
- return regex_match_exid(sCIDR, rCIDR);
- }
- /*############################################################################*/
- /* IP validation regexp */
- stock IsValidIP(const sIP[])
- {
- static
- RegEx:rIP;
- if(!rIP)
- 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]?)");
- return regex_match_exid(sIP, rIP);
- }
- /*############################################################################*/
- /* Check if an IP is in the CIDR ban file */
- stock IsRangeBanned(sClientIP[]) {
- new
- bool:bBanned = false,
- sBanCIDR[24],
- File:fBanList = fopen(BANLIST, io_readwrite);
- while(fread(fBanList, sBanCIDR))
- {
- RemoveNewLine(sBanCIDR);
- if(IsValidCIDR(sBanCIDR))
- if(cidr_match(sClientIP, sBanCIDR))
- bBanned = true;
- }
- fclose(fBanList);
- return bBanned;
- }
- /*############################################################################*/
- /* Add a CIDR range to the ban file */
- stock BanCIDRRange(sCIDR[])
- {
- new
- sBanCIDR[24],
- File:fBanList = fopen(BANLIST, io_append);
- if(fBanList)
- {
- format(sBanCIDR, sizeof(sBanCIDR), "%s\r\n", sCIDR);
- fwrite(fBanList, sBanCIDR);
- fclose(fBanList);
- }
- }
- /*############################################################################*/
- /* Kick all current players in the CIDR range from the server */
- stock BanCurrentPlayersInRange(sCIDR[])
- {
- new
- sPlayerIP[20];
- for(new i = 0; i < MAX_PLAYERS; i++)
- {
- if(IsPlayerConnected(i))
- {
- GetPlayerIp(i, sPlayerIP, sizeof(sPlayerIP));
- if(cidr_match(sPlayerIP, sCIDR))
- {
- SendClientMessage(i, COLOR_RED, "You have been range banned from this server.");
- KickPlayer(i);
- }
- }
- }
- }
- /*############################################################################*/
- /* Returns an array with multiple CIDR ranges that matches the two parameters */
- stock RangeToCIDR(sStartIP[], sEndIP[])
- {
- new
- iStartIP = 0;
- }
- /*############################################################################*/
- /* Remove all new line chars (\n and \r) */
- stock RemoveNewLine(sString[])
- {
- new
- i = 0;
- while(sString[i] != 0)
- {
- if(sString[i] == '\n' || sString[i] == '\r')
- sString[i] = 0;
- else
- i++;
- }
- }
- /*############################################################################*/
- /* Handle the HTTP response */
- forward ShowCIDRRange(index, response_code, data[]);
- public ShowCIDRRange(index, response_code, data[])
- {
- new
- sBuffer[128];
- if(response_code == 200)
- {
- format(sBuffer, sizeof(sBuffer), "CIDR Range(s) : %s", data);
- SendClientMessage(index, COLOR_GREEN, sBuffer);
- }
- }
- /*############################################################################*/
- /* For the kick timer */
- forward KickTimer(playerid);
- public KickTimer(playerid)
- {
- Kick(playerid);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement