mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-11-16 21:43:33 +08:00
815 lines
22 KiB
Plaintext
815 lines
22 KiB
Plaintext
'use strict';
|
|
|
|
import { popen, readfile, glob } from 'fs';
|
|
import { cursor } from 'uci';
|
|
import * as rtnl from 'rtnl';
|
|
|
|
const ubus = require('ubus').connect();
|
|
|
|
function get_str_raw(iface, property) {
|
|
return readfile(sprintf('/var/run/mwan3track/%s/%s', iface, property));
|
|
}
|
|
|
|
function get_str(iface, property) {
|
|
return rtrim(get_str_raw(iface, property), '\n');
|
|
}
|
|
|
|
function get_int(iface, property) {
|
|
return int(get_str(iface, property));
|
|
}
|
|
|
|
function get_uptime() {
|
|
return int(split(readfile('/proc/uptime'), '.', 2)[0]);
|
|
}
|
|
|
|
function get_x_time(uptime, iface, property) {
|
|
let t = get_int(iface, property);
|
|
if (t > 0) {
|
|
t = uptime - t;
|
|
}
|
|
return t;
|
|
}
|
|
|
|
function ucibool(val) {
|
|
switch (val) {
|
|
case 'yes':
|
|
case 'on':
|
|
case 'true':
|
|
case 'enabled':
|
|
return true;
|
|
default:
|
|
return !!int(val);
|
|
}
|
|
}
|
|
|
|
function get_mwan3track_status(iface, uci_track_ips, track_gateway, procd) {
|
|
if (length(uci_track_ips) == 0 && !track_gateway) {
|
|
return 'disabled';
|
|
}
|
|
if (procd?.[sprintf('track_%s', iface)]?.running) {
|
|
const started = get_str(iface, 'STARTED');
|
|
switch (started) {
|
|
case '0':
|
|
return 'paused';
|
|
case '1':
|
|
return 'active';
|
|
default:
|
|
return 'down';
|
|
}
|
|
}
|
|
return 'down';
|
|
}
|
|
|
|
const nft_connected_set = {
|
|
'4': 'mwan3_connected_v4',
|
|
'6': 'mwan3_connected_v6',
|
|
};
|
|
|
|
function parse_elem_val(v) {
|
|
if (type(v) == 'string')
|
|
return v;
|
|
if (v?.prefix)
|
|
return sprintf('%s/%d', v.prefix.addr, v.prefix.len);
|
|
if (v?.range)
|
|
return sprintf('%s-%s', v.range[0], v.range[1]);
|
|
return null;
|
|
}
|
|
|
|
// Allowlist for nft set elements written back to the kernel: IPv4 and IPv6
|
|
// addresses, CIDR prefixes and hyphen-written ranges only, which is exactly
|
|
// what the apply path emits. These values are interpolated into an nft add
|
|
// element command line built with popen, so anything carrying a character
|
|
// outside this class is dropped before it can break out of the element list.
|
|
|
|
function valid_nft_element(v) {
|
|
return match(v, /^[0-9A-Fa-f:.\/-]+$/) != null;
|
|
}
|
|
|
|
function count_nftset_elements(setname) {
|
|
if (!match(setname, /^[a-zA-Z0-9_-]+$/))
|
|
return 0;
|
|
const nft = popen(sprintf('nft -j list set inet mwan3 %s 2>/dev/null', setname), 'r');
|
|
if (!nft)
|
|
return 0;
|
|
const raw = nft.read('all');
|
|
nft.close();
|
|
if (!raw || length(raw) == 0)
|
|
return 0;
|
|
let data;
|
|
try {
|
|
data = json(raw);
|
|
} catch(e) {
|
|
return 0;
|
|
}
|
|
if (data?.nftables) {
|
|
for (let item in data.nftables) {
|
|
if (item?.set?.elem)
|
|
return length(item.set.elem);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
function get_nftset_members(setname) {
|
|
if (!match(setname, /^[a-zA-Z0-9_-]+$/))
|
|
return [];
|
|
const nft = popen(sprintf('nft -j list set inet mwan3 %s 2>/dev/null', setname), 'r');
|
|
if (!nft)
|
|
return [];
|
|
const raw = nft.read('all');
|
|
nft.close();
|
|
if (!raw || length(raw) == 0)
|
|
return [];
|
|
let data;
|
|
try {
|
|
data = json(raw);
|
|
} catch(e) {
|
|
return [];
|
|
}
|
|
const members = [];
|
|
if (data?.nftables) {
|
|
for (let item in data.nftables) {
|
|
if (item?.set?.elem) {
|
|
for (let elem in item.set.elem) {
|
|
let val;
|
|
if (type(elem) == 'object' && elem?.elem)
|
|
val = parse_elem_val(elem.elem?.val);
|
|
else
|
|
val = parse_elem_val(elem);
|
|
if (val != null)
|
|
push(members, val);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return members;
|
|
}
|
|
|
|
function get_nftset_elements(setname, max_count) {
|
|
if (!match(setname, /^[a-zA-Z0-9_-]+$/))
|
|
return { elements: [], truncated: false };
|
|
const nft = popen(sprintf('nft -j list set inet mwan3 %s 2>/dev/null', setname), 'r');
|
|
if (!nft)
|
|
return { elements: [], truncated: false };
|
|
const raw = nft.read('all');
|
|
nft.close();
|
|
if (!raw || length(raw) == 0)
|
|
return { elements: [], truncated: false };
|
|
let data;
|
|
try {
|
|
data = json(raw);
|
|
} catch(e) {
|
|
return { elements: [], truncated: false };
|
|
}
|
|
const elements = [];
|
|
let truncated = false;
|
|
let done = false;
|
|
if (data?.nftables) {
|
|
for (let item in data.nftables) {
|
|
if (done) break;
|
|
if (item?.set?.elem) {
|
|
for (let elem in item.set.elem) {
|
|
if (length(elements) >= max_count) {
|
|
truncated = true;
|
|
done = true;
|
|
break;
|
|
}
|
|
let val = null;
|
|
let packets = null;
|
|
let bytes = null;
|
|
if (type(elem) == 'object' && elem?.elem) {
|
|
const inner = elem.elem;
|
|
if (inner?.counter) {
|
|
packets = inner.counter.packets;
|
|
bytes = inner.counter.bytes;
|
|
}
|
|
val = parse_elem_val(inner?.val);
|
|
} else {
|
|
val = parse_elem_val(elem);
|
|
}
|
|
if (val != null) {
|
|
const entry = { value: val };
|
|
if (packets != null) {
|
|
entry.packets = packets;
|
|
entry.bytes = bytes;
|
|
}
|
|
push(elements, entry);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return { elements: elements, truncated: truncated };
|
|
}
|
|
|
|
function get_ip_rules() {
|
|
const rules = [];
|
|
for (let family in [2, 10]) {
|
|
const result = rtnl.request(rtnl.const.RTM_GETRULE, rtnl.const.NLM_F_DUMP,
|
|
{ family: family }) ?? [];
|
|
for (let r in result)
|
|
push(rules, r);
|
|
}
|
|
return rules;
|
|
}
|
|
|
|
function get_table_routes(table_id) {
|
|
const routes = [];
|
|
for (let family in [2, 10]) {
|
|
const result = rtnl.request(rtnl.const.RTM_GETROUTE, rtnl.const.NLM_F_DUMP,
|
|
{ family: family }) ?? [];
|
|
for (let r in result)
|
|
if (r.table == table_id)
|
|
push(routes, r);
|
|
}
|
|
return routes;
|
|
}
|
|
|
|
function nslookup_resolve(host, type) {
|
|
|
|
// Reject any host outside the hostname character class before it is
|
|
// interpolated into the /bin/sh -c command line below. This is the single
|
|
// chokepoint for both resolve_host and the nftset_resolve domain path,
|
|
// whose UCI-supplied domain values are otherwise unvalidated.
|
|
|
|
if (!match(host, /^[a-zA-Z0-9._-]+$/))
|
|
return [];
|
|
const p = popen(sprintf('/bin/busybox nslookup -type=%s %s 127.0.0.1 2>/dev/null', type, host), 'r');
|
|
if (!p) return [];
|
|
const raw = p.read('all');
|
|
p.close();
|
|
if (!raw || length(raw) == 0) return [];
|
|
const found = [];
|
|
for (let line in split(raw, '\n')) {
|
|
if (index(line, 'Address:') != 0) continue;
|
|
const rest = ltrim(substr(line, 8));
|
|
if (index(rest, '127.0.0.1') >= 0) continue;
|
|
if (length(rest) > 0) push(found, rest);
|
|
}
|
|
return found;
|
|
}
|
|
|
|
function routing_health() {
|
|
const uci = cursor();
|
|
const mmx_mask = int(uci.get('mwan3', 'globals', 'mmx_mask')) || 0x3F00;
|
|
let bitcnt = 0;
|
|
for (let m = mmx_mask; m; m >>= 1) bitcnt += m & 1;
|
|
const mmdefault = (1 << bitcnt) - 1;
|
|
const iface_max = mmdefault - 3;
|
|
|
|
let IIF_BASE = int(uci.get('mwan3', 'globals', 'iif_rule_base')) || 1000;
|
|
let FWMARK_BASE = int(uci.get('mwan3', 'globals', 'fwmark_rule_base')) || 2000;
|
|
let UNREACH_BASE = int(uci.get('mwan3', 'globals', 'unreachable_rule_base')) || 3000;
|
|
if ((IIF_BASE + iface_max) >= FWMARK_BASE ||
|
|
(FWMARK_BASE + iface_max + 1) >= UNREACH_BASE) {
|
|
IIF_BASE = 1000;
|
|
FWMARK_BASE = 2000;
|
|
UNREACH_BASE = 3000;
|
|
}
|
|
|
|
const SRC_BASE = UNREACH_BASE + iface_max + 1;
|
|
|
|
// Build ordered interface list with 1-based index (same ordering mwan3 uses)
|
|
|
|
const iface_order = [];
|
|
const iface_index = {};
|
|
const iface_family = {};
|
|
uci.foreach('mwan3', 'interface', intf => {
|
|
push(iface_order, intf['.name']);
|
|
iface_index[intf['.name']] = length(iface_order);
|
|
iface_family[intf['.name']] = intf['family'] || 'ipv4';
|
|
});
|
|
|
|
// Fetch all ip rules and index by priority for O(1) lookup
|
|
|
|
const all_rules = get_ip_rules();
|
|
const rule_by_priority = {};
|
|
const v4_rule_by_priority = {};
|
|
for (let rule in all_rules) {
|
|
if (rule?.priority != null) {
|
|
rule_by_priority[rule.priority] = rule;
|
|
if (rule.family == 2)
|
|
v4_rule_by_priority[rule.priority] = rule;
|
|
}
|
|
}
|
|
|
|
// Pre-build the set of priorities that correspond to current UCI interfaces
|
|
|
|
const valid_priorities = {};
|
|
for (let ifname in iface_order) {
|
|
const id = iface_index[ifname];
|
|
valid_priorities[IIF_BASE + id] = true;
|
|
valid_priorities[FWMARK_BASE + id] = true;
|
|
valid_priorities[UNREACH_BASE + id] = true;
|
|
valid_priorities[UNREACH_BASE + iface_max + 1 + id] = true;
|
|
}
|
|
valid_priorities[FWMARK_BASE + mmdefault - 2] = true;
|
|
valid_priorities[FWMARK_BASE + mmdefault - 1] = true;
|
|
|
|
// Per-interface health report
|
|
|
|
const result = {};
|
|
for (let ifname in iface_order) {
|
|
const id = iface_index[ifname];
|
|
const iif_pri = IIF_BASE + id;
|
|
const fwmark_pri = FWMARK_BASE + id;
|
|
const unreach_pri = UNREACH_BASE + id;
|
|
const iif_rule = rule_by_priority[iif_pri];
|
|
const fwmark_rule = rule_by_priority[fwmark_pri];
|
|
const unreach_rule = rule_by_priority[unreach_pri];
|
|
const table_routes = get_table_routes(id);
|
|
const default_routes = filter(table_routes, r =>
|
|
r?.dst == null || r?.dst == '0.0.0.0/0' || r?.dst == '::/0'
|
|
);
|
|
const status = get_str(ifname, 'STATUS') || 'unknown';
|
|
|
|
result[ifname] = {
|
|
index: id,
|
|
status: status,
|
|
iif_rule: {
|
|
present: iif_rule != null,
|
|
priority: iif_pri,
|
|
},
|
|
fwmark_rule: {
|
|
present: fwmark_rule != null,
|
|
priority: fwmark_pri,
|
|
},
|
|
unreach_rule: {
|
|
present: unreach_rule != null,
|
|
priority: unreach_pri,
|
|
},
|
|
table: {
|
|
id: id,
|
|
has_default: length(default_routes) > 0,
|
|
default_routes: map(default_routes, r => ({
|
|
dst: r.dst ?? 'default',
|
|
gateway: r.gateway,
|
|
dev: r.oif,
|
|
family: (r.family == 10) ? 'ipv6' : 'ipv4',
|
|
})),
|
|
},
|
|
};
|
|
|
|
// The source band is IPv4-only, so test presence against the IPv4
|
|
// rule index alone. A device holds one source rule per global IPv4
|
|
// address, all sharing this priority, so a populated slot means the
|
|
// band exists for this interface, not that every address is covered.
|
|
|
|
if (iface_family[ifname] == 'ipv4') {
|
|
result[ifname].src_rule = {
|
|
present: v4_rule_by_priority[SRC_BASE + id] != null,
|
|
priority: SRC_BASE + id,
|
|
};
|
|
}
|
|
}
|
|
|
|
// Stale rules: priorities in mwan3 ranges but not matching any current UCI interface
|
|
|
|
const stale = [];
|
|
for (let rule in all_rules) {
|
|
const p = rule?.priority;
|
|
if (p == null) continue;
|
|
|
|
// The iif and fwmark windows are mmdefault wide (the fwmark band
|
|
// carries the global blackhole and unreachable rules above
|
|
// iface_max). The unreachable band holds only ids 1..iface_max;
|
|
// the slot above it is unused and the source band follows at
|
|
// iface_max + 1 + id.
|
|
|
|
const in_iif_range = (p > IIF_BASE && p <= IIF_BASE + mmdefault);
|
|
const in_fwmark_range = (p > FWMARK_BASE && p <= FWMARK_BASE + mmdefault);
|
|
const in_unreach_range = (p > UNREACH_BASE && p <= UNREACH_BASE + iface_max);
|
|
const in_src_range = (p > UNREACH_BASE + iface_max + 1 && p <= UNREACH_BASE + 2 * iface_max + 1);
|
|
if ((in_iif_range || in_fwmark_range || in_unreach_range || in_src_range) && !valid_priorities[p])
|
|
push(stale, { priority: p });
|
|
}
|
|
|
|
const mwan3_active = length(glob('/var/run/mwan3track/*/STATUS') || []) > 0;
|
|
|
|
return {
|
|
interfaces: result,
|
|
stale_rules: stale,
|
|
mwan3_active: mwan3_active,
|
|
rule_bases: { iif: IIF_BASE, fwmark: FWMARK_BASE, unreach: UNREACH_BASE, src: SRC_BASE },
|
|
};
|
|
}
|
|
|
|
function get_connected_ips(version) {
|
|
const setname = nft_connected_set[version];
|
|
const nft = popen(sprintf('nft -j list set inet mwan3 %s 2>/dev/null', setname), 'r');
|
|
if (!nft) {
|
|
return [];
|
|
}
|
|
const raw = nft.read('all');
|
|
nft.close();
|
|
|
|
if (!raw || length(raw) == 0) {
|
|
return [];
|
|
}
|
|
|
|
let data;
|
|
try {
|
|
data = json(raw);
|
|
} catch(e) {
|
|
return [];
|
|
}
|
|
|
|
const ips = [];
|
|
// nft JSON output: { nftables: [ { set: { elem: [ ... ] } } ] }
|
|
if (data?.nftables) {
|
|
for (let item in data.nftables) {
|
|
if (item?.set?.elem) {
|
|
for (let elem in item.set.elem) {
|
|
if (type(elem) == 'string') {
|
|
push(ips, elem);
|
|
} else if (type(elem) == 'object') {
|
|
// Handle prefix notation: { prefix: { addr: "x.x.x.x", len: N } }
|
|
if (elem?.prefix) {
|
|
push(ips, sprintf('%s/%d', elem.prefix.addr, elem.prefix.len));
|
|
} else if (elem?.range) {
|
|
push(ips, sprintf('%s-%s', elem.range[0], elem.range[1]));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return ips;
|
|
}
|
|
|
|
function get_policies(version) {
|
|
const uci = cursor();
|
|
const family_filter = (version == '6') ? 'ipv6' : 'ipv4';
|
|
|
|
// Build interface family map: iface_name -> 'ipv4'/'ipv6'
|
|
|
|
const iface_family = {};
|
|
uci.foreach('mwan3', 'interface', intf => {
|
|
iface_family[intf['.name']] = intf['family'] || 'ipv4';
|
|
});
|
|
|
|
// Build member info map: member_name -> { interface, metric, weight }
|
|
|
|
const member_info = {};
|
|
uci.foreach('mwan3', 'member', m => {
|
|
member_info[m['.name']] = {
|
|
interface: m['interface'],
|
|
metric: int(m['metric'] || 1),
|
|
weight: int(m['weight'] || 1),
|
|
};
|
|
});
|
|
|
|
const policies = {};
|
|
|
|
uci.foreach('mwan3', 'policy', p => {
|
|
const pname = p['.name'];
|
|
let use_members = p['use_member'];
|
|
if (!use_members) return;
|
|
if (type(use_members) != 'array') use_members = [use_members];
|
|
|
|
// Collect members matching the requested address family
|
|
|
|
const by_metric = {};
|
|
for (let mname in use_members) {
|
|
const m = member_info[mname];
|
|
if (!m) continue;
|
|
if (iface_family[m.interface] != family_filter) continue;
|
|
if (!by_metric[m.metric]) by_metric[m.metric] = [];
|
|
push(by_metric[m.metric], {
|
|
interface: m.interface,
|
|
metric: m.metric,
|
|
weight: m.weight,
|
|
});
|
|
}
|
|
|
|
if (!length(keys(by_metric))) return;
|
|
|
|
// Find the lowest metric with at least one online member
|
|
|
|
const sorted_metrics = sort(keys(by_metric), (a, b) => int(a) - int(b));
|
|
let active_metric = null;
|
|
for (let metric in sorted_metrics) {
|
|
for (let m in by_metric[metric]) {
|
|
if (get_str(m.interface, 'STATUS') == 'online') {
|
|
active_metric = metric;
|
|
break;
|
|
}
|
|
}
|
|
if (active_metric != null) break;
|
|
}
|
|
|
|
// Sum weights of online members at the active metric for percent calculation
|
|
|
|
let total_weight = 0;
|
|
if (active_metric != null) {
|
|
for (let m in by_metric[active_metric]) {
|
|
if (get_str(m.interface, 'STATUS') == 'online')
|
|
total_weight += m.weight;
|
|
}
|
|
}
|
|
|
|
policies[pname] = [];
|
|
for (let metric in sorted_metrics) {
|
|
for (let m in by_metric[metric]) {
|
|
const iface_status = get_str(m.interface, 'STATUS') || 'offline';
|
|
const is_active = (metric == active_metric) && (iface_status == 'online');
|
|
const percent = (is_active && total_weight > 0) ? int(m.weight * 100 / total_weight) : 0;
|
|
push(policies[pname], {
|
|
'interface': m.interface,
|
|
'metric': m.metric,
|
|
'weight': m.weight,
|
|
'percent': percent,
|
|
'status': iface_status,
|
|
});
|
|
}
|
|
}
|
|
});
|
|
|
|
return policies;
|
|
}
|
|
|
|
function interfaces_status(request) {
|
|
const uci = cursor();
|
|
const procd = ubus.call('service', 'list', { 'name': 'mwan3' })?.mwan3?.instances;
|
|
const interfaces = {};
|
|
uci.foreach('mwan3', 'interface', intf => {
|
|
const ifname = intf['.name'];
|
|
if (request.args.interface != null && request.args.interface != ifname) {
|
|
return;
|
|
}
|
|
const netstatus = ubus.call(sprintf('network.interface.%s', ifname), 'status', {});
|
|
const uptime = get_uptime();
|
|
const uci_track_ips = intf['track_ip'] || [];
|
|
const track_gateway = ucibool(intf['track_gateway'] || '0');
|
|
const track_status = get_mwan3track_status(ifname, uci_track_ips, track_gateway, procd);
|
|
const track_ips = [];
|
|
const track_files = glob(sprintf('/var/run/mwan3track/%s/TRACK_*', ifname)) || [];
|
|
for (let f in track_files) {
|
|
const ip = substr(f, length(sprintf('/var/run/mwan3track/%s/TRACK_', ifname)));
|
|
if (ip == 'OUTPUT') continue;
|
|
push(track_ips, {
|
|
'ip': ip,
|
|
'status': get_str(ifname, sprintf('TRACK_%s', ip)) || 'unknown',
|
|
'latency': get_int(ifname, sprintf('LATENCY_%s', ip)),
|
|
'packetloss': get_int(ifname, sprintf('LOSS_%s', ip)),
|
|
});
|
|
}
|
|
|
|
// Derive check_quality from LATENCY file content rather than UCI.
|
|
// UCI may be stale if check_quality was changed without restarting mwan3track.
|
|
// mwan3track only writes LATENCY_* files when check_quality=1, so file
|
|
// presence with content is the authoritative indicator of actual state.
|
|
|
|
let check_quality = false;
|
|
for (let tip in track_ips) {
|
|
const lat = get_str_raw(ifname, sprintf('LATENCY_%s', tip.ip));
|
|
if (lat && length(rtrim(lat, '\n')) > 0) {
|
|
check_quality = true;
|
|
break;
|
|
}
|
|
}
|
|
interfaces[ifname] = {
|
|
'age': get_x_time(uptime, ifname, 'TIME'),
|
|
'online': get_x_time(uptime, ifname, 'ONLINE'),
|
|
'offline': get_x_time(uptime, ifname, 'OFFLINE'),
|
|
'uptime': netstatus?.uptime || 0,
|
|
'score': get_int(ifname, 'SCORE'),
|
|
'lost': get_int(ifname, 'LOST'),
|
|
'turn': get_int(ifname, 'TURN'),
|
|
'status': get_str(ifname, 'STATUS') || 'unknown',
|
|
'enabled': ucibool(intf['enabled']),
|
|
'running': track_status == 'active',
|
|
'tracking': track_status,
|
|
'up': netstatus?.up || false,
|
|
'check_quality': check_quality,
|
|
'track_ip': track_ips,
|
|
};
|
|
});
|
|
return interfaces;
|
|
}
|
|
|
|
const methods = {
|
|
nftset_info: {
|
|
args: {},
|
|
call: function(request) {
|
|
const nft = popen('nft -j list table inet mwan3 2>/dev/null', 'r');
|
|
if (!nft)
|
|
return { sets: {} };
|
|
const raw = nft.read('all');
|
|
nft.close();
|
|
if (!raw || length(raw) == 0)
|
|
return { sets: {} };
|
|
let data;
|
|
try {
|
|
data = json(raw);
|
|
} catch(e) {
|
|
return { sets: {} };
|
|
}
|
|
const sets = {};
|
|
if (data?.nftables) {
|
|
for (let item in data.nftables) {
|
|
if (item?.set) {
|
|
const s = item.set;
|
|
if (s.table == 'mwan3' && !match(s.name, /^mwan3_/)) {
|
|
let counters = false;
|
|
if (type(s.stmt) == 'array') {
|
|
for (let stmt in s.stmt) {
|
|
if (type(stmt) == 'object' && 'counter' in stmt) {
|
|
counters = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
sets[s.name] = { type: s.type, flags: s.flags || [], counters: counters, count: type(s.elem) == 'array' ? length(s.elem) : 0 };
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return { sets: sets };
|
|
}
|
|
},
|
|
nftset_members: {
|
|
args: { set: '' },
|
|
call: function(request) {
|
|
const setname = request.args.set;
|
|
if (!setname)
|
|
return { error: 'set name required', members: [] };
|
|
return { members: get_nftset_members(setname) };
|
|
}
|
|
},
|
|
nftset_elements: {
|
|
args: { set: '', max: 0 },
|
|
call: function(request) {
|
|
const setname = request.args.set;
|
|
if (!setname)
|
|
return { error: 'set name required', elements: [], truncated: false };
|
|
const max = (request.args.max > 0) ? int(request.args.max) : 200;
|
|
const result = get_nftset_elements(setname, max > 5000 ? 5000 : max);
|
|
result.set = setname;
|
|
return result;
|
|
}
|
|
},
|
|
resolve_host: {
|
|
args: { host: '', family: '' },
|
|
call: function(request) {
|
|
const host = request.args.host;
|
|
if (!host || !match(host, /^[a-zA-Z0-9._-]+$/))
|
|
return { error: 'invalid hostname', v4: [], v6: [] };
|
|
const family = request.args.family || '';
|
|
const v4 = (family !== 'ipv6') ? nslookup_resolve(host, 'A') : [];
|
|
const v6 = (family !== 'ipv4') ? nslookup_resolve(host, 'AAAA') : [];
|
|
return { v4: v4, v6: v6 };
|
|
}
|
|
},
|
|
routing_health: {
|
|
args: {},
|
|
call: function(request) {
|
|
return routing_health();
|
|
}
|
|
},
|
|
status: {
|
|
args: {
|
|
section: 'section',
|
|
interface: 'interface'
|
|
},
|
|
call: function (request) {
|
|
switch (request.args.section) {
|
|
case 'connected':
|
|
return {
|
|
'connected': {
|
|
'ipv4': get_connected_ips('4'),
|
|
'ipv6': get_connected_ips('6'),
|
|
},
|
|
};
|
|
case 'policies':
|
|
return {
|
|
'policies': {
|
|
'ipv4': get_policies('4'),
|
|
'ipv6': get_policies('6'),
|
|
},
|
|
};
|
|
case 'interfaces':
|
|
return {
|
|
'interfaces': interfaces_status(request),
|
|
};
|
|
default:
|
|
return {
|
|
'interfaces': interfaces_status(request),
|
|
'connected': {
|
|
'ipv4': get_connected_ips('4'),
|
|
'ipv6': get_connected_ips('6'),
|
|
},
|
|
'policies': {
|
|
'ipv4': get_policies('4'),
|
|
'ipv6': get_policies('6'),
|
|
},
|
|
};
|
|
}
|
|
}
|
|
},
|
|
nftset_flush: {
|
|
args: { set: '' },
|
|
call: function(request) {
|
|
const setname = request.args.set;
|
|
if (!setname || !match(setname, /^[a-zA-Z0-9_-]+$/))
|
|
return { error: 'invalid set name' };
|
|
const proc = popen(sprintf('nft flush set inet mwan3 %s 2>&1', setname), 'r');
|
|
if (!proc)
|
|
return { error: 'failed to run nft' };
|
|
const out = proc.read('all');
|
|
const rc = proc.close();
|
|
if (rc != 0)
|
|
return { error: rtrim(out, '\n') || 'nft flush failed' };
|
|
return {};
|
|
}
|
|
},
|
|
nftset_reload: {
|
|
args: { set: '' },
|
|
call: function(request) {
|
|
const setname = request.args.set;
|
|
if (!setname || !match(setname, /^[a-zA-Z0-9_-]+$/))
|
|
return { error: 'invalid set name' };
|
|
|
|
let proc = popen(sprintf('nft flush set inet mwan3 %s 2>&1', setname), 'r');
|
|
if (!proc) return { error: 'failed to run nft' };
|
|
let out = proc.read('all');
|
|
let rc = proc.close();
|
|
if (rc != 0) return { error: rtrim(out, '\n') || 'nft flush failed' };
|
|
|
|
const uci = cursor();
|
|
let cfg = null;
|
|
uci.foreach('mwan3', 'ipset', function(s) {
|
|
if (s.name == setname) {
|
|
if (s.enabled !== '0')
|
|
cfg = s;
|
|
return false;
|
|
}
|
|
});
|
|
if (!cfg) return {};
|
|
|
|
const elements = [];
|
|
let entries = cfg.entry || [];
|
|
if (type(entries) != 'array') entries = [entries];
|
|
for (let e in entries) {
|
|
const ev = ltrim(rtrim(e, ' \t\r\n'), ' \t\r\n');
|
|
if (length(ev) > 0 && valid_nft_element(ev)) push(elements, ev);
|
|
}
|
|
if (cfg.loadfile) {
|
|
const content = readfile(cfg.loadfile);
|
|
if (content) {
|
|
for (let line in split(content, '\n')) {
|
|
const ci = index(line, '#');
|
|
if (ci >= 0) line = substr(line, 0, ci);
|
|
line = ltrim(rtrim(line, ' \t\r\n'), ' \t\r\n');
|
|
if (length(line) > 0 && valid_nft_element(line)) push(elements, line);
|
|
}
|
|
}
|
|
}
|
|
if (length(elements) == 0) return {};
|
|
|
|
proc = popen(sprintf('nft add element inet mwan3 %s { %s } 2>&1',
|
|
setname, join(', ', elements)), 'r');
|
|
if (!proc) return { error: 'failed to run nft add element' };
|
|
out = proc.read('all');
|
|
rc = proc.close();
|
|
if (rc != 0) return { error: rtrim(out, '\n') || 'nft add element failed' };
|
|
return {};
|
|
}
|
|
},
|
|
nftset_resolve: {
|
|
args: { set: '' },
|
|
call: function(request) {
|
|
const setname = request.args.set;
|
|
if (!setname || !match(setname, /^[a-zA-Z0-9_-]+$/))
|
|
return { error: 'invalid set name' };
|
|
|
|
const uci = cursor();
|
|
let cfg = null;
|
|
uci.foreach('mwan3', 'ipset', function(s) {
|
|
if (s.name == setname) {
|
|
if (s.enabled !== '0')
|
|
cfg = s;
|
|
return false;
|
|
}
|
|
});
|
|
if (!cfg) return { error: 'set not found in config' };
|
|
|
|
let domains = cfg.domain || [];
|
|
if (type(domains) != 'array') domains = [domains];
|
|
if (length(domains) == 0) return {};
|
|
|
|
ubus.call('service', 'signal', { name: 'dnsmasq', signal: 1 });
|
|
|
|
const qtype = (cfg.family == 'ipv6') ? 'AAAA' : 'A';
|
|
let resolved = 0;
|
|
for (let domain in domains) {
|
|
const addrs = nslookup_resolve(domain, qtype);
|
|
if (length(addrs) > 0) resolved++;
|
|
}
|
|
return { resolved: resolved };
|
|
}
|
|
}
|
|
};
|
|
|
|
return { 'mwan3': methods };
|