Dawnline 0.2.0

- Diagnostics page: Health and Log tabs in place of Health and Activity
- Watchdog: repairs the AmneziaWG module, tunnels, nft table, firewall
  rules, uhttpd portal setting, portal, resolver and rpcd after two bad
  checks in a row, with backoff; after 3 failed repairs logs once in red,
  keeps repairing quietly and logs "restored" when it works again
- Heartbeat cron job restarts a hung agent
- Service log: tunnel and service starts with reasons (restarted, router
  start, update, crash), stops Dawnline made itself vs unexpected ones,
  certificate renewal and expiry, WAN address changes
- Admin actions logged as "You ..." with their own icon and tint; every
  event has an icon tile; names kept for removed people and devices;
  server pill on events when there are several servers
- Log filters: All, Connections, People, System, Problems; search and the
  command palette removed
- Home network reaches people's devices: fw4 forwarding from the home
  zones, limited to people with home access
- "Devices reach each other" per server, in the Access card
- Portal: "This device" card first with a dotted border, key rotation and
  copy address in the menu, devices sorted Online/Offline/Not set up/Paused,
  over-limit slots in orange
- People cards: slot bar, Online / Never connected, add chip always shown
- Person and device pages: Add device tile, Connect at the bottom with
  File/Key/QR tabs, status text without dots; pulsing server tab dot
- Server pill: Running / Not running / Turned off
This commit is contained in:
Aemeath committed 2026-10-15 17:05:24 +03:00
1 parent d0955e44b8
commit 82ab86848e
50 files changed
+2166 -982

No files matched your search

