CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Canceled after 0s
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Canceled after 0s
CI / ucode static checks (push) Canceled after 0s
CI / ShellCheck (push) Canceled after 0s
CI / Web (format, types, tests, bundles) (push) Canceled after 0s
Build packages / CI (push) Canceled after 0s
Build packages / Version (push) Canceled after 0s
Build packages / Build apk (push) Canceled after 0s
Build packages / Build ipk (push) Canceled after 0s
Build packages / Release (push) Canceled after 0s
- A device is renamed where its name stands on its card: a click on the
name, Return or Space on it, or Rename in the ⋯ menu turns it into a
field with the name selected. Return saves, and the words shimmer until
the router answers. Esc or a click elsewhere puts the old name back. The
rename window is gone, and the page no longer redraws under a name being
edited
- Device names take up to 20 characters: letters of any language, digits,
spaces and - _ . The router refuses anything else when a device is added
or renamed, so a name can't carry symbols into configurations, file
names or the AmneziaVPN key. The portal and the admin turn a character
like that away as it is typed: the field shakes, its outline goes red
and a short line says why. Suggested names (a phone's model, "Phone 2")
are stripped of such characters and cut to fit. Names set before this
stay as they are
- LAN Bypass: Add a device, in the portal and in the admin, has a switch
that keeps the device's own local network (printer, TV, NAS) reachable
while it is connected. It is off by default and stays as chosen; the
admin's device drawer shows it. With home and internet access the switch
says which addresses still lead to the home network
- Health warns when a server's home network is on one of the subnets home
routers come with (192.168.0.x, 192.168.1.x and others). People with
home access who connect from a network on the same addresses reach your
home instead of their own, LAN Bypass or not
- The router tells when a device can't connect properly. After the problem
lasts three minutes, the portal card and the admin drawer name it:
can't finish connecting (the network may block it), connected but no
data (an outdated configuration, and the card offers Reconnect), on two
devices at once, or a connection that keeps dropping. Activity lists
these under Problems, one line per device per hour, and says when the
device connects normally again
- The set-up sheet's connection line follows what the device does:
waiting, can't finish, connected, or connected but no data. The sheet
closes by itself only once data comes through. On the device itself and
for a router, the steps regroup to what fixes the problem: "Set it up
again" when the handshake never finishes, "Add the connection again"
("Import the configuration again" for a router) when no data comes
- Set-up in the portal: a QR code too long for a camera to read gives its
place to a code that opens the set-up link (a LAN Bypass configuration
or a long AmneziaVPN key can be that long). A TV, box or NAS added from
a phone gets the steps for setting up from another device. Back from
"Open on the device" keeps the kind, LAN Bypass and the name typed. The
packet-size note under the kinds is gone
- The portal on phones keeps its place: nothing jumps when an access row
opens or folds, a notice comes or goes, or Safari redraws the cards. A
reload, or coming back from the VPN app, opens where the page was. Cards
glide to their new height, and an animation interrupted half-way goes on
from where it is instead of snapping. A double click no longer closes
the sheet it just opened
- In the admin, a device's drawer has its own MTU and keep-alive (empty
means the server's), each with a Change dialog. A change touches only
that device's configuration and restarts nothing on the router. Opening
the drawer no longer counts as handing out the configuration: "Update
config" stays until the new configuration is shown as a QR code,
downloaded or copied ("Show the new configuration"). New keys set the
device up again, as the portal's Reconnect does
- "Speed now" on a server's Overview is live: one point a second over the
last two minutes, all there as soon as the Overview opens. The agent
keeps those two minutes in memory and writes them out only while someone
is looking
- Disguise values that AmneziaWG refuses or crashes on are stopped before
they reach the kernel. That covers overlapping H1-H4, Jmin above Jmax,
broken I1-I5 tags, padding under 12 with header protection, and more.
Import and restore refuse them. After a hand edit in uci, the server's
tunnel stays down and Health and the service log say why and what to do.
Errors are full sentences in the reader's language
- The header protection key no longer shows in the status that read-only
users can see: the Server page says only that the key is set, and each
device's configuration still has it. The server no longer turns off
WireGuard's protection against handshake floods (devices keep
DisableCookies). A device can't be added with a server's own key
- An AmneziaWG update whose kernel module can't be swapped (another
AmneziaWG interface holds the old one) no longer fails. It is installed,
the tunnels come back on the old module, and Health and Components say
the update starts after a reboot. On apk firmware, an install that apk
left at the old version counts as failed. A module installed by hand is
recorded, so after a firmware upgrade Health can tell that it is gone
- Dawnline reads only its own tunnels, each one separately. Another
AmneziaWG interface on the router no longer slows the agent's
once-a-second check, has its keys read or breaks Dawnline's reads. A
portal request reads only that person's server: one process however many
servers there are
- Fewer false disconnects: a tunnel that netifd sets up again no longer
logs everyone as leaving and coming back, and a device with a long
keep-alive stays online between handshakes. One configuration on two
devices isn't logged as roaming every few seconds, and new keys end the
old session at once. A tunnel that is missing devices is synced within a
minute, and traffic across a tunnel restart is no longer undercounted
- Renaming a server no longer restarts its tunnel. If a tunnel's interface
is edited outside Dawnline (LuCI, uci), the watchdog sets it up again
with Dawnline's settings and names only the changed options. The routing
rules for "Internet like a home device" change one at a time, new ones
first, so traffic never falls between them. Health and the Server page
warn when the address's AAAA record isn't this router's, or the router
has no public IPv6: devices that prefer IPv6 fail to connect
- The README screenshots come in English and Russian; the portal's are
taken from its demo. The demo has a switch beside × in the set-up sheet
to show each state of the connection line. In Russian, long labels in
the admin's wide option buttons wrap under their icon. DESIGN.md
describes the switch, renaming on the card, the shimmer, the shake and
how the portal scrolls. New tests cover link states, the meter, access,
Health, firewall rules, network settings and packages
909 lines
33 KiB
Plaintext
Executable File
909 lines
33 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). The same read tells when
|
|
// a device's link fails in a named way (link.json, dawnline.link), what it does
|
|
// right now for the portal's set-up sheet (setup.json) and the live meter's
|
|
// last two minutes, written out while someone looks (meter.json, dawnline.meter).
|
|
// 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, date_end, 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';
|
|
import * as builds from 'dawnline.awg_builds';
|
|
import * as link from 'dawnline.link';
|
|
import * as meter from 'dawnline.meter';
|
|
import { L_ } from 'dawnline.i18n';
|
|
|
|
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, {});
|
|
// What a link showed comes from the watcher's memory, which starts empty: a
|
|
// state shows again once it held (link.HOLD), a set-up state with the next
|
|
// read that tells it. The meter's points start again too, written at the next look.
|
|
write_json(live.LINK, {}, 0o600);
|
|
write_json(live.SETUP, {}, 0o600);
|
|
meter.stop();
|
|
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;
|
|
// Per server, how the agent's last sync of its devices went (dawnline.awg.sync_peers).
|
|
let last_peers = {};
|
|
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: irreversible, so never
|
|
// on a clock that has not been set yet (a router before NTP is in 1970).
|
|
if (core.person_expired(p, ts) && p.expire_action == 'remove' && length(p.devices) && localtime(ts).year >= 2025) {
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** 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),
|
|
// and the public key it was seen with: new keys (Reconnect, New keys) end
|
|
// the old session, whatever its handshake said.
|
|
let last_hs = {}, last_key = {};
|
|
// The model, read again only when the config file changes (the watcher runs
|
|
// every second), the tunnels it reads (live.tunnel_ifnames: a regex per
|
|
// server, compiled each time it runs, so not every second), each device's
|
|
// online window (live.online_windows: its keep-alive's, from the model) and
|
|
// the largest keepalive its server's disguise makes (link.keepalive_sizes).
|
|
let wm = null, wm_sig = null, wm_ifnames = [], wm_windows = {}, wm_ka = {};
|
|
// netifd deletes and adds a tunnel's link each time it sets it up (a new
|
|
// ifindex): the devices come back only with the sync its hotplug starts, and
|
|
// then with no handshake until each one makes a new one. For this long after
|
|
// the link changed, a device missing from it keeps what was known of it,
|
|
// rather than a burst of disconnects and connects for everyone online.
|
|
const RESTART_GRACE = 45;
|
|
// Per tunnel: the link last seen (null while it is down), and until when its grace lasts.
|
|
let links = {}, grace = {};
|
|
|
|
/** A file's mtime and inode ('-' when it is missing): what the cached model depends on. */
|
|
function file_sig(path) {
|
|
let st = stat(path);
|
|
return st ? `${st.mtime}/${st.inode}` : '-';
|
|
}
|
|
|
|
function watch_model() {
|
|
// uci commits replace the file: a new inode even within the same second.
|
|
// Which servers are kept down (core.BLOCKED, written by apply) is in the
|
|
// model too: their interface names may be someone else's tunnels.
|
|
let sig = `${file_sig(CONFIG_FILE)} ${file_sig(core.BLOCKED)}`;
|
|
if (wm == null || sig != wm_sig) {
|
|
wm = core.load();
|
|
wm_sig = sig;
|
|
wm_ifnames = live.tunnel_ifnames(wm);
|
|
wm_windows = live.online_windows(wm);
|
|
wm_ka = link.keepalive_sizes(wm);
|
|
}
|
|
return wm;
|
|
}
|
|
|
|
// Per device: how its link behaves (dawnline.link: a few recent events and
|
|
// what is shown), and whether what is shown (link.json) or the set-up states
|
|
// (setup.json) changed since they were written.
|
|
let link_hist = {}, link_dirty = false, setup_dirty = false;
|
|
|
|
/**
|
|
* A device's read into its link history: its set-up state follows at once
|
|
* (link.setup, a few compares unless something happened); it is classified
|
|
* again when something happened or link.CHECK_EVERY passed (its spans age).
|
|
* Returns the change in what is shown (link.settle: { from, to }), or null.
|
|
*/
|
|
function watch_link(id, dev, peer, now, on) {
|
|
let h = link_hist[id] ??= link.fresh(now);
|
|
let moved = false;
|
|
if (peer)
|
|
moved = link.observe(h, peer, now, wm_ka[id] ?? link.KEEPALIVE);
|
|
else
|
|
link.gone(h);
|
|
if (link.setup(h, now, on, moved))
|
|
setup_dirty = true;
|
|
if (!moved && now - h.checked < link.CHECK_EVERY)
|
|
return null;
|
|
h.checked = now;
|
|
link.prune(h, now);
|
|
h.raw = link.classify(h, now, on);
|
|
let change = link.settle(h, h.raw, now, on);
|
|
if (change)
|
|
link_dirty = true;
|
|
return change;
|
|
}
|
|
|
|
// The live meter: the last sample and the last two minutes of points
|
|
// ([ ts, JSON ], newest last, meter.keep), kept whether anyone looks or not so
|
|
// that a look gets them at once; whether meter.json is out for a look now.
|
|
let meter_prev = null, meter_ring = [], meter_out = false;
|
|
|
|
/**
|
|
* A meter point a second from this read, kept; meter.json written while
|
|
* someone looks (meter.wanted): all the points the first second of a look,
|
|
* then with each new one. Once nobody does, the file goes; the points stay.
|
|
*/
|
|
function watch_meter(m, d, now) {
|
|
let s = meter.sample(m, d, now);
|
|
let p = meter_prev ? meter.point(meter_prev, s) : null;
|
|
meter_prev = s;
|
|
if (p)
|
|
meter.keep(meter_ring, p);
|
|
if (!meter.wanted(now)) {
|
|
if (meter_out)
|
|
meter.stop();
|
|
meter_out = false;
|
|
return;
|
|
}
|
|
if (p || !meter_out)
|
|
meter.save(meter_ring);
|
|
meter_out = true;
|
|
}
|
|
|
|
/** 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);
|
|
}
|
|
|
|
/* ---------- locations ---------- */
|
|
|
|
// A lookup run: this many addresses at most, within this many seconds; a run
|
|
// still going after GEO_KILL seconds (past every timeout) is given up.
|
|
const GEO_PER_RUN = 3;
|
|
const GEO_BUDGET = 12;
|
|
const GEO_KILL = 60;
|
|
|
|
let last_home = 0;
|
|
// Addresses the watcher saw connect or roam that wait for a run.
|
|
let geo_pending = {};
|
|
// The run in a child process (uloop.task) and when it started; null when none.
|
|
let geo_task = null, geo_since = 0;
|
|
|
|
/** Every client address still shown on the map (the cache never pushes these out). */
|
|
function geo_shown() {
|
|
let ips = [];
|
|
for (let id, rec in usage.devices)
|
|
for (let ip in keys(rec.ips ?? {}))
|
|
push(ips, ip);
|
|
return uniq(ips);
|
|
}
|
|
|
|
/*
|
|
* A run's results: into the cache (the agent is its only writer), onto the
|
|
* connections in the activity log that were logged before their place was
|
|
* known, and to open pages at once: the revision moves for the people seen
|
|
* from those addresses.
|
|
*/
|
|
function geo_done(found) {
|
|
let cache = geo.load();
|
|
if (type(found) != 'object' || !geo.merge(cache, found, geo_shown()))
|
|
return;
|
|
geo.save(cache);
|
|
let where = {};
|
|
for (let ip, r in found)
|
|
if (r)
|
|
where[ip] = geo.label(r);
|
|
if (!length(keys(where)))
|
|
return;
|
|
let m = watch_model(), people = {};
|
|
for (let id, rec in usage.devices)
|
|
for (let ip in keys(where))
|
|
if (rec.ips?.[ip] && m.devices[id])
|
|
people[m.devices[id].person] = true;
|
|
live.set_places(where, time() - 3600);
|
|
save_presence(keys(people));
|
|
}
|
|
|
|
/*
|
|
* Locate client addresses for the admin map in a child process: a lookup
|
|
* waits on the network for seconds, and neither the 1-second watcher nor the
|
|
* tick may wait with it. Addresses that just connected go first, at once
|
|
* (the watcher calls this on a connect); the tick adds anything still shown
|
|
* that is missing or stale (`more`). One run at a time, a few addresses each;
|
|
* what just connected and did not fit waits for the next run.
|
|
*/
|
|
function geo_lookup(m, more) {
|
|
if (!m.settings.geo_enabled) {
|
|
geo_pending = {};
|
|
return;
|
|
}
|
|
if (geo_task && !geo_task.finished()) {
|
|
if (time() - geo_since < GEO_KILL)
|
|
return;
|
|
geo_task.kill();
|
|
}
|
|
geo_task = null;
|
|
let todo = geo.stale(geo.load(), [ ...keys(geo_pending), ...(more ?? []) ]);
|
|
let want = slice(todo, 0, GEO_PER_RUN);
|
|
let rest = {};
|
|
for (let ip in slice(todo, GEO_PER_RUN))
|
|
if (geo_pending[ip])
|
|
rest[ip] = true;
|
|
geo_pending = rest;
|
|
if (!length(want))
|
|
return;
|
|
let provider = m.settings.geo_provider;
|
|
let t = null;
|
|
t = uloop.task(() => {
|
|
// The child shares the instance lock: it must not keep it past the agent (a restart would find it taken).
|
|
instance.close();
|
|
return geo.lookup_all(provider, want, GEO_PER_RUN, GEO_BUDGET);
|
|
}, (found) => {
|
|
if (geo_task == t)
|
|
geo_task = null;
|
|
geo_done(found);
|
|
if (length(keys(geo_pending)))
|
|
geo_lookup(watch_model());
|
|
});
|
|
geo_task = t;
|
|
geo_since = time();
|
|
}
|
|
|
|
/** The tick: shown addresses missing or stale get a run; the router's own place about hourly. */
|
|
function check_geo(m) {
|
|
geo_lookup(m, geo_shown());
|
|
if (!m.settings.geo_enabled || time() - last_home <= 3600)
|
|
return;
|
|
last_home = time();
|
|
let cache = geo.load();
|
|
let wan = live.iface_status('wan')?.['ipv4-address']?.[0]?.address;
|
|
if (geo.resolve_home(m.settings, cache, wan))
|
|
geo.save(cache);
|
|
}
|
|
|
|
function place_of(ip) {
|
|
return geo.label(geo.place(geo.load(), ip));
|
|
}
|
|
|
|
/*
|
|
* One look at the tunnels. Connects (a new handshake from a device that was
|
|
* away), disconnects (its session expired: no handshake within its online
|
|
* window, 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 Dawnline's own tunnels are read, each on its own (one process with a
|
|
* single server): a tunnel that could not be read says nothing about its
|
|
* devices, which stay as they were.
|
|
*/
|
|
function watch() {
|
|
let m = watch_model();
|
|
let d = awg.dump(wm_ifnames);
|
|
let now = time();
|
|
for (let ifn, r in d.reads) {
|
|
if (r == 'error')
|
|
continue;
|
|
let ix = d.ifaces[ifn]?.ifindex;
|
|
if (ifn in links && links[ifn] != ix)
|
|
grace[ifn] = now + RESTART_GRACE;
|
|
links[ifn] = ix;
|
|
}
|
|
for (let ifn in keys(links))
|
|
if (!(ifn in d.reads)) {
|
|
delete links[ifn];
|
|
delete grace[ifn];
|
|
}
|
|
let events = [], people = {}, ips = [], failing = [];
|
|
for (let id, dev in m.devices) {
|
|
let ifn = m.servers[m.people[dev.person]?.server]?.ifname;
|
|
if (d.reads[ifn] == 'error')
|
|
continue;
|
|
let peer = d.peers[dev.public_key];
|
|
let ep = '';
|
|
// New keys: offline until they shake hands, and a new link history. The
|
|
// old one's set-up session (its no data, or its data flowing) and its
|
|
// answers would otherwise be taken for the new configuration's.
|
|
if (last_key[id] != dev.public_key) {
|
|
if (last_key[id] != null) {
|
|
last_hs[id] = 0;
|
|
let h = link_hist[id];
|
|
if (h?.state != null) {
|
|
link_dirty = true;
|
|
people[dev.person] = true;
|
|
}
|
|
if (h?.setup != null)
|
|
setup_dirty = true;
|
|
delete link_hist[id];
|
|
}
|
|
last_key[id] = dev.public_key;
|
|
}
|
|
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. Unless its
|
|
// tunnel was just set up again and the sync has not brought it back yet.
|
|
else if (!(grace[ifn] > now))
|
|
last_hs[id] = 0;
|
|
let on = last_hs[id] > 0 && now - last_hs[id] < (wm_windows[id] ?? live.ONLINE_WINDOW);
|
|
let was = last_online[id];
|
|
// What is shown changed: the person's pages read it again (link.json), also
|
|
// when it cleared without an event (the device stopped trying, or was paused).
|
|
let change = watch_link(id, dev, peer, now, on);
|
|
if (change) {
|
|
people[dev.person] = true;
|
|
let le = link.event_for(change, dev, on);
|
|
if (le)
|
|
push(failing, le);
|
|
}
|
|
// One configuration on two devices: its address goes back and forth all
|
|
// the time, which is no roaming (nor a reason to move the revision).
|
|
let flapping = link_hist[id].raw == 'shared' || link_hist[id].state == 'shared';
|
|
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] && !flapping) {
|
|
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] && !flapping))
|
|
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];
|
|
delete last_key[id];
|
|
}
|
|
for (let id in keys(link_hist))
|
|
if (!m.devices[id]) {
|
|
if (link_hist[id].state != null)
|
|
link_dirty = true;
|
|
if (link_hist[id].setup != null)
|
|
setup_dirty = true;
|
|
delete link_hist[id];
|
|
}
|
|
if (link_dirty) {
|
|
write_json(live.LINK, link.shown(link_hist), 0o600);
|
|
link_dirty = false;
|
|
}
|
|
// The portal's set-up sheet polls for it: no revision moves with it.
|
|
if (setup_dirty) {
|
|
write_json(live.SETUP, link.setups(link_hist), 0o600);
|
|
setup_dirty = false;
|
|
}
|
|
// Merged per device for an hour: a link that keeps failing and coming back is one line.
|
|
for (let e in failing)
|
|
live.add_event_merged(e, 3600);
|
|
watch_meter(m, d, now);
|
|
if (!length(keys(people)))
|
|
return;
|
|
save_presence(keys(people));
|
|
if (!length(events))
|
|
return;
|
|
live.collect(m, usage, d);
|
|
live.save_usage(usage, false);
|
|
// The event carries the place when the cache already knows it. A new
|
|
// address is looked up at once, in a child process (geo_lookup): its place
|
|
// goes onto this event and the revision moves again when it is known.
|
|
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));
|
|
if (m.settings.geo_enabled && length(ips)) {
|
|
for (let ip in ips)
|
|
geo_pending[ip] = true;
|
|
geo_lookup(m);
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
/*
|
|
* End dates in the activity log: a heads-up once per end date when its
|
|
* warning window opens, and "keep access" passing it (which changes no
|
|
* state, so nothing else would say it). Not on a clock that is not set.
|
|
*/
|
|
function check_expiry(m, events) {
|
|
let ts = time();
|
|
if (localtime(ts).year < 2025)
|
|
return;
|
|
for (let id, p in m.people) {
|
|
if (!p.expires)
|
|
continue;
|
|
let end = date_end(p.expires);
|
|
if (end == null)
|
|
continue;
|
|
let left = end > ts ? int((end - ts) / 86400) + 1 : 0;
|
|
if (left > 0 && p.expire_warn > 0 && left <= p.expire_warn && notes[`warned:${id}`] != p.expires) {
|
|
note(`warned:${id}`, p.expires);
|
|
push(events, { type: 'expiring', person: id, detail: `${left} day${left == 1 ? '' : 's'}` });
|
|
}
|
|
if (left == 0 && p.expire_action == 'notify' && notes[`kept:${id}`] != p.expires) {
|
|
note(`kept:${id}`, p.expires);
|
|
push(events, { type: 'expired_kept', person: id, detail: p.expires });
|
|
}
|
|
}
|
|
}
|
|
|
|
/** "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', sprintf(L_('certificate renewed · valid until %s'), cert_end(c.cert) ?? '?'), null, L_('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) {
|
|
let end = cert_end(c.cert);
|
|
slog('portal', 'error', end ? sprintf(L_('certificate expires %s'), end) : L_('certificate expires within 7 days'),
|
|
L_('Browsers will refuse the portal after that. Renew it with ACME, or choose another certificate in Settings.'), L_('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', sprintf(L_('public address changed · %s → %s'), was, ip),
|
|
sprintf(length(stuck) == 1
|
|
? L_('%s still gives devices the old address: set the public address in the server settings (a DDNS name keeps working), then devices need new configurations.')
|
|
: L_('%s still give devices the old address: set the public address in the server settings (a DDNS name keeps working), then devices need new configurations.'),
|
|
join(', ', stuck)), L_('Router'));
|
|
else
|
|
slog('dawnline', 'info', sprintf(L_('public address changed · %s → %s'), was, ip), null, L_('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) {
|
|
// Tried again tomorrow; meanwhile the admin should know (a full flash, mostly).
|
|
slog('dawnline', 'error', sprintf(L_('nightly backup failed: %s'), e.message), null, L_('Backup'));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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), the
|
|
* access table is gone (a flushed ruleset: the firewall alone would let every
|
|
* device reach everything, so the sync unloads them until it is back), a
|
|
* running tunnel holds other devices than it should (check_peers), 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 || (length(t.m.servers) && !apply.table_present())) {
|
|
last_peers = apply.sync(t.m, usage).peers ?? {};
|
|
last_sync = time();
|
|
}
|
|
conf_mtime = mt;
|
|
last_states = t.states;
|
|
}
|
|
|
|
/*
|
|
* A running tunnel without all of its devices, or with ones it should not
|
|
* have (set up again before its hotplug sync ran, an `awg set` by hand): the
|
|
* sync runs on this tick, not up to RESYNC_EVERY later. By the same choice
|
|
* of devices the sync makes (and Health compares); only where the agent's
|
|
* last sync took: a server that refuses its devices has said so in the
|
|
* service log and keeps the slower pace.
|
|
*/
|
|
function check_peers(t) {
|
|
for (let id, srv in t.m.servers) {
|
|
let last = last_peers[id] ?? 'ok';
|
|
if (t.dmp?.reads?.[srv.ifname] != 'read' || (last != 'ok' && last != 'down'))
|
|
continue;
|
|
let diff = awg.peer_diff(awg.tunnel_devices(t.m, id, t.states), t.dmp, srv.ifname);
|
|
if (diff.missing || diff.extra)
|
|
last_sync = 0;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 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', L_('network changed - Dawnline set itself up again'), null, L_('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: L_('Traffic'), run: (t) => {
|
|
t.reset = check_usage_reset(t.m);
|
|
t.dmp = live.dump(t.m);
|
|
live.collect(t.m, usage, t.dmp);
|
|
} },
|
|
{ who: L_('Speed'), quiet: true, run: (t) => record_rate(t.m) },
|
|
{ who: L_('People'), run: (t) => {
|
|
t.states = apply.states_for(t.m, usage);
|
|
check_people(t.m, t.states, t.events);
|
|
check_expiry(t.m, t.events);
|
|
} },
|
|
{ who: L_('Access rules'), run: (t) => {
|
|
if (!t.states)
|
|
return;
|
|
check_peers(t);
|
|
sync_runtime(t);
|
|
} },
|
|
// Tunnels going down and up are in the service log (hotplug, watchdog), with names and reasons.
|
|
{ who: L_('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: L_('Internet like a home device'), run: (t) => apply.sync_loop(t.m, usage) },
|
|
{ who: L_('Router'), run: check_network },
|
|
{ who: L_('Watchdog'), run: (t) => watchdog.run(core.load()) },
|
|
{ who: L_('Personal portal'), run: (t) => check_cert(t.m) },
|
|
{ who: L_('Router'), quiet: true, run: (t) => check_wan(t.m) },
|
|
{ who: L_('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', sprintf(L_('failed: %s'), 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 put other counters on flash (dawnline.backup leaves
|
|
// a marker): start over from them, whatever the watcher wrote since.
|
|
if (stat(live.USAGE_RELOAD)) {
|
|
unlink(live.USAGE_RELOAD);
|
|
usage = live.load_usage(true);
|
|
}
|
|
else if (!stat(`${RUN_DIR}/usage.json`) && usage.updated)
|
|
usage = live.load_usage();
|
|
let t = { m: null, pol: null, reset: false, events: [], dmp: null };
|
|
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: L_('Traffic') }, () => {
|
|
live.save_usage(usage, persist);
|
|
if (persist) {
|
|
last_flush = time();
|
|
geo.flush();
|
|
}
|
|
});
|
|
step('slog', { who: 'Dawnline', quiet: true }, () => slog_trim(persist));
|
|
step('events', { who: L_('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();
|
|
geo.flush();
|
|
slog_trim(true);
|
|
// The live meter's file goes with it: a stopped agent's points are no speed now.
|
|
meter.stop();
|
|
// 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();
|
|
// postupdate put this (kept) version back because the new one did not come up.
|
|
let rolled = trim(readfile(`${RUN_DIR}/update-rolled-back`) ?? '');
|
|
unlink(`${RUN_DIR}/update-rolled-back`);
|
|
if (rolled)
|
|
slog('dawnline', 'error', sprintf(L_('update to %s failed its check - rolled back to %s'), rolled, VERSION),
|
|
L_('The new version did not answer in LuCI or its agent did not start. Try again from Components, or install it with install.sh to see why.'), 'Dawnline');
|
|
else if (was && was != VERSION)
|
|
slog('dawnline', 'up', sprintf(L_('updated · %s → %s'), was, VERSION), null, 'Dawnline');
|
|
else if (crashed)
|
|
slog('dawnline', 'repair', L_('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', sprintf(L_('restarted · version %s'), VERSION), null, 'Dawnline');
|
|
else if (live.uptime() < 600)
|
|
slog('dawnline', 'info', sprintf(L_('started with the router · version %s'), VERSION), null, 'Dawnline');
|
|
else
|
|
slog('dawnline', 'info', sprintf(L_('started · version %s'), VERSION), null, 'Dawnline');
|
|
// A module Dawnline did not install has no record of its kernel: without one
|
|
// Health cannot tell a firmware upgrade from a missing module.
|
|
builds.adopt();
|
|
|
|
uloop.init();
|
|
|
|
let timer;
|
|
timer = uloop.timer(1000, function() {
|
|
try {
|
|
tick();
|
|
}
|
|
catch (e) {
|
|
slog('dawnline', 'error', sprintf(L_('background check failed: %s'), e.message), e.stacktrace?.[0]?.context, 'Dawnline');
|
|
}
|
|
timer.set(TICK);
|
|
});
|
|
|
|
let watcher;
|
|
watcher = uloop.timer(WATCH, function() {
|
|
step('watch', { who: L_('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();
|