-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.h
190 lines (184 loc) · 5.46 KB
/
filter.h
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "global.h"
#ifndef FILTER_H
#define FILTER_H
class filter
{
private:
bool notMatching;
struct rule
{
string ruleData;
int ruleOperatorType; //!= == > < >= <=
// 1 2 3 4 5 6
rule(string _ruleData, int _ruleOperatorType)
{
ruleData = _ruleData;
ruleOperatorType = _ruleOperatorType;
}
};
vector<rule> wanIpRule, lanIpRule, portRule;
map<string, int> StringToIntForruleOperator =
{
{"!=", 1},
{"==", 2},
{">", 3},
{"<", 4},
{">=", 5},
{"<=", 6}};
bool GetnotMatchingState()
{
return this->notMatching;
}
public:
enum ruleDataType
{
wanip = 1,
lanip = 2,
port = 3,
all = 4
};
const bool matching(string wanIp, string lanIp, string port)
{
if (notMatching)
{
return true;
}
bool wanIpMatch = false;
bool lanIpMatch = false;
bool portMatch = false;
if ((wanIpRule.size() == 0 && lanIpRule.size() == 0 && portRule.size() == 0)
|| (wanIp.empty() && lanIp.empty() && port.empty()))
{
return false;
}
// wanIp matching
if (wanIpRule.size() > 0 && !wanIp.empty())
for (int i = 1; i <= wanIpRule.size(); i++)
{
if (wanIp.find(wanIpRule[i].ruleData) != string::npos)
{
if (wanIpRule[i].ruleOperatorType == 2)
{
wanIpMatch = true;
break;
}
else
{
return false;
}
}
}
else if (wanIpRule.size() > 0 && wanIp.empty())
return false;
else
wanIpMatch = true;
// lanIp matching
if (lanIpRule.size() > 0 && !lanIp.empty())
for (int i = 1; i <= lanIpRule.size(); i++)
{
if (lanIp.find(lanIpRule[i - 1].ruleData) != string::npos)
{
if (lanIpRule[i].ruleOperatorType == 2)
{
lanIpMatch = true;
break;
}
else
{
return false;
}
}
}
else if (lanIpRule.size() > 0 && lanIp.empty())
return false;
else
lanIpMatch = true;
// port matching
if (portRule.size() > 0 && !portRule.empty())
for (int i = 1; i <= portRule.size(); i++)
{
int iPort = stoi(port);
int iPortRule = stoi(portRule[i - 1].ruleData);
switch (portRule[i - 1].ruleOperatorType)
{
case 1: //!=
if (port == portRule[i - 1].ruleData)
return false;
portMatch = true;
break;
case 2: //==
if (port == portRule[i - 1].ruleData)
portMatch = true;
else
return false;
break;
case 3: //>
if (iPort > iPortRule)
portMatch = true;
else
return false;
break;
case 4: //<
if (iPort < iPortRule)
portMatch = true;
else
return false;
case 5: //>=
if (iPort >= iPortRule)
portMatch = true;
else
return false;
case 6: //<=
if (iPort <= iPortRule)
portMatch = true;
else
return false;
}
}
else if (portRule.size() > 0 && portRule.empty())
return false;
else
portMatch = true;
return wanIpMatch && lanIpMatch && portMatch;
}
bool addRule(ruleDataType ruleDataType, string ruleOperatorType, string ruleData)
{
vector<rule> *ruleVector;
switch (ruleDataType)
{
case wanip:
if (ruleOperatorType == "!=" || ruleOperatorType == "==")
{
ruleVector = &wanIpRule;
break;
}
else
return false;
case lanip:
if (ruleOperatorType == "!=" || ruleOperatorType == "==")
{
ruleVector = &lanIpRule;
break;
}
else
return false;
case port:
if (ruleOperatorType == "!=" || ruleOperatorType == "==" || ruleOperatorType == ">" || ruleOperatorType == "<" || ruleOperatorType == ">=" || ruleOperatorType == "<=")
{
ruleVector = &portRule;
break;
}
else
return false;
case all:
notMatching = true;
return true;
default:
return false;
}
ruleVector->push_back(rule{ruleData, StringToIntForruleOperator[ruleOperatorType]});
ruleVector = NULL;
return true;
}
};
#endif