@@ -10,7 +10,7 @@ import { cursor } from 'uci';
import { readfile, writefile, stat, unlink } from 'fs';
import {
q, sh, sh_out, sh_bg, ensure_dir, write_file, write_json, read_json, parse_cidr, int2ip, uniq,
lock, unlock, RUN_DIR
lock, unlock, slog, iface_intent, RUN_DIR
} from 'dawnline.util';
import * as core from 'dawnline.core';
import * as net from 'dawnline.net';
@@ -26,8 +26,8 @@ const HOSTS_FILE = '/tmp/hosts/dawnline';
*/
const PREF_REPLY = 90;
function log(msg) {
system(`logger -t dawnline ${q(msg)}`);
function log(who, msg, level, detail) {
slog('dawnline', level ?? 'info', msg, detail, who);
}
/* ---------- helpers ---------- */
@@ -103,20 +103,30 @@ function sync_network(m) {
want[ifn] = true;
let addr = core.server_address(srv);
let pc = parse_cidr(srv.subnet);
let before = changed;
changed = ensure_section(c, 'network', ifn, 'interface') || changed;
changed = put(c, 'network', ifn, 'proto', 'amneziawg') || changed;
changed = put(c, 'network', ifn, 'private_key', srv.private_key) || changed;
changed = put(c, 'network', ifn, 'listen_port', srv.port) || changed;
changed = put(c, 'network', ifn, 'addresses', [ `${addr}/${pc.prefix}` ]) || changed;
changed = put(c, 'network', ifn, 'mtu', srv.mtu) || changed;
changed = put(c, 'network', ifn, 'disabled', srv.enabled ? null : '1') || changed;
changed = put(c, 'network', ifn, 'auto', srv.enabled ? null : '0') || changed;
changed = put(c, 'network', ifn, '_dawnline', id) || changed;
// Per server: with several, each changed one has to be brought up again.
let mine = ensure_section(c, 'network', ifn, 'interface');
let set = (opt, val) => {
if (put(c, 'network', ifn, opt, val))
mine = true;
};
set('proto', 'amneziawg');
set('private_key', srv.private_key);
set('listen_port', srv.port);
set('addresses', [ `${addr}/${pc.prefix}` ]);
set('mtu', srv.mtu);
set('disabled', srv.enabled ? null : '1');
set('auto', srv.enabled ? null : '0');
set('_dawnline', id);
// For the log once the server is gone.
set('_dawnline_name', srv.name);
for (let k in core.OBF_KEYS)
changed = put(c, 'network', ifn, `awg_${k}`, srv.obf[k]) || changed;
if (changed != before)
set(`awg_${k}`, srv.obf[k]);
if (mine) {
changed = true;
push(touched, ifn);
// The hotplug log says why the tunnel goes down (not a failure).
iface_intent(ifn, srv.enabled ? 'settings' : 'off', srv.name);
}
}
// Remove interfaces Dawnline created for servers that no longer exist.
@@ -126,6 +136,7 @@ function sync_network(m) {
push(stale, s['.name']);
});
for (let ifn in stale) {
iface_intent(ifn, 'removed', c.get('network', ifn, '_dawnline_name') ?? ifn);
system(`ifdown ${q(ifn)} 2>/dev/null`);
c.delete('network', ifn);
changed = true;
@@ -138,7 +149,8 @@ function sync_network(m) {
for (let ifn in touched)
if (m.servers[c.get('network', ifn, '_dawnline')]?.enabled)
system(`ifup ${q(ifn)}`);
log(`network updated (${join(', ', touched) || 'removed ' + join(', ', stale)})`);
// Each tunnel's own line (restarting, turned off, removed) comes from hotplug.
system(`logger -t dawnline ${q('network updated: ' + join(', ', [ ...touched, ...stale ]))}`);
}
return changed;
}
@@ -147,7 +159,8 @@ function sync_network(m) {
const LOOP_ZONE = 'dl_loop';
function sync_firewall(m) {
/** With `dry`, only says whether the firewall config differs from what Dawnline wants. */
function sync_firewall(m, dry) {
let c = cursor();
c.load('firewall');
let changed = false, want = {};
@@ -168,14 +181,20 @@ function sync_firewall(m) {
let sn = net.server_net(srv, snap);
portal_zone ??= sn.wan.zone;
sec(`${z}_zone`, 'zone', {
// Forward within the zone (device to device) is decided by the inet dawnline table.
name: srv.ifname, network: [ srv.ifname ],
input: 'ACCEPT', output: 'ACCEPT', forward: 'REJECT',
input: 'ACCEPT', output: 'ACCEPT', forward: 'ACCEPT',
});
// Access per person is enforced by the inet dawnline table; the zone
// only needs to be allowed to reach each home-network zone and WAN.
// The home network may open connections to the devices too (a router
// or a NAS on the tunnel); their answers pass the table as established.
for (let i, zone in sn.zones)
if (zone != sn.wan.zone && zone != srv.ifname)
sec(i == 0 ? `${z}_lan` : `${z}_lan_${replace(zone, /[^A-Za-z0-9_]/g, '_')}`, 'forwarding', { src: srv.ifname, dest: zone });
if (zone != sn.wan.zone && zone != srv.ifname) {
let sfx = i == 0 ? '' : `_${replace(zone, /[^A-Za-z0-9_]/g, '_')}`;
sec(`${z}_lan${sfx}`, 'forwarding', { src: srv.ifname, dest: zone });
sec(`${z}_from_lan${sfx}`, 'forwarding', { src: zone, dest: srv.ifname });
}
sec(`${z}_wan`, 'forwarding', { src: srv.ifname, dest: sn.wan.zone });
if (srv.lan_like) {
sec(`${z}_loop`, 'forwarding', { src: srv.ifname, dest: LOOP_ZONE });
@@ -232,12 +251,14 @@ function sync_firewall(m) {
c.delete('firewall', s);
changed = true;
}
if (dry)
return changed;
if (changed) {
c.commit('firewall');
ack_config([ 'firewall' ]);
system('fw4 -q reload');
log('firewall updated');
system("logger -t dawnline 'firewall updated'");
}
return changed;
}
@@ -313,11 +334,32 @@ function nft_table(m, sets) {
// DNS through the netns (it forwards to the LAN's DNS), for everyone.
push(t, ` ip daddr ${dns_addr} meta l4proto { tcp, udp } th dport 53 accept`);
push(t, ' ip daddr @lan_nets ip saddr @lan_ok accept');
// Devices with home access are part of home: they reach each other, where the
// server allows it. Within one server: the firewall has no forwarding between
// different servers' zones.
for (let id, srv in m.servers)
if (srv.private_key && srv.device_mesh)
push(t, ` iifname "${srv.ifname}" ip saddr @lan_ok ip daddr @lan_ok accept`);
push(t, ' meta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept');
push(t, ` ip daddr { ${join(', ', PRIVATE4)} } counter drop`);
push(t, ' ip saddr @inet_ok accept');
push(t, ' counter drop');
push(t, ' }');
// The other way: fw4 lets the server's home zones reach the tunnel; here
// only connections from its home networks to people with home access pass
// (paused, ended and internet-only people are not reachable). The admin's
// own port forwards (DNAT) stay with fw4.
push(t, ' chain inbound {');
push(t, ' type filter hook forward priority filter - 5; policy accept;');
if (length(ifs))
push(t, ` oifname { ${join(', ', ifs)} } iifname != { ${join(', ', ifs)} } ct state new jump to_device`);
push(t, ' }');
push(t, ' chain to_device {');
push(t, ' meta nfproto ipv6 drop');
push(t, ' ct status dnat accept');
push(t, ' ip saddr @lan_nets ip daddr @lan_ok accept');
push(t, ' counter reject with icmpx admin-prohibited');
push(t, ' }');
// Devices ask the tunnel address; send each query where it belongs.
let to_resolver = rt.mode == 'ns' ? `dnat ip to ${rt.address}:53` : `redirect to :${rt.port}`;
let lan_dns = loop.lan_dns();
@@ -345,7 +387,7 @@ function nft_load(script) {
writefile(f, script);
let r = sh(`nft -f ${f}`);
if (r.code != 0)
log(`nft failed: ${trim(r.out)}`);
log('Access rules', "couldn't load", 'error', trim(r.out));
return r.code == 0;
}
@@ -399,7 +441,7 @@ export function sync_peers(m, states) {
unlink(f);
res[id] = r.code == 0 ? 'ok' : trim(r.out);
if (r.code != 0)
log(`awg syncconf ${srv.ifname}: ${trim(r.out)}`);
slog('awg', 'error', "couldn't load devices into the tunnel", trim(r.out), srv.name);
}
return res;
};
@@ -514,10 +556,45 @@ function sync_uhttpd(m) {
if (changed) {
c.commit('uhttpd');
system('/etc/init.d/uhttpd reload >/dev/null 2>&1');
log(want ? 'portal answers on 443 at home too' : 'portal removed from the LuCI web server');
system(`logger -t dawnline ${q(want ? 'portal answers through the LuCI web server' : 'portal removed from the LuCI web server')}`);
}
}
/**
* For the watchdog: what is wrong with Dawnline's firewall, or null.
* 'off': fw4 is not running at all (not ours to start); 'changed': the config
* lost or changed Dawnline's sections; 'not loaded': fw4 runs without them.
*/
export function firewall_drift(m) {
let t = sh_out('nft -t list table inet fw4 2>/dev/null') ?? '';
if (t == '')
return 'off';
if (sync_firewall(m, true))
return 'changed';
// Named rules show up in fw4's ruleset as comments.
let names = [];
for (let id, srv in m.servers)
if (srv.private_key && srv.enabled)
push(names, `Dawnline-${srv.ifname}`);
if (m.settings.portal_enabled)
push(names, 'Dawnline-Portal');
for (let n in names)
if (index(t, `${n}"`) < 0)
return 'not loaded';
return null;
};
/** For the watchdog: LuCI's web server should hand portal links to the portal, but does not. */
export function uhttpd_drift(m) {
if (!(m.settings.portal_enabled && core.portal_public_port(m) == 443 && m.settings.portal_port != 443))
return false;
let c = cursor();
c.load('uhttpd');
let cur = c.get('uhttpd', 'main', 'error_page');
// Someone else's error page is theirs: Health explains, nothing to repair.
return cur == null || (cur == LAN_CGI_URL && !stat(LAN_CGI));
};
/** One `ipproto <p> sport <port> lookup main` rule per listening port and family. */
function sync_reply_routes(m) {
let want = [];
@@ -580,7 +657,7 @@ function ensure_word_codes() {
if (changed) {
c.commit('dawnline');
system('chmod 600 /etc/config/dawnline');
log('personal links moved to word codes');
log('Personal links', 'moved to word codes');
}
}
@@ -618,4 +695,7 @@ export function teardown() {
remove_nft();
unlink(HOSTS_FILE);
system('killall -HUP dnsmasq 2>/dev/null');
// The agent's heartbeat check.
if (system('grep -qs /usr/libexec/dawnline/heartbeat /etc/crontabs/root') == 0)
system("sed -i '\\#/usr/libexec/dawnline/heartbeat#d' /etc/crontabs/root; /etc/init.d/cron restart >/dev/null 2>&1");
};
@@ -87,6 +87,8 @@ export function load(c) {
hostnames: bool(s.hostnames, true),
domain: s.domain ?? 'lan',
log_connections: bool(s.log_connections, true),
// Repair tunnels and services on its own (the agent, every minute).
watchdog: bool(s.watchdog, true),
// Second DNS for "home names" devices, used by the device when the tunnel is down.
home_dns_fallback: s.home_dns_fallback == 'none' ? '' : (s.home_dns_fallback ?? '1.1.1.1'),
// Raw retention values; dawnline.live policy() interprets them.
@@ -126,6 +128,8 @@ export function load(c) {
detected_wan: r.detected_wan ?? '',
detected_lan: r.detected_lan ?? '',
lan_like: bool(r.lan_like, true),
// Devices of people with home access reach each other through the tunnel.
device_mesh: bool(r.device_mesh, true),
enabled: bool(r.enabled, true),
obf: {},
};
@@ -459,8 +463,12 @@ export function client_dns(m, srv, p) {
export function allowed_ips(m, srv, p) {
if (p.access == 'lan') {
let nets = net.server_net(srv).nets;
// With devices reaching each other, the whole tunnel network (the router's
// address is in it); otherwise only the router's own address.
let a = server_address(srv);
if (a)
if (srv.device_mesh && srv.subnet)
push(nets, srv.subnet);
else if (a)
push(nets, `${a}/32`);
// The resolver address must be reachable through the tunnel too (not the fallback).
let dns = client_dns(m, srv, p)[0];
@@ -21,12 +21,14 @@ const KEEP_EVENTS = 2000;
/** Event type → log category (what Settings lets you switch off or clear). */
export const CATEGORY = {
connect: 'connections', disconnect: 'connections', roam: 'connections',
device_added: 'devices', device_removed: 'devices', devices_removed: 'devices',
// People and their devices: one category (devices used to have their own).
device_added: 'people', device_removed: 'people', devices_removed: 'people',
device_changed: 'people', device_keys: 'people', link_changed: 'people',
person_added: 'people', person_removed: 'people', person_changed: 'people',
paused: 'people', resumed: 'people', paused_all: 'people', resumed_all: 'people',
server_down: 'problems', server_up: 'problems', portal_blocked: 'problems',
};
export const CATEGORIES = [ 'connections', 'devices', 'people', 'problems' ];
export const CATEGORIES = [ 'connections', 'people', 'problems' ];
export function category_of(type) {
return CATEGORY[type] ?? 'problems';
@@ -55,7 +57,7 @@ export function policy(s) {
cats = (s.log_connections == '0') ? filter(CATEGORIES, (x) => x != 'connections') : CATEGORIES;
else if (type(cats) != 'array')
cats = split(cats, /[ ,]+/);
cats = filter(cats, (x) => index(CATEGORIES, x) >= 0);
cats = uniq(filter(map(cats, (x) => x == 'devices' ? 'people' : x), (x) => index(CATEGORIES, x) >= 0));
return {
keep_days: int_or(s.log_keep_days, 30),
max: min(max(int_or(s.log_max, KEEP_EVENTS), 100), 20000),
@@ -298,17 +300,33 @@ export function load_events() {
return read_json(`${RUN_DIR}/events.json`, null) ?? read_json(`${DATA_DIR}/events.json`, []);
};
/*
* Names as they are now (pname, dname, dkind), so the log still reads well
* once the person or device is gone. Callers that delete first pass them.
*/
function with_names(e, c) {
if (e.person && !e.pname)
e.pname = c.get('dawnline', e.person, 'name');
if (e.device && !e.dname)
e.dname = c.get('dawnline', e.device, 'name');
if (e.device && !e.dkind)
e.dkind = c.get('dawnline', e.device, 'kind');
return e;
}
export function add_events(list, pol) {
pol ??= policy();
list = filter(list ?? [], (e) => index(pol.categories, category_of(e.type)) >= 0);
if (!length(list))
return;
let c = cursor();
c.load('dawnline');
let l = lock('events');
let ev = load_events();
let ts = time();
for (let e in list) {
e.ts ??= ts;
push(ev, e);
push(ev, with_names(e, c));
}
ev = trim_events(ev, pol);
ensure_dir(RUN_DIR);
@@ -335,8 +353,10 @@ export function add_event_merged(e, window) {
last.ts = ts;
}
else {
let c = cursor();
c.load('dawnline');
e.ts = ts;
push(ev, e);
push(ev, with_names(e, c));
}
ev = trim_events(ev, pol);
ensure_dir(RUN_DIR);
@@ -27,7 +27,7 @@
import { readfile, writefile, readlink, stat, unlink } from 'fs';
import { cursor } from 'uci';
import { sh, sh_out, q, ip2int, int2ip, ensure_dir, read_json, write_json, uniq, RUN_DIR } from 'dawnline.util';
import { sh, sh_out, q, ip2int, int2ip, ensure_dir, read_json, write_json, uniq, slog, RUN_DIR } from 'dawnline.util';
import * as net from 'dawnline.net';
export const NS = 'dawnline';
@@ -40,8 +40,10 @@ const RESOLVER_PID = '/var/run/dawnline-dns.pid';
const STATE = `${RUN_DIR}/loop.json`;
const FALLBACK_DNS = [ '1.1.1.1', '8.8.8.8' ];
function log(msg) {
system(`logger -t dawnline ${q(msg)}`);
const WHO = 'Internet like a home device';
function log(msg, level, detail) {
slog('dawnline', level ?? 'info', msg, detail, WHO);
}
function run(cmds) {
@@ -289,8 +291,9 @@ table inet dawnline {
function current_rules(ifnames) {
let out = [];
try {
// Ours, plus ours left behind by a removed tunnel (its interface is gone).
for (let r in json(sh_out('ip -j -4 rule show') || '[]') ?? [])
if (r.priority >= 5200 && r.priority < 5300 && r.iif && index(ifnames, r.iif) >= 0)
if (r.priority >= 5200 && r.priority < 5300 && r.iif && (index(ifnames, r.iif) >= 0 || 'iif_detached' in r))
push(out, r);
}
catch (e) {}
@@ -299,6 +302,9 @@ function current_rules(ifnames) {
function rule_key(pref, iif, to, table) {
// `ip -j rule` shows a /32 destination without its length.
if (to && index(to, '/') < 0)
to += '/32';
return `${pref} ${iif} ${to ?? 'all'} ${table}`;
}
@@ -360,9 +366,10 @@ export function ensure(m, force) {
if (stat(STATE) || exists(TX_ROOT) || exists(LAN_ROOT) || length(current_rules(ifnames)))
remove(ifnames);
ensure_dir(RUN_DIR);
let was = read_json(STATE, {});
write_json(STATE, { error: p.error, off: !!p.off }, 0o644);
if (p.error)
log(`like-home routing off: ${p.error}`);
if (p.error && p.error != was.error)
log('is off', 'error', p.error);
return p;
}
let last = read_json(STATE, {});
@@ -410,7 +417,8 @@ export function ensure(m, force) {
push(errs, ...run(sync_rules(p, ifnames)));
if (length(errs)) {
log(`like-home routing failed, using the direct path: ${errs[0]}`);
if (read_json(STATE, {}).error != `Setup failed: ${errs[0]}`)
log('failed — internet goes out directly for now', 'error', errs[0]);
remove(ifnames);
write_json(STATE, { error: `Setup failed: ${errs[0]}` }, 0o644);
return { error: errs[0] };
@@ -426,7 +434,10 @@ export function ensure(m, force) {
}
ensure_dir(RUN_DIR);
write_json(STATE, { applied: time(), plan: p, transit: p.transit }, 0o644);
log(`like-home routing: ${p.dev} as ${p.address}`);
if (!last.applied || last.plan?.dev != p.dev || last.plan?.address != p.address)
log(`is on · traffic enters ${p.dev} as ${p.address}`, 'up');
else if (!healthy)
log(`was broken — set up again (${p.dev} as ${p.address})`, 'repair');
return p;
};
@@ -93,15 +93,15 @@ function check(id, title, status, detail, fix) {
return { id, title, status, detail: detail ?? '', fix: fix ?? null };
}
function udp_listening(port) {
export function udp_listening(port) {
let hex = sprintf(':%04X ', port);
for (let f in [ '/proc/net/udp', '/proc/net/udp6' ])
if (index(readfile(f) ?? '', hex) >= 0)
return true;
return false;
}
};
function tcp_listening(port) {
export function tcp_listening(port) {
let hex = sprintf(':%04X ', port);
for (let f in [ '/proc/net/tcp', '/proc/net/tcp6' ])
for (let line in split(readfile(f) ?? '', '\n'))
@@ -109,7 +109,7 @@ function tcp_listening(port) {
if (index(line, hex) > 0 && match(line, / 0A /))
return true;
return false;
}
};
/** Processes other than our portal listening on a TCP port: [ "name" ]. */
export function tcp_port_owners(port) {
@@ -202,7 +202,7 @@ export function portal_candidates(m) {
return sort(out, (a, b) => score(b) - score(a));
};
function service_running(name, instance) {
export function service_running(name, instance) {
let s = sh_out(`ubus call service list '{"name":"${name}"}' 2>/dev/null`);
if (!s)
return false;
@@ -214,7 +214,7 @@ function service_running(name, instance) {
catch (e) {
return false;
}
}
};
function other_subnets(srv_id) {
let out = [];
@@ -3,7 +3,7 @@
import { open, readfile, writefile, rename, mkdir, stat, unlink, popen, lsdir } from 'fs';
export const VERSION = '0.1.1';
export const VERSION = '0.2.0';
/** GitHub repository with Dawnline releases (updates and the installer). */
export const REPO = 'tickcount/luci-app-dawnline';
export const RUN_DIR = '/var/run/dawnline';
@@ -126,6 +126,131 @@ export function unlock(f) {
}
};
/* ---------- service log ---------- */
/*
* Tunnel and service starts and stops, watchdog repairs and errors: one JSON
* object per line in tmpfs (appends from several processes stay whole), a
* copy on flash that the agent refreshes hourly, and a line in syslog.
* src: awg | dawnline | portal; level: info | up | down | repair | error.
*/
export const SLOG = `${RUN_DIR}/service.log`;
export const SLOG_SAVED = `${DATA_DIR}/service.log`;
const SLOG_KEEP = 300;
export function slog(src, level, msg, detail, who, extra) {
let e = { ts: time(), src, level, msg };
// The subject, shown first and bold: a server's name, "Personal portal", "Dawnline".
if (who)
e.who = `${who}`;
// The admin's own changes: { by: 'admin', kind: 'server', note: 'port 50115' }
// read "You <msg> <kind> <who> · <note>".
for (let k in [ 'by', 'kind', 'note' ])
if (extra?.[k])
e[k] = `${extra[k]}`;
if (detail)
e.detail = substr(trim(`${detail}`), 0, 2000);
ensure_dir(RUN_DIR);
let f = open(SLOG, 'a', 0o600);
if (f) {
f.write(sprintf('%J\n', e));
f.close();
}
let line = e.by == 'admin' ? join(' ', filter([ 'admin', msg, e.kind, who, e.note ? '· ' + e.note : null ], (x) => x)) : who ? `${who} ${msg}` : msg;
system(`logger -t dawnline${level == 'error' ? ' -p user.err' : ''} ${q(line)}`);
};
/** A change the admin made in LuCI, for the service log. */
export function admin_log(src, msg, kind, who, note, detail) {
slog(src, 'info', msg, detail, who, { by: 'admin', kind, note });
};
/*
* Why Dawnline itself is about to take a tunnel down, so the hotplug log can
* tell that from a tunnel that stopped on its own. reason: settings | off |
* removed | update. Kept for two minutes.
*/
const INTENT = `${RUN_DIR}/iface-intent.json`;
export function iface_intent(ifname, reason, name) {
let all = read_json(INTENT, {});
all[ifname] = { reason, name, ts: time() };
ensure_dir(RUN_DIR);
write_json(INTENT, all, 0o600);
};
export function take_iface_intent(ifname) {
let all = read_json(INTENT, {});
let it = all[ifname];
if (!it)
return null;
delete all[ifname];
write_json(INTENT, all, 0o600);
return time() - it.ts < 120 ? it : null;
};
/** Newest first. */
export function slog_read(limit) {
let lines = split(readfile(SLOG) ?? readfile(SLOG_SAVED) ?? '', '\n');
let out = [];
for (let i = length(lines) - 1; i >= 0 && length(out) < (limit ?? SLOG_KEEP); i--) {
if (lines[i] == '')
continue;
try {
let e = json(lines[i]);
if (type(e) == 'object' && e.ts)
push(out, e);
}
catch (err) {}
}
return out;
};
/** Keep the last entries only, and save them to flash when asked. */
export function slog_trim(save) {
let lines = filter(split(readfile(SLOG) ?? '', '\n'), (l) => l != '');
if (length(lines) > SLOG_KEEP + 50) {
lines = slice(lines, -SLOG_KEEP);
write_file(SLOG, join('\n', lines) + '\n');
}
if (save && length(lines) && stat(SLOG)?.mtime > (stat(SLOG_SAVED)?.mtime ?? 0)) {
ensure_dir(DATA_DIR, 0o700);
write_file(SLOG_SAVED, join('\n', lines) + '\n');
}
};
/** Remove entries (all, or older than `days`); returns how many went. */
export function slog_clear(days) {
let keep = [], removed = 0, cut = days > 0 ? time() - days * 86400 : null;
for (let l in split(readfile(SLOG) ?? readfile(SLOG_SAVED) ?? '', '\n')) {
if (l == '')
continue;
let e = null;
try {
e = json(l);
}
catch (err) {}
if (cut != null && e?.ts >= cut)
push(keep, l);
else
removed++;
}
let data = length(keep) ? join('\n', keep) + '\n' : '';
ensure_dir(RUN_DIR);
write_file(SLOG, data);
if (stat(SLOG_SAVED))
write_file(SLOG_SAVED, data);
return removed;
};
/** After a reboot tmpfs is empty: continue the log saved on flash. */
export function slog_restore() {
if (!stat(SLOG) && stat(SLOG_SAVED)) {
ensure_dir(RUN_DIR);
write_file(SLOG, readfile(SLOG_SAVED) ?? '');
}
};
/* ---------- IPv4 ---------- */
export function ip2int(ip) {
@@ -0,0 +1,190 @@
// 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; its state to
// RUN_DIR for the Health page. A cron job (libexec/dawnline/heartbeat) looks
// after the agent itself.
import { stat } from 'fs';
import { sh_out, q, read_json, write_json, slog, slog_read, 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 loop from 'dawnline.loop';
import * as ops from 'dawnline.ops';
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 ];
/** Why a tunnel is down, from netifd and the last related syslog lines. */
export function iface_errors(ifname) {
let s = live.iface_status(ifname);
let errs = map(s?.errors ?? [], (e) => e.code + (length(e.data ?? []) ? ` (${join(' ', e.data)})` : ''));
// Only what explains a failure, not the ordinary "is now up/down" lines.
let lines = sh_out(`logread -l 400 2>/dev/null | grep -E ${q(`${ifname}|amneziawg`)} | grep -iE 'error|fail|invalid|unrecogni|cannot|unable|not found|refused|denied' | tail -n 6`);
return join('\n', filter([ length(errs) ? `netifd: ${join(', ', errs)}` : null, lines ], (x) => x));
};
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), who (the log's subject), ok,
* problem, back, detail?, repair: { what, run }? }. The log reads "<who> <problem> — <what>".
*/
function probes(m) {
let out = [];
// The kernel module first: without it no tunnel can run (they come back on the next pass).
if (length(filter(values(m.servers), (x) => x.enabled && x.private_key)) && stat('/usr/bin/awg'))
push(out, {
id: 'awg', src: 'awg', name: 'AmneziaWG', who: 'AmneziaWG', ok: stat('/sys/module/amneziawg') != null,
problem: "kernel module isn't loaded", back: 'kernel module is loaded again',
detail: () => sh_out("modprobe amneziawg 2>&1; dmesg 2>/dev/null | grep -i amneziawg | grep -iE 'error|fail|invalid|unknown|disagrees|taint' | tail -n 4"),
repair: { what: 'loading it', run: () => system('modprobe amneziawg >/dev/null 2>&1') },
});
for (let id, srv in m.servers) {
if (!srv.enabled || !srv.private_key)
continue;
let s = live.iface_status(srv.ifname);
let up = !!s?.up;
push(out, {
id: `up_${id}`, src: 'awg', name: srv.name, who: srv.name, ok: up && ops.udp_listening(srv.port),
problem: !s ? 'is missing from the router' : !up ? "isn't running" : `isn't accepting connections on port ${srv.port}`,
back: `is running again · accepting connections on port ${srv.port}`,
detail: () => iface_errors(srv.ifname),
repair: s ? { what: 'restarting it', run: () => system(`ifup ${q(srv.ifname)}`) } : { what: 'setting it up again', run: () => apply.apply() },
});
}
if (length(m.servers))
push(out, {
id: 'nft', src: 'dawnline', name: 'Access rules', who: 'Access rules', ok: system('nft list table inet dawnline >/dev/null 2>&1') == 0,
problem: 'are missing', back: 'are loaded again',
repair: { what: '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: 'Firewall', ok: fw == null,
problem: fw == 'off' ? "isn't running on this router (fw4): tunnels and the portal are closed"
: fw == 'changed' ? "rules for Dawnline were changed or removed" : "rules for Dawnline aren't loaded",
back: '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: 'restoring them', run: () => { apply.apply(); system('fw4 -q reload'); } },
});
}
let s = m.settings;
if (s.portal_enabled && core.portal_public_port(m) == 443 && s.portal_port != 443)
push(out, {
id: 'portal_lan', src: 'portal', name: 'Portal at home', who: 'Personal portal', ok: !apply.uhttpd_drift(m),
problem: "links don't open at home (LuCI's web server lost the setting)", back: 'links open at home again',
repair: { what: 'restoring it', run: () => apply.apply() },
});
if (s.portal_enabled && !length(ops.needs().https) && !length(ops.tcp_port_owners(s.portal_port)))
push(out, {
id: 'portal', src: 'portal', name: 'Portal', who: 'Personal portal', ok: ops.tcp_listening(s.portal_port),
problem: `isn't answering on port ${s.portal_port}`, back: '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: 'restarting it', run: restart_services },
});
if (stat(loop.RESOLVER.conf))
push(out, {
id: 'resolver', src: 'dawnline', name: 'Resolver', who: 'DNS for Home-only devices', ok: loop.resolver_running(),
problem: "isn't running", back: 'runs again',
repair: { what: '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: "can't reach Dawnline (luci.dawnline is missing in rpcd)", back: '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.
repair: { what: 'reloading rpcd', 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;
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', p.back + (x.stuck ? ' · restored' : x.tries ? ` (after ${x.tries} repair${x.tries > 1 ? 's' : ''})` : ''), null, p.who);
x.bad = 0;
x.tries = 0;
x.next = 0;
x.reported = false;
x.stuck = false;
continue;
}
// One bad minute can be a restart in progress.
if (++x.bad < 2)
continue;
if (!fix || !p.repair) {
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', `${p.problem} — ${x.tries} repairs didn't help`, 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', `${p.problem} — ${p.repair.what}`, p.detail?.(), p.who);
try {
p.repair.run(x.tries);
}
catch (e) {
if (!x.stuck)
slog(p.src, 'error', `couldn't be repaired: ${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);
};
/** For the Health page: the watchdog's state and the newest log entries. */
export function view(m) {
let st = read_json(STATE, {});
let beat = +(read_json(BEAT, 0) ?? 0);
// The agent itself: fine while it ticks (cron restarts it after 5 minutes without).
let targets = [ { id: 'agent', name: 'Agent', ok: beat > 0 && time() - beat < 180, tries: 0 } ];
for (let id, x in (type(st.targets) == 'object' ? st.targets : {}))
push(targets, { id, name: 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: st.repairs ?? [],
},
log: slog_read(200),
};
};