55# SPDX-License-Identifier: GPL-2.0-only
66#
77
8+ import os
89import subprocess
910
1011from euci import EUci
1112from jinja2 import Environment , BaseLoader
1213
1314CHAIN = """
1415chain dpi_blocks {
15- # filter is + 11 to be before the nfq hook at + 10
16- type filter hook forward priority filter + 11; policy accept;
16+ type filter hook prerouting priority filter + 10; policy accept;
1717
1818 # init is to allow kernel to set labels
1919 ct label set netify-init
2020 ct label netify-block counter {% if log_enabled %}log prefix "DPI block: " limit rate {{ log_limit }} {% endif %}drop
2121}
22+
2223"""
2324
2425
@@ -27,13 +28,16 @@ def generate_dpi():
2728 template = Environment (loader = BaseLoader ()).from_string (CHAIN )
2829 render = template .render (
2930 log_enabled = e_uci .get ('dpi' , 'config' , 'log_blocked' , dtype = bool , default = False ),
30- log_limit = e_uci .get ('firewall' , 'ns_defaults' , 'rule_log_limit' , dtype = str , default = '1/s ' )
31+ log_limit = e_uci .get ('firewall' , 'ns_defaults' , 'rule_log_limit' , dtype = str , default = '1/second ' )
3132 )
3233 # save to nftables directory table-pre, only if the file is changed
33- with open ('/usr/share/nftables.d/table-pre/dpi_blocks.nft' , 'r' ) as f :
34- current = f .read ()
34+ file_path = '/usr/share/nftables.d/table-pre/dpi_blocks.nft'
35+ current = None
36+ if os .path .exists (file_path ):
37+ with open (file_path , 'r' ) as f :
38+ current = f .read ()
3539 if current != render :
36- with open ('/usr/share/nftables.d/table-pre/dpi_blocks.nft' , 'w' ) as f :
40+ with open (file_path , 'w' ) as f :
3741 f .write (render )
3842 # reload nftables
3943 subprocess .run (['fw4' , 'reload' ], check = True )
0 commit comments