Files
dawnline/package/luci-app-dawnline/root/usr/libexec/dawnline/agent
T
Aemeath 972e522518
Build packages / Version (push) Waiting to run
Dawnline 0.5.0
- Where people connect from is a globe map now: live arcs from each
  person to the server, grey ones for the last 24 h, 7 or 30 days, avatars
  instead of dots, the server's own marker, and a light map in light mode;
  it zooms to fit the people shown and follows them as they come and go
- Markers that overlap move apart with a pointer to their spot; people
  next to the server fold into a count on it; a click zooms in and opens a
  floating card (never clipped by the map), closing flies back
- The person page maps where they connected from, older places fading;
  the device panel lists its places in order, and connections from the LAN
  or through the router's own address stay off the maps ("LAN" in grey)
- Setup and new servers check the UDP port (taken by another program, a
  forward or another server) and the subnet (three numbers + .x, a private
  range not in use) as you type; the default port is 4500
- The personal portal can share the server's port number (TCP beside UDP);
  its port is checked against forwards and other services
- A new server comes up at once: the kernel module is loaded and the
  network restarted when OpenWrt doesn't know AmneziaWG yet
- A turned-off server is a state of its own: no "tunnel down" or DNS
  warnings, "Turned off" in yellow, a white Turn on, and people on it are
  not shown as paused
- Pause everyone is removed: turning a server off does the same (a router
  left paused keeps its servers off after the update)
- People: a stopped person's card fades and says why across its bottom,
  with the one action that lifts it (Resume, +10 GB, Extend 1 month);
  people held by a limit or end date sort below the others, paused last
- The person page shows the state as text ("Paused") with a Resume
  button, also while their server is off
- The portal tells a paused or ended person just that, even with the
  server off, and lets them change nothing until the admin lifts it (the
  router refuses too); its notices look like Overview's
