Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- 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
231 lines
9.7 KiB
Ucode
231 lines
9.7 KiB
Ucode
// Dawnline — watchdog. The agent runs it every minute: it checks what keeps
|
|
// people connected and repairs what it can, waiting longer after each repair
|
|
// that did not help. What it finds goes to the service log (English, as
|
|
// logged); its state to RUN_DIR for the Health page, whose chip names are put
|
|
// in the admin's language when they are read (view()). A cron job
|
|
// (libexec/dawnline/heartbeat) looks after the agent itself.
|
|
|
|
import { stat } from 'fs';
|
|
import { _, L_ } from 'dawnline.i18n';
|
|
import { sh_out, q, read_json, write_json, slog, slog_read, RUN_DIR } from 'dawnline.util';
|
|
import * as apply from 'dawnline.apply';
|
|
import * as loop from 'dawnline.loop';
|
|
import * as health from 'dawnline.health';
|
|
import * as awg from 'dawnline.awg';
|
|
import * as nft from 'dawnline.nft';
|
|
import * as netifd from 'dawnline.netifd';
|
|
import * as upd from 'dawnline.updates';
|
|
|
|
export const STATE = `${RUN_DIR}/watchdog.json`;
|
|
/** The agent writes the time here on every tick. */
|
|
export const BEAT = `${RUN_DIR}/agent.beat`;
|
|
// Seconds before the next repair, by the number of repairs so far.
|
|
const WAIT = [ 0, 120, 600, 1800, 3600 ];
|
|
// Good minutes in a row before a repaired target's count of repairs is
|
|
// forgotten: one good minute between two failures is still flapping.
|
|
const SETTLED = 10;
|
|
|
|
/** Why a tunnel is down (AmneziaWG; kept here for older callers). */
|
|
export function iface_errors(ifname) {
|
|
return awg.iface_errors(ifname);
|
|
};
|
|
|
|
function restart_services() {
|
|
// procd keeps unchanged instances (the agent running this) and starts missing ones.
|
|
system('/etc/init.d/dawnline start >/dev/null 2>&1');
|
|
}
|
|
|
|
/**
|
|
* What to look at: { id (= the Health check id), src, name (chip, stored in English: view()
|
|
* shows names() by id), who (the log's subject), ok,
|
|
* problem, back, detail?, repair: { what, run }? }. The log reads "<who> <problem> - <what>"; its
|
|
* words are L_(): English in the log, translated where it is shown.
|
|
*/
|
|
function probes(m) {
|
|
let out = [];
|
|
// The tunnels check themselves: AmneziaWG (packages, kernel module, netifd) and each server's interface.
|
|
let tunnels = awg.probes(m, { udp_listening: health.udp_listening, reapply: () => apply.apply(), owner: (ifn) => netifd.owner(ifn) });
|
|
push(out, ...tunnels);
|
|
// A tunnel's interface edited outside Dawnline (LuCI, uci) keeps that until
|
|
// the next apply: set up again with Dawnline's settings. Only while
|
|
// AmneziaWG itself works (its target comes first, and is fixed first).
|
|
if (filter(tunnels, (p) => p.id == 'awg')[0]?.ok)
|
|
for (let id, diff in netifd.drift(m)) {
|
|
let srv = m.servers[id];
|
|
push(out, {
|
|
id: `cfg_${id}`, src: 'awg', name: `${srv.name} · interface settings`, who: srv.name, ok: !length(diff),
|
|
problem: sprintf(L_('interface %s was changed outside Dawnline'), srv.ifname),
|
|
back: sprintf(L_("interface %s has Dawnline's settings again"), srv.ifname),
|
|
// Option names only: their values hold the server's private key.
|
|
detail: () => sprintf(L_('Changed: %s.'), join(', ', diff)),
|
|
repair: { what: L_('setting it up again'), run: () => apply.apply() },
|
|
});
|
|
}
|
|
if (length(m.servers))
|
|
push(out, {
|
|
id: 'nft', src: 'dawnline', name: 'Access rules', who: L_('Access rules'), ok: nft.present(),
|
|
problem: L_('are missing'), back: L_('are loaded again'),
|
|
repair: { what: L_('loading them again'), run: () => apply.apply() },
|
|
});
|
|
|
|
if (length(filter(values(m.servers), (x) => x.private_key))) {
|
|
let fw = apply.firewall_drift(m);
|
|
push(out, {
|
|
id: 'fw', src: 'dawnline', name: 'Firewall', who: L_('Firewall'), ok: fw == null,
|
|
problem: fw == 'off' ? L_("isn't running on this router (fw4): tunnels and the portal are closed")
|
|
: fw == 'changed' ? L_("rules for Dawnline were changed or removed") : L_("rules for Dawnline aren't loaded"),
|
|
back: L_('rules for Dawnline are in place again'),
|
|
// fw4 stopped on purpose is the admin's call: report it, do not start it.
|
|
repair: fw == 'off' ? null : { what: L_('restoring them'), run: () => { apply.apply(); system('fw4 -q reload'); } },
|
|
});
|
|
}
|
|
|
|
let s = m.settings;
|
|
if (s.portal_enabled && !length(health.needs().https) && !length(health.tcp_port_owners(s.portal_port)))
|
|
push(out, {
|
|
id: 'portal', src: 'portal', name: 'Portal', who: L_('Personal portal'), ok: health.tcp_listening(s.portal_port),
|
|
problem: sprintf(L_("isn't answering on port %d"), s.portal_port), back: L_('answers again'),
|
|
detail: () => sh_out("logread -l 400 2>/dev/null | grep 'uhttpd' | grep -iE 'error|fail|cert|key|bind|address' | tail -n 4"),
|
|
repair: { what: L_('restarting it'), run: restart_services },
|
|
});
|
|
|
|
// Only while a server is on: with all of them off it idles on loopback.
|
|
if (stat(loop.RESOLVER.conf) && length(filter(values(m.servers), (x) => x.enabled && x.private_key)))
|
|
push(out, {
|
|
id: 'resolver', src: 'dawnline', name: 'Resolver', who: L_('DNS for Home-only devices'), ok: loop.resolver_running(),
|
|
problem: L_("isn't running"), back: L_('runs again'),
|
|
repair: { what: L_('restarting it'), run: restart_services },
|
|
});
|
|
|
|
push(out, {
|
|
id: 'rpc', src: 'dawnline', name: 'LuCI link', who: 'LuCI', ok: system('ubus list luci.dawnline >/dev/null 2>&1') == 0,
|
|
problem: L_("can't reach Dawnline (luci.dawnline is missing in rpcd)"), back: L_('reaches Dawnline again'),
|
|
detail: () => sh_out('logread -l 400 2>/dev/null | grep rpcd | tail -n 4'),
|
|
// A reload keeps LuCI logins; a restart (second try) signs everyone out —
|
|
// and after three it stops: restarting rpcd every hour for good would
|
|
// sign every admin out for good.
|
|
repair: { what: L_('reloading rpcd'), max: 3, run: (n) => system(n > 1 ? '/etc/init.d/rpcd restart >/dev/null 2>&1' : '/etc/init.d/rpcd reload >/dev/null 2>&1') },
|
|
});
|
|
return out;
|
|
}
|
|
|
|
/** One pass. With repairs off it only reports. */
|
|
export function run(m) {
|
|
let now = time();
|
|
let fix = m.settings.watchdog;
|
|
// An update is replacing packages or restarting tunnels: what looks broken
|
|
// meanwhile is being worked on; repairs would only get in its way.
|
|
let busy = upd.busy();
|
|
let st = read_json(STATE, {});
|
|
let t = type(st.targets) == 'object' ? st.targets : {};
|
|
let repairs = filter(st.repairs ?? [], (r) => now - r.ts < 86400);
|
|
let seen = {};
|
|
for (let p in probes(m)) {
|
|
seen[p.id] = true;
|
|
let x = t[p.id] ??= { bad: 0, tries: 0, next: 0 };
|
|
x.name = p.name;
|
|
x.ok = p.ok;
|
|
if (p.ok) {
|
|
if (x.reported)
|
|
slog(p.src, 'up', x.stuck ? sprintf(L_('%s · restored'), p.back)
|
|
: x.tries ? sprintf(x.tries > 1 ? L_('%s (after %d repairs)') : L_('%s (after %d repair)'), p.back, x.tries) : p.back, null, p.who);
|
|
x.bad = 0;
|
|
x.next = 0;
|
|
x.reported = false;
|
|
x.stuck = false;
|
|
// The repairs so far are forgotten only once it has stayed up a while:
|
|
// a target that fails every other minute keeps its back-off.
|
|
x.good = (x.good ?? 0) + 1;
|
|
if (x.good >= SETTLED) {
|
|
x.tries = 0;
|
|
x.good = 0;
|
|
}
|
|
continue;
|
|
}
|
|
x.good = 0;
|
|
// One bad minute can be a restart in progress.
|
|
if (++x.bad < 2)
|
|
continue;
|
|
if (busy)
|
|
continue;
|
|
if (p.repair?.max && x.tries >= p.repair.max && !x.reported) {
|
|
slog(p.src, 'error', sprintf(L_("%s - not repaired again (%d repairs didn't help)"), p.problem, p.repair.max), p.detail?.(), p.who);
|
|
x.reported = true;
|
|
continue;
|
|
}
|
|
if (!fix || !p.repair || (p.repair.max && x.tries >= p.repair.max)) {
|
|
if (!x.reported)
|
|
slog(p.src, 'error', p.problem, p.detail?.(), p.who);
|
|
x.reported = true;
|
|
continue;
|
|
}
|
|
// Three repairs did not help: say so once, in red, and from then on keep
|
|
// trying quietly until it works again (that is logged).
|
|
if (x.tries >= 3 && !x.stuck && now - (x.last ?? 0) >= 50) {
|
|
x.stuck = true;
|
|
slog(p.src, 'error', sprintf(L_("%s - %d repairs didn't help"), p.problem, x.tries), p.detail?.(), p.who);
|
|
}
|
|
if (now < x.next)
|
|
continue;
|
|
x.tries++;
|
|
x.last = now;
|
|
x.next = now + WAIT[min(x.tries, length(WAIT) - 1)];
|
|
x.reported = true;
|
|
if (!x.stuck)
|
|
slog(p.src, 'repair', sprintf(L_('%s - %s'), p.problem, p.repair.what), p.detail?.(), p.who);
|
|
try {
|
|
p.repair.run(x.tries);
|
|
}
|
|
catch (e) {
|
|
if (!x.stuck)
|
|
slog(p.src, 'error', sprintf(L_("couldn't be repaired: %s"), e.message), null, p.who);
|
|
}
|
|
if (!x.stuck)
|
|
push(repairs, { ts: now, id: p.id, name: p.name });
|
|
}
|
|
for (let id in keys(t))
|
|
if (!seen[id])
|
|
delete t[id];
|
|
write_json(STATE, { last: now, targets: t, repairs }, 0o644);
|
|
};
|
|
|
|
/**
|
|
* The chip names of probes() by target id, in the language of the call. The
|
|
* others (AmneziaWG's, "Home · AmneziaWG") are names only and stay as stored.
|
|
*/
|
|
function names() {
|
|
return {
|
|
agent: _('Agent'), nft: _('Access rules'), fw: _('Firewall'), portal: _('Portal'),
|
|
resolver: _('Resolver'), rpc: _('LuCI link'),
|
|
};
|
|
}
|
|
|
|
/** A target's chip name in the language of the call: names(), a server's interface settings, else as stored. */
|
|
function chip_name(m, n, id, stored) {
|
|
if (n[id])
|
|
return n[id];
|
|
let srv = substr(id, 0, 4) == 'cfg_' ? m.servers[substr(id, 4)] : null;
|
|
return srv ? sprintf(_('%s · interface settings'), srv.name) : stored;
|
|
}
|
|
|
|
/**
|
|
* For the Health page: the watchdog's state and the newest log entries. Target
|
|
* and repair names in the language of the call; the log as it was written.
|
|
*/
|
|
export function view(m) {
|
|
let st = read_json(STATE, {});
|
|
let beat = +(read_json(BEAT, 0) ?? 0);
|
|
let n = names();
|
|
// The agent itself: fine while it ticks (cron restarts it after 5 minutes without).
|
|
let targets = [ { id: 'agent', name: n.agent, ok: beat > 0 && time() - beat < 180, tries: 0 } ];
|
|
for (let id, x in (type(st.targets) == 'object' ? st.targets : {}))
|
|
push(targets, { id, name: chip_name(m, n, id, x.name), ok: !!x.ok, tries: x.tries ?? 0 });
|
|
return {
|
|
watchdog: {
|
|
enabled: m.settings.watchdog, last: st.last ?? null, beat: beat || null, now: time(),
|
|
targets, repairs: map(st.repairs ?? [], (r) => ({ ...r, name: chip_name(m, n, r.id, r.name) })),
|
|
},
|
|
log: slog_read(200),
|
|
};
|
|
};
|