Files
dawnline/package/luci-app-dawnline/root/usr/bin/dawnline
T
Aemeath 34293fb8e5
Build packages / Version (push) Waiting to run
Dawnline 0.4.0
- Components tab replaces Updates: Dawnline, the AmneziaWG build for this
  firmware (Slava-Shchipunov 2.0 or 2Grey 3.1, Russian translation) and
  the system packages Dawnline adds, each checked, installed, reinstalled
  or rolled back in place, with What's new, versions and an activity log
- Automatic updates: daily check, install at a set time or right away,
  per component; AmneziaWG is left out by default and an automatic run
  never switches its build or generation
- Downloads verified against GitHub's sha256; mirrors are used only when
  GitHub is unreachable and the hash is known
- New installer: checks firmware, clock, space and DNS, lets you pick a
  release, shows the plan and asks first; --dry-run
- New uninstaller removes everything Dawnline added (cron, nft, firewall,
  routing, interfaces, packages it installed); --dry-run
- Works without AmneziaWG on the router: Overview, the setup wizard,
  Health and Components say what is missing and offer to install it
- Overview shows failed health checks as notices with their fix
- People list no longer reads the config from the browser: private keys
  stay on the router; personal links are fetched on demand
- Config changes are serialized under one lock, the nft table loads
  atomically and fails closed, and Dawnline never touches network
  sections it did not create
- The portal never opens system ports, keeps disabled devices disabled,
  rate-limits per address and counts days by the router's clock
- Settings and Diagnostics load their heavy parts in the background
- Release notes are written from the commits since the previous release
- Backend and admin UI split into small single-purpose modules
2026-10-16 02:14:15 +03:00

207 lines
7.1 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 health from 'dawnline.health';
import * as backup from 'dawnline.backup';
import * as pc from 'dawnline.portalcert';
import * as watchdog from 'dawnline.watchdog';
import * as awg from 'dawnline.awg';
import * as store from 'dawnline.store';
import {
VERSION, slog, sh_out, q, take_iface_intent, read_json, write_json, ensure_dir, with_lock, RUN_DIR
} 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 tunnels
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 (.conf)
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`);
}
/*
* netifd retries a tunnel that fails to start every few seconds (a kernel
* module that won't load, AmneziaWG removed): one line per interface every
* 10 minutes, with how many attempts failed meanwhile. A start clears it.
*/
const FAILS = `${RUN_DIR}/ifup-failed.json`;
const FAIL_EVERY = 600;
function ifup_failed(ifname, who) {
let now = time();
// Attempts since the last line (a quiet spell starts a new count), and over how long.
let r = with_lock('ifup-failed', () => {
let all = read_json(FAILS, {});
let f = all[ifname];
if (!f || now - f.last > FAIL_EVERY)
f = all[ifname] = { logged: 0, count: 0, since: now };
f.count++;
f.last = now;
let out = null;
if (now - f.logged >= FAIL_EVERY) {
out = { n: f.count, mins: int((now - f.since) / 60) };
f.logged = f.since = now;
f.count = 0;
}
ensure_dir(RUN_DIR);
write_json(FAILS, all, 0o600);
return out;
});
if (r)
slog('awg', 'error', r.n > 1 ? `couldn't start · ${r.n} failed attempts in ${r.mins} min` : "couldn't start",
watchdog.iface_errors(ifname), who);
}
function ifup_ok(ifname) {
with_lock('ifup-failed', () => {
let all = read_json(FAILS, {});
if (all[ifname]) {
delete all[ifname];
write_json(FAILS, all, 0o600);
}
});
}
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(m), 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(health.health());
else if (cmd == 'person' && args[1] == 'add') {
let id = store.transact((c, m) => 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'),
}));
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') {
store.transact((c, m) => core.delete_person(c, m, args[2]));
apply.sync();
}
else if (cmd == 'device' && args[1] == 'add') {
// Keys first: generating them runs the awg tool, not worth holding the config lock for.
let keys = awg.new_keys();
let id = store.transact((c, m) => core.create_device(c, m, args[2], args[3], opt('kind', 'phone'), { by: 'admin', keys }));
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(awg.client_config(m, d).conf);
}
else if (cmd == 'device' && args[1] == 'rm') {
store.transact((c, m) => core.delete_device(c, m, args[2]));
apply.sync();
}
else if (cmd == 'backup')
out(backup.backup_create(opt('reason')));
else if (cmd == 'portal-cert') {
let c = pc.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') {
ifup_ok(ifname);
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')
ifup_failed(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);
}