-
Notifications
You must be signed in to change notification settings - Fork 3
/
nginxConfigReader.cpp
executable file
·48 lines (45 loc) · 1.62 KB
/
nginxConfigReader.cpp
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
#include "nginxConfigReader.h"
void ConfigReader::nginxConfigReadFunc() {
std::ifstream ifs(logFile);
if (ifs.is_open())
{
ifs.seekg(-1,ios_base::end);
std::string line;
const char* strToFind_limreq = "limiting requests";
const char* strToFind_client = "client:";
while (true)
{
while (std::getline(ifs, line))
{
const char* result = nullptr;
char* cmdCheckResult = strMatch((char *) line.c_str(), (char *) strToFind_limreq);
if(cmdCheckResult != nullptr)
{
result = strMatch(cmdCheckResult, (char *) strToFind_client);
if(result != nullptr)
{
++result;
char buf[256];
memcpy(buf, result, 20);
*findChar(buf, ',') = '\0';
if(checkIpHashTable.TryToPush(buf))
banMgr->BanIp(buf);
}
}
}
if (!ifs.eof()) break;
ifs.clear();
usleep(100000);
}
}
}
ConfigReader::ConfigReader(const char *_logFile, int _banTime, const char* _chainName, uint bitCount)
: checkIpHashTable(bitCount), chainName(_chainName), logFile(_logFile), banTime(_banTime) {
banMgr = new BanMgr(this);
char* cmd = (char*)malloc(1024);
sprintf(cmd, Configuration::chainAddCmd, chainName.c_str(), chainName.c_str());
system(cmd);
free(cmd);
thread thr(&ConfigReader::nginxConfigReadFunc, this);
thr.detach();
}