Skip to content

Commit 3dd9f67

Browse files
Enable negation when parsing monthdays and weekdays
This change enables parsing of the "!" character for monthdays and weekdays paraemters of a firewall redirect object when present in UCI configuration. This change does not add support for negation in NetJSON config.
1 parent b9cf322 commit 3dd9f67

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

netjsonconfig/backends/openwrt/converters/firewall.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,31 @@ def __netjson_redirect(self, redirect):
205205
redirect["proto"] = ["tcp", "udp"]
206206
else:
207207
redirect["proto"] = [proto]
208+
209+
if "weekdays" in redirect:
210+
weekdays = redirect["weekdays"]
211+
if not isinstance(weekdays, list):
212+
weekdays = weekdays.split()
213+
# UCI allows the first entry to be "!" which means negate the remaining
214+
# entries
215+
if weekdays[0] == "!":
216+
all_days = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"]
217+
wd = set([x for x in weekdays[1:]])
218+
redirect["weekdays"] = list(set(all_days) - wd)
219+
# Sort the days for predictability when testing
220+
redirect["weekdays"].sort(key=lambda v: all_days.index(v))
221+
208222
if "monthdays" in redirect:
209-
redirect["monthdays"] = [int(x) for x in redirect["monthdays"]]
223+
monthdays = redirect["monthdays"]
224+
if not isinstance(monthdays, list):
225+
monthdays = monthdays.split()
226+
# UCI allows the first entry to be "!" which means negate the remaining
227+
# entries
228+
if monthdays[0] == "!":
229+
all_days = set(range(1, 32))
230+
md = set([int(x) for x in monthdays[1:]])
231+
redirect["monthdays"] = list(all_days - md)
232+
else:
233+
redirect["monthdays"] = [int(x) for x in monthdays]
210234

211235
return self.type_cast(redirect)

tests/openwrt/test_firewall.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,70 @@ def test_redirect_monthdays_validation_error_2(self):
484484
o = OpenWrt({"firewall": {"redirects": [{"monthdays": [0, 2, 8]}]}})
485485
with self.assertRaises(ValidationError):
486486
o.validate()
487+
488+
_redirect_3_uci = textwrap.dedent(
489+
"""\
490+
package firewall
491+
492+
config defaults 'defaults'
493+
494+
config redirect 'redirect_Adblock DNS, port 53'
495+
option name 'Adblock DNS, port 53'
496+
option src 'lan'
497+
option proto 'tcpudp'
498+
option src_dport '53'
499+
option dest_port '53'
500+
option target 'DNAT'
501+
option weekdays '! mon tue wed'
502+
option monthdays '! 1 2 3 4 5'
503+
"""
504+
)
505+
506+
_redirect_3_netjson = {
507+
"firewall": {
508+
"redirects": [
509+
{
510+
"name": "Adblock DNS, port 53",
511+
"src": "lan",
512+
"proto": ["tcp", "udp"],
513+
"src_dport": "53",
514+
"dest_port": "53",
515+
"target": "DNAT",
516+
"weekdays": ["sun", "thu", "fri", "sat"],
517+
"monthdays": [
518+
6,
519+
7,
520+
8,
521+
9,
522+
10,
523+
11,
524+
12,
525+
13,
526+
14,
527+
15,
528+
16,
529+
17,
530+
18,
531+
19,
532+
20,
533+
21,
534+
22,
535+
23,
536+
24,
537+
25,
538+
26,
539+
27,
540+
28,
541+
29,
542+
30,
543+
31,
544+
],
545+
}
546+
]
547+
}
548+
}
549+
550+
def test_parse_redirect_3(self):
551+
o = OpenWrt(native=self._redirect_3_uci)
552+
print(o.config)
553+
self.assertEqual(o.config, self._redirect_3_netjson)

0 commit comments

Comments
 (0)