Skip to content

Commit 3974272

Browse files
authored
Merge pull request #3 from Fabian123333/domain-filter-arrays
support lists of domains for filters
2 parents 97a1b17 + ec031dc commit 3974272

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Elevating security measures, our module seamlessly enforces a custom ban command
2323
Experience granular control over your filtering strategy with a diverse array of customizable parameters, empowering you to tailor defenses to your unique environment.
2424

2525
- `userAgent`: Filter requests based on user agents, a pivotal strategy to pinpoint potentially malicious clients.
26-
- `domain`: Employ domain-based filters to focus on specific sections of your application.
26+
- `domain`: Employ domain-based filters to focus on specific sections of your application. Supports lists or single domains.
2727
- `referer`: Evaluate the source of incoming requests with referer-based filters.
2828
- `request`: Scrutinize request content with contextual filtering.
2929
- `method`: Filter requests by their HTTP methods (GET, POST, etc.).

char_list.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
class CharList{
1414
public:
15+
int Count(){
16+
return count;
17+
}
18+
1519
void Add(char* c){
20+
count++;
1621
// if currently no entry
1722
if(content == NULL){
1823
content = (char*)malloc(strlen(c) + 1);
@@ -47,7 +52,7 @@ class CharList{
4752
return false;
4853
}
4954

50-
bool Contains(char* c){
55+
bool Contains(const char* c){
5156
if(content == NULL){
5257
return false;
5358
}
@@ -63,6 +68,7 @@ class CharList{
6368
private:
6469
CharList* nextEntry;
6570
char* content;
71+
int count = 0;
6672

6773
bool ContainsChar(const char* str, char c) {
6874
while (*str != '\0') {

config.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,22 @@ class Config{
132132
free(tmp);
133133
}catch(const std::exception& e){}
134134

135+
try{
136+
for (auto& el2 : el.value()["domain"].items())
137+
{
138+
std::string tmp = el2.value().get<std::string>();
139+
char* tmp2 = (char*)malloc(tmp.length() + 1);
140+
strcpy(tmp2, tmp.c_str());
141+
142+
newFilter.AddDomain(tmp2);
143+
free(tmp2);
144+
}
145+
}catch(const std::exception& e){}
146+
135147
try{
136148
char* tmp = (char*)malloc(el.value()["domain"].get<std::string>().length());
137149
strcpy(tmp, el.value()["domain"].get<std::string>().c_str());
138-
newFilter.SetDomain(tmp);
150+
newFilter.AddDomain(tmp);
139151
free(tmp);
140152
}catch(const std::exception& e){}
141153

config_example/anti-ddos.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"refeer": ""
2222
},
2323
{
24-
"domain": "localhost",
24+
"domain": [ "localhost", "example.com", "www.example.com" ],
2525
"score": 5
2626
},
2727
{

filter.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ class Filter{
1717
char* GetRefeer(){
1818
return refeer;
1919
}
20-
char* GetDomain(){
21-
return domain;
20+
CharList GetDomains(){
21+
return domains;
2222
}
2323
char* GetRequest(){
2424
return request;
2525
}
2626
char* GetMethod(){
2727
return method;
2828
}
29-
char* GetContent(){
29+
char* GetContent(){
3030
return content;
3131
}
3232
int GetStatusCode(){
@@ -54,9 +54,8 @@ class Filter{
5454
strcpy(userAgent, value);
5555
}
5656

57-
void SetDomain(char* value){
58-
domain = (char*)malloc(strlen(value) + 1);
59-
strcpy(domain, value);
57+
void AddDomain(char* value){
58+
domains.Add(value);
6059
}
6160

6261
void SetRefeer(char* value){
@@ -154,8 +153,8 @@ class Filter{
154153
free(m);
155154
}
156155

157-
if(GetDomain() != NULL){
158-
if(!CompareValues(r->hostname, GetDomain(), IsUseRegex())){
156+
if(GetDomains().Count() != 0){
157+
if(!GetDomains().Contains(r->hostname)){
159158
// std::cerr << "skipped filter with 'wrong' domain " << r->hostname << " domain would be " << GetDomain() << "\n";
160159
return 0;
161160
}
@@ -203,7 +202,6 @@ class Filter{
203202

204203
private:
205204
char* userAgent;
206-
char* domain;
207205
char* refeer;
208206
char* request;
209207
char* method;

0 commit comments

Comments
 (0)