Skip to content

Commit 1657c62

Browse files
doncatotomasz-c
authored andcommitted
Feature: Optionally apply blacklist for Forwards (#7)
1 parent 8a656ac commit 1657c62

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

nft-blackhole.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ BLOCK_POLICY: drop
1515
# Connections to blocked countries will still be possible.
1616
BLOCK_OUTPUT: off
1717

18+
# Block forwarded connections from blacklisted ips: 'on' or 'off', default: 'off'
19+
BLOCK_FORWARD: off
1820

1921
# Whitelist: IP or Network adresses
2022
WHITELIST:

nft-blackhole.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
BLACKLIST = config['BLACKLIST']
3232
COUNTRY_LIST = config['COUNTRY_LIST']
3333
BLOCK_OUTPUT = config['BLOCK_OUTPUT']
34+
BLOCK_FORWARD = config['BLOCK_FORWARD']
3435

3536

3637
# Correct incorrect YAML parsing of NO (Norway)
@@ -42,6 +43,17 @@
4243
SET_TEMPLATE = ('table inet blackhole {\n\tset ${set_name} {\n\t\ttype ${ip_ver}_addr\n'
4344
'\t\tflags interval\n\t\tauto-merge\n\t\telements = { ${ip_list} }\n\t}\n}').expandtabs()
4445

46+
FORWARD_TEMPLATE = ('\tchain forward {\n\t\ttype filter hook forward priority -1; policy accept;\n'
47+
'\t\tct state established,related accept\n'
48+
'\t\tip saddr @whitelist-v4 counter accept\n'
49+
'\t\tip6 saddr @whitelist-v6 counter accept\n'
50+
'\t\tip saddr @blacklist-v4 counter ${block_policy}\n'
51+
'\t\tip6 saddr @blacklist-v6 counter ${block_policy}\n'
52+
'\t\t${country_ex_ports_rule}'
53+
'\t\tip saddr @country-v4 counter ${country_policy}\n'
54+
'\t\tip6 saddr @country-v6 counter ${country_policy}\n'
55+
'\t\tcounter\n\t}').expandtabs()
56+
4557
OUTPUT_TEMPLATE = ('\tchain output {\n\t\ttype filter hook output priority -1; policy accept;\n'
4658
'\t\tip daddr @whitelist-v4 counter accept\n'
4759
'\t\tip6 daddr @whitelist-v6 counter accept\n'
@@ -79,6 +91,13 @@
7991
else:
8092
chain_output = ''
8193

94+
if BLOCK_FORWARD:
95+
chain_forward = Template(FORWARD_TEMPLATE).substitute(block_policy=block_policy,
96+
country_policy=country_policy,
97+
country_ex_ports_rule=country_ex_ports_rule)
98+
else:
99+
chain_forward = ''
100+
82101
# Setting urllib
83102
ctx = ssl.create_default_context()
84103
IGNORE_CERTIFICATE = False
@@ -106,7 +125,8 @@ def start():
106125
block_policy=block_policy,
107126
country_ex_ports_rule=country_ex_ports_rule,
108127
country_policy=country_policy,
109-
chain_output=chain_output)
128+
chain_output=chain_output,
129+
chain_forward=chain_forward)
110130

111131
run(['nft', '-f', '-'], input=nft_conf.encode(), check=True)
112132

nft-blackhole.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ table inet blackhole {
5050
}
5151

5252
${chain_output}
53+
54+
${chain_forward}
5355
}

0 commit comments

Comments
 (0)