wlan-ap-Telecominfraproject/feeds/ucentral/uspot/files/usr/bin/captive
Thibaut VARÈNE 243f42454f uspot/spotfilter: implement captive generate
For basic uspot setups, this commit implements a 'generate' verb to
uspot /usr/bin/captive that takes a config uspot section name, and
parses the following extra options to generate the relevant spotfilter
config:

  option generate_spotfilter (bool) # if unset/false, generate is a NOP
  option interface 'name' # config/network interface name to redirect to
  option client_autoremove (bool) # if set/true, sets client_autoremove
  list wl_hosts '*.example.com' # optional list of whitelist hostnames
  list wl_addrs '1.2.3.4' # optional list of whitelist IPs

"captive generate" is called in spotfilter.init to optionally (depending
on 'generate_spotfilter') create the required spotfilter-XXX.json before
starting spotfilter.

Signed-off-by: Thibaut VARÈNE <hacks@slashdirt.org>
2023-05-30 07:56:09 +02:00

129 lines
2.7 KiB
Plaintext
Executable File

#!/usr/bin/ucode
let ubus = require('ubus').connect();
let uci = require('uci').cursor();
function restart() {
system('/etc/init.d/spotfilter restart');
system('/etc/init.d/uhttpd restart');
}
function interfaces() {
let interfaces = [];
uci.foreach('uspot', 'uspot', (d) => {
if (!d[".anonymous"])
push(interfaces, d[".name"]);
});
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()) {
let clients = ubus.call('spotfilter', 'client_list', { interface });
printf('%.J\n', clients);
}
break;
case 'clients':
for (let interface in interfaces()) {
let clients = ubus.call('spotfilter', 'client_list', { interface});
let res = {};
let t = time();
for (let c, val in clients) {
res[c] = {
status: val.state ? 'Authenticated' : 'Garden',
idle: val.idle || 0,
time: val.data.connect ? t - val.data.connect : 0,
ip4addr: val.ip4addr || '',
ip6addr: val.ip6addr || '',
packets_ul: val.packets_ul || 0,
bytes_ul: val.bytes_ul || 0,
packets_dl: val.packets_dl || 0,
bytes_dl: val.bytes_dl || 0,
};
}
printf('%.J\n', res);
}
break;
case 'restart':
restart();
break;
case 'log':
system('logread -f | grep uspot:');
break;
case 'debugon':
case 'debugoff':
uci.set('uspot', 'def_captive', 'debug', 1);
uci.commit();
restart();
break;
case 'generate':
generate_spotfilter(ARGV[1]);
break;
default:
break;
}