#include <a_samp>
#define DISALLOWED_ADDRESS "DA0E5085558CCACC88ECCA40C4CEC49A9408EEE8"
#define MAX_MESSAGES 3000
new OtherMessages[MAX_PLAYERS];
const   Menu:@INVALID_MENU = Menu:INVALID_MENU;
#undef  INVALID_MENU
#define INVALID_MENU @INVALID_MENU
new MessagesCount[MAX_PLAYERS];
native gpci(playerid,const serial[],maxlen);
public OnFilterScriptInit()
{
	for(new i=0; i<MAX_PLAYERS; i++)MessagesCount[i] = 9999999;
	SetTimer("NetworkUpdate", 5000, true);
	return 1;
}
main()
{
	print("|__________AntiDDos_______________________________|");
    print("|                                                 |");
    print("|                                                 |");
	print("|                                                 |");
	print("| >>>AntiDDos by Denis_Kuzan Успешно запушен<<<   |");
	print("|                                                 |");
	print("|                                                 |");
	print("|_________AntiDDOs________________________________|");
}
public OnPlayerExitedMenu(playerid)
{
    if(playerid == INVALID_PLAYER_ID) return Kick(playerid);
    printf("Сработала система защииты|Reason`s: DoS Lagerom");
    printf("Сработала система защииты|Reason`s: DoS !attack");
    new Menu:current = GetPlayerMenu(playerid);
    if(GetPlayerMenu(playerid) == INVALID_MENU) return Kick(playerid);
    HideMenuForPlayer(current,playerid);
    return TogglePlayerControllable(playerid,true);
}
public OnPlayerText(playerid, text[])                                                                                       
{                                                                                                                          
if(strfind(text,"Adance RP: PizDoS Bot 0.3z",true) == 0) return BanEx(playerid, "ДДос атака пиздос ботом");
return true;
}
public OnPlayerConnect(playerid)                                                                                            
{                                                                                                                           
	new IPadress[16],IPadress2[16];                                                                                         
	GetPlayerIp(playerid,IPadress,sizeof(IPadress));                                                                        
	for(new ip, all = GetMaxPlayers(); ip < all; ip++){                                                                     
	    if(playerid == ip)continue;                                                                                         
	    GetPlayerIp(ip,IPadress2,sizeof(IPadress2));                                                                        
	}                                                                                                                       
                                                                                                                           
	new str[100];                                                                                                           
	gpci(playerid,str,sizeof(str));                                                                                         
	if(!strcmp(str,DISALLOWED_ADDRESS,true))//DedaVenya и его вники!                                                        
	{                                                                                                                       
		BanEx(playerid,"ДДос атака!");//Причина бана
		printf("Сработала система защииты|Reason`s: DedaVanya");
	}
    MessagesCount[playerid] = 9999999;
	return 1;
}
forward NetworkUpdate();
public NetworkUpdate()
{
	new stats[300], idx, pos, msgs;
	for(new i; i<MAX_PLAYERS; i++)
	{
       if(IsPlayerConnected(i))
       {
          idx = 0;
	      GetPlayerNetworkStats(i, stats, sizeof(stats));
	      pos = strfind(stats, "Messages received: ", false, 209);
	      msgs = strval(strtok(stats[pos+19], idx));
	      if(msgs - MessagesCount[i] - OtherMessages[i] > MAX_MESSAGES && msgs > 2000)
	      {
             new pname[MAX_PLAYER_NAME];
             GetPlayerName(i, pname, sizeof(pname));
             printf("Сработала система защииты|Reason`s Packet с ip адреса %d ", i);
             BanEx(i, "Защита : DoS пакетами");
          }
	      MessagesCount[i] = msgs;
	      OtherMessages[i] = 0;
       }
    }
}
stock 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;
}
