Dawnline 0.18.0
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

- Add V2RayTun to a device's set-up on Android: its awg:// link as a code and to copy, in the portal and in the admin (phones, tablets and other devices), with the server's IP, which its Android app needs; not with the Maximum disguise. Its Windows app is ready but stays off until it gives the tunnel the server's DNS.
- Portal: show V2RayTun's Telegram channel beside Google Play.
- Make WireGuard part of AmneziaWG's choice when the disguise is off: the same file and code, both apps named in the steps, WireGuard's store a tap away; a router still picks its package.
- Portal: Reconnect asks again what the device is and about LAN Bypass, and LAN Bypass switches from a device's ⋯ menu, in a group of its own between the copy actions and Rename, Reconnect, Remove (the keys stay; the device takes its updated configuration).
- Turn LAN Bypass on by default for new devices, in the portal and the admin.
- Portal: an app's name too long for the picker fades at its edge and glides to its end on hover or tap; two store buttons stack where they do not fit side by side; the month chart's "Today" stays inside its axis.
- Write to the router's system log by level (Settings → Logs & history → System log: errors, warnings, normal, verbose): the service starting and stopping, every apply with its time, network, firewall, nftables and peer changes, changed settings and portal errors, in English whatever the admin's language, with keys masked.
- Activity: say what a device's change on the portal was (LAN Bypass on or off, its type).
- Demo: build again after a force-pushed main instead of failing on the commit that is gone.
This commit is contained in:
Aemeath committed 2026-11-05 13:56:20 +03:00
1 parent 18e3013047
commit f8130e39a8
65 files changed
+2066 -338

No files matched your search

