-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathAntiFloodSystem.cpp
More file actions
147 lines (130 loc) · 2.84 KB
/
AntiFloodSystem.cpp
File metadata and controls
147 lines (130 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "stdafx.h"
#include "LogProc.h"
#include "User.h"
#include "LogToFile.h"
#include "..\include\readscript.h"
#include "AntiFloodSystem.h"
cAntiFlood AntiFlood;
void cAntiFlood::Init()
{
this->Enabled = GetPrivateProfileInt("Common", "SCFAntiFloodEnabled",0, BLISTCFGFILE) ;
this->IsAutoAdd = GetPrivateProfileInt("Common", "SCFAutoAddToBlackList",0, BLISTCFGFILE) ;
this->MaxIPConnection = GetPrivateProfileInt("Common", "SCFMaxIPConnection",20, BLISTCFGFILE) ;
this->AutoCloseFloodCon = GetPrivateProfileInt("Common", "SCFAutoCloseAllFloodConnection",0, BLISTCFGFILE) ;
this->ReadBlackList(BLISTFILE);
}
void cAntiFlood::BlackListClean()
{
for(int i=0;i<=MAXIPINBLACKLIST;i++)
{
memset(this->BlackListIP[i],0,sizeof(this->BlackListIP[i])-1);
}
}
void cAntiFlood::ReadBlackList(LPSTR filename)
{
this->BlackListClean();
int Token;
this->BLSize=0;
SMDFile = fopen(filename, "r");
if ( SMDFile == NULL )
{
MsgBox("Error reading file %s", filename);
exit(1);
}
while ( true )
{
Token = GetToken();
if ( Token == 2 )
{
break;
}
//if ( Token == 1 )
//{
if(this->AddToBlackList(TokenString) == false)
{
MsgBox("error-L3 : Max IP count in BlackList reached!");
exit(1);
}
//}
}
fclose(SMDFile);
LogAddC(3, "[BlackList] Loaded Successfull!");
}
int cAntiFlood::GetIPCount(char * IP)
{
int Count = 0;
for (int n=OBJ_STARTUSERINDEX;n<OBJMAX; n++)
{
if ( gObj[n].Connected >= PLAYER_CONNECTED)
{
if ( strcmp(&gObj[n].Ip_addr[0] , IP) == 0)
{
Count++;
}
}
}
return Count;
}
void cAntiFlood::AutoClose(char * IP)
{
for (int n=OBJ_STARTUSERINDEX;n<OBJMAX; n++)
{
if ( gObj[n].Connected >= PLAYER_CONNECTED)
{
if ( strcmp(&gObj[n].Ip_addr[0] , IP) == 0)
{
LogAddC(2,"[AutoClose] Index: %d",n);
CloseClient(n);
}
}
}
}
bool cAntiFlood::Check(char * IP)
{
if(this->Enabled == TRUE)
{
if(this->BL_IPCheck(IP) == false)
{
LogAddC(2,"[BlackList] Rejected IP %s",IP);
return false;
}
if(this->MaxIPConnection < this->GetIPCount(IP))
{
if(this->IsAutoAdd == 1)
{
if(this->AddToBlackList(IP) == true)
{
WriteTxt(BLISTFILE,IP);
LogAddC(2,"[BlackList] IP: %s Added to Black List - Flood Attempt: %d",IP,this->MaxIPConnection);
}else
{
LogAddC(2,"[BlackList][BLACKLIST FULL] IP: %s Fail Add to Black List - Flood Attempt: %d",IP,this->MaxIPConnection);
}
if(this->AutoCloseFloodCon == 1)
{
this->AutoClose(IP);
}
}
return false;
}
}
return true;
}
bool cAntiFlood::BL_IPCheck(char * IP)
{
for(int i=0;i<this->BLSize;i++)
if(this->BlackListIP[i][0] != 0)
if(!stricmp(IP,this->BlackListIP[i]))
return false;
return true;
}
bool cAntiFlood::AddToBlackList(LPSTR IP)
{
if(this->BLSize >= MAXIPINBLACKLIST)
{
return false;
}
strcpy(this->BlackListIP[this->BLSize],IP);
this->BLSize++;
return true;
}