diff --git a/feeds/ucentral/spotfilter/files/spotfilter.init b/feeds/ucentral/spotfilter/files/spotfilter.init index 56e2e9155..f7bceac5d 100644 --- a/feeds/ucentral/spotfilter/files/spotfilter.init +++ b/feeds/ucentral/spotfilter/files/spotfilter.init @@ -7,7 +7,12 @@ USE_PROCD=1 PROG=/usr/sbin/spotfilter add_interface() { - ubus call spotfilter interface_add "$(cat /tmp/spotfilter-$1.json)" + local cfg="$1" + local enabled + + config_get_bool enabled "$cfg" "configure_spotfilter" 0 + [ $enabled -eq 0 ] || /usr/bin/captive generate "$cfg" > /tmp/spotfilter-$cfg.json + ubus call spotfilter interface_add "$(cat /tmp/spotfilter-$cfg.json)" } reload_service() { diff --git a/feeds/ucentral/uspot/files/usr/bin/captive b/feeds/ucentral/uspot/files/usr/bin/captive index 32c7491e4..04e7c1171 100755 --- a/feeds/ucentral/uspot/files/usr/bin/captive +++ b/feeds/ucentral/uspot/files/usr/bin/captive @@ -17,6 +17,68 @@ function interfaces() { return uniq(interfaces); } +function generate_spotfilter(name) { + function parse_bool(val) { + if (val == "1" || val == "on" || val == "true" || val == "yes") + return true; + else if (val == "0" || val == "off" || val == "false" || val == "no") + return false; + else + return null; + } + function fail(msg) { + warn(msg + '\n'); + exit(1); + } + + if (!parse_bool(uci.get('uspot', name, 'configure_spotfilter'))) + exit(0); + + let uspot = uci.get_all('uspot', name); + if (!uspot) + fail('Cannot load uspot config for "' + name); + + let device_macaddr = uci.get('network', uspot.interface, 'device'); + if (!device_macaddr) + fail('Cannot find target network interface'); + if (uspot.wl_hosts && type(uspot.wl_hosts) != "array") + fail('Expecting list for wl_hosts'); + if (uspot.wl_addrs && type(uspot.wl_addrs) != "array") + fail('Expecting list for wl_addrs'); + + let class = [ + { + index: 0, + device_macaddr, + fwmark: 1, + fwmark_mask: 127 + }, { + index: 1, + fwmark: 2, + fwmark_mask: 127 + } + ]; + + let whitelist = [{ + class: 1, + hosts: uspot.wl_hosts || [], + address: uspot.wl_addrs || [], + }]; + + let conf = { + name, + devices: type(uspot.ifname) == "array" ? uspot.ifname : [ uspot.ifname ], + config: { + default_class: 0, + default_dns_class: 1, + client_autoremove: !!parse_bool(uspot.client_autoremove), + class, + whitelist + } + }; + printf('%.J\n', conf); +} + switch(ARGV[0]) { case 'dump': for (let interface in interfaces()) { @@ -58,6 +120,9 @@ case 'debugoff': uci.commit(); restart(); break; +case 'generate': + generate_spotfilter(ARGV[1]); + break; default: break; }