@@ -8,15 +8,33 @@ USE_PROCD=1
PORTAL_ROOT=/usr/share/dawnline/portal/www
# A line in syslog as dawnline.util syslog() writes them: "dawnline: [info] [service] …" at the
# matching priority, when the admin's level (settings.log_level: error|warn|info|debug) takes it.
log() {
local lvl="$1" want rank prio
shift
want="$(uci -q get dawnline.settings.log_level)"
case "$lvl" in error) rank=0 prio=err ;; warn) rank=1 prio=warning ;; debug) rank=3 prio=debug ;; *) rank=2 prio=info ;; esac
case "$want" in error) want=0 ;; warn) want=1 ;; debug) want=3 ;; *) want=2 ;; esac
[ "$rank" -le "$want" ] || return 0
logger -t dawnline -p "daemon.$prio" "[$lvl] [service] $*"
}
start_portal() {
local enabled port certs cert key hport http
config_get_bool enabled settings portal_enabled 0
[ "$enabled" = 1 ] || return 0
if [ "$enabled" != 1 ]; then
log info "personal portal is off"
return 0
fi
config_get port settings portal_port 8443
certs="$(/usr/bin/dawnline portal-cert 2>/dev/null)"
cert="${certs% *}"
key="${certs#* }"
[ -f "$cert" ] && [ -f "$key" ] || return 0
if ! [ -f "$cert" ] || ! [ -f "$key" ]; then
log warn "personal portal not started: no certificate (${cert:-none}): choose one in Settings, or save the portal settings again"
return 0
fi
# IP addresses in links get plain HTTP on the next port (names stay HTTPS-only).
hport="$(/usr/bin/dawnline portal-http-port 2>/dev/null)"
[ -n "$hport" ] && http="-p 0.0.0.0:$hport -p [::]:$hport"
@@ -34,23 +52,30 @@ start_portal() {
procd_set_param file "$cert"
procd_set_param respawn 3600 5 0
procd_close_instance
log info "personal portal on port $port (HTTPS${hport:+, plain HTTP on $hport for links with an IP address}) · certificate $cert$(openssl x509 -enddate -noout -in "$cert" 2>/dev/null | sed 's/^notAfter=/ until /') · $procs processes"
}
# Plain DNS for Home-only tunnel devices (config written by `dawnline apply`).
start_resolver() {
[ -f /var/etc/dawnline-dns.conf ] || return 0
if ! [ -f /var/etc/dawnline-dns.conf ]; then
log debug "no DNS for Home-only devices (nobody needs it)"
return 0
fi
procd_open_instance dns
procd_set_param command /usr/libexec/dawnline/resolver
procd_set_param file /var/etc/dawnline-dns.conf
# A new config kills it (dawnline.resolver): back within a second, not five.
procd_set_param respawn 3600 1 0
procd_close_instance
log info "DNS for Home-only devices started (/var/etc/dawnline-dns.conf)"
}
start_service() {
config_load dawnline
mkdir -p /var/run/dawnline
/usr/bin/dawnline apply >/dev/null 2>&1
log info "starting"
# The apply writes its own lines: what it came to and how long it took (each step at debug).
/usr/bin/dawnline apply >/dev/null 2>&1 || log error "applying the configuration failed: run \"dawnline apply\" to see why"
procd_open_instance agent
procd_set_param command /usr/libexec/dawnline/agent
@@ -60,10 +85,12 @@ start_service() {
start_portal
start_resolver
log info "started"
}
# Noted for the service log: the next agent start is a restart, not a crash or a boot.
stop_service() {
log info "stopping"
mkdir -p /var/run/dawnline
date +%s > /var/run/dawnline/agent.stopped
# Copies procd doesn't know about (started by hand, or left by an older version) go too.
@@ -71,7 +98,8 @@ stop_service() {
}
reload_service() {
/usr/bin/dawnline apply >/dev/null 2>&1
log info "reloading: settings changed"
/usr/bin/dawnline apply >/dev/null 2>&1 || log error "applying the configuration failed: run \"dawnline apply\" to see why"
start
}
@@ -15,7 +15,7 @@ import * as store from 'dawnline.store';
import * as devices from 'dawnline.devices';
import { L_ } from 'dawnline.i18n';
import {
VERSION, slog, slog_read, sh_out, q, take_iface_intent, read_json, write_json, ensure_dir, with_lock, RUN_DIR
VERSION, slog, syslog, slog_read, sh_out, q, take_iface_intent, read_json, write_json, ensure_dir, with_lock, RUN_DIR
} from 'dawnline.util';
/** Options that take no value. */
@@ -338,5 +338,7 @@ try {
}
catch (e) {
warn(`dawnline: ${e.message}\n`);
// Run from the init script or cron, stderr goes nowhere: syslog keeps why.
syslog('error', 'cli', `dawnline ${cmd} failed: ${e.message}`);
exit(1);
}
@@ -17,7 +17,7 @@
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
read_json, write_json, ensure_dir, sh_out, q, uniq, slog, syslog, 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';
@@ -641,8 +641,9 @@ function nightly_backup() {
let st = read_json(`${DATA_DIR}/backup-state.json`, {});
if (sum && sum != st.auto) {
try {
backup.backup_create('auto');
let b = backup.backup_create('auto');
write_json(`${DATA_DIR}/backup-state.json`, { auto: sum }, 0o600);
syslog('info', 'backup', `nightly backup saved${b ? ': ' + b : ''}`);
}
catch (e) {
// Tried again tomorrow; meanwhile the admin should know (a full flash, mostly).
@@ -794,9 +795,11 @@ function step(i, s, fn) {
delete step_err[i];
}
catch (e) {
system(`logger -t dawnline-agent ${q(`${s.who}: ${e.message}`)}`);
// A new error goes to the service log (and from there to syslog); a repeat, or a quiet step's, to syslog alone.
if (!s.quiet && step_err[i] != e.message)
slog('dawnline', 'error', sprintf(L_('failed: %s'), e.message), e.stacktrace?.[0]?.context, s.who);
else
syslog('error', 'agent', `${s.who}: ${e.message}`);
step_err[i] = e.message;
}
}
@@ -835,7 +838,14 @@ function tick() {
});
}
// Once: a signal ends the loop and calls it, and so does the line after uloop.run().
let stopping = false;
function shutdown() {
if (stopping)
return;
stopping = true;
syslog('info', 'agent', 'stopping · saving the counters and the logs');
live.save_usage(usage, true);
live.persist_events();
geo.flush();
@@ -876,6 +886,7 @@ 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');
syslog('debug', 'agent', sprintf('watching connections every %d ms, checking the rest every %d ms', WATCH, TICK));
// 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();
@@ -29,4 +29,4 @@ else detail="It never completed a check."; fi
msg="stopped responding — restarted"
mkdir -p /var/run/dawnline
printf '{"ts":%s,"src":"dawnline","level":"repair","who":"Dawnline","msg":"%s","detail":"%s"}\n' "$now" "$msg" "$detail" >> /var/run/dawnline/service.log
logger -t dawnline "Dawnline $msg"
logger -t dawnline -p daemon.warning "[warn] [agent] Dawnline $msg - $detail"
@@ -31,13 +31,13 @@ while [ "$i" -lt 12 ]; do
up && exit 0
done
logger -t dawnline "update to $to failed its check: luci.dawnline or the agent is not up"
logger -t dawnline -p daemon.err "[error] [update] update to $to failed its check: luci.dawnline or the agent is not up"
set --
for f in "$KEEP"/*.ipk "$KEEP"/*.apk; do
[ -f "$f" ] && set -- "$@" "$f"
done
if [ "$#" -eq 0 ]; then
logger -t dawnline "no previous version is kept: nothing to roll back to"
logger -t dawnline -p daemon.err "[error] [update] no previous version is kept: nothing to roll back to"
exit 1
fi
if command -v apk >/dev/null 2>&1; then
@@ -50,5 +50,5 @@ fi
mkdir -p "$RUN"
# The restored agent reports it in the service log on its start.
echo "$to" > "$RUN/update-rolled-back"
logger -t dawnline "rolled back from $to to the kept version"
logger -t dawnline -p daemon.warning "[warn] [update] rolled back from $to to the kept version"
exit 0
@@ -13,7 +13,7 @@
// (dawnline.store) is never taken while 'apply' is held.
import { readfile, writefile, stat, unlink } from 'fs';
import { ensure_dir, write_file, write_json, with_lock, ip2int, slog, RUN_DIR } from 'dawnline.util';
import { ensure_dir, write_file, write_json, with_lock, ip2int, slog, syslog, log_on, RUN_DIR } from 'dawnline.util';
import * as core from 'dawnline.core';
import * as net from 'dawnline.net';
import * as loop from 'dawnline.loop';
@@ -54,8 +54,10 @@ export function sync_hosts(m) {
let body = length(lines) ? join('\n', lines) + '\n' : '';
// dnsmasq runs as its own user; rpcd's umask would leave the file 0600.
let readable = ((stat(HOSTS_FILE)?.perm?.other_read) ?? false);
if ((readfile(HOSTS_FILE) ?? '') == body && (body == '' || readable))
if ((readfile(HOSTS_FILE) ?? '') == body && (body == '' || readable)) {
syslog('debug', 'dns', `${HOSTS_FILE}: unchanged · ${length(lines)} names`);
return false;
}
ensure_dir('/tmp/hosts');
if (body == '')
unlink(HOSTS_FILE);
@@ -63,6 +65,7 @@ export function sync_hosts(m) {
writefile(HOSTS_FILE, body);
system(`chmod 644 ${HOSTS_FILE}`);
}
syslog('info', 'dns', body == '' ? `${HOSTS_FILE}: removed (no device names)` : `${HOSTS_FILE}: ${length(lines)} names written (*.${m.settings.domain}) · killall -HUP dnsmasq`);
system('killall -HUP dnsmasq 2>/dev/null');
return true;
};
@@ -154,6 +157,21 @@ function sync_warning(m, table, peers) {
return length(out) ? join(' ', [ ...out, _('Open Diagnostics → Health.') ]) : null;
}
/**
* The same as sync_warning for syslog: in English whatever the admin's
* language (a log is read by anyone, searched and sent on), with what nft or
* awg said. Null when all went in.
*/
function sync_facts(m, table, peers) {
let out = [];
if (table != null)
push(out, `access rules ${nft.present() ? 'not updated, the previous ones stay' : 'not loaded, no device loaded'}: ${table}`);
for (let id, r in peers)
if (r != 'ok' && r != 'down')
push(out, `${m.servers[id]?.name ?? id}: devices not loaded: ${r}`);
return length(out) ? join(' · ', out) : null;
}
/*
* With the lock held: access sets first, then devices into the tunnels.
* Fail closed: without the table the firewall would let every loaded device
@@ -226,24 +244,60 @@ export function sync_loop(m, usage, force) {
/* ---------- entry points ---------- */
/** Full reconcile: router configuration + runtime state. */
/** Milliseconds on the monotonic clock, for how long an apply's steps take. */
function ms() {
let c = clock(true);
return c[0] * 1000 + c[1] / 1000000;
}
/**
* Full reconcile: router configuration + runtime state. In syslog: what it
* came to, with how long it took (info; warn when it did not fully take), and
* each step with its result at the debug level.
*/
export function apply(opts) {
opts ??= {};
let t0 = ms();
let debug = log_on('debug');
// One step, timed; `show` picks what of its result goes in the line (never the model: it holds keys).
let step = (name, fn, show) => {
let t = ms();
let out = fn();
let what = show ? show(out) : out;
if (debug)
syslog('debug', 'apply', sprintf('%s done in %d ms%s', name, ms() - t,
what == null ? '' : ` · ${type(what) == 'string' ? what : sprintf('%J', what)}`));
return out;
};
if (debug)
syslog('debug', 'apply', 'applying the configuration');
migrate.run();
let r = with_lock('apply', () => {
let m = core.load();
let usage = core.load_usage();
let network = netifd.sync(m);
let firewall = fw.sync(m);
let lp = loop.ensure(m, true);
loop.sync_resolver(m);
fw.sync_reply_routes(m);
let s = locked_sync(m, usage, true);
let changed = (x) => x ? 'changed' : 'unchanged';
let network = step('network (netifd)', () => netifd.sync(m), changed);
let firewall = step('firewall (fw4)', () => fw.sync(m), changed);
let lp = step('internet like a home device', () => loop.ensure(m, true));
step('resolver for Home-only devices', () => { loop.sync_resolver(m); });
step('reply routes', () => { fw.sync_reply_routes(m); });
let s = step('access rules and devices', () => locked_sync(m, usage, true), (x) => ({ table: x.table, peers: x.peers }));
ensure_dir(RUN_DIR);
write_file(NET_SIG, net_signature(m), 0o644);
return { network, firewall, lp, peers: s.peers, warning: s.warning };
return { network, firewall, lp, peers: s.peers, warning: s.warning, facts: sync_facts(m, s.table, s.peers),
counts: [ length(keys(m.servers)), length(keys(m.people)), length(keys(m.devices)) ] };
});
loop.remember(r.lp);
let n = (k, one, many) => `${k} ${k == 1 ? one : many}`;
let took = sprintf('configuration applied in %d ms · %s, %s, %s', ms() - t0,
n(r.counts[0], 'server', 'servers'), n(r.counts[1], 'person', 'people'), n(r.counts[2], 'device', 'devices'));
// Syslog in English (sync_facts, loop.error_note); the admin's answer carries the words in their language.
if (r.facts)
syslog('warn', 'apply', `${took} · ${r.facts}`);
else
syslog('info', 'apply', took);
if (r.lp.error)
syslog('warn', 'apply', `internet like a home device: ${type(r.lp.error) == 'object' ? loop.error_note(r.lp.error) : r.lp.error}`);
return { network: r.network, firewall: r.firewall, like_home: r.lp.error ? { error: loop.error_text(r.lp.error) } : !r.lp.off, peers: r.peers, warning: r.warning };
};
@@ -8,7 +8,7 @@
import { readfile, stat, unlink } from 'fs';
import {
q, sh, sh_out, rand_int, rand_bytes, valid_key, ip2int, tunnel_name, ensure_dir, write_file, read_json, write_json, lock, unlock,
slog, iface_status, DATA_DIR, RUN_DIR
slog, syslog, some_of, iface_status, DATA_DIR, RUN_DIR
} from 'dawnline.util';
import * as core from 'dawnline.core';
import * as net from 'dawnline.net';
@@ -763,32 +763,58 @@ export function peer_diff(devs, dmp, ifname) {
};
/** Push the peer list of every server to the kernel without restarting it. */
// Each tunnel's peers as last loaded ({ ifname: [ "Person · Device (address)" ] }), so a log line can say who came and went.
const PEERS_SEEN = `${RUN_DIR}/peers-seen.json`;
export function sync_peers(m, states) {
ensure_dir(RUN_DIR);
let res = {};
let seen = read_json(PEERS_SEEN, {}), now = {};
for (let id, srv in m.servers) {
// Only Dawnline's own interfaces: never another tunnel of the same name (core.tunnels).
if (!srv.enabled || !length(core.tunnels(srv)))
continue;
if (system(`awg show ${q(srv.ifname)} public-key >/dev/null 2>&1`) != 0) {
res[id] = 'down';
syslog('debug', 'awg', `interface ${srv.ifname} (${srv.name}): down, no devices to load`);
continue;
}
let lines = [ '[Interface]', `PrivateKey = ${srv.private_key}`, `ListenPort = ${srv.port}`, '' ];
let who = [];
for (let d in tunnel_devices(m, id, states)) {
push(lines, '[Peer]', `PublicKey = ${d.public_key}`);
if (d.preshared_key)
push(lines, `PresharedKey = ${d.preshared_key}`);
push(lines, `AllowedIPs = ${d.address}/32`, '');
push(who, `${m.people[d.person]?.name ?? d.person} · ${d.name} (${d.address})`);
}
let f = `${RUN_DIR}/${srv.ifname}.conf`;
write_file(f, join('\n', lines));
let r = sh(`awg syncconf ${q(srv.ifname)} ${f}`);
unlink(f);
res[id] = r.code == 0 ? 'ok' : trim(r.out);
if (r.code != 0)
if (r.code != 0) {
slog('awg', 'error', L_("couldn't load devices into the tunnel"), trim(r.out), srv.name);
continue;
}
// Who came in and who left since the last load: info; the same peers: debug.
now[srv.ifname] = who;
let was = seen[srv.ifname];
let head = `interface ${srv.ifname} (${srv.name}): awg syncconf · listen port ${srv.port} · ${length(who)} ${length(who) == 1 ? 'peer' : 'peers'}`;
// The first load since the router started (or since the tunnel came up): how many, the names at debug.
if (was == null) {
syslog('info', 'awg', `${head} loaded`);
syslog('debug', 'awg', `interface ${srv.ifname}: ${join(', ', who)}`);
continue;
}
let add = filter(who, (x) => index(was, x) < 0), del = filter(was, (x) => index(who, x) < 0);
if (length(add) || length(del))
syslog('info', 'awg', `${head}${length(add) ? ' · added ' + some_of(add) : ''}${length(del) ? ' · removed ' + some_of(del) : ''}`);
else
syslog('debug', 'awg', `${head}, unchanged`);
}
if (sprintf('%J', now) != sprintf('%J', seen))
write_json(PEERS_SEEN, now, 0o600);
return res;
};
@@ -90,6 +90,8 @@ export function load(c) {
hostnames: g('hostnames'),
domain: g('domain'),
log_connections: g('log_connections'),
// What goes to syslog (dawnline.util syslog).
log_level: g('log_level'),
// Repair tunnels and services on its own (the agent, every minute).
watchdog: g('watchdog'),
// Second DNS for "home names" devices, used by the device when the tunnel is down.
@@ -789,6 +791,24 @@ export function set_device_link(c, m, id, a) {
return changed;
};
/**
* Change what a device is (one of KINDS): true when it changed, false when it
* already was. Its configuration follows (device_mtu: phones and tablets take
* smaller packets); nothing on the router changes.
*/
export function set_device_kind(c, m, id, kind) {
let d = m.devices[id];
if (!d)
fail(_('Unknown device.'));
if (index(KINDS, kind) < 0)
fail(_('Unknown device type.'));
if (kind == d.kind)
return false;
c.set(CONFIG, id, 'kind', kind);
d.kind = kind;
return true;
};
export function delete_device(c, m, id) {
let d = m.devices[id];
if (!d)
@@ -29,6 +29,22 @@ export function need_awg() {
}
};
/**
* What a device_changed event says changed: each option with its new value
* ("lan_bypass=on", "mtu=1400", "keepalive=auto": '' is the server's again,
* "kind=phone"), so the activity log can say what it became.
*/
function change_detail(names, ch) {
return join(', ', map(names, (k) => {
if (k == 'lan_bypass')
return `lan_bypass=${ch.lan_bypass === true ? 'on' : 'off'}`;
if (k == 'kind')
return `kind=${ch.kind}`;
let v = trim(`${ch[k] ?? ''}`);
return `${k}=${v == '' ? 'auto' : v}`;
}));
}
/** The device `id` as `opts` may touch it (inside a transaction's model). */
function own(m, id, opts) {
let d = m.devices[id];
@@ -58,9 +74,11 @@ export function add(person, name, kind, opts) {
/**
* Rename and/or turn a device off or on, or change how it connects (`ch` =
* { name?, disabled?, mtu?, keepalive?, lan_bypass? }, see
* core.set_device_link); only the admin turns devices off or changes the
* rest. Returns the runtime sync ({} after a change of the link values alone:
* they are in the device's configuration, nothing on the router changes).
* core.set_device_link); only the admin turns devices off or sets the MTU and
* keep-alive, the portal may switch LAN Bypass (the device then takes its
* updated configuration, keys unchanged). Returns the runtime sync ({} after a
* change of the link values alone: they are in the device's configuration,
* nothing on the router changes).
*/
export function update(id, ch, opts) {
opts ??= {};
@@ -68,7 +86,7 @@ export function update(id, ch, opts) {
if (ch.disabled != null && by != 'admin')
die(_('Only the admin can turn devices off.'));
let link = ch.mtu != null || ch.keepalive != null || ch.lan_bypass != null;
if (link && by != 'admin')
if ((ch.mtu != null || ch.keepalive != null) && by != 'admin')
die(_('Only the admin can change how a device connects.'));
let linked = [];
let d0 = store.transact((c, m) => {
@@ -91,9 +109,9 @@ export function update(id, ch, opts) {
live.add_event({ type: 'device_changed', person: d0.person, device: id, detail: ch.disabled ? 'turned off' : 'turned on', by });
else if (ch.name != null && ch.name != d0.name)
live.add_event({ type: 'device_changed', person: d0.person, device: id, detail: `renamed from ${d0.name}`, by });
// The option names, as person_changed lists a person's.
// The options with what they became (change_detail).
if (length(linked))
live.add_event({ type: 'device_changed', person: d0.person, device: id, detail: join(', ', linked), by });
live.add_event({ type: 'device_changed', person: d0.person, device: id, detail: change_detail(linked, ch), by });
return r;
};
@@ -106,10 +124,19 @@ export function update(id, ch, opts) {
function rotate(id, opts) {
opts ??= {};
let by = opts.by ?? 'admin';
store.transact((c, m) => {
own(m, id, opts);
let changed = [];
let person = store.transact((c, m) => {
let d = own(m, id, opts);
if (opts.kind != null && core.set_device_kind(c, m, id, opts.kind))
push(changed, 'kind');
if (opts.lan_bypass != null)
push(changed, ...core.set_device_link(c, m, id, { lan_bypass: opts.lan_bypass === true }));
core.rotate_device(c, m, id, awg.new_keys());
return d.person;
});
if (length(changed))
live.add_event({ type: 'device_changed', person, device: id, by,
detail: change_detail(changed, { kind: opts.kind, lan_bypass: opts.lan_bypass === true }) });
// The device a portal page came through would lose its tunnel before the
// answer gets out: load the new key a moment after answering.
let s = null;
@@ -132,8 +159,10 @@ function rotate(id, opts) {
* the admin changed; the portal's Reconnect and the admin's New keys): new
* keys, so the configuration it holds stops working with the sync, and it
* counts as not set up until it connects with the new one. The usage keeps
* the old keys' last handshake: awg.handshake_since hides it. Returns the new
* configuration (with `warning` when the sync did not fully take).
* the old keys' last handshake: awg.handshake_since hides it. `opts.kind` and
* `opts.lan_bypass` (the portal's Reconnect: what it is, LAN Bypass) change
* with the keys, in the same write. Returns the new configuration (with
* `warning` when the sync did not fully take).
*/
export function reconnect(id, opts) {
opts ??= {};
@@ -5,7 +5,7 @@
// named dawnline_* are ever changed.
import { readfile, unlink } from 'fs';
import { q, sh, sh_out, uniq, in_nets, uci_put, uci_section, ack_config, write_file, RUN_DIR } from 'dawnline.util';
import { sh, sh_out, uniq, in_nets, uci_put, uci_section, ack_config, write_file, syslog, opts_text, RUN_DIR } from 'dawnline.util';
import { cursor } from 'uci';
import * as core from 'dawnline.core';
import * as net from 'dawnline.net';
@@ -33,12 +33,21 @@ export function sync(m, dry) {
let c = cursor();
c.load('firewall');
let changed = false, want = {};
// What changed, section by section, for syslog once it is saved (a dry run says nothing).
let notes = [];
let sec = (name, stype, opts) => {
want[name] = true;
changed = uci_section(c, 'firewall', name, stype) || changed;
let created = uci_section(c, 'firewall', name, stype), diff = {};
for (let k, v in opts)
changed = uci_put(c, 'firewall', name, k, v) || changed;
if (uci_put(c, 'firewall', name, k, v))
diff[k] = v;
if (created || length(keys(diff))) {
changed = true;
push(notes, [ 'info', `${stype} ${name}: ${created ? 'created' : 'updated'} · ${opts_text(created ? opts : diff)}` ]);
}
else
push(notes, [ 'debug', `${stype} ${name}: unchanged` ]);
};
let snap = net.snapshot(true);
@@ -130,17 +139,20 @@ export function sync(m, dry) {
push(stale, s['.name']);
});
for (let s in stale) {
push(notes, [ 'info', `${c.get('firewall', s) ?? 'section'} ${s}: removed` ]);
c.delete('firewall', s);
changed = true;
}
if (dry)
return changed;
for (let n in notes)
syslog(n[0], 'firewall', n[1]);
if (changed) {
c.commit('firewall');
ack_config([ 'firewall' ]);
syslog('info', 'firewall', 'fw4 -q reload');
system('fw4 -q reload');
system("logger -t dawnline 'firewall updated'");
}
return changed;
};
@@ -268,18 +280,24 @@ export function sync_reply_routes(m) {
}
let s = reply_script(m, proxy_diverts());
if (s == '') {
if (reply_loaded())
if (reply_loaded()) {
syslog('info', 'nftables', `table inet ${REPLY_TABLE}: removed (no proxy to keep Dawnline's answers out of)`);
system(`nft delete table inet ${REPLY_TABLE} 2>/dev/null`);
}
unlink(REPLY_SIG);
return;
}
if (s == readfile(REPLY_SIG) && reply_loaded())
if (s == readfile(REPLY_SIG) && reply_loaded()) {
syslog('debug', 'nftables', `table inet ${REPLY_TABLE}: unchanged`);
return;
}
let err = nft.load(`table inet ${REPLY_TABLE}\ndelete table inet ${REPLY_TABLE}\n${s}`);
if (err) {
unlink(REPLY_SIG);
system(`logger -t dawnline -p user.err ${q('reply table: ' + err)}`);
syslog('error', 'nftables', `table inet ${REPLY_TABLE}: loading failed · ${err}`);
return;
}
syslog('info', 'nftables', `table inet ${REPLY_TABLE}: loaded · ${length(match(s, /\n/g) ?? []) + 1} lines (Dawnline's own answers kept out of proxies)`);
syslog('debug', 'nftables', `table inet ${REPLY_TABLE}: ${s}`);
write_file(REPLY_SIG, s, 0o644);
};
@@ -7,7 +7,7 @@
import { readfile, stat, unlink } from 'fs';
import { cursor } from 'uci';
import {
ensure_dir, read_json, write_json, lock, unlock, month_key, day_key, day_key_ago, uniq,
ensure_dir, read_json, write_json, lock, unlock, month_key, day_key, day_key_ago, uniq, syslog,
iface_status as util_iface_status, RUN_DIR, DATA_DIR
} from 'dawnline.util';
import * as awg from 'dawnline.awg';
@@ -422,13 +422,51 @@ function with_names(e, c) {
return e;
}
// Events about someone's connection, not about the system: they stay out of syslog
// (the activity log has them). Connects, roams, a device's link trouble and its end.
const NOT_SYSTEM = [ 'connect', 'disconnect', 'roam', 'device_failing', 'device_recovered' ];
/**
* An activity event at its syslog level, or null when it stays out (a
* connection: syslog says what the system does). People and their devices
* (what changes the tunnels and the rules) are info, problems warn and their
* end ("…_up") info.
*/
export function event_level(e) {
let cat = category_of(e.type);
if (cat == 'connections' || index(NOT_SYSTEM, e.type) >= 0)
return null;
if (cat == 'problems' && !match(e.type, /_up$/))
return 'warn';
return 'info';
};
/** An activity event as a syslog line: "Myata · Phone device changed · kind=phone, lan_bypass=on (portal)". */
export function event_line(e) {
let who = join(' · ', filter([ e.pname ?? e.person, e.dname ?? e.device ], (x) => x != null && x != ''));
return join(' ', filter([ who, replace(e.type ?? 'event', /_/g, ' '), e.detail ? `· ${e.detail}` : '', e.by ? `(${e.by})` : '' ], (x) => x != ''));
};
/** The system's events to syslog, whatever the activity log records (its categories are the admin's choice for the page). */
function syslog_events(list, c) {
for (let e in list) {
let lvl = event_level(e);
if (lvl)
syslog(lvl, category_of(e.type), event_line(with_names(e, c)));
}
}
export function add_events(list, pol) {
pol ??= policy();
list = filter(list ?? [], (e) => index(pol.categories, category_of(e.type)) >= 0);
list ??= [];
if (!length(list))
return;
let c = cursor();
c.load('dawnline');
syslog_events(list, c);
list = filter(list, (e) => index(pol.categories, category_of(e.type)) >= 0);
if (!length(list))
return;
let l = lock('events');
let ev = load_events();
let ts = time();
@@ -451,6 +489,9 @@ export function add_event(e) {
* and device (when it has one) within `window` s.
*/
export function add_event_merged(e, window) {
let c0 = cursor();
c0.load('dawnline');
syslog_events([ { ...e } ], c0);
let pol = policy();
if (index(pol.categories, category_of(e.type)) < 0)
return;
@@ -84,6 +84,18 @@ export function resolve(host) {
return resolve_all([ host ])[host] ?? [];
};
/**
* The address to give an app that takes no name for the server (V2RayTun's
* Android core hands the endpoint over as it is): IPv4 first, as a phone on
* mobile data may have no IPv6. The host itself when it is an IP; null when
* the name does not resolve.
*/
export function host_ip(host) {
let ips = resolve(host);
let v4 = filter(ips, (x) => ip2int(x) != null);
return v4[0] ?? ips[0] ?? null;
};
/** The IPv4 address of a netifd interface (a zone name for older configs). */
export function wan_ipv4(zone) {
let st = live.iface_status(zone == 'wan' ? 'wan' : zone);
@@ -39,7 +39,7 @@
import { readfile, writefile, readlink, stat, unlink } from 'fs';
import { cursor } from 'uci';
import {
sh, sh_out, q, ip2int, int2ip, parse_cidr, cidr_any, ensure_dir, read_json, write_json, slog, RUN_DIR
sh, sh_out, q, ip2int, int2ip, parse_cidr, cidr_any, ensure_dir, read_json, write_json, slog, syslog, syslog_lines, RUN_DIR
} from 'dawnline.util';
import * as net from 'dawnline.net';
import * as core from 'dawnline.core';
@@ -60,12 +60,20 @@ function log(msg, level, detail) {
slog('dawnline', level ?? 'info', msg, detail, WHO);
}
/**
* Run the commands that set the path up or take it down, each in syslog as
* it is given ("$ ip netns add dawnline"), a failure with what it said.
* Only called when something changes (ensure() returns early otherwise).
*/
function run(cmds) {
let errs = [];
syslog_lines('info', 'loop', join('\n', cmds), '$ ');
for (let c in cmds) {
let r = sh(c);
if (r.code != 0)
if (r.code != 0) {
push(errs, `${c}: ${trim(r.out)}`);
syslog('error', 'loop', `failed: ${c} · ${trim(r.out)}`);
}
}
return errs;
}
@@ -232,8 +240,8 @@ function pick_table(saved) {
* { code: 'setup', detail } a command failed (detail: it and what it said)
*/
/** A reason from plan() for the service log: English, like every log line. */
function error_note(e) {
/** A reason from plan() for the service log and syslog: English, like every log line. */
export function error_note(e) {
if (e.code == 'no_lan')
return L_('No LAN detected.');
if (e.code == 'not_bridge')
@@ -245,7 +253,7 @@ function error_note(e) {
if (e.code == 'no_transit')
return L_('No free link-local range for the internal link.');
return L_('No free routing table.');
}
};
/**
* Why "like a home device" is off (a reason above), as a sentence in the
@@ -5,7 +5,7 @@
import { cursor } from 'uci';
import { stat, unlink } from 'fs';
import { q, slog } from 'dawnline.util';
import { slog, syslog } from 'dawnline.util';
import * as core from 'dawnline.core';
import * as store from 'dawnline.store';
import { L_ } from 'dawnline.i18n';
@@ -47,7 +47,7 @@ export function uhttpd() {
c.delete('uhttpd', 'main', 'error_page');
c.commit('uhttpd');
system('/etc/init.d/uhttpd reload >/dev/null 2>&1');
system(`logger -t dawnline ${q('portal removed from the LuCI web server')}`);
syslog('info', 'portal', 'portal removed from the LuCI web server');
}
if (stat(LAN_CGI))
unlink(LAN_CGI);
@@ -8,7 +8,7 @@
import { cursor } from 'uci';
import { stat, unlink, writefile } from 'fs';
import { q, uci_put, uci_section, ack_config, iface_intent, ensure_dir, read_json, write_json, slog, RUN_DIR } from 'dawnline.util';
import { q, uci_put, uci_section, ack_config, iface_intent, ensure_dir, read_json, write_json, slog, syslog, opts_text, RUN_DIR } from 'dawnline.util';
import * as core from 'dawnline.core';
import * as awg from 'dawnline.awg';
import { L_ } from 'dawnline.i18n';
@@ -95,10 +95,17 @@ export function sync(m) {
if (!tools)
continue;
// Per server: with several, each changed one has to be brought up again.
let mine = uci_section(c, 'network', ifn, 'interface');
let created = uci_section(c, 'network', ifn, 'interface');
let mine = created, diff = {};
for (let k, v in options(srv))
if (uci_put(c, 'network', ifn, k, v))
if (uci_put(c, 'network', ifn, k, v)) {
mine = true;
diff[k] = v;
}
if (mine)
syslog('info', 'network', `interface ${ifn} (${srv.name}): ${created ? 'created' : 'updated'} · ${opts_text(diff)}`);
else
syslog('debug', 'network', `interface ${ifn} (${srv.name}): unchanged · ${opts_text(options(srv))}`);
// The markers are saved, but netifd does not read them: a renamed server
// keeps its tunnel and every session in it.
if (uci_put(c, 'network', ifn, '_dawnline', id))
@@ -127,6 +134,7 @@ export function sync(m) {
});
for (let ifn in stale) {
iface_intent(ifn, 'removed', c.get('network', ifn, '_dawnline_name') ?? ifn);
syslog('info', 'network', `interface ${ifn} (${c.get('network', ifn, '_dawnline_name') ?? 'a removed server'}): removed, ifdown ${ifn}`);
system(`ifdown ${q(ifn)} 2>/dev/null`);
c.delete('network', ifn);
changed = true;
@@ -138,12 +146,14 @@ export function sync(m) {
}
// Only the markers changed: nothing for netifd to read (it ignores them, and so does a reload).
if (length(touched) || length(stale)) {
syslog('info', 'network', 'ubus call network reload');
system('ubus call network reload');
for (let ifn in touched)
if (m.servers[c.get('network', ifn, '_dawnline')]?.enabled)
if (m.servers[c.get('network', ifn, '_dawnline')]?.enabled) {
syslog('info', 'network', `ifup ${ifn}`);
system(`ifup ${q(ifn)}`);
}
// Each tunnel's own line (restarting, turned off, removed) comes from hotplug.
system(`logger -t dawnline ${q('network updated: ' + join(', ', [ ...touched, ...stale ]))}`);
}
return changed;
};
@@ -5,7 +5,7 @@
// changes often (update, one atomic transaction).
import { unlink } from 'fs';
import { sh, ensure_dir, write_file, rand_hex, ip2int, uniq, RUN_DIR } from 'dawnline.util';
import { sh, ensure_dir, write_file, rand_hex, ip2int, uniq, read_json, write_json, syslog, syslog_lines, some_of, RUN_DIR } from 'dawnline.util';
import * as core from 'dawnline.core';
import * as net from 'dawnline.net';
import * as loop from 'dawnline.loop';
@@ -14,6 +14,55 @@ import * as loop from 'dawnline.loop';
export const PRIVATE4 = [ '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', '100.64.0.0/10', '169.254.0.0/16', '224.0.0.0/3' ];
/** The sets whose members change at run time (update replaces them together). */
const SETS = [ 'lan_ok', 'lan_all', 'lan_part', 'mesh_pairs', 'inet_ok', 'svc_ok', 'svc_host', 'dns_real' ];
// The sets' members as last loaded, so a log line can say what changed.
const SETS_SEEN = `${RUN_DIR}/nft-sets.json`;
/** What a table script holds: its chains, its sets and the rules in its chains (for a log line). */
export function shape(t) {
let n = { chains: 0, sets: 0, rules: 0 }, in_chain = false;
for (let l in split(t ?? '', '\n')) {
l = trim(l);
if (substr(l, 0, 6) == 'chain ') {
n.chains++;
in_chain = true;
}
else if (substr(l, 0, 4) == 'set ')
n.sets++;
else if (in_chain && l == '}')
in_chain = false;
else if (in_chain && l != '' && substr(l, 0, 5) != 'type ' && substr(l, 0, 7) != 'policy ' && substr(l, 0, 1) != '#')
n.rules++;
}
return n;
};
/**
* The sets in syslog: info with each set that changed ("inet_ok 3 → 4: +10.8.0.5"),
* debug when none did; remembered for the next time.
*/
function log_sets(sets, how) {
let first = read_json(SETS_SEEN, null) == null;
let was = read_json(SETS_SEEN, {}), now = {}, diff = [];
for (let name in SETS) {
let a = uniq(was[name] ?? []), b = uniq(sets[name] ?? []);
now[name] = b;
let add = filter(b, (x) => index(a, x) < 0), del = filter(a, (x) => index(b, x) < 0);
if (length(add) || length(del))
push(diff, `${name} ${length(a)} → ${length(b)}${length(add) ? ': +' + some_of(add) : ''}${length(del) ? (length(add) ? ' ' : ': ') + '-' + some_of(del) : ''}`);
}
write_json(SETS_SEEN, now, 0o600);
// The first load since the router started: how many in each set (the members at debug).
if (first) {
syslog('info', 'nftables', `table inet dawnline: ${how} · ${join(', ', map(SETS, (n) => `${n} ${length(now[n])}`))}`);
for (let n in SETS)
if (length(now[n]))
syslog('debug', 'nftables', `set ${n}: ${join(' ', now[n])}`);
}
else if (length(diff))
syslog('info', 'nftables', `table inet dawnline: ${how} · ${join('; ', diff)}`);
else
syslog('debug', 'nftables', `table inet dawnline: ${how} · sets unchanged (${join(', ', map(SETS, (n) => `${n} ${length(now[n])}`))})`);
}
function ip4(a) {
return ip2int(a ?? '') != null;
@@ -260,7 +309,17 @@ export function render(m, sets, ctx) {
/** Full table rebuild (structure changes: servers, home networks, DNS paths); null or the error. */
export function rebuild(m, sets) {
return load('table inet dawnline\ndelete table inet dawnline\n' + table(m, sets));
let t = table(m, sets);
let err = load('table inet dawnline\ndelete table inet dawnline\n' + t);
if (err) {
syslog('error', 'nftables', `table inet dawnline: rebuild failed · ${err}`);
return err;
}
let n = shape(t);
syslog('info', 'nftables', sprintf('table inet dawnline: rebuilt · %d chains, %d sets, %d rules', n.chains, n.sets, n.rules));
syslog_lines('debug', 'nftables', t, 'nft: ');
log_sets(sets, 'sets loaded with the table');
return null;
};
/**
@@ -276,7 +335,12 @@ export function update(sets) {
if (length(members))
s += `add element inet dawnline ${name} { ${join(', ', members)} }\n`;
}
return load(s);
let err = load(s);
if (err)
syslog('warn', 'nftables', `table inet dawnline: set update failed, rebuilding · ${err}`);
else
log_sets(sets, 'sets updated');
return err;
};
/** Is the table loaded (watchdog). */
@@ -5,7 +5,7 @@
// GET /<code>/api/state
// POST /<code>/api/devices { name, kind, public_key?, lan_bypass? }
// GET /<code>/api/devices/<id>/config
// POST /<code>/api/devices/<id> { action: rename|rotate|delete, name? }
// POST /<code>/api/devices/<id> { action: rename|reconnect|bypass|delete, name?, kind?, lan_bypass?, on? }
//
// The personal link is the key: every request carries its code, there are
// no sessions. Without a valid code every path answers the same bare 404 —
@@ -33,10 +33,11 @@
// that answers anything not shaped like a link (its not_found() twin).
import { stat, readfile } from 'fs';
import { read_json, write_json, ensure_dir, lock, unlock, cloudflare_ip, ip2int, today, VERSION, RUN_DIR } from 'dawnline.util';
import { read_json, write_json, ensure_dir, lock, unlock, cloudflare_ip, ip2int, today, syslog, VERSION, RUN_DIR } from 'dawnline.util';
import * as core from 'dawnline.core';
import * as awg from 'dawnline.awg';
import * as live from 'dawnline.live';
import * as lookup from 'dawnline.lookup';
import * as view from 'dawnline.view';
import * as schema from 'dawnline.schema';
import * as devices from 'dawnline.devices';
@@ -423,6 +424,17 @@ function api_state(r) {
send_json(r, '200 OK', state(r));
}
/**
* A configuration as the portal sends it: with the server's address as an IP
* too (amnezia.host_ip, lookup.host_ip()), for V2RayTun on Android, which
* takes no name for AmneziaWG. Lookups are cached for minutes (lookup.uc).
*/
function with_ip(cfg) {
if (cfg?.amnezia)
cfg.amnezia.host_ip = lookup.host_ip(cfg.amnezia.host);
return cfg;
}
function api_add(r) {
need_open(r);
need_https(r);
@@ -433,7 +445,7 @@ function api_add(r) {
// lan_bypass: JSON true keeps the device's own local network out of the tunnel; anything else is off.
let res = guard(r, () => devices.add(r.person.id, b.name, b.kind, { by: 'portal', public_key: b.public_key, lan_bypass: b.lan_bypass === true }));
reload(r);
send_json(r, '200 OK', { id: res.id, config: res.config, state: state(r) });
send_json(r, '200 OK', { id: res.id, config: with_ip(res.config), state: state(r) });
}
function api_config(r, id) {
@@ -441,7 +453,7 @@ function api_config(r, id) {
need_open(r);
need_https(r);
awg.mark_issued(r.m, d.id);
send_json(r, '200 OK', awg.client_config(r.m, d));
send_json(r, '200 OK', with_ip(awg.client_config(r.m, d)));
}
/** POST /devices/<id> { action }: what each action does; the answer carries the new state. */
@@ -455,8 +467,20 @@ const ACTIONS = {
need_https(r);
if (!devices.ready())
fail(r, '503 Service Unavailable', not_ready());
let config = guard(r, () => devices.reconnect(d.id, { by: 'portal', person: r.person.id, defer: d.address == r.peer }));
return { config };
// What it is and LAN Bypass may change with the keys (the sheet asks again); left out, they stay.
let kind = type(b.kind) == 'string' ? b.kind : null;
let bypass = type(b.lan_bypass) == 'bool' ? b.lan_bypass : null;
let config = guard(r, () => devices.reconnect(d.id, { by: 'portal', person: r.person.id, defer: d.address == r.peer,
kind, lan_bypass: bypass }));
return { config: with_ip(config) };
},
// LAN Bypass on or off, keys unchanged: the device takes its updated configuration (the set-up sheet opens on it).
bypass: (r, d, b) => {
need_https(r);
if (type(b.on) != 'bool')
fail(r, '400 Bad Request', _('Unknown action.'));
guard(r, () => devices.update(d.id, { lan_bypass: b.on }, { by: 'portal', person: r.person.id }));
return {};
},
'delete': (r, d, b) => {
guard(r, () => devices.remove(d.id, { by: 'portal', person: r.person.id }));
@@ -590,7 +614,11 @@ export function serve(io) {
handle(io);
}
catch (e) {
if (e.message != DONE)
if (e.message != DONE) {
// What broke and where; the path without its first part (the person's link code is a secret).
let path = replace(split(io.env.REQUEST_URI ?? '', '?')[0], /^\/[^\/]*/, '/<link>');
syslog('error', 'portal', `${io.env.REQUEST_METHOD ?? '?'} ${path}: 500 · ${e.message}${e.stacktrace?.[0]?.context ? ' · ' + replace(e.stacktrace[0].context, /[\r\n\t]+/g, ' ') : ''}`);
io.write(`Status: 500 Internal Server Error\r\nContent-Type: text/plain\r\n\r\nError\n`);
}
}
};
@@ -6,7 +6,7 @@
import { readfile, writefile, stat } from 'fs';
import { cursor } from 'uci';
import { ensure_dir, iface_status, uniq } from 'dawnline.util';
import { ensure_dir, iface_status, uniq, syslog, syslog_lines } from 'dawnline.util';
import * as net from 'dawnline.net';
import * as core from 'dawnline.core';
@@ -90,10 +90,15 @@ export function sync(m, target, lan) {
for (let d in lan_domain(m))
push(lines, `server=/${d}/${lan}`);
let conf = join('\n', lines) + '\n';
if ((readfile(CONF) ?? '') == conf)
let where = target.mode == 'ns' ? `${target.address}:${target.port} in the netns` : length(ifs) ? `port ${target.port} on ${join(', ', ifs)}` : 'loopback only (no tunnel on)';
if ((readfile(CONF) ?? '') == conf) {
syslog('debug', 'dns', `resolver for Home-only devices: unchanged · ${where}`);
return false;
}
writefile(CONF, conf);
system(`chmod 644 ${CONF}`);
syslog('info', 'dns', `resolver for Home-only devices: ${CONF} written · ${where} · upstream ${join(' ', wan_dns()) || 'none'}${lan ? ` · home names via ${lan}` : ''}; restarting it`);
syslog_lines('debug', 'dns', conf, 'dnsmasq: ');
let pid = trim(readfile(PID) ?? '');
if (match(pid, /^[0-9]+$/))
system(`kill ${pid} 2>/dev/null`);
@@ -8,6 +8,7 @@ import * as view from 'dawnline.view';
import * as awg from 'dawnline.awg';
import * as store from 'dawnline.store';
import * as devices from 'dawnline.devices';
import * as lookup from 'dawnline.lookup';
import { wrap, str, clamp_days } from 'dawnline.rpc.common';
import { _ } from 'dawnline.i18n';
@@ -155,7 +156,13 @@ function device_config(a) {
die(_('Unknown device.'));
if (a.issue === true)
awg.mark_issued(m, d.id);
return awg.client_config(m, d);
let cfg = awg.client_config(m, d);
// V2RayTun on Android takes the server by its IP (the admin's set-up shows its link for phones, tablets and
// other devices): only where it can connect at all, and AmneziaWG 3.1 (Maximum) is beyond it.
let srv = m.servers[m.people[d.person]?.server];
if (cfg && d.kind != 'router' && d.kind != 'computer' && srv?.preset != 'max')
cfg.amnezia.host_ip = lookup.host_ip(cfg.amnezia.host);
return cfg;
}
/** Name, on/off and the link values (mtu, keepalive: strings, '' = the server's; lan_bypass: true or false). */
@@ -2,7 +2,7 @@
// history, updates). What may be saved is dawnline.schema's.
import { stat } from 'fs';
import { q, sh_out, admin_log, RUN_DIR, DATA_DIR } from 'dawnline.util';
import { q, sh_out, admin_log, syslog, RUN_DIR, DATA_DIR } from 'dawnline.util';
import * as core from 'dawnline.core';
import * as live from 'dawnline.live';
import * as apply from 'dawnline.apply';
@@ -96,16 +96,25 @@ function settings_update(a) {
die(_('Nothing to save.'));
let res = store.transact((c, m) => {
let restart = false;
// What each key was and became, as uci holds them (for syslog: every change, not only those worth a line).
let diff = [];
let note = (k, to) => {
let from = c.get(core.CONFIG, 'settings', k);
if (!same(to, from))
push(diff, { k, from, to });
};
c.set(core.CONFIG, 'settings', 'settings');
for (let k, val in v) {
if (k == 'log_category') {
let lst = filter(type(val) == 'array' ? val : [], (x) => index(live.CATEGORIES, x) >= 0);
note(k, length(lst) ? lst : [ '' ]);
c.set(core.CONFIG, 'settings', 'log_category', length(lst) ? lst : [ '' ]);
// Superseded by the category list.
c.delete(core.CONFIG, 'settings', 'log_connections');
continue;
}
let nv = schema.validate(k, val);
note(k, nv);
if (schema.SETTINGS[k].restart && !same(nv, c.get(core.CONFIG, 'settings', k)))
restart = true;
if (nv == null)
@@ -114,8 +123,13 @@ function settings_update(a) {
c.set(core.CONFIG, 'settings', k, nv);
}
check_portal(core.load(c), m);
return { restart, was: m.settings };
return { restart, was: m.settings, diff };
});
// Every setting that changed, in syslog: "admin changed settings · log_keep_days: 30 → 90, domain: lan → home".
if (length(res.diff)) {
let val = (x) => x == null || x == '' ? 'default' : type(x) == 'array' ? join(' ', filter(x, (y) => y != '')) || 'none' : `${x}`;
syslog('info', 'settings', `admin changed settings · ${join(', ', map(res.diff, (d) => `${d.k}: ${val(d.from)} → ${val(d.to)}`))}`);
}
let s = null;
if (res.restart)
system('/etc/init.d/dawnline reload >/dev/null 2>&1');
@@ -127,6 +127,8 @@ export const SETTINGS = {
geo_enabled: { type: 'bool', dflt: false },
geo_provider: { type: 'enum', dflt: 'ipwho.is', values: [ 'ipwho.is', 'ipapi.co' ] },
log_connections: { type: 'bool', dflt: true },
// What goes to syslog (dawnline.util syslog): error | warn | info | debug (each step of a start or an apply).
log_level: { type: 'enum', dflt: 'info', values: [ 'error', 'warn', 'info', 'debug' ] },
watchdog: { type: 'bool', dflt: true },
log_keep_days: { type: 'int', dflt: 30, min: 0, max: 9999 },
log_max: { type: 'int', dflt: 2000, min: 100, max: 20000 },
@@ -158,6 +160,7 @@ function messages() {
default_device_limit: _('Default device limit must be 1-32.'),
domain: _('Domain may use lowercase letters, digits, dashes and dots.'),
geo_provider: _('Unknown location service.'),
log_level: _('Choose a system log level: errors, warnings, normal or verbose.'),
log_keep_days: _('Keep activity for 0 (no age limit) to 9999 days.'),
log_max: _('Log size must be 100-20000 events.'),
log_persist: _('Save the log every 10min, hourly or off.'),
@@ -5,7 +5,7 @@
import { open, readfile, writefile, rename, mkdir, stat, unlink, popen, lsdir } from 'fs';
import { _ } from 'dawnline.i18n';
export const VERSION = '0.17.0';
export const VERSION = '0.18.0';
/** GitHub repository with Dawnline releases (updates and the installer). */
export const REPO = 'tickcount/luci-app-dawnline';
export const RUN_DIR = '/var/run/dawnline';
@@ -226,6 +226,96 @@ export function ack_config(confs) {
writefile(md5f, join('\n', lines) + '\n');
};
/* ---------- syslog ---------- */
/*
* Every line Dawnline puts in syslog, as podkop and sing-box do it:
* dawnline: [info] [awg] Home started · accepting connections on port 51820
* with the matching priority (daemon.err, .warning, .info, .debug), so
* `logread -e dawnline` or a remote syslog filters by level. The admin's
* level (settings.log_level) decides what goes out: error | warn | info
* (the default) | debug (each step of a start or an apply).
*/
const LOG_RANK = { error: 0, warn: 1, info: 2, debug: 3 };
const LOG_PRIO = { error: 'err', warn: 'warning', info: 'info', debug: 'debug' };
const LOG_LEVEL_RE = regexp("option log_level '?(error|warn|info|debug)'?");
// The level as last read, and the config file's mtime it was read at (a long-running agent sees a change).
let log_cfg = { mtime: -1, level: 'info' };
/** The admin's syslog level (settings.log_level): error | warn | info | debug; info when not set. */
export function log_level() {
let mt = stat('/etc/config/dawnline')?.mtime ?? 0;
if (mt != log_cfg.mtime) {
let m = match(readfile('/etc/config/dawnline') ?? '', LOG_LEVEL_RE);
log_cfg = { mtime: mt, level: m?.[1] ?? 'info' };
}
return log_cfg.level;
};
/** Does a line at `level` go out at the admin's level? */
export function log_on(level) {
return (LOG_RANK[level] ?? 2) <= LOG_RANK[log_level()];
};
// A line break or a tab with the blanks around it: one space in a syslog line (compiled once, syslog runs often).
const LOG_BREAK = /[ \t\r\n]*[\t\r\n][ \t\r\n]*/g;
/** One syslog line's words: "[level] [src] line", on one line, at most 1000 characters. */
export function syslog_line(level, src, line) {
let one = replace(trim(`${line ?? ''}`), LOG_BREAK, ' ');
return substr(`[${LOG_RANK[level] != null ? level : 'info'}] [${src ?? 'dawnline'}] ${one}`, 0, 1000);
};
/**
* A line in syslog at `level` (error | warn | info | debug) from `src`
* (dawnline, awg, portal, apply, agent, service, settings, people, …), when
* the admin's level takes it. Nothing is kept anywhere else: what the admin UI
* shows goes through slog() below, which calls this too.
*/
export function syslog(level, src, line) {
if (!log_on(level))
return;
system(`logger -t dawnline -p daemon.${LOG_PRIO[level] ?? 'info'} ${q(syslog_line(level, src, line))}`);
};
// Option names whose values never go in a log line: private, preshared and header-protection keys.
const SECRET_OPT = /(private|preshared|protection)_?key/;
/**
* Options as a log line's words: "listen_port=51820, addresses=10.8.0.1/24,
* private_key=***" (arrays joined with spaces, null as "-", secrets masked).
*/
export function opts_text(o) {
let out = [];
for (let k, v in o ?? {}) {
let s = v == null ? '-' : type(v) == 'array' ? join(' ', v) : `${v}`;
push(out, `${k}=${match(k, SECRET_OPT) ? '***' : s}`);
}
return join(', ', out);
};
/**
* A block of text in syslog, a line per line (an nft script, a peer list),
* each "[level] [src] <prefix><line>": one logger for all of them.
*/
export function syslog_lines(level, src, text, prefix) {
if (!log_on(level) || trim(`${text ?? ''}`) == '')
return;
let p = popen(`logger -t dawnline -p daemon.${LOG_PRIO[level] ?? 'info'}`, 'w');
if (!p)
return;
for (let l in split(`${text ?? ''}`, '\n'))
if (trim(l) != '')
p.write(syslog_line(level, src, `${prefix ?? ''}${trim(l)}`) + '\n');
p.close();
};
/** A list in a log line: the first `n` (10) and how many more ("a, b, c and 12 more"). */
export function some_of(list, n) {
n ??= 10;
return length(list) <= n ? join(', ', list) : `${join(', ', slice(list, 0, n))} and ${length(list) - n} more`;
};
/* ---------- service log ---------- */
/*
@@ -257,7 +347,11 @@ export function slog(src, level, msg, detail, who, extra) {
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)}`);
// Problems carry what went wrong (the service log's detail: an error's text, the syslog lines around a crash).
let lvl = { error: 'error', repair: 'warn', down: 'warn' }[level] ?? 'info';
if (e.detail && lvl != 'info')
line += ` · ${substr(replace(e.detail, /[\r\n\t]+/g, ' | '), 0, 400)}`;
syslog(lvl, src, line);
};
/** A change the admin made in LuCI, for the service log. */