Skip to content

Commit 873863f

Browse files
committed
Show custom 403 page when user ip/network is banned by netfilter
Signed-off-by: Kristian Feldsam <[email protected]>
1 parent 8d4ef14 commit 873863f

File tree

8 files changed

+460
-164
lines changed

8 files changed

+460
-164
lines changed

data/Dockerfiles/netfilter/modules/IPTables.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ def initChainIPv4(self):
1919
rule.target = target
2020
if rule not in chain.rules:
2121
chain.insert_rule(rule)
22+
23+
# always allow TCP connections to 80 and 443 ports to show 403 page in case of ban
24+
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), self.chain_name)
25+
rule = iptc.Rule()
26+
rule.create_target("ACCEPT")
27+
match = rule.create_match('multiport')
28+
rule.protocol = 'tcp'
29+
match.dports = '80,443'
30+
if rule not in chain.rules:
31+
chain.insert_rule(rule)
2232

2333
def initChainIPv6(self):
2434
if not iptc.Chain(iptc.Table6(iptc.Table6.FILTER), self.chain_name) in iptc.Table6(iptc.Table6.FILTER).chains:
@@ -32,6 +42,16 @@ def initChainIPv6(self):
3242
rule.target = target
3343
if rule not in chain.rules:
3444
chain.insert_rule(rule)
45+
46+
# always allow TCP connections to 80 and 443 ports to show 403 page in case of ban
47+
chain = iptc.Chain(iptc.Table6(iptc.Table6.FILTER), self.chain_name)
48+
rule = iptc.Rule6()
49+
rule.create_target("ACCEPT")
50+
match = rule.create_match('multiport')
51+
rule.protocol = 'tcp'
52+
match.dports = '80,443'
53+
if rule not in chain.rules:
54+
chain.insert_rule(rule)
3555

3656
def checkIPv4ChainOrder(self):
3757
filter_table = iptc.Table(iptc.Table.FILTER)
@@ -98,7 +118,7 @@ def banIPv4(self, source):
98118
rule.target = target
99119
if rule in chain.rules:
100120
return False
101-
chain.insert_rule(rule)
121+
chain.append_rule(rule)
102122
return True
103123

104124
def banIPv6(self, source):
@@ -109,7 +129,7 @@ def banIPv6(self, source):
109129
rule.target = target
110130
if rule in chain.rules:
111131
return False
112-
chain.insert_rule(rule)
132+
chain.append_rule(rule)
113133
return True
114134

115135
def unbanIPv4(self, source):

data/conf/nginx/fastcgi_params

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
fastcgi_param QUERY_STRING $query_string;
3+
fastcgi_param REQUEST_METHOD $request_method;
4+
fastcgi_param CONTENT_TYPE $content_type;
5+
fastcgi_param CONTENT_LENGTH $content_length;
6+
7+
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
8+
fastcgi_param REQUEST_URI $request_uri;
9+
fastcgi_param DOCUMENT_URI $document_uri;
10+
fastcgi_param DOCUMENT_ROOT $document_root;
11+
fastcgi_param SERVER_PROTOCOL $server_protocol;
12+
fastcgi_param REQUEST_SCHEME $scheme;
13+
fastcgi_param HTTPS $https if_not_empty;
14+
15+
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
16+
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
17+
18+
fastcgi_param REMOTE_ADDR $remote_addr;
19+
fastcgi_param REMOTE_PORT $remote_port;
20+
fastcgi_param SERVER_ADDR $server_addr;
21+
fastcgi_param SERVER_PORT $server_port;
22+
fastcgi_param SERVER_NAME $server_name;
23+
24+
# PHP only, required if PHP was built with --enable-force-cgi-redirect
25+
fastcgi_param REDIRECT_STATUS 200;

0 commit comments

Comments
 (0)