-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbanMgr.cpp
executable file
·66 lines (56 loc) · 1.54 KB
/
banMgr.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Created by origin on 31.07.16.
//
#include "banMgr.h"
#include "nginxConfigReader.h"
#include <string>
#include <algorithm>
#include <smmintrin.h>
void BanMgr::banCmdWrapper(const char* ipBuf) {
char cmdBuf[1024];
sprintf(cmdBuf, Configuration::banCmd, owner->chainName.c_str(), ipBuf);
cout << cmdBuf << endl;
system(cmdBuf);
queueProtector.lock();
{
unbanQueue.push(unbanRecord(time(0) + owner->banTime, string(ipBuf)));
}
queueProtector.unlock();
}
void BanMgr::unbanCmdWrapper(string& ip) {
char cmdBuf[1024];
sprintf(cmdBuf, Configuration::unbanCmd, owner->chainName.c_str(), ip.c_str());
cout << cmdBuf << endl;
system(cmdBuf);
}
void BanMgr::UnbanThrFunc() {
while(true) {
time_t curr_time = time(0);
if(queueProtector.try_lock())
{
if(unbanQueue.empty())
goto ex;
{
unbanRecord& frontRecord = unbanQueue.front();
while(frontRecord.first < curr_time)
{
unbanCmdWrapper(frontRecord.second);
owner->checkIpHashTable.Remove(frontRecord.second.c_str());
unbanQueue.pop();
if(unbanQueue.empty())
goto ex;
}
}
ex:
queueProtector.unlock();
}
sleep(1);
}
}
void BanMgr::BanIp(const char* ipBuf) {
banCmdWrapper(ipBuf);
}
void BanMgr::StartUnbanThr() {
thread t(&BanMgr::UnbanThrFunc, this);
t.detach();
}