mirror of
https://github.com/kiddin9/op-packages.git
synced 2026-11-16 21:43:33 +08:00
🤞 Sync 2026-10-06 13:52:46
This commit is contained in:
1 parent
4d2d69bed9
commit
947eaea2ee
42 files changed
+2681
-1582
No files matched your search
@@ -75,6 +75,16 @@ function parse_elem_val(v) {
|
||||
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;
|
||||
@@ -216,6 +226,14 @@ function get_table_routes(table_id) {
|
||||
}
|
||||
|
||||
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');
|
||||
@@ -233,7 +251,6 @@ function nslookup_resolve(host, type) {
|
||||
|
||||
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;
|
||||
@@ -250,34 +267,47 @@ function routing_health() {
|
||||
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)
|
||||
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];
|
||||
@@ -319,17 +349,38 @@ function routing_health() {
|
||||
})),
|
||||
},
|
||||
};
|
||||
|
||||
// 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 + mmdefault);
|
||||
if ((in_iif_range || in_fwmark_range || in_unreach_range) && !valid_priorities[p])
|
||||
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 });
|
||||
}
|
||||
|
||||
@@ -339,7 +390,7 @@ function routing_health() {
|
||||
interfaces: result,
|
||||
stale_rules: stale,
|
||||
mwan3_active: mwan3_active,
|
||||
rule_bases: { iif: IIF_BASE, fwmark: FWMARK_BASE, unreach: UNREACH_BASE },
|
||||
rule_bases: { iif: IIF_BASE, fwmark: FWMARK_BASE, unreach: UNREACH_BASE, src: SRC_BASE },
|
||||
};
|
||||
}
|
||||
|
||||
@@ -391,12 +442,14 @@ function get_policies(version) {
|
||||
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']] = {
|
||||
@@ -415,6 +468,7 @@ function get_policies(version) {
|
||||
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];
|
||||
@@ -431,6 +485,7 @@ function get_policies(version) {
|
||||
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) {
|
||||
@@ -444,6 +499,7 @@ function get_policies(version) {
|
||||
}
|
||||
|
||||
// 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]) {
|
||||
@@ -498,10 +554,12 @@ function interfaces_status(request) {
|
||||
'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));
|
||||
@@ -694,7 +752,7 @@ const methods = {
|
||||
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) push(elements, ev);
|
||||
if (length(ev) > 0 && valid_nft_element(ev)) push(elements, ev);
|
||||
}
|
||||
if (cfg.loadfile) {
|
||||
const content = readfile(cfg.loadfile);
|
||||
@@ -703,7 +761,7 @@ const methods = {
|
||||
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) push(elements, line);
|
||||
if (length(line) > 0 && valid_nft_element(line)) push(elements, line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
'use strict';
|
||||
|
||||
// common.uc: the shared mwan3 ucode library, the ucode analogue of common.sh.
|
||||
// Each shared ucode routine is defined here so it lives, and is tested, in one
|
||||
// place.
|
||||
|
||||
import { openlog, syslog, LOG_PID, LOG_DAEMON } from 'log';
|
||||
|
||||
// ---- Logging ---------------------------------------------------------------
|
||||
//
|
||||
// All mwan3 ucode logs through these functions so the sink, tag format and
|
||||
// verbose gate live in one place. Output goes to syslog, the same destination
|
||||
// logger(1) reaches, so it appears in logread regardless of how procd handles
|
||||
// the process stderr.
|
||||
|
||||
let verbose = false;
|
||||
|
||||
// Map the level names mwan3 uses (the shell's warn/error vocabulary and the
|
||||
// canonical syslog names) onto the exact tokens parse_priority() accepts. It
|
||||
// rejects "warn" and "error", so an unmapped name would be dropped; an unknown
|
||||
// level falls back to notice so a mislabelled message still logs.
|
||||
|
||||
const PRIO = {
|
||||
emerg: "emerg", alert: "alert", crit: "crit",
|
||||
err: "err", error: "err",
|
||||
warn: "warning", warning: "warning",
|
||||
notice: "notice", info: "info", debug: "debug",
|
||||
};
|
||||
|
||||
// Open the syslog connection with the given identity. LOG_PID appends the pid
|
||||
// so the tag reads ident[pid], matching the shell's "${SCRIPTNAME}[$$]"
|
||||
// convention. Facility daemon suits these long-running and service-invoked
|
||||
// components.
|
||||
|
||||
function log_open(ident) {
|
||||
openlog(ident, LOG_PID, LOG_DAEMON);
|
||||
}
|
||||
|
||||
// Set the verbose gate. The caller reads mwan3.globals.verbose_logging and
|
||||
// pushes the parsed bool here; debug is suppressed unless verbose, exactly as
|
||||
// common.sh:LOG() gates the debug facility behind MWAN3_VERBOSE_LOGGING.
|
||||
|
||||
function log_verbose(enable) {
|
||||
verbose = enable;
|
||||
}
|
||||
|
||||
// Emit one message. debug is dropped unless verbose; every other level always
|
||||
// logs. Message content is passed as a %s argument so it is never treated as a
|
||||
// format string.
|
||||
|
||||
function log_msg(level, msg) {
|
||||
if (level == "debug" && !verbose)
|
||||
return;
|
||||
syslog(PRIO[level] ?? "notice", "%s", msg);
|
||||
}
|
||||
|
||||
// ---- Config helpers --------------------------------------------------------
|
||||
|
||||
// Parse a UCI bool the way the shell's config_get_bool does: 1/on/true/yes/
|
||||
// enabled are true, everything else is false. Kept here so every ucode
|
||||
// consumer interprets a UCI bool identically to the shell. It reads no config
|
||||
// itself, so it does not couple the module to uci.
|
||||
|
||||
function ucibool(val) {
|
||||
switch (val) {
|
||||
case 'yes':
|
||||
case 'on':
|
||||
case 'true':
|
||||
case 'enabled':
|
||||
return true;
|
||||
default:
|
||||
return !!int(val);
|
||||
}
|
||||
}
|
||||
|
||||
// ---- Route classification --------------------------------------------------
|
||||
|
||||
// The width classifier for the bypass sweeps: a route is default-equivalent
|
||||
// when its destination is absent (a literal default) or its prefix is wide
|
||||
// enough that it can only exist as a component of an in-substance default
|
||||
// route - /2 or wider for IPv4, /3 or wider for IPv6, which covers the
|
||||
// split-half pairs VPN clients install and the 2000::/3 global-unicast
|
||||
// aggregate. Each route is judged alone, so a half-installed pair is still
|
||||
// excluded. The family comes from iptoarr, the inet_pton wrapper, so no
|
||||
// address text is hand-parsed; a destination it cannot parse is not
|
||||
// default-equivalent. The strict literal-default predicate the mirroring and
|
||||
// route-state paths use is deliberately not widened and lives on beside this.
|
||||
|
||||
function is_default_equivalent(route) {
|
||||
let dst = route.dst;
|
||||
if (dst == null)
|
||||
return true;
|
||||
let slash = index(dst, "/");
|
||||
if (slash < 0)
|
||||
return false;
|
||||
let a = iptoarr(substr(dst, 0, slash));
|
||||
if (a == null)
|
||||
return false;
|
||||
return +substr(dst, slash + 1) <= (length(a) == 16 ? 3 : 2);
|
||||
}
|
||||
|
||||
// ---- Exports ---------------------------------------------------------------
|
||||
|
||||
// The module's public interface. Declared as an export list rather than inline
|
||||
// on each function, which this ucode build does not accept.
|
||||
|
||||
export { log_open, log_verbose, log_msg, ucibool, is_default_equivalent };
|
||||
Reference in new issue
Block a user