Skip to content

Commit 80f7f6b

Browse files
committed
add variable-appendix to only contain a single IP
1 parent c4a6645 commit 80f7f6b

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ cat /etc/nftables.d/addons/dns.nft
7878
}
7979
```
8080

81+
**Note**: If your variable ends in `_1` it will only contain **ONE** IP address! This can be useful if you need a DNAT target.
82+
8183

8284
2. The script is executed
8385

lib/util.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,18 @@
1313
from json import loads as json_loads
1414
from json import JSONDecodeError
1515

16-
IS_ROOT = getuid() == 0
17-
SUDO = '' if IS_ROOT else 'sudo '
18-
CMD_RELOAD = f'{SUDO}systemctl reload nftables.service' # has to be changed if no systemd is available
1916
CONFIG = '/etc/nftables.conf'
2017
BASE_DIR = '/etc/nftables.d'
2118
ADDON_DIR = '/etc/nftables.d/addons'
2219
CONFIG_EXT = 'nft'
2320
APPENDIX_4 = 'v4'
2421
APPENDIX_6 = 'v6'
2522

23+
IS_ROOT = getuid() == 0
24+
SUDO = '' if IS_ROOT else 'sudo '
25+
CMD_RELOAD = f'{SUDO}systemctl reload nftables.service' # has to be changed if no systemd is available
26+
VAR_SINGLE_END = '_1'
27+
2628
if not CONFIG_EXT.startswith('.'):
2729
CONFIG_EXT = f'.{CONFIG_EXT}'
2830

@@ -41,7 +43,7 @@ def ensure_list(data: (str, list)) -> list:
4143
return [data]
4244

4345

44-
def format_var(name: str, data: list, version: int, as_set: bool = True, fallback: str = None) -> str:
46+
def format_var(name: str, data: list, version: int, fallback: str = None) -> str:
4547
if version not in FALLBACK_VAR_VALUE:
4648
version = 4
4749

@@ -50,7 +52,10 @@ def format_var(name: str, data: list, version: int, as_set: bool = True, fallbac
5052
if append not in [None, ' ', '']:
5153
name = f'{name}_{append}'
5254

53-
if as_set or len(data) > 1:
55+
if name.endswith(VAR_SINGLE_END) and len(data) > 0:
56+
data = data[0]
57+
58+
if len(data) > 1:
5459
raw = f"define { name } = {{ %s }}"
5560

5661
else:

0 commit comments

Comments
 (0)