Dawnline 0.1.0: AmneziaWG access server for OpenWrt
Build packages / Version (push) Waiting to run

LuCI admin (people, devices, limits, disguise, health, updates, backups)
and a personal self-service portal, built as ipk (OpenWrt 24.10) and apk
(OpenWrt 25.12) with the OpenWrt SDK. Successor of openwrt-liminal.

- install.sh: checks the router, installs AmneziaWG for its firmware,
  then the release package verified against sha256sums.txt
- CI: Prettier, types, tests, committed bundles, ShellCheck, ucode checks;
  tagged releases build both packages and publish them
- Web bundles minified with esbuild (JS and CSS)
- README with framed screenshots
This commit is contained in:
Aemeath committed 2026-10-15 03:29:30 +03:00
commit a3910f8b97
105 files changed
+22278

No files matched your search

+334
View File
@@ -0,0 +1,334 @@
#!/usr/bin/ucode
// Dawnline agent (procd instance). Every minute it:
// * folds AmneziaWG counters into usage (tmpfs; flushed to flash hourly)
// * saves the activity log to flash as often as Settings says (or never)
// * keeps the "like a home device" path and the Home-only resolver up
// * logs connects, disconnects and roaming
// * enforces end dates, quotas, schedules and pauses (peers + nft sets)
// * watches the tunnel interfaces
'use strict';
import * as uloop from 'uloop';
import { readfile, stat, unlink } from 'fs';
import { read_json, write_json, ensure_dir, sh_out, q, DATA_DIR, RUN_DIR } from 'dawnline.util';
import * as core from 'dawnline.core';
import * as live from 'dawnline.live';
import * as apply from 'dawnline.apply';
import * as ops from 'dawnline.ops';
import * as geo from 'dawnline.geo';
const TICK = 60 * 1000;
const FLUSH_EVERY = 3600;
const LOG_FLUSH = { '10min': 600, hourly: 3600 };
const RESYNC_EVERY = 600;
let usage = live.load_usage();
let last_states = null;
// Who was online, kept in tmpfs so an agent restart (update, reload) does not
// swallow connects that happened in between.
let presence = read_json(`${RUN_DIR}/presence.json`, {});
let last_online = presence.online ?? {};
let last_endpoint = presence.endpoint ?? {};
let offline_ticks = {};
let iface_down = {};
let last_flush = time();
let last_log_flush = time();
let last_net = null;
let rate_prev = null;
/**
* Throughput per server for the Overview's "Speed now": bytes per second
* over the last minute, newest 30 points, kept in tmpfs only.
*/
function record_rate(m) {
let ts = time();
let now = {};
for (let pid, p in m.people) {
let t = usage.people?.[pid]?.total;
if (!t)
continue;
now[p.server] ??= [ 0, 0 ];
now[p.server][0] += t[0];
now[p.server][1] += t[1];
}
let hist = read_json(`${RUN_DIR}/rate.json`, {});
// Per person: the last 15 points (People list, person page).
let people = read_json(`${RUN_DIR}/rate_people.json`, {});
if (type(people) != 'object')
people = {};
if (rate_prev) {
let dt = ts - rate_prev.ts;
if (dt > 0 && dt < 600) {
for (let s, v in now) {
let p = rate_prev.v[s] ?? v;
push(hist[s] ??= [], [ ts, int(max(0, v[0] - p[0]) / dt), int(max(0, v[1] - p[1]) / dt) ]);
while (length(hist[s]) > 30)
shift(hist[s]);
}
for (let pid in keys(m.people)) {
let t = usage.people?.[pid]?.total, p = rate_prev.p?.[pid];
if (!t || !p)
continue;
if (type(people[pid]?.[0]) != 'array')
people[pid] = [];
push(people[pid], [ ts, int(max(0, t[0] - p[0]) / dt), int(max(0, t[1] - p[1]) / dt) ]);
while (length(people[pid]) > 15)
shift(people[pid]);
}
}
}
for (let s in keys(hist))
if (!m.servers[s])
delete hist[s];
for (let pid in keys(people))
if (!m.people[pid])
delete people[pid];
let totals = {};
for (let pid in keys(m.people))
if (usage.people?.[pid]?.total)
totals[pid] = [ ...usage.people[pid].total ];
rate_prev = { ts, v: now, p: totals };
write_json(`${RUN_DIR}/rate.json`, hist, 0o644);
write_json(`${RUN_DIR}/rate_people.json`, people, 0o644);
}
let last_sync = 0;
let cert_mtime = null;
const REASON = {
paused: 'paused by admin', paused_all: 'everyone paused', expired: 'end date reached',
quota: 'data limit reached', hours: 'outside allowed hours', no_server: 'no server', active: 'active',
};
function check_people(m, states, events) {
let ts = time();
for (let id, p in m.people) {
let st = states[id], prev = last_states?.[id];
if (prev != null && prev != st)
push(events, { type: st == 'active' ? 'resumed' : 'paused', person: id, detail: REASON[st] ?? st });
// "Remove their devices" when the end date passes.
if (core.person_expired(p, ts) && p.expire_action == 'remove' && length(p.devices)) {
let c = core.open_cursor();
let mm = core.load(c);
for (let d in mm.people[id]?.devices ?? [])
core.delete_device(c, mm, d);
c.commit('dawnline');
system('chmod 600 /etc/config/dawnline');
push(events, { type: 'devices_removed', person: id, detail: 'end date reached' });
last_sync = 0;
}
}
}
let last_home = 0;
/** Locate new client addresses for the admin map (a couple per minute). */
function check_geo(m) {
if (!m.settings.geo_enabled)
return;
let cache = geo.load();
let ips = [];
for (let id, rec in usage.devices)
for (let ip in keys(rec.ips ?? {}))
push(ips, ip);
let changed = geo.resolve(m.settings, cache, ips, 2);
if (time() - last_home > 3600) {
last_home = time();
let wan = live.iface_status('wan')?.['ipv4-address']?.[0]?.address;
changed = geo.resolve_home(m.settings, cache, wan) || changed;
}
if (changed)
geo.save(cache);
}
function place_of(ip) {
let pl = geo.place(geo.load(), ip);
return pl ? join(' · ', filter([ pl.city, pl.org ], (x) => x)) : null;
}
function check_devices(m, lv, events) {
for (let id, d in lv) {
let dev = m.devices[id];
let was = last_online[id];
// A device we have never seen before that is online now counts as a connect.
if (was == null && d.online && d.present)
was = false;
// Debounce: a device has to look offline on two ticks in a row before
// a disconnect is logged (handshakes are renewed only every 2-3 min).
if (was && !d.online) {
offline_ticks[id] = (offline_ticks[id] ?? 0) + 1;
if (offline_ticks[id] < 2)
continue;
}
else
delete offline_ticks[id];
if (was != null && was != d.online) {
if (d.online)
push(events, { type: 'connect', person: dev.person, device: id, detail: d.endpoint, where: place_of(d.endpoint) });
else
push(events, { type: 'disconnect', person: dev.person, device: id });
}
else if (d.online && last_endpoint[id] && d.endpoint && last_endpoint[id] != d.endpoint)
push(events, { type: 'roam', person: dev.person, device: id, detail: `${last_endpoint[id]} → ${d.endpoint}` });
last_online[id] = d.online;
if (d.endpoint)
last_endpoint[id] = d.endpoint;
}
for (let id in keys(last_online))
if (!m.devices[id]) {
delete last_online[id];
delete last_endpoint[id];
}
ensure_dir(RUN_DIR);
write_json(`${RUN_DIR}/presence.json`, { online: last_online, endpoint: last_endpoint }, 0o600);
}
function check_servers(m, dmp, events) {
for (let id, srv in m.servers) {
if (!srv.enabled || !srv.private_key)
continue;
let up = dmp.ifaces[srv.ifname] != null;
if (!up) {
iface_down[id] = (iface_down[id] ?? 0) + 1;
if (iface_down[id] == 2) {
push(events, { type: 'server_down', server: id, detail: srv.ifname });
}
}
else {
if ((iface_down[id] ?? 0) >= 2) {
push(events, { type: 'server_up', server: id, detail: srv.ifname });
}
iface_down[id] = 0;
}
}
}
function check_cert(m) {
if (!m.settings.portal_enabled)
return;
let c = ops.portal_cert(m.settings);
let mt = stat(c.cert)?.mtime;
if (cert_mtime != null && mt != null && mt != cert_mtime)
system('/etc/init.d/dawnline reload >/dev/null 2>&1 &');
cert_mtime = mt;
}
let last_backup_day = null;
function nightly_backup() {
let t = localtime();
if (t.hour != 3 || last_backup_day == t.mday)
return;
last_backup_day = t.mday;
let sum = sh_out('md5sum /etc/config/dawnline | cut -d" " -f1');
let st = read_json(`${DATA_DIR}/backup-state.json`, {});
if (sum && sum != st.auto) {
try {
ops.backup_create('auto');
write_json(`${DATA_DIR}/backup-state.json`, { auto: sum }, 0o600);
}
catch (e) {}
}
}
/**
* Traffic history resets requested from LuCI (rpcd cannot touch the
* counters this process holds in memory). Applied, then saved to flash.
*/
function check_usage_reset(m) {
let path = `${RUN_DIR}/usage.reset`;
let req = read_json(path, null);
if (req == null)
return false;
unlink(path);
for (let r in (type(req) == 'array' ? req : [ req ])) {
live.reset_usage(usage, m, r.scope, r.id);
}
return true;
}
function tick() {
// A restore or import drops the live counters; start over from flash.
if (!stat(`${RUN_DIR}/usage.json`) && usage.updated)
usage = live.load_usage();
let m = core.load();
let reset = check_usage_reset(m);
let pol = live.policy(m.settings.retention);
let dmp = live.dump();
let lv = live.collect(m, usage, dmp);
try {
record_rate(m);
}
catch (e) {}
let states = apply.states_for(m, usage);
let events = [];
check_devices(m, lv, events);
check_people(m, states, events);
check_servers(m, dmp, events);
let changed = last_states == null || sprintf('%J', states) != sprintf('%J', last_states);
if (changed || time() - last_sync > RESYNC_EVERY) {
apply.sync(m, usage);
last_sync = time();
}
last_states = states;
live.add_events(events, pol);
let persist = reset || time() - last_flush >= FLUSH_EVERY;
live.save_usage(usage, persist);
if (persist)
last_flush = time();
if (pol.persist == 'off' || time() - last_log_flush >= LOG_FLUSH[pol.persist]) {
live.persist_events(pol);
last_log_flush = time();
}
try {
apply.sync_loop(m, usage);
// The router's network changed under us: re-derive everything.
let sig = apply.net_signature(m);
if (last_net != null && sig != last_net) {
system(`logger -t dawnline-agent 'network changed, re-applying'`);
apply.apply();
sig = apply.net_signature(core.load());
}
last_net = sig;
}
catch (e) {
system(`logger -t dawnline-agent ${q('like-home: ' + e.message)}`);
}
check_cert(m);
nightly_backup();
try {
check_geo(m);
}
catch (e) {
system(`logger -t dawnline-agent ${q('geo: ' + e.message)}`);
}
}
function shutdown() {
live.save_usage(usage, true);
live.persist_events();
}
uloop.init();
let timer;
timer = uloop.timer(1000, function() {
try {
tick();
}
catch (e) {
system(`logger -t dawnline-agent ${q('tick failed: ' + e.message)}`);
}
timer.set(TICK);
});
if (type(uloop.signal) == 'function') {
uloop.signal('TERM', () => { shutdown(); uloop.end(); });
uloop.signal('INT', () => { shutdown(); uloop.end(); });
}
uloop.run();
shutdown();
@@ -0,0 +1,9 @@
#!/bin/sh
# Dawnline: run the Home-only resolver where its config says (inside the
# dawnline netns while it exists, otherwise on the router itself).
CONF=/var/etc/dawnline-dns.conf
[ -f "$CONF" ] || exit 1
if head -n1 "$CONF" | grep -q '^# mode=ns' && [ -e /var/run/netns/dawnline ]; then
exec ip netns exec dawnline /usr/sbin/dnsmasq -k -C "$CONF"
fi
exec /usr/sbin/dnsmasq -k -C "$CONF"
+521
View File
@@ -0,0 +1,521 @@
#!/usr/bin/ucode
// Dawnline background update worker, started detached by luci.dawnline.
//
// updater check
// updater install app|amneziawg[:slava|grey[:ru]]|pkgs:<name,…>|all
//
// Progress: /var/run/dawnline/update-job.json; check results:
// /var/run/dawnline/update-check.json. The Updates tab polls both.
'use strict';
import * as fs from 'fs';
import { cursor } from 'uci';
import { connect } from 'ubus';
import { VERSION, REPO } from 'dawnline.util';
const STATE_DIR = '/var/run/dawnline';
const JOB_FILE = `${STATE_DIR}/update-job.json`;
const CHECK_FILE = `${STATE_DIR}/update-check.json`;
const WORK_DIR = '/tmp/dawnline-update';
// Builds per OpenWrt release (tag = firmware version); the kernel module must match the firmware.
// Which AmneziaWG a build carries (2.0 or 3.1) is read from the package, per firmware.
const AWG_SOURCES = {
slava: { repo: 'Slava-Shchipunov/awg-openwrt', label: 'Slava-Shchipunov' },
grey: { repo: '2Grey/awg-openwrt', label: '2Grey' },
};
const AWG_PKGS = [ 'kmod-amneziawg', 'amneziawg-tools', 'luci-proto-amneziawg' ];
const AWG_RU = 'luci-i18n-amneziawg-ru';
// OpenWrt packages Dawnline runs on (the Updates tab groups and names them).
// What Dawnline itself adds to OpenWrt (the rest of LuCI, ucode, rpcd and uhttpd is the
// stock system): the home-like link, and HTTPS plus certificates for the personal portal.
const SYS_PKGS = [ 'ip-full', 'kmod-veth', 'libustream-openssl', 'openssl-util', 'acme', 'acme-acmesh-dnsapi', 'luci-app-acme' ];
// Packages released together with each of the above, updated as a set so OpenSSL never ends up
// half old and half new.
const PKG_FAMILY = [
[ /^ip-full$/, /^ip-/ ],
[ /^(libustream-openssl|openssl-util)$/, /^(libustream-openssl|libopenssl|openssl-util$)/ ],
[ /^(acme|luci-app-acme$)/, /^acme/ ],
];
function family(p) {
for (let f in PKG_FAMILY)
if (match(p, f[0]))
return f[1];
return null;
}
let job = {
action: ARGV[0], component: ARGV[1] ?? null, state: 'running', started: time(), finished: null,
phase: '', log: [], error: null, result: null,
};
function save() {
fs.writefile(`${JOB_FILE}.tmp`, sprintf('%J', job));
fs.rename(`${JOB_FILE}.tmp`, JOB_FILE);
}
function log(msg) {
let t = localtime();
push(job.log, sprintf('%02d:%02d:%02d %s', t.hour, t.min, t.sec, msg));
if (length(job.log) > 300)
splice(job.log, 0, length(job.log) - 300);
save();
}
function phase(p) {
job.phase = p;
log(p);
}
function q(s) {
return "'" + replace(`${s ?? ''}`, "'", "'\\''") + "'";
}
function run(cmd) {
let p = fs.popen(cmd + ' 2>&1', 'r');
if (!p)
return { code: 127, out: '' };
let out = p.read('all') ?? '';
return { code: p.close(), out };
}
function run_logged(cmd) {
let r = run(cmd);
for (let line in split(trim(r.out), '\n'))
if (length(trim(line)))
log(` ${line}`);
return r;
}
function repo() {
return REPO;
}
function have_curl() {
return fs.access('/usr/bin/curl', 'x');
}
function download(url, dest) {
let r = have_curl()
? run(`curl -fsSL --connect-timeout 15 --max-time 300 --retry 2 -o ${q(dest)} ${q(url)}`)
: run(`uclient-fetch -q -T 30 -O ${q(dest)} ${q(url)}`);
return r.code == 0 && (fs.stat(dest)?.size ?? 0) > 0;
}
/** Latest release tag; the /releases/latest redirect avoids API rate limits. */
function latest_tag(rp) {
if (have_curl()) {
let r = run(`curl -sI --connect-timeout 15 --max-time 30 -o /dev/null -w '%{redirect_url}' ${q('https://github.com/' + rp + '/releases/latest')}`);
let m = match(trim(r.out), /\/releases\/tag\/([^\/?#]+)$/);
if (m)
return m[1];
}
let r = run(`uclient-fetch -q -T 20 -O - ${q('https://api.github.com/repos/' + rp + '/releases/latest')}`);
try {
return json(r.out)?.tag_name;
}
catch (e) {
return null;
}
}
function version_cmp(a, b) {
let pa = split(replace(`${a}`, /^v/, ''), /[.~+-]/), pb = split(replace(`${b}`, /^v/, ''), /[.~+-]/);
for (let i = 0; i < length(pa) || i < length(pb); i++) {
let x = pa[i] ?? '0', y = pb[i] ?? '0';
if (match(x, /^[0-9]+$/) && match(y, /^[0-9]+$/)) {
if (int(x) != int(y))
return int(x) < int(y) ? -1 : 1;
}
else if (x != y)
return x < y ? -1 : 1;
}
return 0;
}
function is_apk() {
return fs.access('/usr/bin/apk', 'x');
}
function pkg_version(name) {
let esc = replace(name, /[.+]/g, '\\$&');
if (is_apk()) {
let m = match(run(`apk list --installed ${q(name)}`).out, regexp(`^${esc}-([^ ]+) `));
return m ? m[1] : null;
}
// Libraries carry their ABI in the name (libustream-openssl20201210).
let re = regexp(`^${esc}([0-9]+)? - (.+)$`), found = null;
for (let line in split(run('opkg list-installed').out, '\n')) {
let m = match(trim(line), re);
if (m && (found == null || m[1] == null))
found = m;
}
return found ? trim(found[2]) : null;
}
function pkg_file_version(path) {
if (match(path, /\.ipk$/)) {
let o = run(`tar -xzOf ${q(path)} ./control.tar.gz | tar -xzO ./control`).out;
return match(o, /(^|\n)Version: *([^\n]+)/)?.[2];
}
return match(run(`apk adbdump ${q(path)}`).out, /\n *version: *([^\n]+)/)?.[1];
}
function workdir() {
run(`rm -rf ${WORK_DIR}`);
fs.mkdir(WORK_DIR, 0o700);
}
/* ---------- Dawnline itself ---------- */
function check_app() {
let rp = repo();
let tag = latest_tag(rp);
if (!tag)
return { installed: VERSION, latest: null, update: false, note: `No published release in ${rp}` };
let latest = replace(tag, /^v/, '');
return { installed: VERSION, latest, tag, repo: rp, update: version_cmp(VERSION, latest) < 0 };
}
function install_app() {
let c = check_app();
if (!c.latest)
die(c.note);
if (!c.update) {
log('Dawnline is up to date');
return { installed: VERSION };
}
// Releases carry luci-app-dawnline-<version>.ipk and .apk plus sha256sums.txt.
let asset = `luci-app-dawnline-${c.latest}.${is_apk() ? 'apk' : 'ipk'}`;
let base = `https://github.com/${c.repo}/releases/download/${c.tag}`;
workdir();
phase(`Downloading ${asset}`);
let f = `${WORK_DIR}/${asset}`, sums = `${WORK_DIR}/sha256sums.txt`;
if (!download(`${base}/${asset}`, f))
die(`Download failed: ${asset}`);
phase('Verifying package');
if (!download(`${base}/sha256sums.txt`, sums))
die('The release has no sha256sums.txt');
let want = null;
for (let line in split(fs.readfile(sums) ?? '', '\n')) {
let m = match(trim(line), /^([0-9a-f]{64})[ \t]+\*?(\S+)$/);
if (m && m[2] == asset)
want = m[1];
}
let got = match(run(`sha256sum ${q(f)}`).out, /^([0-9a-f]{64})/)?.[1];
if (!want || got != want)
die(`Checksum mismatch for ${asset}`);
phase('Backing up configuration');
run('/usr/bin/dawnline backup --reason pre-update');
phase('Installing package');
if (run_logged(is_apk() ? `apk add --allow-untrusted ${q(f)}` : `opkg install ${q(f)}`).code != 0)
die('Installation failed');
run('rm -f /tmp/luci-indexcache* /tmp/luci-modulecache/* 2>/dev/null');
// Restart after this job has saved its final state.
run('( sleep 3; /etc/init.d/rpcd reload; /etc/init.d/dawnline restart ) </dev/null >/dev/null 2>&1 &');
return { installed: c.latest };
}
/* ---------- AmneziaWG ---------- */
function awg_release() {
let u = connect();
let b = u?.call('system', 'board') ?? {};
u?.disconnect();
let rel = b.release ?? {};
let target = split(rel.target ?? '', '/');
let arch = rel.arch ?? trim(run("sed -n \"s/^DISTRIB_ARCH='\\(.*\\)'/\\1/p\" /etc/openwrt_release").out);
if (!rel.version || length(target) != 2 || !arch)
return null;
return {
tag: `v${rel.version}`,
postfix: `_v${rel.version}_${arch}_${target[0]}_${target[1]}`,
ext: is_apk() ? 'apk' : 'ipk',
};
}
function awg_download(r, name, rp) {
let file = `${name}${r.postfix}.${r.ext}`;
let dest = `${WORK_DIR}/${replace(rp, '/', '_')}_${file}`;
return download(`https://github.com/${rp}/releases/download/${r.tag}/${file}`, dest) ? dest : null;
}
/** When the build for this firmware was published (best effort). */
function release_date(rp, tag) {
let r = run(`uclient-fetch -q -T 20 -O - ${q(`https://api.github.com/repos/${rp}/releases/tags/${tag}`)}`);
try {
return json(r.out)?.published_at ?? null;
}
catch (e) {
return null;
}
}
/** AmneziaWG generation of a tools version: 3.x packages carry 3.1, older ones (1.0.x) 2.0. */
function awg_gen(version) {
return version == null ? null : match(version, /^3\./) ? '3.1' : '2.0';
}
/** The newest tag of the same OpenWrt series (24.10.x) in a repository, if newer than ours. */
function newer_series_tag(rp, tag) {
let r = run(`uclient-fetch -q -T 20 -O - ${q(`https://api.github.com/repos/${rp}/releases?per_page=30`)}`);
let series = match(tag, /^v([0-9]+\.[0-9]+)\./)?.[1];
let best = null;
try {
for (let x in json(r.out) ?? []) {
let t = x.tag_name;
if (series && match(t, regexp(`^v${replace(series, '.', '\\.')}\\.`)) && version_cmp(t, tag) > 0 && (!best || version_cmp(t, best) > 0))
best = t;
}
}
catch (e) {}
return best;
}
function awg_installed() {
let r = {};
for (let p in AWG_PKGS)
r[p] = pkg_version(p);
return r;
}
function check_awg() {
let installed = awg_installed();
let ru = pkg_version(AWG_RU) != null;
let cur = installed['amneziawg-tools'];
let r = awg_release();
if (!r)
return { installed: cur, gen: awg_gen(cur), packages: installed, current: null, ru, sources: {}, update: false, note: 'Cannot detect the firmware target' };
workdir();
let sources = {};
for (let id, src in AWG_SOURCES) {
let f = awg_download(r, 'amneziawg-tools', src.repo);
let v = f ? pkg_file_version(f) : null;
let s2 = { label: src.label, repo: src.repo, tag: r.tag, version: v, gen: awg_gen(v), date: f ? release_date(src.repo, r.tag) : null };
// A newer firmware may already get 3.1 from this source.
if (s2.gen != '3.1') {
let nt = newer_series_tag(src.repo, r.tag);
if (nt) {
let nf = awg_download({ ...r, tag: nt, postfix: replace(r.postfix, r.tag, nt) }, 'amneziawg-tools', src.repo);
let nv = nf ? pkg_file_version(nf) : null;
if (awg_gen(nv) == '3.1')
s2.newer = { tag: nt, gen: '3.1' };
}
}
sources[id] = s2;
}
// The installed build: the source whose package matches, else the main one.
let current = null;
for (let id, x in sources)
if (!current && cur && x.version == cur)
current = id;
current ??= cur ? 'slava' : null;
let mine = current ? sources[current] : null;
return { installed: cur, gen: awg_gen(cur), packages: installed, current, ru, release: r.tag, sources,
latest: mine?.version ?? null,
update: cur == null || (mine?.version != null && version_cmp(cur, mine.version) < 0),
note: !length(filter(values(sources), (x) => x.version)) ? `No AmneziaWG build for OpenWrt ${substr(r.tag, 1)} yet` : null };
}
function install_awg(arg) {
let parts = split(arg ?? '', ':');
let id = parts[0] || 'slava';
let src = AWG_SOURCES[id];
if (!src)
die(`Unknown AmneziaWG build ${id}`);
let want_ru = parts[1] == 'ru';
let r = awg_release();
if (!r)
die('Cannot detect the firmware target');
let before = awg_installed();
workdir();
let files = [];
for (let p in [ ...AWG_PKGS, ...(want_ru ? [ AWG_RU ] : []) ]) {
phase(`Downloading ${p} from ${src.repo}`);
let f = awg_download(r, p, src.repo);
if (!f)
die(`No ${p} build for OpenWrt ${substr(r.tag, 1)} in ${src.repo}`);
push(files, f);
}
phase('Refreshing package lists');
run_logged(is_apk() ? 'apk update' : 'opkg update');
phase(`Installing the ${src.label} build`);
let cmd = is_apk()
? `apk add --allow-untrusted ${join(' ', map(files, q))}`
: `opkg install --force-reinstall --force-downgrade ${join(' ', map(files, q))}`;
if (run_logged(cmd).code != 0)
die('Package installation failed');
if (!want_ru && pkg_version(AWG_RU))
run_logged(is_apk() ? `apk del ${AWG_RU}` : `opkg remove ${AWG_RU}`);
let after = awg_installed();
if (before['kmod-amneziawg'] && before['kmod-amneziawg'] != after['kmod-amneziawg']) {
// New kernel module: every AmneziaWG interface must be re-created.
let ifaces = [];
let c = cursor();
c.load('network');
c.foreach('network', 'interface', (s) => {
if (s.proto == 'amneziawg' && s.disabled != '1')
push(ifaces, s['.name']);
});
phase(`Reloading the kernel module (${length(ifaces)} tunnel(s) restart)`);
for (let i in ifaces)
run(`ifdown ${q(i)}`);
run('rmmod amneziawg');
run_logged('modprobe amneziawg');
for (let i in ifaces)
run(`ifup ${q(i)}`);
}
run('rm -f /tmp/luci-indexcache* /tmp/luci-modulecache/* 2>/dev/null');
return { build: id, before, after };
}
/* ---------- OpenWrt packages Dawnline uses ---------- */
// Installed packages with a newer version in the feeds: { name: latest }.
function upgradable(refresh) {
if (refresh) {
phase('Refreshing package lists');
run(is_apk() ? 'apk update' : 'opkg update');
}
let up = {};
if (is_apk()) {
for (let line in split(run('apk version -l "<"').out, '\n')) {
let m = match(line, /^(\S+?)-([0-9][^ ]*)\s+<\s+(\S+)/);
if (m)
up[m[1]] = m[3];
}
}
else
for (let line in split(run('opkg list-upgradable').out, '\n')) {
let m = match(line, /^(\S+) - (\S+) - (\S+)/);
if (m)
up[m[1]] = m[3];
}
return up;
}
// The packages to update for these names: each one plus its upgradable family.
function with_family(names, up) {
let set = {};
for (let n in names) {
if (up[n])
set[n] = true;
let f = family(n);
if (f)
for (let u in keys(up))
if (match(u, f))
set[u] = true;
}
return sort(keys(set));
}
function check_pkgs(refresh) {
let up = upgradable(refresh ?? true);
let res = {}, all = {};
for (let p in SYS_PKGS) {
let v = pkg_version(p);
let set = v ? with_family([ p ], up) : [];
res[p] = { installed: v, latest: v && up[p] ? up[p] : null, with: filter(set, (x) => x != p) };
for (let x in set)
all[x] = true;
}
return { pkgs: res, updates: length(keys(all)) };
}
// Before install_pkgs: ucode resolves a name at the point it is used.
function save_check(res, at) {
fs.writefile(CHECK_FILE, sprintf('%J', { time: at ?? time(), components: res }));
}
function install_pkgs(arg) {
let names = filter(split(arg ?? '', ','), (x) => x != '');
for (let n in names)
if (index(SYS_PKGS, n) < 0)
die(`${n} is not a package Dawnline manages`);
if (!length(names))
die('Nothing to install');
phase('Refreshing package lists');
run_logged(is_apk() ? 'apk update' : 'opkg update');
let missing = filter(names, (n) => pkg_version(n) == null);
let have = with_family(filter(names, (n) => pkg_version(n) != null), upgradable(false));
if (length(have)) {
phase(`Updating ${join(', ', have)}`);
if (run_logged(is_apk() ? `apk upgrade ${join(' ', map(have, q))}` : `opkg upgrade ${join(' ', map(have, q))}`).code != 0)
die('Update failed');
}
if (length(missing)) {
phase(`Installing ${join(', ', missing)}`);
if (run_logged(is_apk() ? `apk add ${join(' ', map(missing, q))}` : `opkg install ${join(' ', map(missing, q))}`).code != 0)
die('Installation failed');
}
// Show the new state right away instead of the check from before the update.
try {
let c = json(fs.readfile(CHECK_FILE) ?? '{}') ?? {};
c.components ??= {};
c.components.packages = check_pkgs(false);
save_check(c.components, c.time);
}
catch (e) {
log(`Could not refresh the package check: ${e.message}`);
}
// The portal's HTTPS runs in uhttpd. Restart after this job saved its state.
let touched = [ ...names, ...have ];
// A missing piece arrived: set up what waited for it (the portal, the home-like link).
if (length(missing) && length(filter(missing, (n) => match(n, /^(openssl-util|libustream|ip-full|kmod-veth)/))))
run('( sleep 3; /usr/bin/dawnline apply; /etc/init.d/uhttpd restart; /etc/init.d/dawnline restart ) </dev/null >/dev/null 2>&1 &');
else if (length(filter(touched, (n) => match(n, /^(libustream|libopenssl)/))))
run('( sleep 3; /etc/init.d/uhttpd restart; /etc/init.d/dawnline restart ) </dev/null >/dev/null 2>&1 &');
return { updated: have, installed: missing };
}
/* ---------- main ---------- */
const CHECKS = { app: check_app, amneziawg: check_awg, packages: check_pkgs };
const INSTALLS = { app: install_app, amneziawg: install_awg, pkgs: install_pkgs };
if (!fs.access(STATE_DIR))
fs.mkdir(STATE_DIR, 0o755);
save();
try {
if (job.action == 'check') {
let res = {};
for (let name, fn in CHECKS) {
phase(`Checking ${name}`);
try {
res[name] = fn();
}
catch (e) {
res[name] = { error: e.message };
}
}
save_check(res);
job.result = res;
}
else if (job.action == 'install') {
let what = job.component == 'all' ? [ 'amneziawg', 'app' ] : [ job.component ];
job.result = {};
for (let w in what) {
let i = index(w, ':');
let name = i < 0 ? w : substr(w, 0, i), arg = i < 0 ? null : substr(w, i + 1);
if (!INSTALLS[name])
die(`Unknown component ${name}`);
phase(`Updating ${name}`);
job.result[name] = INSTALLS[name](arg);
}
}
else
die('Usage: updater check | install app|amneziawg[:build[:ru]]|pkgs:<names>|all');
job.state = 'done';
phase('Finished');
}
catch (e) {
job.state = 'failed';
job.error = e.message;
log(`Error: ${e.message}`);
}
run(`rm -rf ${WORK_DIR}`);
job.finished = time();
save();