Código:
#pragma once
namespace Network
{
struct FloodCheck
{
bool Send;
bool Delete;
unsigned long Timer;
unsigned long Count;
std::string Address;
};
struct FloodStruct
{
FloodCheck Check;
std::string Address;
};
class Flood
{
public:
static bool Inits();
static bool Check(std::string szAddress);
static short stringExplode(std::string szAddress, std::string szExplode);
static short Connection(SOCKET szSocket, char* szAddress);
public:
static FloodStruct Struct[500];
public:
static unsigned long Count;
};
}
Código:
#include "Library.h"
namespace Network
{
FloodStruct Flood::Struct[500];
unsigned long Flood::Count;
bool Flood::Init()
{
for(int i = 0; i < 500; i++)
{
Struct[i].Address = "";
}
Count = 0;
std::fstream f(".\\Network\\Block\\IpAddress.txt", std::ios::in);
if(f.is_open() && f.good())
{
BYTE szNum = 0;
char szBuffer[1024];
std::string szLine;
while(!f.eof())
{
if(Count == 500)
{
MessageBox(NULL, "O limite de endereço IP de usuários foi excedido.", "Erro", MB_OK | MB_ICONERROR);
break;
}
memset(&szBuffer[0], 0x00, 1024);
f.getline(szBuffer, 1024);
szLine.assign(&szBuffer[0]);
if(szLine.length() > 9)
{
if(szLine.substr(0, 2) != "//" && szLine.substr(0, 1) != "#" && szLine.substr(0, 1) != ";")
{
szNum = (BYTE)Count;
for(int i = 0; i < (int)szLine.length(); i++)
{
if(szLine[i] != ' ' && szLine[i] != '\t' && szLine[i] != '"')
{
if(isdigit(szLine.c_str()[i]) || szLine[i] == '.')
{
Struct[szNum].Address += szLine[i];
}
else
{
f.close();
MessageBox(NULL, "O arquivo IpAddress.txt está configurado de forma inadequada.", "Erro", MB_OK | MB_ICONERROR);
return false;
}
}
}
Count++;
}
}
}
f.close();
return true;
}
return false;
}
short Flood::stringExplode(std::string szAddress, std::string szExplode)
{
BYTE szId = 0, szDec[4] = {0};
std::string szConvert = "";
for(int i = 0; i <= (int)szAddress.length(); i++)
{
if(!szAddress.compare(i, 1, szExplode) || i == szAddress.length())
{
szDec[szId] = (BYTE)atoi((char*)szConvert.c_str());
szConvert = "";
szId++;
}
else
{
szConvert += szAddress[i];
}
}
float szResult = (float)(((szDec[0] + szDec[1]) - szDec[2]) + (szDec[3] * 2)) / 2;
if((szResult > 400))
{
szResult /= 2;
}
return (short)szResult;
}
bool Flood::Check(std::string szAddress)
{
WORD szIndex = Function::stringExplode(szAddress, (std::string)".");
if(Count > 0)
{
for(int i = 0; i < (int)Count; i++)
{
if(Struct[i].Address.empty())
{
break;
}
if((boost::iequals(Struct[i].Address, szAddress)))
{
if(!Struct[szIndex].Check.Delete)
{
for(int i = 4800; i < 5800; i++)
{
if(Object[i].Connected <= 1)
{
if(boost::iequals(Object[i].IpAddress, szAddress.c_str()))
{
ServerDelete(i);
}
}
}
Struct[szIndex].Check.Delete++;
}
return false;
}
}
}
if(Struct[szIndex].Check.Timer <= 0)
{
Struct[szIndex].Check.Send = true;
Struct[szIndex].Check.Delete = false;
Struct[szIndex].Check.Address = szAddress;
Struct[szIndex].Check.Timer = GetTickCount();
Struct[szIndex].Check.Count = 0;
}
if(!Struct[szIndex].Check.Send)
{
if(boost::iequals(Struct[szIndex].Check.Address, szAddress))
{
return false;
}
}
if(Struct[szIndex].Check.Count >= 2)
{
DWORD dwTimer = (GetTickCount() - Struct[szIndex].Check.Timer) / 1000;
if(dwTimer <= 1)
{
Struct[szIndex].Check.Send = false;
Struct[szIndex].Check.Delete = false;
Struct[szIndex].Check.Address = szAddress;
bool szWriteProceed = false;
if(Count < 500)
{
if(Count == 499)
{
if(Struct[499].Address.empty())
{
Struct[499].Check.Address = szAddress;
szWriteProceed = true;
}
}
else
{
BYTE szNum = (BYTE)Count;
Struct[szNum].Address = szAddress;
szWriteProceed = true;
Count++;
}
}
if(szWriteProceed)
{
std::fstream f(".\\Network\\Block\\IpAddress.txt", std::ios::app | std::ios::out);
if(f.is_open() && f.good())
{
std::string Msg(255, ' ');
sprintf_s(&Msg[0], 255, "\"%s\"\n", &szAddress[0]);
f.write(Msg.c_str(), Msg.length());
f.close();
}
}
}
else
{
if(!boost::iequals(Struct[szIndex].Check.Address, szAddress))
{
Struct[szIndex].Check.Send = true;
Struct[szIndex].Check.Delete = false;
Struct[szIndex].Check.Address = szAddress;
}
Struct[szIndex].Check.Timer = GetTickCount();
Struct[szIndex].Check.Count = 0;
}
}
Struct[szIndex].Check.Count++;
return true;
}
short Flood::Connection(SOCKET szSocket, char* szAddress)
{
if(!Check((std::string)szAddress))
{
return (-1);
}
return ServerConnect(szSocket, szAddress);
}
}