The Profiles page was a stub for the actual catalog §7 feature (WAN-mode / failover conditional overrides); this implements it. A `config profile` defines conditions (active default-route interface, a connectivity probe up/down, a time window — all AND'd) and overrides (force-enable/disable named rules, override the default catch-all target/egress). The highest-priority profile whose conditions all hold is active; its overrides are applied at gen/reconcile time and the cron re-applies when the active profile flips. - model.go/uci.go: Profile struct + `config profile` parse. - profile_wanmode.go: defaultIface() (ip route), profileProbeUp() (HTTP), condition eval, activeWanProfile() (priority + AND conditions; no-condition profile never auto-activates), ProfileStatusJSON. strSet helper. - generate.go: builder.activeProfile set in buildConfigInternal; buildRoutingRules applies enable/disable-by-name + injects a default-target catch-all when active. - schedule.go: scheduleSignature includes the active profile name, so shater-cron reconciles on a profile transition (iface/probe/time boundary). - main.go: `xrayctl wanmode`. shater.uc `wanmode` ubus + acl. - profiles.js: rewritten to host WAN-mode Profiles (config profile GridSection) + active-profile banner + Config snapshots + Backup/Restore + xray compatibility. - tests: profile_wanmode_test.go (iface/schedule/AND conditions, priority selection, no-condition never active, override enable/disable, default-target injection, inactive-no-override). Verified live on the 512M VM: `wanmode` reports active sim-mode (iface eth1); force-enabled a disabled rule (profileonly.example appears); flipping the iface condition deactivates it; schedule condition active at 22:30 / inactive at 12:00; xray -test OK. Playwright: Profiles page renders the WAN-mode form + active banner + snapshots + backup + compat with 0 console errors. r16. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
333 lines
9.1 KiB
Ucode
333 lines
9.1 KiB
Ucode
#!/usr/bin/env ucode
|
|
// rpcd ucode backend for luci-app-shater. Registers ubus object "xray".
|
|
// Install at /usr/share/rpcd/ucode/shater.uc. After changes: /etc/init.d/rpcd restart
|
|
// Verify: ubus list ; ubus call xray status
|
|
// Thin wrappers around /usr/bin/xrayctl; each returns parsed JSON (read methods) or
|
|
// an { ok, code } result (action methods). Methods must be granted in acl.d.
|
|
'use strict';
|
|
|
|
import { popen } from 'fs';
|
|
|
|
const XRAYCTL = '/usr/bin/xrayctl';
|
|
|
|
// Run a command, return its stdout as a string (empty on failure).
|
|
function run(cmd) {
|
|
let out = '';
|
|
let fh = popen(cmd);
|
|
if (fh) {
|
|
let r = fh.read('all');
|
|
if (r != null)
|
|
out = r;
|
|
fh.close();
|
|
}
|
|
return out;
|
|
}
|
|
|
|
// Run a command expected to emit JSON on stdout; parse and return it.
|
|
// On parse failure return an { error, raw } object so the UI can show something.
|
|
function run_json(cmd) {
|
|
let out = run(cmd);
|
|
let data;
|
|
try {
|
|
data = json(out);
|
|
} catch (e) {
|
|
return { error: 'invalid JSON from xrayctl', raw: trim(out) };
|
|
}
|
|
if (data == null)
|
|
return {};
|
|
return data;
|
|
}
|
|
|
|
// Minimal shell-argument quoting: strip single quotes, wrap in single quotes.
|
|
function shq(v) {
|
|
let s = replace('' + v, "'", '');
|
|
return "'" + s + "'";
|
|
}
|
|
|
|
// Normalize xrayctl `nodes` output (a JSON array) into { nodes: [...] }.
|
|
function nodes_result() {
|
|
let d = run_json(XRAYCTL + ' nodes');
|
|
if (type(d) == 'array')
|
|
return { nodes: d };
|
|
return d;
|
|
}
|
|
|
|
// Run an action command and report its exit code.
|
|
function action(cmd) {
|
|
let rc = system(cmd + ' >/dev/null 2>&1');
|
|
return { ok: rc == 0, code: rc };
|
|
}
|
|
|
|
return {
|
|
xray: {
|
|
// --- read methods (acl read.ubus) ---
|
|
status: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' status');
|
|
}
|
|
},
|
|
|
|
nodes: {
|
|
call: function() {
|
|
return nodes_result();
|
|
}
|
|
},
|
|
|
|
stats: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' stats');
|
|
}
|
|
},
|
|
|
|
// Optional geoip.dat/geosite.dat status: presence, sizes, free space, and
|
|
// whether a download would fit. { present, geoip_size, geosite_size,
|
|
// asset_dir, free_kb, need_kb, can_download }.
|
|
geodata_status: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' geodata status');
|
|
}
|
|
},
|
|
|
|
explain: {
|
|
args: { src: '', dst: '', proto: '' },
|
|
call: function(req) {
|
|
let a = req.args;
|
|
if (!a) a = {};
|
|
let cmd = XRAYCTL + ' explain ' + shq(a.src) + ' ' + shq(a.dst);
|
|
if (a.proto)
|
|
cmd += ' --proto ' + shq(a.proto);
|
|
return run_json(cmd);
|
|
}
|
|
},
|
|
|
|
// --- action methods (acl write.ubus) ---
|
|
// On-demand node probe. method: "tcp" (default, fast endpoint connect) or
|
|
// "http" (real proxy-path GET/HEAD through the node, with exit IP). Returns
|
|
// { probe: [{name,alive,latency_ms,status?,exit_ip?,error?}] }.
|
|
node_test: {
|
|
args: { name: '', method: '', url: '', http_method: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
let cmd = XRAYCTL + ' node test';
|
|
if (a.name) cmd += ' ' + shq(a.name);
|
|
if (a.method == 'http') {
|
|
cmd += ' --method http';
|
|
if (a.url) cmd += ' --url ' + shq(a.url);
|
|
if (a.http_method) cmd += ' --http-method ' + shq(a.http_method);
|
|
}
|
|
let d = run_json(cmd);
|
|
if (type(d) == 'array') return { probe: d };
|
|
// A single HTTP probe returns one object.
|
|
if (d.name != null) return { probe: [ d ] };
|
|
return { probe: d.probe || [] };
|
|
}
|
|
},
|
|
|
|
// End-to-end HTTP probe of a whole chain (all hops) — reports reachable /
|
|
// latency / exit-IP. Returns { probe: {name,alive,latency_ms,status?,exit_ip?,error?} }.
|
|
chain_test: {
|
|
args: { name: '', url: '', http_method: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name) return { error: 'chain name required' };
|
|
let cmd = XRAYCTL + ' chain test ' + shq(a.name);
|
|
if (a.url) cmd += ' --url ' + shq(a.url);
|
|
if (a.http_method) cmd += ' --http-method ' + shq(a.http_method);
|
|
return { probe: run_json(cmd) };
|
|
}
|
|
},
|
|
|
|
// Bulk-import pasted share-links. The frontend sends a base64-encoded blob
|
|
// (UTF-8 safe) in `b64`; we decode and pipe it to `xrayctl node import`,
|
|
// which parses the links into `config node` / the node cache.
|
|
node_import: {
|
|
args: { b64: '', text: '' },
|
|
call: function(req) {
|
|
let a = req.args;
|
|
if (!a) a = {};
|
|
let cmd;
|
|
if (a.b64 != null && a.b64 != '')
|
|
cmd = 'echo ' + shq(a.b64) + ' | base64 -d | ' + XRAYCTL + ' node import';
|
|
else
|
|
cmd = 'printf %s ' + shq(a.text) + ' | ' + XRAYCTL + ' node import';
|
|
return action(cmd);
|
|
}
|
|
},
|
|
|
|
// Download geoip.dat/geosite.dat on demand (opkg/apk install after a free-space
|
|
// check). Returns the post-install status (with an "error" field on failure).
|
|
// Slow (fetches ~25 MB) — the frontend calls this with a long rpc timeout.
|
|
geodata_download: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' geodata download');
|
|
}
|
|
},
|
|
|
|
// Remove geoip.dat/geosite.dat to reclaim flash. Returns the post-removal status.
|
|
geodata_remove: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' geodata remove');
|
|
}
|
|
},
|
|
|
|
// Live connections from conntrack (best-effort outbound label).
|
|
conns: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' conns');
|
|
}
|
|
},
|
|
|
|
// QR / share-link export: SVG of the node's share-link (needs qrencode).
|
|
node_qr: {
|
|
args: { name: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name) return { error: 'name required' };
|
|
let svg = run(XRAYCTL + ' node qr ' + shq(a.name));
|
|
if (index(svg, '<svg') < 0)
|
|
return { error: 'qrencode unavailable — opkg install qrencode' };
|
|
return { svg: svg };
|
|
}
|
|
},
|
|
|
|
// --- bulk-action write methods (manual nodes) ---
|
|
node_enable: {
|
|
args: { name: '', enabled: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name) return { ok: false, code: 2 };
|
|
let verb = (a.enabled == '0' || a.enabled == 0) ? ' node disable ' : ' node enable ';
|
|
return action(XRAYCTL + verb + shq(a.name));
|
|
}
|
|
},
|
|
node_delete: {
|
|
args: { name: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name) return { ok: false, code: 2 };
|
|
return action(XRAYCTL + ' node delete ' + shq(a.name));
|
|
}
|
|
},
|
|
node_assign_group: {
|
|
args: { name: '', group: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name || !a.group) return { ok: false, code: 2 };
|
|
return action(XRAYCTL + ' node group ' + shq(a.name) + ' ' + shq(a.group));
|
|
}
|
|
},
|
|
|
|
// Per-subscription quota/expiry + alerts -> array of userinfo views.
|
|
sub_info: {
|
|
call: function() {
|
|
let d = run_json(XRAYCTL + ' sub info');
|
|
return (type(d) == 'array') ? { subs: d } : d;
|
|
}
|
|
},
|
|
|
|
// xray version-compat report { xray_version, ok, features:[...] }.
|
|
compat: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' compat');
|
|
}
|
|
},
|
|
|
|
// Active WAN-mode profile + evaluation { default_iface, active, profiles:[...] }.
|
|
wanmode: {
|
|
call: function() {
|
|
return run_json(XRAYCTL + ' wanmode');
|
|
}
|
|
},
|
|
|
|
// List saved config profiles -> { profiles: [...] }.
|
|
profile_list: {
|
|
call: function() {
|
|
let d = run_json(XRAYCTL + ' profile list');
|
|
return (type(d) == 'array') ? { profiles: d } : d;
|
|
}
|
|
},
|
|
|
|
// Export the whole config as a base64 tar.gz for client-side download.
|
|
backup: {
|
|
call: function() {
|
|
let b64 = trim(run(XRAYCTL + ' backup | base64 | tr -d "\\n"'));
|
|
return { data: b64, filename: 'shater-backup.tar.gz' };
|
|
}
|
|
},
|
|
|
|
// Restore a config from an uploaded base64 tar.gz (validate -> swap ->
|
|
// apply with commit-confirm). confirm = seconds to arm auto-rollback.
|
|
restore: {
|
|
args: { b64: '', confirm: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.b64) return { ok: false, error: 'no data' };
|
|
let c = a.confirm ? (' --confirm ' + shq(a.confirm)) : '';
|
|
let cmd = 'echo ' + shq(a.b64) + ' | base64 -d > /tmp/shater-restore.tar.gz && ' +
|
|
XRAYCTL + ' restore --file /tmp/shater-restore.tar.gz' + c +
|
|
'; rc=$?; rm -f /tmp/shater-restore.tar.gz; exit $rc';
|
|
return action(cmd);
|
|
}
|
|
},
|
|
|
|
profile_save: {
|
|
args: { name: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name) return { ok: false, error: 'name required' };
|
|
return action(XRAYCTL + ' profile save ' + shq(a.name));
|
|
}
|
|
},
|
|
|
|
profile_switch: {
|
|
args: { name: '', confirm: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name) return { ok: false, error: 'name required' };
|
|
let c = a.confirm ? (' --confirm ' + shq(a.confirm)) : '';
|
|
return action(XRAYCTL + ' profile switch ' + shq(a.name) + c);
|
|
}
|
|
},
|
|
|
|
profile_delete: {
|
|
args: { name: '' },
|
|
call: function(req) {
|
|
let a = req.args || {};
|
|
if (!a.name) return { ok: false, error: 'name required' };
|
|
return action(XRAYCTL + ' profile delete ' + shq(a.name));
|
|
}
|
|
},
|
|
|
|
sub_update: {
|
|
args: { name: '' },
|
|
call: function(req) {
|
|
let a = req.args;
|
|
if (!a) a = {};
|
|
let cmd = XRAYCTL + ' sub update';
|
|
if (a.name)
|
|
cmd += ' ' + shq(a.name);
|
|
return action(cmd);
|
|
}
|
|
},
|
|
|
|
apply: {
|
|
call: function() {
|
|
return action(XRAYCTL + ' apply');
|
|
}
|
|
},
|
|
|
|
confirm: {
|
|
call: function() {
|
|
return action(XRAYCTL + ' confirm');
|
|
}
|
|
},
|
|
|
|
// Idempotent re-apply (hotplug/watchdog path).
|
|
reload: {
|
|
call: function() {
|
|
return action(XRAYCTL + ' reconcile');
|
|
}
|
|
}
|
|
}
|
|
};
|