- Automatic MTU: with the field empty the tunnel, computers and routers
  get 1380 minus AmneziaWG S4 (1368 on Maximum), phones and tablets 1280;
  a typed MTU still caps phones and tablets at 1280, and servers on the
  old default 1380 move to automatic (phones and tablets get "Update
  config", their old files keep working)
- Only procd's agent runs: a second copy (started by hand or left over)
  refuses to start or is ended within 5 minutes, so no more stray paused
  and resumed events; a device that leaves the tunnel is offline at once
- The installer picks the newest release (it took the lowest), DNS
  lookups can't hang, and tooltips and floating cards close on scroll and
  on leaving the page
2026-10-17 01:03:23 +03:00

577 lines
20 KiB
Plaintext
Executable File

#!/usr/bin/ucode
// Dawnline agent (procd instance). Every second its watcher reads the tunnels
// and, the moment a device connects, its session expires or it roams, logs
// it, folds the counters, locates the new address and moves the revision the
// admin UI polls (presence.json, see dawnline.live). Every minute it:
// * folds the tunnels' 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
// * enforces end dates, quotas, schedules and pauses (peers + nft sets)
// * watches the tunnel interfaces and repairs what it can (dawnline.watchdog)
'use strict';
import * as uloop from 'uloop';
import { open, readfile, writefile, stat, unlink } from 'fs';
import {
read_json, write_json, ensure_dir, sh_out, q, uniq, slog, slog_trim, slog_restore, DATA_DIR, RUN_DIR, VERSION
} from 'dawnline.util';
import * as core from 'dawnline.core';
import * as live from 'dawnline.live';
import * as apply from 'dawnline.apply';
import * as backup from 'dawnline.backup';
import * as pc from 'dawnline.portalcert';
import * as geo from 'dawnline.geo';
import * as watchdog from 'dawnline.watchdog';
import * as net from 'dawnline.net';
import * as store from 'dawnline.store';
import * as upd from 'dawnline.updates';
import * as awg from 'dawnline.awg';
const TICK = 60 * 1000;
const WATCH = 1000;
const FLUSH_EVERY = 3600;
const LOG_FLUSH = { '10min': 600, hourly: 3600 };
const RESYNC_EVERY = 600;
const CONFIG_FILE = '/etc/config/dawnline';
// One agent, procd's. A second copy would run forever beside it with the code
// it started with: its own events (pauses and resumes from stale states), its
// own counters and syncs. It takes no arguments: the commands are `dawnline`'s.
if (length(ARGV)) {
warn(`The agent takes no arguments (procd runs it). For commands use: dawnline ${join(' ', ARGV)}\n`);
exit(2);
}
ensure_dir(RUN_DIR);
// Held as long as the process lives; close-on-exec, so children don't keep it.
let instance = open(`${RUN_DIR}/agent.lock`, 'we');
if (!instance?.lock('xn')) {
warn(`The agent is already running (${RUN_DIR}/agent.lock is held). Use /etc/init.d/dawnline restart.\n`);
exit(1);
}
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(live.PRESENCE, {});
let last_online = presence.online ?? {};
let last_endpoint = presence.endpoint ?? {};
let rev = +(presence.rev ?? 0);
let person_rev = presence.people ?? {};
let last_flush = time();
let last_log_flush = time();
// The network as apply() last saw it (apply.NET_SIG; this when it never ran),
// and a different one seen on the previous tick.
let base_net = null;
let pending_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 conf_mtime = null;
let cert_mtime = null;
const REASON = {
paused: 'paused by admin', expired: 'end date reached',
quota: 'data limit reached', hours: 'outside allowed hours', no_server: 'no server', server_off: 'server turned off',
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];
// The admin's own pauses (and turning a server off or on) are logged by
// rpcd when they happen; here only what changes on its own (end date,
// data limit, allowed hours).
let by_admin = (x) => x == 'paused' || x == 'server_off';
if (prev != null && prev != st && !by_admin(prev) && !by_admin(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)) {
store.transact((c, mm) => {
for (let d in mm.people[id]?.devices ?? [])
core.delete_device(c, mm, d);
});
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;
}
/** Connections from home: the DHCP name of the address, as it is now. */
function named(e) {
let h = net.event_hosts(e, net.lan_names());
if (h)
e.hosts = h;
return e;
}
/* ---------- the watcher ---------- */
// Per device: its latest real handshake, kept when apply re-adds the peer
// (its counters and handshake restart from zero, or it is briefly missing).
let last_hs = {};
// The model, read again only when the config file changes (the watcher runs every second).
let wm = null, wm_sig = null;
function watch_model() {
// uci commits replace the file: a new inode even within the same second.
let st = stat(CONFIG_FILE);
let sig = st ? `${st.mtime}/${st.inode}` : null;
if (wm == null || sig != wm_sig) {
wm = core.load();
wm_sig = sig;
}
return wm;
}
/** Move the revisions (all, and these people's) and save who is online. */
function save_presence(people) {
rev++;
for (let pid in people)
person_rev[pid] = rev;
ensure_dir(RUN_DIR);
write_json(live.PRESENCE, { online: last_online, endpoint: last_endpoint, rev, people: person_rev }, 0o600);
}
/*
* One look at the tunnels. Connects (a new handshake from a device that was
* away), disconnects (its session expired: no handshake for live.ONLINE_WINDOW;
* a phone asleep looks the same as one switched off, so silence alone is not
* enough) and roaming (a new address) are acted on at
* once: the log, the counters and the map, then the revision the UI polls.
* Only from a complete read: a failed one says nothing about anyone.
*/
function watch() {
let d = awg.dump();
if (!d.ok)
return;
let m = watch_model();
let now = time();
let events = [], people = {}, ips = [];
for (let id, dev in m.devices) {
let peer = d.peers[dev.public_key];
let ep = '';
if (peer) {
last_hs[id] = max(last_hs[id] ?? 0, peer.handshake);
ep = live.endpoint_ip(peer.endpoint);
}
// Not in the tunnel (turned off, paused, out of time or data): offline now, and online
// again only with a new handshake, not the one remembered from before.
else
last_hs[id] = 0;
let on = last_hs[id] > 0 && now - last_hs[id] < live.ONLINE_WINDOW;
let was = last_online[id];
if (on && !was) {
push(events, { type: 'connect', person: dev.person, device: id, detail: ep });
push(ips, ep);
}
else if (!on && was)
push(events, { type: 'disconnect', person: dev.person, device: id });
else if (on && ep && last_endpoint[id] && ep != last_endpoint[id]) {
push(events, { type: 'roam', person: dev.person, device: id, detail: `${last_endpoint[id]} → ${ep}` });
push(ips, ep);
}
if (on != !!was || (ep && ep != last_endpoint[id]))
people[dev.person] = true;
last_online[id] = on;
if (ep)
last_endpoint[id] = ep;
}
for (let id in keys(last_online))
if (!m.devices[id]) {
delete last_online[id];
delete last_endpoint[id];
delete last_hs[id];
}
if (!length(keys(people)))
return;
// The status first, then what takes longer (a geo lookup may wait on the network).
save_presence(keys(people));
if (!length(events))
return;
live.collect(m, usage, d);
live.save_usage(usage, false);
if (m.settings.geo_enabled && length(ips)) {
let cache = geo.load();
if (geo.resolve(m.settings, cache, ips, length(ips)))
geo.save(cache);
}
for (let e in events)
if (e.type == 'connect')
e.where = place_of(e.detail);
live.add_events(map(events, (e) => e.type == 'disconnect' ? e : named(e)), live.policy(m.settings.retention));
save_presence(keys(people));
}
// What the service log was already told. On flash: the public address often
// changes with a reboot (PPPoE), and that is when it matters. Written rarely.
const NOTES = `${DATA_DIR}/notes.json`;
let notes = read_json(NOTES, {});
let last_cert_check = 0;
function note(k, v) {
notes[k] = v;
ensure_dir(DATA_DIR, 0o700);
write_json(NOTES, notes, 0o600);
}
/** "Jan 1 2027" from the certificate, or null. */
function cert_end(path) {
let m = match(sh_out(`openssl x509 -enddate -noout -in ${q(path)} 2>/dev/null`) ?? '', /notAfter=([A-Za-z]+) +([0-9]+) [0-9:]+ ([0-9]+)/);
return m ? `${m[1]} ${m[2]} ${m[3]}` : null;
}
function check_cert(m) {
if (!m.settings.portal_enabled)
return;
let c = pc.portal_cert(m.settings);
let mt = stat(c.cert)?.mtime;
if (cert_mtime != null && mt != null && mt != cert_mtime) {
slog('portal', 'up', `certificate renewed · valid until ${cert_end(c.cert) ?? '?'}`, null, 'Personal portal');
system('/etc/init.d/dawnline reload >/dev/null 2>&1 &');
}
cert_mtime = mt;
// Running out soon (ACME did not renew it): once per certificate, checked hourly.
if (mt == null || c.self_signed || time() - last_cert_check < 3600)
return;
last_cert_check = time();
if (notes.cert_warned != mt && system(`openssl x509 -checkend 604800 -noout -in ${q(c.cert)} >/dev/null 2>&1`) != 0) {
slog('portal', 'error', `certificate expires ${cert_end(c.cert) ?? 'within 7 days'}`,
'Browsers will refuse the portal after that. Renew it with ACME, or choose another certificate in Settings.', 'Personal portal');
note('cert_warned', mt);
}
}
/**
* The router's public IPv4 changed. Devices whose configuration holds the
* address itself (a server's public address is an IP) cannot connect until
* they get a new one; with a hostname only the DNS record must follow.
*/
function check_wan(m) {
let srv = filter(values(m.servers), (x) => x.enabled && x.private_key)[0];
let ip = srv ? net.wan_address(srv) : null;
if (!ip || ip == notes.wan)
return;
let was = notes.wan;
note('wan', ip);
if (!was)
return;
let stuck = map(filter(values(m.servers), (x) => core.endpoint_host(m, x) == was), (x) => x.name);
if (length(stuck))
slog('dawnline', 'error', `public address changed · ${was} → ${ip}`,
`${join(', ', stuck)} still give${length(stuck) == 1 ? 's' : ''} devices the old address: set the public address in the server settings (a DDNS name keeps working), then devices need new configurations.`, 'Router');
else
slog('dawnline', 'info', `public address changed · ${was} → ${ip}`, null, 'Router');
}
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 {
backup.backup_create('auto');
write_json(`${DATA_DIR}/backup-state.json`, { auto: sum }, 0o600);
}
catch (e) {}
}
}
/**
* Settings → Updates: an automatic check or update when one is due. The
* updater does the work in its own process; this only decides when.
*/
function check_updates(m) {
if (upd.due(m.settings.updates, upd.auto_state()) && !upd.busy())
system('start-stop-daemon -S -b -x /usr/libexec/dawnline/updater -- auto');
}
/**
* 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;
}
/*
* Push devices and access rules when people's states changed, the config
* file was written (by anyone: rpcd, the portal, the CLI, a restore), or
* every RESYNC_EVERY seconds anyway. apply.sync reads the model again itself.
*/
function sync_runtime(t) {
// uci commits replace the file: a new inode even within the same second.
let st = stat(CONFIG_FILE);
let mt = st ? `${st.mtime}/${st.inode}` : null;
let changed = last_states == null || sprintf('%J', t.states) != sprintf('%J', last_states) ||
(conf_mtime != null && mt != conf_mtime);
if (changed || time() - last_sync > RESYNC_EVERY) {
apply.sync(t.m, usage);
last_sync = time();
}
conf_mtime = mt;
last_states = t.states;
}
/*
* The router's network changed under us: re-derive everything. Only a change
* seen on two ticks in a row (a bridge recreated or an interface restarting
* passes), compared with what the last apply() used.
*/
function check_network(t) {
let sig = apply.net_signature(t.m);
let applied = readfile(apply.NET_SIG) ?? base_net;
if (applied == null || sig == applied) {
base_net ??= sig;
pending_net = null;
return;
}
if (pending_net != sig) {
pending_net = sig;
return;
}
pending_net = null;
slog('dawnline', 'info', 'network changed — Dawnline set itself up again', null, 'Router');
apply.apply();
base_net = readfile(apply.NET_SIG) ?? apply.net_signature(core.load());
}
/*
* One tick is a list of steps, each on its own: a failing step is logged
* (each new message once in the service log, every time in syslog) and the
* others still run. Steps that need the model wait for it; the counters and
* the logs are saved at the end whatever happened.
*/
const STEPS = [
{ who: 'Dawnline', run: (t) => {
t.m = core.load();
t.pol = live.policy(t.m.settings.retention);
} },
{ who: 'Traffic', run: (t) => {
t.reset = check_usage_reset(t.m);
live.collect(t.m, usage, live.dump(t.m));
} },
{ who: 'Speed', quiet: true, run: (t) => record_rate(t.m) },
{ who: 'People', run: (t) => {
t.states = apply.states_for(t.m, usage);
check_people(t.m, t.states, t.events);
} },
{ who: 'Access rules', run: (t) => {
if (t.states)
sync_runtime(t);
} },
// Tunnels going down and up are in the service log (hotplug, watchdog), with names and reasons.
{ who: 'Activity', run: (t) => {
live.add_events(t.events, t.pol);
// Pauses and resumes on their own: the UI shows them without waiting for its next poll.
if (length(t.events))
save_presence(uniq(map(filter(t.events, (e) => e.person), (e) => e.person)));
} },
{ who: 'Internet like a home device', run: (t) => apply.sync_loop(t.m, usage) },
{ who: 'Router', run: check_network },
{ who: 'Watchdog', run: (t) => watchdog.run(core.load()) },
{ who: 'Personal portal', run: (t) => check_cert(t.m) },
{ who: 'Router', quiet: true, run: (t) => check_wan(t.m) },
{ who: 'Backup', run: (t) => nightly_backup() },
{ who: 'updates', quiet: true, run: (t) => check_updates(t.m) },
{ who: 'geo', quiet: true, run: (t) => check_geo(t.m) },
];
// Step → the last error it logged (cleared when it works again).
let step_err = {};
function step(i, s, fn) {
try {
fn();
delete step_err[i];
}
catch (e) {
system(`logger -t dawnline-agent ${q(`${s.who}: ${e.message}`)}`);
if (!s.quiet && step_err[i] != e.message)
slog('dawnline', 'error', `failed: ${e.message}`, e.stacktrace?.[0]?.context, s.who);
step_err[i] = e.message;
}
}
function tick() {
ensure_dir(RUN_DIR);
writefile(watchdog.BEAT, `${time()}`);
// 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 t = { m: null, pol: null, reset: false, events: [] };
for (let i, s in STEPS)
if (i == 0 || t.m)
step(i, s, () => s.run(t));
let persist = t.reset || time() - last_flush >= FLUSH_EVERY;
step('save', { who: 'Traffic' }, () => {
live.save_usage(usage, persist);
if (persist)
last_flush = time();
});
step('slog', { who: 'Dawnline', quiet: true }, () => slog_trim(persist));
step('events', { who: 'Activity' }, () => {
let pol = t.pol ?? live.policy();
if (pol.persist == 'off' || time() - last_log_flush >= LOG_FLUSH[pol.persist]) {
live.persist_events(pol);
last_log_flush = time();
}
});
}
function shutdown() {
live.save_usage(usage, true);
live.persist_events();
slog_trim(true);
// A clean stop: the next start is not a crash.
unlink(watchdog.BEAT);
}
// Why this start: the heartbeat file outlives an agent that died (crash, out
// of memory, kill -9); the init script notes a stop; the version on flash
// changes with an update; a fresh uptime means the router just booted.
let crashed = stat(watchdog.BEAT) != null;
let stopped = +(readfile(`${RUN_DIR}/agent.stopped`) ?? 0);
unlink(`${RUN_DIR}/agent.stopped`);
let was = trim(readfile(`${DATA_DIR}/version`) ?? '');
if (was != VERSION) {
ensure_dir(DATA_DIR, 0o700);
writefile(`${DATA_DIR}/version`, VERSION);
}
slog_restore();
if (was && was != VERSION)
slog('dawnline', 'up', `updated · ${was} → ${VERSION}`, null, 'Dawnline');
else if (crashed)
slog('dawnline', 'repair', 'stopped unexpectedly — started again',
// Why, when syslog knows: procd, the kernel (out of memory, segfault), ucode errors.
sh_out('logread -l 400 2>/dev/null | grep -iE "kernel:.*(out of memory|killed process|oom-kill|segfault)|procd:.*dawnline|dawnline/agent" | tail -n 6'), 'Dawnline');
else if (stopped && time() - stopped < 300)
slog('dawnline', 'info', `restarted · version ${VERSION}`, null, 'Dawnline');
else if (live.uptime() < 600)
slog('dawnline', 'info', `started with the router · version ${VERSION}`, null, 'Dawnline');
else
slog('dawnline', 'info', `started · version ${VERSION}`, null, 'Dawnline');
uloop.init();
let timer;
timer = uloop.timer(1000, function() {
try {
tick();
}
catch (e) {
slog('dawnline', 'error', `background check failed: ${e.message}`, e.stacktrace?.[0]?.context, 'Dawnline');
}
timer.set(TICK);
});
let watcher;
watcher = uloop.timer(WATCH, function() {
step('watch', { who: 'Connections' }, watch);
watcher.set(WATCH);
});
if (type(uloop.signal) == 'function') {
uloop.signal('TERM', () => { shutdown(); uloop.end(); });
uloop.signal('INT', () => { shutdown(); uloop.end(); });
}
uloop.run();
shutdown();