forked from 5ku11Cru5h3r/ADDAPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.cpp
377 lines (301 loc) · 9.88 KB
/
config.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// std::map
#include <map>
// string
#include <string>
// std::ifstream
#include <fstream>
// nlohmann::json
#include "json.hpp"
#include "httpd.h" // Allgemeine Apache-Header-Datei
#include "http_config.h" // Konfigurations-Header-Datei
#include "http_core.h" // AP_CORE_MODULE_INDEX
#include "http_log.h" // Error-Log-Header-Datei
#include "http_protocol.h"// HTTP-Protokoll-Header-Datei
#include "http_request.h" // Request-Header-Datei
#include "ap_config.h" // Apache-Konfigurations-Header-Datei
#undef APLOG_MODULE_INDEX
#define APLOG_MODULE_INDEX AP_CORE_MODULE_INDEX
using json = nlohmann::json;
class Config{
public:
Config(){
filtersPreRequest = FilterList();
filtersPostRequest = FilterList();
filtersPostContent = FilterList();
whitelist = CharList();
}
// Config Hooks
static char* SetConfigLocalHook(cmd_parms *cmd, void *cfg, const char *arg){
configPathLocal = (char*)malloc(strlen(arg) + 1);
strcpy(configPathLocal, arg);
return NULL;
}
static char* SetConfigHook(cmd_parms *cmd, void *cfg, const char *arg){
configPath = (char*)malloc(strlen(arg) + 1);
strcpy(configPath, arg);
return NULL;
}
static char* SetRedisPath(cmd_parms *cmd, void *cfg, const char *arg){
redisUrl = (char*)malloc(strlen(arg) + 1);
strcpy(redisUrl, arg);
return NULL;
}
static char* SetBlockCommandHook(cmd_parms *cmd, void *cfg, const char *arg){
blockCommandFormat = (char*)malloc(strlen(arg) + 1);
strcpy(blockCommandFormat, arg);
return NULL;
}
static char* SetRedisConnectionType(cmd_parms *cmd, void *cfg, const char *arg){
if(strcmp(arg, "unix") == 0 || strcmp(arg, "UNIX") == 0 )
redisProtocol = AF_UNIX;
redisProtocol = AF_INET;
return NULL;
}
static char* SetRedisPort(cmd_parms *cmd, void *cfg, const char *arg){
redisPort = atoi(arg);
return NULL;
}
static char* SetRedisTimeout(cmd_parms *cmd, void *cfg, const char *arg){
redisTimeout = atoi(arg);
return NULL;
}
// Config File Parser
static void Parse(char* configPath){
std::ifstream f(configPath);
json data = json::parse(f);
try{
data.at("maxHits").get_to(maxHits);
}catch(const std::exception& e){}
try{
data.at("blockTime").get_to(blockTime);
}catch(const std::exception& e){}
try{
data.at("tickDown").get_to(tickDown);
}catch(const std::exception& e){}
defaults = new Filter();
try{
defaults->SetUseRegex(data["defaults"]["useRegex"]);
}catch(const std::exception& e){}
try{
defaults->SetScore(data["defaults"]["score"]);
}catch(const std::exception& e){}
try{
defaults->SetApplyForCDN(data["defaults"]["applyForCDN"]);
}catch(const std::exception& e){}
try{
defaults->SetApplyForBots(data["defaults"]["applyForBots"]);
}catch(const std::exception& e){}
try{
defaults->SetApplyForAssets(data["defaults"]["applyForAssets"]);
}catch(const std::exception& e){}
for (auto& el : data["defaults"]["whitelist"].items())
{
std::string ip_tmp = el.value().get<std::string>();
char* ip = (char*)malloc(ip_tmp.length() + 1);
strcpy(ip, ip_tmp.c_str());
whitelist.Add(ip);
free(ip);
}
for (auto& el : data["filters"].items())
{
Filter newFilter = Filter();
try{
char* tmp = (char*)malloc(el.value()["userAgent"].get<std::string>().length());
strcpy(tmp, el.value()["userAgent"].get<std::string>().c_str());
newFilter.SetUserAgent(tmp);
free(tmp);
}catch(const std::exception& e){}
try{
for (auto& el2 : el.value()["domain"].items())
{
std::string tmp = el2.value().get<std::string>();
char* tmp2 = (char*)malloc(tmp.length() + 1);
strcpy(tmp2, tmp.c_str());
newFilter.AddDomain(tmp2);
free(tmp2);
}
}catch(const std::exception& e){}
try{
char* tmp = (char*)malloc(el.value()["domain"].get<std::string>().length());
strcpy(tmp, el.value()["domain"].get<std::string>().c_str());
newFilter.AddDomain(tmp);
free(tmp);
}catch(const std::exception& e){}
try{
char* tmp = (char*)malloc(el.value()["refeer"].get<std::string>().length());
strcpy(tmp, el.value()["refeer"].get<std::string>().c_str());
newFilter.SetRefeer(tmp);
free(tmp);
}catch(const std::exception& e){}
try{
char* tmp = (char*)malloc(el.value()["content"].get<std::string>().length());
strcpy(tmp, el.value()["content"].get<std::string>().c_str());
newFilter.SetContent(tmp);
free(tmp);
}catch(const std::exception& e){}
try{
char* tmp = (char*)malloc(el.value()["request"].get<std::string>().length());
strcpy(tmp, el.value()["request"].get<std::string>().c_str());
newFilter.SetRequest(tmp);
free(tmp);
}catch(const std::exception& e){}
try{
char* tmp = (char*)malloc(el.value()["method"].get<std::string>().length());
strcpy(tmp, el.value()["method"].get<std::string>().c_str());
newFilter.SetMethod(tmp);
free(tmp);
}catch(const std::exception& e){}
try{
newFilter.SetStatusCode(el.value()["statusCode"]);
}catch(const std::exception& e){}
try{
newFilter.SetUseRegex(el.value()["useRegex"]);
}catch(const std::exception& e){}
try{
newFilter.SetScore(el.value()["score"]);
}catch(const std::exception& e){}
try{
newFilter.SetApplyForCDN(el.value()["applyForCDN"]);
}catch(const std::exception& e){}
try{
newFilter.SetApplyForBots(el.value()["applyForBots"]);
}catch(const std::exception& e){}
try{
newFilter.SetApplyForAssets(el.value()["applyForAssets"]);
}catch(const std::exception& e){}
if(newFilter.GetContent() != NULL){
// PostContent
if(newFilter.GetDomains().Count() != 0)
{
for(int i = 0; i < newFilter.GetDomains().Count(); i++){
domainFiltersPostContent[newFilter.GetDomains().Get(i)].Add(newFilter);
}
}
else
filtersPostContent.Add(newFilter);
}
else if(newFilter.GetStatusCode() != 0){
// PostProcessing
if(newFilter.GetDomains().Count() != 0)
{
for(int i = 0; i < newFilter.GetDomains().Count(); i++){
domainFiltersPostRequest[newFilter.GetDomains().Get(i)].Add(newFilter);
}
}
else
filtersPostRequest.Add(newFilter);
} else {
// PreProcessing
if(newFilter.GetDomains().Count() != 0)
{
for(int i = 0; i < newFilter.GetDomains().Count(); i++){
domainFiltersPreRequest[newFilter.GetDomains().Get(i)].Add(newFilter);
}
}
else
filtersPreRequest.Add(newFilter);
}
}
f.close();
}
static FilterList FiltersPreRequest(){
return filtersPreRequest;
}
static FilterList FiltersPostRequest(){
return filtersPostRequest;
}
static FilterList FiltersPostContent(){
return filtersPostContent;
}
static FilterList DomainFiltersPreRequest(const char* domain){
std::string str = domain;
return domainFiltersPreRequest[domain];
}
static FilterList DomainFiltersPostRequest(const char* domain){
std::string str = domain;
return domainFiltersPostRequest[domain];
}
static FilterList DomainFiltersPostContent(const char* domain){
std::string str = domain;
return domainFiltersPostContent[domain];
}
static int TickDown(){
return tickDown;
}
static int GetBlockTime(){
return blockTime;
}
static char* GetBlockCommandFormat(){
return blockCommandFormat;
}
static char* GetUnblockCommandFormat(){
return unblockCommandFormat;
}
static char* ConfigPath(){
return configPath;
}
static char* ConfigPathLocal(){
return configPathLocal;
}
static int MaxHits(){
return maxHits;
}
static char* RedisUrl(){
if(redisUrl == NULL){
redisUrl = (char*)malloc(strlen("127.0.0.1") + 1);
strcpy(redisUrl, "127.0.0.1");
}
return redisUrl;
}
static int RedisPort(){
return redisPort;
}
static int RedisProtocol(){
return redisProtocol;
}
static int RedisTimeout(){
return redisTimeout;
}
static CharList Whitelist(){
return whitelist;
}
private:
static int maxHits;
static int tickDown;
static int blockTime;
static FilterList filtersPreRequest;
static FilterList filtersPostRequest;
static FilterList filtersPostContent;
static std::map<std::string, FilterList> domainFiltersPreRequest;
static std::map<std::string, FilterList> domainFiltersPostRequest;
static std::map<std::string, FilterList> domainFiltersPostContent;
static Filter* defaults;
static CharList whitelist;
static char* blockCommandFormat;
static char* unblockCommandFormat;
static char* configPathLocal;
static char* configPath;
static char* redisUrl;
static int redisPort;
static int redisProtocol;
static int redisTimeout;
};
int Config::maxHits = 1000;
int Config::tickDown = 5;
int Config::blockTime = 60;
Filter* Config::defaults;
FilterList Config::filtersPreRequest;
FilterList Config::filtersPostRequest;
FilterList Config::filtersPostContent;
std::map<std::string, FilterList> Config::domainFiltersPreRequest;
std::map<std::string, FilterList> Config::domainFiltersPostRequest;
std::map<std::string, FilterList> Config::domainFiltersPostContent;
CharList Config::whitelist;
char* Config::blockCommandFormat;
char* Config::unblockCommandFormat;
char* Config::configPathLocal;
char* Config::configPath;
char* Config::redisUrl;
int Config::redisPort = 6379;
int Config::redisProtocol = AF_INET;
int Config::redisTimeout = 1500;