Files
dawnline/package/luci-app-dawnline/root/usr/bin/dawnline
T
Aemeath 82ab86848e 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
2026-10-15 17:05:24 +03:00

168 lines
5.8 KiB
Plaintext
Executable File

#!/usr/bin/ucode
// Dawnline command line: `dawnline help`.
'use strict';
import * as core from 'dawnline.core';
import * as live from 'dawnline.live';
import * as apply from 'dawnline.apply';
import * as view from 'dawnline.view';
import * as ops from 'dawnline.ops';
import * as watchdog from 'dawnline.watchdog';
import { VERSION, slog, sh_out, q, take_iface_intent } from 'dawnline.util';
const args = ARGV;
const cmd = args[0] ?? 'help';
function out(v) {
print(type(v) == 'string' ? v + '\n' : sprintf('%.J\n', v));
}
function opt(name, dflt) {
for (let i = 1; i < length(args); i++)
if (args[i] == `--${name}`)
return args[i + 1];
return dflt;
}
function help() {
out(`Dawnline ${VERSION}
Usage: dawnline <command>
apply Reconcile router configuration and runtime state
sync Push devices and access rules to the tunnel
status People and devices with live state (JSON)
health Run health checks (JSON)
person add <name> [--slug s] [--access lan|internet|both] [--devices n]
[--expires YYYY-MM-DD] [--quota GB]
person link <slug> Print the personal portal link
person rm <slug> Remove a person and their devices
device add <slug> <name> [--kind phone|computer|tablet|router]
device conf <id> Print a device configuration
device rm <id> Remove a device
backup Create a backup in /etc/dawnline/backups
portal-cert Print the certificate and key the portal uses
portal-http-port Print the plain-HTTP port when an IP is among the portal addresses
log Service log: tunnel and service starts, stops, repairs (newest first)
teardown Remove everything Dawnline created on the router
version`);
}
function commit(c) {
c.commit('dawnline');
system('chmod 600 /etc/config/dawnline');
}
try {
if (cmd == 'apply')
out(apply.apply());
else if (cmd == 'sync')
out(apply.sync());
else if (cmd == 'status') {
let m = core.load(), u = core.load_usage(), d = live.dump(), st = view.states(m, u);
out(map(values(m.people), (p) => view.person_view(m, p, u, d, st, { admin: true })));
}
else if (cmd == 'health')
out(ops.health());
else if (cmd == 'person' && args[1] == 'add') {
let c = core.open_cursor(), m = core.load(c);
let id = core.create_person(c, m, {
name: args[2], slug: opt('slug'), access: opt('access', 'internet'),
device_limit: opt('devices'), expires: opt('expires'), quota_gb: opt('quota'),
});
commit(c);
let m2 = core.load();
out(core.portal_url(m2, m2.people[id]) ?? id);
}
else if (cmd == 'person' && args[1] == 'link') {
let m = core.load();
let p = m.people[args[2]] ?? die('Unknown person.');
out(core.portal_url(m, p) ?? 'Set settings.portal_host first.');
}
else if (cmd == 'person' && args[1] == 'rm') {
let c = core.open_cursor(), m = core.load(c);
core.delete_person(c, m, args[2]);
commit(c);
apply.sync();
}
else if (cmd == 'device' && args[1] == 'add') {
let c = core.open_cursor(), m = core.load(c);
let id = core.create_device(c, m, args[2], args[3], opt('kind', 'phone'), { by: 'admin' });
commit(c);
apply.sync();
out(id);
}
else if (cmd == 'device' && args[1] == 'conf') {
let m = core.load();
let d = m.devices[args[2]] ?? die('Unknown device.');
print(core.client_config(m, d).conf);
}
else if (cmd == 'device' && args[1] == 'rm') {
let c = core.open_cursor(), m = core.load(c);
core.delete_device(c, m, args[2]);
commit(c);
apply.sync();
}
else if (cmd == 'backup')
out(ops.backup_create(opt('reason')));
else if (cmd == 'portal-cert') {
let c = ops.portal_cert(core.load().settings);
out(`${c.cert} ${c.key}`);
}
else if (cmd == 'portal-http-port') {
let m = core.load();
if (core.portal_http(m))
out(`${core.portal_http_port(m)}`);
}
else if (cmd == 'log') {
for (let e in watchdog.view(core.load()).log) {
let t = localtime(e.ts);
let text = e.by == 'admin'
? join(' ', filter([ 'You', e.msg, e.kind, e.who, e.note ? '· ' + e.note : null ], (x) => x))
: (e.who ? e.who + ' ' : '') + e.msg;
out(sprintf('%02d-%02d %02d:%02d %-8s %-6s %s', t.mon, t.mday, t.hour, t.min, e.src, e.level, text));
if (e.detail)
out(' ' + replace(e.detail, '\n', '\n '));
}
}
else if (cmd == 'iface-event') {
// From the iface hotplug script: a tunnel started, stopped or failed to start.
let ifname = args[2] ?? '';
if (!match(ifname, /^[a-z][a-z0-9_]{0,15}$/))
die('Bad interface.');
// Dawnline leaves a note before it takes a tunnel down itself; anything else is unexpected.
let it = args[1] == 'ifdown' ? take_iface_intent(ifname) : null;
let who = it?.name ?? ifname;
for (let id, srv in core.load().servers)
if (srv.ifname == ifname)
who = srv.name;
if (args[1] == 'ifup') {
let port = sh_out(`awg show ${q(ifname)} listen-port 2>/dev/null`);
slog('awg', 'up', port ? `started · accepting connections on port ${port}` : 'started', null, who);
}
else if (args[1] == 'ifdown') {
// Stops Dawnline made itself are explained by the admin's own line
// (changed, turned off, removed); only an AmneziaWG update gets one here.
if (it?.reason == 'update')
slog('awg', 'info', 'restarting for the AmneziaWG update', null, who);
else if (!it)
slog('awg', 'down', 'stopped unexpectedly', join('\n', filter([
"Dawnline didn't stop it: the router's network restarted, someone stopped it by hand, or it failed.",
watchdog.iface_errors(ifname),
], (x) => x)), who);
}
else if (args[1] == 'ifup-failed')
slog('awg', 'error', "couldn't start", watchdog.iface_errors(ifname), who);
}
else if (cmd == 'teardown')
apply.teardown();
else if (cmd == 'version')
out(VERSION);
else
help();
}
catch (e) {
warn(`dawnline: ${e.message}\n`);
exit(1);
}