Dawnline 0.20.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 / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
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 / Backend (Go: vet, tests, a real ubusd and uhttpd) (push) Blocked by required conditions
CI / Package files (push) Blocked by required conditions
- **Backend rewritten in Go.** One daemon, `dawnlined`, replaces the whole ucode backend: the rpcd plugin, the agent, the CLI and the updater. The package no longer depends on ucode. - **Per-architecture builds.** The release carries the backend for arm64, ARMv7, mips, mipsle and amd64; the router fetches its own after the package is installed and checks its sha256. Dawnline does not run on other architectures (older ARM, 32-bit x86, RISC-V). - **Faster on the router.** A firewall sync takes 0.06 s instead of 0.9 s; a full apply takes about 300 ms. The portal is served by the daemon itself, without a CGI process per request. - **Live pages.** The admin and the portal follow a live stream from the router instead of re-fetching the whole state every 30 seconds. - **Admin redesigned in the portal's style.** Three sections (Overview · Log · Settings); a server is a page of its own with People, Places and Settings; a device opens as a dialog over its person. - **Changes wait for «Apply».** Every setting, and a person's access, is staged in one dock before it reaches the router. - **Configuration rollout.** After a change that makes devices fetch their configuration again, the admin shows how many have fetched it and who has not. Every setting shows beforehand (⟳N) how many devices it will touch. - **Waiting for a decision.** Overview and People put first the people who need one (access ended, limit reached, a device that does not connect), each with its one action a tap away. - **Bulk actions on people.** «Select» (or a long press on a card) picks several people to extend by a month, pause or resume at once. - **Undo.** An action done from a card or a request is undone from its toast. - **Update dialogs redesigned.** An update is one view from its question to its result; Dawnline's own update runs in it like every other, and the page reloads itself once the new version answers. Other versions are a list, each with its notes and Install on a page of its own. - **Site to site: networks behind a router.** A router connected as a device can have networks behind it (up to eight, /16 to /30): the server routes them and applies the router's access to them. A network that shares addresses with a tunnel, the router's own networks or another site is refused by name. - **Routers from the portal.** People can add a router and name its networks on their own page; the admin accepts or declines with a reason, or lets them through without asking. - **Check access and Health know the networks behind routers.** - **Usage limits reworked.** A monthly limit no longer follows the calendar. A person's month starts with their first traffic and ends a month later; the next one starts only when they connect again, so time away uses up none of it. Before, everyone's limit reset on the 1st. - Extra data lasts until that person's month ends; «Reset usage» starts the month anew. - A limit is for a month or for the whole term; the admin and the portal show when it resets. - Charts that said «this month» show the last 30 days. - After the update nobody loses what was counted: the month that was running becomes the person's first and ends on the 1st. - **Access by time of day removed.** The Hours limit is gone from the admin, the portal and the backend; saved hours are dropped. - **Expiry simplified.** An end date always stops access, with a heads-up three days before; the per-person choice is gone. - **No false disconnects from sleeping phones.** A phone or a tablet stays online through ten minutes of silence. - **Mobile Safari fixes.** Pressing a control no longer sends the page to its top; tab tracks that do not fit scroll; switches animate after a press.
This commit is contained in:
1 parent
f8130e39a8
commit
ac90575b0b
788 files changed
+139244
-41720
No files matched your search
@@ -1,22 +0,0 @@
|
||||
#!/usr/bin/ucode
|
||||
// Dawnline personal portal as a CGI script: the request handling itself is
|
||||
// dawnline.portal. www/cgi-bin/portal (a shell) runs it for requests shaped
|
||||
// like a personal link and answers the rest itself.
|
||||
'use strict';
|
||||
|
||||
import { stdin } from 'fs';
|
||||
import { serve } from 'dawnline.portal';
|
||||
|
||||
let env = {};
|
||||
for (let k in [ 'REQUEST_METHOD', 'REQUEST_URI', 'REMOTE_ADDR', 'HTTPS', 'CONTENT_LENGTH', 'CONTENT_TYPE',
|
||||
'HTTP_HOST', 'HTTP_ORIGIN', 'HTTP_USER_AGENT', 'HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_ENCODING',
|
||||
// The visitor behind Cloudflare (believed only when REMOTE_ADDR is Cloudflare's).
|
||||
'HTTP_CF_CONNECTING_IP' ])
|
||||
env[k] = getenv(k);
|
||||
|
||||
serve({
|
||||
env,
|
||||
headers: {},
|
||||
read: (n) => stdin.read(n),
|
||||
write: (s) => print(s),
|
||||
});
|
||||
@@ -1,51 +0,0 @@
|
||||
#!/bin/sh
|
||||
# Dawnline personal portal, the door: uhttpd runs this for every request.
|
||||
# Anything that is not shaped like a personal link (/<word-code>[/…]) gets
|
||||
# the portal's bare answer from here, in a shell that costs nothing, so a
|
||||
# scanner or a flood on the root never starts ucode (compiling the portal's
|
||||
# module graph is ~140 ms of CPU). Links go on to the portal itself
|
||||
# (/usr/share/dawnline/portal/cgi.uc), which judges the code.
|
||||
#
|
||||
# The answer here is byte for byte the portal's own not_found(): keep both
|
||||
# in step (dawnline.portal headers(), SECURITY_HEADERS, not_found()).
|
||||
|
||||
path="${REQUEST_URI%%\?*}"
|
||||
w='[a-z][a-z][a-z][a-z][a-z][a-z]'
|
||||
# The expansion is meant as a pattern (a word of six letters), not literally.
|
||||
# shellcheck disable=SC2254
|
||||
case "$path" in
|
||||
/$w-$w-$w|/$w-$w-$w/*) exec /usr/bin/ucode /usr/share/dawnline/portal/cgi.uc ;;
|
||||
esac
|
||||
|
||||
# HSTS pins the whole name: only when the visitor is on the standard port.
|
||||
hsts=
|
||||
if [ "$HTTPS" = on ]; then
|
||||
port="${HTTP_HOST##*:}"
|
||||
case "$HTTP_HOST" in *:*) ;; *) port=443 ;; esac
|
||||
case "$HTTP_HOST" in *\]) port=443 ;; esac
|
||||
[ "$port" = 443 ] && hsts=1
|
||||
fi
|
||||
|
||||
# Files always get the 404: a missing asset must not turn into someone's website.
|
||||
file=
|
||||
case "$path" in
|
||||
/luci-static|/luci-static/*|/cgi-bin|/cgi-bin/*|/ubus|/ubus/*) file=1 ;;
|
||||
*) case "${path##*/}" in *.*) file=1 ;; esac ;;
|
||||
esac
|
||||
redirect=
|
||||
[ -n "$file" ] || redirect="$(uci -q get dawnline.settings.portal_redirect)"
|
||||
|
||||
if [ -n "$redirect" ]; then
|
||||
printf 'Status: 302 Found\r\n'
|
||||
printf 'X-Content-Type-Options: nosniff\r\nReferrer-Policy: no-referrer\r\nX-Frame-Options: DENY\r\nCache-Control: no-store\r\n'
|
||||
printf 'Location: %s\r\nContent-Type: text/plain\r\n' "$redirect"
|
||||
[ -n "$hsts" ] && printf 'Strict-Transport-Security: max-age=31536000\r\n'
|
||||
printf '\r\nMoved\n'
|
||||
else
|
||||
printf 'Status: 404 Not Found\r\n'
|
||||
printf 'X-Content-Type-Options: nosniff\r\nReferrer-Policy: no-referrer\r\nX-Frame-Options: DENY\r\nCache-Control: no-store\r\n'
|
||||
printf 'Content-Type: text/plain\r\n'
|
||||
[ -n "$hsts" ] && printf 'Strict-Transport-Security: max-age=31536000\r\n'
|
||||
printf '\r\nNot found\n'
|
||||
fi
|
||||
exit 0
|
||||
@@ -0,0 +1 @@
|
||||
0.20.0
|
||||
@@ -4,7 +4,7 @@
|
||||
"order": 65,
|
||||
"action": {
|
||||
"type": "alias",
|
||||
"path": "admin/services/dawnline/server"
|
||||
"path": "admin/services/dawnline/overview"
|
||||
},
|
||||
"depends": {
|
||||
"acl": [
|
||||
@@ -18,7 +18,7 @@
|
||||
"admin/services/dawnline/overview": {
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/server"
|
||||
"path": "dawnline/overview"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/people": {
|
||||
@@ -27,29 +27,49 @@
|
||||
"path": "dawnline/people"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/places": {
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/places"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/activity": {
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/activity"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/log": {
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/log"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/person": {
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/person"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/device": {
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/device"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/server": {
|
||||
"title": "Servers",
|
||||
"order": 10,
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/server"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/system": {
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/system"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/diagnostics": {
|
||||
"title": "Diagnostics",
|
||||
"order": 40,
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/diagnostics"
|
||||
@@ -62,16 +82,12 @@
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/settings": {
|
||||
"title": "Settings",
|
||||
"order": 60,
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/settings"
|
||||
}
|
||||
},
|
||||
"admin/services/dawnline/updates": {
|
||||
"title": "Components",
|
||||
"order": 70,
|
||||
"action": {
|
||||
"type": "view",
|
||||
"path": "dawnline/updates"
|
||||
|
||||
@@ -3,32 +3,75 @@
|
||||
"description": "Grant access to Dawnline",
|
||||
"read": {
|
||||
"ubus": {
|
||||
"luci.dawnline": [
|
||||
"rev",
|
||||
"status",
|
||||
"meter",
|
||||
"setup_defaults",
|
||||
"health",
|
||||
"dawnline": [
|
||||
"hello",
|
||||
"sync",
|
||||
"wait",
|
||||
"watch",
|
||||
"refresh",
|
||||
"events",
|
||||
"person_usage",
|
||||
"service_log",
|
||||
"backup_list",
|
||||
"settings_get",
|
||||
"server_ports",
|
||||
"interfaces",
|
||||
"updates_status",
|
||||
"setup_defaults",
|
||||
"updates_releases",
|
||||
"updates_notes",
|
||||
"updates_changes",
|
||||
"person_usage",
|
||||
"settings_get",
|
||||
"portal_info",
|
||||
"interfaces",
|
||||
"server_ports",
|
||||
"endpoint_probe",
|
||||
"events"
|
||||
"health",
|
||||
"portal_info",
|
||||
":subscribe"
|
||||
]
|
||||
}
|
||||
},
|
||||
"write": {
|
||||
"ubus": {
|
||||
"luci.dawnline": [
|
||||
"*"
|
||||
"dawnline": [
|
||||
"person_links",
|
||||
"device_config",
|
||||
"backup_read",
|
||||
"home_hosts",
|
||||
"export",
|
||||
"access_check",
|
||||
"logs_clear",
|
||||
"service_log_clear",
|
||||
"usage_clear",
|
||||
"geo_clear",
|
||||
"backup_delete",
|
||||
"service_set",
|
||||
"service_delete",
|
||||
"circle_set",
|
||||
"circle_delete",
|
||||
"person_pause",
|
||||
"person_delete",
|
||||
"person_update",
|
||||
"people_update",
|
||||
"person_create",
|
||||
"person_regen",
|
||||
"device_update",
|
||||
"device_delete",
|
||||
"device_create",
|
||||
"device_rotate",
|
||||
"device_networks",
|
||||
"device_answer",
|
||||
"settings_update",
|
||||
"server_update",
|
||||
"server_interfaces",
|
||||
"server_delete",
|
||||
"backup_create",
|
||||
"server_obfuscation",
|
||||
"server_create",
|
||||
"health_fix",
|
||||
"updates_run",
|
||||
"apply",
|
||||
"backup_restore",
|
||||
"backup_upload",
|
||||
"import",
|
||||
"uninstall"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
// Dawnline — rpcd backend for the LuCI admin UI (ubus object luci.dawnline).
|
||||
//
|
||||
// The methods live in dawnline.rpc.* by page: status (Overview, activity),
|
||||
// servers, people (and their devices), settings, home (services, circles),
|
||||
// maint (Diagnostics, backups, removal) and updates (Components). Each module
|
||||
// exports `methods` as { name: { args, call } }; read-only users get the
|
||||
// methods listed under "read" in acl.d/luci-app-dawnline.json. Every method
|
||||
// also takes `lang`, the admin page's language, and answers in it (its
|
||||
// errors and the texts the backend writes, through dawnline.i18n).
|
||||
'use strict';
|
||||
|
||||
import * as status from 'dawnline.rpc.status';
|
||||
import * as servers from 'dawnline.rpc.servers';
|
||||
import * as people from 'dawnline.rpc.people';
|
||||
import * as settings from 'dawnline.rpc.settings';
|
||||
import * as home from 'dawnline.rpc.home';
|
||||
import * as maint from 'dawnline.rpc.maint';
|
||||
import * as updates from 'dawnline.rpc.updates';
|
||||
import * as i18n from 'dawnline.i18n';
|
||||
|
||||
/** The methods with `lang` added: the language is set for the call, the method never sees it. */
|
||||
function in_lang(methods) {
|
||||
let out = {};
|
||||
for (let name, m in methods) {
|
||||
let call = m.call;
|
||||
out[name] = {
|
||||
...m,
|
||||
args: { ...(m.args ?? {}), lang: '' },
|
||||
call: function(req) {
|
||||
i18n.use(req.args?.lang);
|
||||
if (req.args)
|
||||
delete req.args.lang;
|
||||
return call(req);
|
||||
},
|
||||
};
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
return {
|
||||
'luci.dawnline': in_lang({
|
||||
...status.methods,
|
||||
...servers.methods,
|
||||
...people.methods,
|
||||
...settings.methods,
|
||||
...home.methods,
|
||||
...maint.methods,
|
||||
...updates.methods,
|
||||
}),
|
||||
};
|
||||
@@ -1,333 +0,0 @@
|
||||
// Dawnline: "Check access" — walks the same rules the nft table and the
|
||||
// client configuration apply, and says in words where a connection stops
|
||||
// (in the admin's language: each step is a whole sentence).
|
||||
|
||||
'use strict';
|
||||
|
||||
import { ip2int, in_nets } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as view from 'dawnline.view';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/** "Bob can't connect: they are paused", by the person's state (core.person_state). */
|
||||
function cant_connect(name, st) {
|
||||
if (st == 'no_server')
|
||||
return sprintf(_("%s can't connect: they have no server"), name);
|
||||
if (st == 'server_off')
|
||||
return sprintf(_("%s can't connect: their server is turned off"), name);
|
||||
if (st == 'paused')
|
||||
return sprintf(_("%s can't connect: they are paused"), name);
|
||||
if (st == 'expired')
|
||||
return sprintf(_("%s can't connect: their access has ended"), name);
|
||||
if (st == 'quota')
|
||||
return sprintf(_("%s can't connect: they used up their data"), name);
|
||||
if (st == 'hours')
|
||||
return sprintf(_("%s can't connect: it is outside their allowed hours"), name);
|
||||
return sprintf(_("%s can't connect: %s"), name, st);
|
||||
}
|
||||
|
||||
/** "Bob · Phone can't be reached: it is turned off", by the device and its person's state. */
|
||||
function cant_reach(who, disabled, st) {
|
||||
if (disabled)
|
||||
return sprintf(_("%s can't be reached: it is turned off"), who);
|
||||
if (st == 'no_server')
|
||||
return sprintf(_("%s can't be reached: they have no server"), who);
|
||||
if (st == 'server_off')
|
||||
return sprintf(_("%s can't be reached: their server is turned off"), who);
|
||||
if (st == 'paused')
|
||||
return sprintf(_("%s can't be reached: they are paused"), who);
|
||||
if (st == 'expired')
|
||||
return sprintf(_("%s can't be reached: their access has ended"), who);
|
||||
if (st == 'quota')
|
||||
return sprintf(_("%s can't be reached: they used up their data"), who);
|
||||
if (st == 'hours')
|
||||
return sprintf(_("%s can't be reached: it is outside their allowed hours"), who);
|
||||
return sprintf(_("%s can't be reached: %s"), who, st);
|
||||
}
|
||||
|
||||
/** A person's access as the admin page names it. */
|
||||
function access_text(a) {
|
||||
if (a == 'lan')
|
||||
return _('Home only');
|
||||
if (a == 'internet')
|
||||
return _('Internet only');
|
||||
if (a == 'both')
|
||||
return _('Home and Internet');
|
||||
return a;
|
||||
}
|
||||
|
||||
/** "Bob has Internet only": the step where access stops a connection. */
|
||||
function has_access(name, a) {
|
||||
if (a == 'lan')
|
||||
return sprintf(_('%s has Home only'), name);
|
||||
if (a == 'internet')
|
||||
return sprintf(_('%s has Internet only'), name);
|
||||
return sprintf(_('%s has Home and Internet'), name);
|
||||
}
|
||||
|
||||
/** "Bob reaches lan, guest." (the person's home networks), or that there is none. */
|
||||
function reaches(name, devs) {
|
||||
return length(devs) ? sprintf(_('%s reaches %s.'), name, join(', ', devs)) : sprintf(_('%s reaches no home network.'), name);
|
||||
}
|
||||
|
||||
function step(ok, title, detail) {
|
||||
return { ok: !!ok, title, detail: detail ?? '' };
|
||||
}
|
||||
|
||||
/** Add the step where it stops and end the walk. */
|
||||
function stop(steps, s) {
|
||||
push(steps, s);
|
||||
return steps;
|
||||
}
|
||||
|
||||
/** The tunnel subnet (of any server) that holds `ip`: never home, whatever routes cover it. */
|
||||
function in_tunnels(m, ip) {
|
||||
let nets = [];
|
||||
for (let id, s in m.servers)
|
||||
for (let t in core.tunnels(s))
|
||||
push(nets, t.subnet);
|
||||
return in_nets(ip, nets);
|
||||
}
|
||||
|
||||
/** "10.0.1.20:445" → [ip, port]; a device id → its address. */
|
||||
function parse_target(m, s) {
|
||||
s = trim(`${s ?? ''}`);
|
||||
if (m.devices[s])
|
||||
return { dev: m.devices[s], ip: m.devices[s].address, port: null };
|
||||
let mm = match(s, /^([0-9.]+)(:([0-9]{1,5}))?$/);
|
||||
if (!mm || ip2int(mm[1]) == null)
|
||||
return null;
|
||||
let ip = mm[1];
|
||||
for (let id, d in m.devices)
|
||||
if (d.address == ip)
|
||||
return { dev: d, ip, port: mm[3] ? +mm[3] : null };
|
||||
return { ip, port: mm[3] ? +mm[3] : null };
|
||||
}
|
||||
|
||||
function home_access(p) {
|
||||
return p.access == 'both' || (p.access == 'lan' && !length(p.services));
|
||||
}
|
||||
|
||||
function peer_of(dmp, d) {
|
||||
return dmp.peers[d.public_key];
|
||||
}
|
||||
|
||||
/** What the device sends to the router: its configuration's AllowedIPs (its own, with lan_bypass). */
|
||||
function routed(m, srv, p, d) {
|
||||
return awg.allowed_ips(m, srv, p, d);
|
||||
}
|
||||
|
||||
/** The home network (interface) of a server that holds an address. */
|
||||
function home_dev_of(srv, ip, snap) {
|
||||
for (let h in net.server_net(srv, snap).home)
|
||||
if (in_nets(ip, h.nets))
|
||||
return h.dev;
|
||||
return null;
|
||||
}
|
||||
|
||||
function router_addrs(snap) {
|
||||
let out = [];
|
||||
for (let dev, d in snap.devices)
|
||||
for (let a in d.addrs ?? [])
|
||||
push(out, split(a, '/')[0]);
|
||||
return out;
|
||||
}
|
||||
|
||||
function person_ok(m, p, d, states) {
|
||||
let st = states[p.id] ?? 'active';
|
||||
if (d.disabled)
|
||||
return step(false, sprintf(_('%s is turned off'), d.name), _('Turn the device on again on its page.'));
|
||||
if (st != 'active')
|
||||
return step(false, cant_connect(p.name, st), '');
|
||||
return step(true, sprintf(_('%s is active'), p.name), access_text(p.access));
|
||||
}
|
||||
|
||||
function connected(dmp, d, who) {
|
||||
let peer = peer_of(dmp, d);
|
||||
if (!peer || !(peer.handshake > 0))
|
||||
return step(false, sprintf(_('%s has never connected'), who), _('There is no session the router could reach.'));
|
||||
if (!live.online(dmp, d.id, peer))
|
||||
return step(false, sprintf(_("%s isn't connected right now"), who), _('The router reaches it only while its VPN is on.'));
|
||||
return step(true, sprintf(_('%s is connected'), who), '');
|
||||
}
|
||||
|
||||
/** A device → somewhere: the router, home, another device or the internet. */
|
||||
function from_device(m, d, t, states, dmp, snap) {
|
||||
let p = m.people[d.person];
|
||||
let srv = p ? m.servers[p.server] : null;
|
||||
if (!p || !srv)
|
||||
return [ step(false, sprintf(_('%s has no server'), d.name), '') ];
|
||||
let steps = [ person_ok(m, p, d, states) ];
|
||||
if (!steps[0].ok)
|
||||
return steps;
|
||||
let allowed = routed(m, srv, p, d);
|
||||
let via = (ip) => {
|
||||
let hit = in_nets(ip, allowed);
|
||||
// The configuration's own words: the option and its value.
|
||||
let what = `AllowedIPs ${join(', ', filter(allowed, (x) => index(x, ':') < 0))}`;
|
||||
return hit
|
||||
? step(true, _('Its configuration sends it to the router'), what)
|
||||
: step(false, _("Its configuration doesn't send it to the router"), sprintf(_("%s: the device's own network handles it."), what));
|
||||
};
|
||||
|
||||
// Another device.
|
||||
if (t.dev) {
|
||||
let q = m.people[t.dev.person];
|
||||
let who = `${q?.name ?? '?'} · ${t.dev.name}`;
|
||||
if (t.dev.id == d.id)
|
||||
return [ step(false, _('That is the same device'), '') ];
|
||||
if (!q || q.server != p.server)
|
||||
return stop(steps, step(false, sprintf(_('%s is on another server'), who), _('Devices of different servers never reach each other.')));
|
||||
if (!home_access(p))
|
||||
return stop(steps, step(false, has_access(p.name, p.access), _('Only people with home access reach other devices.')));
|
||||
if (!home_access(q))
|
||||
return stop(steps, step(false, has_access(q.name, q.access), _('Only devices of people with home access can be reached.')));
|
||||
if (srv.mesh == 'off')
|
||||
return stop(steps, step(false, sprintf(_('"Devices reach each other" is off on %s'), srv.name), _('Server → Access → Home network.')));
|
||||
if (srv.mesh == 'circles' && q.id != p.id) {
|
||||
let a = core.person_circle(m, p), b = core.person_circle(m, q);
|
||||
if (!a || !b || a.id != b.id)
|
||||
return stop(steps, step(false, sprintf(_('%s and %s are not in one circle'), p.name, q.name),
|
||||
`${p.name}: ${a?.name ?? _('no circle')} · ${q.name}: ${b?.name ?? _('no circle')}`));
|
||||
push(steps, step(true, sprintf(_('Both are in %s'), a.name), _('Devices in one circle reach each other.')));
|
||||
}
|
||||
else
|
||||
push(steps, step(true, q.id == p.id ? sprintf(_("Both are %s's devices"), p.name) : _('Devices reach each other on this server'), ''));
|
||||
let st = states[q.id] ?? 'active';
|
||||
if (st != 'active' || t.dev.disabled)
|
||||
return stop(steps, step(false, cant_reach(who, t.dev.disabled, st), ''));
|
||||
push(steps, via(t.ip));
|
||||
push(steps, connected(dmp, t.dev, who));
|
||||
return steps;
|
||||
}
|
||||
|
||||
// A device that keeps its own local network out of the tunnel (lan_bypass;
|
||||
// Home only is the same either way) sends a private address there, router
|
||||
// or home address alike: it never reaches this router's rules.
|
||||
if (d.lan_bypass && p.access != 'lan' && !in_nets(t.ip, allowed))
|
||||
return stop(steps, via(t.ip));
|
||||
|
||||
// The router itself.
|
||||
if (index(router_addrs(snap), t.ip) >= 0) {
|
||||
let portal = m.settings.portal_enabled && (t.port == m.settings.portal_port || (core.portal_http(m) && t.port == core.portal_http_port(m)));
|
||||
if (t.port == 53)
|
||||
push(steps, step(true, sprintf(_('%s is this router: DNS is open to everyone'), t.ip), ''));
|
||||
else if (portal)
|
||||
push(steps, step(true, sprintf(_('%s is this router: the personal portal is open to everyone'), t.ip), ''));
|
||||
else if (!home_access(p))
|
||||
return stop(steps, step(false, sprintf(_('%s is this router'), t.ip), sprintf(p.access == 'lan'
|
||||
? _('%s has Home only: only DNS and the portal on the router.')
|
||||
: _('%s has Internet only: only DNS and the portal on the router.'), p.name)));
|
||||
else {
|
||||
// Like any home address: only on the networks the person reaches.
|
||||
let h = core.person_home(srv, p, snap);
|
||||
if (!h.all && !in_nets(t.ip, h.nets))
|
||||
return stop(steps, step(false, sprintf(_("%s is this router on a network %s doesn't reach"), t.ip, p.name), reaches(p.name, h.devs)));
|
||||
push(steps, step(true, sprintf(_('%s is this router'), t.ip),
|
||||
h.all ? _('Home access includes the router.') : sprintf(_('On one of the networks %s reaches.'), p.name)));
|
||||
}
|
||||
push(steps, via(t.ip));
|
||||
return steps;
|
||||
}
|
||||
|
||||
// A tunnel address no device has.
|
||||
let tn = in_tunnels(m, t.ip);
|
||||
if (tn)
|
||||
return stop(steps, step(false, sprintf(_('No device uses %s'), t.ip), sprintf(_('It is in the tunnel network %s.'), tn)));
|
||||
|
||||
// Home.
|
||||
let sn = net.server_net(srv, snap);
|
||||
let hdev = home_dev_of(srv, t.ip, snap);
|
||||
if (hdev) {
|
||||
if (p.access == 'internet')
|
||||
return stop(steps, step(false, sprintf(_('%s is at home (%s)'), t.ip, hdev), sprintf(_('%s has Internet only.'), p.name)));
|
||||
if (p.access == 'lan' && length(p.services)) {
|
||||
let hit = null;
|
||||
for (let sid in p.services) {
|
||||
let s = m.services[sid];
|
||||
if (s?.address == t.ip && (!length(s.ports) || t.port == null || index(s.ports, `${t.port}`) >= 0))
|
||||
hit = s;
|
||||
}
|
||||
let names = join(', ', map(filter(map(p.services, (x) => m.services[x]), (x) => x), (x) => x.name));
|
||||
if (!hit)
|
||||
return stop(steps, step(false, sprintf(_("%s is not one of %s's home services"), t.port ? `${t.ip}:${t.port}` : t.ip, p.name),
|
||||
sprintf(_('Allowed: %s'), names)));
|
||||
push(steps, step(true, sprintf(_("%s is %s, one of %s's home services"), t.ip, hit.name, p.name),
|
||||
length(hit.ports) ? sprintf(_('Ports %s'), join(', ', hit.ports)) : _('All ports')));
|
||||
}
|
||||
else {
|
||||
let h = core.person_home(srv, p, snap);
|
||||
if (!in_nets(t.ip, h.nets))
|
||||
return stop(steps, step(false, sprintf(_("%s is in %s, which %s doesn't reach"), t.ip, hdev, p.name), reaches(p.name, h.devs)));
|
||||
push(steps, step(true, sprintf(_('%s is in home network %s'), t.ip, hdev),
|
||||
h.all ? _('Home access reaches every home network.') : sprintf(_('One of the networks %s reaches.'), p.name)));
|
||||
}
|
||||
push(steps, via(t.ip));
|
||||
push(steps, step(true, _('The firewall lets it through'), `${srv.ifname} → ${join(', ', sn.zones) || _('home')}`));
|
||||
return steps;
|
||||
}
|
||||
|
||||
// Another private address, or the internet.
|
||||
if (in_nets(t.ip, [ '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' ]))
|
||||
return stop(steps, step(false, sprintf(_("%s isn't in %s's home network"), t.ip, srv.name), _('Private addresses outside the home network are blocked.')));
|
||||
if (p.access == 'lan')
|
||||
return stop(steps, step(true, sprintf(_('%s is on the internet'), t.ip),
|
||||
sprintf(_("%s has Home only: their device's own internet reaches it, not the tunnel."), p.name)));
|
||||
push(steps, via(t.ip));
|
||||
push(steps, step(true, sprintf(_('%s is on the internet'), t.ip), srv.lan_like ? _('It leaves like a home device would.')
|
||||
: sn.wan.dev != null ? sprintf(_('It leaves through %s.'), sn.wan.dev) : _('It leaves through the WAN.')));
|
||||
return steps;
|
||||
}
|
||||
|
||||
/** A home address → a device. */
|
||||
function to_device(m, ip, d, states, dmp, snap) {
|
||||
let p = m.people[d.person];
|
||||
let srv = p ? m.servers[p.server] : null;
|
||||
if (!p || !srv)
|
||||
return [ step(false, sprintf(_('%s has no server'), d.name), '') ];
|
||||
let who = `${p.name} · ${d.name}`;
|
||||
let hdev = in_tunnels(m, ip) ? null : home_dev_of(srv, ip, snap);
|
||||
if (!hdev)
|
||||
return [ step(false, sprintf(_("%s isn't in %s's home network"), ip, srv.name), _("Only home devices reach people's devices.")) ];
|
||||
let steps = [ step(true, sprintf(_('%s is at home (%s)'), ip, hdev), '') ];
|
||||
if (!home_access(p))
|
||||
return stop(steps, step(false, p.access == 'lan' ? sprintf(_('%s has only some home services'), p.name) : has_access(p.name, p.access),
|
||||
_('Home devices reach only people with home access.')));
|
||||
let h = core.person_home(srv, p, snap);
|
||||
if (!in_nets(ip, h.nets))
|
||||
return stop(steps, step(false, sprintf(_("%s doesn't reach %s"), p.name, hdev), _('Networks a person does not reach cannot reach them either.')));
|
||||
push(steps, person_ok(m, p, d, states));
|
||||
if (!steps[length(steps) - 1].ok)
|
||||
return steps;
|
||||
push(steps, step(true, _('The firewall lets it through'), `${join(', ', net.server_net(srv, snap).zones) || _('home')} → ${srv.ifname}`));
|
||||
push(steps, connected(dmp, d, who));
|
||||
return steps;
|
||||
}
|
||||
|
||||
/**
|
||||
* `from` and `to`: a device id, or an IPv4 address (optionally :port).
|
||||
* `ctx` = { states, dmp, snap } (a test's own); read from the router when not
|
||||
* given. Returns { ok, steps: [{ ok, title, detail }] }.
|
||||
*/
|
||||
export function check(m, from, to, ctx) {
|
||||
let a = parse_target(m, from), b = parse_target(m, to);
|
||||
if (!a)
|
||||
die(sprintf(_('"%s" is not a device or an IPv4 address.'), from));
|
||||
if (!b)
|
||||
die(sprintf(_('"%s" is not a device or an IPv4 address.'), to));
|
||||
let states = ctx?.states ?? view.states(m, core.load_usage());
|
||||
let dmp = ctx?.dmp ?? live.dump(m);
|
||||
let snap = ctx?.snap ?? net.snapshot(true);
|
||||
let steps;
|
||||
if (a.dev)
|
||||
steps = from_device(m, a.dev, b, states, dmp, snap);
|
||||
else if (b.dev)
|
||||
steps = to_device(m, a.ip, b.dev, states, dmp, snap);
|
||||
else
|
||||
die(_('Choose a device on at least one side.'));
|
||||
let ok = length(filter(steps, (s) => !s.ok)) == 0;
|
||||
return { ok, steps };
|
||||
};
|
||||
@@ -1,324 +0,0 @@
|
||||
// Dawnline — reconciliation. Turns /etc/config/dawnline into router state:
|
||||
// * network interfaces (dawnline.netifd) and the devices loaded live into
|
||||
// them (dawnline.awg)
|
||||
// * firewall zones, forwardings, WAN rules and reply routes (dawnline.fw)
|
||||
// * the nft table `inet dawnline` enforcing each person's access (dawnline.nft)
|
||||
// * hostnames in /tmp/hosts/dawnline
|
||||
// * the "like a home device" path and the Home-only resolver (dawnline.loop)
|
||||
// Only objects created by Dawnline are ever modified.
|
||||
//
|
||||
// apply(), sync() and sync_loop() run under one lock ('apply') and read the
|
||||
// model inside it, so a rebuild and a runtime update never interleave and the
|
||||
// last one to run always works from the newest configuration. The config lock
|
||||
// (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, 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';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as nft from 'dawnline.nft';
|
||||
import * as fw from 'dawnline.fw';
|
||||
import * as netifd from 'dawnline.netifd';
|
||||
import * as migrate from 'dawnline.migrate';
|
||||
import { _, L_ } from 'dawnline.i18n';
|
||||
|
||||
const HOSTS_FILE = '/tmp/hosts/dawnline';
|
||||
/** The network signature the last apply() worked from (the agent compares against it). */
|
||||
export const NET_SIG = `${RUN_DIR}/net.sig`;
|
||||
// The last access-table error that was logged (each distinct failure once).
|
||||
const NFT_ERR = `${RUN_DIR}/nft.err`;
|
||||
|
||||
function log(who, msg, level, detail) {
|
||||
slog('dawnline', level ?? 'info', msg, detail, who);
|
||||
}
|
||||
|
||||
/* ---------- hostnames ---------- */
|
||||
|
||||
export function sync_hosts(m) {
|
||||
let lines = [];
|
||||
if (m.settings.hostnames)
|
||||
for (let pid, p in m.people) {
|
||||
// One label per device, unique within the person (the UI shows the same).
|
||||
let labels = core.device_labels(m, p);
|
||||
for (let id in p.devices) {
|
||||
let d = m.devices[id];
|
||||
if (ip2int(d.address) != null)
|
||||
push(lines, `${d.address} ${labels[id]}.${p.id}.${m.settings.domain}`);
|
||||
}
|
||||
}
|
||||
let ls = loop.status();
|
||||
if (ls.active)
|
||||
push(lines, `${ls.address} dawnline.${m.settings.domain}`);
|
||||
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)) {
|
||||
syslog('debug', 'dns', `${HOSTS_FILE}: unchanged · ${length(lines)} names`);
|
||||
return false;
|
||||
}
|
||||
ensure_dir('/tmp/hosts');
|
||||
if (body == '')
|
||||
unlink(HOSTS_FILE);
|
||||
else {
|
||||
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;
|
||||
};
|
||||
|
||||
/* ---------- access table ---------- */
|
||||
|
||||
export function states_for(m, usage) {
|
||||
let st = {};
|
||||
for (let id, p in m.people)
|
||||
st[id] = core.person_state(m, p, usage);
|
||||
return st;
|
||||
};
|
||||
|
||||
/** Per-person runtime membership (dawnline.nft). */
|
||||
export function access_sets(m, usage, states) {
|
||||
return nft.access_sets(m, usage, states);
|
||||
};
|
||||
|
||||
/**
|
||||
* Log an access-table failure once per distinct error (with what it means
|
||||
* for the devices: none loaded while the table is missing, the previous rules
|
||||
* kept when an older one survived the failed rebuild); the error, or null.
|
||||
*/
|
||||
function nft_result(err, present) {
|
||||
let was = readfile(NFT_ERR);
|
||||
if (err == null) {
|
||||
if (was != null)
|
||||
unlink(NFT_ERR);
|
||||
return null;
|
||||
}
|
||||
if (was != err) {
|
||||
log(L_('Access rules'), present
|
||||
? L_("couldn't be updated - the previous rules stay, and changes to access wait until they load")
|
||||
: L_("couldn't load - no device is loaded into the tunnels until they do"), 'error', err);
|
||||
ensure_dir(RUN_DIR);
|
||||
writefile(NFT_ERR, err);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* The table in place: rebuilt with `full`, otherwise members only, rebuilt
|
||||
* when that fails (no table yet at boot, or an older shape). Null, or the
|
||||
* error when the rebuild fails too.
|
||||
*/
|
||||
function load_table(m, sets, full) {
|
||||
let err = full ? nft.rebuild(m, sets) : nft.update(sets);
|
||||
if (err != null && !full)
|
||||
err = nft.rebuild(m, sets);
|
||||
return nft_result(err, err != null && nft.present());
|
||||
}
|
||||
|
||||
/** Full table rebuild (structure changes: servers, LAN subnets); true when it loaded. */
|
||||
export function rebuild_nft(m, sets) {
|
||||
let err = nft.rebuild(m, sets);
|
||||
return nft_result(err, err != null && nft.present()) == null;
|
||||
};
|
||||
|
||||
/** Atomically replace set contents only. */
|
||||
export function update_sets(sets) {
|
||||
return nft.update(sets) == null;
|
||||
};
|
||||
|
||||
export function remove_nft() {
|
||||
nft.remove();
|
||||
};
|
||||
|
||||
/** For the watchdog (dawnline.fw). */
|
||||
export function firewall_drift(m) {
|
||||
return fw.drift(m);
|
||||
};
|
||||
|
||||
/* ---------- runtime sync ---------- */
|
||||
|
||||
/**
|
||||
* What an admin should hear when a sync did not fully take: the access table
|
||||
* failed, or a server did not accept its devices (sentences in the caller's
|
||||
* language, with what nft or awg said). Null when all went in.
|
||||
*/
|
||||
function sync_warning(m, table, peers) {
|
||||
let out = [];
|
||||
if (table != null)
|
||||
push(out, nft.present()
|
||||
? sprintf(_("Access rules couldn't be updated (%s): the previous rules stay, so this change isn't in effect yet."), table)
|
||||
: sprintf(_("Access rules couldn't load (%s): no device is loaded into the tunnels until they do."), table));
|
||||
for (let id, r in peers)
|
||||
if (r != 'ok' && r != 'down')
|
||||
push(out, sprintf(_("Couldn't load devices into %s: %s."), m.servers[id]?.name ?? id, r));
|
||||
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
|
||||
* reach everything, so while it is missing no device is loaded at all. An
|
||||
* older table that survived a failed rebuild keeps limiting them with the
|
||||
* sets it has, and the devices still follow people's states (a revoked one
|
||||
* leaves).
|
||||
*/
|
||||
function locked_sync(m, usage, full) {
|
||||
let states = states_for(m, usage);
|
||||
let sets = nft.access_sets(m, usage, states);
|
||||
let table = load_table(m, sets, full);
|
||||
let peers = awg.sync_peers(m, table == null || nft.present() ? states : {});
|
||||
sync_hosts(m);
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(`${RUN_DIR}/states.json`, states, 0o644);
|
||||
return { peers, states, table, warning: sync_warning(m, table, peers) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime sync: devices into the tunnels (AmneziaWG peers), access sets,
|
||||
* hostnames. Cheap; safe to call often. The model is always read again under
|
||||
* the lock (`m` is accepted for older callers and not used); `usage` is the
|
||||
* caller's (the agent holds the live counters). The result carries `warning`
|
||||
* (a sentence for the admin) when the sync did not fully take.
|
||||
*/
|
||||
export function sync(m, usage) {
|
||||
return with_lock('apply', () => locked_sync(core.load(), usage ?? core.load_usage()));
|
||||
};
|
||||
|
||||
/** Is the access table loaded (the agent's tick re-syncs when it is not). */
|
||||
export function table_present() {
|
||||
return nft.present();
|
||||
};
|
||||
|
||||
/**
|
||||
* Everything on the router that Dawnline's state is derived from: WAN and
|
||||
* LAN, the home networks and zones of each server, the netns address and the
|
||||
* LAN's DNS. The agent re-applies when it changes (LAN renumbered, interface
|
||||
* added, bridge recreated, DHCP DNS changed, ...).
|
||||
*/
|
||||
export function net_signature(m) {
|
||||
let snap = net.snapshot(true);
|
||||
let srvs = {};
|
||||
for (let id, srv in m.servers)
|
||||
srvs[id] = net.server_net(srv, snap);
|
||||
let lan = snap.devices[snap.lan];
|
||||
return sprintf('%J', { wan: snap.wan, lan: snap.lan, addrs: lan?.addrs, servers: srvs, dns: loop.lan_dns(), loop: loop.client_dns() });
|
||||
};
|
||||
|
||||
/**
|
||||
* Keep the "like a home device" path and the resolver in step (agent tick).
|
||||
* Cheap when nothing changed; rebuilds the access table when the netns
|
||||
* address appears, moves or goes away (the guard allows DNS to it).
|
||||
*/
|
||||
export function sync_loop(m, usage, force) {
|
||||
let p = with_lock('apply', () => {
|
||||
let mm = core.load();
|
||||
let before = `${loop.client_dns()} ${loop.resolver_target().mode}`;
|
||||
let lp = loop.ensure(mm, force);
|
||||
loop.sync_resolver(mm);
|
||||
fw.sync_reply_routes(mm);
|
||||
if (`${loop.client_dns()} ${loop.resolver_target().mode}` != before)
|
||||
locked_sync(mm, usage ?? core.load_usage(), true);
|
||||
return lp;
|
||||
});
|
||||
loop.remember(p);
|
||||
return p;
|
||||
};
|
||||
|
||||
/* ---------- entry points ---------- */
|
||||
|
||||
/** 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 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, 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 };
|
||||
};
|
||||
|
||||
/** Remove everything Dawnline created (used by the uninstaller). */
|
||||
export function teardown() {
|
||||
with_lock('apply', () => {
|
||||
let m = core.load();
|
||||
loop.remove();
|
||||
unlink(loop.RESOLVER.conf);
|
||||
m.servers = {};
|
||||
m.settings.portal_enabled = false;
|
||||
netifd.sync(m);
|
||||
fw.sync(m);
|
||||
fw.sync_reply_routes(m);
|
||||
nft.remove();
|
||||
unlink(HOSTS_FILE);
|
||||
unlink(NET_SIG);
|
||||
system('killall -HUP dnsmasq 2>/dev/null');
|
||||
});
|
||||
migrate.uhttpd();
|
||||
// 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");
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,170 +0,0 @@
|
||||
// Dawnline — where AmneziaWG comes from: the awg-openwrt projects publish
|
||||
// builds per OpenWrt release and target (the kernel module must match the
|
||||
// firmware), and the record of what was installed on which kernel and boot
|
||||
// (an update whose module could not be swapped starts with the next boot:
|
||||
// until then caps() and Health go by the module that still runs). Used by
|
||||
// the updater, the Components RPCs, dawnline.awg and Health. Installing
|
||||
// itself is dawnline.upd_awg.
|
||||
|
||||
import { readfile, stat } from 'fs';
|
||||
import { connect } from 'ubus';
|
||||
import { ensure_dir, read_json, write_json, DATA_DIR } from 'dawnline.util';
|
||||
import * as pkg from 'dawnline.pkg';
|
||||
|
||||
export const SOURCES = {
|
||||
slava: { repo: 'Slava-Shchipunov/awg-openwrt', label: 'Slava-Shchipunov' },
|
||||
grey: { repo: '2Grey/awg-openwrt', label: '2Grey' },
|
||||
};
|
||||
/** Tried in this order when no build is chosen. */
|
||||
export const ORDER = [ 'slava', 'grey' ];
|
||||
export const PKGS = [ 'kmod-amneziawg', 'amneziawg-tools', 'luci-proto-amneziawg' ];
|
||||
export const RU = 'luci-i18n-amneziawg-ru';
|
||||
|
||||
/**
|
||||
* This firmware as the builds name it: { tag: 'v24.10.6', postfix:
|
||||
* '_v24.10.6_aarch64_cortex-a53_mediatek_filogic', ext }, or null.
|
||||
*/
|
||||
export function firmware() {
|
||||
let u = connect();
|
||||
let b = u?.call('system', 'board') ?? {};
|
||||
u?.disconnect();
|
||||
let rel = b.release ?? {};
|
||||
let target = split(rel.target ?? '', '/');
|
||||
let arch = rel.arch ?? match(readfile('/etc/openwrt_release') ?? '', /DISTRIB_ARCH='([^']*)'/)?.[1];
|
||||
if (!rel.version || length(target) != 2 || !arch)
|
||||
return null;
|
||||
return { tag: `v${rel.version}`, postfix: `_v${rel.version}_${arch}_${target[0]}_${target[1]}`, ext: pkg.ext(),
|
||||
snapshot: match(rel.version, /SNAPSHOT/) != null };
|
||||
};
|
||||
|
||||
/** What was installed, for whom: /etc/dawnline/awg.json (survives a sysupgrade with the data). */
|
||||
export const RECORD = `${DATA_DIR}/awg.json`;
|
||||
|
||||
/** This boot (a new one loads the module on disk), or null when the kernel does not say. */
|
||||
export function boot_id() {
|
||||
return trim(readfile('/proc/sys/kernel/random/boot_id') ?? '') || null;
|
||||
};
|
||||
|
||||
/**
|
||||
* The size in memory of the loaded kernel module, or null when none is
|
||||
* loaded. sysfs has no version for it, but builds differ in size: a module
|
||||
* reloaded by hand ends a wait for a reboot (pending()).
|
||||
*/
|
||||
export function module_core() {
|
||||
let n = int(trim(readfile('/sys/module/amneziawg/coresize') ?? ''));
|
||||
return n > 0 ? n : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Remember which build was installed on which kernel. After a firmware
|
||||
* upgrade the module is gone (it was built for that kernel): Health can then
|
||||
* say so and offer the build for the new one (dawnline.health). `run` comes
|
||||
* from an install: prev_gen and prev_core, the module that ran before it
|
||||
* (module_now()), and reloaded, false while that one stays loaded until the
|
||||
* next boot. null for a module Dawnline did not install (adopt()).
|
||||
*/
|
||||
export function record(source, tag, packages, run) {
|
||||
ensure_dir(DATA_DIR, 0o700);
|
||||
write_json(RECORD, {
|
||||
osrelease: trim(readfile('/proc/sys/kernel/osrelease') ?? ''),
|
||||
source, tag, packages, installed: time(), boot: boot_id(),
|
||||
prev_gen: run?.prev_gen ?? null, prev_core: run?.prev_core ?? null, reloaded: run?.reloaded ?? null,
|
||||
}, 0o600);
|
||||
};
|
||||
|
||||
/**
|
||||
* A record for a module Dawnline did not install (install.sh, by hand, or
|
||||
* before Dawnline kept records), so kernel_changed() notices the next
|
||||
* firmware upgrade: the kernel it is for, source null, `installed` the time
|
||||
* Dawnline first saw it. Only when there is no record and the module is
|
||||
* loaded or on disk for the running kernel. The agent calls it when it starts.
|
||||
*/
|
||||
export function adopt() {
|
||||
let rel = trim(readfile('/proc/sys/kernel/osrelease') ?? '');
|
||||
if (stat(RECORD) || (!stat('/sys/module/amneziawg') && (rel == '' || !stat(`/lib/modules/${rel}/amneziawg.ko`))))
|
||||
return;
|
||||
record(null, null, null, null);
|
||||
};
|
||||
|
||||
/**
|
||||
* The firmware the recorded AmneziaWG build was for, when it is not this one
|
||||
* (and the module is gone): { was, now, tag, source }, else null.
|
||||
*/
|
||||
export function kernel_changed() {
|
||||
let r = read_json(RECORD, null);
|
||||
let now = trim(readfile('/proc/sys/kernel/osrelease') ?? '');
|
||||
if (!r?.osrelease || r.osrelease == now)
|
||||
return null;
|
||||
return { was: r.osrelease, now, tag: r.tag, source: r.source };
|
||||
};
|
||||
|
||||
/** The file name of a package in a build. */
|
||||
export function asset(fw, name) {
|
||||
return `${name}${fw.postfix}.${fw.ext}`;
|
||||
};
|
||||
|
||||
/** The same firmware, another release of its series (v24.10.6 → v24.10.8). */
|
||||
export function for_tag(fw, tag) {
|
||||
return { ...fw, tag, postfix: replace(fw.postfix, fw.tag, tag) };
|
||||
};
|
||||
|
||||
/**
|
||||
* AmneziaWG generation of a package version: 3.x packages carry 3.1; 1.0.x
|
||||
* ones are 2.0 (S3/S4, I1-I5, header ranges) from July 2025 on, and the
|
||||
* first generation before that. A kernel module's version carries the kernel
|
||||
* release in front (6.12.94.3.1.20260906): the AmneziaWG part is at the end.
|
||||
*/
|
||||
export function gen(version) {
|
||||
if (version == null)
|
||||
return null;
|
||||
let v = match(`${version}`, /([0-9]+\.[0-9]+\.[0-9]{8})(-r?[0-9]+)?$/)?.[1] ?? `${version}`;
|
||||
if (match(v, /^3\./))
|
||||
return '3.1';
|
||||
if (match(v, /^1\.0\.2024/) || match(v, /^1\.0\.20250[1-6]/))
|
||||
return '1.0';
|
||||
return '2.0';
|
||||
};
|
||||
|
||||
/**
|
||||
* An installed update that waits for a reboot: the install could not swap
|
||||
* the kernel module (the old one stayed loaded), this is still the boot it
|
||||
* ran in, and the module loaded is still the one from before it (core:
|
||||
* module_core() now). Pure: rec is the record or null.
|
||||
*/
|
||||
export function pending(rec, boot, core) {
|
||||
return rec?.reloaded === false && rec.boot != null && rec.boot == boot && core != null && rec.prev_core == core;
|
||||
};
|
||||
|
||||
/**
|
||||
* The AmneziaWG generation of the kernel module that runs: the one from
|
||||
* before the install while it waits for a reboot (pending(); null when that
|
||||
* module had no package), else the installed package's version (gen()).
|
||||
* Pure, with the record, this boot and module_core() passed in.
|
||||
*/
|
||||
export function running_gen(rec, boot, core, version) {
|
||||
return pending(rec, boot, core) ? rec.prev_gen : gen(version);
|
||||
};
|
||||
|
||||
/**
|
||||
* The kernel module loaded now, taken before an install changes packages:
|
||||
* { core, gen, pending }. core is null when none is loaded; gen is what it
|
||||
* runs (running_gen(), from `version`, the kmod-amneziawg package installed
|
||||
* now); pending, an earlier update that still waits for a reboot.
|
||||
*/
|
||||
export function module_now(version) {
|
||||
let rec = read_json(RECORD, null), boot = boot_id(), core = module_core();
|
||||
return { core, gen: core == null ? null : running_gen(rec, boot, core, version), pending: pending(rec, boot, core) };
|
||||
};
|
||||
|
||||
/** The installed AmneziaWG update waits for a reboot (pending(), for Health and Components). */
|
||||
export function waits_for_reboot() {
|
||||
return pending(read_json(RECORD, null), boot_id(), module_core());
|
||||
};
|
||||
|
||||
/** The installed AmneziaWG packages: { name: version | null }. */
|
||||
export function installed() {
|
||||
let r = {};
|
||||
for (let p in PKGS)
|
||||
r[p] = pkg.version(p);
|
||||
return r;
|
||||
};
|
||||
@@ -1,326 +0,0 @@
|
||||
// Dawnline — backups (tar.gz of the config and its data files), restoring
|
||||
// them, and the JSON export / import. Whatever comes back in is checked first:
|
||||
// only Dawnline's own regular files, and servers that cannot take over one of
|
||||
// the router's own interfaces or networks, or hand the kernel disguise values
|
||||
// AmneziaWG refuses.
|
||||
|
||||
import { readfile, stat, unlink, lsdir, rename } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import {
|
||||
q, sh, ensure_dir, read_json, write_json, write_file, file_exists, rand_hex, lock, unlock, DATA_DIR, RUN_DIR, VERSION
|
||||
} from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as apply from 'dawnline.apply';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as health from 'dawnline.health';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
const BACKUP_DIR = `${DATA_DIR}/backups`;
|
||||
/**
|
||||
* How many backups of each kind stay (the newest): the nightly ones must not
|
||||
* push out what the admin made by hand or what a removal left behind.
|
||||
*/
|
||||
const KEEP = { manual: 10, auto: 7, 'pre-delete': 5, 'pre-remove': 3, 'pre-update': 3, 'pre-restore': 3, 'pre-import': 3 };
|
||||
const NAME = /^dawnline-([0-9]{8}-[0-9]{6})(-([a-z0-9-]+))?\.tar\.gz$/;
|
||||
/** What a backup holds (paths inside the archive, relative to /). */
|
||||
const FILES = [ 'etc/config/dawnline', 'etc/dawnline/usage.json', 'etc/dawnline/events.json' ];
|
||||
/**
|
||||
* Kinds that hold the config alone (about 20 KB): the history can be a
|
||||
* megabyte or more, and these are made often and never to bring history
|
||||
* back. Manual, pre-restore and pre-import backups protect the data that is
|
||||
* about to be replaced, so they take everything.
|
||||
*/
|
||||
const CONFIG_ONLY = [ 'auto', 'pre-delete', 'pre-remove', 'pre-update' ];
|
||||
/** The unpacked size a backup may have (the upload itself is limited to 4 MB). */
|
||||
const MAX_UNPACKED = 16 * 1024 * 1024;
|
||||
|
||||
/* ---------- checking a configuration before it is used ---------- */
|
||||
|
||||
/**
|
||||
* What is wrong with a configuration about to replace the current one
|
||||
* (cursor `c` holds it): each server must have a valid, unique interface
|
||||
* name that is not one of the router's own interfaces, a free subnet, a
|
||||
* unique port and disguise values AmneziaWG takes (awg.obf_unsafe), and the
|
||||
* people and devices must be consistent (core.model_errors). [] when it can
|
||||
* be used.
|
||||
*/
|
||||
export function config_errors(c) {
|
||||
let m = core.load(c);
|
||||
let errs = core.model_errors(m);
|
||||
let nc = cursor();
|
||||
nc.load('network');
|
||||
let foreign = health.foreign_subnets();
|
||||
let internal = values(net.INTERNAL);
|
||||
let ifnames = {}, ports = {}, subnets = [];
|
||||
for (let id, srv in m.servers) {
|
||||
for (let e in [ ...core.server_errors(m, srv, [ ...foreign, ...subnets ]), ...awg.obf_unsafe(srv.obf) ])
|
||||
push(errs, sprintf(_('Server %s: %s'), srv.name, e));
|
||||
if (ifnames[srv.ifname])
|
||||
push(errs, sprintf(_('Server %s: interface %s is also used by %s.'), srv.name, srv.ifname, ifnames[srv.ifname]));
|
||||
ifnames[srv.ifname] = srv.name;
|
||||
let t = nc.get('network', srv.ifname);
|
||||
if (index(internal, srv.ifname) >= 0 || (t != null && (t != 'interface' || nc.get('network', srv.ifname, '_dawnline') == null)))
|
||||
push(errs, sprintf(_("Server %s: %s is one of the router's own network interfaces."), srv.name, srv.ifname));
|
||||
if (ports[srv.port])
|
||||
push(errs, sprintf(_('Server %s: UDP %s is also used by %s.'), srv.name, srv.port, ports[srv.port]));
|
||||
ports[srv.port] = srv.name;
|
||||
if (srv.subnet)
|
||||
push(subnets, srv.subnet);
|
||||
}
|
||||
return errs;
|
||||
};
|
||||
|
||||
/** Check the config file in `dir` (as /etc/config/dawnline would be); dies listing the problems. */
|
||||
function check_config_dir(dir) {
|
||||
ensure_dir(`${dir}/.delta`, 0o700);
|
||||
// Its own delta directory: changes staged in LuCI must not leak into the check.
|
||||
let c = cursor(dir, `${dir}/.delta`);
|
||||
if (!c.load('dawnline'))
|
||||
die(_('The configuration in this file cannot be read.'));
|
||||
let errs = config_errors(c);
|
||||
if (length(errs))
|
||||
die(sprintf(_("This configuration can't be used: %s"), join(' ', errs)));
|
||||
}
|
||||
|
||||
/** A private scratch directory under RUN_DIR (the caller removes it). */
|
||||
function scratch(kind) {
|
||||
ensure_dir(RUN_DIR);
|
||||
let dir = `${RUN_DIR}/${kind}.${rand_hex(4)}`;
|
||||
system(`rm -rf ${q(dir)}; mkdir -m 700 -p ${q(dir)}`);
|
||||
return dir;
|
||||
}
|
||||
|
||||
/* ---------- backups ---------- */
|
||||
|
||||
export function backup_list() {
|
||||
let out = [];
|
||||
for (let f in lsdir(BACKUP_DIR) ?? []) {
|
||||
let mm = match(f, NAME);
|
||||
if (!mm)
|
||||
continue;
|
||||
let st = stat(`${BACKUP_DIR}/${f}`);
|
||||
push(out, { name: f, created: st?.mtime ?? 0, size: st?.size ?? 0, reason: mm[3] ?? 'manual' });
|
||||
}
|
||||
return sort(out, (a, b) => b.created - a.created);
|
||||
};
|
||||
|
||||
/**
|
||||
* Make a backup now; returns its name. Written whole or not at all (a half
|
||||
* archive from a full flash never shows up as a backup). Of each kind the
|
||||
* oldest beyond KEEP are removed, except `keep` (the backup being restored,
|
||||
* which must survive the backup made just before restoring it).
|
||||
*/
|
||||
export function backup_create(reason, keep) {
|
||||
let kind = reason && match(reason, /^[a-z0-9-]{1,24}$/) ? reason : 'manual';
|
||||
let config_only = index(CONFIG_ONLY, kind) >= 0;
|
||||
// Flush live counters first so the backup has current usage.
|
||||
if (!config_only) {
|
||||
let u = read_json(`${RUN_DIR}/usage.json`, null);
|
||||
if (u)
|
||||
write_json(`${DATA_DIR}/usage.json`, u, 0o600);
|
||||
live.persist_events();
|
||||
}
|
||||
ensure_dir(BACKUP_DIR, 0o700);
|
||||
let t = localtime();
|
||||
let name = sprintf('dawnline-%04d%02d%02d-%02d%02d%02d%s.tar.gz', t.year, t.mon, t.mday, t.hour, t.min, t.sec, kind == 'manual' ? '' : `-${kind}`);
|
||||
let want = config_only ? [ FILES[0] ] : FILES;
|
||||
let files = join(' ', map(filter(want, (f) => file_exists(`/${f}`)), q));
|
||||
// Not a NAME until it is complete.
|
||||
let tmp = `${BACKUP_DIR}/.${name}.tmp`;
|
||||
let r = sh(`tar -czf ${q(tmp)} -C / ${files}`);
|
||||
if (r.code != 0 || !rename(tmp, `${BACKUP_DIR}/${name}`)) {
|
||||
unlink(tmp);
|
||||
let why = trim(r.out);
|
||||
die(why ? sprintf(_('Backup failed: %s'), why) : _('Backup failed: the archive could not be written (is the flash full?)'));
|
||||
}
|
||||
system(`chmod 600 ${q(BACKUP_DIR + '/' + name)}`);
|
||||
let by_kind = {};
|
||||
for (let b in backup_list())
|
||||
if (b.name != keep && b.name != name)
|
||||
push(by_kind[b.reason] ??= [], b);
|
||||
for (let k, list in by_kind)
|
||||
for (let i = KEEP[k] ?? 3; i < length(list); i++)
|
||||
unlink(`${BACKUP_DIR}/${list[i].name}`);
|
||||
return name;
|
||||
};
|
||||
|
||||
function backup_path(name) {
|
||||
if (!match(name ?? '', NAME))
|
||||
die(_('Invalid backup name.'));
|
||||
let p = `${BACKUP_DIR}/${name}`;
|
||||
if (!file_exists(p))
|
||||
die(_('Backup not found.'));
|
||||
return p;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate an archive: only Dawnline's own files, all of them regular files
|
||||
* (no links, devices or directories), within a sane unpacked size.
|
||||
*/
|
||||
function check_archive(path) {
|
||||
let r = sh(`tar -tvzf ${q(path)}`);
|
||||
if (r.code != 0)
|
||||
die(_('Not a valid backup archive.'));
|
||||
let has_config = false, total = 0;
|
||||
for (let line in split(trim(r.out), '\n')) {
|
||||
let f = split(trim(line), /[ \t]+/);
|
||||
let name = replace(f[length(f) >= 6 ? 5 : length(f) - 1] ?? '', /^\.?\//, '');
|
||||
// "-rw------- root/root 1234 2026-01-01 12:00:00 etc/config/dawnline"; links show "a -> b".
|
||||
if (substr(line, 0, 1) != '-' || index(line, ' -> ') >= 0 || index(line, ' link to ') >= 0)
|
||||
die(sprintf(_('Unexpected entry in backup: %s'), name));
|
||||
if (index(FILES, name) < 0)
|
||||
die(sprintf(_('Unexpected file in backup: %s'), name));
|
||||
total += +(f[2] ?? 0) || 0;
|
||||
if (name == 'etc/config/dawnline')
|
||||
has_config = true;
|
||||
}
|
||||
if (!has_config)
|
||||
die(_('Backup does not contain /etc/config/dawnline.'));
|
||||
if (total > MAX_UNPACKED)
|
||||
die(_('Backup is too large.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Put the files unpacked in `dir` in place, under the config lock. The
|
||||
* config goes first: when it cannot be written nothing else is touched, and
|
||||
* a data file that fails afterwards is reported, not ignored.
|
||||
*/
|
||||
function install_files(dir) {
|
||||
ensure_dir(DATA_DIR, 0o700);
|
||||
let l = lock('config');
|
||||
try {
|
||||
for (let f in FILES) {
|
||||
let data = readfile(`${dir}/${f}`);
|
||||
if (data != null && !write_file(`/${f}`, data, 0o600))
|
||||
die(sprintf(_("Couldn't write %s. Is the flash full?"), `/${f}`));
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
unlock(l);
|
||||
die(e.message);
|
||||
}
|
||||
unlock(l);
|
||||
system('chmod 600 /etc/config/dawnline');
|
||||
}
|
||||
|
||||
export function backup_restore(name, path) {
|
||||
path ??= backup_path(name);
|
||||
check_archive(path);
|
||||
let dir = scratch('restore');
|
||||
try {
|
||||
let r = sh(`tar -xzf ${q(path)} -C ${q(dir)} ${join(' ', map(FILES, q))} 2>/dev/null; test -f ${q(dir + '/etc/config/dawnline')}`);
|
||||
if (r.code != 0)
|
||||
die(_('Not a valid backup archive.'));
|
||||
check_config_dir(`${dir}/etc/config`);
|
||||
backup_create('pre-restore', name);
|
||||
install_files(dir);
|
||||
}
|
||||
catch (e) {
|
||||
system(`rm -rf ${q(dir)}`);
|
||||
die(e.message);
|
||||
}
|
||||
system(`rm -rf ${q(dir)}`);
|
||||
// Live counters must follow the restored file: the agent reloads them from
|
||||
// flash on its next tick (its watcher may write the old ones again before).
|
||||
unlink(`${RUN_DIR}/usage.json`);
|
||||
unlink(`${RUN_DIR}/events.json`);
|
||||
write_file(live.USAGE_RELOAD, `${time()}`, 0o600);
|
||||
return apply.apply();
|
||||
};
|
||||
|
||||
export function backup_delete(name) {
|
||||
unlink(backup_path(name));
|
||||
return true;
|
||||
};
|
||||
|
||||
export function backup_read(name) {
|
||||
return b64enc(readfile(backup_path(name)));
|
||||
};
|
||||
|
||||
export function backup_upload(data_b64) {
|
||||
let data = b64dec(data_b64 ?? '');
|
||||
if (!data || length(data) > 4 * 1024 * 1024)
|
||||
die(_('Upload is empty or too large.'));
|
||||
ensure_dir(RUN_DIR);
|
||||
let tmp = `${RUN_DIR}/upload.tar.gz`;
|
||||
write_file(tmp, data);
|
||||
let res;
|
||||
try {
|
||||
res = backup_restore(null, tmp);
|
||||
}
|
||||
catch (e) {
|
||||
unlink(tmp);
|
||||
die(e.message);
|
||||
}
|
||||
unlink(tmp);
|
||||
return res;
|
||||
};
|
||||
|
||||
/* ---------- export / import (JSON) ---------- */
|
||||
|
||||
export function export_json() {
|
||||
let c = cursor();
|
||||
c.load('dawnline');
|
||||
let sections = [];
|
||||
c.foreach('dawnline', null, (s) => {
|
||||
let o = { '.type': s['.type'], '.name': s['.name'] };
|
||||
for (let k, v in s)
|
||||
if (substr(k, 0, 1) != '.')
|
||||
o[k] = v;
|
||||
push(sections, o);
|
||||
});
|
||||
return { format: 'dawnline', version: VERSION, exported: time(), sections };
|
||||
};
|
||||
|
||||
/** An export as /etc/config/dawnline text; dies on anything that is not one. */
|
||||
function export_to_uci(data) {
|
||||
if (type(data) != 'object' || data.format != 'dawnline' || type(data.sections) != 'array')
|
||||
die(_('Not a Dawnline export.'));
|
||||
let types = { settings: true, server: true, person: true, device: true, service: true, circle: true };
|
||||
let esc = (x) => replace(`${x}`, "'", "'\\''");
|
||||
let lines = [];
|
||||
for (let s in data.sections) {
|
||||
if (type(s) != 'object' || !types[s['.type']] || !match(s['.name'] ?? '', /^[A-Za-z0-9_]{1,64}$/))
|
||||
die(sprintf(_('Invalid section %s.'), s?.['.name']));
|
||||
push(lines, `config ${s['.type']} '${s['.name']}'`);
|
||||
for (let k, v in s) {
|
||||
if (substr(k, 0, 1) == '.')
|
||||
continue;
|
||||
if (!match(k, /^[a-z0-9_]{1,32}$/) || (type(v) != 'string' && type(v) != 'array'))
|
||||
die(sprintf(_('Invalid option %s.'), k));
|
||||
if (type(v) == 'array')
|
||||
for (let x in v) {
|
||||
if (type(x) != 'string')
|
||||
die(sprintf(_('Invalid option %s.'), k));
|
||||
push(lines, `\tlist ${k} '${esc(x)}'`);
|
||||
}
|
||||
else
|
||||
push(lines, `\toption ${k} '${esc(v)}'`);
|
||||
}
|
||||
push(lines, '');
|
||||
}
|
||||
return join('\n', lines);
|
||||
}
|
||||
|
||||
export function import_json(data) {
|
||||
let text = export_to_uci(data);
|
||||
let dir = scratch('import');
|
||||
try {
|
||||
write_file(`${dir}/dawnline`, text, 0o600);
|
||||
check_config_dir(dir);
|
||||
}
|
||||
catch (e) {
|
||||
system(`rm -rf ${q(dir)}`);
|
||||
die(e.message);
|
||||
}
|
||||
system(`rm -rf ${q(dir)}`);
|
||||
backup_create('pre-import');
|
||||
let l = lock('config');
|
||||
let ok = write_file('/etc/config/dawnline', text, 0o600);
|
||||
unlock(l);
|
||||
if (!ok)
|
||||
die(sprintf(_("Couldn't write %s. Is the flash full?"), '/etc/config/dawnline'));
|
||||
return apply.apply();
|
||||
};
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,187 +0,0 @@
|
||||
// Dawnline — adding, changing, rotating and removing a person's devices: one
|
||||
// code path for the admin UI (rpcd) and the portal, with the config lock,
|
||||
// the runtime sync, the activity log and the "configuration issued" mark.
|
||||
//
|
||||
// opts = { by: 'admin' | 'portal', person?: the only person whose devices may
|
||||
// be touched (the portal), defer?: load new keys a moment later (the portal
|
||||
// page came through the device being rotated) }. The portal is held to the
|
||||
// device limit (dawnline.core) and cannot touch a device the admin turned off.
|
||||
|
||||
import { sh_bg } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as apply from 'dawnline.apply';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as store from 'dawnline.store';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/** AmneziaWG is on the router, so device keys can be made and loaded. */
|
||||
export function ready() {
|
||||
return awg.installed();
|
||||
};
|
||||
|
||||
/** Dies with the admin's wording when AmneziaWG is missing. */
|
||||
export function need_awg() {
|
||||
if (!awg.installed()) {
|
||||
let miss = awg.state().missing ?? [];
|
||||
die(length(miss) ? sprintf(_('AmneziaWG is not installed (%s): install it in Dawnline → Components.'), join(', ', miss))
|
||||
: _('AmneziaWG is not installed: install it in Dawnline → Components.'));
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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];
|
||||
if (!d || (opts.person != null && d.person != opts.person))
|
||||
die(opts.person != null ? _('Device not found.') : _('Unknown device.'));
|
||||
if (opts.by == 'portal' && d.disabled)
|
||||
die(_('The admin has turned this device off.'));
|
||||
return d;
|
||||
}
|
||||
|
||||
/**
|
||||
* New device with fresh keys (or the browser's public key): { id, config }.
|
||||
* `opts.lan_bypass` true keeps its own local network outside the tunnel.
|
||||
*/
|
||||
export function add(person, name, kind, opts) {
|
||||
opts ??= {};
|
||||
let by = opts.by ?? 'admin';
|
||||
let id = store.transact((c, m) => core.create_device(c, m, person, name, kind,
|
||||
{ by, keys: awg.new_keys(opts.public_key || null), lan_bypass: opts.lan_bypass === true }));
|
||||
let s = apply.sync();
|
||||
let m = core.load();
|
||||
live.add_event({ type: 'device_added', person, device: id, detail: m.devices[id].name, by });
|
||||
awg.mark_issued(m, id);
|
||||
return { id, config: awg.client_config(m, m.devices[id]), warning: s.warning };
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 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 ??= {};
|
||||
let by = opts.by ?? 'admin';
|
||||
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 ((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) => {
|
||||
let d = own(m, id, opts);
|
||||
let was = { ...d };
|
||||
if (ch.name != null && ch.name != d.name)
|
||||
core.rename_device(c, m, id, ch.name);
|
||||
if (ch.disabled != null) {
|
||||
if (ch.disabled)
|
||||
c.set(core.CONFIG, id, 'disabled', '1');
|
||||
else
|
||||
c.delete(core.CONFIG, id, 'disabled');
|
||||
}
|
||||
if (link)
|
||||
linked = core.set_device_link(c, m, id, ch);
|
||||
return was;
|
||||
});
|
||||
let r = ch.name != null || ch.disabled != null ? apply.sync() : {};
|
||||
if (ch.disabled != null && !!ch.disabled != !!d0.disabled)
|
||||
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 options with what they became (change_detail).
|
||||
if (length(linked))
|
||||
live.add_event({ type: 'device_changed', person: d0.person, device: id, detail: change_detail(linked, ch), by });
|
||||
return r;
|
||||
};
|
||||
|
||||
/*
|
||||
* New keys for a device: returns its new configuration, marked as issued (it
|
||||
* goes to the set-up sheet; the device then waits for it as "Not set up yet",
|
||||
* not as "settings changed"). Only through reconnect: new keys always mean
|
||||
* setting the device up again.
|
||||
*/
|
||||
function rotate(id, opts) {
|
||||
opts ??= {};
|
||||
let by = opts.by ?? 'admin';
|
||||
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;
|
||||
if (opts.defer)
|
||||
sh_bg('sleep 3; /usr/bin/dawnline sync');
|
||||
else
|
||||
s = apply.sync();
|
||||
let m = core.load();
|
||||
let d = m.devices[id];
|
||||
live.add_event({ type: 'device_keys', person: d.person, device: id, detail: d.name, by });
|
||||
awg.mark_issued(m, id);
|
||||
let cfg = awg.client_config(m, d);
|
||||
if (s?.warning)
|
||||
cfg.warning = s.warning;
|
||||
return cfg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a device up from scratch (a new phone, the app reinstalled, settings
|
||||
* 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. `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 ??= {};
|
||||
let cfg = rotate(id, opts);
|
||||
// A deferred sync (the page came through this device) lets the old keys live a few seconds more.
|
||||
awg.mark_reconnect(core.load(), id, time() + (opts.defer ? 5 : 0));
|
||||
return cfg;
|
||||
};
|
||||
|
||||
/** Remove a device. Returns the runtime sync. */
|
||||
export function remove(id, opts) {
|
||||
opts ??= {};
|
||||
let by = opts.by ?? 'admin';
|
||||
let d = store.transact((c, m) => {
|
||||
let d = own(m, id, opts);
|
||||
core.delete_device(c, m, id);
|
||||
return d;
|
||||
});
|
||||
let r = apply.sync();
|
||||
live.add_event({ type: 'device_removed', person: d.person, device: id, detail: d.name, dname: d.name, dkind: d.kind, by });
|
||||
return r;
|
||||
};
|
||||
@@ -1,303 +0,0 @@
|
||||
// Dawnline — fw4 and routing around it: each tunnel's zone, its forwardings
|
||||
// to home and WAN, the WAN rules for the tunnels and the portal (named
|
||||
// sections dawnline_*), what the watchdog checks about them, and the nft
|
||||
// table that keeps Dawnline's own answers out of proxies. Only sections
|
||||
// named dawnline_* are ever changed.
|
||||
|
||||
import { readfile, unlink } from 'fs';
|
||||
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';
|
||||
import * as nft from 'dawnline.nft';
|
||||
import * as loop from 'dawnline.loop';
|
||||
|
||||
const LOOP_ZONE = 'dl_loop';
|
||||
/*
|
||||
* Our own answers leave by the main table: the portal's (TCP) and each
|
||||
* tunnel's (UDP). Proxies on the router (tproxy with an fwmark rule to a
|
||||
* `local default` table) mark the router's own traffic to internet addresses
|
||||
* in an output route chain, which diverts replies to outside clients into lo.
|
||||
* A policy rule on the source port can't exempt them: the reroute after the
|
||||
* mark is set looks up without ports. So a later output route chain of our
|
||||
* own clears exactly those proxy marks on packets from our listening ports,
|
||||
* and the kernel routes them again by main. Forwarded traffic is not touched.
|
||||
*/
|
||||
const REPLY_TABLE = 'dawnline_reply';
|
||||
const REPLY_SIG = `${RUN_DIR}/reply.nft`;
|
||||
/** Older versions kept answers out of proxies with `ip rule` at this priority; it never matched. */
|
||||
const PREF_REPLY = 90;
|
||||
|
||||
/** With `dry`, only says whether the firewall config differs from what Dawnline wants. */
|
||||
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;
|
||||
let created = uci_section(c, 'firewall', name, stype), diff = {};
|
||||
for (let k, v in opts)
|
||||
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);
|
||||
let portal_zone = null, want_loop = false;
|
||||
// Where tunnel DNS is sent (dawnline.nft): into the netns for home names
|
||||
// while any server runs "like a home device", and to the LAN's DNS for
|
||||
// everyone else. A server that does not forward to those zones on its own
|
||||
// gets a rule for port 53 alone.
|
||||
let any_loop = length(filter(values(m.servers), (s) => s.lan_like && length(core.tunnels(s)))) > 0;
|
||||
let lan_dns = loop.lan_dns();
|
||||
let lan_dns_zone = null;
|
||||
if (lan_dns)
|
||||
for (let dev, d in snap.devices)
|
||||
if (d.zone && in_nets(lan_dns, d.nets ?? []))
|
||||
lan_dns_zone ??= d.zone;
|
||||
for (let id, srv in m.servers) {
|
||||
if (!length(core.tunnels(srv)))
|
||||
continue;
|
||||
let z = `dawnline_${id}`;
|
||||
let sn = net.server_net(srv, snap);
|
||||
portal_zone ??= sn.wan.zone;
|
||||
if (!srv.lan_like && any_loop)
|
||||
sec(`${z}_loopdns`, 'rule', {
|
||||
name: `Dawnline-${srv.ifname}-names`, src: srv.ifname, dest: LOOP_ZONE,
|
||||
proto: 'tcp udp', dest_port: 53, target: 'ACCEPT',
|
||||
});
|
||||
if (lan_dns_zone && lan_dns_zone != srv.ifname && index(sn.zones, lan_dns_zone) < 0)
|
||||
sec(`${z}_landns`, 'rule', {
|
||||
name: `Dawnline-${srv.ifname}-dns`, src: srv.ifname, dest: lan_dns_zone, dest_ip: lan_dns,
|
||||
proto: 'tcp udp', dest_port: 53, target: 'ACCEPT',
|
||||
});
|
||||
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: '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) {
|
||||
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 });
|
||||
want_loop = true;
|
||||
}
|
||||
sec(`${z}_port`, 'rule', {
|
||||
name: `Dawnline-${srv.ifname}`, src: sn.wan.zone, proto: 'udp',
|
||||
dest_port: srv.port, target: 'ACCEPT', enabled: srv.enabled ? null : '0',
|
||||
});
|
||||
}
|
||||
|
||||
// The link into the netns that re-enters the router from the LAN.
|
||||
if (want_loop) {
|
||||
sec('dawnline_loop', 'zone', {
|
||||
name: LOOP_ZONE, device: [ net.INTERNAL.transit ],
|
||||
input: 'REJECT', output: 'ACCEPT', forward: 'REJECT',
|
||||
});
|
||||
// The Home-only resolver in the netns reaches its upstream DNS this way.
|
||||
sec('dawnline_loop_wan', 'forwarding', { src: LOOP_ZONE, dest: portal_zone ?? 'wan' });
|
||||
}
|
||||
|
||||
// Never open SSH, DNS or the LuCI web server for the portal, whatever the settings say.
|
||||
let portal_port_ok = (p) => p >= 1 && p <= 65535 && index([ 22, 53, 80, 443 ], p) < 0;
|
||||
if (m.settings.portal_enabled && portal_port_ok(m.settings.portal_port)) {
|
||||
let wan = portal_zone ?? 'wan';
|
||||
sec('dawnline_portal', 'rule', {
|
||||
name: 'Dawnline-Portal', src: wan, proto: 'tcp',
|
||||
dest_port: m.settings.portal_port, target: 'ACCEPT',
|
||||
});
|
||||
// IP addresses in links: plain HTTP next to the HTTPS port, and 80 forwarded to it.
|
||||
if (core.portal_http(m) && portal_port_ok(core.portal_http_port(m))) {
|
||||
sec('dawnline_portal_http', 'rule', {
|
||||
name: 'Dawnline-Portal-HTTP', src: wan, proto: 'tcp',
|
||||
dest_port: core.portal_http_port(m), target: 'ACCEPT',
|
||||
});
|
||||
}
|
||||
// The portal keeps its own port: 80 and 443 stay with the LuCI web server.
|
||||
}
|
||||
|
||||
let stale = [];
|
||||
c.foreach('firewall', null, (s) => {
|
||||
if (substr(s['.name'], 0, 9) == 'dawnline_' && !want[s['.name']])
|
||||
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');
|
||||
}
|
||||
return changed;
|
||||
};
|
||||
|
||||
/**
|
||||
* 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 drift(m) {
|
||||
let t = sh_out('nft -t list table inet fw4 2>/dev/null') ?? '';
|
||||
if (t == '')
|
||||
return 'off';
|
||||
if (sync(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.enabled && length(core.tunnels(srv)))
|
||||
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;
|
||||
};
|
||||
|
||||
const MARK_ALL = 0xffffffff;
|
||||
// "1000: from all fwmark 0x1 lookup 100", or "fwmark 0x1/0xff". A module
|
||||
// constant, compiled once, not for each rule.
|
||||
const MARK_RULE = /^(\d+):.*fwmark 0x([0-9a-f]+)(\/0x([0-9a-f]+))?.* lookup (\S+)/;
|
||||
|
||||
function hx(n) {
|
||||
return sprintf('0x%x', n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Policy rules that send marked traffic into a table with `local default`,
|
||||
* the way transparent proxies capture the router's own traffic:
|
||||
* [{ pref, mark, mask, table }], one per mark and mask.
|
||||
*/
|
||||
export function proxy_diverts() {
|
||||
let out = [], seen = {};
|
||||
for (let fam in [ '-4', '-6' ])
|
||||
for (let line in split(sh_out(`ip ${fam} rule show 2>/dev/null`) ?? '', '\n')) {
|
||||
let mm = match(line, MARK_RULE);
|
||||
if (!mm || +mm[1] >= 32766)
|
||||
continue;
|
||||
let d = { pref: +mm[1], mark: hex(mm[2]), mask: mm[4] ? hex(mm[4]) : MARK_ALL, table: mm[5] };
|
||||
let key = `${d.mark}/${d.mask}`;
|
||||
if (seen[key] || !match(sh_out(`ip ${fam} route show table ${d.table} 2>/dev/null`) ?? '', /(^|\n)local default/))
|
||||
continue;
|
||||
seen[key] = true;
|
||||
push(out, d);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/** A divert as `ip rule` shows it, for people: "pref 1000 fwmark 0x1 → table 100". */
|
||||
export function divert_str(d) {
|
||||
return `pref ${d.pref} fwmark ${hx(d.mark)}${d.mask == MARK_ALL ? '' : `/${hx(d.mask)}`} → table ${d.table}`;
|
||||
};
|
||||
|
||||
/** Ports Dawnline answers on from the outside: { tcp: [...], udp: [...] }. */
|
||||
function reply_ports(m) {
|
||||
let p = { tcp: [], udp: [] };
|
||||
let ok = (n) => n >= 1 && n <= 65535;
|
||||
if (m.settings.portal_enabled) {
|
||||
push(p.tcp, +m.settings.portal_port);
|
||||
if (core.portal_http(m))
|
||||
push(p.tcp, +core.portal_http_port(m));
|
||||
}
|
||||
for (let id, srv in m.servers)
|
||||
if (srv.private_key && srv.enabled)
|
||||
push(p.udp, +srv.port);
|
||||
return { tcp: uniq(filter(p.tcp, ok)), udp: uniq(filter(p.udp, ok)) };
|
||||
}
|
||||
|
||||
/** The reply table for these diverts, or '' when there is nothing to keep out of them. */
|
||||
function reply_script(m, diverts) {
|
||||
let p = reply_ports(m);
|
||||
if (!length(diverts) || !length(p.tcp) && !length(p.udp))
|
||||
return '';
|
||||
let t = [];
|
||||
push(t, `table inet ${REPLY_TABLE} {`);
|
||||
push(t, ' chain out {');
|
||||
// After the proxies' own output chains (they mark at mangle, -150).
|
||||
push(t, ' type route hook output priority filter + 10; policy accept;');
|
||||
for (let proto in [ 'tcp', 'udp' ]) {
|
||||
if (!length(p[proto]))
|
||||
continue;
|
||||
for (let d in diverts)
|
||||
// Only the divert's bits: other marks on the packet (multi-WAN) stay.
|
||||
push(t, ` ${proto} sport { ${join(', ', p[proto])} } ` + (d.mask == MARK_ALL
|
||||
? `meta mark ${hx(d.mark)} meta mark set 0x0`
|
||||
: `meta mark & ${hx(d.mask)} == ${hx(d.mark)} meta mark set meta mark & ${hx(~d.mask & MARK_ALL)}`));
|
||||
}
|
||||
push(t, ' }');
|
||||
push(t, '}');
|
||||
return join('\n', t) + '\n';
|
||||
}
|
||||
|
||||
function reply_loaded() {
|
||||
return system(`nft list table inet ${REPLY_TABLE} >/dev/null 2>&1`) == 0;
|
||||
}
|
||||
|
||||
/** Are the answers from our ports kept out of these diverts right now (watchdog)? */
|
||||
export function reply_ok(m, diverts) {
|
||||
let s = reply_script(m, diverts);
|
||||
return s == '' || (s == readfile(REPLY_SIG) && reply_loaded());
|
||||
};
|
||||
|
||||
/**
|
||||
* Keep the reply table in step with our ports and the proxies' diverts (apply
|
||||
* and every agent tick: a proxy may start after Dawnline). Cheap when nothing changed.
|
||||
*/
|
||||
export function sync_reply_routes(m) {
|
||||
// Rules of older versions ("90: from all iif lo ipproto udp sport 51820 lookup main", once without "iif lo").
|
||||
for (let fam in [ '-4', '-6' ])
|
||||
for (let line in split(sh_out(`ip ${fam} rule show priority ${PREF_REPLY} 2>/dev/null`) ?? '', '\n')) {
|
||||
let mm = match(line, /(iif lo )?ipproto (tcp|udp) sport ([0-9]+) lookup main/);
|
||||
if (mm)
|
||||
sh(`ip ${fam} rule del priority ${PREF_REPLY}${mm[1] ? ' iif lo' : ''} ipproto ${mm[2]} sport ${mm[3]} lookup main 2>/dev/null`);
|
||||
}
|
||||
let s = reply_script(m, proxy_diverts());
|
||||
if (s == '') {
|
||||
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()) {
|
||||
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);
|
||||
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);
|
||||
};
|
||||
@@ -1,196 +0,0 @@
|
||||
// Dawnline — city-level locations for the admin map.
|
||||
//
|
||||
// Client addresses are looked up once with a third-party service (ipwho.is,
|
||||
// ipapi.co as fallback) and cached: the live copy in RUN_DIR (tmpfs), put on
|
||||
// flash (DATA_DIR/geo.json) by flush() about hourly, like the usage
|
||||
// counters. Only the agent performs lookups, in a child process (the network
|
||||
// may take seconds; neither its watcher nor its tick waits for them) and
|
||||
// merges them into the cache itself; the UI reads the cache.
|
||||
|
||||
import { stat } from 'fs';
|
||||
import { read_json, write_json, sh_out, q, ensure_dir, ip2int, DATA_DIR, RUN_DIR } from 'dawnline.util';
|
||||
|
||||
const CACHE = `${RUN_DIR}/geo.json`;
|
||||
const SAVED = `${DATA_DIR}/geo.json`;
|
||||
const TTL = 30 * 86400;
|
||||
const RETRY = 86400;
|
||||
// Entries kept at least; addresses still shown are never pushed out (merge).
|
||||
const CAP = 500;
|
||||
|
||||
// [base, prefix] of ranges with no meaningful location (private, CGNAT, multicast, …).
|
||||
const PRIVATE4 = map([ [ '0.0.0.0', 8 ], [ '10.0.0.0', 8 ], [ '100.64.0.0', 10 ], [ '127.0.0.0', 8 ], [ '169.254.0.0', 16 ],
|
||||
[ '172.16.0.0', 12 ], [ '192.168.0.0', 16 ], [ '198.18.0.0', 15 ], [ '224.0.0.0', 3 ] ], (r) => [ ip2int(r[0]), 1 << (32 - r[1]) ]);
|
||||
|
||||
/** True for addresses a geolocation service can say something about. */
|
||||
export function is_public(ip) {
|
||||
if (type(ip) != 'string' || ip == '')
|
||||
return false;
|
||||
let n = ip2int(ip);
|
||||
if (n != null) {
|
||||
for (let r in PRIVATE4)
|
||||
if (n >= r[0] && n < r[0] + r[1])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
let l = lc(ip);
|
||||
return match(l, /^[0-9a-f:]+$/) != null && !match(l, /^(fe[89ab]|f[cd]|::1$|::$)/);
|
||||
};
|
||||
|
||||
export function load() {
|
||||
return read_json(CACHE, null) ?? read_json(SAVED, null) ?? { ips: {}, home: null };
|
||||
};
|
||||
|
||||
/** The live copy (tmpfs); flush() puts it on flash. */
|
||||
export function save(cache) {
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(CACHE, cache, 0o600);
|
||||
};
|
||||
|
||||
/** Copy the live cache to flash when it is newer (the agent, about hourly and on a clean stop). */
|
||||
export function flush() {
|
||||
let live = stat(CACHE);
|
||||
if (!live || live.mtime <= (stat(SAVED)?.mtime ?? 0))
|
||||
return false;
|
||||
let c = read_json(CACHE, null);
|
||||
if (!c)
|
||||
return false;
|
||||
ensure_dir(DATA_DIR, 0o700);
|
||||
write_json(SAVED, c, 0o600);
|
||||
return true;
|
||||
};
|
||||
|
||||
function fetch_json(url) {
|
||||
let out = sh_out(`uclient-fetch -q -T 5 -O - ${q(url)}`);
|
||||
if (!out)
|
||||
return null;
|
||||
try {
|
||||
return json(out);
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function lookup_ipwho(ip) {
|
||||
let j = fetch_json(`https://ipwho.is/${ip}?fields=success,ip,latitude,longitude,city,country,country_code,connection`);
|
||||
if (!j || j.success !== true || j.latitude == null)
|
||||
return null;
|
||||
let cn = j.connection ?? {};
|
||||
return { lat: +j.latitude, lon: +j.longitude, city: j.city ?? '', country: j.country ?? '', cc: j.country_code ?? '', ip: j.ip,
|
||||
asn: cn.asn ? +cn.asn : 0, org: cn.isp || cn.org || '' };
|
||||
}
|
||||
|
||||
function lookup_ipapi(ip) {
|
||||
let j = fetch_json(ip ? `https://ipapi.co/${ip}/json/` : 'https://ipapi.co/json/');
|
||||
if (!j || j.error || j.latitude == null)
|
||||
return null;
|
||||
return { lat: +j.latitude, lon: +j.longitude, city: j.city ?? '', country: j.country_name ?? '', cc: j.country_code ?? '', ip: j.ip,
|
||||
asn: +replace(`${j.asn ?? ''}`, /^AS/i, '') || 0, org: j.org ?? '' };
|
||||
}
|
||||
|
||||
/** One lookup with the configured provider and the other as fallback. */
|
||||
export function lookup(provider, ip) {
|
||||
let order = provider == 'ipapi.co' ? [ lookup_ipapi, lookup_ipwho ] : [ lookup_ipwho, lookup_ipapi ];
|
||||
for (let fn in order) {
|
||||
let r = fn(ip ?? '');
|
||||
if (r)
|
||||
return r;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
/**
|
||||
* The public addresses of `ips` the cache does not know or knows from too
|
||||
* long ago (a failed lookup is retried after a day), once each and in order.
|
||||
* Returns an empty array when there is nothing to look up.
|
||||
*/
|
||||
export function stale(cache, ips, ts) {
|
||||
let now = ts ?? time(), out = [], seen = {};
|
||||
for (let ip in ips) {
|
||||
if (seen[ip] || !is_public(ip))
|
||||
continue;
|
||||
seen[ip] = true;
|
||||
let c = cache.ips[ip];
|
||||
if (c && ((c.fail && now - c.fail < RETRY) || (!c.fail && c.asn != null && now - c.ts < TTL)))
|
||||
continue;
|
||||
push(out, ip);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* Look up at most `max` of `ips`, within `budget` seconds (each lookup may
|
||||
* wait on the network). Returns { ip: place, or false when the services had
|
||||
* nothing } for the ones tried. Network only, no cache: the agent runs it in
|
||||
* a child process and merges the result itself.
|
||||
*/
|
||||
export function lookup_all(provider, ips, max, budget) {
|
||||
let started = time(), out = {}, done = 0;
|
||||
for (let ip in ips) {
|
||||
if (done >= max || (budget != null && time() - started >= budget))
|
||||
break;
|
||||
let r = lookup(provider, ip);
|
||||
done++;
|
||||
if (r)
|
||||
delete r.ip;
|
||||
out[ip] = r ?? false;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* Put lookup_all() results into the cache. Returns true when the cache
|
||||
* changed (the caller saves it). `shown` is what is still on the map: the
|
||||
* cache is kept bounded by dropping other entries only, oldest first, so a
|
||||
* lookup is never repeated for an address that is still on the map.
|
||||
*/
|
||||
export function merge(cache, found, shown, ts) {
|
||||
let now = ts ?? time(), changed = false;
|
||||
for (let ip, r in found) {
|
||||
if (!is_public(ip))
|
||||
continue;
|
||||
cache.ips[ip] = r ? { ...r, ts: now } : { fail: now };
|
||||
changed = true;
|
||||
}
|
||||
let keep = {};
|
||||
for (let ip in shown)
|
||||
keep[ip] = true;
|
||||
let all = keys(cache.ips);
|
||||
if (length(all) > CAP) {
|
||||
let other = sort(filter(all, (ip) => !keep[ip]),
|
||||
(a, b) => (cache.ips[a].ts ?? cache.ips[a].fail ?? 0) - (cache.ips[b].ts ?? cache.ips[b].fail ?? 0));
|
||||
for (let i = 0; i < length(all) - CAP && i < length(other); i++) {
|
||||
delete cache.ips[other[i]];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
};
|
||||
|
||||
/** Locate the router itself (the arcs' origin), at most every 6 hours. */
|
||||
export function resolve_home(settings, cache, wan_ip) {
|
||||
let h = cache.home;
|
||||
let now = time();
|
||||
if (h && h.asn != null && now - (h.ts ?? 0) < 6 * 3600 && (!wan_ip || !is_public(wan_ip) || h.wan == wan_ip))
|
||||
return false;
|
||||
// Behind NAT, ask the service for our public address instead.
|
||||
let r = lookup(settings.geo_provider, is_public(wan_ip) ? wan_ip : '');
|
||||
cache.home = r ? { ...r, wan: wan_ip, ts: now } : { ...(h ?? {}), ts: now };
|
||||
return true;
|
||||
};
|
||||
|
||||
export function place(cache, ip) {
|
||||
let c = cache.ips[ip];
|
||||
return (c && !c.fail) ? c : null;
|
||||
};
|
||||
|
||||
/** "City · ISP" for the activity log; null for no place. */
|
||||
export function label(pl) {
|
||||
return pl ? join(' · ', filter([ pl.city, pl.org ], (x) => x)) : null;
|
||||
};
|
||||
|
||||
export function clear() {
|
||||
save({ ips: {}, home: null });
|
||||
ensure_dir(DATA_DIR, 0o700);
|
||||
write_json(SAVED, { ips: {}, home: null }, 0o600);
|
||||
};
|
||||
@@ -1,257 +0,0 @@
|
||||
// Dawnline — GitHub for the updater and the Components tab: API calls with a
|
||||
// short cache, releases (with each asset's sha256), the changes between two
|
||||
// tags, and verified downloads.
|
||||
//
|
||||
// Trust: the API is only ever asked directly (TLS to api.github.com). Files
|
||||
// may come through a mirror when GitHub is unreachable, but only when their
|
||||
// sha256 is known from the API, and a file is kept only if it matches.
|
||||
|
||||
import { stat, popen, access } from 'fs';
|
||||
import { q, read_json, write_json, ensure_dir, RUN_DIR } from 'dawnline.util';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/** Prefixes that fetch github.com files when GitHub itself is unreachable. */
|
||||
export const MIRRORS = [ 'https://ghfast.top/', 'https://gh-proxy.com/', 'https://ghproxy.net/' ];
|
||||
|
||||
function run(cmd) {
|
||||
let p = popen(cmd + ' 2>&1', 'r');
|
||||
if (!p)
|
||||
return { code: 127, out: '' };
|
||||
let out = p.read('all') ?? '';
|
||||
return { code: p.close(), out };
|
||||
}
|
||||
|
||||
function have_curl() {
|
||||
return access('/usr/bin/curl', 'x');
|
||||
}
|
||||
|
||||
/**
|
||||
* GET a URL: { status, body } (status 0 when there was no answer). curl gives
|
||||
* the HTTP status, so a 403 rate limit is told apart from a dead network.
|
||||
*/
|
||||
function get(url, timeout) {
|
||||
timeout ??= 12;
|
||||
if (have_curl()) {
|
||||
let r = run(`curl -sSL --connect-timeout 6 --max-time ${+timeout} -H 'Accept: application/vnd.github+json' -A Dawnline -w '\\n%{http_code}' ${q(url)}`);
|
||||
// The body, then the status on the last line (-w).
|
||||
let i = rindex(r.out, '\n');
|
||||
let code = i < 0 ? '' : substr(r.out, i + 1);
|
||||
return match(code, /^[0-9][0-9][0-9]$/) ? { status: +code, body: substr(r.out, 0, i) } : { status: 0, body: '' };
|
||||
}
|
||||
let r = run(`uclient-fetch -q -T ${+timeout} -O - ${q(url)}`);
|
||||
return { status: r.code == 0 ? 200 : 0, body: r.code == 0 ? r.out : '' };
|
||||
}
|
||||
|
||||
/** JSON from the GitHub API, or null when GitHub did not answer; dies on a rate limit or another API error. */
|
||||
function api_json(url) {
|
||||
let r = get(url);
|
||||
if (r.status == 0)
|
||||
return null;
|
||||
let j = null;
|
||||
try {
|
||||
j = json(r.body);
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
if (r.status == 403 || r.status == 429 || (type(j) == 'object' && match(j.message ?? '', /rate limit/i)))
|
||||
die(_('GitHub API rate limit reached: try again in an hour.'));
|
||||
if (r.status == 404)
|
||||
return null;
|
||||
if (r.status >= 400)
|
||||
die(sprintf(_('GitHub: %s'), j?.message ?? `HTTP ${r.status}`));
|
||||
return j;
|
||||
}
|
||||
|
||||
function cached(key, ttl, fn) {
|
||||
let path = `${RUN_DIR}/gh-${replace(key, /[^A-Za-z0-9_.-]/g, '_')}.json`;
|
||||
let c = read_json(path, null);
|
||||
if (c && time() - (c.at ?? 0) < ttl)
|
||||
return c.data;
|
||||
let data = fn();
|
||||
if (data != null) {
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(path, { at: time(), data });
|
||||
}
|
||||
return data ?? c?.data;
|
||||
}
|
||||
|
||||
/* ---------- releases ---------- */
|
||||
|
||||
function slim(x) {
|
||||
let digests = {}, ids = {};
|
||||
for (let a in x.assets ?? []) {
|
||||
if (match(a.digest ?? '', /^sha256:[0-9a-f]{64}$/))
|
||||
digests[a.name] = substr(a.digest, 7);
|
||||
if (type(a.id) == 'int' && a.id > 0)
|
||||
ids[a.name] = a.id;
|
||||
}
|
||||
return {
|
||||
tag: x.tag_name, name: x.name ?? x.tag_name, published: x.published_at ?? null,
|
||||
prerelease: !!x.prerelease, url: x.html_url ?? null, body: substr(x.body ?? '', 0, 12000),
|
||||
assets: map(x.assets ?? [], (a) => a.name), digests, ids,
|
||||
};
|
||||
}
|
||||
|
||||
/** Releases of a repository, newest first, five per page (drafts skipped). */
|
||||
export function releases(rp, page) {
|
||||
page = page ?? 1;
|
||||
return cached(`rel-${rp}-${page}`, 600, () => {
|
||||
let j = api_json(`https://api.github.com/repos/${rp}/releases?per_page=5&page=${+page}`);
|
||||
return type(j) == 'array' ? map(filter(j, (x) => !x.draft), slim) : null;
|
||||
}) ?? [];
|
||||
};
|
||||
|
||||
/** One release by tag (notes, assets and their sha256), or null. */
|
||||
export function release(rp, tag) {
|
||||
return cached(`tag-${rp}-${tag}`, 600, () => {
|
||||
let j = api_json(`https://api.github.com/repos/${rp}/releases/tags/${tag}`);
|
||||
return type(j) == 'object' && j.tag_name ? slim(j) : null;
|
||||
});
|
||||
};
|
||||
|
||||
/** The sha256 GitHub publishes for a release file, or null (unknown: no mirrors for it). */
|
||||
export function asset_sha256(rp, tag, name) {
|
||||
try {
|
||||
return release(rp, tag)?.digests?.[name] ?? null;
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/** The latest stable release; the /releases/latest redirect when the API is out of reach. */
|
||||
export function latest(rp) {
|
||||
let r = null;
|
||||
try {
|
||||
r = cached(`latest-${rp}`, 300, () => {
|
||||
let j = api_json(`https://api.github.com/repos/${rp}/releases/latest`);
|
||||
return type(j) == 'object' && j.tag_name ? slim(j) : null;
|
||||
});
|
||||
}
|
||||
catch (e) {}
|
||||
if (r)
|
||||
return r;
|
||||
if (have_curl()) {
|
||||
let o = run(`curl -sI --connect-timeout 15 --max-time 30 -o /dev/null -w '%{redirect_url}' ${q('https://github.com/' + rp + '/releases/latest')}`);
|
||||
let m = match(trim(o.out), /\/releases\/tag\/([^\/?#]+)$/);
|
||||
if (m)
|
||||
return { tag: m[1], name: m[1], published: null, prerelease: false, url: `https://github.com/${rp}/releases/tag/${m[1]}`, body: '', assets: [], digests: {} };
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
/* ---------- what changed ---------- */
|
||||
|
||||
/**
|
||||
* What changed in a commit message, as points: release commits ("Dawnline
|
||||
* 0.3.1") list their changes as "- " lines in the body; other commits are
|
||||
* their title. Wrapped points are joined back into one line.
|
||||
*/
|
||||
export function points(msg) {
|
||||
let lines = split(msg ?? '', '\n');
|
||||
let title = trim(lines[0] ?? '');
|
||||
if (match(title, /^Merge /))
|
||||
return [];
|
||||
let out = [], cur = null;
|
||||
for (let i = 1; i < length(lines); i++) {
|
||||
let m = match(lines[i], /^\s*[-*]\s+(.*)$/);
|
||||
if (m) {
|
||||
if (cur != null)
|
||||
push(out, cur);
|
||||
cur = trim(m[1]);
|
||||
}
|
||||
else if (cur != null && match(lines[i], /^\s+\S/))
|
||||
cur += ' ' + trim(lines[i]);
|
||||
else if (cur != null) {
|
||||
push(out, cur);
|
||||
cur = null;
|
||||
}
|
||||
}
|
||||
if (cur != null)
|
||||
push(out, cur);
|
||||
if (!length(out) && !match(title, /^(Dawnline )?v?[0-9]+\.[0-9]+(\.[0-9]+)?$/) && title != '')
|
||||
push(out, title);
|
||||
return out;
|
||||
};
|
||||
|
||||
/** The changes from one tag to another, newest first: commit by commit, or the release's own commit. */
|
||||
export function changes(rp, base, head) {
|
||||
return cached(`changes-${rp}-${base}-${head}`, 600, () => {
|
||||
let out = [];
|
||||
let j = base ? api_json(`https://api.github.com/repos/${rp}/compare/${base}...${head}`) : null;
|
||||
if (type(j?.commits) == 'array') {
|
||||
for (let i = length(j.commits) - 1; i >= 0; i--)
|
||||
for (let p in points(j.commits[i]?.commit?.message))
|
||||
push(out, p);
|
||||
return slice(out, 0, 20);
|
||||
}
|
||||
let c = api_json(`https://api.github.com/repos/${rp}/commits/${head}`);
|
||||
return c?.commit ? slice(points(c.commit.message), 0, 20) : null;
|
||||
}) ?? [];
|
||||
};
|
||||
|
||||
/* ---------- downloads ---------- */
|
||||
|
||||
export function sha256(path) {
|
||||
return match(run(`sha256sum ${q(path)}`).out, /^([0-9a-f]{64})/)?.[1];
|
||||
};
|
||||
|
||||
/** A release file's URL on github.com in parts: { repo, tag, name }, or null for any other URL. */
|
||||
export function release_file(url) {
|
||||
let m = match(url ?? '', /^https:\/\/github\.com\/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)\/releases\/download\/([^\/?#]+)\/([^\/?#]+)$/);
|
||||
return m ? { repo: m[1], tag: m[2], name: m[3] } : null;
|
||||
};
|
||||
|
||||
/** The same file through the API (api.github.com), when its release is known; else null. */
|
||||
function asset_api_url(url) {
|
||||
let f = release_file(url);
|
||||
if (!f)
|
||||
return null;
|
||||
let id = null;
|
||||
try {
|
||||
id = release(f.repo, f.tag)?.ids?.[f.name];
|
||||
}
|
||||
catch (e) {}
|
||||
return id ? `https://api.github.com/repos/${f.repo}/releases/assets/${id}` : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Download to a file. o: { sha256, mirrors, log }. Direct first; mirrors
|
||||
* only when the sha256 is known (from the API) and mirrors are allowed. A
|
||||
* file that does not match the sha256 is thrown away and the next source is
|
||||
* tried. Returns the URL that worked, or null.
|
||||
*/
|
||||
export function download(url, dest, o) {
|
||||
o ??= {};
|
||||
let want = o.sha256 ? lc(o.sha256) : null;
|
||||
let curl = have_curl();
|
||||
let tries = [];
|
||||
// Without curl a release file comes through the API first: github.com's redirect to it carries a
|
||||
// Content-Security-Policy header that can outgrow uclient-fetch's 4 KB line buffer, and it stalls there.
|
||||
let api = curl ? null : asset_api_url(url);
|
||||
if (api)
|
||||
push(tries, { u: api, accept: 'application/octet-stream' });
|
||||
push(tries, { u: url });
|
||||
let direct = length(tries);
|
||||
if (want && o.mirrors && match(url, /^https:\/\/(github\.com|raw\.githubusercontent\.com)\//))
|
||||
for (let m in MIRRORS)
|
||||
push(tries, { u: m + url, mirror: true });
|
||||
for (let i, t in tries) {
|
||||
let host = replace(t.u, /^https:\/\/([^\/]+)\/.*$/, '$1');
|
||||
if (t.mirror)
|
||||
o.log?.(sprintf(_('Trying mirror %s'), host));
|
||||
let r = curl
|
||||
? run(`curl -fsSL --connect-timeout 15 --max-time 300 --retry 1 -o ${q(dest)} ${q(t.u)}`)
|
||||
: run(`uclient-fetch -q -T 30 ${t.accept ? `--header=${q('Accept: ' + t.accept)} ` : ''}-O ${q(dest)} ${q(t.u)}`);
|
||||
if (r.code == 0 && (stat(dest)?.size ?? 0) > 0) {
|
||||
if (!want || sha256(dest) == want)
|
||||
return t.u;
|
||||
o.log?.(sprintf(_("Warning: %s sent a file that does not match GitHub's sha256, discarded"), host));
|
||||
}
|
||||
system(`rm -f ${q(dest)}`);
|
||||
if (i == direct - 1 && length(tries) > direct)
|
||||
o.log?.(_('GitHub did not answer, trying mirrors'));
|
||||
}
|
||||
return null;
|
||||
};
|
||||
@@ -1,758 +0,0 @@
|
||||
// Dawnline — Health: what the router has and lacks (packages, listeners,
|
||||
// networks), the checks shown on Diagnostics → Health, and their fixes.
|
||||
// The checks' texts are in the language of the call (dawnline.i18n).
|
||||
|
||||
import { readfile, stat, lsdir } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import { _, N_ } from 'dawnline.i18n';
|
||||
import { q, sh, sh_out, parse_cidr, cidr_overlaps, ip2int, in_cidr, file_exists, cloudflare_ip } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as apply from 'dawnline.apply';
|
||||
import * as fw from 'dawnline.fw';
|
||||
import * as loop from 'dawnline.loop';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as builds from 'dawnline.awg_builds';
|
||||
import * as lookup from 'dawnline.lookup';
|
||||
import * as pc from 'dawnline.portalcert';
|
||||
|
||||
/* ---------- what the router has ---------- */
|
||||
|
||||
/**
|
||||
* Packages a feature needs that are missing, by feature: https (the portal: openssl-util and any
|
||||
* TLS module for uhttpd), acme (Let's Encrypt certificates), home (the home-like link), awg
|
||||
* (AmneziaWG: the tools with netifd's protocol script, and the kernel module). Checked by
|
||||
* files, so it is cheap enough for every page load.
|
||||
*/
|
||||
export function needs() {
|
||||
let tls = false;
|
||||
for (let d in [ '/lib', '/usr/lib' ])
|
||||
for (let f in lsdir(d) ?? [])
|
||||
if (substr(f, 0, 16) == 'libustream-ssl.s')
|
||||
tls = true;
|
||||
let kver = trim(readfile('/proc/sys/kernel/osrelease') ?? '');
|
||||
let kmod = (name) => stat(`/sys/module/${name}`) != null || file_exists(`/lib/modules/${kver}/${name}.ko`);
|
||||
let miss = (list) => map(filter(list, (x) => !x[0]), (x) => x[1]);
|
||||
return {
|
||||
https: miss([ [ file_exists('/usr/bin/openssl'), 'openssl-util' ], [ tls, 'libustream-openssl' ] ]),
|
||||
acme: miss([ [ file_exists('/usr/lib/acme/client/acme.sh'), 'acme' ], [ file_exists('/usr/lib/acme/client/dnsapi/dns_cf.sh'), 'acme-acmesh-dnsapi' ],
|
||||
[ file_exists('/www/luci-static/resources/view/acme.js'), 'luci-app-acme' ] ]),
|
||||
home: miss([ [ file_exists('/usr/libexec/ip-full'), 'ip-full' ], [ kmod('veth'), 'kmod-veth' ] ]),
|
||||
// amneziawg-tools ships both the awg tool and netifd's protocol script.
|
||||
awg: miss([ [ file_exists('/usr/bin/awg') && file_exists('/lib/netifd/proto/amneziawg.sh'), 'amneziawg-tools' ],
|
||||
[ kmod('amneziawg'), 'kmod-amneziawg' ] ]),
|
||||
};
|
||||
};
|
||||
|
||||
// " 12: 00000000:1194 00000000:0000 07 …": local address, then the remote one (none).
|
||||
// A module constant, compiled once: a literal in the loop would be compiled
|
||||
// again for every socket (0.12 ms each on the WR3000P, some 90 sockets a call).
|
||||
const UDP_UNCONNECTED = /^ *[0-9]+: [0-9A-F]+:([0-9A-F]{4}) [0-9A-F]+:0000 /;
|
||||
|
||||
/** UDP ports an unconnected socket is bound to (IPv4 and IPv6): nothing else can listen there. */
|
||||
export function udp_bound() {
|
||||
let out = {};
|
||||
for (let f in [ '/proc/net/udp', '/proc/net/udp6' ])
|
||||
for (let line in split(readfile(f) ?? '', '\n')) {
|
||||
let m = match(line, UDP_UNCONNECTED);
|
||||
if (m)
|
||||
out[hex(m[1])] = true;
|
||||
}
|
||||
return map(keys(out), (k) => +k);
|
||||
};
|
||||
|
||||
// netstat's columns and the port of "0.0.0.0:53" / ":::53". Module constants,
|
||||
// compiled once, not for each socket.
|
||||
const BLANKS = /[ \t]+/, PORT_SUFFIX = /:([0-9]+)$/;
|
||||
|
||||
/**
|
||||
* Programs on 'tcp' or 'udp' ports, our portal left out: { port: "dnsmasq" }.
|
||||
* Kernel sockets (a tunnel's) have no name.
|
||||
*/
|
||||
function port_programs(proto) {
|
||||
let out = {};
|
||||
for (let line in split(sh_out(`netstat -ln${proto == 'tcp' ? 't' : 'u'}p 2>/dev/null`) ?? '', '\n')) {
|
||||
let f = split(trim(line), BLANKS);
|
||||
if (f[0] != proto && f[0] != `${proto}6`)
|
||||
continue;
|
||||
let m = match(f[3] ?? '', PORT_SUFFIX);
|
||||
let owner = split(f[length(f) - 1], '/');
|
||||
if (!m || !owner[1] || out[m[1]])
|
||||
continue;
|
||||
// Our own portal runs as a dedicated uhttpd instance.
|
||||
if (index(readfile(`/proc/${+owner[0]}/cmdline`) ?? '', '/usr/share/dawnline/portal') >= 0)
|
||||
continue;
|
||||
out[m[1]] = owner[1];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function udp_port_programs() {
|
||||
return port_programs('udp');
|
||||
};
|
||||
|
||||
export function tcp_port_programs() {
|
||||
return port_programs('tcp');
|
||||
};
|
||||
|
||||
export function udp_listening(port) {
|
||||
return index(udp_bound(), +port) >= 0;
|
||||
};
|
||||
|
||||
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'))
|
||||
// state 0A = LISTEN
|
||||
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) {
|
||||
let out = sh_out(`netstat -lntp 2>/dev/null | awk '$4 ~ /:${+port}$/ {print $7}'`) ?? '';
|
||||
let owners = [];
|
||||
for (let o in split(out, '\n')) {
|
||||
let name = split(o, '/')[1];
|
||||
if (!name || index(owners, name) >= 0)
|
||||
continue;
|
||||
// Our own portal runs as a dedicated uhttpd instance.
|
||||
let pid = +split(o, '/')[0];
|
||||
let cmd = readfile(`/proc/${pid}/cmdline`) ?? '';
|
||||
if (index(cmd, '/usr/share/dawnline/portal') >= 0)
|
||||
continue;
|
||||
push(owners, name);
|
||||
}
|
||||
return owners;
|
||||
};
|
||||
|
||||
export function service_running(name, instance) {
|
||||
let s = sh_out(`ubus call service list '{"name":"${name}"}' 2>/dev/null`);
|
||||
if (!s)
|
||||
return false;
|
||||
try {
|
||||
return !!json(s)?.[name]?.instances?.[instance]?.running;
|
||||
}
|
||||
catch (e) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/** IPv4 networks of netifd interfaces: [{ iface, cidr }], without the given server's tunnel. */
|
||||
function other_subnets(srv_id) {
|
||||
let out = [];
|
||||
let st = sh_out('ubus call network.interface dump 2>/dev/null');
|
||||
let d = null;
|
||||
try {
|
||||
d = st ? json(st) : null;
|
||||
}
|
||||
catch (e) {
|
||||
d = null;
|
||||
}
|
||||
let ours = {};
|
||||
for (let id, s in core.load().servers)
|
||||
ours[s.ifname] = id;
|
||||
for (let i in d?.interface ?? []) {
|
||||
if (srv_id != null && ours[i.interface] == srv_id)
|
||||
continue;
|
||||
if (srv_id === false && ours[i.interface] != null)
|
||||
continue;
|
||||
for (let a in i['ipv4-address'] ?? [])
|
||||
push(out, { iface: i.interface, cidr: `${a.address}/${a.mask}` });
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/** Subnets in use on the router except the given server's (null: all of them). */
|
||||
/** The router's IPv4 networks, Dawnline's tunnels included: [{ iface, cidr }]. */
|
||||
export function networks() {
|
||||
return other_subnets(null);
|
||||
};
|
||||
|
||||
export function used_subnets(except_srv) {
|
||||
return map(other_subnets(except_srv), (x) => x.cidr);
|
||||
};
|
||||
|
||||
/** Subnets of interfaces that are not Dawnline tunnels (an imported config is checked against these). */
|
||||
export function foreign_subnets() {
|
||||
return used_subnets(false);
|
||||
};
|
||||
|
||||
/* ---------- checks ---------- */
|
||||
|
||||
/*
|
||||
* Checks are named for the healthy state ("Clock is set"); a failed one also
|
||||
* gets the problem in words for notices ("Clock isn't set"), passed with it.
|
||||
* A server's checks carry its name after both (` (Home)`, with several servers).
|
||||
*/
|
||||
function check(id, title, status, detail, fix, problem) {
|
||||
let bad = status == 'warn' || status == 'error';
|
||||
return { id, title, status, detail: detail ?? '', fix: fix ?? null, problem: bad ? (problem ?? title) : null };
|
||||
}
|
||||
|
||||
/**
|
||||
* Answers from our ports and the proxies that would swallow them (fw.sync_reply_routes):
|
||||
* { ok, diverts: ["pref 1000 fwmark 0x1 → table 100"] }.
|
||||
*/
|
||||
function replies(m) {
|
||||
let d = fw.proxy_diverts();
|
||||
return { ok: fw.reply_ok(m, d), diverts: map(d, fw.divert_str) };
|
||||
}
|
||||
|
||||
function reply_check(id, title, problem, proto, port, r) {
|
||||
let via = join(', ', r.diverts);
|
||||
let from = `${uc(proto)} ${port}`;
|
||||
return check(id, title, r.ok ? 'ok' : 'error',
|
||||
!r.ok ? sprintf(_('Answers from %s are swallowed by a proxy on the router (%s): clients get no reply.'), from, via)
|
||||
: length(r.diverts) ? sprintf(_('Answers from %s go straight out. A proxy on the router diverts its own traffic (%s); these answers skip it.'), from, via)
|
||||
: sprintf(_('Answers from %s go straight out.'), from),
|
||||
r.ok ? null : 'apply', problem);
|
||||
}
|
||||
|
||||
const PRIVATE = map([ '100.64.0.0/10', '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16' ], parse_cidr);
|
||||
|
||||
/** What kind of IPv4 the WAN has: public, cgnat (100.64/10), private (behind another router), or none. */
|
||||
export function wan_kind(wan) {
|
||||
let wc = wan ? ip2int(wan) : null;
|
||||
if (wc == null)
|
||||
return 'none';
|
||||
if (in_cidr(wc, PRIVATE[0]))
|
||||
return 'cgnat';
|
||||
return length(filter(PRIVATE, (c) => in_cidr(wc, c))) ? 'private' : 'public';
|
||||
};
|
||||
|
||||
const HEX = '0123456789abcdef';
|
||||
|
||||
// An IPv6 address as its 32 hex digits, so two spellings of one address
|
||||
// compare equal (2001:DB8::1, 2001:db8:0::1, as typed or as a tool prints
|
||||
// it); null when it is none. No regex: the router's addresses and a name's
|
||||
// AAAA records go through it on every look.
|
||||
function ip6_full(s) {
|
||||
if (type(s) != 'string')
|
||||
return null;
|
||||
let halves = split(lc(s), '::');
|
||||
if (length(halves) > 2)
|
||||
return null;
|
||||
let head = halves[0] != '' ? split(halves[0], ':') : [];
|
||||
let tail = length(halves) == 2 && halves[1] != '' ? split(halves[1], ':') : [];
|
||||
let gap = 8 - length(head) - length(tail);
|
||||
if (length(halves) == 2 ? gap < 1 : gap != 0)
|
||||
return null;
|
||||
let groups = [ ...head ];
|
||||
for (let i = 0; i < gap; i++)
|
||||
push(groups, '0');
|
||||
push(groups, ...tail);
|
||||
let out = '';
|
||||
for (let g in groups) {
|
||||
if (length(g) < 1 || length(g) > 4 || ltrim(g, HEX) != '')
|
||||
return null;
|
||||
out += substr(`0000${g}`, length(g));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The router's public IPv6 addresses in `ip -6 -j addr show scope global`
|
||||
* output (`links`), on any interface: a DDNS client may publish the LAN's
|
||||
* address from the delegated prefix, which reaches the router too. Unique
|
||||
* local addresses (fc00::/7: OpenWrt gives the LAN one even without IPv6
|
||||
* from the ISP) and failed duplicates do not count: nothing reaches them from
|
||||
* outside. [] when there is none.
|
||||
*/
|
||||
export function own_ipv6(links) {
|
||||
let out = [];
|
||||
for (let l in (type(links) == 'array' ? links : []))
|
||||
for (let a in l?.addr_info ?? []) {
|
||||
let k = ip6_full(a?.local);
|
||||
if (k && a.family == 'inet6' && !a.dadfailed && substr(k, 0, 2) != 'fc' && substr(k, 0, 2) != 'fd')
|
||||
push(out, a.local);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
// The router's public IPv6 addresses (own_ipv6), or null when `ip` could not tell.
|
||||
function router_ipv6() {
|
||||
let out = sh_out('ip -6 -j addr show scope global');
|
||||
if (out == null)
|
||||
return null;
|
||||
try {
|
||||
let links = json(out || '[]');
|
||||
return type(links) == 'array' ? own_ipv6(links) : null;
|
||||
}
|
||||
catch (e) {
|
||||
// No JSON (an `ip` without -j): unknown, so the AAAA records get no verdict.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// What the facts are made from, read on the router.
|
||||
function endpoint_reads(srv, host) {
|
||||
let ips = host ? lookup.resolve(host) : [];
|
||||
// The router's IPv6 only when the name has some to compare with.
|
||||
return {
|
||||
ips, wan: srv ? (net.wan_address(srv) ?? lookup.wan_ipv4(srv.wan_zone)) : lookup.wan_ipv4('wan'),
|
||||
router6: length(filter(ips, (a) => ip2int(a) == null)) ? router_ipv6() : null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* What a public address points at, for Health and the "as you type" check on
|
||||
* the server page: { host, ips, ips4, ips6, wan, kind (wan_kind), cloudflare,
|
||||
* here (an IPv4 of it is this router's WAN), v6_only, router6 (the router's
|
||||
* public IPv6 addresses, own_ipv6; null when unknown, or not read because the
|
||||
* name has no IPv6), foreign6 (its IPv6 addresses that are none of those, all
|
||||
* of them when the router has none; null while router6 is), here6 (it has
|
||||
* IPv6 addresses and each is one of the router's) }. `reads` (a test's)
|
||||
* stands for what is read on the router: { ips, wan, router6 }.
|
||||
*/
|
||||
export function endpoint_facts(srv, host, reads) {
|
||||
let r = reads ?? endpoint_reads(srv, host);
|
||||
let ips = r.ips ?? [], wan = r.wan ?? null;
|
||||
let ips4 = filter(ips, (a) => ip2int(a) != null), ips6 = filter(ips, (a) => ip2int(a) == null);
|
||||
let own = r.router6 != null ? map(r.router6, ip6_full) : null;
|
||||
let foreign6 = own != null ? filter(ips6, (a) => index(own, ip6_full(a)) < 0) : null;
|
||||
return {
|
||||
host, ips, ips4, ips6, wan, kind: wan_kind(wan),
|
||||
cloudflare: length(filter(ips, (a) => cloudflare_ip(a))) > 0,
|
||||
here: wan != null && index(ips4, wan) >= 0,
|
||||
v6_only: !length(ips4) && length(ips6) > 0,
|
||||
router6: r.router6 ?? null, foreign6,
|
||||
here6: foreign6 != null && length(ips6) > 0 && !length(foreign6),
|
||||
};
|
||||
};
|
||||
|
||||
/*
|
||||
* The name's IPv6 addresses that do not reach this router from outside, in
|
||||
* words, or null (each is a public one of the router, or those are unknown).
|
||||
* A name with IPv4 too: devices that prefer IPv6 take these and fail, the
|
||||
* others connect.
|
||||
*/
|
||||
function aaaa_words(f) {
|
||||
let bad = f.foreign6 ?? [], n = length(bad);
|
||||
if (!n)
|
||||
return null;
|
||||
if (!length(f.router6))
|
||||
return sprintf(N_(n, '%s AAAA → %s, but this router has no public IPv6 address: devices that prefer IPv6 will fail to connect. Remove this AAAA record or set up IPv6 on the router.',
|
||||
'%s AAAA → %s, but this router has no public IPv6 address: devices that prefer IPv6 will fail to connect. Remove these AAAA records or set up IPv6 on the router.'),
|
||||
f.host, join(', ', bad));
|
||||
return sprintf(N_(n, '%s AAAA → %s is not a public address of this router: devices that prefer IPv6 will fail to connect. Fix or remove this AAAA record.',
|
||||
'%s AAAA → %s are not public addresses of this router: devices that prefer IPv6 will fail to connect. Fix or remove these AAAA records.'),
|
||||
f.host, join(', ', bad));
|
||||
}
|
||||
|
||||
// An IPv6-only name none of whose addresses is a public one of the router: no device connects. Null otherwise.
|
||||
function v6_only_unreached(f) {
|
||||
let n = length(f.foreign6 ?? []);
|
||||
if (!n || n < length(f.ips6))
|
||||
return null;
|
||||
if (!length(f.router6))
|
||||
return sprintf(_('%s → %s: IPv6 only, but this router has no public IPv6 address: no device can connect. Check your DNS / DDNS, or set up IPv6 on the router.'),
|
||||
f.host, join(', ', f.ips6));
|
||||
return sprintf(N_(n, '%s → %s: IPv6 only, and not a public address of this router: no device can connect. Check your DNS / DDNS.',
|
||||
'%s → %s: IPv6 only, and not public addresses of this router: no device can connect. Check your DNS / DDNS.'),
|
||||
f.host, join(', ', f.ips6));
|
||||
}
|
||||
|
||||
/**
|
||||
* The facts in words, in the language of the call: { status: ok|warn|error, text }
|
||||
* (text a whole sentence). `proto_note` names what must get through ("UDP 4500").
|
||||
*/
|
||||
export function endpoint_words(f, proto_note) {
|
||||
if (!f.host)
|
||||
return { status: 'error', text: _('No public address: devices will not know where to connect.') };
|
||||
if (!length(f.ips))
|
||||
return { status: 'error', text: sprintf(_('%s does not resolve.'), f.host) };
|
||||
if (f.cloudflare)
|
||||
return { status: 'error', text: sprintf(_("%s → %s (Cloudflare). %s reaches the router only if this name is DNS-only there, or use the router's IP."),
|
||||
f.host, join(', ', f.ips), proto_note) };
|
||||
let v6 = aaaa_words(f);
|
||||
if (f.v6_only) {
|
||||
let none = v6_only_unreached(f);
|
||||
if (none != null || v6 != null)
|
||||
return { status: 'warn', text: none ?? v6 };
|
||||
return { status: f.kind == 'public' ? 'warn' : 'ok', text: f.kind == 'public'
|
||||
? sprintf(_("%s → %s: IPv6 only, so only devices with IPv6 connect (the router's IPv4 %s has no name)."), f.host, join(', ', f.ips6), f.wan)
|
||||
: sprintf(_('%s → %s: IPv6 only, so only devices with IPv6 connect.'), f.host, join(', ', f.ips6)) };
|
||||
}
|
||||
let v4 = null;
|
||||
if (f.kind == 'cgnat' || f.kind == 'private')
|
||||
v4 = f.kind == 'cgnat'
|
||||
? sprintf(_('The WAN address %s is CGNAT (shared by the ISP): %s cannot be forwarded to this router from outside unless the ISP gives a public address or IPv6.'), f.wan, proto_note)
|
||||
: sprintf(_('The WAN address %s is private: this router is behind another one. Forward %s on it, or devices cannot connect.'), f.wan, proto_note);
|
||||
else if (f.wan && !f.here)
|
||||
v4 = sprintf(_('%s points at %s, not this router (WAN %s). Check your DNS / DDNS.'), f.host, join(', ', f.ips4), f.wan);
|
||||
// Two separate facts, each a sentence of its own: the IPv4 one first.
|
||||
if (v4 != null || v6 != null)
|
||||
return { status: 'warn', text: join(' ', filter([ v4, v6 ], (x) => x != null)) };
|
||||
// Without the router's WAN address only the facts: names and addresses, no words.
|
||||
return { status: 'ok', text: f.here ? sprintf(_('%s points at this router (%s).'), f.host, join(', ', [ f.wan, ...(f.here6 ? f.ips6 : []) ]))
|
||||
: `${f.host} → ${join(', ', f.ips)}` };
|
||||
};
|
||||
|
||||
/** What a public address resolves to and whether that reaches this router (for a core's protocol). */
|
||||
function endpoint_check(id, title, problem, srv, host, proto_note) {
|
||||
let w = endpoint_words(endpoint_facts(srv, host), proto_note);
|
||||
return check(id, title, w.status, w.text, null, problem);
|
||||
}
|
||||
|
||||
// "1000: from all fwmark 0x1 lookup 100". A module constant, compiled once, not for each rule.
|
||||
const MARK_RULE = /^(\d+):.*fwmark 0x([0-9a-f]+)(\/0x[0-9a-f]+)?.* lookup (\S+)/;
|
||||
|
||||
/**
|
||||
* Policy routing by another package (mwan3, pbr): an fwmark rule ahead of
|
||||
* Dawnline's (pref < 5205) whose table has a default route of its own sends
|
||||
* marked tunnel traffic straight out, past "like a home device". Reported,
|
||||
* since those marks belong to that package. [] when none.
|
||||
*/
|
||||
function policy_routing() {
|
||||
let out = [];
|
||||
for (let line in split(sh_out('ip -4 rule show 2>/dev/null') ?? '', '\n')) {
|
||||
let mm = match(line, MARK_RULE);
|
||||
if (!mm || +mm[1] >= 5205)
|
||||
continue;
|
||||
let routes = sh_out(`ip -4 route show table ${q(mm[4])} 2>/dev/null`) ?? '';
|
||||
let def = match(routes, /(^|\n)default [^\n]*/)?.[0] ?? '';
|
||||
if (def == '' || match(def, /local default/) || index(def, `dev ${net.INTERNAL.transit}`) >= 0)
|
||||
continue;
|
||||
push(out, `pref ${mm[1]} fwmark 0x${mm[2]} → table ${mm[4]}`);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function no_proto() {
|
||||
return _("netifd doesn't know the amneziawg protocol (AmneziaWG missing, or the network wasn't restarted after installing it).");
|
||||
}
|
||||
|
||||
/** Free space on the overlay in KB (null when unknown). */
|
||||
function overlay_free_kb() {
|
||||
let out = sh('df -Pk /overlay 2>/dev/null || df -Pk / 2>/dev/null').out;
|
||||
let m = match(out ?? '', /\n\S+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s/);
|
||||
return m ? +m[1] : null;
|
||||
}
|
||||
|
||||
/** Is AmneziaWG there and usable: packages, kernel module, netifd's protocol handler. */
|
||||
function awg_check() {
|
||||
let st = awg.load();
|
||||
let title = _('AmneziaWG installed');
|
||||
let missing = st.missing ?? [];
|
||||
if (length(missing)) {
|
||||
// The module was built for the kernel of the firmware it was installed on.
|
||||
let kc = builds.kernel_changed();
|
||||
if (kc && index(missing, 'kmod-amneziawg') >= 0)
|
||||
return check('awg', title, 'error',
|
||||
sprintf(_('The firmware changed (kernel %s → %s): the AmneziaWG build installed for it is gone. Install the build for this firmware.'), kc.was, kc.now),
|
||||
'install:awg', _('AmneziaWG needs a build for the new firmware'));
|
||||
return check('awg', title, 'error', sprintf(N_(length(missing), '%s is missing.', '%s are missing.'), join(', ', missing)), 'install:awg',
|
||||
_('AmneziaWG isn’t installed'));
|
||||
}
|
||||
if (!st.loaded) {
|
||||
// load() tried it: the second modprobe only repeats why.
|
||||
let why = trim(sh('modprobe amneziawg 2>&1; dmesg | grep -i amneziawg | tail -n 3').out);
|
||||
return check('awg', title, 'error',
|
||||
why ? sprintf(_("The kernel module doesn't load: %s Reinstall a build for this kernel in Components."), why)
|
||||
: _("The kernel module doesn't load. Reinstall a build for this kernel in Components."),
|
||||
'updates', _('AmneziaWG’s kernel module doesn’t load'));
|
||||
}
|
||||
if (!st.handler)
|
||||
return check('awg', title, 'error', no_proto(), 'netrestart', _('The network doesn’t know AmneziaWG yet'));
|
||||
// The install could not swap the loaded module: the old one runs until the next boot (dawnline.awg_builds).
|
||||
if (builds.waits_for_reboot())
|
||||
return check('awg', title, 'warn', _('The AmneziaWG update is installed and starts after a reboot.'), null,
|
||||
_('The AmneziaWG update waits for a reboot'));
|
||||
return check('awg', title, 'ok', _('Kernel module, awg tool and the netifd protocol are present.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The /24s home routers come with out of the box. A home network on one of
|
||||
* them is very likely the same as the network a person connects from, and
|
||||
* home access then takes those addresses into the tunnel: their own printer or
|
||||
* TV is out of reach while connected, LAN Bypass or not.
|
||||
*/
|
||||
const COMMON_HOME = [
|
||||
'192.168.0.0/24', '192.168.1.0/24', '192.168.2.0/24', '192.168.3.0/24', '192.168.8.0/24', '192.168.10.0/24',
|
||||
'192.168.31.0/24', '192.168.50.0/24', '192.168.88.0/24', '192.168.100.0/24', '192.168.178.0/24',
|
||||
'10.0.0.0/24', '10.1.1.0/24',
|
||||
];
|
||||
|
||||
/** The home networks in `nets` that overlap a common out-of-the-box subnet. */
|
||||
export function common_home(nets) {
|
||||
return filter(nets ?? [], (n) => {
|
||||
let a = parse_cidr(n);
|
||||
return a && length(filter(COMMON_HOME, (c) => cidr_overlaps(a, parse_cidr(c))));
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether the server's home networks are on a common subnet, while anyone on
|
||||
* it has home access (otherwise nothing of home goes into the tunnel). A
|
||||
* warning on Health only: advice, not a fault, so no notice elsewhere.
|
||||
*/
|
||||
function home_check(m, id, srv, label) {
|
||||
let reach = filter(values(m.people), (p) => p.server == id && (p.access == 'lan' || p.access == 'both'));
|
||||
let nets = net.server_net(srv).nets;
|
||||
if (!length(reach) || !length(nets))
|
||||
return null;
|
||||
let bad = common_home(nets);
|
||||
let c = check(`homenet_${id}`, _('Home network addresses') + label, length(bad) ? 'warn' : 'ok',
|
||||
length(bad)
|
||||
? sprintf(_('%s is one of the most common home subnets. On a network with the same addresses, people with home access reach your home instead of their own: their printer or TV there is out of reach while connected, with LAN Bypass too. A rarer subnet for your home network avoids it, for example 10.37.12.0/24.'), join(', ', bad))
|
||||
: sprintf(_('%s is not a common home subnet: the networks people connect from rarely use the same addresses.'), join(', ', nets)),
|
||||
null);
|
||||
c.problem = null;
|
||||
return c;
|
||||
}
|
||||
|
||||
/** Checks of a server's AmneziaWG tunnel. */
|
||||
function awg_checks(m, id, srv, label, fwc, dmp, rep) {
|
||||
let out = [];
|
||||
let st = live.iface_status(srv.ifname);
|
||||
let up = !!st?.up;
|
||||
let codes = map(st?.errors ?? [], (e) => e.code);
|
||||
let running = _('AmneziaWG running') + label, down = _('AmneziaWG is down') + label;
|
||||
if (!up && index(codes, 'INVALID_PROTO') >= 0)
|
||||
push(out, check(`up_${id}`, running, 'error', no_proto(), 'netrestart', down));
|
||||
else
|
||||
push(out, check(`up_${id}`, running, up ? 'ok' : 'error',
|
||||
up ? sprintf(_('%s is up, listening on UDP %d.'), srv.ifname, srv.port) :
|
||||
!st ? sprintf(_('%s does not exist.'), srv.ifname) :
|
||||
length(codes) ? sprintf(_('%s is down: %s.'), srv.ifname, join(', ', codes)) : sprintf(_('%s is down.'), srv.ifname),
|
||||
up ? null : (st ? 'ifup:' + srv.ifname : 'apply'), down));
|
||||
// The devices the sync loads, by the same choice the agent compares with every minute.
|
||||
if (up && dmp.reads[srv.ifname] == 'error')
|
||||
push(out, check(`peers_${id}`, _('AmneziaWG devices loaded') + label, 'warn',
|
||||
_('The tunnel could not be read, so its devices could not be checked.'), null,
|
||||
_('The devices in AmneziaWG could not be checked') + label));
|
||||
else if (up) {
|
||||
let devs = awg.tunnel_devices(m, id, apply.states_for(m, core.load_usage()));
|
||||
let diff = awg.peer_diff(devs, dmp, srv.ifname);
|
||||
let expected = length(devs), missing = diff.missing, extra = diff.extra, actual = diff.loaded;
|
||||
let same = !missing && !extra;
|
||||
push(out, check(`peers_${id}`, _('AmneziaWG devices loaded') + label, same ? 'ok' : 'warn',
|
||||
same ? sprintf(N_(actual, '%d device is loaded in the tunnel.', '%d devices are loaded in the tunnel.'), actual) :
|
||||
extra ? sprintf(N_(expected, '%d of %d device missing, %d loaded that should not be.', '%d of %d devices missing, %d loaded that should not be.'),
|
||||
missing, expected, extra)
|
||||
: sprintf(N_(expected, '%d of %d device missing.', '%d of %d devices missing.'), missing, expected),
|
||||
same ? null : 'sync', _('Not every device is loaded into AmneziaWG') + label));
|
||||
}
|
||||
let rule = fwc.get('firewall', `dawnline_${id}_port`) == 'rule';
|
||||
let zone = fwc.get('firewall', `dawnline_${id}_zone`) == 'zone';
|
||||
push(out, check(`fw_${id}`, _('AmneziaWG firewall') + label, rule && zone ? 'ok' : 'error',
|
||||
rule && zone ? sprintf(_('Zone %s and the WAN rule for UDP %d exist.'), srv.ifname, srv.port) : _('Firewall zone or WAN rule is missing.'),
|
||||
rule && zone ? null : 'apply', _('AmneziaWG firewall rules are missing') + label));
|
||||
push(out, reply_check(`reply_${id}`, _('AmneziaWG answers reach devices') + label, _('AmneziaWG answers may not reach devices') + label,
|
||||
'udp', srv.port, rep));
|
||||
// Port collisions with other tunnels.
|
||||
let clash = [];
|
||||
let nc = cursor();
|
||||
nc.load('network');
|
||||
nc.foreach('network', 'interface', (s) => {
|
||||
if (s['.name'] != srv.ifname && (s.proto == 'amneziawg' || s.proto == 'wireguard') && +s.listen_port == srv.port)
|
||||
push(clash, s['.name']);
|
||||
});
|
||||
if (length(clash))
|
||||
push(out, check(`port_${id}`, _('AmneziaWG port is free') + label, 'error', sprintf(_('UDP %d is also used by %s.'), srv.port, join(', ', clash)),
|
||||
null, _('AmneziaWG port is taken') + label));
|
||||
let overlaps = [];
|
||||
let sc = parse_cidr(srv.subnet);
|
||||
for (let o in other_subnets(id)) {
|
||||
let oc = parse_cidr(o.cidr);
|
||||
if (sc && oc && cidr_overlaps(sc, oc))
|
||||
push(overlaps, `${o.iface} (${o.cidr})`);
|
||||
}
|
||||
push(out, check(`subnet_${id}`, _('AmneziaWG subnet is unique') + label, length(overlaps) ? 'error' : 'ok',
|
||||
length(overlaps) ? sprintf(_('%s overlaps %s.'), srv.subnet, join(', ', overlaps)) : sprintf(_('%s does not overlap other networks.'), srv.subnet),
|
||||
null, _('AmneziaWG subnet overlaps another network') + label));
|
||||
let home = home_check(m, id, srv, label);
|
||||
if (home)
|
||||
push(out, home);
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The portal's checks: listening, its address, answers, plain HTTP and the certificate. */
|
||||
function portal_checks(m, need, rep) {
|
||||
let out = [];
|
||||
let s = m.settings;
|
||||
let portal = _('Personal portal'), portal_bad = _('Personal portal isn’t working');
|
||||
if (length(need.https))
|
||||
return [ check('portal', portal, 'error', sprintf(_("HTTPS isn't available on this router: install %s."), join(', ', need.https)), 'install:https', portal_bad) ];
|
||||
let owners = tcp_port_owners(s.portal_port);
|
||||
let listening = tcp_listening(s.portal_port);
|
||||
if (length(owners))
|
||||
push(out, check('portal', portal, 'error',
|
||||
sprintf(_('TCP %d is already used by %s. Choose another port in Settings.'), s.portal_port, join(', ', owners)), null, portal_bad));
|
||||
else
|
||||
push(out, check('portal', portal, listening ? 'ok' : 'error',
|
||||
listening ? sprintf(_('Listening on TCP %d.'), s.portal_port) : sprintf(_('Nothing listens on TCP %d.'), s.portal_port),
|
||||
listening ? null : 'restart', portal_bad));
|
||||
if (!s.portal_host)
|
||||
push(out, check('portal_host', _('Portal address'), 'error', _('Set the public hostname in Settings so links can be generated.'),
|
||||
null, _('Portal address isn’t set')));
|
||||
else {
|
||||
let ips = lookup.resolve(s.portal_host), wan = lookup.router_wan(m);
|
||||
let cf = filter(ips, (x) => cloudflare_ip(x));
|
||||
let h = s.portal_host;
|
||||
push(out, check('portal_dns', _('Portal address points here'), !length(ips) ? 'error' : length(cf) ? 'ok' : wan && index(ips, wan) < 0 ? 'warn' : 'ok',
|
||||
!length(ips) ? sprintf(_('%s does not resolve.'), h) :
|
||||
length(cf) ? sprintf(_('%s goes through Cloudflare; in Russia such connections often stall after ~16 KB.'), h) :
|
||||
wan && index(ips, wan) < 0 ? sprintf(_('%s points at %s, not this router (%s).'), h, join(', ', ips), wan) :
|
||||
wan ? sprintf(_('%s → %s (this router).'), h, join(', ', ips)) : `${h} → ${join(', ', ips)}.`,
|
||||
null, _('Portal address doesn’t point here')));
|
||||
}
|
||||
push(out, reply_check('portal_reply', _('Portal answers reach visitors'), _('Portal answers may not reach visitors'), 'tcp', s.portal_port, rep));
|
||||
if (core.portal_http(m)) {
|
||||
let ipl = filter(core.portal_hosts(m), (h) => ip2int(h) != null);
|
||||
push(out, check('portal_http', _('Plain HTTP links'), 'warn',
|
||||
sprintf(_('Links on %s use plain HTTP: the link and the VPN keys in configurations travel unencrypted.'), join(', ', ipl)),
|
||||
null, _('Portal links use plain HTTP')));
|
||||
}
|
||||
if (ip2int(s.portal_host) != null) {
|
||||
push(out, check('cert', _('Portal certificate'), 'skip', _('The main address is an IP: its links use plain HTTP.')));
|
||||
return out;
|
||||
}
|
||||
let cert = pc.portal_cert(s);
|
||||
let ok = system(`openssl x509 -checkend 1209600 -noout -in ${q(cert.cert)} >/dev/null 2>&1`) == 0;
|
||||
let sans = cert.self_signed ? [] : pc.cert_names(cert.cert);
|
||||
let covers = !s.portal_host || cert.self_signed || length(filter(sans, (n) => pc.name_covers(n, s.portal_host))) > 0;
|
||||
push(out, check('cert', _('Portal certificate'), cert.self_signed || !ok || !covers ? 'warn' : 'ok',
|
||||
cert.self_signed ? (length(need.acme) ? _('Using a self-signed certificate: browsers will show a warning. ACME can get a free Let’s Encrypt one.')
|
||||
: _('Using a self-signed certificate: browsers will show a warning.')) :
|
||||
!covers ? sprintf(_('%s does not cover %s (it has %s): browsers will warn.'), cert.cert, s.portal_host, join(', ', sans)) :
|
||||
ok ? sprintf(_('%s covers %s and is valid for more than 14 days.'), cert.cert, s.portal_host) : sprintf(_('%s expires within 14 days.'), cert.cert),
|
||||
null, _('Portal certificate needs attention')));
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run all checks. Each result: { id, title, status: ok|warn|error|skip, detail, fix,
|
||||
* problem (a failed check in words, else null) }, the texts in the language of the call.
|
||||
* `fix` names an action accepted by fix(), or one the UI handles itself
|
||||
* (install:<feature>, updates).
|
||||
*/
|
||||
export function health(m) {
|
||||
m ??= core.load();
|
||||
let rep = replies(m);
|
||||
let out = [];
|
||||
let dmp = live.dump(m);
|
||||
let need = needs();
|
||||
|
||||
if (length(m.servers))
|
||||
push(out, awg_check());
|
||||
let y = localtime().year;
|
||||
push(out, check('clock', _('Clock is set'), y >= 2025 ? 'ok' : 'error',
|
||||
y >= 2025 ? _('End dates and quotas use the router clock.') : _('The router clock looks wrong; end dates will misbehave. Check NTP.'),
|
||||
null, _('Clock isn’t set')));
|
||||
|
||||
let agent = service_running('dawnline', 'agent');
|
||||
push(out, check('agent', _('Background agent running'), agent ? 'ok' : 'error',
|
||||
_('Counts traffic and enforces end dates and quotas.'), agent ? null : 'restart', _('Background agent isn’t running')));
|
||||
|
||||
// A full overlay fails every config write on the router, Dawnline's and
|
||||
// OpenWrt's own; backups and history are what Dawnline adds to it.
|
||||
let free = overlay_free_kb();
|
||||
if (free != null)
|
||||
push(out, check('flash', _('Free space on the router'), free < 512 ? 'error' : free < 2048 ? 'warn' : 'ok',
|
||||
free < 2048 ? sprintf(_('%s MB free: settings may stop saving. Remove old backups (Settings → Backups) or shorten the history.'), int(free / 1024 * 10) / 10)
|
||||
: sprintf(_('%d MB free on the overlay.'), int(free / 1024)),
|
||||
null, _('The router is running out of flash')));
|
||||
|
||||
if (!length(m.servers)) {
|
||||
push(out, check('server', _('Server configured'), 'warn', _('Run the setup wizard on the Server tab.'), null, _('No server yet')));
|
||||
return out;
|
||||
}
|
||||
|
||||
let nft_ok = system('nft list table inet dawnline >/dev/null 2>&1') == 0;
|
||||
push(out, check('nft', _('Access rules loaded'), nft_ok ? 'ok' : 'error',
|
||||
nft_ok ? _('Table inet dawnline enforces who may reach LAN and the internet.')
|
||||
: _('Table inet dawnline is missing: no device is loaded into the tunnels until it is back.'),
|
||||
nft_ok ? null : 'apply', _('Access rules aren’t loaded')));
|
||||
|
||||
if (length(filter(values(m.servers), (s) => s.lan_like && s.enabled))) {
|
||||
let pr = policy_routing();
|
||||
push(out, check('policy', _('Internet like a home device not bypassed'), length(pr) ? 'warn' : 'ok',
|
||||
length(pr) ? sprintf(_("Policy routing on the router (%s) sends marked traffic straight out, past the home-device path: tunnel traffic it matches skips your LAN's proxies and filters."), join('; ', pr))
|
||||
: _('No other policy routing ahead of the tunnel rules.'),
|
||||
null, _('Other policy routing bypasses the home-device path')));
|
||||
}
|
||||
|
||||
// A device the sync has to leave out (a merged import, a hand-edited config).
|
||||
// Its sentences (core.model_errors), how many more, then what it means.
|
||||
let data = core.model_errors(m);
|
||||
push(out, check('data', _('People and devices consistent'), length(data) ? 'error' : 'ok',
|
||||
length(data) ? join(' ', filter([ join(' ', slice(data, 0, 4)), length(data) > 4 ? sprintf(_('(%d more)'), length(data) - 4) : null,
|
||||
_('Such devices are left out of the tunnels: fix or remove them.') ], (x) => x))
|
||||
: _('Every device has a key and an address of its own inside its server.'),
|
||||
null, _('People and devices are inconsistent')));
|
||||
|
||||
let fwc = cursor();
|
||||
fwc.load('firewall');
|
||||
|
||||
for (let id, srv in m.servers) {
|
||||
let label = length(m.servers) > 1 ? ` (${srv.name})` : '';
|
||||
if (!srv.enabled) {
|
||||
push(out, check(`srv_${id}`, _('Server') + label, 'skip', _('Server is disabled.')));
|
||||
continue;
|
||||
}
|
||||
push(out, ...awg_checks(m, id, srv, label, fwc, dmp, rep));
|
||||
push(out, endpoint_check(`ep_${id}`, _('Public address') + label, _('Public address needs attention') + label,
|
||||
srv, core.endpoint_host(m, srv), `UDP ${srv.port}`));
|
||||
|
||||
if (srv.lan_like) {
|
||||
let ls = loop.status();
|
||||
let nh = need.home;
|
||||
push(out, check(`home_${id}`, _('Internet like a home device') + label, ls.active && ls.bridged ? 'ok' : 'warn',
|
||||
ls.active && ls.bridged ? sprintf(_('Traffic enters %s as %s, so it is handled like any device on your LAN.'), ls.dev, ls.address) :
|
||||
length(nh) ? sprintf(_('Needs %s. Internet traffic leaves through WAN directly until then.'), join(', ', nh)) :
|
||||
ls.active ? sprintf(_('The link is up but not attached to %s.'), ls.dev) :
|
||||
// loop.status()'s own sentence, then what it means.
|
||||
`${ls.error ?? _('Not set up.')} ${_('Internet traffic leaves through WAN directly.')}`,
|
||||
ls.active && ls.bridged ? null : length(nh) ? 'install:home' : 'apply', _('Internet like a home device isn’t working') + label));
|
||||
}
|
||||
}
|
||||
|
||||
// While every server is off the resolver only idles on loopback: nothing to check.
|
||||
if (length(filter(values(m.servers), (x) => x.enabled && x.private_key))) {
|
||||
let rt = loop.resolver_target();
|
||||
let conf = stat(loop.RESOLVER.conf);
|
||||
let running = conf != null && loop.resolver_running();
|
||||
// A new config (a server turned on, the home link up) restarts it: give it a few seconds.
|
||||
let restarting = !running && conf != null && time() - conf.mtime < 10;
|
||||
let ok = running || restarting;
|
||||
push(out, check('resolver', _('DNS for Home-only devices'), ok ? 'ok' : 'warn',
|
||||
restarting ? _('Restarting with the new settings.') :
|
||||
ok ? (rt.mode == 'ns' ? sprintf(_('Plain resolver running next to the LAN (%s).'), rt.address) : sprintf(_('Plain resolver on port %d.'), rt.port))
|
||||
: _('The resolver is not running; Home-only devices cannot resolve names.'),
|
||||
ok ? null : 'apply', _('DNS for Home-only devices isn’t working')));
|
||||
}
|
||||
|
||||
if (m.settings.portal_enabled)
|
||||
push(out, ...portal_checks(m, need, rep));
|
||||
return out;
|
||||
};
|
||||
|
||||
/** Run a fix named by a check. */
|
||||
export function fix(id) {
|
||||
if (id == 'apply')
|
||||
return apply.apply();
|
||||
if (id == 'sync')
|
||||
return apply.sync();
|
||||
if (id == 'restart')
|
||||
return { code: system('/etc/init.d/dawnline restart >/dev/null 2>&1') };
|
||||
// netifd learns protocol handlers (amneziawg) only when it starts. In the
|
||||
// background: the answer goes out before the network blinks.
|
||||
if (id == 'netrestart') {
|
||||
system('( sleep 1; /etc/init.d/network restart ) >/dev/null 2>&1 </dev/null &');
|
||||
return { ok: true, background: true };
|
||||
}
|
||||
let m = match(id ?? '', /^ifup:([a-z][a-z0-9_]{0,11})$/);
|
||||
if (m) {
|
||||
// Only Dawnline's own tunnels: never another interface of the router.
|
||||
let mine = filter(values(core.load().servers), (s) => s.ifname == m[1] && s.private_key);
|
||||
if (!length(mine))
|
||||
die(sprintf(_("%s is not one of Dawnline's tunnels."), m[1]));
|
||||
system(`ifup ${q(m[1])}`);
|
||||
return { ok: true };
|
||||
}
|
||||
die(_('Unknown fix.'));
|
||||
};
|
||||
@@ -1,71 +0,0 @@
|
||||
// Dawnline — the backend's messages in the admin's language: what die()
|
||||
// reports, the health checks, the updater's phases. Text is English in the
|
||||
// code, wrapped in _() and N_() (web/i18n/extract.ts collects them into the
|
||||
// luci-i18n-dawnline-<lang> catalog with the admin UI's strings), with
|
||||
// placeholders filled by sprintf() after the lookup:
|
||||
//
|
||||
// die(sprintf(_('Download failed: %s'), asset));
|
||||
//
|
||||
// Translated through LuCI's own catalog reader (luci.core, as LuCI's pages
|
||||
// are) in the language use() set: the admin page's, sent with every RPC call
|
||||
// (rpcd/ucode/luci.dawnline) and handed to the updater. With no language,
|
||||
// no Dawnline translation installed for it, or no luci-base (the unit tests'
|
||||
// rootfs), the English as written. The portal sets none: its page translates
|
||||
// the messages it gets with its own catalog.
|
||||
|
||||
import { stat } from 'fs';
|
||||
|
||||
const DIR = '/usr/lib/lua/luci/i18n';
|
||||
|
||||
// A C module, loaded at run time: luci-base may be missing, and a static import would not compile then.
|
||||
let lmo = null;
|
||||
try {
|
||||
lmo = require('luci.core');
|
||||
}
|
||||
catch (e) {
|
||||
lmo = null;
|
||||
}
|
||||
|
||||
let active = null;
|
||||
|
||||
/**
|
||||
* Speak `lang` from now on ("ru"; null or "en" for English). Only a language
|
||||
* with a Dawnline catalog: LuCI's others alone would translate a few words.
|
||||
*/
|
||||
export function use(lang) {
|
||||
active = null;
|
||||
if (!lmo || type(lang) != 'string' || lang == 'en' || !match(lang, /^[a-z][a-z][a-z]?([_-][A-Za-z][A-Za-z][A-Za-z]?[A-Za-z]?)?$/))
|
||||
return;
|
||||
lang = lc(replace(lang, '_', '-'));
|
||||
if (!stat(`${DIR}/dawnline.${lang}.lmo`))
|
||||
return;
|
||||
if (lmo.load_catalog(lang, DIR) && lmo.change_catalog(lang))
|
||||
active = lang;
|
||||
};
|
||||
|
||||
/** The language use() set, when its catalog is in use; null for English. */
|
||||
export function current() {
|
||||
return active;
|
||||
};
|
||||
|
||||
/** `msg` in the language use() set (the English when the catalog has no translation). */
|
||||
export function _(msg) {
|
||||
return (active ? lmo.translate(msg) : null) ?? msg;
|
||||
};
|
||||
|
||||
/** `one` or `many` for n, by the language's plural rules (Russian has three forms). */
|
||||
export function N_(n, one, many) {
|
||||
return (active ? lmo.ntranslate(n, one, many) : null) ?? (n == 1 ? one : many);
|
||||
};
|
||||
|
||||
/**
|
||||
* A service log line's words, as written: the log is kept in English (the
|
||||
* agent that writes most of it has no language) and read later in any.
|
||||
* Marking them collects them into the catalog and into the admin page's list
|
||||
* of log templates, which puts each line into the reader's language when it
|
||||
* is shown (web/src/admin/events/logwords.ts): sprintf(L_('updated · %s → %s'), a, b).
|
||||
* Keep a line's English as it was: old lines match it too.
|
||||
*/
|
||||
export function L_(s) {
|
||||
return s;
|
||||
};
|
||||
@@ -1,433 +0,0 @@
|
||||
// Dawnline — how each device's link behaves, from the counters the agent's
|
||||
// watcher reads every second (dawnline.awg.dump): a device that knocks but
|
||||
// never finishes connecting, one whose configuration is in use on two devices,
|
||||
// one whose traffic the router drops, one whose connection keeps breaking.
|
||||
//
|
||||
// The agent keeps one history per device in memory (fresh(), observe(): a few
|
||||
// events of the last HISTORY seconds, not one entry per tick), classifies it
|
||||
// (classify(), pure) and shows a state only once it held HOLD seconds
|
||||
// (settle()). It writes what is shown to dawnline.live.LINK on a change; the
|
||||
// read side (live.dump's `link`, view.device_view) is dawnline.live's, so the
|
||||
// portal never loads this module. Who is online stays live.PRESENCE's.
|
||||
//
|
||||
// The same history tells the set-up sheet, at once and without HOLD, what a
|
||||
// device that is being set up does right now (setup_state(), setup()): it
|
||||
// knocks without finishing, data flows, or it connected but nothing it sends
|
||||
// comes through. The agent writes that to dawnline.live.SETUP on a change.
|
||||
//
|
||||
// What the counters mean (kmod 3.1, receive.c and send.c):
|
||||
// * an accepted handshake initiation adds exactly INITIATION bytes to the
|
||||
// peer's rx: the S1 prefix and any trailer are cut off and the header
|
||||
// protection is undone in place before update_rx_stats(skb->len). The
|
||||
// router answers it; its own handshake time moves only when the first
|
||||
// packet under the new keys arrives.
|
||||
// * a keepalive adds KEEPALIVE plus its content padding; a handshake
|
||||
// response (the router initiated) RESPONSE; data 32 + its padded length.
|
||||
// Plain data is a multiple of 16, so 148, 296 and 444 never are data.
|
||||
// * a data packet the router then drops (its source is not the device's
|
||||
// address: an old configuration after renumbering; this is what moves
|
||||
// the interface's rx_frame_errors) adds nothing to rx, but the router
|
||||
// still answers it with a keepalive within KeepaliveTimeout (about 10 s).
|
||||
|
||||
/** Seconds of events kept per device. */
|
||||
export const HISTORY = 600;
|
||||
/** A state is shown once it held this long (a flapping mobile link passes). */
|
||||
export const HOLD = 180;
|
||||
/** A device is classified at least this often when nothing happened (its spans age). */
|
||||
export const CHECK_EVERY = 15;
|
||||
|
||||
/** Bytes an accepted initiation adds to rx (sizeof(struct message_handshake_initiation)). */
|
||||
export const INITIATION = 148;
|
||||
/** Bytes a handshake response adds to rx (the router initiated). */
|
||||
export const RESPONSE = 92;
|
||||
/** Bytes an empty data packet adds to rx, before content padding. */
|
||||
export const KEEPALIVE = 32;
|
||||
// Random trailers without content padding: a keepalive grows up to a full packet.
|
||||
const TRAILER_MAX = 1500;
|
||||
// Initiations a device sends at most in one second (retries are 5 s apart).
|
||||
const KNOCKS_PER_TICK = 3;
|
||||
|
||||
/** trying: at least TRY_KNOCKS accepted initiations within TRY_SPAN seconds while not online. */
|
||||
export const TRY_SPAN = 120;
|
||||
export const TRY_KNOCKS = 3;
|
||||
/*
|
||||
* Churning: at least CHURN_COUNT handshakes within CHURN_SPAN seconds, with
|
||||
* CHURN_COUNT - 1 of the gaps under CHURN_GAP seconds. A device that is
|
||||
* there renews its keys every 100-120 s (RekeyAfterTime), more only when
|
||||
* its packets get nothing back (KeepaliveTimeout + RekeyTimeout, ~15 s).
|
||||
*/
|
||||
export const CHURN_SPAN = 300;
|
||||
export const CHURN_COUNT = 4;
|
||||
export const CHURN_GAP = 45;
|
||||
/** shared: while churning, the endpoint (ip:port) went back to an earlier one this often. */
|
||||
export const SHARE_RETURNS = 2;
|
||||
/*
|
||||
* dropped: online with a handshake within DROP_HS seconds, nothing but
|
||||
* handshakes and keepalives received for DROP_SPAN seconds, and the router
|
||||
* sent something at least DROP_ANSWERS times in that span with nothing
|
||||
* coming in (its keepalives answering packets it dropped), under DROP_TX
|
||||
* bytes in all (a real download is far more).
|
||||
*/
|
||||
export const DROP_SPAN = 180;
|
||||
export const DROP_HS = 240;
|
||||
export const DROP_ANSWERS = 6;
|
||||
export const DROP_TX = 65536;
|
||||
|
||||
/** Every state classify() can give. */
|
||||
export const STATES = [ 'trying', 'shared', 'dropped', 'unstable' ];
|
||||
|
||||
/*
|
||||
* The set-up sheet's trying: at least SETUP_KNOCKS accepted initiations
|
||||
* within SETUP_SPAN seconds, none of them followed by a handshake. A device
|
||||
* retries every 5 s, so it shows with the second (5-6 s in) and goes about
|
||||
* 15 s after the last.
|
||||
*/
|
||||
export const SETUP_SPAN = 20;
|
||||
export const SETUP_KNOCKS = 2;
|
||||
/*
|
||||
* nodata: online for SETUP_QUIET seconds since the handshake its session
|
||||
* began with, nothing but handshakes and keepalives in since, and the router
|
||||
* answered at least once with nothing coming in (a keepalive for data it
|
||||
* dropped, about 10 s after it: an old address in the configuration). An
|
||||
* idle device gets no answers, so it never shows.
|
||||
*/
|
||||
export const SETUP_QUIET = 15;
|
||||
|
||||
// Per list, the newest entries kept (a device whose endpoint flips every second).
|
||||
const KEEP = 64;
|
||||
|
||||
function keep(list, entry) {
|
||||
push(list, entry);
|
||||
if (length(list) > KEEP)
|
||||
shift(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* A new, empty history for a device first seen at `now`. Fields: rx, tx
|
||||
* (the counters last read, null before the first read), hs (the newest
|
||||
* handshake), ep (the endpoint), data (when rx last carried data), hsev
|
||||
* (handshake times), epev ([ ts, ip:port, the one before ] endpoint
|
||||
* changes), knocks (times
|
||||
* of accepted initiations), ans ([ ts, bytes ] the router sent with nothing
|
||||
* received), state/since (what is shown), pend (a state waiting for HOLD),
|
||||
* raw (the last classification), checked (when it was made); for the set-up
|
||||
* sheet (setup()): on (online at the last read), sess (the handshake its
|
||||
* online stretch began with, 0 while away), setup ({ state, since } or null)
|
||||
* and due (when setup_state() may change with time alone, 0 for never).
|
||||
*/
|
||||
export function fresh(now) {
|
||||
return {
|
||||
start: now, rx: null, tx: null, hs: 0, ep: null, data: 0,
|
||||
hsev: [], epev: [], knocks: [], ans: [],
|
||||
state: null, since: 0, pend: null, raw: null, checked: 0,
|
||||
on: false, sess: 0, setup: null, due: 0,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* The largest rx a keepalive adds for a server's disguise: KEEPALIVE plus
|
||||
* the top of its ContentPaddingAddition, or a full packet with random
|
||||
* trailers and no content padding (the apps get the server's values).
|
||||
*/
|
||||
export function keepalive_max(srv) {
|
||||
let o = srv?.obf ?? {};
|
||||
let cpa = split(`${o.content_padding_addition ?? ''}`, '-');
|
||||
let hi = +(cpa[length(cpa) - 1]);
|
||||
if (o.content_padding_addition != null && o.content_padding_addition != '' && hi > 0)
|
||||
return KEEPALIVE + hi;
|
||||
if (o.random_trailers == 'on' || o.random_trailers == '1')
|
||||
return KEEPALIVE + TRAILER_MAX;
|
||||
return KEEPALIVE;
|
||||
};
|
||||
|
||||
/** keepalive_max() for each device's server: { device id: bytes }. */
|
||||
export function keepalive_sizes(m) {
|
||||
let out = {};
|
||||
for (let id, d in m.devices)
|
||||
out[id] = keepalive_max(m.servers[m.people[d.person]?.server]);
|
||||
return out;
|
||||
};
|
||||
|
||||
/** An rx increment made of accepted initiations only. */
|
||||
export function knock(drx) {
|
||||
return drx > 0 && drx % INITIATION == 0 && drx <= KNOCKS_PER_TICK * INITIATION;
|
||||
};
|
||||
|
||||
/**
|
||||
* An rx increment that carried no data: nothing, or one keepalive of up to
|
||||
* `ka` bytes (keepalive_max); in the second a handshake completed (`shook`)
|
||||
* also the handshake messages that came with it. Outside that second a
|
||||
* handshake message is never counted in: with a padded keepalive their
|
||||
* sums would cover every small packet (a DNS query, an ACK) up to some
|
||||
* 670 bytes, and a device with light traffic would look like one whose
|
||||
* data never comes through.
|
||||
*/
|
||||
export function bare(drx, ka, shook) {
|
||||
if (drx <= 0)
|
||||
return true;
|
||||
if (!shook)
|
||||
return drx >= KEEPALIVE && drx <= ka;
|
||||
if (drx > KNOCKS_PER_TICK * INITIATION + RESPONSE + ka)
|
||||
return false;
|
||||
for (let k = 0; k <= KNOCKS_PER_TICK; k++)
|
||||
for (let r = 0; r <= 1; r++) {
|
||||
let rest = drx - k * INITIATION - r * RESPONSE;
|
||||
if (rest == 0 || (rest >= KEEPALIVE && rest <= ka))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Fold one read of the device's peer ({ endpoint, handshake, rx, tx } from
|
||||
* awg.dump) at `now` into its history `h`; `ka` is its keepalive_max().
|
||||
* Returns true when something worth classifying again happened (a
|
||||
* handshake, a new endpoint, a knock, an answer, data after a quiet span).
|
||||
* Counters that went down (the peer re-added, its tunnel set up again)
|
||||
* start a new baseline.
|
||||
*/
|
||||
export function observe(h, peer, now, ka) {
|
||||
let changed = false;
|
||||
let shook = peer.handshake > h.hs;
|
||||
if (shook) {
|
||||
keep(h.hsev, peer.handshake);
|
||||
h.hs = peer.handshake;
|
||||
changed = true;
|
||||
}
|
||||
if (peer.endpoint != h.ep) {
|
||||
if (h.ep != null && peer.endpoint != '') {
|
||||
keep(h.epev, [ now, peer.endpoint, h.ep ]);
|
||||
changed = true;
|
||||
}
|
||||
h.ep = peer.endpoint;
|
||||
}
|
||||
if (h.rx == null || peer.rx < h.rx || peer.tx < h.tx) {
|
||||
h.rx = peer.rx;
|
||||
h.tx = peer.tx;
|
||||
return changed;
|
||||
}
|
||||
let drx = peer.rx - h.rx, dtx = peer.tx - h.tx;
|
||||
h.rx = peer.rx;
|
||||
h.tx = peer.tx;
|
||||
if (drx > 0) {
|
||||
if (!shook && knock(drx)) {
|
||||
keep(h.knocks, now);
|
||||
changed = true;
|
||||
}
|
||||
else if (!bare(drx, ka, shook)) {
|
||||
if (now - h.data >= DROP_SPAN)
|
||||
changed = true;
|
||||
h.data = now;
|
||||
}
|
||||
}
|
||||
else if (dtx > 0) {
|
||||
keep(h.ans, [ now, dtx ]);
|
||||
changed = true;
|
||||
}
|
||||
return changed;
|
||||
};
|
||||
|
||||
/** The device's peer is not in its tunnel: the next read starts a new baseline. */
|
||||
export function gone(h) {
|
||||
h.rx = null;
|
||||
h.tx = null;
|
||||
};
|
||||
|
||||
/** Forget events older than HISTORY seconds. */
|
||||
export function prune(h, now) {
|
||||
let cut = now - HISTORY;
|
||||
h.hsev = filter(h.hsev, (t) => t > cut);
|
||||
h.knocks = filter(h.knocks, (t) => t > cut);
|
||||
h.epev = filter(h.epev, (e) => e[0] > cut);
|
||||
h.ans = filter(h.ans, (e) => e[0] > cut);
|
||||
};
|
||||
|
||||
/** Endpoint changes within the span that went back to an endpoint seen before in it (A → B → A → B: 2). */
|
||||
function returns(h, now) {
|
||||
let seen = null, n = 0;
|
||||
for (let e in h.epev)
|
||||
if (now - e[0] < CHURN_SPAN) {
|
||||
if (seen == null) {
|
||||
seen = {};
|
||||
seen[e[2]] = true;
|
||||
}
|
||||
if (seen[e[1]])
|
||||
n++;
|
||||
seen[e[1]] = true;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
function churning(h, now) {
|
||||
let hs = filter(h.hsev, (t) => now - t < CHURN_SPAN);
|
||||
if (length(hs) < CHURN_COUNT)
|
||||
return false;
|
||||
let short = 0;
|
||||
for (let i = 1; i < length(hs); i++)
|
||||
if (hs[i] - hs[i - 1] < CHURN_GAP)
|
||||
short++;
|
||||
return short >= CHURN_COUNT - 1;
|
||||
}
|
||||
|
||||
function dropped(h, now) {
|
||||
if (now - h.start < DROP_SPAN || now - h.data < DROP_SPAN || !(h.hs > 0 && now - h.hs < DROP_HS))
|
||||
return false;
|
||||
let n = 0, bytes = 0;
|
||||
for (let a in h.ans)
|
||||
if (now - a[0] < DROP_SPAN) {
|
||||
n++;
|
||||
bytes += a[1];
|
||||
}
|
||||
return n >= DROP_ANSWERS && bytes < DROP_TX;
|
||||
}
|
||||
|
||||
/**
|
||||
* What the history says about the device at `now` (`on`: it is online, as
|
||||
* the watcher decides it): null (nothing wrong, or nothing to tell), or
|
||||
* 'trying' not online, but its initiations reach the router and are
|
||||
* answered, and it never finishes connecting (the network
|
||||
* may block it, or its configuration is out of date: a
|
||||
* stale preshared key looks the same)
|
||||
* 'shared' connecting over and over from two or more alternating
|
||||
* endpoints (ip:port): one configuration on two devices
|
||||
* 'unstable' connecting over and over from one place: the connection
|
||||
* keeps breaking
|
||||
* 'dropped' connected, but nothing it sends gets through: the router
|
||||
* drops it (an old address in its configuration)
|
||||
*/
|
||||
export function classify(h, now, on) {
|
||||
if (!on) {
|
||||
let n = 0;
|
||||
for (let t in h.knocks)
|
||||
if (now - t < TRY_SPAN)
|
||||
n++;
|
||||
return n >= TRY_KNOCKS ? 'trying' : null;
|
||||
}
|
||||
if (churning(h, now))
|
||||
return returns(h, now) >= SHARE_RETURNS ? 'shared' : 'unstable';
|
||||
return dropped(h, now) ? 'dropped' : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Turn a classification into what is shown (h.state, h.since): a state, or
|
||||
* a change of state, shows once classify() gave it for HOLD seconds (since
|
||||
* = when it began); nothing wrong shows at once when the device is online
|
||||
* (it works), else after HOLD as well. Returns the change, { from, to }
|
||||
* (either may be null), or null when what is shown stays.
|
||||
*/
|
||||
export function settle(h, raw, now, on) {
|
||||
if (raw == h.state) {
|
||||
h.pend = null;
|
||||
return null;
|
||||
}
|
||||
let since = now;
|
||||
if (raw != null || !on) {
|
||||
if (h.pend == null || h.pend.state != raw) {
|
||||
h.pend = { state: raw, since: now };
|
||||
return null;
|
||||
}
|
||||
if (now - h.pend.since < HOLD)
|
||||
return null;
|
||||
since = h.pend.since;
|
||||
}
|
||||
let from = h.state;
|
||||
h.state = raw;
|
||||
h.since = raw != null ? since : 0;
|
||||
h.pend = null;
|
||||
return { from, to: raw };
|
||||
};
|
||||
|
||||
/**
|
||||
* What the device's link does at `now` for the set-up sheet (pure; `on`: it
|
||||
* is online, as the watcher decides it): { state, due }. state is
|
||||
* 'trying' its initiations reach the router and are answered, and no
|
||||
* handshake follows (SETUP_KNOCKS within SETUP_SPAN): the
|
||||
* network blocks the answers, or the configuration is not this
|
||||
* device's any more
|
||||
* 'flowing' online, and data came in during this session (since h.sess)
|
||||
* 'nodata' online for SETUP_QUIET seconds, nothing but handshakes and
|
||||
* keepalives came in, and the router answered packets it
|
||||
* dropped: an old address in the configuration
|
||||
* null none of these (waiting, idle, or away)
|
||||
* due: when the state may change with time alone (a knock ages out, the
|
||||
* quiet span ends), 0 for never: anything else comes with a read.
|
||||
*/
|
||||
export function setup_state(h, now, on) {
|
||||
let n = 0;
|
||||
for (let i = length(h.knocks) - 1; i >= 0; i--) {
|
||||
let t = h.knocks[i];
|
||||
if (t <= h.hs || now - t >= SETUP_SPAN)
|
||||
break;
|
||||
if (++n == SETUP_KNOCKS)
|
||||
return { state: 'trying', due: t + SETUP_SPAN };
|
||||
}
|
||||
if (!on || !h.sess)
|
||||
return { state: null, due: 0 };
|
||||
if (h.data >= h.sess)
|
||||
return { state: 'flowing', due: 0 };
|
||||
if (now - h.sess < SETUP_QUIET)
|
||||
return { state: null, due: h.sess + SETUP_QUIET };
|
||||
let a = h.ans[length(h.ans) - 1];
|
||||
return { state: a != null && a[0] >= h.sess ? 'nodata' : null, due: 0 };
|
||||
};
|
||||
|
||||
/**
|
||||
* Bring h.setup to setup_state() after a read at `now` (`moved`: what
|
||||
* observe() returned). Most reads cost a few compares: the state is worked
|
||||
* out again only when something happened, the device came or went, data
|
||||
* came in while it is not 'flowing', or its `due` time came. Returns true
|
||||
* when h.setup changed ({ state, since: when it began } or null).
|
||||
*/
|
||||
export function setup(h, now, on, moved) {
|
||||
// A session begins with the handshake that brought the device online.
|
||||
if (!on)
|
||||
h.sess = 0;
|
||||
else if (!h.sess)
|
||||
h.sess = h.hs;
|
||||
let came = on != h.on;
|
||||
h.on = on;
|
||||
let cur = h.setup?.state;
|
||||
if (!moved && !came && !(h.due && now >= h.due) && !(h.data == now && cur != 'flowing'))
|
||||
return false;
|
||||
let s = setup_state(h, now, on);
|
||||
h.due = s.due;
|
||||
if (s.state == cur)
|
||||
return false;
|
||||
h.setup = s.state != null ? { state: s.state, since: now } : null;
|
||||
return true;
|
||||
};
|
||||
|
||||
/** setup() of every device: { device id: { state, since } }, devices with none left out. */
|
||||
export function setups(hist) {
|
||||
let out = {};
|
||||
for (let id, h in hist)
|
||||
if (h.setup != null)
|
||||
out[id] = h.setup;
|
||||
return out;
|
||||
};
|
||||
|
||||
/** What is shown for every device: { device id: { state, since } }, devices with nothing to show left out. */
|
||||
export function shown(hist) {
|
||||
let out = {};
|
||||
for (let id, h in hist)
|
||||
if (h.state != null)
|
||||
out[id] = { state: h.state, since: h.since };
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* The activity event for a change settle() made, or null: device_failing
|
||||
* (detail: the new state) when a state shows, device_recovered (detail:
|
||||
* the state it had) when it clears while the device is online. One that
|
||||
* clears while the device is away (it stopped trying, it was paused) logs
|
||||
* nothing: nobody saw it work.
|
||||
*/
|
||||
export function event_for(change, dev, on) {
|
||||
if (!change)
|
||||
return null;
|
||||
if (change.to != null)
|
||||
return { type: 'device_failing', person: dev.person, device: dev.id, detail: change.to };
|
||||
if (on && change.from != null)
|
||||
return { type: 'device_recovered', person: dev.person, device: dev.id, detail: change.from };
|
||||
return null;
|
||||
};
|
||||
@@ -1,603 +0,0 @@
|
||||
// Dawnline — live data: peer status from the tunnels (dawnline.awg), traffic
|
||||
// accounting and the connection/event log.
|
||||
//
|
||||
// Counters live in RUN_DIR (tmpfs) and are flushed to DATA_DIR at most once
|
||||
// an hour, so flash is not worn by the 60-second agent loop.
|
||||
|
||||
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, syslog,
|
||||
iface_status as util_iface_status, RUN_DIR, DATA_DIR
|
||||
} from 'dawnline.util';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as schema from 'dawnline.schema';
|
||||
|
||||
// Online while the WireGuard session lives: it is dropped 180 s after its
|
||||
// handshake, and a device that is there renews it every 2 min (a little slack
|
||||
// for a renewal that needs a retry). One that left goes offline 1-3 min later.
|
||||
// The shortest window: a device whose keep-alive is longer gets more
|
||||
// (online_window), or it would flap between its handshakes.
|
||||
export const ONLINE_WINDOW = 190;
|
||||
/** A restore put other counters on flash: the agent reloads from there (dawnline.backup writes it). */
|
||||
export const USAGE_RELOAD = `${RUN_DIR}/usage.reload`;
|
||||
|
||||
/* ---------- retention policy ---------- */
|
||||
|
||||
/** Event type → log category (what Settings lets you switch off or clear). */
|
||||
export const CATEGORY = {
|
||||
connect: 'connections', disconnect: 'connections', roam: 'connections',
|
||||
// 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', expiring: 'people', expired_kept: 'people',
|
||||
server_down: 'problems', server_up: 'problems', portal_blocked: 'problems',
|
||||
// A device's link (dawnline.link): failing in a named way, and working again.
|
||||
device_failing: 'problems', device_recovered: 'problems',
|
||||
};
|
||||
export const CATEGORIES = [ 'connections', 'people', 'problems' ];
|
||||
|
||||
export function category_of(type) {
|
||||
return CATEGORY[type] ?? 'problems';
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Log and history retention from /etc/config/dawnline (read fresh, the
|
||||
* portal and rpcd call add_event without a loaded model).
|
||||
* log_keep_days days of activity to keep, 0 = until log_max
|
||||
* log_max newest N events kept
|
||||
* log_category categories that are recorded
|
||||
* log_persist how often the log is saved to flash: 10min | hourly | off
|
||||
* usage_keep_days / usage_keep_months daily / monthly traffic buckets
|
||||
*/
|
||||
export function policy(s) {
|
||||
if (s == null) {
|
||||
let c = cursor();
|
||||
s = c.get_all('dawnline', 'settings') ?? {};
|
||||
}
|
||||
let cats = s.log_category;
|
||||
if (cats == null)
|
||||
cats = (s.log_connections == '0') ? filter(CATEGORIES, (x) => x != 'connections') : CATEGORIES;
|
||||
else if (type(cats) != 'array')
|
||||
cats = split(cats, /[ ,]+/);
|
||||
cats = uniq(filter(map(cats, (x) => x == 'devices' ? 'people' : x), (x) => index(CATEGORIES, x) >= 0));
|
||||
// Defaults and ranges come from dawnline.schema, like every other setting.
|
||||
return {
|
||||
keep_days: schema.get(s, 'log_keep_days'),
|
||||
max: schema.get(s, 'log_max'),
|
||||
categories: cats,
|
||||
persist: schema.get(s, 'log_persist'),
|
||||
usage_days: schema.get(s, 'usage_keep_days'),
|
||||
usage_months: schema.get(s, 'usage_keep_months'),
|
||||
};
|
||||
};
|
||||
|
||||
/** Drop events older than keep_days and beyond max. */
|
||||
function trim_events(ev, pol) {
|
||||
if (pol.keep_days > 0) {
|
||||
let cut = time() - pol.keep_days * 86400;
|
||||
ev = filter(ev, (e) => (e.ts ?? 0) >= cut);
|
||||
}
|
||||
if (length(ev) > pol.max)
|
||||
ev = slice(ev, length(ev) - pol.max);
|
||||
return ev;
|
||||
}
|
||||
|
||||
/* ---------- live peers ---------- */
|
||||
|
||||
/* ---------- presence (the agent's watcher) ---------- */
|
||||
|
||||
/**
|
||||
* Written by the agent's watcher on every change: { online: { device id: bool },
|
||||
* endpoint: { device id: ip }, rev, people: { person id: rev } }. `rev` moves on
|
||||
* any connect, disconnect, roam or logged change; `people` per person (the portal).
|
||||
*/
|
||||
export const PRESENCE = `${RUN_DIR}/presence.json`;
|
||||
// The agent's heartbeat (dawnline.watchdog.BEAT): its presence counts while it runs.
|
||||
const BEAT = `${RUN_DIR}/agent.beat`;
|
||||
const BEAT_STALE = 180;
|
||||
|
||||
/** The agent runs (its heartbeat is fresh): what its watcher wrote counts. */
|
||||
function agent_running() {
|
||||
let beat = +(read_json(BEAT, 0) ?? 0);
|
||||
return beat > 0 && time() - beat <= BEAT_STALE;
|
||||
}
|
||||
|
||||
/** What the watcher sees, or null when the agent isn't running. */
|
||||
export function presence() {
|
||||
return agent_running() ? read_json(PRESENCE, null) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Written by the agent's watcher when what it shows changes (dawnline.link):
|
||||
* { device id: { state: 'trying' | 'shared' | 'dropped' | 'unstable', since } },
|
||||
* only devices whose link fails in one of these ways (link.classify()).
|
||||
*/
|
||||
export const LINK = `${RUN_DIR}/link.json`;
|
||||
|
||||
/**
|
||||
* Written by the agent's watcher when it changes (dawnline.link.setup, at
|
||||
* once, without LINK's hold): what each device's link does right now, for the
|
||||
* set-up sheet: { device id: { state: 'trying' | 'flowing' | 'nodata', since } },
|
||||
* devices with none of these left out.
|
||||
*/
|
||||
export const SETUP = `${RUN_DIR}/setup.json`;
|
||||
|
||||
/**
|
||||
* The interfaces of the servers that run: enabled, with keys and an
|
||||
* interface that is Dawnline's (core.tunnels: never another tunnel that
|
||||
* took the name); with `sid`, only that server's. What dawnline.awg.dump()
|
||||
* reads; an empty list when none.
|
||||
*/
|
||||
export function tunnel_ifnames(m, sid) {
|
||||
let out = [];
|
||||
for (let id, srv in m.servers)
|
||||
if (srv.enabled && (sid == null || id == sid))
|
||||
for (let t in core.tunnels(srv))
|
||||
push(out, t.ifname);
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* How long after its last handshake a device still counts as online, in
|
||||
* seconds: ONLINE_WINDOW, or 30 s more than an idle device with its
|
||||
* keep-alive goes between handshakes (awg.idle_handshake) when that is
|
||||
* longer. A device that sends nothing renews its session only with its
|
||||
* keep-alive: with one of about 95-120 s, or over 160 s, the fixed window
|
||||
* would see it leave and come back between two handshakes. Keep-alive off:
|
||||
* ONLINE_WINDOW.
|
||||
*/
|
||||
export function online_window(srv, dev) {
|
||||
let h = srv ? awg.idle_handshake(srv, dev) : null;
|
||||
return h != null ? max(ONLINE_WINDOW, h + 30) : ONLINE_WINDOW;
|
||||
};
|
||||
|
||||
/** online_window() of every device (with `sid`, of that server's): { device id: seconds }. */
|
||||
export function online_windows(m, sid) {
|
||||
let out = {};
|
||||
for (let id, d in m.devices) {
|
||||
let s = m.people[d.person]?.server;
|
||||
if (sid == null || s == sid)
|
||||
out[id] = online_window(m.servers[s], d);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/**
|
||||
* Dawnline's tunnels as dawnline.awg.dump() reads them (only those, each on
|
||||
* its own: { ok, reads, ifaces, peers: { public key: { ifname, endpoint,
|
||||
* handshake, rx, tx } } }), plus `presence`, the agent watcher's, `windows`,
|
||||
* each device's online_window() (online() without the agent), `link`, the
|
||||
* watcher's LINK ({} without the agent: its states would be stale), and
|
||||
* `setup`, its SETUP (null without the agent: nobody tells data from a bare
|
||||
* handshake then, view.device_view goes by the handshake). With `sid` only
|
||||
* that server's tunnel is read (one person's page needs no other: one `awg`
|
||||
* process, not one per server).
|
||||
*/
|
||||
export function dump(m, sid) {
|
||||
let d = awg.dump(tunnel_ifnames(m, sid));
|
||||
let up = agent_running();
|
||||
d.presence = up ? read_json(PRESENCE, null) : null;
|
||||
d.windows = online_windows(m, sid);
|
||||
d.link = up ? (read_json(LINK, null) ?? {}) : {};
|
||||
d.setup = up ? (read_json(SETUP, null) ?? {}) : null;
|
||||
return d;
|
||||
};
|
||||
|
||||
/** A device is online: as the agent's watcher says, or (without the agent) by a recent handshake. */
|
||||
export function online(dmp, id, peer) {
|
||||
let p = dmp?.presence?.online;
|
||||
if (p)
|
||||
return !!(peer && p[id]);
|
||||
return !!(peer && peer.handshake > 0 && time() - peer.handshake < (dmp?.windows?.[id] ?? ONLINE_WINDOW));
|
||||
};
|
||||
|
||||
// "203.0.113.7:41821", "[2001:db8::1]:41821". A module constant, compiled once:
|
||||
// the agent's watcher asks for every device, every second.
|
||||
const ENDPOINT = /^\[?([^\]]+)\]?:[0-9]+$/;
|
||||
|
||||
export function endpoint_ip(ep) {
|
||||
let m = match(ep ?? '', ENDPOINT);
|
||||
return m ? m[1] : ep;
|
||||
};
|
||||
|
||||
/* ---------- usage ---------- */
|
||||
|
||||
function empty_usage() {
|
||||
return { devices: {}, people: {}, updated: 0, flushed: 0 };
|
||||
}
|
||||
|
||||
/**
|
||||
* The usage counters: the live copy in RUN_DIR, else what flash holds. With
|
||||
* `from_flash` only flash counts (a restore put new counters there, and the
|
||||
* watcher may have written the old live ones again since).
|
||||
*/
|
||||
export function load_usage(from_flash) {
|
||||
let u = (from_flash ? null : read_json(`${RUN_DIR}/usage.json`, null)) ?? read_json(`${DATA_DIR}/usage.json`, null) ?? empty_usage();
|
||||
// Per-app counters of earlier test builds are not kept.
|
||||
delete u.clients;
|
||||
return u;
|
||||
};
|
||||
|
||||
function add(bucket, key, rx, tx) {
|
||||
let v = bucket[key] ?? [ 0, 0 ];
|
||||
bucket[key] = [ v[0] + rx, v[1] + tx ];
|
||||
}
|
||||
|
||||
function prune(obj, keep) {
|
||||
let ks = sort(keys(obj));
|
||||
for (let i = 0; i < length(ks) - keep; i++)
|
||||
delete obj[ks[i]];
|
||||
}
|
||||
|
||||
function account(rec, rx, tx, ts) {
|
||||
rec.total = [ (rec.total?.[0] ?? 0) + rx, (rec.total?.[1] ?? 0) + tx ];
|
||||
rec.months ??= {};
|
||||
rec.days ??= {};
|
||||
add(rec.months, month_key(ts), rx, tx);
|
||||
add(rec.days, day_key(ts), rx, tx);
|
||||
}
|
||||
|
||||
/** Apply the traffic history retention to every record. */
|
||||
export function prune_usage(usage, pol) {
|
||||
pol ??= policy();
|
||||
for (let grp in [ usage.devices, usage.people ])
|
||||
for (let id, rec in grp) {
|
||||
if (rec.months)
|
||||
prune(rec.months, pol.usage_months);
|
||||
if (rec.days)
|
||||
prune(rec.days, pol.usage_days);
|
||||
}
|
||||
};
|
||||
|
||||
/** Forget a record's traffic history; kernel counters (raw) and the last handshake stay. */
|
||||
function wipe(rec) {
|
||||
if (!rec)
|
||||
return;
|
||||
delete rec.total;
|
||||
delete rec.months;
|
||||
delete rec.days;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset traffic history. scope: all | person | device. A person reset also
|
||||
* clears their devices and restarts their data limit; a device reset leaves
|
||||
* the person's totals (and so their limit) alone.
|
||||
*/
|
||||
export function reset_usage(usage, m, scope, id) {
|
||||
if (scope == 'all') {
|
||||
for (let k, rec in usage.devices)
|
||||
wipe(rec);
|
||||
for (let k, rec in usage.people)
|
||||
wipe(rec);
|
||||
}
|
||||
else if (scope == 'person') {
|
||||
wipe(usage.people[id]);
|
||||
for (let did in m.people[id]?.devices ?? [])
|
||||
wipe(usage.devices[did]);
|
||||
}
|
||||
else if (scope == 'device')
|
||||
wipe(usage.devices[id]);
|
||||
};
|
||||
|
||||
function offline(rec) {
|
||||
return { online: false, handshake: rec?.handshake ?? 0, endpoint: rec?.endpoint ?? '', present: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Fold the current kernel counters into `usage`. Counters restart from zero
|
||||
* when a peer is re-added or its tunnel is set up again; a drop is treated
|
||||
* as a reset, and so is a new link (each record keeps the interface index
|
||||
* its counters were read on: new counters that already passed the old ones
|
||||
* would be counted short). Returns the per-device live view keyed by device id.
|
||||
*
|
||||
* A device missing from the dump had its counters reset only when its tunnel
|
||||
* was read or is down (dawnline.awg.dump's `reads`): one the tool couldn't
|
||||
* read says nothing, and forgetting the last counters there would count them
|
||||
* again on the next good read.
|
||||
*/
|
||||
export function collect(m, usage, d) {
|
||||
d ??= dump(m);
|
||||
let ts = time();
|
||||
let live = {};
|
||||
for (let id, dev in m.devices) {
|
||||
let rec = usage.devices[id] ??= { raw: [ 0, 0 ] };
|
||||
let peer = d.peers[dev.public_key];
|
||||
if (!peer) {
|
||||
let ifname = m.servers[m.people[dev.person]?.server]?.ifname;
|
||||
if (d.reads[ifname] != 'error')
|
||||
rec.raw = [ 0, 0 ];
|
||||
live[id] = offline(rec);
|
||||
continue;
|
||||
}
|
||||
let raw = rec.raw ?? [ 0, 0 ];
|
||||
let link = d.ifaces[peer.ifname]?.ifindex;
|
||||
if (link != null && rec.ifindex != null && link != rec.ifindex)
|
||||
raw = [ 0, 0 ];
|
||||
if (link != null)
|
||||
rec.ifindex = link;
|
||||
let drx = peer.rx >= raw[0] ? peer.rx - raw[0] : peer.rx;
|
||||
let dtx = peer.tx >= raw[1] ? peer.tx - raw[1] : peer.tx;
|
||||
rec.raw = [ peer.rx, peer.tx ];
|
||||
if (drx > 0 || dtx > 0) {
|
||||
account(rec, drx, dtx, ts);
|
||||
let p = usage.people[dev.person] ??= {};
|
||||
account(p, drx, dtx, ts);
|
||||
}
|
||||
if (peer.handshake > 0) {
|
||||
rec.handshake = peer.handshake;
|
||||
rec.endpoint = endpoint_ip(peer.endpoint);
|
||||
// Recent client addresses (for the map): ip → last seen, newest 8.
|
||||
rec.ips ??= {};
|
||||
if (rec.endpoint)
|
||||
rec.ips[rec.endpoint] = peer.handshake;
|
||||
let ips = sort(keys(rec.ips), (x, y) => rec.ips[y] - rec.ips[x]);
|
||||
for (let i = 8; i < length(ips); i++)
|
||||
delete rec.ips[ips[i]];
|
||||
}
|
||||
live[id] = {
|
||||
online: peer.handshake > 0 && ts - peer.handshake < online_window(m.servers[m.people[dev.person]?.server], dev),
|
||||
handshake: peer.handshake || rec.handshake || 0,
|
||||
endpoint: endpoint_ip(peer.endpoint) || rec.endpoint || '',
|
||||
present: true,
|
||||
};
|
||||
}
|
||||
|
||||
// Forget counters of devices that no longer exist (their bytes stay in
|
||||
// the person's totals).
|
||||
for (let id in keys(usage.devices))
|
||||
if (!m.devices[id])
|
||||
delete usage.devices[id];
|
||||
|
||||
prune_usage(usage, policy(m.settings.retention));
|
||||
usage.updated = ts;
|
||||
return live;
|
||||
};
|
||||
|
||||
export function save_usage(usage, persist) {
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(`${RUN_DIR}/usage.json`, usage, 0o600);
|
||||
if (persist) {
|
||||
usage.flushed = time();
|
||||
ensure_dir(DATA_DIR, 0o700);
|
||||
write_json(`${DATA_DIR}/usage.json`, usage, 0o600);
|
||||
}
|
||||
};
|
||||
|
||||
/** Sum of [rx, tx] over the last `days` days for a usage record. */
|
||||
export function recent(rec, days) {
|
||||
let out = [ 0, 0 ];
|
||||
for (let i = 0; i < days; i++) {
|
||||
let v = rec?.days?.[day_key_ago(i)];
|
||||
if (v) {
|
||||
out[0] += v[0];
|
||||
out[1] += v[1];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
export function month(rec) {
|
||||
return rec?.months?.[month_key()] ?? [ 0, 0 ];
|
||||
};
|
||||
|
||||
/** Daily series for the last `days` days: [{ day, rx, tx }]. */
|
||||
export function series(recs, days) {
|
||||
let out = [];
|
||||
for (let i = days - 1; i >= 0; i--) {
|
||||
let k = day_key_ago(i);
|
||||
let rx = 0, tx = 0;
|
||||
for (let r in recs) {
|
||||
let v = r?.days?.[k];
|
||||
if (v) {
|
||||
rx += v[0];
|
||||
tx += v[1];
|
||||
}
|
||||
}
|
||||
push(out, { day: k, rx, tx });
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/* ---------- events ---------- */
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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 ??= [];
|
||||
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();
|
||||
for (let e in list) {
|
||||
e.ts ??= ts;
|
||||
push(ev, with_names(e, c));
|
||||
}
|
||||
ev = trim_events(ev, pol);
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(`${RUN_DIR}/events.json`, ev, 0o600);
|
||||
unlock(l);
|
||||
};
|
||||
|
||||
export function add_event(e) {
|
||||
add_events([ e ]);
|
||||
};
|
||||
|
||||
/**
|
||||
* Like add_event, but folds into the previous event of the same kind, person
|
||||
* 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;
|
||||
let l = lock('events');
|
||||
let ev = load_events();
|
||||
let last = ev[length(ev) - 1];
|
||||
let ts = time();
|
||||
if (last && last.type == e.type && last.person == e.person && last.device == e.device && ts - last.ts < (window ?? 120)) {
|
||||
let parts = uniq([ ...split(last.detail ?? '', ', '), ...split(e.detail ?? '', ', ') ]);
|
||||
last.detail = join(', ', filter(parts, (x) => x != ''));
|
||||
last.ts = ts;
|
||||
}
|
||||
else {
|
||||
let c = cursor();
|
||||
c.load('dawnline');
|
||||
e.ts = ts;
|
||||
push(ev, with_names(e, c));
|
||||
}
|
||||
ev = trim_events(ev, pol);
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(`${RUN_DIR}/events.json`, ev, 0o600);
|
||||
unlock(l);
|
||||
};
|
||||
|
||||
/**
|
||||
* Places for connections logged before their address was located (the
|
||||
* lookup finishes after the event): connect events since `since` from an
|
||||
* address in `where` ({ ip: "City · ISP" }) that have no place yet get it.
|
||||
* Returns true when `ev` changed.
|
||||
*/
|
||||
export function fill_places(ev, where, since) {
|
||||
let changed = false;
|
||||
for (let i = length(ev) - 1; i >= 0 && ev[i].ts >= since; i--) {
|
||||
let e = ev[i];
|
||||
if (e.type == 'connect' && !e.where && where[e.detail]) {
|
||||
e.where = where[e.detail];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
return changed;
|
||||
};
|
||||
|
||||
/** fill_places() on the live log, written only when something changed. */
|
||||
export function set_places(where, since) {
|
||||
let l = lock('events');
|
||||
let ev = load_events();
|
||||
if (fill_places(ev, where, since)) {
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(`${RUN_DIR}/events.json`, ev, 0o600);
|
||||
}
|
||||
unlock(l);
|
||||
};
|
||||
|
||||
/** Save the log to flash, or drop the flash copy when it is kept in RAM only. */
|
||||
export function persist_events(pol) {
|
||||
pol ??= policy();
|
||||
if (pol.persist == 'off') {
|
||||
if (stat(`${DATA_DIR}/events.json`))
|
||||
unlink(`${DATA_DIR}/events.json`);
|
||||
return;
|
||||
}
|
||||
// Only when something was logged since the last copy: flash is written rarely.
|
||||
let live_st = stat(`${RUN_DIR}/events.json`);
|
||||
if (!live_st || live_st.mtime <= (stat(`${DATA_DIR}/events.json`)?.mtime ?? 0))
|
||||
return;
|
||||
let ev = read_json(`${RUN_DIR}/events.json`, null);
|
||||
if (ev != null) {
|
||||
ensure_dir(DATA_DIR, 0o700);
|
||||
write_json(`${DATA_DIR}/events.json`, trim_events(ev, pol), 0o600);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove events matching every given filter (person, device, category,
|
||||
* older than `before` ts); no filters clears the whole log. Edits both the
|
||||
* live copy and the one on flash. Returns how many were removed.
|
||||
*/
|
||||
export function clear_events(f) {
|
||||
f ??= {};
|
||||
let hit = (e) =>
|
||||
(!f.person || e.person == f.person) &&
|
||||
(!f.device || e.device == f.device) &&
|
||||
(!f.category || category_of(e.type) == f.category) &&
|
||||
(!f.before || (e.ts ?? 0) < f.before);
|
||||
let l = lock('events');
|
||||
let removed = 0;
|
||||
for (let path in [ `${RUN_DIR}/events.json`, `${DATA_DIR}/events.json` ]) {
|
||||
let ev = read_json(path, null);
|
||||
if (ev == null)
|
||||
continue;
|
||||
let keep = filter(ev, (e) => !hit(e));
|
||||
if (path == `${RUN_DIR}/events.json` || removed == 0)
|
||||
removed = max(removed, length(ev) - length(keep));
|
||||
write_json(path, keep, 0o600);
|
||||
}
|
||||
unlock(l);
|
||||
return removed;
|
||||
};
|
||||
|
||||
/* ---------- health ---------- */
|
||||
|
||||
export function iface_status(ifname) {
|
||||
return util_iface_status(ifname);
|
||||
};
|
||||
|
||||
export function uptime() {
|
||||
let s = readfile('/proc/uptime');
|
||||
return s ? int(+split(s, ' ')[0]) : 0;
|
||||
};
|
||||
@@ -1,114 +0,0 @@
|
||||
// Dawnline — where names point and the router's public address, for Health
|
||||
// and the portal settings. Lookups are cached in RUN_DIR for a few minutes
|
||||
// and made in parallel with a short timeout: rpcd answers one call at a
|
||||
// time, so a slow DNS server must not hold up the whole admin UI.
|
||||
|
||||
import { readfile } from 'fs';
|
||||
import { q, sh, ensure_dir, read_json, write_json, ip2int, is_ip6, rand_hex, RUN_DIR } from 'dawnline.util';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as net from 'dawnline.net';
|
||||
|
||||
const CACHE = `${RUN_DIR}/resolve.json`;
|
||||
// Answers are kept 5 minutes, failures 1 minute.
|
||||
const TTL = 300;
|
||||
const TTL_FAIL = 60;
|
||||
const TIMEOUT = 2;
|
||||
const MAX_NAMES = 16;
|
||||
// Public resolvers first: local DNS often rewrites the router's own name to
|
||||
// its LAN address (split horizon).
|
||||
const SERVERS = [ '1.1.1.1', '8.8.8.8', '127.0.0.1' ];
|
||||
|
||||
function valid_name(h) {
|
||||
return type(h) == 'string' && length(h) <= 253 && match(h, /^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$/) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Addresses of each name, IPv4 and IPv6: { host: [ ip, … ] } (empty when it
|
||||
* does not resolve). IP addresses map to themselves. Every uncached name is
|
||||
* asked of every server at once, A and AAAA; the first server (in SERVERS
|
||||
* order) that answers wins.
|
||||
*/
|
||||
export function resolve_all(hosts) {
|
||||
let out = {}, now = time(), todo = [];
|
||||
let cache = read_json(CACHE, {});
|
||||
for (let h in hosts) {
|
||||
if (h == null || out[h] != null)
|
||||
continue;
|
||||
if (ip2int(h) != null || is_ip6(h)) {
|
||||
out[h] = [ h ];
|
||||
continue;
|
||||
}
|
||||
let c = cache[h];
|
||||
if (c && now - c.ts < (length(c.ips) ? TTL : TTL_FAIL))
|
||||
out[h] = c.ips;
|
||||
else if (valid_name(h) && length(todo) < MAX_NAMES) {
|
||||
out[h] = [];
|
||||
push(todo, h);
|
||||
}
|
||||
else
|
||||
out[h] = [];
|
||||
}
|
||||
if (!length(todo))
|
||||
return out;
|
||||
|
||||
ensure_dir(RUN_DIR);
|
||||
let dir = `${RUN_DIR}/resolve.${rand_hex(4)}`;
|
||||
let cmds = [];
|
||||
for (let i = 0; i < length(todo); i++)
|
||||
for (let j = 0; j < length(SERVERS); j++)
|
||||
for (let t in [ 'a', 'aaaa' ])
|
||||
// nslookup's own timeout: some firmware builds BusyBox without `timeout`.
|
||||
push(cmds, `( nslookup -type=${t} -timeout=${TIMEOUT} -retry=1 ${q(todo[i])} ${SERVERS[j]} 2>/dev/null | awk '/^Address/ && NR>2 {print $NF}' > ${dir}/${i}.${j}.${t} ) &`);
|
||||
sh(`mkdir -p ${dir}; ${join(' ', cmds)} wait`);
|
||||
for (let i = 0; i < length(todo); i++) {
|
||||
let ips = [];
|
||||
for (let j = 0; j < length(SERVERS) && !length(ips); j++)
|
||||
for (let t in [ 'a', 'aaaa' ]) {
|
||||
let r = readfile(`${dir}/${i}.${j}.${t}`) ?? '';
|
||||
push(ips, ...filter(split(trim(r), '\n'), (x) => ip2int(x) != null || is_ip6(x)));
|
||||
}
|
||||
out[todo[i]] = ips;
|
||||
cache[todo[i]] = { ts: now, ips };
|
||||
}
|
||||
system(`rm -rf ${dir}`);
|
||||
// Only what is still fresh is kept.
|
||||
for (let h, c in cache)
|
||||
if (now - c.ts >= TTL)
|
||||
delete cache[h];
|
||||
write_json(CACHE, cache, 0o644);
|
||||
return out;
|
||||
};
|
||||
|
||||
/** IPv4 addresses of one name (see resolve_all). */
|
||||
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);
|
||||
return st?.['ipv4-address']?.[0]?.address;
|
||||
};
|
||||
|
||||
/** The router's public IPv4 (the first server's WAN, else the wan interface). */
|
||||
export function router_wan(m) {
|
||||
for (let id, srv in m.servers)
|
||||
if (srv.private_key) {
|
||||
let a = net.wan_address(srv) ?? wan_ipv4(srv.wan_zone);
|
||||
if (a)
|
||||
return a;
|
||||
}
|
||||
return wan_ipv4('wan');
|
||||
};
|
||||
@@ -1,686 +0,0 @@
|
||||
// Dawnline — "like a home device": tunnel clients' internet traffic enters
|
||||
// the router from the LAN, so whatever handles LAN traffic there (proxies,
|
||||
// DPI bypass, DNS filters, fw4's lan zone, ...) handles it too, without
|
||||
// Dawnline knowing about any of them.
|
||||
//
|
||||
// dawn0 ──policy route──▶ dlx0 ═veth═ dlx1 ┐ netns "dawnline" ┌ dll1 ═veth═ dll0 ── br-lan
|
||||
// └── NAT to a LAN address ┘
|
||||
//
|
||||
// The netns is one more device on the LAN bridge with its own address (picked
|
||||
// from the LAN subnet above the DHCP pool) and its own MAC (hashed from the
|
||||
// bridge's), the same each time it is created: the router's neighbour table
|
||||
// knows the address by that MAC, so a netns created again keeps its address
|
||||
// instead of seeing it held by another device. Replies come back the same way.
|
||||
// Home-network destinations keep their direct route; per-person access is
|
||||
// still enforced on dawn0 before any of this (table inet dawnline).
|
||||
//
|
||||
// Clients also use the netns address as DNS: it forwards to the DNS the LAN
|
||||
// gets from DHCP, from the LAN side, so DNS interception applies as well.
|
||||
//
|
||||
// The netns forwards and NATs every packet a second time (conntrack, the
|
||||
// forward hook): about 40% more CPU per packet on an MT7981. A flowtable on
|
||||
// its two links (dlx1, dll1) lets established TCP and UDP flows skip that
|
||||
// work. The netns holds no access rules: who may reach what is judged on the
|
||||
// tunnel interfaces in the root namespace, which no flowtable here touches.
|
||||
// Without kernel flowtables (kmod-nft-offload) the loop runs as before,
|
||||
// slower, and logs it once.
|
||||
//
|
||||
// Separately, people with Home-only access get a plain resolver
|
||||
// (dawnline.resolver). While the netns exists it runs inside it and sends its
|
||||
// upstream queries back out through the transit link as forwarded traffic —
|
||||
// not as the router's own traffic and not from the LAN — so tools that
|
||||
// capture either leave it alone. This module says where it runs.
|
||||
//
|
||||
// Everything is derived from the router at run time; nothing assumes a
|
||||
// particular subnet or tool. If the LAN is not a bridge or setup fails, the
|
||||
// direct path stays and status() says why (kept as a code in loop.json, which
|
||||
// apply and the agent write with no language; error_text() makes the words).
|
||||
|
||||
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, syslog, syslog_lines, RUN_DIR
|
||||
} from 'dawnline.util';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as store from 'dawnline.store';
|
||||
import * as resolver from 'dawnline.resolver';
|
||||
import { _, L_ } from 'dawnline.i18n';
|
||||
|
||||
export const NS = 'dawnline';
|
||||
const TX_ROOT = net.INTERNAL.transit, TX_NS = 'dlx1', LAN_ROOT = net.INTERNAL.lan, LAN_NS = 'dll1';
|
||||
const PREF_LOOP_DNS = 5205, PREF_MAIN = 5210, PREF_LOOP = 5290;
|
||||
export const RESOLVER_CONF = resolver.CONF;
|
||||
const STATE = `${RUN_DIR}/loop.json`;
|
||||
const NS_RULES = '/var/etc/dawnline-ns.nft';
|
||||
|
||||
const WHO = L_('Internet like a home device');
|
||||
|
||||
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) {
|
||||
push(errs, `${c}: ${trim(r.out)}`);
|
||||
syslog('error', 'loop', `failed: ${c} · ${trim(r.out)}`);
|
||||
}
|
||||
}
|
||||
return errs;
|
||||
}
|
||||
|
||||
function exists(dev) {
|
||||
return stat(`/sys/class/net/${dev}`) != null;
|
||||
}
|
||||
|
||||
/* ---------- facts about the LAN ---------- */
|
||||
|
||||
/** DHCP pool, static leases and the DNS handed to LAN clients, for one netifd interface. */
|
||||
function dhcp_facts(logical, lan) {
|
||||
let c = cursor();
|
||||
c.load('dhcp');
|
||||
let pool = null, dns = null, statics = [];
|
||||
c.foreach('dhcp', 'dhcp', (s) => {
|
||||
if (s.interface != logical || s.ignore == '1')
|
||||
return;
|
||||
let start = +(s.start ?? 100), limit = +(s.limit ?? 150);
|
||||
pool = [ lan.net + start, lan.net + start + limit - 1 ];
|
||||
let opts = type(s.dhcp_option) == 'array' ? s.dhcp_option : (s.dhcp_option ? [ s.dhcp_option ] : []);
|
||||
for (let o in opts) {
|
||||
let m = match(o, /^(option:dns-server|6),([0-9.]+)/);
|
||||
if (m && !dns)
|
||||
dns = m[2];
|
||||
}
|
||||
});
|
||||
c.foreach('dhcp', 'host', (s) => {
|
||||
let ip = ip2int(s.ip ?? '');
|
||||
if (ip != null)
|
||||
push(statics, ip);
|
||||
});
|
||||
return { pool, dns, statics };
|
||||
}
|
||||
|
||||
/** Addresses other devices are using right now (neighbour table + leases). */
|
||||
function used_addresses(dev) {
|
||||
let used = {};
|
||||
try {
|
||||
for (let n in json(sh_out(`ip -j -4 neigh show dev ${q(dev)}`) || '[]') ?? [])
|
||||
if (n.dst && !(index(n.state ?? [], 'FAILED') >= 0 || index(n.state ?? [], 'INCOMPLETE') >= 0))
|
||||
used[n.dst] = n.lladdr ?? true;
|
||||
}
|
||||
catch (e) {}
|
||||
for (let l in split(readfile('/tmp/dhcp.leases') ?? '', '\n')) {
|
||||
let f = split(l, ' ');
|
||||
if (length(f) >= 3)
|
||||
used[f[2]] = f[1];
|
||||
}
|
||||
return used;
|
||||
}
|
||||
|
||||
/** The MAC the netns's LAN link has now; null without the link. */
|
||||
function our_mac() {
|
||||
return sh_out(`ip -n ${NS} -j link show ${LAN_NS} 2>/dev/null | jsonfilter -e '@[0].address'`);
|
||||
}
|
||||
|
||||
/** 32-bit FNV-1a of a string, continuing from `h`. */
|
||||
function fnv1a(s, h) {
|
||||
for (let i = 0; i < length(s); i++)
|
||||
h = ((h ^ ord(s, i)) * 16777619) & 0xffffffff;
|
||||
return h;
|
||||
}
|
||||
|
||||
/**
|
||||
* The MAC for the netns's LAN link, from the bridge's MAC: a locally
|
||||
* administered unicast address hashed from it, so it is the same every time
|
||||
* and differs between routers. Not the bridge's MAC with a bit flipped: radios
|
||||
* and ports often get those.
|
||||
*/
|
||||
export function lan_mac(bridge) {
|
||||
let s = lc(trim(bridge ?? ''));
|
||||
let h1 = fnv1a(`dawnline lan ${s}`, 0x811c9dc5), h2 = fnv1a(s, h1);
|
||||
let b = [ ((h1 >> 24) & 0xfc) | 0x02, (h1 >> 16) & 0xff, (h1 >> 8) & 0xff, h1 & 0xff, (h2 >> 24) & 0xff, (h2 >> 16) & 0xff ];
|
||||
return join(':', map(b, (x) => sprintf('%02x', x)));
|
||||
};
|
||||
|
||||
/**
|
||||
* True when the netns may use a LAN address (a number): a host of the
|
||||
* network, not the router's, not a static lease, outside the DHCP pool and
|
||||
* not held by another device in `used` (address → MAC, or true without one).
|
||||
* An entry with one of the MACs in `mine` is the netns itself, also from
|
||||
* before it was created again.
|
||||
*/
|
||||
export function address_free(ip, lan, facts, used, mine) {
|
||||
if (ip <= lan.net || ip >= lan.net + lan.size - 1 || ip == lan.ip || index(facts.statics, ip) >= 0)
|
||||
return false;
|
||||
if (facts.pool && ip >= facts.pool[0] && ip <= facts.pool[1])
|
||||
return false;
|
||||
let u = used[int2ip(ip)];
|
||||
return u == null || index(mine, u) >= 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* A LAN address for the netns: keep the saved one while it is free (see
|
||||
* address_free; `mine` are the netns's MACs), otherwise the highest free host
|
||||
* above the DHCP pool (then below it) that does not answer.
|
||||
*/
|
||||
function pick_address(lan, facts, saved, dev, mine) {
|
||||
let used = used_addresses(dev);
|
||||
let sv = ip2int(saved ?? '');
|
||||
if (sv != null && address_free(sv, lan, facts, used, mine))
|
||||
return int2ip(sv);
|
||||
for (let ip = lan.net + lan.size - 2; ip > lan.net; ip--) {
|
||||
if (!address_free(ip, lan, facts, used, mine))
|
||||
continue;
|
||||
// Last check: nobody answers on it.
|
||||
sh(`ping -c 1 -W 1 -I ${q(dev)} ${int2ip(ip)}`);
|
||||
if (address_free(ip, lan, facts, used_addresses(dev), mine))
|
||||
return int2ip(ip);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** A /30 in 169.254.0.0/16 that no route or address on the router overlaps. */
|
||||
function pick_transit(saved) {
|
||||
let busy = [];
|
||||
for (let dev, d in net.snapshot().devices)
|
||||
for (let n in [ ...d.nets, ...d.addrs ])
|
||||
push(busy, n);
|
||||
let free = (base) => {
|
||||
for (let b in busy) {
|
||||
let c = cidr_any(b);
|
||||
if (c && base < c.net + c.size && c.net < base + 4)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
let sv = ip2int(saved ?? '');
|
||||
if (sv != null && free(sv))
|
||||
return sv;
|
||||
for (let k = 77; k < 77 + 256; k++) {
|
||||
let base = ip2int(`169.254.${k % 256}.0`);
|
||||
if (free(base))
|
||||
return base;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** A routing table nobody else uses (ours holds a single route via the transit link). */
|
||||
function pick_table(saved) {
|
||||
let free = (t) => {
|
||||
let r = sh_out(`ip -4 route show table ${t}`) ?? '';
|
||||
return r == '' || (length(split(r, '\n')) == 1 && index(r, ` dev ${TX_ROOT}`) >= 0);
|
||||
};
|
||||
if (saved && free(saved))
|
||||
return saved;
|
||||
for (let t = 2077; t < 2177; t++)
|
||||
if (free(t))
|
||||
return t;
|
||||
return null;
|
||||
}
|
||||
|
||||
/* ---------- why it is off ---------- */
|
||||
|
||||
/*
|
||||
* The reasons plan() and ensure() give, kept in loop.json:
|
||||
* { code: 'no_lan' } no LAN detected
|
||||
* { code: 'not_bridge', dev } the LAN is not a bridge
|
||||
* { code: 'no_ipv4', dev } the LAN has no IPv4 address
|
||||
* { code: 'no_address', net } no free address on the LAN's network
|
||||
* { code: 'no_transit' } no free link-local range for the internal link
|
||||
* { code: 'no_table' } no free routing table
|
||||
* { code: 'setup', detail } a command failed (detail: it and what it said)
|
||||
*/
|
||||
|
||||
/** 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')
|
||||
return sprintf(L_('%s is not a bridge, so Dawnline cannot join the LAN as a device.'), e.dev);
|
||||
if (e.code == 'no_ipv4')
|
||||
return sprintf(L_('%s has no IPv4 address.'), e.dev);
|
||||
if (e.code == 'no_address')
|
||||
return sprintf(L_('No free address on %s.'), e.net);
|
||||
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
|
||||
* reader's language. Null for none; the English of a loop.json written by an
|
||||
* older version as it is.
|
||||
*/
|
||||
export function error_text(e) {
|
||||
if (type(e) != 'object')
|
||||
return e ?? null;
|
||||
if (e.code == 'no_lan')
|
||||
return _('No LAN detected.');
|
||||
if (e.code == 'not_bridge')
|
||||
return sprintf(_('%s is not a bridge, so Dawnline cannot join the LAN as a device.'), e.dev);
|
||||
if (e.code == 'no_ipv4')
|
||||
return sprintf(_('%s has no IPv4 address.'), e.dev);
|
||||
if (e.code == 'no_address')
|
||||
return sprintf(_('No free address on %s.'), e.net);
|
||||
if (e.code == 'no_transit')
|
||||
return _('No free link-local range for the internal link.');
|
||||
if (e.code == 'no_table')
|
||||
return _('No free routing table.');
|
||||
return sprintf(_('Setup failed: %s'), e.detail);
|
||||
};
|
||||
|
||||
/* ---------- plan ---------- */
|
||||
|
||||
function settings() {
|
||||
let c = cursor();
|
||||
c.load('dawnline');
|
||||
return c.get_all('dawnline', 'settings') ?? {};
|
||||
}
|
||||
|
||||
/** Servers that want "like a home device" (default on) and run a core. */
|
||||
function wanted(m) {
|
||||
return filter(values(m.servers), (s) => length(core.tunnels(s)) && s.enabled && s.lan_like);
|
||||
}
|
||||
|
||||
/** Every server's interface, for rules. */
|
||||
function all_ifnames(m) {
|
||||
return map(values(m.servers), (s) => s.ifname);
|
||||
}
|
||||
|
||||
/**
|
||||
* What should exist, or { error } (a reason, see error_text) when it cannot.
|
||||
* Pure apart from reading router state (and one ping per candidate address).
|
||||
*/
|
||||
export function plan(m) {
|
||||
let srvs = wanted(m);
|
||||
if (!length(srvs))
|
||||
return { off: true };
|
||||
let snap = net.snapshot();
|
||||
let dev = snap.lan;
|
||||
let d = snap.devices[dev];
|
||||
if (!dev || !d)
|
||||
return { error: { code: 'no_lan' } };
|
||||
if (d.kind != 'bridge')
|
||||
return { error: { code: 'not_bridge', dev } };
|
||||
let lan = parse_cidr(d.addrs[0]);
|
||||
if (!lan)
|
||||
return { error: { code: 'no_ipv4', dev } };
|
||||
let facts = dhcp_facts(d.logical, lan);
|
||||
let st = settings();
|
||||
let mac = lan_mac(readfile(`/sys/class/net/${dev}/address`));
|
||||
// The netns's MACs: the one it gets, and the one it has (a random one from an older version).
|
||||
let addr = pick_address(lan, facts, st.lan_address, dev, [ mac, our_mac() ]);
|
||||
if (!addr)
|
||||
return { error: { code: 'no_address', net: `${int2ip(lan.net)}/${lan.prefix}` } };
|
||||
let last = read_json(STATE, {});
|
||||
let tx = pick_transit(last?.transit);
|
||||
if (tx == null)
|
||||
return { error: { code: 'no_transit' } };
|
||||
let table = pick_table(last?.plan?.table);
|
||||
if (table == null)
|
||||
return { error: { code: 'no_table' } };
|
||||
let servers = [];
|
||||
for (let srv in srvs) {
|
||||
let sn = net.server_net(srv, snap);
|
||||
for (let t in core.tunnels(srv))
|
||||
push(servers, { ifname: t.ifname, subnet: t.subnet, direct: net.collapse([ t.subnet, ...sn.nets ]) });
|
||||
}
|
||||
return {
|
||||
dev,
|
||||
gateway: int2ip(lan.ip),
|
||||
address: addr,
|
||||
mac,
|
||||
prefix: lan.prefix,
|
||||
dns: facts.dns ?? int2ip(lan.ip),
|
||||
transit: tx,
|
||||
tx_root: int2ip(tx + 1),
|
||||
tx_ns: int2ip(tx + 2),
|
||||
table,
|
||||
servers,
|
||||
};
|
||||
};
|
||||
|
||||
/* ---------- apply ---------- */
|
||||
|
||||
/**
|
||||
* The netns's nft script as text (nothing is loaded). With `fast`, a
|
||||
* flowtable on the netns's own two links takes established TCP and UDP flows
|
||||
* past conntrack and the forward hook here. Only dlx1 and dll1: the root
|
||||
* namespace's links (dawn0, dlx0) stay out, the guard there sees every packet.
|
||||
*/
|
||||
export function ns_rules(p, fast) {
|
||||
return `table inet dawnline
|
||||
delete table inet dawnline
|
||||
table inet dawnline {${fast ? `
|
||||
flowtable ft {
|
||||
hook ingress priority 0;
|
||||
devices = { "${TX_NS}", "${LAN_NS}" };
|
||||
}` : ''}
|
||||
chain nat_pre {
|
||||
type nat hook prerouting priority dstnat; policy accept;
|
||||
iifname "${TX_NS}" ip daddr ${p.address} meta l4proto { tcp, udp } th dport 53 dnat ip to ${p.dns}:53
|
||||
}
|
||||
chain nat_post {
|
||||
type nat hook postrouting priority srcnat; policy accept;
|
||||
oifname "${LAN_NS}" masquerade
|
||||
}
|
||||
chain filter_forward {
|
||||
type filter hook forward priority filter; policy drop;${fast ? `
|
||||
ct state established meta l4proto { tcp, udp } flow add @ft` : ''}
|
||||
ct state established,related accept
|
||||
iifname "${TX_NS}" oifname "${LAN_NS}" accept
|
||||
}
|
||||
chain filter_input {
|
||||
type filter hook input priority filter; policy drop;
|
||||
ct state established,related accept
|
||||
iifname "lo" accept
|
||||
icmp type echo-request accept
|
||||
iifname "${TX_NS}" meta l4proto { tcp, udp } th dport 53 accept
|
||||
}
|
||||
}
|
||||
`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Load the netns's rules, with the flowtable when the kernel takes it, else
|
||||
* without (the loop works either way). { fast, slow, err }: slow is why the
|
||||
* flowtable failed, err why the rules without it failed too.
|
||||
*/
|
||||
function load_ns_rules(p) {
|
||||
ensure_dir('/var/etc');
|
||||
let load = (fast) => {
|
||||
writefile(NS_RULES, ns_rules(p, fast));
|
||||
return run([ `ip netns exec ${NS} nft -f ${NS_RULES}` ])[0];
|
||||
};
|
||||
let slow = load(true);
|
||||
return slow ? { fast: false, slow, err: load(false) } : { fast: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* The policy rules plan `p` wants (pure), in order: [{ pref, iif, to, table }]
|
||||
* (to null: all). Per server interface: DNS to the netns address through the
|
||||
* netns, home networks and the tunnel network by main, the rest into the netns.
|
||||
*/
|
||||
export function rules(p) {
|
||||
let out = [];
|
||||
let rule = (pref, iif, to, table) => push(out, { pref, iif, to, table });
|
||||
for (let s in p?.servers ?? []) {
|
||||
rule(PREF_LOOP_DNS, s.ifname, `${p.address}/32`, `${p.table}`);
|
||||
for (let n in s.direct)
|
||||
rule(PREF_MAIN, s.ifname, n, 'main');
|
||||
rule(PREF_LOOP, s.ifname, null, `${p.table}`);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/** Our policy rules: in our priority band and bound to a Dawnline tunnel interface. */
|
||||
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 || 'iif_detached' in r))
|
||||
push(out, r);
|
||||
}
|
||||
catch (e) {}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
// A rule of ours with a source (none of rules() has one) is told apart, so it is removed.
|
||||
function rule_key(pref, from, iif, to, table) {
|
||||
// `ip -j rule` shows a /32 source or destination without its length.
|
||||
let full = (n) => n && index(n, '/') < 0 ? `${n}/32` : n;
|
||||
return `${pref} ${full(from) ?? 'all'} ${iif} ${full(to) ?? 'all'} ${table}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* A source or destination as `ip -j rule` has it ("10.9.0.2", or with its
|
||||
* length), or null for all: a rule without a source shows `"src": "all"`
|
||||
* (iproute2 prints "from all"), one without a destination no `dst`.
|
||||
*/
|
||||
function rule_net(addr, len) {
|
||||
return addr && addr != 'all' ? `${addr}${len != null ? '/' + len : ''}` : null;
|
||||
}
|
||||
|
||||
/** The command that removes one of current_rules(). */
|
||||
function rule_del(r) {
|
||||
let from = rule_net(r.src, r.srclen), to = rule_net(r.dst, r.dstlen);
|
||||
return `ip -4 rule del priority ${r.priority}${from ? ' from ' + from : ''} iif ${q(r.iif)}${to ? ' to ' + to : ''} lookup ${r.table}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The commands that turn rules `current` (ours, as `ip -j -4 rule show`
|
||||
* prints them) into rules(p) (pure): the missing ones added first, then the
|
||||
* ones no longer wanted removed, so a device's traffic never falls between
|
||||
* two rules while others change. [] when they are as wanted.
|
||||
*/
|
||||
export function rule_cmds(p, current) {
|
||||
let have = {}, want = {}, cmds = [], twice = [];
|
||||
for (let r in current) {
|
||||
let k = rule_key(r.priority, rule_net(r.src, r.srclen), r.iif, rule_net(r.dst, r.dstlen), r.table);
|
||||
if (have[k])
|
||||
push(twice, rule_del(r));
|
||||
have[k] = r;
|
||||
}
|
||||
for (let w in rules(p)) {
|
||||
let k = rule_key(w.pref, null, w.iif, w.to, w.table);
|
||||
if (!have[k] && !want[k])
|
||||
push(cmds, `ip -4 rule add priority ${w.pref} iif ${q(w.iif)}${w.to ? ' to ' + w.to : ''} lookup ${w.table}`);
|
||||
want[k] = true;
|
||||
}
|
||||
for (let k, r in have)
|
||||
if (!want[k])
|
||||
push(cmds, rule_del(r));
|
||||
// The same rule twice (one deletion removes one of them).
|
||||
push(cmds, ...twice);
|
||||
return cmds;
|
||||
};
|
||||
|
||||
function sync_rules(p, ifnames) {
|
||||
return rule_cmds(p, current_rules(ifnames));
|
||||
}
|
||||
|
||||
function tunnel_ifnames() {
|
||||
let c = cursor();
|
||||
c.load('dawnline');
|
||||
let out = [];
|
||||
c.foreach('dawnline', 'server', (s) => {
|
||||
push(out, s.ifname ?? s['.name']);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Remove the netns, links and rules, and the router's neighbour entry for the netns. */
|
||||
export function remove(ifnames) {
|
||||
ifnames ??= tunnel_ifnames();
|
||||
let last = read_json(STATE, {})?.plan;
|
||||
// Read while the link is there: the entry is ours when it carries the netns's MAC.
|
||||
let held = last?.dev ? used_addresses(last.dev)[last.address] : null;
|
||||
let stale = type(held) == 'string' && index([ last.mac, our_mac() ], held) >= 0;
|
||||
let cmds = map(current_rules(ifnames), rule_del);
|
||||
if (last?.table)
|
||||
push(cmds, `ip -4 route flush table ${last.table}`);
|
||||
if (exists(TX_ROOT))
|
||||
push(cmds, `ip link del ${TX_ROOT}`);
|
||||
if (exists(LAN_ROOT))
|
||||
push(cmds, `ip link del ${LAN_ROOT}`);
|
||||
if (stat(`/var/run/netns/${NS}`))
|
||||
push(cmds, `ip netns del ${NS}`);
|
||||
// Left there, it would hold the address for a MAC nobody has.
|
||||
if (stale)
|
||||
push(cmds, `ip -4 neigh del ${q(last.address)} dev ${q(last.dev)}`);
|
||||
run(cmds);
|
||||
unlink(STATE);
|
||||
};
|
||||
|
||||
/**
|
||||
* Bring the loop to the plan. Cheap when nothing changed: compares with the
|
||||
* last applied plan and only checks that links, bridge port and rules exist.
|
||||
*/
|
||||
export function ensure(m, force) {
|
||||
let ifnames = all_ifnames(m);
|
||||
let p = plan(m);
|
||||
if (p.off || p.error) {
|
||||
// Read before remove(), which deletes it: the error is logged once, not every tick.
|
||||
let was = read_json(STATE, {});
|
||||
if (was.plan || exists(TX_ROOT) || exists(LAN_ROOT) || length(current_rules(ifnames)))
|
||||
remove(ifnames);
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(STATE, { error: p.error, off: !!p.off }, 0o644);
|
||||
if (p.error && sprintf('%J', p.error) != sprintf('%J', was.error))
|
||||
log(L_('is off'), 'error', error_note(p.error));
|
||||
return p;
|
||||
}
|
||||
let last = read_json(STATE, {});
|
||||
let same = !force && last.applied && sprintf('%J', last.plan) == sprintf('%J', p);
|
||||
let healthy = exists(TX_ROOT) && exists(LAN_ROOT) && stat(`/var/run/netns/${NS}`) &&
|
||||
replace(readlink(`/sys/class/net/${LAN_ROOT}/master`) ?? '', /^.*\//, '') == p.dev;
|
||||
let rules = sync_rules(p, ifnames);
|
||||
if (same && healthy && !length(rules))
|
||||
return p;
|
||||
|
||||
// A link made by an older version has a random MAC (its plan had none, so
|
||||
// this runs once after an update): it gets the stable one.
|
||||
let remac = exists(LAN_ROOT) && our_mac() != p.mac;
|
||||
let cmds = [];
|
||||
if (!stat(`/var/run/netns/${NS}`))
|
||||
push(cmds, `ip netns add ${NS}`);
|
||||
if (!exists(TX_ROOT))
|
||||
push(cmds, `ip link add ${TX_ROOT} type veth peer name ${TX_NS} netns ${NS}`);
|
||||
if (!exists(LAN_ROOT))
|
||||
push(cmds, `ip link add ${LAN_ROOT} type veth peer name ${LAN_NS} address ${p.mac} netns ${NS}`);
|
||||
if (remac)
|
||||
push(cmds, `ip -n ${NS} link set ${LAN_NS} down`, `ip -n ${NS} link set ${LAN_NS} address ${p.mac}`);
|
||||
let errs = run(cmds);
|
||||
cmds = [
|
||||
`ip addr flush dev ${TX_ROOT}`,
|
||||
`ip addr add ${p.tx_root}/30 dev ${TX_ROOT}`,
|
||||
`ip link set ${TX_ROOT} up`,
|
||||
`ip link set ${LAN_ROOT} master ${q(p.dev)}`,
|
||||
`ip link set ${LAN_ROOT} up`,
|
||||
`ip -n ${NS} link set lo up`,
|
||||
`ip -n ${NS} addr flush dev ${TX_NS}`,
|
||||
`ip -n ${NS} addr add ${p.tx_ns}/30 dev ${TX_NS}`,
|
||||
`ip -n ${NS} link set ${TX_NS} up`,
|
||||
`ip -n ${NS} addr flush dev ${LAN_NS}`,
|
||||
`ip -n ${NS} addr add ${p.address}/${p.prefix} dev ${LAN_NS}`,
|
||||
`ip -n ${NS} link set ${LAN_NS} up`,
|
||||
`ip -n ${NS} route replace default via ${p.gateway} dev ${LAN_NS}`,
|
||||
// The resolver's own queries (from the transit address) go back out the transit link.
|
||||
`ip -n ${NS} route replace default via ${p.tx_root} dev ${TX_NS} table 100`,
|
||||
`ip -n ${NS} rule del from ${p.tx_ns} lookup 100 2>/dev/null; ip -n ${NS} rule add from ${p.tx_ns} lookup 100`,
|
||||
`ip netns exec ${NS} sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward; echo 0 > /proc/sys/net/ipv4/conf/all/rp_filter; echo 0 > /proc/sys/net/ipv4/conf/default/rp_filter'`,
|
||||
`ip -4 route replace default via ${p.tx_ns} dev ${TX_ROOT} table ${p.table}`,
|
||||
];
|
||||
for (let s in p.servers)
|
||||
push(cmds, `ip -n ${NS} route replace ${q(s.subnet)} via ${q(p.tx_root)} dev ${TX_NS}`);
|
||||
// The router knows the address by the old MAC: ask again now, not when the entry expires.
|
||||
if (remac)
|
||||
push(cmds, `ip -4 neigh flush to ${p.address} dev ${q(p.dev)}`);
|
||||
push(errs, ...run(cmds));
|
||||
let ft = load_ns_rules(p);
|
||||
if (ft.err)
|
||||
push(errs, ft.err);
|
||||
push(errs, ...run(sync_rules(p, ifnames)));
|
||||
|
||||
if (length(errs)) {
|
||||
let why = { code: 'setup', detail: errs[0] };
|
||||
if (sprintf('%J', read_json(STATE, {}).error) != sprintf('%J', why))
|
||||
log(L_('failed - internet goes out directly for now'), 'error', errs[0]);
|
||||
remove(ifnames);
|
||||
write_json(STATE, { error: why }, 0o644);
|
||||
return { error: why };
|
||||
}
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(STATE, { applied: time(), plan: p, transit: p.transit, fast: ft.fast }, 0o644);
|
||||
if (!last.applied || last.plan?.dev != p.dev || last.plan?.address != p.address)
|
||||
log(sprintf(L_('is on · traffic enters %s as %s'), p.dev, p.address), 'up');
|
||||
else if (!healthy)
|
||||
log(sprintf(L_('was broken - set up again (%s as %s)'), p.dev, p.address), 'repair');
|
||||
// Once, not again on every apply while the module is still missing.
|
||||
if (!ft.fast && last.fast !== false)
|
||||
log(L_('runs without its fast path'), 'info', join('\n', [
|
||||
L_('Without kmod-nft-offload every packet on this path passes the firewall twice. Install it and restart Dawnline.'),
|
||||
ft.slow,
|
||||
]));
|
||||
return p;
|
||||
};
|
||||
|
||||
/**
|
||||
* Keep the chosen LAN address stable across restarts (settings.lan_address).
|
||||
* Called by dawnline.apply once its own lock is released: the config lock is
|
||||
* never taken inside it.
|
||||
*/
|
||||
export function remember(p) {
|
||||
if (!p?.address || settings().lan_address == p.address)
|
||||
return;
|
||||
store.transact((c, m) => {
|
||||
c.set('dawnline', 'settings', 'settings');
|
||||
c.set('dawnline', 'settings', 'lan_address', p.address);
|
||||
});
|
||||
};
|
||||
|
||||
/** Last applied state for views and health checks; `error` says why it is off (error_text). */
|
||||
export function status() {
|
||||
let s = read_json(STATE, {});
|
||||
if (s.plan)
|
||||
return {
|
||||
active: exists(TX_ROOT) && exists(LAN_ROOT),
|
||||
dev: s.plan.dev, address: s.plan.address, dns: s.plan.dns, gateway: s.plan.gateway, transit: s.plan.tx_ns,
|
||||
bridged: replace(readlink(`/sys/class/net/${LAN_ROOT}/master`) ?? '', /^.*\//, '') == s.plan.dev,
|
||||
};
|
||||
return { active: false, off: !!s.off, error: error_text(s.error) };
|
||||
};
|
||||
|
||||
/** The DNS address tunnel clients should use while the loop is up. */
|
||||
export function client_dns() {
|
||||
let s = status();
|
||||
return s.active ? s.address : null;
|
||||
};
|
||||
|
||||
/* ---------- plain resolver for Home-only clients ---------- */
|
||||
|
||||
/** The DNS your LAN gets from DHCP (option 6), else the router on the LAN. */
|
||||
export function lan_dns() {
|
||||
let st = status();
|
||||
if (st.dns)
|
||||
return st.dns;
|
||||
let snap = net.snapshot();
|
||||
let d = snap.devices[snap.lan];
|
||||
let a = d?.addrs?.[0];
|
||||
if (!a)
|
||||
return null;
|
||||
let lan = parse_cidr(a);
|
||||
return (lan ? dhcp_facts(d.logical, lan).dns : null) ?? split(a, '/')[0];
|
||||
};
|
||||
|
||||
/**
|
||||
* Where Home-only clients' DNS goes: inside the netns while it is up
|
||||
* (`ns`: transit address, port 53), otherwise on the tunnel address (`root`).
|
||||
*/
|
||||
export function resolver_target() {
|
||||
let st = status();
|
||||
return st.active && st.transit ? { mode: 'ns', address: st.transit, port: 53 } : { mode: 'root', port: resolver.PORT };
|
||||
};
|
||||
|
||||
/** Write the resolver config where it should run now; true when it changed (see dawnline.resolver). */
|
||||
export function sync_resolver(m) {
|
||||
return resolver.sync(m, resolver_target(), lan_dns());
|
||||
};
|
||||
|
||||
/** True when the resolver process is alive. */
|
||||
export function resolver_running() {
|
||||
return resolver.running();
|
||||
};
|
||||
|
||||
export const RESOLVER = { port: resolver.PORT, conf: resolver.CONF };
|
||||
@@ -1,155 +0,0 @@
|
||||
// Dawnline — the live meter (Speed now on the Overview, the People list and a
|
||||
// person's page): each server's throughput and that of each device moving
|
||||
// traffic, one point a second over the last two minutes.
|
||||
//
|
||||
// The meter RPC (dawnline.rpc.status) calls want() and read(); the agent's
|
||||
// watcher makes a point from every read it already does (sample(), point())
|
||||
// and keeps the last two minutes in memory (keep()) whether anyone looks or
|
||||
// not, so a look gets them all at once. It writes them to FILE (save()) only
|
||||
// while WANT is younger than WANT_FOR seconds: the first second of a look,
|
||||
// then with each new point. When WANT goes stale it removes FILE: with nobody
|
||||
// looking the watcher pays a little arithmetic and one stat() a second, and
|
||||
// writes nothing. FILE holds server and device ids and byte rates only, no
|
||||
// keys, names or personal codes. Who is online and the per-minute rates are not here
|
||||
// (dawnline.live, the agent's tick).
|
||||
//
|
||||
// Bytes are the peers' counters (dawnline.awg.dump): tx includes the
|
||||
// disguise (S prefixes, padding, junk), rx the packets as they were
|
||||
// received without their prefix, so both are close to what is on the wire.
|
||||
|
||||
import { writefile, stat, unlink } from 'fs';
|
||||
import { read_json, write_file, ensure_dir, RUN_DIR } from 'dawnline.util';
|
||||
|
||||
/** Touched by every meter RPC: the watcher samples while it is fresh. */
|
||||
export const WANT = `${RUN_DIR}/meter.want`;
|
||||
/** The points, written by the watcher each second while WANT is fresh. */
|
||||
export const FILE = `${RUN_DIR}/meter.json`;
|
||||
/** Seconds after the last meter RPC the watcher keeps sampling. */
|
||||
export const WANT_FOR = 10;
|
||||
/** Points kept: two minutes, one a second. */
|
||||
export const POINTS = 120;
|
||||
|
||||
/** Ask the watcher to sample for the next WANT_FOR seconds (the meter RPC). */
|
||||
export function want() {
|
||||
ensure_dir(RUN_DIR);
|
||||
writefile(WANT, `${time()}`);
|
||||
};
|
||||
|
||||
/** Someone asked for the meter within WANT_FOR seconds of `now`. */
|
||||
export function wanted(now) {
|
||||
let st = stat(WANT);
|
||||
return st != null && now - st.mtime < WANT_FOR && st.mtime - now < WANT_FOR;
|
||||
};
|
||||
|
||||
/**
|
||||
* What the watcher wrote, for the RPC: { now, want: WANT_FOR, keep: POINTS,
|
||||
* ts (newest point, 0 for none), points } with only the points newer than
|
||||
* `since` when it is given (a page that polls asks for what it lacks).
|
||||
* No points when nobody asked within WANT_FOR seconds or the agent is not
|
||||
* running: FILE is gone (the agent removes it as it stops and starts), or,
|
||||
* from one that died while someone looked, its newest point is over
|
||||
* WANT_FOR seconds old (ts then stops moving).
|
||||
*/
|
||||
export function read(since) {
|
||||
let f = read_json(FILE, null);
|
||||
let ts = +(f?.ts ?? 0);
|
||||
let pts = type(f?.points) == 'array' && time() - ts <= WANT_FOR ? f.points : [];
|
||||
if (since > 0)
|
||||
pts = filter(pts, (p) => p.ts > since);
|
||||
return { now: time(), want: WANT_FOR, keep: POINTS, ts: ts, points: pts };
|
||||
};
|
||||
|
||||
/** Monotonic seconds, with the fraction: the gap between two samples. */
|
||||
function mono() {
|
||||
let c = clock(true);
|
||||
return c[0] + c[1] / 1e9;
|
||||
}
|
||||
|
||||
/**
|
||||
* One sample at `now` from the dump the watcher just read (`d`): { t, ts,
|
||||
* servers: [ server id ] (those whose tunnel was read), dev: { device id:
|
||||
* [ rx, tx, server id ] } (its peer's counters, on those servers) }. Nothing
|
||||
* but the dump is read.
|
||||
*/
|
||||
export function sample(m, d, now) {
|
||||
let s = { t: mono(), ts: now, servers: [], dev: {} };
|
||||
let read = {};
|
||||
for (let sid, srv in m.servers)
|
||||
if (d.reads[srv.ifname] == 'read') {
|
||||
read[sid] = true;
|
||||
push(s.servers, sid);
|
||||
}
|
||||
for (let id, dev in m.devices) {
|
||||
let peer = d.peers[dev.public_key];
|
||||
let sid = m.people[dev.person]?.server;
|
||||
if (peer && read[sid])
|
||||
s.dev[id] = [ peer.rx, peer.tx, sid ];
|
||||
}
|
||||
return s;
|
||||
};
|
||||
|
||||
/** A counter's change, 0 when it went down (a tunnel set up again starts from zero). */
|
||||
function delta(a, b) {
|
||||
return (b ?? 0) >= (a ?? 0) ? (b ?? 0) - (a ?? 0) : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* The point between samples `a` and `b`, null when they are not a second or
|
||||
* so apart (the first sample, a stalled watcher): { ts, servers: { server
|
||||
* id: [ rx, tx ] }, dev: { device id: [ rx, tx ] } }, bytes per second of
|
||||
* each server's devices together (rx what came from them, tx what went to
|
||||
* them, as a server's per-minute `rate`), for every server whose tunnel `b`
|
||||
* read ([ 0, 0 ] when nothing moved), and of each device that moved
|
||||
* something (one left out moved nothing, while its server was read). A
|
||||
* device is counted from its second sample on, on the server it was on in both.
|
||||
*/
|
||||
export function point(a, b) {
|
||||
let dt = b.t - a.t;
|
||||
if (!(dt > 0.2 && dt < 5))
|
||||
return null;
|
||||
let sum = {}, dev = {};
|
||||
for (let sid in b.servers)
|
||||
sum[sid] = [ 0, 0 ];
|
||||
for (let id, v in b.dev) {
|
||||
let o = a.dev[id];
|
||||
if (!o || o[2] != v[2])
|
||||
continue;
|
||||
let rx = delta(o[0], v[0]), tx = delta(o[1], v[1]);
|
||||
sum[v[2]][0] += rx;
|
||||
sum[v[2]][1] += tx;
|
||||
if (rx || tx)
|
||||
dev[id] = [ int(rx / dt), int(tx / dt) ];
|
||||
}
|
||||
let p = { ts: b.ts, servers: {}, dev };
|
||||
for (let sid, v in sum)
|
||||
p.servers[sid] = [ int(v[0] / dt), int(v[1] / dt) ];
|
||||
return p;
|
||||
};
|
||||
|
||||
/**
|
||||
* Keep point `p` in `ring` ([ [ ts, the point as JSON ] ], newest last; each
|
||||
* point is turned into text once, when it is made, not again every second for
|
||||
* as long as it is kept): at most POINTS of them, none older than POINTS
|
||||
* seconds before `p` (the watcher may have stalled in between) and none from
|
||||
* its second or later (the clock went back).
|
||||
*/
|
||||
export function keep(ring, p) {
|
||||
while (length(ring) && ring[length(ring) - 1][0] >= p.ts)
|
||||
pop(ring);
|
||||
push(ring, [ p.ts, sprintf('%J', p) ]);
|
||||
while (length(ring) > POINTS || ring[0][0] <= p.ts - POINTS)
|
||||
shift(ring);
|
||||
};
|
||||
|
||||
/** Write the points kept in `ring` (keep()) to FILE. */
|
||||
export function save(ring) {
|
||||
let ts = length(ring) ? ring[length(ring) - 1][0] : 0;
|
||||
ensure_dir(RUN_DIR);
|
||||
write_file(FILE, `{"ts":${ts},"points":[${join(',', map(ring, (r) => r[1]))}]}`, 0o644);
|
||||
};
|
||||
|
||||
/** Nobody looks any more: FILE goes (the next look gets the points kept then). */
|
||||
export function stop() {
|
||||
if (stat(FILE))
|
||||
unlink(FILE);
|
||||
};
|
||||
@@ -1,114 +0,0 @@
|
||||
// Dawnline — one-time steps for routers set up by older versions. Each one
|
||||
// checks first and writes nothing when there is nothing to do; dawnline.apply
|
||||
// runs them before it takes its own lock (changes to /etc/config/dawnline go
|
||||
// through dawnline.store and its config lock).
|
||||
|
||||
import { cursor } from 'uci';
|
||||
import { stat, unlink } from 'fs';
|
||||
import { slog, syslog } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as store from 'dawnline.store';
|
||||
import { L_ } from 'dawnline.i18n';
|
||||
|
||||
const LAN_CGI = '/www/cgi-bin/dawnline-portal';
|
||||
const LAN_CGI_URL = '/cgi-bin/dawnline-portal';
|
||||
|
||||
/** Every personal code is a word code; older random ones are replaced (their links stop working). */
|
||||
function word_codes() {
|
||||
let old = (s) => s.code != null && !match(s.code, core.WORD_CODE);
|
||||
let c = cursor();
|
||||
c.load('dawnline');
|
||||
let any = false;
|
||||
c.foreach('dawnline', 'person', (s) => {
|
||||
if (old(s))
|
||||
any = true;
|
||||
});
|
||||
if (!any)
|
||||
return;
|
||||
store.transact((w, m) => {
|
||||
w.foreach('dawnline', 'person', (s) => {
|
||||
if (old(s)) {
|
||||
w.set('dawnline', s['.name'], 'code', core.new_code());
|
||||
w.delete('dawnline', s['.name'], 'words');
|
||||
}
|
||||
});
|
||||
});
|
||||
slog('dawnline', 'info', L_('moved to word codes'), null, L_('Personal links'));
|
||||
}
|
||||
|
||||
/*
|
||||
* Older versions answered on 443 at home through LuCI's web server (its error
|
||||
* page ran the portal CGI). The portal now keeps its own port: remove that.
|
||||
*/
|
||||
export function uhttpd() {
|
||||
let c = cursor();
|
||||
c.load('uhttpd');
|
||||
if (c.get('uhttpd', 'main', 'error_page') == LAN_CGI_URL) {
|
||||
c.delete('uhttpd', 'main', 'error_page');
|
||||
c.commit('uhttpd');
|
||||
system('/etc/init.d/uhttpd reload >/dev/null 2>&1');
|
||||
syslog('info', 'portal', 'portal removed from the LuCI web server');
|
||||
}
|
||||
if (stat(LAN_CGI))
|
||||
unlink(LAN_CGI);
|
||||
// The old setting itself.
|
||||
let d = cursor();
|
||||
d.load('dawnline');
|
||||
if (d.get('dawnline', 'settings', 'portal_443') != null)
|
||||
store.transact((w, m) => w.delete('dawnline', 'settings', 'portal_443'));
|
||||
};
|
||||
|
||||
/** The portal names itself after the server now (the breadcrumb, the tab): its own title setting is gone. */
|
||||
function portal_title() {
|
||||
let c = cursor();
|
||||
c.load('dawnline');
|
||||
if (c.get('dawnline', 'settings', 'portal_title') != null)
|
||||
store.transact((w, m) => w.delete('dawnline', 'settings', 'portal_title'));
|
||||
}
|
||||
|
||||
/*
|
||||
* Steps that cannot tell from the config whether they ran are recorded in
|
||||
* `settings.migrated` (a list), so they run exactly once.
|
||||
*/
|
||||
function migrated(c, name) {
|
||||
let done = c.get('dawnline', 'settings', 'migrated') ?? [];
|
||||
return index(type(done) == 'array' ? done : [ done ], name) >= 0;
|
||||
}
|
||||
|
||||
function mark_migrated(w, name) {
|
||||
let done = w.get('dawnline', 'settings', 'migrated') ?? [];
|
||||
w.set('dawnline', 'settings', 'migrated', [ ...(type(done) == 'array' ? done : [ done ]), name ]);
|
||||
}
|
||||
|
||||
/*
|
||||
* 1380 was every server's stored MTU before it became automatic (by device
|
||||
* type and disguise). Once only: an admin may choose 1380 again on purpose.
|
||||
*/
|
||||
function auto_mtu() {
|
||||
let c = cursor();
|
||||
c.load('dawnline');
|
||||
if (migrated(c, 'auto_mtu'))
|
||||
return;
|
||||
let any = false;
|
||||
c.foreach('dawnline', 'server', (s) => {
|
||||
if (s.mtu == '1380')
|
||||
any = true;
|
||||
});
|
||||
store.transact((w, m) => {
|
||||
w.foreach('dawnline', 'server', (s) => {
|
||||
if (s.mtu == '1380')
|
||||
w.delete('dawnline', s['.name'], 'mtu');
|
||||
});
|
||||
mark_migrated(w, 'auto_mtu');
|
||||
});
|
||||
if (any)
|
||||
slog('dawnline', 'info', L_('MTU is now automatic'), null, L_('Servers'));
|
||||
}
|
||||
|
||||
/** All of them, in order. */
|
||||
export function run() {
|
||||
word_codes();
|
||||
uhttpd();
|
||||
auto_mtu();
|
||||
portal_title();
|
||||
};
|
||||
@@ -1,308 +0,0 @@
|
||||
// Dawnline — the router's network interfaces: what exists, which one is the
|
||||
// WAN, and which ones make up a server's "home network".
|
||||
//
|
||||
// Per server (uci `server` section):
|
||||
// auto_wan 1 = use the interface of the default route (detected_wan
|
||||
// is kept as a fallback while the WAN is down)
|
||||
// wan_device the WAN interface when auto_wan is off
|
||||
// auto_lan 1 = include the detected LAN (detected_lan as fallback)
|
||||
// lan_device extra interfaces that count as the home network (list)
|
||||
//
|
||||
// Home access is enforced by destination: the networks routed out of the
|
||||
// chosen interfaces form the server's nft `home_<id>` set, and the tunnel's
|
||||
// firewall zone gets a forwarding to each of their zones.
|
||||
|
||||
import { readfile, readlink, lsdir, stat } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import { sh_out, ip2int, int2ip, cidr_any, uniq } from 'dawnline.util';
|
||||
|
||||
const SYS = '/sys/class/net';
|
||||
/** Links Dawnline creates itself (dawnline.loop); never offered as interfaces. */
|
||||
export const INTERNAL = { transit: 'dlx0', lan: 'dll0' };
|
||||
const VPN_PROTOS = [ 'amneziawg', 'wireguard', 'openvpn', 'openconnect', 'vpnc', 'pptp', 'l2tp', 'gre', 'gretap', 'vti', 'xfrm' ];
|
||||
const VPN_NAMES = /^(wg|awg|tun|tap|ovpn|ipsec|vti|xfrm)/;
|
||||
// Never the WAN while another default route exists: VPN clients. (PPTP and
|
||||
// L2TP stay eligible: some ISPs connect the WAN with them.)
|
||||
const NOT_WAN_PROTOS = [ 'amneziawg', 'wireguard', 'openvpn', 'openconnect', 'vpnc' ];
|
||||
const NOT_WAN_NAMES = /^(wg|awg|tun|tap|ovpn)/;
|
||||
// Kernel placeholder devices that never carry a network on their own.
|
||||
const NOISE = /^(ifb|teql|sit|ip6tnl|gre|gretap|erspan|ip6gre|ip6gretap|ip_vti|ip6_vti|tunl|bond)[0-9]*$/;
|
||||
const PRIVATE = [ [ '10.0.0.0', 8 ], [ '172.16.0.0', 12 ], [ '192.168.0.0', 16 ], [ '100.64.0.0', 10 ] ];
|
||||
|
||||
function sysread(dev, f) {
|
||||
return trim(readfile(`${SYS}/${dev}/${f}`) ?? '');
|
||||
}
|
||||
|
||||
function ip_json(args) {
|
||||
try {
|
||||
return json(sh_out(`ip -j ${args} 2>/dev/null`) || '[]') ?? [];
|
||||
}
|
||||
catch (e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function ubus_json(path, method) {
|
||||
try {
|
||||
return json(sh_out(`ubus call ${path} ${method} 2>/dev/null`) || 'null');
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** "a.b.c.d[/len]" → { net, prefix, size } for any prefix 1-32; null for default or junk. */
|
||||
function net_of(s) {
|
||||
let c = cidr_any(s);
|
||||
return c && c.prefix >= 1 ? c : null;
|
||||
}
|
||||
|
||||
function net_str(n) {
|
||||
return `${int2ip(n.net)}/${n.prefix}`;
|
||||
}
|
||||
|
||||
/** Drop networks already covered by a wider one. */
|
||||
export function collapse(list) {
|
||||
let nets = filter(map(uniq(list), net_of), (x) => x != null);
|
||||
nets = sort(nets, (a, b) => a.prefix - b.prefix || a.net - b.net);
|
||||
let keep = [];
|
||||
for (let n in nets) {
|
||||
let inside = false;
|
||||
for (let k in keep)
|
||||
if (n.net >= k.net && n.net < k.net + k.size) {
|
||||
inside = true;
|
||||
break;
|
||||
}
|
||||
if (!inside)
|
||||
push(keep, n);
|
||||
}
|
||||
return map(sort(keep, (a, b) => a.net - b.net), net_str);
|
||||
};
|
||||
|
||||
function is_private(cidr) {
|
||||
let n = net_of(cidr);
|
||||
if (!n)
|
||||
return false;
|
||||
for (let p in PRIVATE) {
|
||||
let base = ip2int(p[0]), size = 1 << (32 - p[1]);
|
||||
if (n.net >= base && n.net < base + size)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* ---------- snapshot ---------- */
|
||||
|
||||
let cache = null;
|
||||
|
||||
/**
|
||||
* One consistent view of the router's interfaces, cached for a few seconds
|
||||
* (rpcd keeps this module loaded, so the cache must expire).
|
||||
*/
|
||||
export function snapshot(fresh) {
|
||||
if (!fresh && cache && time() - cache.ts < 5)
|
||||
return cache;
|
||||
|
||||
// Networks reachable through each device (main table, IPv4).
|
||||
let routes = {}, defaults = [];
|
||||
for (let r in ip_json('-4 route show table main')) {
|
||||
if (!r.dev)
|
||||
continue;
|
||||
if (r.dst == 'default') {
|
||||
push(defaults, r);
|
||||
continue;
|
||||
}
|
||||
push(routes[r.dev] ??= [], r.dst);
|
||||
}
|
||||
let addrs = {};
|
||||
for (let l in ip_json('-4 addr show'))
|
||||
for (let a in l.addr_info ?? [])
|
||||
if (a.family == 'inet')
|
||||
push(addrs[l.ifname] ??= [], `${a.local}/${a.prefixlen}`);
|
||||
|
||||
// Logical (netifd) interfaces by device, and firewall zones by network.
|
||||
let logical = {};
|
||||
for (let i in ubus_json('network.interface', 'dump')?.interface ?? []) {
|
||||
let dev = i.l3_device ?? i.device;
|
||||
if (dev && !logical[dev])
|
||||
logical[dev] = { name: i.interface, proto: i.proto, up: i.up };
|
||||
}
|
||||
// The WAN: the default route with the lowest metric that does not go into
|
||||
// a VPN client (its default route is not where the tunnels listen); only
|
||||
// VPN routes: the lowest of them.
|
||||
let vpn = (dev) => index(NOT_WAN_PROTOS, logical[dev]?.proto) >= 0 || match(dev, NOT_WAN_NAMES) != null;
|
||||
let wan_dev = null, wan_metric = null, wan_vpn = true;
|
||||
for (let r in defaults) {
|
||||
let v = vpn(r.dev), metric = r.metric ?? 0;
|
||||
if (wan_dev == null || (wan_vpn && !v) || (wan_vpn == v && metric < wan_metric)) {
|
||||
wan_dev = r.dev;
|
||||
wan_metric = metric;
|
||||
wan_vpn = v;
|
||||
}
|
||||
}
|
||||
let zone_by_net = {}, zone_by_dev = {}, zones = [];
|
||||
let fc = cursor();
|
||||
fc.load('firewall');
|
||||
fc.foreach('firewall', 'zone', (z) => {
|
||||
if (!z.name)
|
||||
return;
|
||||
push(zones, z.name);
|
||||
for (let n in (type(z.network) == 'array' ? z.network : split(z.network ?? '', /\s+/)))
|
||||
if (n != '')
|
||||
zone_by_net[n] ??= z.name;
|
||||
for (let d in (type(z.device) == 'array' ? z.device : split(z.device ?? '', /\s+/)))
|
||||
if (d != '')
|
||||
zone_by_dev[d] ??= z.name;
|
||||
});
|
||||
|
||||
let devices = {};
|
||||
for (let dev in sort(lsdir(SYS) ?? [])) {
|
||||
if (dev == 'lo' || dev == INTERNAL.transit || dev == INTERNAL.lan)
|
||||
continue;
|
||||
let lg = logical[dev];
|
||||
let flags = hex(sysread(dev, 'flags') || '0');
|
||||
let kind = +(sysread(dev, 'type') || '0');
|
||||
let master = readlink(`${SYS}/${dev}/master`);
|
||||
master = master ? substr(master, rindex(master, '/') + 1) : null;
|
||||
let bridge = stat(`${SYS}/${dev}/bridge`) != null;
|
||||
let wifi = stat(`${SYS}/${dev}/phy80211`) != null || stat(`${SYS}/${dev}/wireless`) != null;
|
||||
let nets = routes[dev] ? collapse(routes[dev]) : [];
|
||||
let up = (flags & 1) != 0;
|
||||
if (match(dev, NOISE) && !length(nets))
|
||||
continue;
|
||||
let cat;
|
||||
if (wifi)
|
||||
cat = 'wifi';
|
||||
else if ((lg && index(VPN_PROTOS, lg.proto) >= 0) || kind == 65534 || ((flags & 0x10) && kind != 1) || match(dev, VPN_NAMES))
|
||||
cat = 'vpn';
|
||||
else if (kind == 1 || bridge)
|
||||
cat = 'ethernet';
|
||||
else
|
||||
cat = 'other';
|
||||
devices[dev] = {
|
||||
name: dev,
|
||||
category: dev == wan_dev ? 'wan' : cat,
|
||||
kind: bridge ? 'bridge' : cat,
|
||||
up,
|
||||
master,
|
||||
members: [],
|
||||
nets,
|
||||
addrs: addrs[dev] ?? [],
|
||||
logical: lg?.name,
|
||||
proto: lg?.proto,
|
||||
zone: zone_by_dev[dev] ?? (lg ? zone_by_net[lg.name] : null),
|
||||
};
|
||||
}
|
||||
for (let dev, d in devices)
|
||||
if (d.master && devices[d.master])
|
||||
push(devices[d.master].members, dev);
|
||||
|
||||
// The LAN: netifd's "lan" if present, otherwise the first bridge (then
|
||||
// ethernet device) carrying a private network.
|
||||
let lan_dev = null;
|
||||
for (let dev, lg in logical)
|
||||
if (lg.name == 'lan' && devices[dev])
|
||||
lan_dev = dev;
|
||||
if (!lan_dev)
|
||||
for (let pass in [ 'bridge', 'ethernet' ])
|
||||
for (let dev, d in devices)
|
||||
if (!lan_dev && dev != wan_dev && d.kind == pass && !d.master && length(filter(d.nets, is_private)))
|
||||
lan_dev = dev;
|
||||
|
||||
cache = { ts: time(), devices, wan: wan_dev, lan: lan_dev, zones };
|
||||
return cache;
|
||||
};
|
||||
|
||||
/* ---------- per server ---------- */
|
||||
|
||||
/**
|
||||
* What a server actually uses right now:
|
||||
* wan: { dev, zone, auto, missing }
|
||||
* home: [{ dev, auto, nets, zone, missing }]
|
||||
* nets: home networks (collapsed), zones: their firewall zones
|
||||
*/
|
||||
export function server_net(srv, snap) {
|
||||
snap ??= snapshot();
|
||||
let devs = snap.devices;
|
||||
|
||||
let wan_dev = srv.auto_wan ? (snap.wan ?? srv.detected_wan) : srv.wan_device;
|
||||
let wan = {
|
||||
dev: wan_dev || null,
|
||||
auto: !!srv.auto_wan,
|
||||
zone: devs[wan_dev]?.zone ?? srv.wan_zone ?? 'wan',
|
||||
missing: !!wan_dev && !devs[wan_dev],
|
||||
};
|
||||
|
||||
let picked = [];
|
||||
let auto_lan = srv.auto_lan ? (snap.lan ?? srv.detected_lan) : null;
|
||||
if (auto_lan)
|
||||
push(picked, [ auto_lan, true ]);
|
||||
for (let d in srv.lan_devices ?? [])
|
||||
if (d != auto_lan)
|
||||
push(picked, [ d, false ]);
|
||||
|
||||
let home = [], nets = [], zones = [];
|
||||
for (let p in picked) {
|
||||
let dev = p[0];
|
||||
// Never the WAN, the tunnel itself, or a bridge port (routing only sees the bridge).
|
||||
if (dev == wan.dev || dev == srv.ifname || devs[dev]?.master)
|
||||
continue;
|
||||
let d = devs[dev];
|
||||
// The detected LAN missing for a moment (bridge recreated) keeps its zone,
|
||||
// so its forwardings are not removed meanwhile.
|
||||
let h = { dev, auto: p[1], nets: d?.nets ?? [], zone: d?.zone ?? (p[1] ? srv.lan_zone : null), missing: !d };
|
||||
push(home, h);
|
||||
push(nets, ...h.nets);
|
||||
if (h.zone)
|
||||
push(zones, h.zone);
|
||||
}
|
||||
// Older configs and routers where nothing could be resolved: the zone set at setup.
|
||||
if (!length(home) && srv.auto_lan && srv.lan_zone)
|
||||
push(zones, srv.lan_zone);
|
||||
return { wan, home, nets: collapse(nets), zones: uniq(zones) };
|
||||
};
|
||||
|
||||
/** Router address on the home network (DNS for tunnel clients). */
|
||||
export function home_address(srv, snap) {
|
||||
snap ??= snapshot();
|
||||
let sn = server_net(srv, snap);
|
||||
for (let h in sn.home)
|
||||
for (let a in snap.devices[h.dev]?.addrs ?? [])
|
||||
return split(a, '/')[0];
|
||||
return null;
|
||||
};
|
||||
|
||||
/** WAN IPv4 address of a server's WAN interface. */
|
||||
export function wan_address(srv, snap) {
|
||||
snap ??= snapshot();
|
||||
let a = snap.devices[server_net(srv, snap).wan.dev]?.addrs?.[0];
|
||||
return a ? split(a, '/')[0] : null;
|
||||
};
|
||||
|
||||
/** Names of devices at home by address: DHCP leases, then static hosts. */
|
||||
export function lan_names() {
|
||||
let out = {};
|
||||
for (let line in split(readfile('/tmp/dhcp.leases') ?? '', '\n')) {
|
||||
let f = split(line, ' ');
|
||||
if (length(f) >= 4 && f[3] != '*')
|
||||
out[f[2]] = f[3];
|
||||
}
|
||||
let c = cursor();
|
||||
c.foreach('dhcp', 'host', (h) => {
|
||||
if (h.ip && h.name)
|
||||
out[h.ip] ??= h.name;
|
||||
});
|
||||
return out;
|
||||
};
|
||||
|
||||
// A module constant, compiled once: event_hosts runs for every event of the log.
|
||||
const ANY_IPV4 = /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/g;
|
||||
|
||||
/** Event addresses that belong to devices at home, with their names ({ ip: name }). */
|
||||
export function event_hosts(e, names) {
|
||||
let out = null;
|
||||
for (let ip in match(e.detail ?? '', ANY_IPV4) ?? [])
|
||||
if (names[ip[0]])
|
||||
(out ??= {})[ip[0]] = names[ip[0]];
|
||||
return out;
|
||||
};
|
||||
@@ -1,185 +0,0 @@
|
||||
// Dawnline — netifd: one AmneziaWG interface per server, named after its
|
||||
// ifname and marked `_dawnline` = server id. Only interfaces Dawnline created
|
||||
// are changed or removed: a name already used by another interface (`lan`, a
|
||||
// tunnel of your own, one from an imported config) is left alone and the
|
||||
// server is reported instead. Why a server is kept down is kept in
|
||||
// core.BLOCKED as a code with its values (awg.blocked_text says it).
|
||||
// drift() tells the watchdog when one of them was edited outside Dawnline.
|
||||
|
||||
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, 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';
|
||||
|
||||
// Set while AmneziaWG is missing and that was logged (once until it is back).
|
||||
const NO_AWG = `${RUN_DIR}/netifd-no-awg`;
|
||||
|
||||
function log(who, msg, level, detail) {
|
||||
slog('dawnline', level ?? 'info', msg, detail, who);
|
||||
}
|
||||
|
||||
/** The server id that owns network.<ifname>: null when the section is free, '' when someone else's. */
|
||||
export function owner(ifname, c) {
|
||||
if (!c) {
|
||||
c = cursor();
|
||||
c.load('network');
|
||||
}
|
||||
if (c.get('network', ifname) == null)
|
||||
return null;
|
||||
return c.get('network', ifname, '_dawnline') ?? '';
|
||||
};
|
||||
|
||||
/** What sync() writes into network.<ifname> apart from the markers: what netifd sets the tunnel up from. */
|
||||
function options(srv) {
|
||||
let o = awg.netifd_options(srv);
|
||||
o.disabled = srv.enabled ? null : '1';
|
||||
o.auto = srv.enabled ? null : '0';
|
||||
return o;
|
||||
}
|
||||
|
||||
/** Bring network config in line with the servers; true when it changed. */
|
||||
export function sync(m) {
|
||||
let c = cursor();
|
||||
c.load('network');
|
||||
let changed = false, touched = [], want = {};
|
||||
// Servers whose interface can't be set up (core.BLOCKED), each logged once while it lasts.
|
||||
let was = read_json(core.BLOCKED, {}), blocked = {};
|
||||
let block = (srv, why, detail) => {
|
||||
blocked[srv.id] = why;
|
||||
// The rest of this apply works from the same model: no tunnel for it there either.
|
||||
srv.blocked = why;
|
||||
if (sprintf('%J', was[srv.id]) != sprintf('%J', why))
|
||||
log(srv.name, awg.blocked_note(why), 'error', detail);
|
||||
};
|
||||
|
||||
// Without amneziawg-tools there is no protocol script: changing or bringing
|
||||
// up an interface would only stop a tunnel that is still running.
|
||||
let tools = awg.state().tools;
|
||||
if (tools && stat(NO_AWG))
|
||||
unlink(NO_AWG);
|
||||
else if (!tools && length(filter(values(m.servers), (s) => s.private_key)) && !stat(NO_AWG)) {
|
||||
log('AmneziaWG', L_("isn't installed: tunnels were left as they are"), 'error', L_('Install it in Dawnline → Components.'));
|
||||
ensure_dir(RUN_DIR);
|
||||
writefile(NO_AWG, `${time()}`);
|
||||
}
|
||||
|
||||
for (let id, srv in m.servers) {
|
||||
if (!srv.private_key)
|
||||
continue;
|
||||
let ifn = srv.ifname;
|
||||
if (!core.valid_ifname(ifn)) {
|
||||
block(srv, { code: 'ifname', ifname: ifn }, 'Interface name: 1-12 lowercase letters, digits or _.');
|
||||
continue;
|
||||
}
|
||||
let own = owner(ifn, c);
|
||||
if (own != null && own != id) {
|
||||
block(srv, { code: 'taken', ifname: ifn },
|
||||
'Dawnline never changes interfaces it did not create. Choose another interface name for this server.');
|
||||
continue;
|
||||
}
|
||||
// Disguise values AmneziaWG refuses or crashes on (a hand edit in uci;
|
||||
// import and restore refuse them) never reach setconf and the kernel.
|
||||
// A disguise this build cannot run (a 3.1 preset after a rollback to
|
||||
// 2.0): the kernel would drop those options silently and run a tunnel
|
||||
// no device can reach. Down, and said so, until the build or the
|
||||
// disguise is changed.
|
||||
let why = !tools ? null : length(awg.obf_unsafe(srv.obf)) ? { code: 'disguise_invalid' } : awg.obf_unsupported(srv.obf);
|
||||
if (why) {
|
||||
block(srv, why, awg.blocked_fix(why));
|
||||
continue;
|
||||
}
|
||||
delete srv.blocked;
|
||||
want[ifn] = true;
|
||||
if (!tools)
|
||||
continue;
|
||||
// Per server: with several, each changed one has to be brought up again.
|
||||
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)) {
|
||||
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))
|
||||
changed = true;
|
||||
// For the log once the server is gone.
|
||||
if (uci_put(c, 'network', ifn, '_dawnline_name', srv.name))
|
||||
changed = true;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (sprintf('%J', blocked) != sprintf('%J', was)) {
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(core.BLOCKED, blocked, 0o644);
|
||||
}
|
||||
|
||||
// Remove interfaces Dawnline created for servers that no longer exist.
|
||||
let stale = [];
|
||||
c.foreach('network', 'interface', (s) => {
|
||||
if (s._dawnline && !want[s['.name']])
|
||||
push(stale, s['.name']);
|
||||
});
|
||||
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;
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
c.commit('network');
|
||||
ack_config([ 'network' ]);
|
||||
}
|
||||
// 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) {
|
||||
syslog('info', 'network', `ifup ${ifn}`);
|
||||
system(`ifup ${q(ifn)}`);
|
||||
}
|
||||
// Each tunnel's own line (restarting, turned off, removed) comes from hotplug.
|
||||
}
|
||||
return changed;
|
||||
};
|
||||
|
||||
/**
|
||||
* For the watchdog: what of each running server's interface differs from
|
||||
* what sync() writes, by option name (a LuCI or uci edit of network.<ifname>,
|
||||
* which would otherwise last until the next apply): { server id: [ names ] },
|
||||
* an empty list when it is as Dawnline set it. Only enabled servers whose
|
||||
* section exists and is theirs (a missing one is the tunnel probe's); the
|
||||
* markers are not compared, netifd does not read them. A dry run on a
|
||||
* cursor of its own: nothing is saved. Meaningful while amneziawg-tools is
|
||||
* installed (sync() leaves the interfaces alone without it).
|
||||
*/
|
||||
export function drift(m) {
|
||||
let c = cursor();
|
||||
c.load('network');
|
||||
let out = {};
|
||||
for (let id, srv in m.servers) {
|
||||
if (!srv.enabled || !length(core.tunnels(srv)) || owner(srv.ifname, c) != id || c.get('network', srv.ifname) != 'interface')
|
||||
continue;
|
||||
let diff = [];
|
||||
for (let k, v in options(srv))
|
||||
if (uci_put(c, 'network', srv.ifname, k, v))
|
||||
push(diff, k);
|
||||
out[id] = diff;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
@@ -1,353 +0,0 @@
|
||||
// Dawnline — the nft table `inet dawnline`: each person's access, enforced
|
||||
// on the tunnel interfaces before anything else on the router sees the
|
||||
// traffic, and where tunnel DNS goes. Its shape follows the servers and their
|
||||
// home networks (rebuild); who is in which set follows people's state and
|
||||
// changes often (update, one atomic transaction).
|
||||
|
||||
import { unlink } from 'fs';
|
||||
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';
|
||||
|
||||
/** Private IPv4 ranges: internet people never reach them (awg.BYPASS4 is the rest). */
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Per-person runtime membership: which tunnel addresses may reach what.
|
||||
* `snap` is the network (net.snapshot() when not given).
|
||||
*/
|
||||
export function access_sets(m, usage, states, snap) {
|
||||
// lan_ok: home access (the router itself, the mesh); lan_all: every home
|
||||
// network; lan_part: device . network for people limited to some networks;
|
||||
// mesh_pairs: device . device within a circle (or one person's own devices);
|
||||
// svc_ok: device . service . port; svc_host: device . service with no ports (all of them).
|
||||
let sets = {};
|
||||
for (let k in SETS)
|
||||
sets[k] = [];
|
||||
snap ??= net.snapshot();
|
||||
let homes = {}, groups = {};
|
||||
for (let id, d in m.devices) {
|
||||
let p = m.people[d.person];
|
||||
// Off: paused, ended, over quota or out of hours. Addresses go into nft as they are: IPv4 only.
|
||||
if (!p || d.disabled || !ip4(d.address) || states[p.id] != 'active')
|
||||
continue;
|
||||
// Home names: the plain resolver (their internet does not use the tunnel).
|
||||
if (p.access == 'lan' && p.dns_mode == 'home')
|
||||
push(sets.dns_real, d.address);
|
||||
if (p.access == 'internet' || p.access == 'both')
|
||||
push(sets.inet_ok, d.address);
|
||||
if (p.access == 'both' || (p.access == 'lan' && !length(p.services))) {
|
||||
push(sets.lan_ok, d.address);
|
||||
let srv = m.servers[p.server];
|
||||
let h = homes[p.id] ??= srv ? core.person_home(srv, p, snap) : { all: true, nets: [] };
|
||||
if (h.all)
|
||||
push(sets.lan_all, d.address);
|
||||
else
|
||||
for (let n in h.nets)
|
||||
push(sets.lan_part, `${d.address} . ${n}`);
|
||||
// Who this device may reach directly: its circle, or at least its owner's devices.
|
||||
if (srv?.mesh == 'circles') {
|
||||
let ci = core.person_circle(m, p);
|
||||
push(groups[ci ? `c:${ci.id}` : `p:${p.id}`] ??= [], d.address);
|
||||
}
|
||||
}
|
||||
else if (p.access == 'lan')
|
||||
for (let sid in p.services) {
|
||||
let s = m.services[sid];
|
||||
if (!s || !ip4(s.address))
|
||||
continue;
|
||||
if (!length(s.ports))
|
||||
push(sets.svc_host, `${d.address} . ${s.address}`);
|
||||
for (let port in s.ports)
|
||||
if (match(port, /^[0-9]+$/))
|
||||
push(sets.svc_ok, `${d.address} . ${s.address} . ${port}`);
|
||||
}
|
||||
}
|
||||
for (let g, addrs in groups)
|
||||
for (let a in addrs)
|
||||
for (let b in addrs)
|
||||
if (a != b)
|
||||
push(sets.mesh_pairs, `${a} . ${b}`);
|
||||
return sets;
|
||||
};
|
||||
|
||||
// Duplicate members (two devices with one address) would make the whole load fail.
|
||||
function set_elems(arr) {
|
||||
arr = uniq(arr);
|
||||
return length(arr) ? `elements = { ${join(', ', arr)} }` : '';
|
||||
}
|
||||
|
||||
/** The portal's TCP ports people may reach through the tunnel (never SSH, DNS or LuCI, whatever is stored). */
|
||||
function portal_ports(m) {
|
||||
if (!m.settings.portal_enabled)
|
||||
return [];
|
||||
let ok = (p) => p >= 1 && p <= 65535 && index([ 22, 53, 80, 443 ], p) < 0;
|
||||
let ports = filter([ m.settings.portal_port ], ok);
|
||||
if (core.portal_http(m) && ok(core.portal_http_port(m)))
|
||||
push(ports, core.portal_http_port(m));
|
||||
return ports;
|
||||
}
|
||||
|
||||
/**
|
||||
* What the table is made from besides the model, read from the router: the
|
||||
* netns's DNS address (loop.client_dns), where the Home-only resolver runs
|
||||
* (loop.resolver_target), the LAN's DNS and the network snapshot.
|
||||
*/
|
||||
function router_ctx() {
|
||||
return { dns_addr: loop.client_dns(), resolver: loop.resolver_target(), lan_dns: loop.lan_dns(), snap: net.snapshot() };
|
||||
}
|
||||
|
||||
function table(m, sets, ctx) {
|
||||
ctx ??= router_ctx();
|
||||
let ifs = [], tun = [];
|
||||
let dns_addr = ctx.dns_addr;
|
||||
if (!ip4(dns_addr))
|
||||
dns_addr = null;
|
||||
let rt = ctx.resolver;
|
||||
let snap = ctx.snap;
|
||||
// Each server's home networks are its own: with several servers, "all
|
||||
// home" on one never reaches the other's networks (set lan_<id>).
|
||||
let homes = {};
|
||||
for (let id, srv in m.servers) {
|
||||
// Only valid interface names reach here (core.tunnels): they are written into the rules as strings.
|
||||
let tns = core.tunnels(srv);
|
||||
for (let t in tns) {
|
||||
push(ifs, `"${t.ifname}"`);
|
||||
push(tun, t.subnet);
|
||||
}
|
||||
if (length(tns))
|
||||
homes[id] = net.collapse(net.server_net(srv, snap).nets);
|
||||
}
|
||||
tun = net.collapse(tun);
|
||||
let t = [];
|
||||
push(t, 'table inet dawnline {');
|
||||
for (let id, nets in homes)
|
||||
push(t, ` set home_${id} { type ipv4_addr; flags interval; auto-merge; ${set_elems(nets)} }`);
|
||||
// Tunnel subnets are never "home", even inside a wider route to the LAN.
|
||||
push(t, ` set tun_nets { type ipv4_addr; flags interval; auto-merge; ${set_elems(tun)} }`);
|
||||
push(t, ` set lan_ok { type ipv4_addr; ${set_elems(sets.lan_ok)} }`);
|
||||
push(t, ` set lan_all { type ipv4_addr; ${set_elems(sets.lan_all)} }`);
|
||||
push(t, ` set lan_part { type ipv4_addr . ipv4_addr; flags interval; ${set_elems(sets.lan_part)} }`);
|
||||
push(t, ` set mesh_pairs { type ipv4_addr . ipv4_addr; ${set_elems(sets.mesh_pairs)} }`);
|
||||
push(t, ` set inet_ok { type ipv4_addr; ${set_elems(sets.inet_ok)} }`);
|
||||
push(t, ` set svc_ok { type ipv4_addr . ipv4_addr . inet_service; ${set_elems(sets.svc_ok)} }`);
|
||||
push(t, ` set svc_host { type ipv4_addr . ipv4_addr; ${set_elems(sets.svc_host)} }`);
|
||||
push(t, ` set dns_real { type ipv4_addr; ${set_elems(sets.dns_real)} }`);
|
||||
push(t, ' chain pre {');
|
||||
// Ahead of mangle-priority hooks (transparent proxies mark at -150).
|
||||
push(t, ' type filter hook prerouting priority -160; policy accept;');
|
||||
if (length(ifs))
|
||||
push(t, ` iifname { ${join(', ', ifs)} } jump guard`);
|
||||
push(t, ' }');
|
||||
push(t, ' chain guard {');
|
||||
push(t, ' meta nfproto ipv6 drop');
|
||||
// Every packet is judged by the rules below, not only the first of a
|
||||
// connection: a change of access cuts open connections too, instead of
|
||||
// letting a session or a long UDP flow run on for days. Only what has no
|
||||
// rule of its own passes on its connection: errors and the like, answers
|
||||
// to the admin's port forwards, and the router's own pings.
|
||||
push(t, ' ct state related accept');
|
||||
push(t, ' ct status dnat accept');
|
||||
push(t, ' icmp type { echo-request, echo-reply } fib daddr type local accept');
|
||||
push(t, ' fib daddr type local meta l4proto { tcp, udp } th dport 53 accept');
|
||||
let pp = portal_ports(m);
|
||||
if (length(pp))
|
||||
// Their own portal, also with the VPN on (the address may be this router's).
|
||||
push(t, ` fib daddr type local tcp dport { ${join(', ', pp)} } accept`);
|
||||
// The router itself is part of home: for everyone with all of it, and for
|
||||
// people limited to some networks only on the addresses in those.
|
||||
push(t, ' fib daddr type local ip saddr @lan_all accept');
|
||||
push(t, ' fib daddr type local ip saddr . ip daddr @lan_part accept');
|
||||
push(t, ' fib daddr type local counter drop');
|
||||
if (dns_addr)
|
||||
// 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`);
|
||||
for (let id, nets in homes)
|
||||
for (let tn in core.tunnels(m.servers[id]))
|
||||
push(t, ` iifname "${tn.ifname}" ip daddr @home_${id} ip daddr != @tun_nets ip saddr @lan_all accept`);
|
||||
push(t, ' ip saddr . ip daddr @lan_part ip daddr != @tun_nets accept');
|
||||
// Devices with home access are part of home: they reach each other, where the
|
||||
// server allows it (everyone, or within circles). Within one server: the
|
||||
// firewall has no forwarding between different servers' zones.
|
||||
for (let id, srv in m.servers)
|
||||
for (let tn in core.tunnels(srv))
|
||||
if (srv.mesh == 'all')
|
||||
push(t, ` iifname "${tn.ifname}" ip saddr @lan_ok ip daddr @lan_ok accept`);
|
||||
else if (srv.mesh == 'circles')
|
||||
push(t, ` iifname "${tn.ifname}" ip saddr . ip daddr @mesh_pairs accept`);
|
||||
push(t, ' meta l4proto { tcp, udp } ip saddr . ip daddr . th dport @svc_ok accept');
|
||||
push(t, ' ip saddr . ip daddr @svc_host 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');
|
||||
for (let id, nets in homes)
|
||||
for (let tn in core.tunnels(m.servers[id]))
|
||||
push(t, ` oifname "${tn.ifname}" ip saddr @home_${id} ip saddr != @tun_nets ip daddr @lan_all accept`);
|
||||
push(t, ' ip daddr . ip saddr @lan_part ip saddr != @tun_nets 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' && ip4(rt.address) ? `dnat ip to ${rt.address}:53` : `redirect to :${+rt.port}`;
|
||||
let lan_dns = ctx.lan_dns;
|
||||
if (!ip4(lan_dns))
|
||||
lan_dns = null;
|
||||
push(t, ' chain dns {');
|
||||
push(t, ' type nat hook prerouting priority dstnat - 5; policy accept;');
|
||||
if (length(ifs))
|
||||
// Home names (and no LAN DNS at all): the plain resolver, whatever address was asked.
|
||||
push(t, ` iifname { ${join(', ', ifs)} } ip saddr @dns_real meta l4proto { tcp, udp } th dport 53 ${to_resolver}`);
|
||||
push(t, ' }');
|
||||
// After the LAN's own DNS interceptors (transparent proxies redirect port 53
|
||||
// at dstnat - 1): only the first NAT of a connection counts, so a DNAT here
|
||||
// first would hand the query to the plain resolver behind their back. When
|
||||
// none of them takes the tunnel, this sends it to the LAN's DNS.
|
||||
push(t, ' chain dns_lan {');
|
||||
push(t, ' type nat hook prerouting priority dstnat + 5; policy accept;');
|
||||
for (let id, srv in m.servers)
|
||||
for (let tn in core.tunnels(srv)) {
|
||||
// Everyone else: the LAN's DNS, through the netns (from the LAN side) when internet goes that way.
|
||||
let target = srv.lan_like && dns_addr ? `dnat ip to ${dns_addr}:53` : lan_dns ? `dnat ip to ${lan_dns}:53` : to_resolver;
|
||||
push(t, ` iifname "${tn.ifname}" ip daddr ${tn.address} meta l4proto { tcp, udp } th dport 53 ${target}`);
|
||||
}
|
||||
push(t, ' }');
|
||||
push(t, '}');
|
||||
return join('\n', t) + '\n';
|
||||
}
|
||||
|
||||
/** Run an nft script (one transaction); null when it loaded, else what nft said. */
|
||||
export function load(script) {
|
||||
ensure_dir(RUN_DIR);
|
||||
// A file of its own: two writers must never load each other's script.
|
||||
let f = `${RUN_DIR}/nft.${rand_hex(4)}.rules`;
|
||||
if (!write_file(f, script))
|
||||
return `couldn't write ${f}`;
|
||||
let r = sh(`nft -f ${f}`);
|
||||
unlink(f);
|
||||
if (r.code == 0)
|
||||
return null;
|
||||
// Without the random file name, so the same failure reads the same every time.
|
||||
return replace(trim(r.out), f, 'rules') || `nft exited with ${r.code}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* The table as text, for a check (`nft -c -f`) or a test; nothing is loaded.
|
||||
* `ctx` (a test's) stands for what is read from the router: { dns_addr,
|
||||
* resolver, lan_dns, snap } (router_ctx).
|
||||
*/
|
||||
export function render(m, sets, ctx) {
|
||||
return table(m, sets, ctx);
|
||||
};
|
||||
|
||||
/** Full table rebuild (structure changes: servers, home networks, DNS paths); null or the error. */
|
||||
export function rebuild(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;
|
||||
};
|
||||
|
||||
/**
|
||||
* Replace the members of every runtime set at once; null or the error. Fails
|
||||
* as a whole when the table or one of its sets is missing (not loaded yet,
|
||||
* or an older shape): the caller then rebuilds.
|
||||
*/
|
||||
export function update(sets) {
|
||||
let s = '';
|
||||
for (let name in SETS) {
|
||||
let members = uniq(sets[name]);
|
||||
s += `flush set inet dawnline ${name}\n`;
|
||||
if (length(members))
|
||||
s += `add element inet dawnline ${name} { ${join(', ', members)} }\n`;
|
||||
}
|
||||
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). */
|
||||
export function present() {
|
||||
return system('nft list table inet dawnline >/dev/null 2>&1') == 0;
|
||||
};
|
||||
|
||||
export function remove() {
|
||||
system('nft delete table inet dawnline 2>/dev/null');
|
||||
};
|
||||
@@ -1,207 +0,0 @@
|
||||
// Dawnline — one interface over the router's package manager: opkg on
|
||||
// OpenWrt 24.10, apk on 25.12. Commands that change packages take a log(line)
|
||||
// callback, which gets their output line by line.
|
||||
|
||||
import { access, popen, stat } from 'fs';
|
||||
import { q } from 'dawnline.util';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
function run(cmd) {
|
||||
let p = popen(cmd + ' 2>&1', 'r');
|
||||
if (!p)
|
||||
return { code: 127, out: '' };
|
||||
let out = p.read('all') ?? '';
|
||||
return { code: p.close(), out };
|
||||
}
|
||||
|
||||
function logged(cmd, log) {
|
||||
let r = run(cmd);
|
||||
for (let line in split(trim(r.out), '\n'))
|
||||
if (length(trim(line)))
|
||||
log?.(` ${line}`);
|
||||
return r;
|
||||
}
|
||||
|
||||
export function is_apk() {
|
||||
return access('/usr/bin/apk', 'x');
|
||||
};
|
||||
|
||||
export function ext() {
|
||||
return is_apk() ? 'apk' : 'ipk';
|
||||
};
|
||||
|
||||
/** The installed version of a package, or null. */
|
||||
export function version(name) {
|
||||
let esc = replace(name, /[.+]/g, '\\$&');
|
||||
if (is_apk()) {
|
||||
let m = match(run(`apk list --installed ${q(name)}`).out, regexp(`^${esc}-([^ ]+) `));
|
||||
return m ? m[1] : null;
|
||||
}
|
||||
// Libraries carry their ABI in the name (libustream-openssl20201210).
|
||||
let re = regexp(`^${esc}([0-9]+)? - (.+)$`), found = null;
|
||||
for (let line in split(run('opkg list-installed').out, '\n')) {
|
||||
let m = match(trim(line), re);
|
||||
if (m && (found == null || m[1] == null))
|
||||
found = m;
|
||||
}
|
||||
return found ? trim(found[2]) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* The package a control text names: { name, version }, each null when it
|
||||
* does not say. Pure: an ipk's control file (ipk true), else `apk adbdump`
|
||||
* of an apk, whose first name and version are the package's own (its files
|
||||
* are "- name:" entries further down).
|
||||
*/
|
||||
export function control_info(text, ipk) {
|
||||
if (ipk)
|
||||
return { name: match(text, /(^|\n)Package: *([^\n]+)/)?.[2], version: match(text, /(^|\n)Version: *([^\n]+)/)?.[2] };
|
||||
return { name: match(text, /\n *name: *([^\n]+)/)?.[1], version: match(text, /\n *version: *([^\n]+)/)?.[1] };
|
||||
};
|
||||
|
||||
/** The package inside a file: { name, version }, each null when the file does not say. */
|
||||
export function file_info(path) {
|
||||
if (match(path, /\.ipk$/))
|
||||
return control_info(run(`tar -xzOf ${q(path)} ./control.tar.gz | tar -xzO ./control`).out, true);
|
||||
return control_info(run(`apk adbdump ${q(path)}`).out, false);
|
||||
};
|
||||
|
||||
/** The version inside a package file, or null. */
|
||||
export function file_version(path) {
|
||||
return file_info(path).version;
|
||||
};
|
||||
|
||||
/**
|
||||
* Compare two package versions the way the package manager does (so -r10 is
|
||||
* newer than -r9 and ~rc before the release): -1, 0 or 1; null when unknown.
|
||||
*/
|
||||
export function cmp(a, b) {
|
||||
if (a == null || b == null)
|
||||
return null;
|
||||
if (a == b)
|
||||
return 0;
|
||||
if (is_apk()) {
|
||||
let r = trim(run(`apk version -t ${q(a)} ${q(b)}`).out);
|
||||
return r == '<' ? -1 : r == '>' ? 1 : r == '=' ? 0 : null;
|
||||
}
|
||||
if (run(`opkg compare-versions ${q(a)} '<<' ${q(b)}`).code == 0)
|
||||
return -1;
|
||||
if (run(`opkg compare-versions ${q(a)} '>>' ${q(b)}`).code == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
};
|
||||
|
||||
export function refresh(log) {
|
||||
return logged(is_apk() ? 'apk update' : 'opkg update', log).code == 0;
|
||||
};
|
||||
|
||||
/** Free space on the overlay in KB (null when unknown). */
|
||||
function overlay_free_kb() {
|
||||
let out = run('df -Pk /overlay 2>/dev/null || df -Pk / 2>/dev/null').out;
|
||||
let m = match(out, /\n\S+\s+[0-9]+\s+[0-9]+\s+([0-9]+)\s/);
|
||||
return m ? +m[1] : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Room for `files` (or `need_kb` from the feeds): the package manager
|
||||
* unpacks before it removes, so a full flash leaves half an install behind.
|
||||
* Dies with the numbers.
|
||||
*/
|
||||
function preflight(files, need_kb, log) {
|
||||
let free = overlay_free_kb();
|
||||
if (free == null)
|
||||
return;
|
||||
let need = need_kb ?? 512;
|
||||
for (let f in files ?? [])
|
||||
need += int(((stat(f)?.size ?? 0) * 3) / 1024);
|
||||
if (free < need)
|
||||
die(sprintf(_('Not enough free space on the router: %d MB free, about %d MB needed. Remove old backups or packages first'),
|
||||
int(free / 1024), int(need / 1024) + 1));
|
||||
log?.(' ' + sprintf(_('%d MB free on the overlay'), int(free / 1024)));
|
||||
}
|
||||
|
||||
/** Dawnline's own package: a file of it tells whether apk really reinstalled it. */
|
||||
const SELF = 'luci-app-dawnline', SELF_MARKER = '/usr/share/ucode/dawnline/util.uc';
|
||||
|
||||
/**
|
||||
* The packages an apk install did not leave at their file's version: apk
|
||||
* takes a given file as it is (an older version too, a rollback) but leaves
|
||||
* an installed version alone, whatever the file. Pure: want [{ name,
|
||||
* version, file }] from the files (file_info()), have { name: installed
|
||||
* version }. [] when every package took.
|
||||
*/
|
||||
export function mismatched(want, have) {
|
||||
return filter(want, (w) => w.name == null || w.version == null || have?.[w.name] != w.version);
|
||||
};
|
||||
|
||||
/**
|
||||
* Package files (from a release): true when every one is installed. force
|
||||
* also reinstalls the same version or goes back to an older one (opkg; apk
|
||||
* takes a file as it is: Dawnline's own package is checked by a file of it,
|
||||
* any other by its installed version).
|
||||
*/
|
||||
export function install_files(files, force, log) {
|
||||
preflight(files, 0, log);
|
||||
let args = join(' ', map(files, q));
|
||||
if (!is_apk())
|
||||
return logged(`opkg install ${force ? '--force-reinstall --force-downgrade ' : ''}${args}`, log).code == 0;
|
||||
let want = map(files, (f) => ({ ...file_info(f), file: f }));
|
||||
let own = length(filter(want, (w) => w.name == SELF)) > 0;
|
||||
// Dawnline's own reinstall that changed nothing is a failure, not a silent "done".
|
||||
let before = own && force ? stat(SELF_MARKER)?.mtime : null;
|
||||
if (logged(`apk add --allow-untrusted ${args}`, log).code != 0)
|
||||
return false;
|
||||
if (own) {
|
||||
if (force && before != null && stat(SELF_MARKER)?.mtime == before) {
|
||||
log?.(' ' + _('apk did not reinstall the version that is already installed'));
|
||||
die(_("apk doesn't reinstall an installed version. Install a different release, or run install.sh on the router"));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
// Any other package took when it is at its file's version (the same
|
||||
// version again counts: apk keeps the installed one).
|
||||
let have = {};
|
||||
for (let w in want)
|
||||
if (w.name != null)
|
||||
have[w.name] = version(w.name);
|
||||
let bad = mismatched(want, have);
|
||||
for (let w in bad)
|
||||
log?.(' ' + sprintf(_('apk did not install %s %s (installed: %s)'), w.name ?? w.file, w.version ?? '?', have[w.name] ?? '-'));
|
||||
return !length(bad);
|
||||
};
|
||||
|
||||
/** Packages by name from the feeds. */
|
||||
export function add(names, log) {
|
||||
preflight(null, 2048, log);
|
||||
return logged(is_apk() ? `apk add ${join(' ', map(names, q))}` : `opkg install ${join(' ', map(names, q))}`, log).code == 0;
|
||||
};
|
||||
|
||||
export function upgrade(names, log) {
|
||||
preflight(null, 2048, log);
|
||||
return logged(is_apk() ? `apk upgrade ${join(' ', map(names, q))}` : `opkg upgrade ${join(' ', map(names, q))}`, log).code == 0;
|
||||
};
|
||||
|
||||
export function remove(names, log) {
|
||||
return logged(is_apk() ? `apk del ${join(' ', map(names, q))}` : `opkg remove ${join(' ', map(names, q))}`, log).code == 0;
|
||||
};
|
||||
|
||||
/** Installed packages with a newer version in the feeds: { name: latest }. */
|
||||
export function upgradable() {
|
||||
let up = {};
|
||||
if (is_apk()) {
|
||||
for (let line in split(run('apk version -l "<"').out, '\n')) {
|
||||
// "name-version-rN < newer": the version is what follows the last dash
|
||||
// before the release suffix (a name may hold digits, a version no dash).
|
||||
let m = match(line, /^(\S+)-([^- ]+-r[0-9]+)\s+<\s+(\S+)/);
|
||||
if (m)
|
||||
up[m[1]] = m[3];
|
||||
}
|
||||
}
|
||||
else
|
||||
for (let line in split(run('opkg list-upgradable').out, '\n')) {
|
||||
let m = match(line, /^(\S+) - (\S+) - (\S+)/);
|
||||
if (m)
|
||||
up[m[1]] = m[3];
|
||||
}
|
||||
return up;
|
||||
};
|
||||
@@ -1,624 +0,0 @@
|
||||
// Dawnline — the personal portal.
|
||||
//
|
||||
// GET /<code> the app shell
|
||||
// GET /<code>/assets/<file> its script and styles (gzipped when the browser takes it)
|
||||
// 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|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 —
|
||||
// the root, a person's link, the API and the assets alike — so nothing tells
|
||||
// a scanner there is a portal here. Wrong codes are rate-limited per client
|
||||
// (blocked clients get the same 404 even with the right code): per IPv4
|
||||
// address or IPv6 /64; behind Cloudflare per visitor (CF-Connecting-IP), with
|
||||
// a higher ceiling for all Cloudflare visitors together.
|
||||
// Regenerating the code in LuCI locks the old link out at once. POST bodies
|
||||
// must be JSON from the same origin; Referrer-Policy keeps the code out of
|
||||
// other sites' logs. The portal can only manage the person's own devices
|
||||
// within their limits, and not those the admin turned off (dawnline.devices).
|
||||
//
|
||||
// Changes through a link are limited too (a link is not a terminal: a few
|
||||
// dozen every quarter hour), and the configurations themselves (keys, the
|
||||
// disguise) only go over HTTPS: a plain-HTTP link (an IP address in Settings)
|
||||
// shows the page, but gets them from its HTTPS twin.
|
||||
//
|
||||
// Messages go out in the page's language (Accept-Language: the portal's
|
||||
// fetch sends the visitor's choice) when a Dawnline translation for it is
|
||||
// installed; else in English, and the page translates the ones it knows.
|
||||
//
|
||||
// handle() serves one request: `io` = { env, headers, read(n), write(s) }; the
|
||||
// CGI script (cgi.uc) is its wrapper, behind the shell door www/cgi-bin/portal
|
||||
// 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, 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';
|
||||
import * as i18n from 'dawnline.i18n';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
const RATE = `${RUN_DIR}/portal-rate.json`;
|
||||
const ASSETS = '/usr/share/dawnline/portal/assets';
|
||||
const TYPES = { js: 'text/javascript', css: 'text/css', png: 'image/png' };
|
||||
const FAIL_LIMIT = 8;
|
||||
const FAIL_WINDOW = 900;
|
||||
// All Cloudflare visitors together (three-word codes can't be guessed anyway).
|
||||
const SHARED_LIMIT = FAIL_LIMIT * 25;
|
||||
const SHARED_KEY = 'cloudflare';
|
||||
// Clients remembered at most (the oldest go first).
|
||||
const RATE_MAX = 2000;
|
||||
// Changes one link may make within FAIL_WINDOW (each one writes flash and syncs the tunnels).
|
||||
const ACTION_LIMIT = 40;
|
||||
const BODY_MAX = 8192;
|
||||
// The early end of a request (a response was written).
|
||||
const DONE = '__dawnline_done__';
|
||||
|
||||
const SECURITY_HEADERS = {
|
||||
'X-Content-Type-Options': 'nosniff',
|
||||
'Referrer-Policy': 'no-referrer',
|
||||
'X-Frame-Options': 'DENY',
|
||||
'Cache-Control': 'no-store',
|
||||
};
|
||||
|
||||
// States the admin stopped the person in: the portal only shows, it changes nothing.
|
||||
const LOCKED = [ 'paused', 'expired' ];
|
||||
|
||||
/** Why a stopped person (a LOCKED state) can't change anything, in the visitor's language; null for other states. */
|
||||
function locked_reason(st) {
|
||||
if (st == 'paused')
|
||||
return _('Your access is paused.');
|
||||
if (st == 'expired')
|
||||
return _('Your access has ended.');
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Why a new device or a new set-up is refused while the router has no AmneziaWG. */
|
||||
function not_ready() {
|
||||
return _("Adding devices isn't available right now: the VPN server isn't ready. Ask whoever sent you this link.");
|
||||
}
|
||||
|
||||
function html_escape(s) {
|
||||
return replace(`${s}`, /[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[c]);
|
||||
}
|
||||
|
||||
/* ---------- answering ---------- */
|
||||
|
||||
function headers(r, status, extra) {
|
||||
let h = `Status: ${status}\r\n`;
|
||||
let all = { ...SECURITY_HEADERS, ...(extra ?? {}) };
|
||||
// HSTS pins the whole name: only when the visitor is on the standard port.
|
||||
if (r.hsts)
|
||||
all['Strict-Transport-Security'] = 'max-age=31536000';
|
||||
for (let k, v in all)
|
||||
h += `${k}: ${v}\r\n`;
|
||||
r.io.write(h + '\r\n');
|
||||
}
|
||||
|
||||
function finish() {
|
||||
die(DONE);
|
||||
}
|
||||
|
||||
function send_json(r, status, obj) {
|
||||
headers(r, status, { 'Content-Type': 'application/json' });
|
||||
r.io.write(sprintf('%J', obj));
|
||||
finish();
|
||||
}
|
||||
|
||||
function fail(r, status, msg) {
|
||||
send_json(r, status, { error: msg });
|
||||
}
|
||||
|
||||
/**
|
||||
* The one answer for anything without a valid code: a bare 404, or the redirect from Settings.
|
||||
* Files always get the 404: a missing asset must not turn into someone's website.
|
||||
*/
|
||||
function not_found(r) {
|
||||
let redirect = r.m.settings.portal_redirect;
|
||||
let parts = split(r.path, '/');
|
||||
let file = match(r.path, /^\/(luci-static|cgi-bin|ubus)(\/|$)/) || match(parts[length(parts) - 1], /\./);
|
||||
if (redirect && !file) {
|
||||
headers(r, '302 Found', { 'Location': redirect, 'Cache-Control': 'no-store', 'Content-Type': 'text/plain' });
|
||||
r.io.write('Moved\n');
|
||||
}
|
||||
else {
|
||||
headers(r, '404 Not Found', { 'Content-Type': 'text/plain' });
|
||||
r.io.write('Not found\n');
|
||||
}
|
||||
finish();
|
||||
}
|
||||
|
||||
/** The JSON object a POST carries ({} without a body). */
|
||||
function body(r) {
|
||||
let len = +(r.env.CONTENT_LENGTH ?? 0);
|
||||
if (len <= 0)
|
||||
return {};
|
||||
if (len > BODY_MAX)
|
||||
fail(r, '413 Payload Too Large', _('Request too large.'));
|
||||
let raw = '';
|
||||
while (length(raw) < len) {
|
||||
let chunk = r.io.read(len - length(raw));
|
||||
if (chunk == null || chunk == '')
|
||||
break;
|
||||
raw += chunk;
|
||||
}
|
||||
try {
|
||||
let j = json(raw);
|
||||
return type(j) == 'object' ? j : {};
|
||||
}
|
||||
catch (e) {
|
||||
fail(r, '400 Bad Request', _('Invalid JSON.'));
|
||||
}
|
||||
}
|
||||
|
||||
/** Run a change; what it refuses becomes a 400 with its message. */
|
||||
function guard(r, fn) {
|
||||
try {
|
||||
return fn();
|
||||
}
|
||||
catch (e) {
|
||||
if (e.message == DONE)
|
||||
die(DONE);
|
||||
fail(r, '400 Bad Request', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
/* ---------- rate limiting ---------- */
|
||||
|
||||
/** The rate-limit key of an address: an IPv4 address, or the /64 of an IPv6 one. */
|
||||
function addr_key(a) {
|
||||
if (schema.ipv4(a))
|
||||
return a;
|
||||
let g = schema.ipv6_groups(a);
|
||||
if (!g)
|
||||
return null;
|
||||
// An IPv4 address seen through an IPv6 socket.
|
||||
if (g[0] == 0 && g[1] == 0 && g[2] == 0 && g[3] == 0 && g[4] == 0 && g[5] == 0xffff)
|
||||
return sprintf('%d.%d.%d.%d', g[6] >> 8, g[6] & 255, g[7] >> 8, g[7] & 255);
|
||||
return sprintf('%x:%x:%x:%x::/64', g[0], g[1], g[2], g[3]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The buckets a request counts against, [{ key, limit }]: its client, and
|
||||
* behind Cloudflare (the only proxy whose CF-Connecting-IP is believed) also
|
||||
* the bucket all Cloudflare visitors share.
|
||||
*/
|
||||
function buckets(env) {
|
||||
let peer = env.REMOTE_ADDR ?? '';
|
||||
if (!cloudflare_ip(peer))
|
||||
return [ { key: addr_key(peer) ?? peer, limit: FAIL_LIMIT } ];
|
||||
let out = [ { key: SHARED_KEY, limit: SHARED_LIMIT } ];
|
||||
let visitor = addr_key(trim(env.HTTP_CF_CONNECTING_IP ?? ''));
|
||||
if (visitor && !cloudflare_ip(visitor))
|
||||
unshift(out, { key: visitor, limit: FAIL_LIMIT });
|
||||
return out;
|
||||
}
|
||||
|
||||
function rate_state() {
|
||||
let st = read_json(RATE, {});
|
||||
let now = time();
|
||||
for (let k, v in st)
|
||||
if (type(v) != 'object' || now - v.first > FAIL_WINDOW)
|
||||
delete st[k];
|
||||
return st;
|
||||
}
|
||||
|
||||
function rate_blocked(r) {
|
||||
let st = rate_state();
|
||||
return length(filter(r.buckets, (b) => (st[b.key]?.count ?? 0) >= b.limit)) > 0;
|
||||
}
|
||||
|
||||
function rate_fail(r) {
|
||||
let l = lock('portal-rate');
|
||||
let st = rate_state();
|
||||
let now = time();
|
||||
let hit = [];
|
||||
for (let b in r.buckets) {
|
||||
let v = st[b.key] ?? { first: now, count: 0 };
|
||||
v.count++;
|
||||
st[b.key] = v;
|
||||
if (v.count == b.limit)
|
||||
push(hit, b.key);
|
||||
}
|
||||
// Bounded: an attacker with many addresses cannot grow the file without end.
|
||||
let all = keys(st);
|
||||
if (length(all) > RATE_MAX) {
|
||||
all = sort(all, (a, b) => st[a].first - st[b].first);
|
||||
for (let i = 0; i < length(all) - RATE_MAX; i++)
|
||||
delete st[all[i]];
|
||||
}
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(RATE, st, 0o600);
|
||||
unlock(l);
|
||||
for (let k in hit)
|
||||
live.add_event({ type: 'portal_blocked', detail: k == SHARED_KEY ? 'all Cloudflare visitors' : k });
|
||||
}
|
||||
|
||||
/** One more change through this link; refused beyond ACTION_LIMIT per window. */
|
||||
function rate_action(r) {
|
||||
let key = `act:${r.person.id}`;
|
||||
let l = lock('portal-rate');
|
||||
let st = rate_state();
|
||||
let v = st[key] ?? { first: time(), count: 0 };
|
||||
v.count++;
|
||||
st[key] = v;
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(RATE, st, 0o600);
|
||||
unlock(l);
|
||||
if (v.count > ACTION_LIMIT)
|
||||
fail(r, '429 Too Many Requests', _('Too many changes in a short time. Try again in a few minutes.'));
|
||||
}
|
||||
|
||||
/* ---------- pages ---------- */
|
||||
|
||||
/** What the page is called (the tab, the first crumb): the person's server, or Dawnline. */
|
||||
function place(r) {
|
||||
return r.person ? r.m.servers[r.person.server]?.name || 'Dawnline' : 'Dawnline';
|
||||
}
|
||||
|
||||
/** The app shell; it reads the code from its own URL and sends it along. */
|
||||
function shell(r) {
|
||||
// The build's time joins the version: every update gives the assets new URLs, so they are cached for good.
|
||||
let v = `?v=${VERSION}.${stat(`${ASSETS}/portal.js`)?.mtime ?? 0}`;
|
||||
let base = r.base, title = place(r);
|
||||
// The icon goes in the page: a request of its own would start another CGI process.
|
||||
let icon = readfile(`${ASSETS}/icon.svg`);
|
||||
let favicon = icon ? `<link rel="icon" href="data:image/svg+xml;base64,${b64enc(icon)}" type="image/svg+xml">\n` : '';
|
||||
headers(r, '200 OK', {
|
||||
'Content-Type': 'text/html; charset=utf-8',
|
||||
'Content-Security-Policy': "default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self' data: blob:; " +
|
||||
"connect-src 'self'; base-uri 'none'; form-action 'self'; frame-ancestors 'none'",
|
||||
});
|
||||
r.io.write(`<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
|
||||
<meta name="color-scheme" content="light dark">
|
||||
<meta name="robots" content="noindex, nofollow">
|
||||
<title>${html_escape(title)}</title>
|
||||
${favicon}<link rel="stylesheet" href="${base}/assets/portal.css${v}">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app" data-base="${html_escape(base)}" data-title="${html_escape(title)}"></div>
|
||||
<script src="${base}/assets/portal.js${v}" defer></script>
|
||||
</body>
|
||||
</html>
|
||||
`);
|
||||
finish();
|
||||
}
|
||||
|
||||
/** Script and styles (kept outside the web root); the shell's ?v= changes with every build, so they never go stale. */
|
||||
function asset(r, name) {
|
||||
let ext = match(name, /^[a-z0-9-]+\.(js|css|png)$/)?.[1];
|
||||
// The build keeps a gzipped copy beside each: a third of the bytes for a browser that takes it.
|
||||
let gz = ext && r.gzip ? readfile(`${ASSETS}/${name}.gz`) : null;
|
||||
let data = gz ?? (ext ? readfile(`${ASSETS}/${name}`) : null);
|
||||
if (data == null)
|
||||
not_found(r);
|
||||
let h = { 'Content-Type': TYPES[ext], 'Cache-Control': 'private, max-age=31536000, immutable', 'Vary': 'Accept-Encoding' };
|
||||
if (gz != null)
|
||||
h['Content-Encoding'] = 'gzip';
|
||||
headers(r, '200 OK', h);
|
||||
r.io.write(data);
|
||||
finish();
|
||||
}
|
||||
|
||||
/* ---------- the API ---------- */
|
||||
|
||||
/** The model again after a change. */
|
||||
function reload(r) {
|
||||
r.m = core.load();
|
||||
r.person = r.m.people[r.person.id];
|
||||
if (!r.person)
|
||||
not_found(r);
|
||||
}
|
||||
|
||||
/**
|
||||
* The person's state for the page: a stop by the admin (pause, end of access)
|
||||
* wins over their server being off, so they learn that their access is stopped.
|
||||
*/
|
||||
function person_state(m, p, usage) {
|
||||
let st = core.person_state(m, p, usage);
|
||||
if (st == 'server_off') {
|
||||
let own = core.own_state(m, p, usage);
|
||||
if (index(LOCKED, own) >= 0)
|
||||
return own;
|
||||
}
|
||||
return st;
|
||||
}
|
||||
|
||||
/** Why the person can't change anything right now, or null. */
|
||||
function locked(r) {
|
||||
return locked_reason(person_state(r.m, r.person, core.load_usage()));
|
||||
}
|
||||
|
||||
/** Refuses a change (or a config) while the person is stopped. */
|
||||
function need_open(r) {
|
||||
let why = locked(r);
|
||||
if (why)
|
||||
fail(r, '403 Forbidden', why);
|
||||
}
|
||||
|
||||
/** The same link over HTTPS (for a plain-HTTP visitor), or null when this is it. */
|
||||
function https_url(r) {
|
||||
if (r.https)
|
||||
return null;
|
||||
let host = r.host;
|
||||
let h = match(host, /^\[/) ? match(host, /^(\[[^\]]*\])/)?.[1] : split(host, ':')[0];
|
||||
let port = r.m.settings.portal_port;
|
||||
return `https://${h}${port == 443 ? '' : `:${port}`}${r.base}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keys and the disguise never travel in the clear: over plain HTTP a
|
||||
* configuration is refused, and the answer names the HTTPS link to use.
|
||||
*/
|
||||
function need_https(r) {
|
||||
let u = https_url(r);
|
||||
if (u)
|
||||
send_json(r, '403 Forbidden', { error: _('Open this link over HTTPS to get the connection settings.'), https_url: u });
|
||||
}
|
||||
|
||||
/** The router's offset from UTC in minutes: the admin sets hours in its time, the page shows the visitor's. */
|
||||
function tz_offset() {
|
||||
let t = time();
|
||||
return int((timegm(localtime(t)) - t) / 60);
|
||||
}
|
||||
|
||||
/** Everything the page shows. */
|
||||
function state(r) {
|
||||
let m = r.m, person = r.person;
|
||||
let usage = core.load_usage();
|
||||
// Only their server's tunnel: one `awg` process per request, however many servers there are.
|
||||
let v = view.person_view(m, person, usage, live.dump(m, person.server ?? ''), view.states(m, usage));
|
||||
v.state = person_state(m, person, usage);
|
||||
// Day by day this month, for the page's chart (the admin's views have no use for it).
|
||||
v.month_days = view.month_days(usage.people?.[person.id]);
|
||||
let srv = m.servers[person.server];
|
||||
// The device this page was opened from, through the tunnel or beside it.
|
||||
let here = view.device_here(v.devices, r.peer);
|
||||
return {
|
||||
person: v,
|
||||
here,
|
||||
server: {
|
||||
name: srv?.name, endpoint: srv ? core.endpoint_host(m, srv) : '', device_mesh: !!srv?.device_mesh,
|
||||
// AmneziaWG with the disguise off is plain WireGuard (any WireGuard app).
|
||||
awg_disguise: srv ? !awg.obf_neutral(srv.obf) : false,
|
||||
// Which apps (and versions) connect: off, balanced (2.0), max (3.1); legacy = the old junk-only presets.
|
||||
preset: !srv || awg.obf_neutral(srv.obf) ? 'off' : index([ 'balanced', 'max' ], srv.preset) >= 0 ? srv.preset : 'legacy',
|
||||
// The home networks a device with LAN Bypass still sends into the tunnel (home and internet access):
|
||||
// its own local network on the same addresses is out of reach (Add a device says so).
|
||||
home: srv && person.access == 'both' ? core.person_home(srv, person).nets : [],
|
||||
},
|
||||
can_add: length(person.devices) < person.device_limit,
|
||||
locked: index(LOCKED, v.state) >= 0,
|
||||
// Over plain HTTP: where the configurations are (keys only travel encrypted).
|
||||
https_url: https_url(r),
|
||||
kinds: core.KINDS,
|
||||
// The router's day: access ends and usage days follow its clock, not the browser's.
|
||||
today: today(),
|
||||
// Its clock too: "last connected 3 min ago" is measured against it, not the phone's.
|
||||
now: time(),
|
||||
// And its time zone: hours are set in the router's time, shown in the visitor's.
|
||||
tz_offset: tz_offset(),
|
||||
version: VERSION,
|
||||
};
|
||||
}
|
||||
|
||||
/** One of the person's own devices, or a 404. */
|
||||
function own_device(r, id) {
|
||||
let d = r.m.devices[id];
|
||||
if (!d || d.person != r.person.id)
|
||||
fail(r, '404 Not Found', _('Device not found.'));
|
||||
return d;
|
||||
}
|
||||
|
||||
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);
|
||||
let b = body(r);
|
||||
if (!devices.ready())
|
||||
fail(r, '503 Service Unavailable', not_ready());
|
||||
rate_action(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: with_ip(res.config), state: state(r) });
|
||||
}
|
||||
|
||||
function api_config(r, id) {
|
||||
let d = own_device(r, id);
|
||||
need_open(r);
|
||||
need_https(r);
|
||||
awg.mark_issued(r.m, d.id);
|
||||
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. */
|
||||
const ACTIONS = {
|
||||
rename: (r, d, b) => {
|
||||
guard(r, () => devices.update(d.id, { name: b.name }, { by: 'portal', person: r.person.id }));
|
||||
return {};
|
||||
},
|
||||
// Set up from scratch: new keys and the new configuration, which the set-up sheet takes in.
|
||||
reconnect: (r, d, b) => {
|
||||
need_https(r);
|
||||
if (!devices.ready())
|
||||
fail(r, '503 Service Unavailable', not_ready());
|
||||
// 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 }));
|
||||
return {};
|
||||
},
|
||||
};
|
||||
|
||||
function api_device(r, id) {
|
||||
let d = own_device(r, id);
|
||||
need_open(r);
|
||||
let b = body(r);
|
||||
let act = ACTIONS[b.action];
|
||||
if (!act)
|
||||
fail(r, '400 Bad Request', _('Unknown action.'));
|
||||
rate_action(r);
|
||||
let res = act(r, d, b);
|
||||
reload(r);
|
||||
send_json(r, '200 OK', { ok: true, ...res, state: state(r) });
|
||||
}
|
||||
|
||||
/** API routes: method, path under /<code>/api ('*' matches one segment, passed to run). */
|
||||
const API = [
|
||||
{ method: 'GET', path: [ 'state' ], run: api_state },
|
||||
{ method: 'POST', path: [ 'devices' ], run: api_add },
|
||||
{ method: 'GET', path: [ 'devices', '*', 'config' ], run: api_config },
|
||||
{ method: 'POST', path: [ 'devices', '*' ], run: api_device },
|
||||
];
|
||||
|
||||
/** The route for a request: { run, args } or null (routes take at most one '*'). */
|
||||
function route(table, method, segs) {
|
||||
for (let rt in table) {
|
||||
if (rt.method != method || length(rt.path) != length(segs))
|
||||
continue;
|
||||
let args = [], ok = true;
|
||||
for (let i = 0; i < length(segs) && ok; i++)
|
||||
if (rt.path[i] == '*')
|
||||
push(args, segs[i]);
|
||||
else if (rt.path[i] != segs[i])
|
||||
ok = false;
|
||||
if (ok)
|
||||
return { run: rt.run, args };
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/* ---------- one request ---------- */
|
||||
|
||||
export function handle(io) {
|
||||
let env = io.env ?? {};
|
||||
// Header names arrive lower-case from uhttpd; CGI has only HTTP_* variables.
|
||||
let hdr = {};
|
||||
for (let k, v in io.headers ?? {})
|
||||
hdr[lc(k)] = v;
|
||||
let header = (name) => hdr[name] ?? env[`HTTP_${uc(replace(name, /-/g, '_'))}`];
|
||||
let uri = env.REQUEST_URI ?? '/';
|
||||
let qpos = index(uri, '?');
|
||||
let path = qpos >= 0 ? substr(uri, 0, qpos) : uri;
|
||||
let https = env.HTTPS == 'on';
|
||||
let host = header('host') ?? '';
|
||||
let m = core.load();
|
||||
let r = {
|
||||
io, env, m, path, https, host,
|
||||
method: env.REQUEST_METHOD ?? 'GET',
|
||||
peer: env.REMOTE_ADDR ?? '',
|
||||
buckets: buckets(env),
|
||||
// The port in the visitor's address bar ([v6]:port, name:port, or the default).
|
||||
hsts: https && (match(host, /:([0-9]+)$/)?.[1] ?? '443') == '443',
|
||||
person: null,
|
||||
base: '',
|
||||
gzip: match(lc(header('accept-encoding') ?? ''), /(^|[ ,])gzip([ ;,]|$)/) != null,
|
||||
};
|
||||
let segs = filter(split(path, '/'), (s) => s != '');
|
||||
|
||||
// Plain HTTP only for an IP address chosen in Settings (no certificate exists
|
||||
// for it); names stay HTTPS-only.
|
||||
if (!https) {
|
||||
let h = match(host, /^\[/) ? host : split(host, ':')[0];
|
||||
if (ip2int(h) == null || index(core.portal_hosts(m), h) < 0)
|
||||
not_found(r);
|
||||
}
|
||||
// The link is /<word-code>[/assets/…|/api/…]: the code is the address.
|
||||
if (length(segs) < 1 || !match(segs[0], core.WORD_CODE))
|
||||
not_found(r);
|
||||
let code = segs[0];
|
||||
for (let id, p in m.people)
|
||||
if (p.code == code)
|
||||
r.person = p;
|
||||
|
||||
// A wrong code is indistinguishable from a path that does not exist. Only
|
||||
// well-formed guesses count towards the limit, and a client over it is just
|
||||
// no longer counted: a valid link always opens, so guesses from a shared
|
||||
// address (every Cloudflare visitor) cannot lock its owner out.
|
||||
if (r.person == null) {
|
||||
if (!rate_blocked(r))
|
||||
rate_fail(r);
|
||||
not_found(r);
|
||||
}
|
||||
r.base = `/${code}`;
|
||||
let rest = slice(segs, 1);
|
||||
|
||||
if (length(rest) == 0 && r.method == 'GET')
|
||||
shell(r);
|
||||
if (rest[0] == 'assets' && length(rest) == 2 && r.method == 'GET')
|
||||
asset(r, rest[1]);
|
||||
if (rest[0] != 'api')
|
||||
not_found(r);
|
||||
|
||||
// CSRF: only same-origin JSON requests (a cross-site page cannot send
|
||||
// application/json without CORS, and it would need the code anyway).
|
||||
if (r.method == 'POST') {
|
||||
let origin = header('origin');
|
||||
if (!match(env.CONTENT_TYPE ?? '', /^application\/json/) ||
|
||||
(origin != null && origin != `${https ? 'https' : 'http'}://${host}`))
|
||||
fail(r, '403 Forbidden', _('Cross-site request refused.'));
|
||||
}
|
||||
else if (r.method != 'GET')
|
||||
fail(r, '405 Method Not Allowed', _('Method not allowed.'));
|
||||
|
||||
let rt = route(API, r.method, slice(rest, 1));
|
||||
if (!rt)
|
||||
fail(r, '404 Not Found', _('Not found.'));
|
||||
rt.run(r, rt.args[0]);
|
||||
fail(r, '404 Not Found', _('Not found.'));
|
||||
};
|
||||
|
||||
/** Run handle() and swallow its early end. */
|
||||
export function serve(io) {
|
||||
try {
|
||||
// "ru-RU,ru;q=0.9" → ru (i18n.use takes plain language codes only).
|
||||
i18n.use((match(io.env.HTTP_ACCEPT_LANGUAGE ?? '', /^[ \t]*([A-Za-z][A-Za-z][A-Za-z]?)/) ?? [])[1]);
|
||||
handle(io);
|
||||
}
|
||||
catch (e) {
|
||||
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`);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -1,119 +0,0 @@
|
||||
// Dawnline — the portal's certificate (configured, ACME or self-signed) and
|
||||
// the addresses its links could use.
|
||||
|
||||
import { lsdir } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import { q, sh, sh_out, ensure_dir, file_exists, valid_host, ip2int, cloudflare_ip, DATA_DIR } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as lookup from 'dawnline.lookup';
|
||||
|
||||
/** The DNS names a certificate file covers. */
|
||||
export function cert_names(path) {
|
||||
let sans = sh_out(`openssl x509 -in ${q(path)} -noout -text 2>/dev/null | grep -o 'DNS:[^,]*'`) ?? '';
|
||||
return filter(split(replace(sans, /DNS:/g, ''), /[ \t\r\n]+/), (x) => x != '');
|
||||
};
|
||||
|
||||
/** A certificate name covers host: the same name, or a wildcard one level up. */
|
||||
export function name_covers(name, host) {
|
||||
if (name == host)
|
||||
return true;
|
||||
let dot = index(host ?? '', '.');
|
||||
return substr(name, 0, 2) == '*.' && dot > 0 && substr(host, dot) == substr(name, 1);
|
||||
};
|
||||
|
||||
// A module constant, compiled once, not for each file of the directory.
|
||||
const FULLCHAIN = /^(.+)\.fullchain\.crt$/;
|
||||
|
||||
/** ACME certificates on the router and the names each covers. */
|
||||
export function acme_certs() {
|
||||
let out = [];
|
||||
for (let f in sort(lsdir('/etc/ssl/acme') ?? [])) {
|
||||
let m = match(f, FULLCHAIN);
|
||||
if (!m)
|
||||
continue;
|
||||
let c = `/etc/ssl/acme/${f}`, k = `/etc/ssl/acme/${m[1]}.key`;
|
||||
if (file_exists(k))
|
||||
push(out, { cert: c, key: k, names: cert_names(c) });
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/** The ACME certificate that covers host (exact name or a wildcard), if any. */
|
||||
export function acme_cert_for(host) {
|
||||
for (let a in acme_certs())
|
||||
for (let n in a.names)
|
||||
if (name_covers(n, host))
|
||||
return a;
|
||||
return null;
|
||||
};
|
||||
|
||||
/** Certificate the portal should use: configured, ACME for the host, or self-signed. */
|
||||
export function portal_cert(s) {
|
||||
if (s.portal_cert && s.portal_key && file_exists(s.portal_cert) && file_exists(s.portal_key))
|
||||
return { cert: s.portal_cert, key: s.portal_key, self_signed: false };
|
||||
if (s.portal_host) {
|
||||
for (let base in [ `/etc/ssl/acme/${s.portal_host}`, `/etc/acme/${s.portal_host}_ecc/${s.portal_host}` ]) {
|
||||
let c = `${base}.fullchain.crt`, k = `${base}.key`;
|
||||
if (file_exists(c) && file_exists(k))
|
||||
return { cert: c, key: k, self_signed: false };
|
||||
}
|
||||
// An ACME certificate issued for several names, the portal's among them.
|
||||
let a = acme_cert_for(s.portal_host);
|
||||
if (a)
|
||||
return { cert: a.cert, key: a.key, self_signed: false };
|
||||
}
|
||||
let c = `${DATA_DIR}/portal.crt`, k = `${DATA_DIR}/portal.key`;
|
||||
if (!file_exists(c) || !file_exists(k)) {
|
||||
ensure_dir(DATA_DIR, 0o700);
|
||||
let cn = s.portal_host || 'dawnline';
|
||||
sh(`openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 3650 -subj ${q('/CN=' + cn)} -keyout ${k} -out ${c}`);
|
||||
system(`chmod 600 ${k}`);
|
||||
}
|
||||
return { cert: c, key: k, self_signed: true };
|
||||
};
|
||||
|
||||
/*
|
||||
* Addresses the portal could use in links, found on the router: names in ACME
|
||||
* certificates, server endpoints, DDNS names, the current one, and the WAN IP.
|
||||
* Each says where it points and whether a certificate covers it.
|
||||
*/
|
||||
export function portal_candidates(m) {
|
||||
let wan = lookup.router_wan(m);
|
||||
let names = [];
|
||||
let add = (h) => {
|
||||
if (h && valid_host(h) && ip2int(h) == null && substr(h, 0, 2) != '*.' && index(names, h) < 0)
|
||||
push(names, h);
|
||||
};
|
||||
let certs = acme_certs();
|
||||
for (let a in certs)
|
||||
for (let n in a.names)
|
||||
add(n);
|
||||
for (let id, srv in m.servers)
|
||||
add(srv.endpoint);
|
||||
let dc = cursor();
|
||||
if (dc.load('ddns'))
|
||||
dc.foreach('ddns', 'service', (sv) => {
|
||||
if (sv.enabled != '0') {
|
||||
add(sv.lookup_host);
|
||||
add(sv.domain);
|
||||
}
|
||||
});
|
||||
for (let h in core.portal_hosts(m))
|
||||
add(h);
|
||||
let found = lookup.resolve_all(names);
|
||||
let covered = (h) => length(filter(certs, (a) => length(filter(a.names, (n) => name_covers(n, h))) > 0)) > 0;
|
||||
let out = [];
|
||||
for (let h in names) {
|
||||
let ips = found[h] ?? [];
|
||||
push(out, { host: h, kind: 'name', ips, here: wan != null && index(ips, wan) >= 0,
|
||||
cloudflare: length(filter(ips, (x) => cloudflare_ip(x))) > 0, cert: covered(h) });
|
||||
}
|
||||
if (wan)
|
||||
push(out, { host: wan, kind: 'ip', ips: [ wan ], here: true, cloudflare: false, cert: false });
|
||||
for (let ph in core.portal_hosts(m))
|
||||
if (ip2int(ph) != null && ph != wan)
|
||||
push(out, { host: ph, kind: 'ip', ips: [ ph ], here: false, cloudflare: false, cert: false });
|
||||
// Best first: here with a certificate, then here, then the rest.
|
||||
let score = (c) => (c.here ? 4 : 0) + (c.cert ? 2 : 0) + (c.kind == 'name' ? 1 : 0) - (c.cloudflare ? 8 : 0);
|
||||
return sort(out, (a, b) => score(b) - score(a));
|
||||
};
|
||||
@@ -1,106 +0,0 @@
|
||||
// Dawnline — the plain resolver for people with Home-only access: real
|
||||
// addresses from the WAN's DNS, home names from the LAN DNS. Their internet
|
||||
// does not use the tunnel, so fake-IP answers would break it. dawnline.loop
|
||||
// says where it runs (inside the netns while it exists, otherwise on the
|
||||
// tunnel interfaces); procd runs dnsmasq through /usr/libexec/dawnline/resolver.
|
||||
|
||||
import { readfile, writefile, stat } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import { ensure_dir, iface_status, uniq, syslog, syslog_lines } from 'dawnline.util';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as core from 'dawnline.core';
|
||||
|
||||
/** Port on the tunnel addresses while it runs on the router itself. */
|
||||
export const PORT = 5354;
|
||||
export const CONF = '/var/etc/dawnline-dns.conf';
|
||||
const RESOLV = '/var/etc/dawnline-dns.resolv';
|
||||
const PID = '/var/run/dawnline-dns.pid';
|
||||
const FALLBACK_DNS = [ '1.1.1.1', '8.8.8.8' ];
|
||||
|
||||
function wan_dns() {
|
||||
let out = [];
|
||||
let snap = net.snapshot();
|
||||
let wan = snap.devices[snap.wan]?.logical;
|
||||
for (let name in uniq([ wan, 'wan', 'wan6' ])) {
|
||||
if (!name)
|
||||
continue;
|
||||
for (let a in iface_status(name)?.['dns-server'] ?? [])
|
||||
if (match(a, /^[0-9.]+$/))
|
||||
push(out, a);
|
||||
}
|
||||
return length(out) ? uniq(out) : FALLBACK_DNS;
|
||||
}
|
||||
|
||||
/** Local domain of the LAN DNS (dnsmasq `domain`, else the Dawnline hostname domain). */
|
||||
function lan_domain(m) {
|
||||
let c = cursor();
|
||||
c.load('dhcp');
|
||||
let d = null;
|
||||
c.foreach('dhcp', 'dnsmasq', (s) => { d ??= s.domain; });
|
||||
return uniq(filter([ d, m.settings.domain ], (x) => x));
|
||||
}
|
||||
|
||||
/** True when the resolver process is alive. */
|
||||
export function running() {
|
||||
let pid = trim(readfile(PID) ?? '');
|
||||
return match(pid, /^[0-9]+$/) != null && stat(`/proc/${pid}`) != null;
|
||||
};
|
||||
|
||||
/**
|
||||
* Write the config for `target` (dawnline.loop resolver_target) with home
|
||||
* names from `lan` (the LAN's DNS); returns true when it changed (the running
|
||||
* instance is then restarted by procd's respawn after a kill). The first line
|
||||
* tells /usr/libexec/dawnline/resolver where to run it.
|
||||
*/
|
||||
export function sync(m, target, lan) {
|
||||
ensure_dir('/var/etc');
|
||||
// Servers that are on: with none, the resolver idles on loopback.
|
||||
let ifs = [];
|
||||
for (let id, s in m.servers)
|
||||
if (s.enabled)
|
||||
for (let t in core.tunnels(s))
|
||||
push(ifs, t.ifname);
|
||||
let lines = [
|
||||
`# mode=${target.mode}`,
|
||||
'# Generated by Dawnline: plain DNS for Home-only tunnel clients.',
|
||||
`port=${target.port}`,
|
||||
'no-hosts',
|
||||
'bogus-priv',
|
||||
'cache-size=1000',
|
||||
`pid-file=${PID}`,
|
||||
'user=dnsmasq',
|
||||
'group=dnsmasq',
|
||||
];
|
||||
if (target.mode == 'ns') {
|
||||
push(lines, `listen-address=${target.address}`, 'bind-interfaces', 'no-resolv');
|
||||
// Upstream from the transit address: forwarded through the router to the WAN.
|
||||
for (let a in wan_dns())
|
||||
push(lines, `server=${a}@${target.address}`);
|
||||
}
|
||||
else {
|
||||
// No tunnel on: loopback only (without any interface= line dnsmasq would answer everywhere).
|
||||
push(lines, 'bind-dynamic', ...(length(ifs) ? map(ifs, (i) => `interface=${i}`) : [ 'listen-address=127.0.0.1' ]), `resolv-file=${RESOLV}`);
|
||||
let resolv = join('', map(wan_dns(), (a) => `nameserver ${a}\n`));
|
||||
if ((readfile(RESOLV) ?? '') != resolv) {
|
||||
writefile(RESOLV, resolv);
|
||||
system(`chmod 644 ${RESOLV}`);
|
||||
}
|
||||
}
|
||||
if (lan)
|
||||
for (let d in lan_domain(m))
|
||||
push(lines, `server=/${d}/${lan}`);
|
||||
let conf = join('\n', lines) + '\n';
|
||||
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`);
|
||||
return true;
|
||||
};
|
||||
@@ -1,134 +0,0 @@
|
||||
// Dawnline — what the rpcd method modules (dawnline.rpc.*) share: the call
|
||||
// wrapper, argument helpers, the disguise check and the portal port check.
|
||||
|
||||
import * as core from 'dawnline.core';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as health from 'dawnline.health';
|
||||
import * as schema from 'dawnline.schema';
|
||||
import { uniq } from 'dawnline.util';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/**
|
||||
* An rpcd handler from fn(args): an object result is passed on, anything else
|
||||
* is wrapped as { result }, and a failure becomes { error } (the UI shows it).
|
||||
*/
|
||||
export function wrap(fn) {
|
||||
return function(req) {
|
||||
try {
|
||||
let r = fn(req.args ?? {});
|
||||
return type(r) == 'object' ? r : { result: r };
|
||||
}
|
||||
catch (e) {
|
||||
return { error: e.message ?? `${e}` };
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
/** An argument as a trimmed string ('' for null). */
|
||||
export function str(v) {
|
||||
return v == null ? '' : trim(`${v}`);
|
||||
};
|
||||
|
||||
/** Days of history a caller may ask for: 1–400, default 30. */
|
||||
export function clamp_days(v) {
|
||||
let n = +(v ?? 30);
|
||||
return n >= 1 ? (n > 400 ? 400 : int(n)) : 30;
|
||||
};
|
||||
|
||||
export const METHODS = [ 'off', 'balanced', 'max' ];
|
||||
|
||||
/** The disguise a new server gets: the strongest this router can run. */
|
||||
export function default_method() {
|
||||
let c = awg.caps();
|
||||
return c.v3 ? 'max' : c.v2 ? 'balanced' : 'off';
|
||||
};
|
||||
|
||||
/** A disguise method the installed AmneziaWG can run. */
|
||||
export function check_method(name) {
|
||||
if (index(METHODS, name) < 0)
|
||||
die(_('Unknown disguise method.'));
|
||||
let c = awg.caps();
|
||||
if (name == 'max' && !c.v3)
|
||||
die(awg.installed()
|
||||
? _('Maximum needs AmneziaWG 3.1 on the router (kmod-amneziawg, amneziawg-tools 3.1): install a 3.1 build in Dawnline → Components.')
|
||||
: _('AmneziaWG is not installed: install it in Dawnline → Components, then choose Maximum.'));
|
||||
if (name == 'balanced' && !c.v2)
|
||||
die(awg.installed()
|
||||
? _('Balanced needs AmneziaWG 2.0 or newer on the router: install a current build in Dawnline → Components.')
|
||||
: _('AmneziaWG is not installed: install it in Dawnline → Components, then choose Balanced.'));
|
||||
return name;
|
||||
};
|
||||
|
||||
/**
|
||||
* The portal's ports in the final configuration `m` (compared with `was`,
|
||||
* before the change) are acceptable: its HTTPS port and, when links on IP
|
||||
* addresses use plain HTTP, the next port too — both get a WAN ACCEPT rule.
|
||||
* Neither may be 22, 53, 80 or 443, beyond 65535, or taken by another program.
|
||||
*/
|
||||
/** TCP port forwards from the servers' WAN zones: [{ from, to, name }]. */
|
||||
function tcp_forwards(m) {
|
||||
let out = [];
|
||||
for (let z in uniq([ 'wan', ...map(values(m.servers), (s) => s.wan_zone) ]))
|
||||
push(out, ...core.wan_forwards(z, 'tcp'));
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* TCP ports the portal can't take, and who holds them: [{ from, to, who }].
|
||||
* The router's own (22, 53, 80, 443), other programs and port forwards from WAN.
|
||||
* `who` is shown in the admin's language ("Used by %s.").
|
||||
*/
|
||||
export function tcp_taken(m) {
|
||||
let out = [];
|
||||
let held = {};
|
||||
let hold = (p, who) => {
|
||||
if (!held[p]) {
|
||||
held[p] = true;
|
||||
push(out, { from: p, to: p, who });
|
||||
}
|
||||
};
|
||||
for (let p in schema.RESERVED_PORTS)
|
||||
hold(p, _('the router itself'));
|
||||
for (let p, name in health.tcp_port_programs())
|
||||
hold(+p, sprintf(_('%s on this router'), name));
|
||||
for (let f in tcp_forwards(m))
|
||||
push(out, { from: f.from, to: f.to, who: sprintf(_('port forward “%s”'), f.name) });
|
||||
return out;
|
||||
};
|
||||
|
||||
export function check_portal(m, was) {
|
||||
let s = m.settings;
|
||||
if (!s.portal_enabled)
|
||||
return;
|
||||
let port = s.portal_port;
|
||||
if (!schema.valid_port(port) || index(schema.RESERVED_PORTS, port) >= 0)
|
||||
die(_('Choose a free port between 1 and 65535 (not 22, 53, 80 or 443).'));
|
||||
let ports = [ port ];
|
||||
if (core.portal_http(m)) {
|
||||
let hp = core.portal_http_port(m);
|
||||
if (!schema.valid_port(hp) || index(schema.RESERVED_PORTS, hp) >= 0)
|
||||
die(sprintf(_('Links on IP addresses use plain HTTP on the next port (%d), which the portal cannot take. Choose another portal port.'), hp));
|
||||
push(ports, hp);
|
||||
}
|
||||
// Only ports the portal does not hold already: our own uhttpd is not an owner anyway.
|
||||
let held = [];
|
||||
if (was?.settings?.portal_enabled) {
|
||||
push(held, was.settings.portal_port);
|
||||
if (core.portal_http(was))
|
||||
push(held, core.portal_http_port(was));
|
||||
}
|
||||
for (let p in ports) {
|
||||
if (index(held, p) >= 0)
|
||||
continue;
|
||||
let owners = health.tcp_port_owners(p);
|
||||
if (length(owners))
|
||||
die(sprintf(p != port
|
||||
? _("TCP %d is already used by %s. Links on IP addresses use plain HTTP on the port after the portal's. Choose another portal port.")
|
||||
: _('TCP %d is already used by %s. Choose another portal port.'), p, join(', ', owners)));
|
||||
}
|
||||
// A forwarded port never reaches the router: the portal would be unreachable.
|
||||
for (let p in ports)
|
||||
for (let f in tcp_forwards(m))
|
||||
if (f.from <= p && p <= f.to)
|
||||
die(sprintf(_('TCP %d is sent on by port forward “%s”. Choose another portal port.'), p, f.name));
|
||||
};
|
||||
@@ -1,215 +0,0 @@
|
||||
// Dawnline rpcd methods: what people reach at home — services (home
|
||||
// devices and ports), circles (who reaches whom), the devices found at home,
|
||||
// and "Check access".
|
||||
|
||||
import { readfile } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import { sh_out, ip2int, rand_int, rand_hex, admin_log, valid_name, ulength } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as apply from 'dawnline.apply';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as access from 'dawnline.access';
|
||||
import * as store from 'dawnline.store';
|
||||
import * as schema from 'dawnline.schema';
|
||||
import { wrap, str } from 'dawnline.rpc.common';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/* ---------- services ---------- */
|
||||
|
||||
/** A new id from make() that no section has yet. */
|
||||
function new_id(m, make) {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
let id = make();
|
||||
if (!store.id_taken(m, id))
|
||||
return id;
|
||||
}
|
||||
die(_('Could not find a free id, try again.'));
|
||||
}
|
||||
|
||||
function service_set(a) {
|
||||
let name = str(a.name);
|
||||
if (!valid_name(name))
|
||||
die(_('Service: Enter a name: up to 48 characters, without < > " or \\.'));
|
||||
let address = str(a.address);
|
||||
if (!schema.ipv4(address))
|
||||
die(_('Address must be an IPv4 address.'));
|
||||
let ports = filter(map(type(a.ports) == 'array' ? a.ports : split(str(a.ports), /[ ,]+/), str), (x) => x != '');
|
||||
for (let pt in ports)
|
||||
if (!schema.valid_port(pt))
|
||||
die(sprintf(_('Invalid port %s.'), pt));
|
||||
let id = store.transact((c, m) => {
|
||||
let id = str(a.id);
|
||||
if (id == '')
|
||||
id = new_id(m, () => `svc_${rand_int(100000, 999999)}`);
|
||||
else if (!match(id, /^[A-Za-z0-9_]{1,32}$/))
|
||||
die(_('Invalid service id.'));
|
||||
// Never turn a person, device, server, circle or the settings into a service.
|
||||
else if (!m.services[id] && store.id_taken(m, id))
|
||||
die(_('That id is taken.'));
|
||||
c.set(core.CONFIG, id, 'service');
|
||||
c.set(core.CONFIG, id, 'name', name);
|
||||
c.set(core.CONFIG, id, 'address', address);
|
||||
if (length(ports))
|
||||
c.set(core.CONFIG, id, 'port', ports);
|
||||
else
|
||||
c.delete(core.CONFIG, id, 'port');
|
||||
return id;
|
||||
});
|
||||
let s = apply.sync();
|
||||
return { id, warning: s.warning };
|
||||
}
|
||||
|
||||
function service_delete(a) {
|
||||
store.transact((c, m) => {
|
||||
if (!m.services[a.id])
|
||||
die(_('Unknown service.'));
|
||||
c.delete(core.CONFIG, a.id);
|
||||
for (let pid, p in m.people)
|
||||
if (index(p.services, a.id) >= 0) {
|
||||
let rest = filter(p.services, (x) => x != a.id);
|
||||
if (length(rest))
|
||||
c.set(core.CONFIG, pid, 'service', rest);
|
||||
else
|
||||
c.delete(core.CONFIG, pid, 'service');
|
||||
}
|
||||
});
|
||||
let s = apply.sync();
|
||||
return { ok: true, warning: s.warning };
|
||||
}
|
||||
|
||||
/* ---------- circles ---------- */
|
||||
|
||||
/** Create or edit a circle; `members` (person ids) replaces who is in it. */
|
||||
function circle_set(a) {
|
||||
let name = str(a.name);
|
||||
if (!valid_name(name) || ulength(name) > 32)
|
||||
die(_('Enter a circle name: up to 32 characters, without < > " or \\.'));
|
||||
let res = store.transact((c, m) => {
|
||||
let ci = a.id ? m.circles[a.id] : null;
|
||||
if (a.id && !ci)
|
||||
die(_('Unknown circle.'));
|
||||
let server = ci?.server ?? a.server;
|
||||
if (!m.servers[server])
|
||||
die(_('Unknown server.'));
|
||||
for (let id, o in m.circles)
|
||||
if (id != a.id && o.server == server && lc(o.name) == lc(name))
|
||||
die(sprintf(_('%s already exists.'), name));
|
||||
let id = ci?.id ?? new_id(m, () => `c_${rand_hex(4)}`);
|
||||
let n = core.CIRCLE_COLORS;
|
||||
c.set(core.CONFIG, id, 'circle');
|
||||
c.set(core.CONFIG, id, 'server', server);
|
||||
c.set(core.CONFIG, id, 'name', name);
|
||||
c.set(core.CONFIG, id, 'color', `${((int(a.color) || 0) % n + n) % n}`);
|
||||
if (type(a.members) == 'array') {
|
||||
let want = {};
|
||||
for (let pid in a.members)
|
||||
if (m.people[pid]?.server == server)
|
||||
want[pid] = true;
|
||||
for (let pid, p in m.people)
|
||||
if (want[pid])
|
||||
c.set(core.CONFIG, pid, 'circle', id);
|
||||
else if (p.circle == id)
|
||||
c.delete(core.CONFIG, pid, 'circle');
|
||||
}
|
||||
return { id, edit: !!ci, server: m.servers[server].name };
|
||||
});
|
||||
let s = apply.sync();
|
||||
admin_log('awg', res.edit ? 'changed' : 'added', 'circle', name, res.server);
|
||||
return { id: res.id, circles: values(core.load().circles), warning: s.warning };
|
||||
}
|
||||
|
||||
function circle_delete(a) {
|
||||
let res = store.transact((c, m) => {
|
||||
let ci = m.circles[a.id];
|
||||
if (!ci)
|
||||
die(_('Unknown circle.'));
|
||||
c.delete(core.CONFIG, a.id);
|
||||
for (let pid, p in m.people)
|
||||
if (p.circle == a.id)
|
||||
c.delete(core.CONFIG, pid, 'circle');
|
||||
return { name: ci.name, server: m.servers[ci.server]?.name };
|
||||
});
|
||||
let s = apply.sync();
|
||||
admin_log('awg', 'removed', 'circle', res.name, res.server);
|
||||
return { circles: values(core.load().circles), warning: s.warning };
|
||||
}
|
||||
|
||||
/* ---------- devices at home ---------- */
|
||||
|
||||
/** True while OpenWrt's mDNS daemon answers on ubus. */
|
||||
function mdns_running() {
|
||||
return index(split(sh_out('ubus list umdns 2>/dev/null') ?? '', '\n'), 'umdns') >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Services announced on the home networks (Bonjour), from umdns:
|
||||
* [{ type: '_airplay._tcp', name, host, address, port, txt }].
|
||||
*/
|
||||
function mdns_services() {
|
||||
if (!mdns_running())
|
||||
return [];
|
||||
let r = {};
|
||||
try {
|
||||
r = json(sh_out(`ubus call umdns browse '{"address":true,"array":true}' 2>/dev/null`) || '{}') ?? {};
|
||||
}
|
||||
catch (e) {
|
||||
r = {};
|
||||
}
|
||||
let out = [];
|
||||
for (let t, insts in r)
|
||||
for (let name, i in insts) {
|
||||
let addr = type(i.ipv4) == 'array' ? i.ipv4[0] : i.ipv4;
|
||||
if (addr)
|
||||
push(out, { type: t, name, host: replace(i.host ?? '', /\.local\.?$/, ''), address: addr, port: i.port ?? 0, txt: i.txt ?? [] });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Devices at home for the Home services picker: DHCP leases and static hosts
|
||||
* in the server's home networks, with the services they announce (Bonjour,
|
||||
* when umdns runs).
|
||||
*/
|
||||
function home_hosts(a) {
|
||||
let m = core.load();
|
||||
let srv = m.servers[a.id] ?? values(m.servers)[0];
|
||||
if (!srv)
|
||||
die(_('No server yet.'));
|
||||
let nets = [];
|
||||
for (let cidr in net.server_net(srv).nets) {
|
||||
let x = match(cidr, /^([0-9.]+)\/([0-9]+)$/);
|
||||
if (x && ip2int(x[1]) != null)
|
||||
push(nets, [ ip2int(x[1]), 1 << (32 - +x[2]) ]);
|
||||
}
|
||||
let inside = (ip) => {
|
||||
let n = ip2int(ip);
|
||||
return n != null && length(filter(nets, (r) => n >= r[0] && n < r[0] + r[1])) > 0;
|
||||
};
|
||||
let hosts = {};
|
||||
for (let line in split(readfile('/tmp/dhcp.leases') ?? '', '\n')) {
|
||||
let f = split(line, ' ');
|
||||
if (length(f) >= 4 && inside(f[2]))
|
||||
hosts[f[2]] = { address: f[2], name: f[3] == '*' ? '' : f[3], mac: f[1], services: [] };
|
||||
}
|
||||
let c = cursor();
|
||||
if (c.load('dhcp'))
|
||||
c.foreach('dhcp', 'host', (h) => {
|
||||
if (h.ip && inside(h.ip))
|
||||
hosts[h.ip] ??= { address: h.ip, name: h.name ?? '', mac: type(h.mac) == 'array' ? h.mac[0] : h.mac ?? '', services: [] };
|
||||
});
|
||||
for (let s in mdns_services())
|
||||
if (hosts[s.address])
|
||||
push(hosts[s.address].services, { type: s.type, name: s.name, port: s.port });
|
||||
else if (inside(s.address))
|
||||
hosts[s.address] = { address: s.address, name: s.host ?? '', mac: '', services: [ { type: s.type, name: s.name, port: s.port } ] };
|
||||
return { hosts: sort(values(hosts), (x, y) => ip2int(x.address) - ip2int(y.address)), mdns: mdns_running() };
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
service_set: { args: { id: '', name: '', address: '', ports: [] }, call: wrap(service_set) },
|
||||
service_delete: { args: { id: '' }, call: wrap(service_delete) },
|
||||
circle_set: { args: { id: '', server: '', name: '', color: 0, members: [] }, call: wrap(circle_set) },
|
||||
circle_delete: { args: { id: '' }, call: wrap(circle_delete) },
|
||||
home_hosts: { args: { id: '' }, call: wrap(home_hosts) },
|
||||
access_check: { args: { from: '', to: '' }, call: wrap((a) => access.check(core.load(), a.from, a.to)) },
|
||||
};
|
||||
@@ -1,90 +0,0 @@
|
||||
// Dawnline rpcd methods: Diagnostics (Health, the service log), backups,
|
||||
// export / import, and removing Dawnline.
|
||||
|
||||
import { _ } from 'dawnline.i18n';
|
||||
import { slog_clear, admin_log } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as apply from 'dawnline.apply';
|
||||
import * as watchdog from 'dawnline.watchdog';
|
||||
import * as upd from 'dawnline.updates';
|
||||
import * as store from 'dawnline.store';
|
||||
import * as health from 'dawnline.health';
|
||||
import * as backup from 'dawnline.backup';
|
||||
import { wrap } from 'dawnline.rpc.common';
|
||||
|
||||
function health_view(extra) {
|
||||
return { ...(extra ?? {}), checks: health.health(), ...watchdog.view(core.load()) };
|
||||
}
|
||||
|
||||
function service_log_clear(a) {
|
||||
let removed = slog_clear(+a.older_days || 0);
|
||||
if (removed)
|
||||
admin_log('dawnline', 'cleared', null, 'the service log', `${removed} entr${removed == 1 ? 'y' : 'ies'}${+a.older_days ? ` older than ${a.older_days} days` : ''}`);
|
||||
return { removed };
|
||||
}
|
||||
|
||||
function backup_restore(a) {
|
||||
let r = backup.backup_restore(a.name);
|
||||
admin_log('dawnline', 'restored', 'backup', a.name);
|
||||
return r;
|
||||
}
|
||||
|
||||
function backup_upload(a) {
|
||||
let r = backup.backup_upload(a.data);
|
||||
admin_log('dawnline', 'restored', 'backup', 'from a file');
|
||||
return r;
|
||||
}
|
||||
|
||||
function export_all(a) {
|
||||
let r = backup.export_json();
|
||||
admin_log('dawnline', 'exported', null, 'all settings', 'the file holds private keys');
|
||||
return r;
|
||||
}
|
||||
|
||||
function import_all(a) {
|
||||
let r = backup.import_json(a.data);
|
||||
admin_log('dawnline', 'imported', null, 'settings from a file');
|
||||
return r;
|
||||
}
|
||||
|
||||
/** Remove Dawnline's state from the router (the package is removed by the UI afterwards). */
|
||||
function uninstall(a) {
|
||||
if (a.confirm != 'remove')
|
||||
die(_('Type "remove" to confirm.'));
|
||||
// Not in the middle of an update (opkg/apk may be installing Dawnline itself).
|
||||
if (upd.busy())
|
||||
die(_('An update is running: wait for it to finish, then remove Dawnline.'));
|
||||
backup.backup_create('pre-remove');
|
||||
// Stop first: the agent would put back what teardown removes, and could start an automatic update.
|
||||
system('/etc/init.d/dawnline stop >/dev/null 2>&1; /etc/init.d/dawnline disable >/dev/null 2>&1');
|
||||
apply.teardown();
|
||||
store.transact((c, m) => {
|
||||
let rm = [];
|
||||
c.foreach(core.CONFIG, null, (s) => {
|
||||
if (s['.type'] != 'settings')
|
||||
push(rm, s['.name']);
|
||||
});
|
||||
for (let n in rm)
|
||||
c.delete(core.CONFIG, n);
|
||||
c.set(core.CONFIG, 'settings', 'settings');
|
||||
c.set(core.CONFIG, 'settings', 'portal_enabled', '0');
|
||||
});
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
apply: { call: wrap((a) => apply.apply()) },
|
||||
health: { call: wrap((a) => health_view()) },
|
||||
health_fix: { args: { id: '' }, call: wrap((a) => health_view({ result: health.fix(a.id) })) },
|
||||
service_log: { call: wrap((a) => watchdog.view(core.load())) },
|
||||
service_log_clear: { args: { older_days: '' }, call: wrap(service_log_clear) },
|
||||
backup_list: { call: wrap((a) => ({ backups: backup.backup_list() })) },
|
||||
backup_create: { call: wrap((a) => ({ name: backup.backup_create(null) })) },
|
||||
backup_restore: { args: { name: '' }, call: wrap(backup_restore) },
|
||||
backup_delete: { args: { name: '' }, call: wrap((a) => ({ ok: backup.backup_delete(a.name) })) },
|
||||
backup_read: { args: { name: '' }, call: wrap((a) => ({ data: backup.backup_read(a.name) })) },
|
||||
backup_upload: { args: { data: '' }, call: wrap(backup_upload) },
|
||||
'export': { call: wrap(export_all) },
|
||||
'import': { args: { data: {} }, call: wrap(import_all) },
|
||||
uninstall: { args: { confirm: '' }, call: wrap(uninstall) },
|
||||
};
|
||||
@@ -1,195 +0,0 @@
|
||||
// Dawnline rpcd methods: people (adding, editing, pausing, their personal
|
||||
// links and traffic) and their devices.
|
||||
|
||||
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 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';
|
||||
|
||||
const PERSON_FIELDS = [ 'name', 'access', 'device_limit', 'expires', 'expire_action', 'expire_warn', 'quota_gb', 'quota_period',
|
||||
'bonus_gb', 'hours', 'note', 'dns_mode', 'server' ];
|
||||
|
||||
/* ---------- people ---------- */
|
||||
|
||||
function person_create(a) {
|
||||
let id = store.transact((c, m) => core.create_person(c, m, {
|
||||
name: str(a.name), slug: str(a.slug) || null, server: str(a.server) || null, access: a.access,
|
||||
device_limit: str(a.device_limit), expires: str(a.expires), quota_gb: str(a.quota_gb), quota_period: a.quota_period,
|
||||
}));
|
||||
let m = core.load();
|
||||
live.add_event({ type: 'person_added', person: id, detail: m.people[id].name, by: 'admin' });
|
||||
return { id, link: core.portal_url(m, m.people[id]) };
|
||||
}
|
||||
|
||||
/** Immediate edits from the person's page; returns the person as the page shows them. */
|
||||
function person_update(a) {
|
||||
let fields = {};
|
||||
for (let k in PERSON_FIELDS)
|
||||
if (a[k] != null)
|
||||
fields[k] = a[k];
|
||||
if (a.paused != null)
|
||||
fields.paused = a.paused;
|
||||
if (type(a.services) == 'array')
|
||||
fields.services = a.services;
|
||||
if (a.circle != null)
|
||||
fields.circle = a.circle;
|
||||
if (type(a.home_nets) == 'array')
|
||||
fields.home_nets = a.home_nets;
|
||||
let m0 = null;
|
||||
let before = store.transact((c, m) => {
|
||||
let p = m.people[a.id];
|
||||
m0 = m;
|
||||
core.update_person(c, m, a.id, fields);
|
||||
return p;
|
||||
});
|
||||
// "moved to Test" says more than "server".
|
||||
let changes = join(', ', map(filter(keys(fields), (k) => k != 'note'),
|
||||
(k) => k == 'server' && m0?.servers[fields.server] ? `moved to ${m0.servers[fields.server].name}` : k));
|
||||
if (changes)
|
||||
live.add_event_merged({ type: 'person_changed', person: a.id, detail: changes, by: 'admin' }, 120);
|
||||
// Access changes affect the nft sets and client AllowedIPs.
|
||||
let s = apply.sync();
|
||||
let m = core.load();
|
||||
let u = core.load_usage();
|
||||
// One person: only their server's tunnel is read.
|
||||
let p = m.people[a.id];
|
||||
return { person: view.person_view(m, p, u, live.dump(m, p.server ?? ''), view.states(m, u), { admin: true }), was: before?.access, warning: s.warning };
|
||||
}
|
||||
|
||||
function person_pause(a) {
|
||||
store.transact((c, m) => {
|
||||
if (!m.people[a.id])
|
||||
die(_('Unknown person.'));
|
||||
if (a.paused)
|
||||
c.set(core.CONFIG, a.id, 'paused', '1');
|
||||
else
|
||||
c.delete(core.CONFIG, a.id, 'paused');
|
||||
});
|
||||
live.add_event({ type: a.paused ? 'paused' : 'resumed', person: a.id, by: 'admin' });
|
||||
return apply.sync();
|
||||
}
|
||||
|
||||
/**
|
||||
* A new personal link: the old one stops working at once (every portal
|
||||
* request carries the code). What the old link already handed out stays
|
||||
* unless asked: `remove` lists the person's devices to take away with it.
|
||||
*/
|
||||
function person_regen(a) {
|
||||
let gone = store.transact((c, m) => {
|
||||
if (!m.people[a.id])
|
||||
die(_('Unknown person.'));
|
||||
c.set(core.CONFIG, a.id, 'code', core.new_code());
|
||||
c.set(core.CONFIG, a.id, 'code_set', `${time()}`);
|
||||
let gone = [];
|
||||
for (let id in (type(a.remove) == 'array' ? a.remove : []))
|
||||
if (m.devices[id]?.person == a.id) {
|
||||
push(gone, m.devices[id]);
|
||||
core.delete_device(c, m, id);
|
||||
}
|
||||
return gone;
|
||||
});
|
||||
live.add_event({ type: 'link_changed', person: a.id, by: 'admin' });
|
||||
let s = null;
|
||||
if (length(gone)) {
|
||||
for (let d in gone)
|
||||
live.add_event({ type: 'device_removed', person: a.id, device: d.id, detail: d.name, dname: d.name, dkind: d.kind, by: 'admin' });
|
||||
s = apply.sync();
|
||||
}
|
||||
let m = core.load();
|
||||
return { link: core.portal_url(m, m.people[a.id]), removed: length(gone), warning: s?.warning };
|
||||
}
|
||||
|
||||
/** The personal code and links (Copy link, QR, invite): admins only, unlike status. */
|
||||
function person_links(a) {
|
||||
let m = core.load();
|
||||
let p = m.people[a.id];
|
||||
if (!p)
|
||||
die(_('Unknown person.'));
|
||||
return { code: p.code, link: core.portal_url(m, p), links: core.portal_urls(m, p) };
|
||||
}
|
||||
|
||||
function person_delete(a) {
|
||||
let name = store.transact((c, m) => {
|
||||
let name = m.people[a.id]?.name;
|
||||
core.delete_person(c, m, a.id);
|
||||
return name;
|
||||
});
|
||||
live.add_event({ type: 'person_removed', person: a.id, detail: name, pname: name, by: 'admin' });
|
||||
return apply.sync();
|
||||
}
|
||||
|
||||
function person_usage(a) {
|
||||
let usage = core.load_usage();
|
||||
let m = core.load();
|
||||
let p = m.people[a.id];
|
||||
if (!p)
|
||||
die(_('Unknown person.'));
|
||||
let days = clamp_days(a.days);
|
||||
let devs = {};
|
||||
for (let d in p.devices)
|
||||
devs[d] = live.series([ usage.devices?.[d] ], days);
|
||||
return { person: live.series([ usage.people?.[a.id] ], days), devices: devs };
|
||||
}
|
||||
|
||||
/* ---------- devices ---------- */
|
||||
|
||||
function device_create(a) {
|
||||
devices.need_awg();
|
||||
return devices.add(a.person, a.name, a.kind, { by: 'admin', lan_bypass: a.lan_bypass === true });
|
||||
}
|
||||
|
||||
/**
|
||||
* A device's configuration. Only `issue` (true: the admin shows its QR code,
|
||||
* downloads or copies it) counts as handing it out; a look at the device
|
||||
* marks nothing, so its "settings changed" stays until it really gets them.
|
||||
*/
|
||||
function device_config(a) {
|
||||
let m = core.load();
|
||||
let d = m.devices[a.id];
|
||||
if (!d)
|
||||
die(_('Unknown device.'));
|
||||
if (a.issue === true)
|
||||
awg.mark_issued(m, d.id);
|
||||
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). */
|
||||
function device_update(a) {
|
||||
return devices.update(a.id, { name: a.name, disabled: a.disabled, mtu: a.mtu, keepalive: a.keepalive,
|
||||
lan_bypass: a.lan_bypass == null ? null : a.lan_bypass === true }, { by: 'admin' });
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
person_create: {
|
||||
args: { name: '', slug: '', server: '', access: '', device_limit: '', expires: '', quota_gb: '', quota_period: '' },
|
||||
call: wrap(person_create),
|
||||
},
|
||||
person_update: {
|
||||
args: { id: '', name: '', access: '', device_limit: '', expires: '', expire_action: '', expire_warn: '', quota_gb: '',
|
||||
quota_period: '', bonus_gb: '', hours: '', note: '', dns_mode: '', paused: false, services: [], circle: '', home_nets: [], server: '' },
|
||||
call: wrap(person_update),
|
||||
},
|
||||
person_pause: { args: { id: '', paused: false }, call: wrap(person_pause) },
|
||||
person_regen: { args: { id: '', remove: [] }, call: wrap(person_regen) },
|
||||
person_links: { args: { id: '' }, call: wrap(person_links) },
|
||||
person_delete: { args: { id: '' }, call: wrap(person_delete) },
|
||||
person_usage: { args: { id: '', days: 30 }, call: wrap(person_usage) },
|
||||
device_create: { args: { person: '', name: '', kind: '', lan_bypass: false }, call: wrap(device_create) },
|
||||
device_config: { args: { id: '', issue: false }, call: wrap(device_config) },
|
||||
device_update: { args: { id: '', name: '', disabled: false, mtu: '', keepalive: '', lan_bypass: false }, call: wrap(device_update) },
|
||||
device_delete: { args: { id: '' }, call: wrap((a) => devices.remove(a.id, { by: 'admin' })) },
|
||||
// New keys: the device is set up again, as with the portal's Reconnect.
|
||||
device_rotate: { args: { id: '' }, call: wrap((a) => devices.reconnect(a.id, { by: 'admin' })) },
|
||||
};
|
||||
@@ -1,662 +0,0 @@
|
||||
// Dawnline rpcd methods: setting up a server, its General / Network /
|
||||
// Interfaces settings, its disguise, and removing it.
|
||||
|
||||
import { cursor } from 'uci';
|
||||
import { sh_out, sh_bg, parse_cidr, cidr_str, cidr_overlaps, valid_host, valid_name, rand_int, uniq, admin_log } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as apply from 'dawnline.apply';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as loop from 'dawnline.loop';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as store from 'dawnline.store';
|
||||
import * as schema from 'dawnline.schema';
|
||||
import * as health from 'dawnline.health';
|
||||
import * as backup from 'dawnline.backup';
|
||||
import * as devices from 'dawnline.devices';
|
||||
import { wrap, str, check_method, default_method, check_portal, tcp_taken } from 'dawnline.rpc.common';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/** Server ids that would shadow a portal path. */
|
||||
const PORTAL_PATHS = [ 'api', 'assets', 'cgi-bin' ];
|
||||
|
||||
const MESH_WORDS = { off: 'devices kept apart', all: 'devices reach each other', circles: 'devices reach each other in circles' };
|
||||
|
||||
/* ---------- free names, ports and subnets ---------- */
|
||||
|
||||
/**
|
||||
* UDP ports a server can't listen on, and who holds them: [{ from, to, who }].
|
||||
* Other servers, tunnel interfaces, programs on the router and port forwards
|
||||
* from its WAN zone. `srv` is the server being edited: its own port isn't counted.
|
||||
* `who` is shown in the admin's language ("Used by %s.").
|
||||
*/
|
||||
function udp_taken(m, srv, wan_zone) {
|
||||
let out = [];
|
||||
let held = {};
|
||||
let hold = (p, who) => {
|
||||
if (p > 0 && !held[p]) {
|
||||
held[p] = true;
|
||||
push(out, { from: p, to: p, who });
|
||||
}
|
||||
};
|
||||
for (let id, o in m.servers)
|
||||
if (id != srv?.id)
|
||||
hold(o.port, sprintf(_('server “%s”'), o.name));
|
||||
let nc = cursor();
|
||||
nc.load('network');
|
||||
nc.foreach('network', 'interface', (s) => {
|
||||
if (s.listen_port && s['.name'] != srv?.ifname)
|
||||
hold(+s.listen_port, sprintf(_('interface %s'), s['.name']));
|
||||
});
|
||||
let progs = health.udp_port_programs();
|
||||
for (let p in health.udp_bound())
|
||||
if (p != srv?.port)
|
||||
hold(p, progs[`${p}`] ? sprintf(_('%s on this router'), progs[`${p}`]) : _('another program on this router'));
|
||||
for (let f in core.wan_forwards(wan_zone, 'udp'))
|
||||
push(out, { from: f.from, to: f.to, who: sprintf(_('port forward “%s”'), f.name) });
|
||||
return out;
|
||||
}
|
||||
|
||||
function check_udp_port(port, taken) {
|
||||
for (let t in taken)
|
||||
if (t.from <= port && port <= t.to)
|
||||
die(sprintf(_('UDP %d is used by %s. Choose another port.'), port, t.who));
|
||||
}
|
||||
|
||||
/** The usual AmneziaWG port when free, otherwise a random high one. */
|
||||
const DEFAULT_PORT = 4500;
|
||||
|
||||
function free_port(taken) {
|
||||
let used = (p) => length(filter(taken, (t) => t.from <= p && p <= t.to)) > 0;
|
||||
if (!used(DEFAULT_PORT))
|
||||
return DEFAULT_PORT;
|
||||
for (let i = 0; i < 50; i++) {
|
||||
let p = rand_int(40000, 60000);
|
||||
if (!used(p))
|
||||
return p;
|
||||
}
|
||||
return 51830;
|
||||
}
|
||||
|
||||
function free_subnet(taken) {
|
||||
for (let i = 0; i < 300; i++) {
|
||||
// 10.x first; past 200 tries (a 10/8 LAN) the 172.16-31 range.
|
||||
let cand = parse_cidr(i < 200 ? sprintf('10.%d.%d.0/24', rand_int(60, 250), rand_int(0, 250)) : sprintf('172.%d.%d.0/24', rand_int(16, 31), rand_int(0, 250)));
|
||||
let bad = false;
|
||||
for (let t in taken) {
|
||||
let tc = parse_cidr(t);
|
||||
if (tc && cidr_overlaps(cand, tc)) {
|
||||
bad = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!bad)
|
||||
return cidr_str(cand);
|
||||
}
|
||||
return '10.77.0.0/24';
|
||||
}
|
||||
|
||||
/**
|
||||
* A portal port free together with the next one (plain HTTP for IP links):
|
||||
* `first` (the server's UDP number, TCP is another port) before the usual ones.
|
||||
*/
|
||||
function free_portal_port(first, cur, taken) {
|
||||
let used = (p) => length(filter(taken, (t) => t.from <= p && p <= t.to)) > 0;
|
||||
for (let p in uniq([ first, cur || 8443, 8443, 9443, 10443, 7443, 4443 ]))
|
||||
if (!used(p) && !used(p + 1))
|
||||
return p;
|
||||
return 9443;
|
||||
}
|
||||
|
||||
/** A network section name nothing uses: not the router's, not Dawnline's own links, not another server's. */
|
||||
function ifname_free(m, nc, n) {
|
||||
if (nc.get('network', n) != null || index(values(net.INTERNAL), n) >= 0)
|
||||
return false;
|
||||
for (let id, s in m.servers)
|
||||
if (s.ifname == n)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
function free_ifname(m) {
|
||||
let nc = cursor();
|
||||
nc.load('network');
|
||||
for (let i = 0; i < 10; i++)
|
||||
if (ifname_free(m, nc, `dawn${i}`))
|
||||
return `dawn${i}`;
|
||||
return 'dawnx';
|
||||
}
|
||||
|
||||
function id_free(m, id) {
|
||||
return !store.id_taken(m, id) && index(PORTAL_PATHS, id) < 0;
|
||||
}
|
||||
|
||||
/** A free server id: home, then server2, server3, … (never an id anything else has). */
|
||||
function free_server_id(m) {
|
||||
if (id_free(m, 'home'))
|
||||
return 'home';
|
||||
for (let n = 2; ; n++)
|
||||
if (id_free(m, `server${n}`))
|
||||
return `server${n}`;
|
||||
}
|
||||
|
||||
/** Networks a new server must not overlap, by name: the router's and the other servers'. */
|
||||
function taken_networks(m) {
|
||||
let out = [];
|
||||
let seen = {};
|
||||
let add = (name, cidr) => {
|
||||
let c = parse_cidr(cidr);
|
||||
if (c && !seen[cidr_str(c)]) {
|
||||
seen[cidr_str(c)] = true;
|
||||
push(out, { name, cidr: cidr_str(c) });
|
||||
}
|
||||
};
|
||||
for (let n in health.networks())
|
||||
add(n.iface, n.cidr);
|
||||
for (let id, s in m.servers)
|
||||
add(s.name, s.subnet);
|
||||
return out;
|
||||
}
|
||||
|
||||
/* ---------- setup ---------- */
|
||||
|
||||
function setup_defaults(a) {
|
||||
let m = core.load();
|
||||
let networks = taken_networks(m);
|
||||
let zones = [];
|
||||
let fc = cursor();
|
||||
fc.load('firewall');
|
||||
fc.foreach('firewall', 'zone', (z) => push(zones, z.name));
|
||||
let snap = net.snapshot(true);
|
||||
let wan_ip = snap.wan ? split(snap.devices[snap.wan]?.addrs?.[0] ?? '', '/')[0] : '';
|
||||
let kind = health.wan_kind(wan_ip || null);
|
||||
let host = m.settings.portal_host, from = host ? 'portal' : '';
|
||||
if (!host) {
|
||||
// Prefer a DDNS name, then an ACME certificate's, then the WAN address —
|
||||
// but never a private or CGNAT one: nobody on the internet reaches it.
|
||||
let dc = cursor();
|
||||
if (dc.load('ddns'))
|
||||
dc.foreach('ddns', 'service', (s) => {
|
||||
if (!host && s.lookup_host && s.enabled == '1') {
|
||||
host = s.lookup_host;
|
||||
from = 'ddns';
|
||||
}
|
||||
});
|
||||
if (!host) {
|
||||
host = sh_out("ls /etc/ssl/acme/ 2>/dev/null | sed -n 's/\\.fullchain\\.crt$//p' | head -n1") || '';
|
||||
from = host ? 'cert' : '';
|
||||
}
|
||||
if (!host && kind == 'public') {
|
||||
host = wan_ip;
|
||||
from = 'wan';
|
||||
}
|
||||
}
|
||||
// The zones the WAN and LAN devices are in, whatever they are called.
|
||||
let wan_zone = snap.devices[snap.wan]?.zone ?? (index(zones, 'wan') >= 0 ? 'wan' : zones[1]);
|
||||
let lan_zone = snap.devices[snap.lan]?.zone ?? (index(zones, 'lan') >= 0 ? 'lan' : zones[0]);
|
||||
let udp = udp_taken(m, null, wan_zone);
|
||||
let tcp = tcp_taken(m);
|
||||
let port = free_port(udp);
|
||||
return {
|
||||
id: free_server_id(m),
|
||||
// A name to start from, in the admin's language (it is theirs once saved).
|
||||
name: length(m.servers) ? sprintf(_('Server %d'), length(m.servers) + 1) : _('Home'),
|
||||
ifname: free_ifname(m),
|
||||
port,
|
||||
subnet: free_subnet(map(networks, (n) => n.cidr)),
|
||||
endpoint: host,
|
||||
// Where the address came from (ddns, cert, wan, portal) and what the WAN is (public, private, cgnat, none).
|
||||
endpoint_from: from,
|
||||
wan_kind: kind,
|
||||
wan_ip: wan_ip || null,
|
||||
// Automatic.
|
||||
mtu: 0,
|
||||
zones,
|
||||
lan_zone,
|
||||
wan_zone,
|
||||
// A portal already on keeps its port; a new one takes the server's number when it can.
|
||||
portal_port: m.settings.portal_enabled ? m.settings.portal_port : free_portal_port(port, m.settings.portal_port, tcp),
|
||||
awg: awg.caps(),
|
||||
awg_state: awg.load(),
|
||||
networks,
|
||||
udp_taken: udp,
|
||||
tcp_taken: tcp,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a public address points, as the admin types it (debounced in the
|
||||
* UI): the same facts Health uses, in words. `id` names the server whose WAN
|
||||
* to compare with (a new one: the default WAN).
|
||||
*/
|
||||
function endpoint_probe(a) {
|
||||
let host = str(a.host);
|
||||
if (host != '' && !valid_host(host))
|
||||
die(_('Public address must be a hostname or IP address.'));
|
||||
let m = core.load();
|
||||
let srv = m.servers[str(a.id)] ?? null;
|
||||
let f = health.endpoint_facts(srv, host);
|
||||
let w = health.endpoint_words(f, `UDP ${srv?.port ?? +a.port || 4500}`);
|
||||
return { ...f, status: w.status, text: w.text };
|
||||
}
|
||||
|
||||
/** AmneziaWG can run a server: installed, and its kernel module loads. */
|
||||
function need_awg_loaded() {
|
||||
devices.need_awg();
|
||||
if (!awg.load().loaded)
|
||||
die(_("AmneziaWG's kernel module doesn't load on this firmware: reinstall a build for this kernel in Components."));
|
||||
}
|
||||
|
||||
/** An MTU as typed: '' (automatic) or 1280-1500. */
|
||||
function check_mtu(v) {
|
||||
let s = str(v);
|
||||
if (s != '' && (!match(s, /^[0-9]{1,4}$/) || +s < 1280 || +s > 1500))
|
||||
die(_('MTU must be 1280-1500.'));
|
||||
return s;
|
||||
}
|
||||
|
||||
function server_create(a) {
|
||||
need_awg_loaded();
|
||||
let snap = net.snapshot(true);
|
||||
let router_subnets = health.used_subnets(null);
|
||||
let id = store.transact((c, m) => {
|
||||
let id = str(a.id) || 'home';
|
||||
// A UCI section name: a dash would make uci drop it silently.
|
||||
if (!match(id, /^[a-z][a-z0-9_]{0,23}$/))
|
||||
die(_('Server ID may use lowercase letters, digits and underscores.'));
|
||||
if (!id_free(m, id))
|
||||
die(sprintf(_('"%s" is already taken.'), id));
|
||||
let nc = cursor();
|
||||
nc.load('network');
|
||||
let ifname = str(a.ifname) || free_ifname(m);
|
||||
if (!ifname_free(m, nc, ifname))
|
||||
die(sprintf(_('Network interface %s already exists.'), ifname));
|
||||
let udp = udp_taken(m, null, str(a.wan_zone) || 'wan');
|
||||
let port = free_port(udp);
|
||||
if (a.port != null && str(a.port) != '' && str(a.port) != '0') {
|
||||
if (!schema.valid_port(a.port))
|
||||
die(_('UDP port must be 1-65535.'));
|
||||
port = +str(a.port);
|
||||
}
|
||||
check_udp_port(port, udp);
|
||||
let mtu = check_mtu(a.mtu == 0 ? '' : a.mtu);
|
||||
let preset = str(a.preset) || default_method();
|
||||
let srv = { id, ifname, port, subnet: str(a.subnet), endpoint: str(a.endpoint), obf: awg.obf_preset(check_method(preset)) };
|
||||
let pc = parse_cidr(srv.subnet);
|
||||
if (pc)
|
||||
srv.subnet = cidr_str(pc);
|
||||
// Public addresses inside the tunnel would hide the real hosts behind them.
|
||||
// New servers only: a restored backup keeps what it had.
|
||||
if (pc && !core.private_cidr(pc))
|
||||
die(_('Use a private range: 10.x.x.x, 172.16–31.x.x or 192.168.x.x.'));
|
||||
let taken = [ ...router_subnets ];
|
||||
for (let sid, o in m.servers)
|
||||
push(taken, o.subnet);
|
||||
let errs = core.server_errors(m, srv, taken);
|
||||
push(errs, ...awg.obf_errors(srv.obf));
|
||||
if (length(errs))
|
||||
die(join(' ', errs));
|
||||
|
||||
let set = (k, v) => c.set(core.CONFIG, id, k, v);
|
||||
c.set(core.CONFIG, 'settings', 'settings');
|
||||
c.set(core.CONFIG, id, 'server');
|
||||
if (!valid_name(str(a.name) || 'Home'))
|
||||
die(_('Server: Enter a name: up to 48 characters, without < > " or \\.'));
|
||||
set('name', str(a.name) || 'Home');
|
||||
set('ifname', ifname);
|
||||
set('port', `${port}`);
|
||||
set('subnet', srv.subnet);
|
||||
if (srv.endpoint)
|
||||
set('endpoint', srv.endpoint);
|
||||
if (mtu)
|
||||
set('mtu', mtu);
|
||||
set('keepalive', '25');
|
||||
let kp = awg.server_keys();
|
||||
set('private_key', kp.private_key);
|
||||
set('public_key', kp.public_key);
|
||||
set('preset', preset);
|
||||
for (let ok, ov in srv.obf)
|
||||
set(ok, ov);
|
||||
set('lan_zone', str(a.lan_zone) || 'lan');
|
||||
set('wan_zone', str(a.wan_zone) || 'wan');
|
||||
set('auto_wan', '1');
|
||||
set('auto_lan', '1');
|
||||
if (snap.wan)
|
||||
set('detected_wan', snap.wan);
|
||||
if (snap.lan)
|
||||
set('detected_lan', snap.lan);
|
||||
// Off unless asked for: the loop through the LAN costs speed (the wizard says how much).
|
||||
set('lan_like', a.lan_like === true || a.lan_like === 1 || a.lan_like === '1' ? '1' : '0');
|
||||
set('enabled', '1');
|
||||
if (a.portal_enabled != null) {
|
||||
let on = a.portal_enabled === true || a.portal_enabled === 1 || a.portal_enabled === '1';
|
||||
c.set(core.CONFIG, 'settings', 'portal_enabled', on ? '1' : '0');
|
||||
if (a.portal_port != null && str(a.portal_port) != '' && str(a.portal_port) != '0') {
|
||||
if (!schema.valid_port(a.portal_port))
|
||||
die(_('Portal port must be 1-65535.'));
|
||||
c.set(core.CONFIG, 'settings', 'portal_port', `${+str(a.portal_port)}`);
|
||||
}
|
||||
}
|
||||
if (srv.endpoint && !c.get(core.CONFIG, 'settings', 'portal_host') && valid_host(srv.endpoint))
|
||||
c.set(core.CONFIG, 'settings', 'portal_host', srv.endpoint);
|
||||
// The portal as it will be, whatever this call changed.
|
||||
check_portal(core.load(c), m);
|
||||
return id;
|
||||
});
|
||||
let r = apply.apply();
|
||||
system('/etc/init.d/dawnline enabled || /etc/init.d/dawnline enable');
|
||||
let made = core.load().servers[id];
|
||||
admin_log('dawnline', 'created', 'server', made?.name ?? id, made ? `UDP ${made.port}` : null);
|
||||
// netifd reads its protocol handlers when it starts: AmneziaWG installed
|
||||
// after that (by hand, or by an older installer) needs a network restart.
|
||||
let network_restart = !awg.state().handler;
|
||||
sh_bg(network_restart ? '/etc/init.d/network restart; /etc/init.d/dawnline restart' : '/etc/init.d/dawnline restart');
|
||||
return { id, applied: r, network_restart, warning: r.warning };
|
||||
}
|
||||
|
||||
/* ---------- interfaces ---------- */
|
||||
|
||||
/** Interfaces card on the Server page: what exists, what is detected, what the server uses. */
|
||||
function interfaces_view(m, srv, snap) {
|
||||
let sn = net.server_net(srv, snap);
|
||||
let home = {};
|
||||
for (let h in sn.home)
|
||||
home[h.dev] = h;
|
||||
let devs = [];
|
||||
for (let dev, d in snap.devices)
|
||||
push(devs, {
|
||||
...d,
|
||||
own: dev == srv.ifname,
|
||||
home: !!home[dev],
|
||||
auto: !!home[dev]?.auto,
|
||||
});
|
||||
// Saved names that no longer exist still show, so they can be removed.
|
||||
for (let h in sn.home)
|
||||
if (h.missing)
|
||||
push(devs, { name: h.dev, category: 'other', kind: 'other', missing: true, nets: [], addrs: [], members: [], home: true, auto: h.auto });
|
||||
let lan_only = 0;
|
||||
for (let id, p in m.people)
|
||||
if (p.server == srv.id && p.access == 'lan')
|
||||
lan_only++;
|
||||
return {
|
||||
server: srv.id,
|
||||
ifname: srv.ifname,
|
||||
detected: { wan: snap.wan, lan: snap.lan },
|
||||
saved: { wan: srv.detected_wan, lan: srv.detected_lan },
|
||||
config: { auto_wan: srv.auto_wan, wan_device: srv.wan_device, auto_lan: srv.auto_lan, lan_devices: srv.lan_devices, lan_like: srv.lan_like,
|
||||
device_mesh: srv.device_mesh, mesh: srv.mesh },
|
||||
like_home: loop.status(),
|
||||
needs: health.needs().home,
|
||||
effective: sn,
|
||||
zones: snap.zones,
|
||||
devices: devs,
|
||||
lan_only,
|
||||
};
|
||||
}
|
||||
|
||||
function interfaces_get(a) {
|
||||
let m = core.load();
|
||||
let srv = m.servers[a.id] ?? values(m.servers)[0];
|
||||
if (!srv)
|
||||
die(_('No server yet.'));
|
||||
return interfaces_view(m, srv, net.snapshot(true));
|
||||
}
|
||||
|
||||
function server_interfaces(a) {
|
||||
let snap = net.snapshot(true);
|
||||
let auto_wan = a.auto_wan != false && a.auto_wan != '0';
|
||||
let auto_lan = a.auto_lan != false && a.auto_lan != '0';
|
||||
let wan_dev = str(a.wan_device);
|
||||
let res = store.transact((c, m) => {
|
||||
let srv = m.servers[a.id];
|
||||
if (!srv)
|
||||
die(_('Unknown server.'));
|
||||
if (!auto_wan) {
|
||||
if (!snap.devices[wan_dev])
|
||||
die(_('Choose the interface that leads to the internet.'));
|
||||
if (!snap.devices[wan_dev].zone)
|
||||
die(sprintf(_('%s is not in a firewall zone, so the server port cannot be opened on it.'), wan_dev));
|
||||
}
|
||||
let lans = [];
|
||||
for (let d in (type(a.lan_devices) == 'array' ? a.lan_devices : [])) {
|
||||
d = str(d);
|
||||
// Unknown names are kept only if they were saved before (interface down right now).
|
||||
if (d == '' || d == srv.ifname || (!snap.devices[d] && index(srv.lan_devices, d) < 0))
|
||||
continue;
|
||||
if (snap.devices[d]?.master)
|
||||
die(sprintf(_('%s is part of %s; choose %s instead.'), d, snap.devices[d].master, snap.devices[d].master));
|
||||
if (d == (auto_wan ? snap.wan : wan_dev))
|
||||
die(sprintf(_('%s is the WAN.'), d));
|
||||
push(lans, d);
|
||||
}
|
||||
lans = uniq(lans);
|
||||
let id = srv.id;
|
||||
if (a.lan_like != null)
|
||||
c.set(core.CONFIG, id, 'lan_like', a.lan_like === false || a.lan_like === '0' ? '0' : '1');
|
||||
let mesh = index(core.MESH, a.mesh) >= 0 ? a.mesh
|
||||
: a.device_mesh == null ? srv.mesh : (a.device_mesh === false || a.device_mesh === '0' ? 'off' : 'all');
|
||||
c.set(core.CONFIG, id, 'mesh', mesh);
|
||||
c.delete(core.CONFIG, id, 'device_mesh');
|
||||
c.set(core.CONFIG, id, 'auto_wan', auto_wan ? '1' : '0');
|
||||
c.set(core.CONFIG, id, 'auto_lan', auto_lan ? '1' : '0');
|
||||
if (auto_wan || wan_dev == '')
|
||||
c.delete(core.CONFIG, id, 'wan_device');
|
||||
else
|
||||
c.set(core.CONFIG, id, 'wan_device', wan_dev);
|
||||
if (length(lans))
|
||||
c.set(core.CONFIG, id, 'lan_device', lans);
|
||||
else
|
||||
c.delete(core.CONFIG, id, 'lan_device');
|
||||
// Remember what was detected, as the fallback while an interface is down.
|
||||
if (snap.wan)
|
||||
c.set(core.CONFIG, id, 'detected_wan', snap.wan);
|
||||
if (snap.lan)
|
||||
c.set(core.CONFIG, id, 'detected_lan', snap.lan);
|
||||
// Keep the legacy zone options in step (older code paths, health checks).
|
||||
let now = core.load(c).servers[id];
|
||||
let sn = net.server_net(now, snap);
|
||||
if (sn.wan.zone)
|
||||
c.set(core.CONFIG, id, 'wan_zone', sn.wan.zone);
|
||||
if (length(sn.zones))
|
||||
c.set(core.CONFIG, id, 'lan_zone', sn.zones[0]);
|
||||
return { was: srv, now: core.load(c).servers[id], mesh };
|
||||
});
|
||||
let srv = res.was;
|
||||
// What the admin changed here: the networks, and/or whether devices reach each other.
|
||||
let sig = (x) => {
|
||||
let sn = net.server_net(x, snap);
|
||||
return `${join(',', sn.nets)}|${sn.wan.dev}|${x.lan_like}`;
|
||||
};
|
||||
let what = [];
|
||||
if (sig(res.now) != sig(srv))
|
||||
push(what, 'home network and internet path');
|
||||
if (res.mesh != srv.mesh)
|
||||
push(what, MESH_WORDS[res.mesh]);
|
||||
if (length(what))
|
||||
admin_log('awg', 'changed', 'server', srv.name, join(', ', what));
|
||||
let before = join(',', net.server_net(srv, snap).nets);
|
||||
apply.apply();
|
||||
let m = core.load();
|
||||
let v = interfaces_view(m, m.servers[srv.id], net.snapshot(true));
|
||||
v.nets_changed = before != join(',', v.effective.nets);
|
||||
return v;
|
||||
}
|
||||
|
||||
/* ---------- settings ---------- */
|
||||
|
||||
/** A list of DNS servers as typed (array or "a, b"): each an IPv4 or IPv6 address. */
|
||||
function dns_list(v) {
|
||||
let list = filter(map(type(v) == 'array' ? v : split(str(v), /[ ,]+/), str), (x) => x != '');
|
||||
for (let d in list)
|
||||
if (!schema.ip(d))
|
||||
die(sprintf(_('%s is not an IP address.'), d));
|
||||
return list;
|
||||
}
|
||||
|
||||
/** Server settings from the General and Network tabs; applied at once. */
|
||||
function server_update(a) {
|
||||
let v = a.values ?? {};
|
||||
if (v.device_mesh != null && v.mesh == null)
|
||||
v.mesh = v.device_mesh === true || v.device_mesh == '1' ? 'all' : 'off';
|
||||
let srv = store.transact((c, m) => {
|
||||
let srv = m.servers[a.id];
|
||||
if (!srv)
|
||||
die(_('Unknown server.'));
|
||||
let set = (k, val) => (val == null || val == '') ? c.delete(core.CONFIG, srv.id, k) : c.set(core.CONFIG, srv.id, k, val);
|
||||
if (v.name != null) {
|
||||
if (!valid_name(str(v.name)))
|
||||
die(_('Server: Enter a name: up to 48 characters, without < > " or \\.'));
|
||||
set('name', str(v.name));
|
||||
}
|
||||
// Empty = the portal's address (core.endpoint_host), as at creation.
|
||||
if (v.endpoint != null) {
|
||||
if (str(v.endpoint) != '' && !valid_host(str(v.endpoint)))
|
||||
die(_('Public address must be a hostname or IP address.'));
|
||||
set('endpoint', str(v.endpoint));
|
||||
}
|
||||
if (v.port != null) {
|
||||
if (!schema.valid_port(v.port))
|
||||
die(_('UDP port must be 1-65535.'));
|
||||
let n = +str(v.port);
|
||||
if (n != srv.port)
|
||||
check_udp_port(n, udp_taken(m, srv, srv.wan_zone));
|
||||
set('port', `${n}`);
|
||||
}
|
||||
if (v.enabled != null)
|
||||
set('enabled', v.enabled === true || v.enabled == '1' ? '1' : '0');
|
||||
if (v.mesh != null) {
|
||||
if (index(core.MESH, v.mesh) < 0)
|
||||
die(_('Unknown choice for devices reaching each other.'));
|
||||
set('mesh', v.mesh);
|
||||
set('device_mesh', null);
|
||||
}
|
||||
if (v.mtu != null)
|
||||
set('mtu', check_mtu(v.mtu));
|
||||
if (v.keepalive != null) {
|
||||
if (str(v.keepalive) != '' && (!match(str(v.keepalive), /^[0-9]{1,3}$/) || +v.keepalive > 600))
|
||||
die(_('Keep-alive must be 0-600 seconds.'));
|
||||
set('keepalive', str(v.keepalive));
|
||||
}
|
||||
if (v.dns != null) {
|
||||
let list = dns_list(v.dns);
|
||||
if (length(list))
|
||||
c.set(core.CONFIG, srv.id, 'dns', list);
|
||||
else
|
||||
c.delete(core.CONFIG, srv.id, 'dns');
|
||||
}
|
||||
// A new tunnel network: private, free, and every device renumbered into it.
|
||||
if (v.subnet != null && str(v.subnet) != '' && str(v.subnet) != srv.subnet) {
|
||||
let pc = parse_cidr(str(v.subnet));
|
||||
if (!pc)
|
||||
die(_('Subnet must look like 10.77.0.0/24.'));
|
||||
if (!core.private_cidr(pc))
|
||||
die(_('Use a private range: 10.x.x.x, 172.16–31.x.x or 192.168.x.x.'));
|
||||
let taken = map(filter(taken_networks(m), (n) => n.cidr != srv.subnet && n.name != srv.name), (n) => n.cidr);
|
||||
for (let t in taken)
|
||||
if (cidr_overlaps(pc, parse_cidr(t)))
|
||||
die(sprintf(_('Subnet overlaps %s.'), t));
|
||||
core.renumber_server(c, m, srv, cidr_str(pc));
|
||||
}
|
||||
return srv;
|
||||
});
|
||||
// For the log: what changed, in words. Turning on or off is its own line.
|
||||
let labels = { name: 'name', endpoint: 'public address', port: 'AmneziaWG port', mtu: 'MTU', keepalive: 'keep-alive', dns: 'DNS', subnet: 'tunnel network' };
|
||||
let what = [];
|
||||
for (let k, label in labels) {
|
||||
if (v[k] == null)
|
||||
continue;
|
||||
let was = k == 'dns' ? join(' ', srv.dns ?? []) : k == 'mtu' && !srv.mtu ? '' : `${srv[k] ?? ''}`;
|
||||
let now = k == 'dns' ? join(' ', dns_list(v.dns)) : str(v[k]);
|
||||
if (trim(was) != trim(now))
|
||||
push(what, label);
|
||||
}
|
||||
let turned = v.enabled == null ? null : (v.enabled === true || v.enabled == '1');
|
||||
let name = v.name != null ? str(v.name) : srv.name;
|
||||
let r = apply.apply();
|
||||
if (turned != null && turned != srv.enabled)
|
||||
admin_log('dawnline', turned ? 'turned on' : 'turned off', 'server', name);
|
||||
if (length(what))
|
||||
admin_log('dawnline', 'changed', 'server', name, join(', ', what));
|
||||
if (v.mesh != null && v.mesh != srv.mesh)
|
||||
admin_log('dawnline', 'changed', 'server', name, MESH_WORDS[v.mesh]);
|
||||
return r;
|
||||
}
|
||||
|
||||
/** What the ports of an existing server must avoid (the AmneziaWG card checks as you type). */
|
||||
function server_ports(a) {
|
||||
let m = core.load();
|
||||
let srv = m.servers[a.id];
|
||||
if (!srv)
|
||||
die(_('Unknown server.'));
|
||||
// `networks`: what a new tunnel network must not overlap (everything but this server's own).
|
||||
return { udp_taken: udp_taken(m, srv, srv.wan_zone), tcp_taken: tcp_taken(m),
|
||||
networks: filter(taken_networks(m), (n) => n.cidr != srv.subnet) };
|
||||
}
|
||||
|
||||
function server_obfuscation(a) {
|
||||
let preset = str(a.preset) || default_method();
|
||||
let o = awg.obf_preset(check_method(preset));
|
||||
// A cheap self-check: new values pass what a restore would be held to.
|
||||
let errs = awg.obf_errors(o);
|
||||
if (length(errs))
|
||||
die(join(' ', errs));
|
||||
let srv = store.transact((c, m) => {
|
||||
let srv = m.servers[a.id];
|
||||
if (!srv)
|
||||
die(_('Unknown server.'));
|
||||
for (let k in core.OBF_KEYS)
|
||||
c.delete(core.CONFIG, srv.id, k);
|
||||
for (let k, v in o)
|
||||
c.set(core.CONFIG, srv.id, k, v);
|
||||
c.set(core.CONFIG, srv.id, 'preset', preset);
|
||||
return srv;
|
||||
});
|
||||
let r = apply.apply();
|
||||
admin_log('awg', 'changed', 'server', srv.name, `disguise: ${preset}`);
|
||||
return r;
|
||||
}
|
||||
|
||||
/** Remove a server with its people, their devices and its circles (a backup is made first). */
|
||||
function server_delete(a) {
|
||||
if (!core.load().servers[a.id])
|
||||
die(_('Unknown server.'));
|
||||
backup.backup_create('pre-delete');
|
||||
let res = store.transact((c, m) => {
|
||||
let srv = m.servers[a.id];
|
||||
if (!srv)
|
||||
die(_('Unknown server.'));
|
||||
let gone = 0;
|
||||
for (let pid, p in m.people)
|
||||
if (p.server == srv.id) {
|
||||
core.delete_person(c, m, pid);
|
||||
gone++;
|
||||
}
|
||||
for (let cid, ci in m.circles)
|
||||
if (ci.server == srv.id)
|
||||
c.delete(core.CONFIG, cid);
|
||||
c.delete(core.CONFIG, srv.id);
|
||||
return { name: srv.name, gone };
|
||||
});
|
||||
let r = apply.apply();
|
||||
admin_log('awg', 'removed', 'server', res.name, res.gone ? `with ${res.gone} ${res.gone == 1 ? 'person' : 'people'}` : null);
|
||||
return r;
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
setup_defaults: { call: wrap(setup_defaults) },
|
||||
server_create: {
|
||||
args: { id: '', name: '', ifname: '', port: 0, subnet: '', endpoint: '', mtu: 0, preset: '', lan_zone: '', wan_zone: '',
|
||||
lan_like: true, portal_enabled: false, portal_port: 0 },
|
||||
call: wrap(server_create),
|
||||
},
|
||||
server_ports: { args: { id: '' }, call: wrap(server_ports) },
|
||||
endpoint_probe: { args: { host: '', id: '', port: 0 }, call: wrap(endpoint_probe) },
|
||||
server_obfuscation: { args: { id: '', preset: '' }, call: wrap(server_obfuscation) },
|
||||
server_update: { args: { id: '', values: {} }, call: wrap(server_update) },
|
||||
interfaces: { args: { id: '' }, call: wrap(interfaces_get) },
|
||||
server_interfaces: {
|
||||
args: { id: '', auto_wan: false, wan_device: '', auto_lan: false, lan_devices: [], lan_like: false, device_mesh: true, mesh: '' },
|
||||
call: wrap(server_interfaces),
|
||||
},
|
||||
server_delete: { args: { id: '' }, call: wrap(server_delete) },
|
||||
};
|
||||
@@ -1,169 +0,0 @@
|
||||
// Dawnline rpcd methods: the Settings page (the portal, defaults, logs and
|
||||
// history, updates). What may be saved is dawnline.schema's.
|
||||
|
||||
import { stat } from 'fs';
|
||||
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';
|
||||
import * as geo from 'dawnline.geo';
|
||||
import * as store from 'dawnline.store';
|
||||
import * as schema from 'dawnline.schema';
|
||||
import * as health from 'dawnline.health';
|
||||
import * as pc from 'dawnline.portalcert';
|
||||
import { wrap, check_portal, tcp_taken } from 'dawnline.rpc.common';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/** The portal's own checks from Health, shown next to its settings. */
|
||||
const PORTAL_CHECKS = [ 'portal', 'portal_dns', 'portal_reply', 'portal_http', 'cert' ];
|
||||
|
||||
function file_size(path) {
|
||||
return stat(path)?.size ?? 0;
|
||||
}
|
||||
|
||||
/** What the log and traffic history hold right now (Settings → Logs & history). */
|
||||
function logs_info(m) {
|
||||
let ev = live.load_events();
|
||||
let by = {};
|
||||
for (let cat in live.CATEGORIES)
|
||||
by[cat] = 0;
|
||||
for (let e in ev)
|
||||
by[live.category_of(e.type)]++;
|
||||
let usage = core.load_usage();
|
||||
let days = {};
|
||||
for (let grp in [ usage?.devices ?? {}, usage?.people ?? {} ])
|
||||
for (let id, rec in grp)
|
||||
for (let k in keys(rec.days ?? {}))
|
||||
days[k] = true;
|
||||
let dk = sort(keys(days));
|
||||
return {
|
||||
policy: live.policy(m.settings.retention),
|
||||
events: length(ev),
|
||||
oldest: length(ev) ? ev[0].ts : 0,
|
||||
by_category: by,
|
||||
log_ram: file_size(`${RUN_DIR}/events.json`),
|
||||
log_flash: file_size(`${DATA_DIR}/events.json`),
|
||||
usage_flash: file_size(`${DATA_DIR}/usage.json`),
|
||||
usage_since: length(dk) ? dk[0] : '',
|
||||
usage_days: length(dk),
|
||||
geo_flash: file_size(`${DATA_DIR}/geo.json`),
|
||||
};
|
||||
}
|
||||
|
||||
function settings_get(a) {
|
||||
let c = core.open_cursor();
|
||||
let raw = c.get_all(core.CONFIG, 'settings') ?? {};
|
||||
let m = core.load(c);
|
||||
let out = {};
|
||||
for (let k in keys(schema.SETTINGS))
|
||||
out[k] = raw[k] ?? '';
|
||||
out.log_category = live.policy(m.settings.retention).categories;
|
||||
let cert = m.settings.portal_enabled ? pc.portal_cert(m.settings) : null;
|
||||
return {
|
||||
settings: out,
|
||||
needs: health.needs(),
|
||||
effective: m.settings,
|
||||
services: values(m.services),
|
||||
cert: cert ? { path: cert.cert, self_signed: cert.self_signed,
|
||||
expires: sh_out(`openssl x509 -enddate -noout -in ${q(cert.cert)} 2>/dev/null | cut -d= -f2`) } : null,
|
||||
tcp_taken: tcp_taken(m),
|
||||
portal_public_port: core.portal_public_port(m),
|
||||
geo_cached: length(keys(geo.load().ips)),
|
||||
logs: logs_info(m),
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* The slow part of the portal settings (DNS lookups and the Health checks),
|
||||
* fetched after the page shows.
|
||||
*/
|
||||
function portal_info(a) {
|
||||
let m = core.load();
|
||||
return {
|
||||
candidates: pc.portal_candidates(m),
|
||||
checks: filter(health.health(m), (x) => index(PORTAL_CHECKS, x.id) >= 0),
|
||||
};
|
||||
}
|
||||
|
||||
function same(a, b) {
|
||||
return (type(a) == 'array' ? join('\n', a) : a ?? '') == (type(b) == 'array' ? join('\n', b) : b ?? '');
|
||||
}
|
||||
|
||||
/** Save settings (`values`: key → value, '' back to the default); the portal is checked as it will be. */
|
||||
function settings_update(a) {
|
||||
let v = a.values ?? {};
|
||||
if (type(v) != 'object')
|
||||
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)
|
||||
c.delete(core.CONFIG, 'settings', k);
|
||||
else
|
||||
c.set(core.CONFIG, 'settings', k, nv);
|
||||
}
|
||||
check_portal(core.load(c), m);
|
||||
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');
|
||||
else
|
||||
s = apply.sync();
|
||||
// The changes worth a line: the portal and the watchdog (other settings are housekeeping).
|
||||
let now = core.load().settings, was = res.was;
|
||||
if (now.portal_enabled != was.portal_enabled)
|
||||
admin_log('portal', now.portal_enabled ? 'turned on' : 'turned off', null, 'the personal portal', now.portal_enabled ? `port ${now.portal_port}` : null);
|
||||
else if (now.portal_enabled) {
|
||||
let what = [];
|
||||
if (now.portal_port != was.portal_port)
|
||||
push(what, `port ${now.portal_port}`);
|
||||
if (now.portal_host != was.portal_host)
|
||||
push(what, `address ${now.portal_host}`);
|
||||
if (join(' ', now.portal_alt) != join(' ', was.portal_alt))
|
||||
push(what, 'other addresses');
|
||||
if (now.portal_cert != was.portal_cert || now.portal_key != was.portal_key)
|
||||
push(what, 'certificate');
|
||||
if (now.portal_redirect != was.portal_redirect)
|
||||
push(what, 'redirect');
|
||||
if (length(what))
|
||||
admin_log('portal', 'changed', null, 'the personal portal', join(', ', what));
|
||||
}
|
||||
if (now.watchdog != was.watchdog)
|
||||
admin_log('dawnline', now.watchdog ? 'turned on' : 'turned off', null, 'the watchdog', now.watchdog ? null : 'problems are only logged, nothing is repaired');
|
||||
let out = settings_get({});
|
||||
if (s?.warning)
|
||||
out.warning = s.warning;
|
||||
return out;
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
settings_get: { call: wrap(settings_get) },
|
||||
portal_info: { call: wrap(portal_info) },
|
||||
settings_update: { args: { values: {} }, call: wrap(settings_update) },
|
||||
};
|
||||
@@ -1,309 +0,0 @@
|
||||
// Dawnline rpcd methods: the live revision, the Overview status (servers,
|
||||
// people, totals, map, recent activity), the live meter, the activity log and
|
||||
// traffic history.
|
||||
|
||||
import { readfile } from 'fs';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
import { read_json, write_json, ensure_dir, lock, unlock, admin_log, VERSION, RUN_DIR } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as view from 'dawnline.view';
|
||||
import * as geo from 'dawnline.geo';
|
||||
import * as net from 'dawnline.net';
|
||||
import * as loop from 'dawnline.loop';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as lookup from 'dawnline.lookup';
|
||||
import * as health from 'dawnline.health';
|
||||
import * as meter from 'dawnline.meter';
|
||||
import { wrap, str, clamp_days } from 'dawnline.rpc.common';
|
||||
|
||||
/** The router's public IPv4 (null when none or private). */
|
||||
function wan_public(m) {
|
||||
let w = lookup.router_wan(m);
|
||||
return w && health.wan_kind(w) == 'public' ? w : null;
|
||||
}
|
||||
|
||||
/** The router's own public addresses: a device at home that reaches the server through the WAN address (NAT loopback) is not "outside". */
|
||||
function own_addresses(m) {
|
||||
let own = {};
|
||||
for (let id, srv in m.servers) {
|
||||
let a = net.wan_address(srv) ?? lookup.wan_ipv4(srv.wan_zone);
|
||||
if (a)
|
||||
own[a] = true;
|
||||
}
|
||||
let wan = lookup.wan_ipv4('wan');
|
||||
if (wan)
|
||||
own[wan] = true;
|
||||
return own;
|
||||
}
|
||||
|
||||
/** Router location plus one point per (device, recent client address). */
|
||||
function geo_view(m, usage, dmp) {
|
||||
if (!m.settings.geo_enabled)
|
||||
return { enabled: false, home: null, points: [] };
|
||||
let cache = geo.load();
|
||||
let points = [];
|
||||
let now = time();
|
||||
// The router's own public addresses: a device at home that reaches the server through the
|
||||
// WAN address (NAT loopback) shows up from there. Home is not a place people connect from.
|
||||
let own = {};
|
||||
for (let id, srv in m.servers) {
|
||||
let a = net.wan_address(srv) ?? lookup.wan_ipv4(srv.wan_zone);
|
||||
if (a)
|
||||
own[a] = true;
|
||||
}
|
||||
let wan = lookup.wan_ipv4('wan');
|
||||
if (wan)
|
||||
own[wan] = true;
|
||||
for (let id, d in m.devices) {
|
||||
let rec = usage.devices?.[id];
|
||||
let peer = dmp.peers[d.public_key];
|
||||
let cur = live.endpoint_ip(peer?.endpoint);
|
||||
let online = live.online(dmp, id, peer);
|
||||
for (let ip, ts in rec?.ips ?? {}) {
|
||||
// Local (LAN / CGNAT) client addresses and the router's own are not shown on the map.
|
||||
let pl = geo.is_public(ip) && !own[ip] ? geo.place(cache, ip) : null;
|
||||
if (!pl)
|
||||
continue;
|
||||
push(points, {
|
||||
person: d.person, device: id, ip, ts,
|
||||
online: online && ip == cur,
|
||||
lat: pl.lat, lon: pl.lon, city: pl.city, country: pl.country, cc: pl.cc,
|
||||
asn: pl.asn ?? 0, org: pl.org ?? '',
|
||||
});
|
||||
}
|
||||
}
|
||||
let h = cache.home;
|
||||
return {
|
||||
enabled: true,
|
||||
provider: m.settings.geo_provider,
|
||||
cached: length(keys(cache.ips)),
|
||||
home: (h && h.lat != null) ? { lat: h.lat, lon: h.lon, city: h.city, country: h.country, cc: h.cc, asn: h.asn ?? 0, org: h.org ?? '' } : null,
|
||||
points,
|
||||
};
|
||||
}
|
||||
|
||||
/** Older connections from home get the DHCP name their address has now. */
|
||||
function with_hosts(ev) {
|
||||
let names = null;
|
||||
for (let e in ev)
|
||||
if ((e.type == 'connect' || e.type == 'roam') && !e.hosts) {
|
||||
let h = net.event_hosts(e, names ??= net.lan_names());
|
||||
if (h)
|
||||
e.hosts = h;
|
||||
}
|
||||
return ev;
|
||||
}
|
||||
|
||||
function server_view(m, srv, usage, dmp, rates, days) {
|
||||
let online = 0, peers = 0;
|
||||
let links = map(core.tunnels(srv), (t) => t.ifname);
|
||||
let ids = {};
|
||||
for (let id, d in m.devices)
|
||||
ids[d.public_key] = id;
|
||||
for (let k, p in dmp.peers)
|
||||
if (index(links, p.ifname) >= 0) {
|
||||
peers++;
|
||||
if (live.online(dmp, ids[k], p))
|
||||
online++;
|
||||
}
|
||||
let st = srv.private_key ? awg.server_state(srv) : { up: false, uptime: 0 };
|
||||
// The newest handshake from a public address that is not the router's own:
|
||||
// proof that the port is reachable from the internet.
|
||||
let own = own_addresses(m);
|
||||
let reached = 0;
|
||||
for (let id, d in m.devices)
|
||||
if (m.people[d.person]?.server == srv.id)
|
||||
for (let ip, ts in usage.devices?.[id]?.ips ?? {})
|
||||
if (geo.is_public(ip) && !own[ip] && ts > reached)
|
||||
reached = ts;
|
||||
return {
|
||||
id: srv.id, name: srv.name, ifname: srv.ifname, port: srv.port, subnet: srv.subnet, reached,
|
||||
endpoint: core.endpoint_host(m, srv), public_key: srv.public_key, enabled: srv.enabled,
|
||||
lan_like: srv.lan_like, device_mesh: srv.device_mesh, mesh: srv.mesh, preset: srv.preset, mtu: srv.mtu, keepalive: srv.keepalive, dns: srv.dns,
|
||||
// What devices get now, by kind (mtu 0 = automatic).
|
||||
mtus: { computer: core.device_mtu(srv, { kind: 'computer' }), phone: core.device_mtu(srv, { kind: 'phone' }) },
|
||||
address: core.server_address(srv), up: !!st.up, uptime: st.uptime ?? 0,
|
||||
// Why the tunnel cannot run (its interface name belongs to another network section), or null.
|
||||
// Why it can't start, as a whole sentence in the admin's language (stored as a code).
|
||||
blocked: awg.blocked_text(srv),
|
||||
// Home networks (interfaces) for the per-person picker.
|
||||
home: map(net.server_net(srv).home, (h) => ({ dev: h.dev, nets: h.nets })),
|
||||
// The header protection key is a secret every device shares: whether there is one.
|
||||
peers, online, obf: awg.obf_public(srv.obf), hpk: !!srv.obf.header_protection_key,
|
||||
// Bytes per second, one point a minute: [ts, rx, tx] (rx = from devices).
|
||||
rate: rates[srv.id] ?? [],
|
||||
// Daily traffic of the people on this server (its Overview chart).
|
||||
series: live.series(map(filter(keys(m.people), (pid) => m.people[pid].server == srv.id), (pid) => usage.people?.[pid]), days),
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
* Everything the pages show at once. Readable by read-only users: no personal
|
||||
* codes or links here (person_links has them), and no keys but public ones
|
||||
* (device_config has the rest).
|
||||
*/
|
||||
function status(a) {
|
||||
let m = core.load();
|
||||
let usage = core.load_usage();
|
||||
let dmp = live.dump(m);
|
||||
let states = view.states(m, usage);
|
||||
let days = clamp_days(a.days);
|
||||
|
||||
let servers = [];
|
||||
let rates = read_json(`${RUN_DIR}/rate.json`, {});
|
||||
let prates = read_json(`${RUN_DIR}/rate_people.json`, {});
|
||||
for (let id, srv in m.servers)
|
||||
push(servers, server_view(m, srv, usage, dmp, rates, days));
|
||||
|
||||
let people = [];
|
||||
for (let id, p in m.people) {
|
||||
let pv = view.person_view(m, p, usage, dmp, states, { admin: true });
|
||||
pv.rate = type(prates[p.id]?.[0]) == 'array' ? prates[p.id] : [];
|
||||
push(people, pv);
|
||||
}
|
||||
people = sort(people, (a, b) => (b.online - a.online) || (b.last_seen - a.last_seen) || (a.name < b.name ? -1 : 1));
|
||||
|
||||
let totals = { people: length(people), devices: 0, online: 0, active: 0 };
|
||||
for (let p in people) {
|
||||
totals.devices += length(p.devices);
|
||||
totals.online += p.online;
|
||||
if (p.state == 'active')
|
||||
totals.active++;
|
||||
}
|
||||
let recs = values(usage.people ?? {});
|
||||
let day = live.series(recs, 1)[0];
|
||||
totals.today = [ day.rx, day.tx ];
|
||||
let mon = [ 0, 0 ];
|
||||
for (let r in recs) {
|
||||
let v = live.month(r);
|
||||
mon[0] += v[0];
|
||||
mon[1] += v[1];
|
||||
}
|
||||
totals.month = mon;
|
||||
|
||||
let events = live.load_events();
|
||||
let s = m.settings;
|
||||
return {
|
||||
geo: geo_view(m, usage, dmp),
|
||||
version: VERSION,
|
||||
now: time(),
|
||||
hostname: trim(readfile('/proc/sys/kernel/hostname') ?? ''),
|
||||
settings: {
|
||||
portal_enabled: s.portal_enabled, portal_port: s.portal_port, portal_host: s.portal_host,
|
||||
portal_public_port: core.portal_public_port(m),
|
||||
default_device_limit: s.default_device_limit, domain: s.domain,
|
||||
hostnames: s.hostnames, geo_enabled: s.geo_enabled, home_dns_fallback: s.home_dns_fallback,
|
||||
},
|
||||
servers,
|
||||
people,
|
||||
services: values(m.services),
|
||||
circles: values(m.circles),
|
||||
totals,
|
||||
series: live.series(recs, days),
|
||||
events: with_hosts(slice(events, length(events) > 60 ? length(events) - 60 : 0)),
|
||||
like_home: loop.status(),
|
||||
awg: awg.caps(),
|
||||
awg_state: awg.state(),
|
||||
// The router's public IPv4 (null when none or private): a server whose public address is an old one is told so.
|
||||
wan: wan_public(m),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/** The newest `limit` events; `person` keeps one person's only, `since` only those after a time. */
|
||||
function events_list(a) {
|
||||
// Tunnel down/up events of older versions: the service log tells it better (names, reasons).
|
||||
let person = str(a.person), since = +a.since || 0;
|
||||
let ev = filter(live.load_events(), (e) => e.type != 'server_down' && e.type != 'server_up' &&
|
||||
(!person || e.person == person) && (!since || (e.ts ?? 0) > since));
|
||||
let n = +a.limit || 300;
|
||||
return { events: with_hosts(slice(ev, length(ev) > n ? length(ev) - n : 0)), total: length(ev), policy: live.policy() };
|
||||
}
|
||||
|
||||
/** Remove activity by person, device, category and/or age; nothing given clears it all. */
|
||||
function logs_clear(a) {
|
||||
let m = core.load();
|
||||
let f = {};
|
||||
if (a.person) {
|
||||
if (!m.people[a.person])
|
||||
die(_('Unknown person.'));
|
||||
f.person = a.person;
|
||||
}
|
||||
if (a.device)
|
||||
f.device = a.device;
|
||||
if (a.category) {
|
||||
if (index(live.CATEGORIES, a.category) < 0)
|
||||
die(_('Unknown category.'));
|
||||
f.category = a.category;
|
||||
}
|
||||
if (a.older_days) {
|
||||
if (!match(a.older_days, /^[1-9][0-9]{0,3}$/))
|
||||
die(_('Older than must be a number of days.'));
|
||||
f.before = time() - +a.older_days * 86400;
|
||||
}
|
||||
let removed = live.clear_events(f);
|
||||
if (removed)
|
||||
admin_log('dawnline', 'cleared', null, 'the activity log',
|
||||
join(' · ', filter([ `${removed} event${removed == 1 ? '' : 's'}`, f.category, f.person ? m.people[f.person].name : null,
|
||||
a.older_days ? `older than ${a.older_days} days` : null ], (x) => x)));
|
||||
return { ok: true, removed };
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset traffic history (all | person | device). The agent owns the live
|
||||
* counters, so it gets a marker; the tmpfs copy is edited too so the UI
|
||||
* shows the result straight away.
|
||||
*/
|
||||
function usage_clear(a) {
|
||||
let m = core.load();
|
||||
if (index([ 'all', 'person', 'device' ], a.scope) < 0)
|
||||
die(_('Unknown scope.'));
|
||||
if (a.scope == 'person' && !m.people[a.id])
|
||||
die(_('Unknown person.'));
|
||||
if (a.scope == 'device' && !m.devices[a.id])
|
||||
die(_('Unknown device.'));
|
||||
let req = { scope: a.scope, id: a.id, ts: time() };
|
||||
ensure_dir(RUN_DIR);
|
||||
let l = lock('usage-reset');
|
||||
let pending = read_json(`${RUN_DIR}/usage.reset`, []);
|
||||
push(pending, req);
|
||||
write_json(`${RUN_DIR}/usage.reset`, pending, 0o600);
|
||||
unlock(l);
|
||||
let usage = core.load_usage();
|
||||
if (usage) {
|
||||
live.reset_usage(usage, m, a.scope, a.id);
|
||||
write_json(`${RUN_DIR}/usage.json`, usage, 0o600);
|
||||
}
|
||||
admin_log('dawnline', 'reset traffic history', null,
|
||||
a.scope == 'all' ? 'for everyone' : a.scope == 'person' ? `for ${m.people[a.id].name}` : `for ${m.devices[a.id].name} (${m.people[m.devices[a.id].person]?.name})`);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/** The live revision (dawnline.live.PRESENCE): pages refresh when it moves; 0 without the agent. */
|
||||
/** The live revision, and the version answering (a page notices an update under it). */
|
||||
function rev() {
|
||||
return { rev: +(live.presence()?.rev ?? 0), version: VERSION };
|
||||
}
|
||||
|
||||
/*
|
||||
* The live meter (Overview): each call keeps the agent's watcher writing out
|
||||
* the points it keeps for meter.WANT_FOR seconds more (a first call finds
|
||||
* none yet: the watcher writes the last two minutes within a second) and
|
||||
* returns them (dawnline.meter.read), those newer than `since` when given. Read-only users may look: server ids
|
||||
* and bytes per second only, no keys or personal codes.
|
||||
*/
|
||||
function meter_points(a) {
|
||||
meter.want();
|
||||
return meter.read(+a.since || 0);
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
rev: { call: wrap(rev) },
|
||||
meter: { args: { since: 0 }, call: wrap(meter_points) },
|
||||
status: { args: { days: 30 }, call: wrap(status) },
|
||||
events: { args: { limit: 300, person: '', since: 0 }, call: wrap(events_list) },
|
||||
logs_clear: { args: { person: '', device: '', category: '', older_days: '' }, call: wrap(logs_clear) },
|
||||
usage_clear: { args: { scope: '', id: '' }, call: wrap(usage_clear) },
|
||||
geo_clear: { call: wrap((a) => { geo.clear(); return { ok: true }; }) },
|
||||
};
|
||||
@@ -1,152 +0,0 @@
|
||||
// Dawnline rpcd methods for the Components tab: status, starting update jobs
|
||||
// (the updater does the work in the background), releases, release notes
|
||||
// and the changes between two releases. luci.dawnline merges `methods`.
|
||||
|
||||
import { readfile } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import { read_json, ensure_dir, VERSION, REPO, RUN_DIR } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import * as health from 'dawnline.health';
|
||||
import * as upd from 'dawnline.updates';
|
||||
import * as gh from 'dawnline.github';
|
||||
import * as builds from 'dawnline.awg_builds';
|
||||
import { NAMES as PKG_NAMES } from 'dawnline.upd_pkgs';
|
||||
import { langs } from 'dawnline.upd_app';
|
||||
import * as i18n from 'dawnline.i18n';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
const TAG = /^v?[0-9][0-9A-Za-z.+~-]{0,40}$/;
|
||||
// check[:app|amneziawg|packages] | app[@tag|:reinstall|:rollback|:lang:<code>|:nolang]
|
||||
// | amneziawg[:auto|slava|grey[:ru]|:rollback] | pkgs:<name,…> (only packages Dawnline manages)
|
||||
const TARGET = /^(check(:(app|amneziawg|packages))?|app(@v?[0-9][0-9A-Za-z.+~-]{0,40}|:reinstall|:rollback|:lang:[a-z]{2,3}(_[A-Za-z]{2,4})?|:nolang)?|amneziawg(:rollback|:(auto|slava|grey)(:ru)?)?|pkgs:[a-z0-9][a-z0-9.+,-]*)$/;
|
||||
|
||||
function wrap(fn) {
|
||||
return function(req) {
|
||||
try {
|
||||
let r = fn(req.args ?? {});
|
||||
return type(r) == 'object' ? r : { result: r };
|
||||
}
|
||||
catch (e) {
|
||||
return { error: e.message ?? `${e}` };
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** AmneziaWG interfaces by name: switching builds restarts them. */
|
||||
function awg_tunnels() {
|
||||
let out = [];
|
||||
let c = cursor();
|
||||
c.load('network');
|
||||
c.foreach('network', 'interface', (s) => {
|
||||
if (s.proto == 'amneziawg' && s.disabled != '1')
|
||||
push(out, s._dawnline_name ?? s['.name']);
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
// A line of /etc/openwrt_release. A module constant, compiled once, not for each line.
|
||||
const RELEASE_LINE = /^DISTRIB_(RELEASE|TARGET|ARCH)='([^']*)'/;
|
||||
|
||||
function status(a) {
|
||||
let m = core.load();
|
||||
let rel = {};
|
||||
for (let line in split(readfile('/etc/openwrt_release') ?? '', '\n')) {
|
||||
let x = match(line, RELEASE_LINE);
|
||||
if (x)
|
||||
rel[lc(x[1])] = x[2];
|
||||
}
|
||||
return {
|
||||
version: VERSION,
|
||||
firmware: { version: rel.release ?? '', target: rel.target ?? '', arch: rel.arch ?? '' },
|
||||
portal: m.settings.portal_enabled,
|
||||
home: length(filter(values(m.servers), (x) => x.lan_like)) > 0,
|
||||
needs: health.needs(),
|
||||
check: read_json(upd.CHECK_FILE, null),
|
||||
job: upd.job(),
|
||||
now: time(),
|
||||
repo: REPO,
|
||||
policy: m.settings.updates,
|
||||
auto: upd.auto_state(),
|
||||
kept: upd.kept_all(builds.firmware()?.postfix),
|
||||
// The admin's translations installed (luci-i18n-dawnline-<lang>).
|
||||
translations: langs(),
|
||||
history: slice(upd.history(), 0, 12),
|
||||
tunnels: awg_tunnels(),
|
||||
};
|
||||
}
|
||||
|
||||
/** Releases for the Versions list (Dawnline), five per page. */
|
||||
function releases(a) {
|
||||
let page = +(a.page ?? 1);
|
||||
if (page != int(page) || page < 1 || page > 50)
|
||||
die(_('Unknown page.'));
|
||||
return { releases: map(gh.releases(REPO, page), (x) => ({ ...x, body: null, digests: null, ids: null })), page };
|
||||
}
|
||||
|
||||
/** Release notes for What's new: Dawnline by tag, or an AmneziaWG build's release. */
|
||||
function notes(a) {
|
||||
let rp = a.component == 'amneziawg' ? builds.SOURCES[a.build || 'slava']?.repo : REPO;
|
||||
let tag = a.tag ?? '';
|
||||
if (!rp || !match(tag, TAG))
|
||||
die(_('Unknown release.'));
|
||||
let r = gh.release(rp, tag);
|
||||
if (!r && a.component != 'amneziawg' && substr(tag, 0, 1) != 'v')
|
||||
r = gh.release(rp, `v${tag}`);
|
||||
if (!r)
|
||||
die(sprintf(_('No release %s in %s.'), tag, rp));
|
||||
return { ...r, digests: null, ids: null, repo: rp };
|
||||
}
|
||||
|
||||
/** What changed between the installed Dawnline and a release (points for the Components tab). */
|
||||
function changes(a) {
|
||||
let to = a.to ?? '', from = a.from ?? '';
|
||||
if (!match(to, TAG) || (from != '' && !match(from, TAG)))
|
||||
die(_('Unknown release.'));
|
||||
// The installed version as a tag when it was released (a build ahead of the releases has none).
|
||||
let base = null;
|
||||
for (let t in (from != '' ? [ `v${replace(from, /^v/, '')}`, replace(from, /^v/, '') ] : []))
|
||||
if (!base && gh.release(REPO, t))
|
||||
base = t;
|
||||
return { items: gh.changes(REPO, base, to) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Start an update job. Returns the new job once its worker has written it
|
||||
* (a quick check may be over by then), else null (the caller follows
|
||||
* updates_status); never an older job. Waits two seconds at most: LuCI gives
|
||||
* up on a call after twenty.
|
||||
*/
|
||||
function run(a) {
|
||||
let what = a.what ?? 'check';
|
||||
if (length(what) > 400 || !match(what, TARGET))
|
||||
die(_('Unknown update target.'));
|
||||
// Checked here too, so a wrong name never becomes a failed job in the history.
|
||||
if (substr(what, 0, 5) == 'pkgs:')
|
||||
for (let n in split(substr(what, 5), ','))
|
||||
if (index(PKG_NAMES, n) < 0)
|
||||
die(n == '' ? _('Unknown update target.') : sprintf(_('%s is not a package Dawnline manages'), n));
|
||||
if (upd.busy())
|
||||
die(_('Another component action is already running'));
|
||||
ensure_dir(RUN_DIR);
|
||||
let t0 = time(), prev = read_json(upd.JOB_FILE, null)?.id;
|
||||
let args = substr(what, 0, 5) == 'check' ? `check ${substr(what, 6)}` : `install ${what}`;
|
||||
// The job speaks the admin's language (use() let only a plain language code through).
|
||||
let lang = i18n.current();
|
||||
system(`${lang ? `DAWNLINE_LANG=${lang} ` : ''}start-stop-daemon -S -b -x /usr/libexec/dawnline/updater -- ${args}`);
|
||||
for (let i = 0; i < 20; i++) {
|
||||
let j = read_json(upd.JOB_FILE, null);
|
||||
if (j && j.id != prev && j.started >= t0)
|
||||
return { started: true, job: j };
|
||||
// ucode's own sleep: OpenWrt's BusyBox has no usleep.
|
||||
sleep(100);
|
||||
}
|
||||
return { started: true, job: null };
|
||||
}
|
||||
|
||||
export const methods = {
|
||||
updates_status: { call: wrap(status) },
|
||||
updates_run: { args: { what: '' }, call: wrap(run) },
|
||||
updates_releases: { args: { page: 1 }, call: wrap(releases) },
|
||||
updates_notes: { args: { component: '', tag: '', build: '' }, call: wrap(notes) },
|
||||
updates_changes: { args: { from: '', to: '' }, call: wrap(changes) },
|
||||
};
|
||||
@@ -1,279 +0,0 @@
|
||||
// Dawnline — what the settings section may hold: one entry per option with
|
||||
// its type, default and range, used to validate what the admin saves and to
|
||||
// parse what is stored. Also the address checks the rest of the code shares.
|
||||
//
|
||||
// Booleans are stored as '1' / '0' only, and parsed the way the init script's
|
||||
// config_get_bool does, so the service and the code never disagree.
|
||||
|
||||
import { stat } from 'fs';
|
||||
import { ip2int, valid_host, ulength } from 'dawnline.util';
|
||||
import { INTERVALS, TARGETS } from 'dawnline.updates';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/* ---------- addresses ---------- */
|
||||
|
||||
/** An IPv4 address in dotted form. */
|
||||
export function ipv4(s) {
|
||||
return type(s) == 'string' && ip2int(s) != null;
|
||||
};
|
||||
|
||||
/**
|
||||
* An IPv6 address → its eight 16-bit groups, or null. Accepts '::' and an
|
||||
* embedded IPv4 at the end (::ffff:192.0.2.1); no zone ids or prefixes.
|
||||
*/
|
||||
export function ipv6_groups(s) {
|
||||
if (type(s) != 'string' || length(s) < 2 || length(s) > 45 || !match(s, /^[0-9A-Fa-f:.]+$/))
|
||||
return null;
|
||||
let tail = [];
|
||||
let v4 = match(s, /^(.*:)([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)$/);
|
||||
if (v4) {
|
||||
let n = ip2int(v4[2]);
|
||||
if (n == null)
|
||||
return null;
|
||||
tail = [ (n >> 16) & 0xffff, n & 0xffff ];
|
||||
s = substr(v4[1], -2) == '::' ? v4[1] : substr(v4[1], 0, length(v4[1]) - 1);
|
||||
}
|
||||
else if (index(s, '.') >= 0)
|
||||
return null;
|
||||
let halves = split(s, '::');
|
||||
if (length(halves) > 2)
|
||||
return null;
|
||||
let groups = (part) => {
|
||||
let out = [];
|
||||
if (part == '')
|
||||
return out;
|
||||
for (let g in split(part, ':')) {
|
||||
if (!match(g, /^[0-9A-Fa-f]{1,4}$/))
|
||||
return null;
|
||||
push(out, hex(g));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
let head = groups(halves[0]);
|
||||
let rest = length(halves) == 2 ? groups(halves[1]) : [];
|
||||
if (head == null || rest == null)
|
||||
return null;
|
||||
if (length(halves) == 1) {
|
||||
push(head, ...tail);
|
||||
return length(head) == 8 ? head : null;
|
||||
}
|
||||
push(rest, ...tail);
|
||||
let gap = 8 - length(head) - length(rest);
|
||||
if (gap < 1)
|
||||
return null;
|
||||
let out = [ ...head ];
|
||||
for (let i = 0; i < gap; i++)
|
||||
push(out, 0);
|
||||
push(out, ...rest);
|
||||
return out;
|
||||
};
|
||||
|
||||
/** An IPv6 address (any valid notation). */
|
||||
export function ipv6(s) {
|
||||
return ipv6_groups(s) != null;
|
||||
};
|
||||
|
||||
/** An IPv4 or IPv6 address. */
|
||||
export function ip(s) {
|
||||
return ipv4(s) || ipv6(s);
|
||||
};
|
||||
|
||||
/** A TCP/UDP port as typed: 1-5 digits, 1-65535. */
|
||||
export function valid_port(v) {
|
||||
let s = trim(`${v ?? ''}`);
|
||||
return match(s, /^[0-9]{1,5}$/) != null && +s >= 1 && +s <= 65535;
|
||||
};
|
||||
|
||||
/* ---------- parsing stored values ---------- */
|
||||
|
||||
/** A stored boolean, as the init script's config_get_bool reads it. */
|
||||
export function bool(v, dflt) {
|
||||
v = lc(trim(`${v ?? ''}`));
|
||||
if (index([ '1', 'on', 'true', 'yes', 'enabled' ], v) >= 0)
|
||||
return true;
|
||||
if (index([ '0', 'off', 'false', 'no', 'disabled' ], v) >= 0)
|
||||
return false;
|
||||
return dflt;
|
||||
};
|
||||
|
||||
/** A stored whole number within [min, max], else the default. */
|
||||
export function to_int(v, dflt, lo, hi) {
|
||||
let s = trim(`${v ?? ''}`);
|
||||
if (!match(s, /^-?[0-9]{1,9}$/))
|
||||
return dflt;
|
||||
let n = +s;
|
||||
return (lo != null && n < lo) || (hi != null && n > hi) ? dflt : n;
|
||||
};
|
||||
|
||||
/* ---------- the settings section ---------- */
|
||||
|
||||
/*
|
||||
* type: bool | int | enum | host | hosts | url | file | domain | ipv4 | time | targets
|
||||
* dflt: the parsed default; restart: the portal (uhttpd) must restart when it changes;
|
||||
* min/max: ints; values: enums. The error for a bad value is in messages().
|
||||
*/
|
||||
export const SETTINGS = {
|
||||
portal_enabled: { type: 'bool', dflt: false, restart: true },
|
||||
portal_host: { type: 'host', dflt: '', restart: true },
|
||||
portal_alt: { type: 'hosts', dflt: [], restart: true },
|
||||
portal_port: { type: 'int', dflt: 8443, min: 1, max: 65535, restart: true },
|
||||
portal_redirect: { type: 'url', dflt: '' },
|
||||
portal_cert: { type: 'file', dflt: '', restart: true },
|
||||
portal_key: { type: 'file', dflt: '', restart: true },
|
||||
default_device_limit: { type: 'int', dflt: 3, min: 1, max: 32 },
|
||||
hostnames: { type: 'bool', dflt: true },
|
||||
domain: { type: 'domain', dflt: 'lan' },
|
||||
// Off until the admin turns it on: lookups send people's addresses to a third party.
|
||||
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 },
|
||||
log_persist: { type: 'enum', dflt: 'hourly', values: [ '10min', 'hourly', 'off' ] },
|
||||
usage_keep_days: { type: 'int', dflt: 62, min: 7, max: 1830 },
|
||||
usage_keep_months: { type: 'int', dflt: 24, min: 1, max: 120 },
|
||||
// Empty means "no fallback" (stored as 'none'); unset means the default.
|
||||
home_dns_fallback: { type: 'ipv4', dflt: '1.1.1.1' },
|
||||
update_check: { type: 'bool', dflt: true },
|
||||
update_interval: { type: 'enum', dflt: '1d', values: keys(INTERVALS) },
|
||||
update_auto: { type: 'bool', dflt: true },
|
||||
update_mode: { type: 'enum', dflt: 'scheduled', values: [ 'immediate', 'scheduled' ] },
|
||||
update_time: { type: 'time', dflt: '04:00' },
|
||||
update_targets: { type: 'targets', dflt: [ 'app', 'packages' ] },
|
||||
update_keep: { type: 'bool', dflt: false },
|
||||
update_mirrors: { type: 'bool', dflt: true },
|
||||
};
|
||||
|
||||
/** Ports the portal never takes: SSH, DNS and the LuCI web server. */
|
||||
export const RESERVED_PORTS = [ 22, 53, 80, 443 ];
|
||||
|
||||
/** What the admin is told about a bad value, by setting (made per call: the language changes between calls). */
|
||||
function messages() {
|
||||
return {
|
||||
portal_host: _('Public address must be a hostname or IP address.'),
|
||||
portal_alt: _('Other addresses must be hostnames or IP addresses.'),
|
||||
portal_port: _('Choose a free port between 1 and 65535 (not 22, 53, 80 or 443).'),
|
||||
portal_redirect: _('Redirect must be a full http:// or https:// address.'),
|
||||
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.'),
|
||||
usage_keep_days: _('Daily traffic history must be 7-1830 days.'),
|
||||
usage_keep_months: _('Monthly traffic history must be 1-120 months.'),
|
||||
home_dns_fallback: _('Fallback DNS must be an IPv4 address (or empty for none).'),
|
||||
update_interval: _('Check every 6h, 12h, 1d or 7d.'),
|
||||
update_mode: _('Install updates immediately or scheduled.'),
|
||||
update_time: _('Enter a time as HH:MM, 00:00 to 23:59.'),
|
||||
};
|
||||
}
|
||||
|
||||
function bad(key) {
|
||||
die(messages()[key] ?? sprintf(_('Invalid value for %s.'), key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check a value the admin saves for setting `key`. Returns what to store: a
|
||||
* string, a list, or null to remove the option (back to the default). Dies
|
||||
* with a message for the admin when the value is not acceptable.
|
||||
*/
|
||||
export function validate(key, val) {
|
||||
let spec = SETTINGS[key];
|
||||
if (!spec)
|
||||
die(sprintf(_('Unknown setting %s.'), key));
|
||||
let t = spec.type;
|
||||
if (t == 'hosts') {
|
||||
let lst = [];
|
||||
for (let x in (type(val) == 'array' ? val : [])) {
|
||||
// A typo must not vanish on Save: refused, like the main address.
|
||||
if (type(x) != 'string' || (trim(x) != '' && !valid_host(trim(x))))
|
||||
bad(key);
|
||||
if (trim(x) != '' && index(lst, trim(x)) < 0)
|
||||
push(lst, trim(x));
|
||||
}
|
||||
return length(lst) ? lst : null;
|
||||
}
|
||||
if (t == 'targets') {
|
||||
let lst = [];
|
||||
for (let x in (type(val) == 'array' ? val : []))
|
||||
if (index(TARGETS, x) >= 0 && index(lst, x) < 0)
|
||||
push(lst, x);
|
||||
// An empty list is a choice too ("none"), unlike a missing option (the default).
|
||||
return length(lst) ? lst : [ 'none' ];
|
||||
}
|
||||
if (type(val) == 'bool')
|
||||
val = val ? '1' : '0';
|
||||
val = trim(`${val ?? ''}`);
|
||||
if (t == 'ipv4' && val == '')
|
||||
return 'none';
|
||||
if (val == '')
|
||||
return null;
|
||||
if (t == 'bool') {
|
||||
if (val != '0' && val != '1')
|
||||
die(sprintf(_('%s is 0 or 1.'), key));
|
||||
}
|
||||
else if (t == 'int') {
|
||||
if (!match(val, /^[0-9]{1,9}$/) || +val < spec.min || +val > spec.max)
|
||||
bad(key);
|
||||
val = `${+val}`;
|
||||
}
|
||||
else if (t == 'enum') {
|
||||
if (index(spec.values, val) < 0)
|
||||
bad(key);
|
||||
}
|
||||
else if (t == 'host') {
|
||||
if (!valid_host(val))
|
||||
bad(key);
|
||||
}
|
||||
else if (t == 'url') {
|
||||
if (length(val) > 300 || !match(val, /^https?:\/\/[A-Za-z0-9.-]+(:[0-9]+)?(\/[^[:space:]"'<>]*)?$/))
|
||||
bad(key);
|
||||
}
|
||||
else if (t == 'file') {
|
||||
if (substr(val, 0, 1) != '/' || !stat(val))
|
||||
die(sprintf(_('File %s does not exist.'), val));
|
||||
}
|
||||
else if (t == 'domain') {
|
||||
if (!match(val, /^[a-z0-9-]{1,32}(\.[a-z0-9-]{1,32})*$/))
|
||||
bad(key);
|
||||
}
|
||||
else if (t == 'ipv4') {
|
||||
if (!ipv4(val))
|
||||
bad(key);
|
||||
}
|
||||
else if (t == 'time') {
|
||||
if (!match(val, /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/))
|
||||
bad(key);
|
||||
}
|
||||
return val;
|
||||
};
|
||||
|
||||
/**
|
||||
* A stored setting parsed by its schema entry: the typed value, or the default
|
||||
* when it is missing or invalid. For dawnline.core's load().
|
||||
*/
|
||||
export function get(raw, key) {
|
||||
let spec = SETTINGS[key];
|
||||
let v = raw?.[key];
|
||||
if (!spec)
|
||||
return v;
|
||||
let t = spec.type;
|
||||
if (t == 'bool')
|
||||
return bool(v, spec.dflt);
|
||||
if (t == 'int')
|
||||
return to_int(v, spec.dflt, spec.min, spec.max);
|
||||
if (t == 'hosts' || t == 'targets')
|
||||
return v == null ? spec.dflt : filter(type(v) == 'array' ? v : [ v ], (x) => x != '');
|
||||
if (v == null || v == '')
|
||||
return spec.dflt;
|
||||
if (t == 'enum')
|
||||
return index(spec.values, v) >= 0 ? v : spec.dflt;
|
||||
if (t == 'ipv4')
|
||||
return v == 'none' ? '' : (ipv4(v) ? v : spec.dflt);
|
||||
return v;
|
||||
};
|
||||
@@ -1,52 +0,0 @@
|
||||
// Dawnline — changes to /etc/config/dawnline, the one way: under the config
|
||||
// lock, against a model loaded inside that lock, committed with the file kept
|
||||
// private. rpcd, the portal, the CLI and the agent all go through here, so
|
||||
// no change is made on a stale model and none is lost to another writer.
|
||||
|
||||
import { lock, unlock } from 'dawnline.util';
|
||||
import * as core from 'dawnline.core';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
/** Section names that are never ids of people, devices, servers, services or circles. */
|
||||
export const RESERVED_IDS = [ 'settings', 'main', 'global', 'defaults' ];
|
||||
|
||||
/**
|
||||
* Commit the dawnline config and keep it readable by root only. A commit
|
||||
* that did not reach flash (full or read-only overlay) is an error: the
|
||||
* caller's change never happened, and nothing downstream may act on it.
|
||||
*/
|
||||
export function commit(c) {
|
||||
if (!c.commit(core.CONFIG)) {
|
||||
let why = c.error();
|
||||
die(why != null ? sprintf(_("Couldn't save /etc/config/dawnline: %s. Is the flash full?"), why)
|
||||
: _("Couldn't save /etc/config/dawnline: the write failed. Is the flash full?"));
|
||||
}
|
||||
system('chmod 600 /etc/config/dawnline');
|
||||
};
|
||||
|
||||
/**
|
||||
* Run fn(c, m) with the config lock held, on a fresh cursor and model; commit
|
||||
* when fn returns (the return value is passed on). Anything fn throws aborts
|
||||
* without committing and is rethrown with the same message.
|
||||
*/
|
||||
export function transact(fn) {
|
||||
let l = lock('config');
|
||||
try {
|
||||
let c = core.open_cursor();
|
||||
let m = core.load(c);
|
||||
let r = fn(c, m);
|
||||
commit(c);
|
||||
unlock(l);
|
||||
return r;
|
||||
}
|
||||
catch (e) {
|
||||
unlock(l);
|
||||
die(e.message ?? `${e}`);
|
||||
}
|
||||
};
|
||||
|
||||
/** Is `id` used by anything in the model (any kind of section), or reserved? */
|
||||
export function id_taken(m, id) {
|
||||
return index(RESERVED_IDS, id) >= 0 || m.servers?.[id] != null || m.people?.[id] != null || m.devices?.[id] != null ||
|
||||
m.services?.[id] != null || m.circles?.[id] != null;
|
||||
};
|
||||
@@ -1,202 +0,0 @@
|
||||
// Dawnline updater: Dawnline itself — check the latest release, install a
|
||||
// release (latest, a tag, the same one again) or the kept previous version.
|
||||
// The admin's translations installed on the router (luci-i18n-dawnline-<lang>)
|
||||
// come from the same release and go in with it, in one transaction.
|
||||
// ctx is the updater's job context (see usr/libexec/dawnline/updater).
|
||||
|
||||
import { readfile, lsdir } from 'fs';
|
||||
import { VERSION, REPO } from 'dawnline.util';
|
||||
import * as upd from 'dawnline.updates';
|
||||
import * as gh from 'dawnline.github';
|
||||
import * as pkg from 'dawnline.pkg';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
function status(installed, latest) {
|
||||
let c = upd.version_cmp(installed, latest);
|
||||
return c < 0 ? 'outdated' : c > 0 ? 'dev' : 'latest';
|
||||
}
|
||||
|
||||
export function check(ctx) {
|
||||
let r = gh.latest(REPO);
|
||||
if (!r)
|
||||
return { installed: VERSION, latest: null, update: false, repo: REPO, status: '', note: sprintf(_('Cannot reach github.com/%s'), REPO) };
|
||||
let latest = replace(r.tag, /^v/, '');
|
||||
let st = status(VERSION, latest);
|
||||
ctx.log(st == 'outdated' ? sprintf(_('Dawnline update is available: %s → %s'), VERSION, latest)
|
||||
: st == 'dev' ? sprintf(_('Installed Dawnline %s is newer than the release %s'), VERSION, latest)
|
||||
: sprintf(_('Dawnline is up to date (%s)'), VERSION));
|
||||
return { installed: VERSION, latest, tag: r.tag, repo: REPO, url: r.url, published: r.published, update: st == 'outdated', status: st };
|
||||
};
|
||||
|
||||
/**
|
||||
* The sha256 a release publishes for a file: GitHub's own (API), else the
|
||||
* release's sha256sums.txt fetched straight from GitHub (never a mirror).
|
||||
*/
|
||||
function want_sha(ctx, tag, asset, dir) {
|
||||
let want = gh.asset_sha256(REPO, tag, asset);
|
||||
if (want)
|
||||
return want;
|
||||
let sums = `${dir}/sha256sums-${replace(tag, /[^A-Za-z0-9.-]/g, '_')}.txt`;
|
||||
if (!gh.download(`https://github.com/${REPO}/releases/download/${tag}/sha256sums.txt`, sums, { mirrors: false, log: ctx.log }))
|
||||
return null;
|
||||
for (let line in split(readfile(sums) ?? '', '\n')) {
|
||||
let m = match(trim(line), /^([0-9a-f]{64})[ \t]+\*?(\S+)$/);
|
||||
if (m && m[2] == asset)
|
||||
return m[1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The languages of the translations installed here, by their catalogs (dawnline.<lang>.lmo). */
|
||||
export function langs() {
|
||||
let out = [];
|
||||
for (let f in lsdir('/usr/lib/lua/luci/i18n') ?? []) {
|
||||
let m = match(f, /^dawnline\.([A-Za-z_-]+)\.lmo$/);
|
||||
if (m)
|
||||
push(out, m[1]);
|
||||
}
|
||||
return sort(out);
|
||||
};
|
||||
|
||||
const i18n_pkg = (lang) => `luci-i18n-dawnline-${lang}`;
|
||||
|
||||
/**
|
||||
* Download one file of a release, verified against its sha256. `optional`: a
|
||||
* release without it (one from before the translations) gives null.
|
||||
*/
|
||||
function fetch_file(ctx, tag, asset, dir, optional) {
|
||||
ctx.phase(sprintf(_('Downloading %s'), asset));
|
||||
let want = want_sha(ctx, tag, asset, dir);
|
||||
if (!want && optional) {
|
||||
ctx.log(sprintf(_('Release %s has no %s: it stays as it is'), tag, asset));
|
||||
return null;
|
||||
}
|
||||
if (!want)
|
||||
die(sprintf(_('Release %s publishes no checksum for %s: not installing an unverified package'), tag, asset));
|
||||
let f = `${dir}/${asset}`;
|
||||
if (!gh.download(`https://github.com/${REPO}/releases/download/${tag}/${asset}`, f, { sha256: want, mirrors: ctx.pol.mirrors, log: ctx.log }))
|
||||
die(sprintf(_('Download failed: %s'), asset));
|
||||
ctx.log(sprintf(_('sha256 matches the release (%s…)'), substr(want, 0, 12)));
|
||||
return f;
|
||||
}
|
||||
|
||||
/** A release's package and the translations installed here, verified. */
|
||||
function fetch(ctx, tag, dir) {
|
||||
let ver = replace(tag, /^v/, '');
|
||||
let files = [ fetch_file(ctx, tag, `luci-app-dawnline-${ver}.${pkg.ext()}`, dir) ];
|
||||
for (let l in langs()) {
|
||||
let f = fetch_file(ctx, tag, `${i18n_pkg(l)}-${ver}.${pkg.ext()}`, dir, true);
|
||||
if (f)
|
||||
push(files, f);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
||||
/** The release tag of a version: tags may or may not carry the v. */
|
||||
function tag_of(ver) {
|
||||
for (let t in [ `v${ver}`, ver ]) {
|
||||
try {
|
||||
if (gh.release(REPO, t))
|
||||
return t;
|
||||
}
|
||||
catch (e) {}
|
||||
}
|
||||
return `v${ver}`;
|
||||
}
|
||||
|
||||
function backup(ctx) {
|
||||
ctx.phase(_('Backing up configuration'));
|
||||
system('/usr/bin/dawnline backup --reason pre-update >/dev/null 2>&1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the translation `lang` of the installed version from its release, or
|
||||
* (lang null) remove the installed ones. LuCI reads its catalogs once per web
|
||||
* server process: it restarts after the job.
|
||||
*/
|
||||
function set_lang(ctx, lang) {
|
||||
if (lang == null) {
|
||||
let have = map(langs(), i18n_pkg);
|
||||
if (length(have)) {
|
||||
ctx.phase(_('Removing the translation'));
|
||||
if (!pkg.remove(have, ctx.log))
|
||||
die(_('The translation could not be removed'));
|
||||
}
|
||||
}
|
||||
else {
|
||||
let tag = tag_of(VERSION);
|
||||
let f = fetch_file(ctx, tag, `${i18n_pkg(lang)}-${VERSION}.${pkg.ext()}`, ctx.work, true);
|
||||
if (!f)
|
||||
die(sprintf(_('Release %s has no translation into %s: install a release that has one'), tag, lang));
|
||||
ctx.phase(_('Installing the translation'));
|
||||
if (!pkg.install_files([ f ], false, ctx.log))
|
||||
die(_('Installation failed'));
|
||||
}
|
||||
ctx.after('luci');
|
||||
return { from: VERSION, to: VERSION, lang };
|
||||
}
|
||||
|
||||
/** arg: null (latest) | @<tag> | reinstall | rollback | lang:<code> | nolang */
|
||||
export function install(ctx, arg) {
|
||||
if (arg == 'nolang')
|
||||
return set_lang(ctx, null);
|
||||
if (substr(arg ?? '', 0, 5) == 'lang:')
|
||||
return set_lang(ctx, substr(arg, 5));
|
||||
let out = { from: VERSION, to: null };
|
||||
if (arg == 'rollback') {
|
||||
let k = upd.kept('app');
|
||||
if (!k)
|
||||
die(_('No previous Dawnline version is kept on the router'));
|
||||
out.to = k.version;
|
||||
ctx.versions(VERSION, k.version);
|
||||
backup(ctx);
|
||||
ctx.phase(sprintf(_('Rolling back to %s'), k.version));
|
||||
if (!pkg.install_files(k.files, true, ctx.log))
|
||||
die(_('Installation failed'));
|
||||
upd.forget('app');
|
||||
ctx.after('app');
|
||||
return out;
|
||||
}
|
||||
let tag, force = false;
|
||||
if (arg == 'reinstall') {
|
||||
tag = tag_of(VERSION);
|
||||
force = true;
|
||||
}
|
||||
else if (arg && substr(arg, 0, 1) == '@') {
|
||||
tag = substr(arg, 1);
|
||||
force = upd.version_cmp(replace(tag, /^v/, ''), VERSION) <= 0;
|
||||
}
|
||||
else {
|
||||
let c = check(ctx);
|
||||
if (!c.latest)
|
||||
die(c.note);
|
||||
if (!c.update) {
|
||||
out.to = VERSION;
|
||||
return out;
|
||||
}
|
||||
tag = c.tag;
|
||||
}
|
||||
out.to = replace(tag, /^v/, '');
|
||||
ctx.versions(VERSION, out.to);
|
||||
let files = fetch(ctx, tag, ctx.work);
|
||||
// The version being replaced, for Roll back (downloaded again: opkg keeps
|
||||
// no package files). Always for an automatic run: nobody is there to
|
||||
// recover a router whose new version does not come up (postupdate rolls
|
||||
// back to it on its own).
|
||||
if ((ctx.pol.keep || ctx.auto?.()) && out.to != VERSION && upd.kept('app')?.version != VERSION) {
|
||||
ctx.phase(sprintf(_('Keeping Dawnline %s for rollback'), VERSION));
|
||||
try {
|
||||
system(`mkdir -p '${ctx.work}/keep'`);
|
||||
upd.keep('app', VERSION, fetch(ctx, tag_of(VERSION), `${ctx.work}/keep`));
|
||||
}
|
||||
catch (e) {
|
||||
ctx.log(sprintf(_('Warning: could not keep %s: %s'), VERSION, e.message));
|
||||
}
|
||||
}
|
||||
backup(ctx);
|
||||
ctx.phase(_('Installing package'));
|
||||
if (!pkg.install_files(files, force, ctx.log))
|
||||
die(_('Installation failed'));
|
||||
ctx.after('app');
|
||||
return out;
|
||||
};
|
||||
@@ -1,274 +0,0 @@
|
||||
// Dawnline updater: AmneziaWG — the builds published for this firmware by
|
||||
// each source, installing or switching a build (verified against GitHub's
|
||||
// sha256), the Russian LuCI translation, and rolling back to a kept build.
|
||||
// Every install and rollback is recorded (dawnline.awg_builds), with whether
|
||||
// the new kernel module runs or waits for the next boot.
|
||||
// ctx is the updater's job context (see usr/libexec/dawnline/updater).
|
||||
|
||||
import { stat } from 'fs';
|
||||
import { cursor } from 'uci';
|
||||
import { iface_intent } from 'dawnline.util';
|
||||
import * as upd from 'dawnline.updates';
|
||||
import * as gh from 'dawnline.github';
|
||||
import * as pkg from 'dawnline.pkg';
|
||||
import * as builds from 'dawnline.awg_builds';
|
||||
import { _, N_ } from 'dawnline.i18n';
|
||||
|
||||
function sh(cmd) {
|
||||
return system(`${cmd} >/dev/null 2>&1`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Download one package of a build to dir, verified when GitHub publishes its
|
||||
* sha256 (then mirrors may serve it); without a sha256 only github.com itself.
|
||||
* Returns the path or null.
|
||||
*/
|
||||
function fetch(ctx, fw, name, src, dir) {
|
||||
let file = builds.asset(fw, name);
|
||||
let dest = `${dir}/${src}_${file}`;
|
||||
let want = gh.asset_sha256(builds.SOURCES[src].repo, fw.tag, file);
|
||||
let url = `https://github.com/${builds.SOURCES[src].repo}/releases/download/${fw.tag}/${file}`;
|
||||
return gh.download(url, dest, { sha256: want, mirrors: ctx.pol.mirrors, log: ctx.log }) ? dest : null;
|
||||
}
|
||||
|
||||
/** The newest tag of the same OpenWrt series (24.10.x) in a repository, if newer than ours. */
|
||||
function newer_series_tag(rp, tag) {
|
||||
let series = match(tag, /^v([0-9]+\.[0-9]+)\./)?.[1];
|
||||
let best = null;
|
||||
try {
|
||||
for (let x in gh.releases(rp, 1)) {
|
||||
let t = x.tag;
|
||||
if (series && match(t, regexp(`^v${replace(series, '.', '\\.')}\\.`)) && pkg.cmp(substr(t, 1), substr(tag, 1)) > 0 &&
|
||||
(!best || pkg.cmp(substr(t, 1), substr(best, 1)) > 0))
|
||||
best = t;
|
||||
}
|
||||
}
|
||||
catch (e) {}
|
||||
return best;
|
||||
}
|
||||
|
||||
export function check(ctx) {
|
||||
let installed = builds.installed();
|
||||
let ru = pkg.version(builds.RU) != null;
|
||||
let cur = installed['amneziawg-tools'];
|
||||
let fw = builds.firmware();
|
||||
if (!fw)
|
||||
return { installed: cur, gen: builds.gen(cur), packages: installed, current: null, ru, sources: {}, update: false, status: '',
|
||||
note: _('Cannot detect the firmware target') };
|
||||
if (fw.snapshot)
|
||||
return { installed: cur, gen: builds.gen(cur), packages: installed, current: null, ru, sources: {}, update: false, status: '',
|
||||
note: _('This is a SNAPSHOT firmware: the AmneziaWG projects publish builds for releases only') };
|
||||
let dir = `${ctx.work}/check`;
|
||||
system(`mkdir -p '${dir}'`);
|
||||
let sources = {};
|
||||
for (let id in builds.ORDER) {
|
||||
let src = builds.SOURCES[id];
|
||||
ctx.log(sprintf(_('Looking for the %s build for OpenWrt %s'), src.label, substr(fw.tag, 1)));
|
||||
let f = fetch(ctx, fw, 'amneziawg-tools', id, dir);
|
||||
let v = f ? pkg.file_version(f) : null;
|
||||
let date = null;
|
||||
try {
|
||||
date = f ? gh.release(src.repo, fw.tag)?.published : null;
|
||||
}
|
||||
catch (e) {}
|
||||
let s = { label: src.label, repo: src.repo, tag: fw.tag, version: v, gen: builds.gen(v), date };
|
||||
// A newer firmware may already get 3.1 from this source.
|
||||
if (s.gen != '3.1') {
|
||||
let nt = newer_series_tag(src.repo, fw.tag);
|
||||
if (nt) {
|
||||
let nf = fetch(ctx, builds.for_tag(fw, nt), 'amneziawg-tools', id, dir);
|
||||
if (builds.gen(nf ? pkg.file_version(nf) : null) == '3.1')
|
||||
s.newer = { tag: nt, gen: '3.1' };
|
||||
}
|
||||
}
|
||||
sources[id] = s;
|
||||
}
|
||||
// The installed build: the source whose package matches. Unknown stays unknown
|
||||
// (an automatic update never switches a build it could not identify).
|
||||
let current = null;
|
||||
for (let id, x in sources)
|
||||
if (!current && cur && x.version == cur)
|
||||
current = id;
|
||||
let mine = current ? sources[current] : null;
|
||||
let c = cur && mine?.version ? pkg.cmp(cur, mine.version) : null;
|
||||
// An update that waits for a reboot: Components says so after a check.
|
||||
let pending = builds.waits_for_reboot();
|
||||
let st = c == null ? '' : c < 0 ? 'outdated' : c > 0 ? 'dev' : 'latest';
|
||||
ctx.log(cur == null ? _('AmneziaWG is not installed')
|
||||
: !current ? sprintf(_('AmneziaWG %s: no source publishes this build for this firmware any more'), cur)
|
||||
: st == 'outdated' ? sprintf(_('AmneziaWG update is available: %s → %s'), cur, mine.version)
|
||||
: sprintf(_('AmneziaWG is up to date (%s)'), cur));
|
||||
return { installed: cur, gen: builds.gen(cur), packages: installed, current, ru, release: fw.tag, sources,
|
||||
latest: mine?.version ?? null, status: st,
|
||||
update: cur == null || st == 'outdated',
|
||||
// An update that changes the generation (2.0 → 3.1) breaks older apps: never automatic.
|
||||
gen_change: cur != null && mine?.version != null && builds.gen(cur) != builds.gen(mine.version),
|
||||
pending,
|
||||
note: !length(filter(values(sources), (x) => x.version)) ? sprintf(_('No AmneziaWG build for OpenWrt %s yet'), substr(fw.tag, 1))
|
||||
: pending ? _('The AmneziaWG update is installed and starts after a reboot.') : null };
|
||||
};
|
||||
|
||||
/**
|
||||
* Tunnels on a new kernel module: every AmneziaWG interface is brought down,
|
||||
* the module swapped and the interfaces brought up again. False when the old
|
||||
* module stays loaded: the tunnels come back on it (the tools talk to an
|
||||
* older module at its own netlink version) and the installed one starts
|
||||
* with the next boot, which caps() and Health then wait for.
|
||||
*/
|
||||
function reload_module(ctx) {
|
||||
let ifaces = [];
|
||||
let c = cursor();
|
||||
c.load('network');
|
||||
c.foreach('network', 'interface', (s) => {
|
||||
if (s.proto == 'amneziawg' && s.disabled != '1')
|
||||
push(ifaces, s['.name']);
|
||||
});
|
||||
let n = length(ifaces);
|
||||
ctx.phase(sprintf(N_(n, 'Reloading the kernel module (%d tunnel restarts)', 'Reloading the kernel module (%d tunnels restart)'), n));
|
||||
for (let i in ifaces) {
|
||||
iface_intent(i, 'update', c.get('network', i, '_dawnline_name') ?? i);
|
||||
sh(`ifdown '${i}'`);
|
||||
}
|
||||
if (stat('/sys/module/amneziawg'))
|
||||
ctx.run('rmmod amneziawg');
|
||||
let swapped = !stat('/sys/module/amneziawg');
|
||||
if (swapped) {
|
||||
ctx.run('modprobe amneziawg');
|
||||
if (!stat('/sys/module/amneziawg'))
|
||||
die(_("The new AmneziaWG kernel module doesn't load on this firmware: the tunnels stay down until it does (see the log above)"));
|
||||
}
|
||||
for (let i in ifaces)
|
||||
sh(`ifup '${i}'`);
|
||||
return swapped;
|
||||
}
|
||||
|
||||
/**
|
||||
* After packages changed: the module loaded, netifd knowing the protocol,
|
||||
* Dawnline set up again. was: the module loaded before (builds.module_now()).
|
||||
* Returns whether the module that runs is the installed one; false while the
|
||||
* old one stays loaded until the next boot.
|
||||
*/
|
||||
function settle(ctx, before, after, was) {
|
||||
let b = before['kmod-amneziawg'], a = after['kmod-amneziawg'];
|
||||
let fresh = true;
|
||||
// A new module package, one over a module loaded by hand, or an earlier
|
||||
// update that still waits for a boot (Reinstall tries again).
|
||||
if (a && (a != b || was.pending) && (b || was.core != null))
|
||||
fresh = reload_module(ctx);
|
||||
else if (a && !b) {
|
||||
ctx.phase(_('Loading the kernel module'));
|
||||
if (ctx.run('modprobe amneziawg').code != 0 && !stat('/sys/module/amneziawg'))
|
||||
die(_("AmneziaWG is installed, but its kernel module doesn't load on this firmware"));
|
||||
}
|
||||
// netifd reads its protocol handlers when it starts.
|
||||
if (index(ctx.run('ubus call network get_proto_handlers').out, '"amneziawg"') < 0) {
|
||||
ctx.phase(_('Restarting the network so it knows AmneziaWG (connections drop for a few seconds)'));
|
||||
ctx.run('/etc/init.d/network restart');
|
||||
}
|
||||
ctx.after('apply');
|
||||
return fresh;
|
||||
}
|
||||
|
||||
/**
|
||||
* The packages are in: settle them and record the build (source, tag) with
|
||||
* the module that ran before (was). An old module that stays loaded is no
|
||||
* failure: the job says so, and Health until the next boot.
|
||||
*/
|
||||
function finish(ctx, out, source, tag, was) {
|
||||
out.after = builds.installed();
|
||||
out.reloaded = settle(ctx, out.before, out.after, was);
|
||||
builds.record(source, tag, out.after, { prev_gen: was.gen, prev_core: was.core, reloaded: out.reloaded });
|
||||
if (!out.reloaded)
|
||||
ctx.log(_('The old kernel module stays loaded (another AmneziaWG interface may use it): the AmneziaWG update is installed and starts after a reboot.'));
|
||||
return out;
|
||||
}
|
||||
|
||||
/** The first source that publishes every package for this firmware. */
|
||||
function pick(fw) {
|
||||
for (let id in builds.ORDER) {
|
||||
let r = null;
|
||||
try {
|
||||
r = gh.release(builds.SOURCES[id].repo, fw.tag);
|
||||
}
|
||||
catch (e) {}
|
||||
if (r && length(filter(builds.PKGS, (p) => index(r.assets, builds.asset(fw, p)) >= 0)) == length(builds.PKGS))
|
||||
return id;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** arg: [auto|slava|grey][:ru] | rollback */
|
||||
export function install(ctx, arg) {
|
||||
let before = builds.installed();
|
||||
let was = builds.module_now(before['kmod-amneziawg']);
|
||||
let out = { from: before['amneziawg-tools'], to: null, before };
|
||||
let fw = builds.firmware();
|
||||
if (!fw)
|
||||
die(_('Cannot detect the firmware target'));
|
||||
if (arg == 'rollback') {
|
||||
let k = upd.kept('amneziawg');
|
||||
if (!k)
|
||||
die(_('No previous AmneziaWG build is kept on the router'));
|
||||
if (k.firmware && k.firmware != fw.postfix)
|
||||
die(_('The kept AmneziaWG build was made for another firmware: it would not load on this one'));
|
||||
out.to = k.version;
|
||||
out.build = k.build;
|
||||
ctx.versions(out.from, out.to);
|
||||
ctx.phase(sprintf(_('Rolling back to %s'), k.version));
|
||||
if (!pkg.install_files(k.files, true, ctx.log))
|
||||
die(_('Package installation failed'));
|
||||
upd.forget('amneziawg');
|
||||
return finish(ctx, out, k.build, fw.tag, was);
|
||||
}
|
||||
let parts = split(arg ?? '', ':');
|
||||
let id = parts[0] || 'auto';
|
||||
let want_ru = parts[1] == 'ru';
|
||||
if (id == 'auto') {
|
||||
id = pick(fw);
|
||||
if (!id)
|
||||
die(sprintf(_('No AmneziaWG build for OpenWrt %s (%s) yet'), substr(fw.tag, 1), fw.postfix));
|
||||
}
|
||||
let src = builds.SOURCES[id];
|
||||
if (!src)
|
||||
die(sprintf(_('Unknown AmneziaWG build %s'), id));
|
||||
out.build = id;
|
||||
let files = [];
|
||||
for (let p in [ ...builds.PKGS, ...(want_ru ? [ builds.RU ] : []) ]) {
|
||||
ctx.phase(sprintf(_('Downloading %s from %s'), p, src.repo));
|
||||
let f = fetch(ctx, fw, p, id, ctx.work);
|
||||
if (!f)
|
||||
die(sprintf(_('No %s build for OpenWrt %s in %s, or it could not be verified'), p, substr(fw.tag, 1), src.repo));
|
||||
push(files, f);
|
||||
}
|
||||
out.to = pkg.file_version(files[1]);
|
||||
ctx.versions(out.from, out.to);
|
||||
// The build being replaced, for Roll back: from another source that still publishes it.
|
||||
if (ctx.pol.keep && out.from && out.to != out.from) {
|
||||
let dir = `${ctx.work}/keep`;
|
||||
system(`mkdir -p '${dir}'`);
|
||||
let kept = null;
|
||||
for (let sid in builds.ORDER) {
|
||||
if (sid == id || kept)
|
||||
continue;
|
||||
let f = fetch(ctx, fw, 'amneziawg-tools', sid, dir);
|
||||
if (f && pkg.file_version(f) == out.from)
|
||||
kept = sid;
|
||||
}
|
||||
if (kept) {
|
||||
ctx.phase(sprintf(_('Keeping AmneziaWG %s for rollback'), out.from));
|
||||
let keepf = filter(map(builds.PKGS, (p) => fetch(ctx, fw, p, kept, dir)), (x) => x);
|
||||
if (length(keepf) == length(builds.PKGS))
|
||||
upd.keep('amneziawg', out.from, keepf, kept, fw.postfix);
|
||||
}
|
||||
else
|
||||
ctx.log(_('Warning: the installed build is no longer published, nothing to keep for rollback'));
|
||||
}
|
||||
ctx.phase(_('Refreshing package lists'));
|
||||
pkg.refresh(ctx.log);
|
||||
ctx.phase(sprintf(_('Installing the %s build'), src.label));
|
||||
if (!pkg.install_files(files, true, ctx.log))
|
||||
die(_('Package installation failed'));
|
||||
if (!want_ru && pkg.version(builds.RU))
|
||||
pkg.remove([ builds.RU ], ctx.log);
|
||||
return finish(ctx, out, id, fw.tag, was);
|
||||
};
|
||||
@@ -1,94 +0,0 @@
|
||||
// Dawnline updater: the OpenWrt packages Dawnline adds — the home-like link
|
||||
// (ip-full, kmod-veth) and HTTPS plus certificates for the personal portal —
|
||||
// checked against and updated from the OpenWrt feeds.
|
||||
// ctx is the updater's job context (see usr/libexec/dawnline/updater).
|
||||
|
||||
import * as pkg from 'dawnline.pkg';
|
||||
import { _, N_ } from 'dawnline.i18n';
|
||||
|
||||
/** What Dawnline itself adds to OpenWrt (the rest of LuCI, ucode, rpcd and uhttpd is the stock system). */
|
||||
export const NAMES = [ 'ip-full', 'kmod-veth', 'libustream-openssl', 'openssl-util', 'acme', 'acme-acmesh-dnsapi', 'luci-app-acme' ];
|
||||
|
||||
// Packages released together with each of the above, updated as a set so OpenSSL never ends up
|
||||
// half old and half new.
|
||||
const FAMILY = [
|
||||
[ /^ip-full$/, /^ip-/ ],
|
||||
[ /^(libustream-openssl|openssl-util)$/, /^(libustream-openssl|libopenssl|openssl-util$)/ ],
|
||||
[ /^(acme|luci-app-acme$)/, /^acme/ ],
|
||||
];
|
||||
|
||||
function family(p) {
|
||||
for (let f in FAMILY)
|
||||
if (match(p, f[0]))
|
||||
return f[1];
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The packages to update for these names: each one plus its upgradable family. */
|
||||
function with_family(names, up) {
|
||||
let set = {};
|
||||
for (let n in names) {
|
||||
if (up[n])
|
||||
set[n] = true;
|
||||
let f = family(n);
|
||||
if (f)
|
||||
for (let u in keys(up))
|
||||
if (match(u, f))
|
||||
set[u] = true;
|
||||
}
|
||||
return sort(keys(set));
|
||||
}
|
||||
|
||||
/** refresh: update the package lists first (a check by hand or on schedule). */
|
||||
export function check(ctx, refresh) {
|
||||
if (refresh ?? true) {
|
||||
ctx.phase(_('Refreshing package lists'));
|
||||
pkg.refresh();
|
||||
}
|
||||
let up = pkg.upgradable();
|
||||
let res = {}, all = {};
|
||||
for (let p in NAMES) {
|
||||
let v = pkg.version(p);
|
||||
let set = v ? with_family([ p ], up) : [];
|
||||
res[p] = { installed: v, latest: v && up[p] ? up[p] : null, with: filter(set, (x) => x != p) };
|
||||
for (let x in set)
|
||||
all[x] = true;
|
||||
}
|
||||
let n = length(keys(all));
|
||||
ctx.log(n ? sprintf(N_(n, '%d package update in the feeds: %s', '%d package updates in the feeds: %s'), n, join(', ', sort(keys(all))))
|
||||
: _('System packages are up to date'));
|
||||
return { pkgs: res, updates: n, status: n ? 'outdated' : 'latest' };
|
||||
};
|
||||
|
||||
/** arg: name,name,… (only names Dawnline manages): missing ones are installed, present ones updated with their family. */
|
||||
export function install(ctx, arg) {
|
||||
let names = filter(split(arg ?? '', ','), (x) => x != '');
|
||||
for (let n in names)
|
||||
if (index(NAMES, n) < 0)
|
||||
die(sprintf(_('%s is not a package Dawnline manages'), n));
|
||||
if (!length(names))
|
||||
die(_('Nothing to install'));
|
||||
ctx.phase(_('Refreshing package lists'));
|
||||
pkg.refresh(ctx.log);
|
||||
let missing = filter(names, (n) => pkg.version(n) == null);
|
||||
let have = with_family(filter(names, (n) => pkg.version(n) != null), pkg.upgradable());
|
||||
if (length(have)) {
|
||||
ctx.phase(sprintf(_('Updating %s'), join(', ', have)));
|
||||
if (!pkg.upgrade(have, ctx.log))
|
||||
die(_('Update failed'));
|
||||
}
|
||||
if (length(missing)) {
|
||||
ctx.phase(sprintf(_('Installing %s'), join(', ', missing)));
|
||||
if (!pkg.add(missing, ctx.log))
|
||||
die(_('Installation failed'));
|
||||
}
|
||||
// The portal's HTTPS runs in uhttpd; a piece that just arrived lets the portal or the home-like link start.
|
||||
let touched = [ ...missing, ...have ];
|
||||
if (length(filter(missing, (n) => match(n, /^(openssl-util|libustream|ip-full|kmod-veth)/)))) {
|
||||
ctx.after('apply');
|
||||
ctx.after('uhttpd');
|
||||
}
|
||||
else if (length(filter(touched, (n) => match(n, /^(libustream|libopenssl)/))))
|
||||
ctx.after('uhttpd');
|
||||
return { updated: have, installed: missing, check: check(ctx, false) };
|
||||
};
|
||||
@@ -1,233 +0,0 @@
|
||||
// Dawnline — what the Components tab, the updater and the agent share: the
|
||||
// update policy (Settings → Updates), when an automatic run is due, the one
|
||||
// update job at a time (a kernel lock), the history for Activity, and the
|
||||
// versions kept for rollback. GitHub and the package manager live in
|
||||
// dawnline.github and dawnline.pkg.
|
||||
|
||||
import { open, stat } from 'fs';
|
||||
import { q, read_json, write_json, ensure_dir, RUN_DIR, DATA_DIR } from 'dawnline.util';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
export const JOB_FILE = `${RUN_DIR}/update-job.json`;
|
||||
export const CHECK_FILE = `${RUN_DIR}/update-check.json`;
|
||||
/** Held (flock) by the updater for the whole job: busy() and stale jobs come from it, not from pids. */
|
||||
export const LOCK_FILE = `${RUN_DIR}/update.lock`;
|
||||
/** Automatic runs: when the last check and the last scheduled install happened. */
|
||||
export const AUTO_FILE = `${DATA_DIR}/update-auto.json`;
|
||||
/** What happened: updates (by hand or automatic), failures, checks that found something. */
|
||||
export const HISTORY_FILE = `${DATA_DIR}/update-history.json`;
|
||||
/** The version each component had before its last update (Keep the previous version). */
|
||||
export const KEEP_DIR = `${DATA_DIR}/keep`;
|
||||
|
||||
export const INTERVALS = { '6h': 6 * 3600, '12h': 12 * 3600, '1d': 86400, '7d': 7 * 86400 };
|
||||
export const TARGETS = [ 'app', 'amneziawg', 'packages' ];
|
||||
|
||||
function on(v, dflt) {
|
||||
if (v == null || v == '')
|
||||
return dflt;
|
||||
return v == '1' || v == 'true' || v == 'on' || v == 'yes';
|
||||
}
|
||||
|
||||
/** The update policy from raw UCI settings (core.load puts it in m.settings.updates). */
|
||||
export function policy(s) {
|
||||
s ??= {};
|
||||
let t = type(s.update_targets) == 'array' ? s.update_targets : s.update_targets ? [ s.update_targets ] : null;
|
||||
let time_ok = match(s.update_time ?? '', /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/);
|
||||
return {
|
||||
check: on(s.update_check, true),
|
||||
interval: INTERVALS[s.update_interval] ? s.update_interval : '1d',
|
||||
auto: on(s.update_auto, true),
|
||||
mode: s.update_mode == 'immediate' ? 'immediate' : 'scheduled',
|
||||
time: time_ok ? s.update_time : '04:00',
|
||||
// AmneziaWG is left out by default: a new kernel module restarts every tunnel.
|
||||
targets: t ? filter(t, (x) => index(TARGETS, x) >= 0) : [ 'app', 'packages' ],
|
||||
keep: on(s.update_keep, false),
|
||||
mirrors: on(s.update_mirrors, true),
|
||||
};
|
||||
};
|
||||
|
||||
/* ---------- schedule ---------- */
|
||||
|
||||
/** The scheduled time on the day of `ts` (router local time), in seconds since the epoch. */
|
||||
function scheduled_on(pol, ts) {
|
||||
let t = localtime(ts);
|
||||
let hm = split(pol.time, ':');
|
||||
return ts - (t.hour * 3600 + t.min * 60 + t.sec) + int(hm[0]) * 3600 + int(hm[1]) * 60;
|
||||
}
|
||||
|
||||
function day_of(ts) {
|
||||
let t = localtime(ts);
|
||||
return sprintf('%04d-%02d-%02d', t.year, t.mon, t.mday);
|
||||
}
|
||||
|
||||
/** The scheduled slot `now` is in (up to an hour after the set time, also across midnight), or null. */
|
||||
function slot(pol, now) {
|
||||
for (let at in [ scheduled_on(pol, now), scheduled_on(pol, now - 86400) ])
|
||||
if (now >= at && now - at < 3600)
|
||||
return at;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* What an automatic run should do now: 'install' (the scheduled window, at
|
||||
* most an hour after the set time, once per slot), 'check' (the interval has
|
||||
* passed), or null.
|
||||
*/
|
||||
export function due(pol, st, now) {
|
||||
now ??= time();
|
||||
st ??= {};
|
||||
if (pol.auto && pol.mode == 'scheduled' && length(pol.targets)) {
|
||||
let at = slot(pol, now);
|
||||
if (at != null && st.install_day != day_of(at))
|
||||
return 'install';
|
||||
}
|
||||
if (pol.check && now - (st.check ?? 0) >= INTERVALS[pol.interval])
|
||||
return 'check';
|
||||
return null;
|
||||
};
|
||||
|
||||
export function auto_state() {
|
||||
return read_json(AUTO_FILE, {});
|
||||
};
|
||||
|
||||
export function auto_save(st) {
|
||||
ensure_dir(DATA_DIR);
|
||||
write_json(AUTO_FILE, st, 0o600);
|
||||
};
|
||||
|
||||
/** Record that the scheduled slot `now` is in has run (the slot's own day, not today's after midnight). */
|
||||
export function mark_day(pol, st, now) {
|
||||
now ??= time();
|
||||
st.install_day = day_of(slot(pol, now) ?? now);
|
||||
return st;
|
||||
};
|
||||
|
||||
/* ---------- one job at a time ---------- */
|
||||
|
||||
/**
|
||||
* Take the update lock without waiting: the open file (keep it for the whole
|
||||
* job; the kernel releases it when the process ends), or null when another
|
||||
* job holds it.
|
||||
*/
|
||||
export function try_lock() {
|
||||
ensure_dir(RUN_DIR);
|
||||
let f = open(LOCK_FILE, 'a');
|
||||
if (f && f.lock('xn'))
|
||||
return f;
|
||||
f?.close();
|
||||
return null;
|
||||
};
|
||||
|
||||
/** A job is running: someone holds the update lock. */
|
||||
export function busy() {
|
||||
let f = try_lock();
|
||||
if (!f)
|
||||
return true;
|
||||
f.close();
|
||||
return false;
|
||||
};
|
||||
|
||||
/**
|
||||
* The last job; a "running" one whose worker is gone (the lock is free) reads
|
||||
* as failed. Its phases, log and error are in the language of whoever started
|
||||
* it (English for automatic runs); only the stopped-job error is made here.
|
||||
*/
|
||||
export function job() {
|
||||
let j = read_json(JOB_FILE, null);
|
||||
if (j?.state == 'running' && !busy()) {
|
||||
j.state = 'failed';
|
||||
j.error = _('The update stopped unexpectedly.');
|
||||
j.finished = j.finished ?? time();
|
||||
}
|
||||
return j;
|
||||
};
|
||||
|
||||
/* ---------- history (Components → Activity) ---------- */
|
||||
|
||||
export function history() {
|
||||
let h = read_json(HISTORY_FILE, []);
|
||||
return type(h) == 'array' ? h : [];
|
||||
};
|
||||
|
||||
/** Newest first, the last 30 kept on flash. */
|
||||
export function history_add(e) {
|
||||
let h = history();
|
||||
unshift(h, { ts: time(), ...e });
|
||||
ensure_dir(DATA_DIR);
|
||||
write_json(HISTORY_FILE, slice(h, 0, 30), 0o600);
|
||||
};
|
||||
|
||||
/* ---------- Dawnline versions ---------- */
|
||||
|
||||
/**
|
||||
* Compare Dawnline release versions (0.3.2, v0.4.0-rc1): -1, 0 or 1. Numbers
|
||||
* compare as numbers; a pre-release (-rc1, -beta) comes before its release.
|
||||
*/
|
||||
export function version_cmp(a, b) {
|
||||
let parse = (v) => {
|
||||
let m = match(replace(`${v}`, /^v/, ''), /^([0-9.]+)([-~]([0-9A-Za-z.]+))?/);
|
||||
return m ? { nums: map(split(m[1], '.'), (x) => int(x)), pre: m[3] } : { nums: [ 0 ], pre: `${v}` };
|
||||
};
|
||||
let x = parse(a), y = parse(b);
|
||||
for (let i = 0; i < length(x.nums) || i < length(y.nums); i++) {
|
||||
let p = x.nums[i] ?? 0, r = y.nums[i] ?? 0;
|
||||
if (p != r)
|
||||
return p < r ? -1 : 1;
|
||||
}
|
||||
if (x.pre == y.pre)
|
||||
return 0;
|
||||
if (x.pre == null)
|
||||
return 1;
|
||||
if (y.pre == null)
|
||||
return -1;
|
||||
// rc1 < rc2 < rc10: compare the number runs as numbers.
|
||||
let px = match(x.pre, /^([^0-9]*)([0-9]*)/), py = match(y.pre, /^([^0-9]*)([0-9]*)/);
|
||||
if (px[1] != py[1])
|
||||
return px[1] < py[1] ? -1 : 1;
|
||||
return int(px[2] || 0) < int(py[2] || 0) ? -1 : int(px[2] || 0) > int(py[2] || 0) ? 1 : 0;
|
||||
};
|
||||
|
||||
/* ---------- kept previous versions ---------- */
|
||||
|
||||
/** { version, build?, firmware?, files: [paths], saved } of the kept version, or null. */
|
||||
export function kept(component) {
|
||||
let k = read_json(`${KEEP_DIR}/${component}/meta.json`, null);
|
||||
if (!k?.version || !length(k.files ?? []))
|
||||
return null;
|
||||
for (let f in k.files)
|
||||
if (!stat(f))
|
||||
return null;
|
||||
return k;
|
||||
};
|
||||
|
||||
/** What the Components tab shows; firmware: the current one (a kept AmneziaWG build is for one firmware only). */
|
||||
export function kept_all(firmware) {
|
||||
let out = {};
|
||||
for (let c in [ 'app', 'amneziawg' ]) {
|
||||
let k = kept(c);
|
||||
if (k && c == 'amneziawg' && k.firmware && firmware && k.firmware != firmware)
|
||||
k = null;
|
||||
out[c] = k ? { version: k.version, build: k.build ?? null, saved: k.saved ?? null } : null;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/** Replace the kept version of a component with these files (copied in). */
|
||||
export function keep(component, version, files, build, firmware) {
|
||||
let dir = `${KEEP_DIR}/${component}`;
|
||||
system(`rm -rf ${q(dir)}; mkdir -p ${q(dir)}; chmod 700 ${q(KEEP_DIR)} ${q(dir)}`);
|
||||
let saved = [];
|
||||
for (let f in files) {
|
||||
let to = `${dir}/${replace(f, /^.*\//, '')}`;
|
||||
if (system(`cp ${q(f)} ${q(to)}`) == 0)
|
||||
push(saved, to);
|
||||
}
|
||||
if (length(saved) == length(files))
|
||||
write_json(`${dir}/meta.json`, { version, build, firmware, files: saved, saved: time() }, 0o600);
|
||||
else
|
||||
system(`rm -rf ${q(dir)}`);
|
||||
};
|
||||
|
||||
export function forget(component) {
|
||||
system(`rm -rf ${q(KEEP_DIR + '/' + component)}`);
|
||||
};
|
||||
@@ -1,896 +0,0 @@
|
||||
// Dawnline — small helpers shared by the rpcd plugin, the CLI, the agent and
|
||||
// the portal CGI. Nothing in here loads or commits configuration (the uci
|
||||
// helpers work on a cursor the caller owns).
|
||||
|
||||
import { open, readfile, writefile, rename, mkdir, stat, unlink, popen, lsdir } from 'fs';
|
||||
import { _ } from 'dawnline.i18n';
|
||||
|
||||
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';
|
||||
export const DATA_DIR = '/etc/dawnline';
|
||||
|
||||
/* ---------- shell ---------- */
|
||||
|
||||
export function q(s) {
|
||||
return "'" + replace(`${s}`, "'", "'\\''") + "'";
|
||||
};
|
||||
|
||||
/** Run a shell command; returns { code, out }. Stdout and stderr are merged. */
|
||||
export function sh(cmd) {
|
||||
let p = popen(`${cmd} 2>&1`, 'r');
|
||||
if (!p)
|
||||
return { code: -1, out: '' };
|
||||
let out = p.read('all') ?? '';
|
||||
let code = p.close();
|
||||
return { code: code ?? 0, out: out };
|
||||
};
|
||||
|
||||
/** Trimmed stdout of a command, or null when it failed. */
|
||||
export function sh_out(cmd) {
|
||||
let r = sh(cmd);
|
||||
return r.code == 0 ? trim(r.out) : null;
|
||||
};
|
||||
|
||||
/** Fire-and-forget: run a shell snippet detached from the caller. */
|
||||
export function sh_bg(cmd) {
|
||||
system(`( ${cmd} ) >/dev/null 2>&1 </dev/null &`);
|
||||
};
|
||||
|
||||
/* ---------- randomness ---------- */
|
||||
|
||||
export function rand_bytes(n) {
|
||||
let f = open('/dev/urandom', 'r');
|
||||
let b = f.read(n);
|
||||
f.close();
|
||||
return b;
|
||||
};
|
||||
|
||||
export function rand_hex(n) {
|
||||
return hexenc(rand_bytes(n));
|
||||
};
|
||||
|
||||
const ALNUM = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
||||
|
||||
/** URL-safe random token without look-alike characters. */
|
||||
export function rand_token(len) {
|
||||
let b = rand_bytes(len), s = '';
|
||||
for (let i = 0; i < len; i++)
|
||||
s += substr(ALNUM, ord(b, i) % length(ALNUM), 1);
|
||||
return s;
|
||||
};
|
||||
|
||||
export function rand_int(lo, hi) {
|
||||
let b = rand_bytes(4);
|
||||
let n = (ord(b, 0) << 24 | ord(b, 1) << 16 | ord(b, 2) << 8 | ord(b, 3)) & 0x7fffffff;
|
||||
return lo + n % (hi - lo + 1);
|
||||
};
|
||||
|
||||
/* ---------- files ---------- */
|
||||
|
||||
export function ensure_dir(path, mode) {
|
||||
let parts = split(path, '/'), cur = '';
|
||||
for (let p in parts) {
|
||||
if (p == '')
|
||||
continue;
|
||||
cur += '/' + p;
|
||||
if (!stat(cur))
|
||||
mkdir(cur, mode ?? 0o755);
|
||||
}
|
||||
};
|
||||
|
||||
export function read_json(path, fallback) {
|
||||
let s = readfile(path);
|
||||
if (s == null || s == '')
|
||||
return fallback;
|
||||
try {
|
||||
let v = json(s);
|
||||
return v ?? fallback;
|
||||
}
|
||||
catch (e) {
|
||||
return fallback;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Atomic write (temp file + rename). `mode` defaults to 0600. A short write
|
||||
* (flash full) leaves the old file in place and returns false.
|
||||
*/
|
||||
export function write_file(path, data, mode) {
|
||||
let tmp = `${path}.tmp${rand_hex(3)}`;
|
||||
let f = open(tmp, 'w', mode ?? 0o600);
|
||||
if (!f)
|
||||
return false;
|
||||
data = `${data ?? ''}`;
|
||||
let n = length(data) ? f.write(data) : 0;
|
||||
let closed = f.close();
|
||||
if (n != length(data) || !closed) {
|
||||
unlink(tmp);
|
||||
return false;
|
||||
}
|
||||
if (!rename(tmp, path)) {
|
||||
unlink(tmp);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
export function write_json(path, data, mode) {
|
||||
return write_file(path, sprintf('%J', data), mode);
|
||||
};
|
||||
|
||||
export function file_exists(path) {
|
||||
return stat(path) != null;
|
||||
};
|
||||
|
||||
/** Exclusive lock on a file under RUN_DIR; returns a handle to pass to unlock(). */
|
||||
export function lock(name) {
|
||||
ensure_dir(RUN_DIR);
|
||||
let f = open(`${RUN_DIR}/${name}.lock`, 'w');
|
||||
if (f)
|
||||
f.lock('x');
|
||||
return f;
|
||||
};
|
||||
|
||||
export function unlock(f) {
|
||||
if (f) {
|
||||
f.lock('u');
|
||||
f.close();
|
||||
}
|
||||
};
|
||||
|
||||
// Locks this process holds through with_lock (flock does not nest: a second
|
||||
// open of the same lock file in one process would wait for itself).
|
||||
let held = {};
|
||||
|
||||
/**
|
||||
* Run fn() holding the named lock and return what it returns. Nested calls
|
||||
* for the same name in one process run fn() directly. Anything fn throws
|
||||
* releases the lock and is rethrown with the same message.
|
||||
*/
|
||||
export function with_lock(name, fn) {
|
||||
if (held[name])
|
||||
return fn();
|
||||
let l = lock(name);
|
||||
held[name] = true;
|
||||
try {
|
||||
let r = fn();
|
||||
delete held[name];
|
||||
unlock(l);
|
||||
return r;
|
||||
}
|
||||
catch (e) {
|
||||
delete held[name];
|
||||
unlock(l);
|
||||
die(e.message ?? `${e}`);
|
||||
}
|
||||
};
|
||||
|
||||
/* ---------- uci (on the caller's cursor) ---------- */
|
||||
|
||||
/** Set an option only when it differs (null, '' or [] delete it); true if something changed. */
|
||||
export function uci_put(c, conf, sec, opt, val) {
|
||||
let cur = c.get(conf, sec, opt);
|
||||
if (val == null || val == '' || (type(val) == 'array' && !length(val))) {
|
||||
if (cur != null) {
|
||||
c.delete(conf, sec, opt);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (type(val) == 'array') {
|
||||
if (type(cur) == 'array' && join('\n', cur) == join('\n', val))
|
||||
return false;
|
||||
}
|
||||
else if (cur == `${val}`)
|
||||
return false;
|
||||
c.set(conf, sec, opt, type(val) == 'array' ? val : `${val}`);
|
||||
return true;
|
||||
};
|
||||
|
||||
/** Make `sec` a section of type `stype` (recreated when its type differs); true if it changed. */
|
||||
export function uci_section(c, conf, sec, stype) {
|
||||
if (c.get(conf, sec) == stype)
|
||||
return false;
|
||||
if (c.get(conf, sec) != null)
|
||||
c.delete(conf, sec);
|
||||
c.set(conf, sec, stype);
|
||||
return true;
|
||||
};
|
||||
|
||||
/**
|
||||
* After committing a config ourselves (and applying it), record its new
|
||||
* checksum so a later `reload_config` does not treat it as changed and
|
||||
* restart unrelated services that reload on any network change.
|
||||
*/
|
||||
export function ack_config(confs) {
|
||||
let md5f = '/var/run/config.md5';
|
||||
if (!stat(md5f))
|
||||
return;
|
||||
let lines = split(trim(readfile(md5f) ?? ''), '\n');
|
||||
for (let conf in confs) {
|
||||
let path = `/var/run/config.check/${conf}`;
|
||||
let sum = sh_out(`uci show ${q(conf)} | md5sum | cut -d' ' -f1`);
|
||||
if (!sum)
|
||||
continue;
|
||||
let found = false;
|
||||
for (let i = 0; i < length(lines); i++)
|
||||
if (substr(lines[i], -length(path) - 2) == ` ${path}`) {
|
||||
lines[i] = `${sum} ${path}`;
|
||||
found = true;
|
||||
}
|
||||
if (!found)
|
||||
push(lines, `${sum} ${path}`);
|
||||
}
|
||||
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 ---------- */
|
||||
|
||||
/*
|
||||
* 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;
|
||||
// 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. */
|
||||
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, {});
|
||||
let now = time();
|
||||
for (let k, v in all)
|
||||
if (now - v.ts >= 120)
|
||||
delete all[k];
|
||||
all[ifname] = { reason, name, ts: now };
|
||||
ensure_dir(RUN_DIR);
|
||||
write_json(INTENT, all, 0o600);
|
||||
};
|
||||
|
||||
/**
|
||||
* The note for a stop of `ifname`, or null. netifd may report one stop more
|
||||
* than once (the interface, then its device): the note covers every report
|
||||
* within 15 s of the first, then it is spent.
|
||||
*/
|
||||
export function take_iface_intent(ifname) {
|
||||
let all = read_json(INTENT, {});
|
||||
let it = all[ifname];
|
||||
let now = time();
|
||||
if (!it || now - it.ts >= 120 || (it.used && now - it.used >= 15))
|
||||
return null;
|
||||
if (!it.used) {
|
||||
it.used = now;
|
||||
write_json(INTENT, all, 0o600);
|
||||
}
|
||||
return it;
|
||||
};
|
||||
|
||||
/** 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 ---------- */
|
||||
|
||||
// Module constants, compiled once: a regex literal is compiled again each
|
||||
// time it runs (rules/backend.md), and these run for every address and every
|
||||
// network in a list (status, the portal, the agent's checks).
|
||||
const IPV4 = /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/;
|
||||
const CIDR = /^([0-9.]+)(\/([0-9]+))?$/;
|
||||
|
||||
export function ip2int(ip) {
|
||||
let m = match(ip, IPV4);
|
||||
if (!m)
|
||||
return null;
|
||||
let n = 0;
|
||||
for (let i = 1; i <= 4; i++) {
|
||||
let o = +m[i];
|
||||
if (o > 255)
|
||||
return null;
|
||||
n = n * 256 + o;
|
||||
}
|
||||
return n;
|
||||
};
|
||||
|
||||
export function int2ip(n) {
|
||||
return sprintf('%d.%d.%d.%d', (n >> 24) & 255, (n >> 16) & 255, (n >> 8) & 255, n & 255);
|
||||
};
|
||||
|
||||
/**
|
||||
* "a.b.c.d[/len]" with any prefix 0-32 (a bare address is a /32) →
|
||||
* { net, prefix, size, ip }; null for anything else. Routes, rules and
|
||||
* allowed networks use this; parse_cidr is the stricter one for subnets.
|
||||
*/
|
||||
export function cidr_any(s) {
|
||||
let m = match(s ?? '', CIDR);
|
||||
if (!m)
|
||||
return null;
|
||||
let ip = ip2int(m[1]), prefix = m[3] != null ? +m[3] : 32;
|
||||
if (ip == null || prefix > 32)
|
||||
return null;
|
||||
let size = 1 << (32 - prefix);
|
||||
return { net: ip - ip % size, prefix, size, ip };
|
||||
};
|
||||
|
||||
/** The first of `nets` (CIDR strings, any prefix) that holds `ip` (a string or an int), or null. */
|
||||
export function in_nets(ip, nets) {
|
||||
let n = type(ip) == 'int' ? ip : ip2int(ip);
|
||||
if (n == null)
|
||||
return null;
|
||||
for (let s in nets) {
|
||||
let c = cidr_any(s);
|
||||
if (c && n >= c.net && n < c.net + c.size)
|
||||
return s;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
// Cloudflare's published ranges (https://www.cloudflare.com/ips/).
|
||||
const CF_V4 = [ '173.245.48.0/20', '103.21.244.0/22', '103.22.200.0/22', '103.31.4.0/22', '141.101.64.0/18',
|
||||
'108.162.192.0/18', '190.93.240.0/20', '188.114.96.0/20', '197.234.240.0/22', '198.41.128.0/17',
|
||||
'162.158.0.0/15', '104.16.0.0/13', '104.24.0.0/14', '172.64.0.0/13', '131.0.72.0/22' ];
|
||||
const CF_V6 = [ /^2400:cb00:/i, /^2606:4700:/i, /^2803:f800:/i, /^2405:b500:/i, /^2405:8100:/i, /^2a06:98c[0-7]:/i, /^2c0f:f248:/i ];
|
||||
|
||||
/** True for an address in Cloudflare's network (its proxies and edge). */
|
||||
export function cloudflare_ip(a) {
|
||||
let n = ip2int(a);
|
||||
if (n == null) {
|
||||
for (let r in CF_V6)
|
||||
if (match(a ?? '', r))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return in_nets(n, CF_V4) != null;
|
||||
};
|
||||
|
||||
/** Parse "a.b.c.d/len" → { net, prefix, first, last, size } (net is the masked base). */
|
||||
export function parse_cidr(cidr) {
|
||||
let m = match(cidr ?? '', CIDR);
|
||||
if (!m || m[3] == null)
|
||||
return null;
|
||||
let ip = ip2int(m[1]), prefix = +m[3];
|
||||
if (ip == null || prefix < 8 || prefix > 30)
|
||||
return null;
|
||||
let size = 1 << (32 - prefix);
|
||||
let net = ip - ip % size;
|
||||
return { net, prefix, size, first: net + 1, last: net + size - 2, ip };
|
||||
};
|
||||
|
||||
export function cidr_str(c) {
|
||||
return `${int2ip(c.net)}/${c.prefix}`;
|
||||
};
|
||||
|
||||
export function cidr_overlaps(a, b) {
|
||||
return a.net < b.net + b.size && b.net < a.net + a.size;
|
||||
};
|
||||
|
||||
export function in_cidr(ip, c) {
|
||||
let n = type(ip) == 'int' ? ip : ip2int(ip);
|
||||
return n != null && n >= c.net && n < c.net + c.size;
|
||||
};
|
||||
|
||||
/* ---------- text ---------- */
|
||||
|
||||
/** Characters in a UTF-8 string (length() counts bytes: "Мама" is 8 of them). */
|
||||
export function ulength(s) {
|
||||
let n = 0;
|
||||
for (let i = 0; i < length(s); i++)
|
||||
if ((ord(s, i) & 0xc0) != 0x80)
|
||||
n++;
|
||||
return n;
|
||||
};
|
||||
|
||||
/** The first `n` characters of a UTF-8 string, never cut inside one. */
|
||||
export function ucut(s, n) {
|
||||
let i = 0, c = 0;
|
||||
while (i < length(s)) {
|
||||
if ((ord(s, i) & 0xc0) != 0x80) {
|
||||
if (c == n)
|
||||
break;
|
||||
c++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return substr(s, 0, i);
|
||||
};
|
||||
|
||||
// Cyrillic (Russian, Ukrainian) to Latin: а-я by code point from U+0430, then
|
||||
// ё, і, ї, є, ґ; the rest of a string passes as it is.
|
||||
const CYR_A = 0x430;
|
||||
const CYRILLIC = [ 'a', 'b', 'v', 'g', 'd', 'e', 'zh', 'z', 'i', 'y', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u',
|
||||
'f', 'kh', 'ts', 'ch', 'sh', 'shch', '', 'y', '', 'e', 'yu', 'ya' ];
|
||||
const CYRILLIC_MORE = { '1105': 'yo', '1110': 'i', '1111': 'yi', '1108': 'ye', '1169': 'g' };
|
||||
|
||||
/**
|
||||
* Cyrillic letters written in Latin ("Мама" → "mama", "Юля" → "yulya"):
|
||||
* ids, hostnames and file names are ASCII, and a person should recognise
|
||||
* their own in them. Other characters are left alone.
|
||||
*/
|
||||
export function translit(s) {
|
||||
let out = '', i = 0, n = length(s);
|
||||
while (i < n) {
|
||||
let b = ord(s, i);
|
||||
if (b < 0x80) {
|
||||
out += substr(s, i, 1);
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
let cp = 0, len = 1;
|
||||
if ((b & 0xe0) == 0xc0 && i + 1 < n) {
|
||||
cp = ((b & 0x1f) << 6) | (ord(s, i + 1) & 0x3f);
|
||||
len = 2;
|
||||
}
|
||||
else if ((b & 0xf0) == 0xe0 && i + 2 < n) {
|
||||
cp = ((b & 0x0f) << 12) | ((ord(s, i + 1) & 0x3f) << 6) | (ord(s, i + 2) & 0x3f);
|
||||
len = 3;
|
||||
}
|
||||
else if ((b & 0xf8) == 0xf0 && i + 3 < n)
|
||||
len = 4;
|
||||
// Upper case to lower: А-Я, Ё, І, Ї, Є, Ґ.
|
||||
if (cp >= 0x410 && cp <= 0x42f)
|
||||
cp += 0x20;
|
||||
else if (cp == 0x401)
|
||||
cp = 0x451;
|
||||
else if (cp == 0x406)
|
||||
cp = 0x456;
|
||||
else if (cp == 0x407)
|
||||
cp = 0x457;
|
||||
else if (cp == 0x404)
|
||||
cp = 0x454;
|
||||
else if (cp == 0x490)
|
||||
cp = 0x491;
|
||||
let t = cp >= CYR_A && cp < CYR_A + length(CYRILLIC) ? CYRILLIC[cp - CYR_A] : CYRILLIC_MORE[`${cp}`];
|
||||
out += t != null ? t : substr(s, i, len);
|
||||
i += len;
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/* ---------- validation ---------- */
|
||||
|
||||
export function valid_slug(s) {
|
||||
return type(s) == 'string' && match(s, /^[a-z0-9][a-z0-9-]{0,30}[a-z0-9]$|^[a-z0-9]$/) != null;
|
||||
};
|
||||
|
||||
/** A name people see: 1-48 characters (not bytes), none of < > " \ or control characters. */
|
||||
export function valid_name(s) {
|
||||
return type(s) == 'string' && ulength(trim(s)) > 0 && ulength(s) <= 48 && match(s, /[<>"\\[:cntrl:]]/) == null;
|
||||
};
|
||||
|
||||
/** What valid_name wants, for the admin (in the admin's language). */
|
||||
export function name_rule() {
|
||||
return _('Enter a name: up to 48 characters, without < > " or \\.');
|
||||
};
|
||||
|
||||
// Character sets for checks written without a regex: ucode compiles a regex
|
||||
// literal each time it runs, and musl's regcomp takes milliseconds for a
|
||||
// bounded repeat (rules/backend.md). ltrim stops at a NUL byte, so a string
|
||||
// with one fails these checks (a regex looks only up to it).
|
||||
const ASCII_ALNUM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
||||
const B64 = `${ASCII_ALNUM}+/`;
|
||||
|
||||
/**
|
||||
* The longest name a device takes, in characters: ordinary words fill a portal card's line about as far as its
|
||||
* status ("Return to save, Esc to cancel"), and fit the server list in the AmneziaVPN app. Names set before the rule stay as they are; it holds when a name is set.
|
||||
*/
|
||||
export const DEVICE_NAME_MAX = 20;
|
||||
// What a device's name may hold of ASCII: letters, digits, spaces and - _ . It goes into configurations, file
|
||||
// names and the AmneziaVPN key: nothing there to escape.
|
||||
const DEVICE_NAME_CHARS = `${ASCII_ALNUM} ._-`;
|
||||
// Past ASCII, letters of any language are welcome; these blocks are not: symbols, punctuation, arrows, maths,
|
||||
// emoji and invisibles (code point ranges, both ends in; web/src/shared/format.ts NAME_SYMBOLS is the same list,
|
||||
// and the page asks Unicode besides that a character is a letter or a digit).
|
||||
const NAME_SYMBOLS = [
|
||||
[ 0x80, 0xbf ], [ 0xd7, 0xd7 ], [ 0xf7, 0xf7 ], [ 0x2000, 0x2bff ], [ 0x3000, 0x303f ], [ 0xd800, 0xf8ff ],
|
||||
[ 0xfe00, 0xfe0f ], [ 0xfe30, 0xfe4f ], [ 0xfeff, 0xfeff ], [ 0xfff0, 0xffff ], [ 0x1f000, 0x1fbff ],
|
||||
[ 0xe0000, 0xe007f ]
|
||||
];
|
||||
|
||||
function name_symbol(cp) {
|
||||
for (let r in NAME_SYMBOLS)
|
||||
if (cp >= r[0] && cp <= r[1])
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* A device's name (trimmed by the caller): 1-DEVICE_NAME_MAX characters, not blank, each one of
|
||||
* DEVICE_NAME_CHARS or past ASCII outside NAME_SYMBOLS. Read by code points: broken UTF-8, and an
|
||||
* ASCII character spelled long (0xC0 0xAF for "/"), are refused.
|
||||
*/
|
||||
export function valid_device_name(s) {
|
||||
if (type(s) != 'string' || length(trim(s)) == 0 || ulength(s) > DEVICE_NAME_MAX)
|
||||
return false;
|
||||
let i = 0, n = length(s);
|
||||
while (i < n) {
|
||||
let b = ord(s, i), cp, len;
|
||||
if (b < 0x80) {
|
||||
if (index(DEVICE_NAME_CHARS, substr(s, i, 1)) < 0)
|
||||
return false;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if ((b & 0xe0) == 0xc0) {
|
||||
len = 2;
|
||||
cp = b & 0x1f;
|
||||
}
|
||||
else if ((b & 0xf0) == 0xe0) {
|
||||
len = 3;
|
||||
cp = b & 0x0f;
|
||||
}
|
||||
else if ((b & 0xf8) == 0xf0) {
|
||||
len = 4;
|
||||
cp = b & 0x07;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
if (i + len > n)
|
||||
return false;
|
||||
for (let k = 1; k < len; k++) {
|
||||
let c = ord(s, i + k);
|
||||
if ((c & 0xc0) != 0x80)
|
||||
return false;
|
||||
cp = (cp << 6) | (c & 0x3f);
|
||||
}
|
||||
if (cp < 0xc0 || cp > 0x10ffff || name_symbol(cp))
|
||||
return false;
|
||||
i += len;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
// 32 bytes are 43 base64 characters: the last holds four bits of the key and two zero bits.
|
||||
const KEY_LAST = 'AEIMQUYcgkosw048';
|
||||
|
||||
/**
|
||||
* A (Amnezia)WireGuard key: 32 bytes in base64, 43 characters and "=", the
|
||||
* same as /^[A-Za-z0-9+\/]{42}[AEIMQUYcgkosw048]=$/ without its 1.7 ms
|
||||
* compile on the WR3000P: the agent's watcher checks two keys per line of
|
||||
* `awg show <ifname> dump` every second. This takes about 0.03 ms.
|
||||
*/
|
||||
export function valid_key(s) {
|
||||
return type(s) == 'string' && length(s) == 44 && substr(s, 43) == '=' &&
|
||||
index(KEY_LAST, substr(s, 42, 1)) >= 0 && ltrim(substr(s, 0, 42), B64) == '';
|
||||
};
|
||||
|
||||
/** An IPv6 address in its usual spelling (no zone, no brackets). */
|
||||
export function is_ip6(s) {
|
||||
return type(s) == 'string' && match(s, /^([0-9a-fA-F]{0,4}:){2,7}[0-9a-fA-F]{0,4}$/) != null && match(s, /:::/) == null;
|
||||
};
|
||||
|
||||
const LABEL = `${ASCII_ALNUM}-`;
|
||||
|
||||
/**
|
||||
* Dot-separated labels of 1-63 letters, digits and dashes, none starting or
|
||||
* ending with a dash: the same as
|
||||
* /^[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(\.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*$/
|
||||
* without its 10 ms compile on the WR3000P.
|
||||
*/
|
||||
function host_name(s) {
|
||||
for (let l in split(s, '.'))
|
||||
if (length(l) < 1 || length(l) > 63 || ltrim(l, LABEL) != '' ||
|
||||
index(ASCII_ALNUM, substr(l, 0, 1)) < 0 || index(ASCII_ALNUM, substr(l, -1)) < 0)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/** A host name or an IP address, as a public address or the portal's name may be given. */
|
||||
export function valid_host(s) {
|
||||
return type(s) == 'string' && length(s) <= 253 && (host_name(s) || ip2int(s) != null || is_ip6(s));
|
||||
};
|
||||
|
||||
/** YYYY-MM-DD, and a day that exists (2026-02-30 does not). */
|
||||
export function valid_date(s) {
|
||||
let m = type(s) == 'string' ? match(s, /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/) : null;
|
||||
if (!m)
|
||||
return false;
|
||||
let l = localtime(timelocal({ year: +m[1], mon: +m[2], mday: +m[3], hour: 12, min: 0, sec: 0 }));
|
||||
return l.year == +m[1] && l.mon == +m[2] && l.mday == +m[3];
|
||||
};
|
||||
|
||||
// Runs of other characters become one dash. Module constants: host_label and
|
||||
// tunnel_name run for every device of a status or a portal answer.
|
||||
const NOT_LC_ALNUM = /[^a-z0-9]+/g, NOT_ALNUM = /[^A-Za-z0-9]+/g;
|
||||
// Names Windows keeps for devices: no file may be called that, whatever the case.
|
||||
const WIN_DEVICES = [ 'con', 'prn', 'aux', 'nul', 'com1', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9',
|
||||
'lpt1', 'lpt2', 'lpt3', 'lpt4', 'lpt5', 'lpt6', 'lpt7', 'lpt8', 'lpt9' ];
|
||||
|
||||
/** Turn a display name into a portal slug candidate ("Мама Таня" → "mama-tanya"). */
|
||||
export function slugify(s) {
|
||||
let out = lc(translit(trim(`${s ?? ''}`)));
|
||||
out = replace(out, NOT_LC_ALNUM, '-');
|
||||
out = trim(out, '-');
|
||||
return substr(out, 0, 32) || 'user';
|
||||
};
|
||||
|
||||
/**
|
||||
* A device's name as a DNS label ("Galaxy S24" → "galaxy-s24", "Телефон" →
|
||||
* "telefon"): at most 40 characters, "device" when nothing is left.
|
||||
*/
|
||||
export function host_label(s) {
|
||||
let l = replace(lc(translit(`${s ?? ''}`)), NOT_LC_ALNUM, '-');
|
||||
l = rtrim(substr(trim(l, '-'), 0, 40), '-');
|
||||
return l || 'device';
|
||||
};
|
||||
|
||||
/**
|
||||
* A server's name as a tunnel name ("Outline Home" → "Outline-Home"): the
|
||||
* AmneziaWG and WireGuard apps name the tunnel after the file, and Android and
|
||||
* wg-quick refuse anything but [a-zA-Z0-9_=+.-] up to 15 characters; Windows
|
||||
* refuses its device names (CON, NUL, COM1…). "dawnline" when nothing is left.
|
||||
*/
|
||||
export function tunnel_name(s) {
|
||||
let l = replace(translit(`${s ?? ''}`), NOT_ALNUM, '-');
|
||||
l = rtrim(substr(trim(l, '-'), 0, 15), '-');
|
||||
return l && index(WIN_DEVICES, lc(l)) < 0 ? l : 'dawnline';
|
||||
};
|
||||
|
||||
/* ---------- time ---------- */
|
||||
|
||||
/** "YYYY-MM-DD" → epoch of the end of that local day (23:59:59). */
|
||||
export function date_end(s) {
|
||||
let m = match(s ?? '', /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/);
|
||||
if (!m)
|
||||
return null;
|
||||
return timelocal({ sec: 59, min: 59, hour: 23, mday: +m[3], mon: +m[2], year: +m[1] });
|
||||
};
|
||||
|
||||
export function today() {
|
||||
let t = localtime();
|
||||
return sprintf('%04d-%02d-%02d', t.year, t.mon, t.mday);
|
||||
};
|
||||
|
||||
export function month_key(ts) {
|
||||
let t = localtime(ts ?? time());
|
||||
return sprintf('%04d-%02d', t.year, t.mon);
|
||||
};
|
||||
|
||||
export function day_key(ts) {
|
||||
let t = localtime(ts ?? time());
|
||||
return sprintf('%04d-%02d-%02d', t.year, t.mon, t.mday);
|
||||
};
|
||||
|
||||
/**
|
||||
* The day key `days` days before `ts` (default now). Counted from local noon,
|
||||
* so a 23- or 25-hour day around a DST change neither skips nor repeats a day.
|
||||
*/
|
||||
export function day_key_ago(days, ts) {
|
||||
let t = localtime(ts ?? time());
|
||||
let noon = timelocal({ year: t.year, mon: t.mon, mday: t.mday, hour: 12, min: 0, sec: 0 });
|
||||
return day_key(noon - (days ?? 0) * 86400);
|
||||
};
|
||||
|
||||
export function uniq(arr) {
|
||||
let seen = {}, out = [];
|
||||
for (let v in arr)
|
||||
if (!seen[v]) {
|
||||
seen[v] = true;
|
||||
push(out, v);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
export function list_dir(path) {
|
||||
return lsdir(path) ?? [];
|
||||
};
|
||||
|
||||
export function remove(path) {
|
||||
return unlink(path);
|
||||
};
|
||||
|
||||
export function now() {
|
||||
return time();
|
||||
};
|
||||
|
||||
const IFACE = `${ASCII_ALNUM}_.@-`;
|
||||
|
||||
/**
|
||||
* A netifd interface name (network.interface.<name>, a person's home network):
|
||||
* 1-32 letters, digits and _ . @ -, the same as /^[A-Za-z0-9_.@-]{1,32}$/
|
||||
* without its 3 ms compile on the WR3000P (iface_status runs several times for
|
||||
* the Overview's status, and in the agent's minute checks).
|
||||
*/
|
||||
export function valid_iface(s) {
|
||||
return type(s) == 'string' && length(s) >= 1 && length(s) <= 32 && ltrim(s, IFACE) == '';
|
||||
};
|
||||
|
||||
/** netifd's view of an interface (ubus network.interface.<name> status), or null. */
|
||||
export function iface_status(ifname) {
|
||||
if (!valid_iface(ifname))
|
||||
return null;
|
||||
let s = sh_out(`ubus call network.interface.${ifname} status 2>/dev/null`);
|
||||
if (!s)
|
||||
return null;
|
||||
try {
|
||||
return json(s);
|
||||
}
|
||||
catch (e) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
@@ -1,162 +0,0 @@
|
||||
// Dawnline — read models shared by the admin RPC and the portal: people and
|
||||
// devices merged with live peer state and usage.
|
||||
|
||||
import * as core from 'dawnline.core';
|
||||
import * as live from 'dawnline.live';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import { day_key_ago, month_key } from 'dawnline.util';
|
||||
|
||||
/** Each day's [rx, tx] for the last `n` days, oldest first: the charts draw what came and what went apart. */
|
||||
export function spark(rec, n) {
|
||||
let out = [];
|
||||
for (let i = n - 1; i >= 0; i--) {
|
||||
let v = rec?.days?.[day_key_ago(i)];
|
||||
push(out, v ? [ v[0], v[1] ] : [ 0, 0 ]);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
/** Each day's [rx, tx] from the 1st of this month to today, oldest first (the portal's month chart). */
|
||||
export function month_days(rec) {
|
||||
return spark(rec, +substr(day_key_ago(0), 8, 2));
|
||||
};
|
||||
|
||||
/** The device holds an older configuration than the one it would get now. */
|
||||
function config_stale(m, d) {
|
||||
m._issued ??= awg.issued();
|
||||
let was = m._issued[d.id];
|
||||
return was != null && was != awg.config_fingerprint(m, d);
|
||||
}
|
||||
|
||||
/**
|
||||
* The device a page request came from (`devs`: one person's device views):
|
||||
* through the tunnel, by its tunnel address; outside it, the one online device
|
||||
* whose tunnel comes from the request's address. The apps route the VPN's own
|
||||
* address around their tunnel, and the portal shares it, so a device on the VPN
|
||||
* opens the page from where its tunnel comes from. Two such devices behind one
|
||||
* address (one NAT): neither.
|
||||
*/
|
||||
export function device_here(devs, peer) {
|
||||
peer = replace(peer ?? '', /^::ffff:/, '');
|
||||
if (!peer)
|
||||
return null;
|
||||
let near = [];
|
||||
for (let d in devs) {
|
||||
if (d.address == peer)
|
||||
return d.id;
|
||||
if (d.online && d.endpoint == peer)
|
||||
push(near, d.id);
|
||||
}
|
||||
return length(near) == 1 ? near[0] : null;
|
||||
};
|
||||
|
||||
export function device_view(m, d, usage, dmp) {
|
||||
let u = usage.devices?.[d.id];
|
||||
let peer = dmp.peers[d.public_key];
|
||||
let ts = time();
|
||||
// Set up again (Reconnect): a handshake from before it does not count.
|
||||
m._reconnect ??= awg.reconnects();
|
||||
let hs = awg.handshake_since(peer?.handshake || u?.handshake || 0, m._reconnect[d.id]);
|
||||
let p = m.people[d.person];
|
||||
let srv = m.servers[p?.server];
|
||||
// The same label /tmp/hosts carries (core.device_labels), so what is shown resolves.
|
||||
let label = '';
|
||||
if (m.settings.hostnames && p) {
|
||||
m._labels ??= {};
|
||||
m._labels[p.id] ??= core.device_labels(m, p);
|
||||
label = m._labels[p.id][d.id] ?? 'device';
|
||||
}
|
||||
let online = hs > 0 && live.online(dmp, d.id, peer);
|
||||
let v = {
|
||||
id: d.id,
|
||||
name: d.name,
|
||||
kind: d.kind,
|
||||
address: d.address,
|
||||
disabled: d.disabled,
|
||||
created: d.created,
|
||||
created_by: d.created_by,
|
||||
online,
|
||||
handshake: hs,
|
||||
endpoint: live.endpoint_ip(peer?.endpoint) || u?.endpoint || '',
|
||||
active: !!peer,
|
||||
month: live.month(u),
|
||||
total: u?.total ?? [ 0, 0 ],
|
||||
spark: spark(u, 7),
|
||||
hostname: label ? `${label}.${p.id}.${m.settings.domain}` : '',
|
||||
browser_key: d.private_key == '',
|
||||
// Its configuration changed since it was last shown or downloaded.
|
||||
config_stale: config_stale(m, d),
|
||||
// How it connects: its own local network outside the tunnel, its own MTU
|
||||
// and keep-alive (0 / null: the server's) and what its configuration carries.
|
||||
lan_bypass: d.lan_bypass,
|
||||
mtu: d.mtu,
|
||||
keepalive: d.keepalive,
|
||||
mtu_used: srv ? core.device_mtu(srv, d) : null,
|
||||
keepalive_used: srv ? core.device_keepalive(srv, d) : null,
|
||||
// Its link failing in a named way, as the agent's watcher shows it
|
||||
// (dawnline.link): { state, since }, or null.
|
||||
link: dmp.link?.[d.id] ?? null,
|
||||
// What its link does right now, for the set-up sheet (dawnline.link
|
||||
// setup_state): { state: 'trying' | 'flowing' | 'nodata', since }, or null.
|
||||
// Without the agent nobody tells data from a bare handshake: a session
|
||||
// that lives counts as data flowing, so the sheet still sees it arrive.
|
||||
setup: dmp.setup != null ? (dmp.setup[d.id] ?? null) : online ? { state: 'flowing', since: hs } : null,
|
||||
};
|
||||
return v;
|
||||
};
|
||||
|
||||
export function person_view(m, p, usage, dmp, states, opts) {
|
||||
opts ??= {};
|
||||
let u = usage.people?.[p.id];
|
||||
let devs = [];
|
||||
for (let id in p.devices)
|
||||
push(devs, device_view(m, m.devices[id], usage, dmp));
|
||||
devs = sort(devs, (a, b) => a.created - b.created);
|
||||
let used = core.person_usage(p, usage);
|
||||
let v = {
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
server: p.server,
|
||||
access: p.access,
|
||||
device_limit: p.device_limit,
|
||||
expires: p.expires,
|
||||
quota_gb: p.quota_gb,
|
||||
quota_period: p.quota_period,
|
||||
quota_used: used,
|
||||
quota_limit: core.person_quota(p),
|
||||
bonus_gb: (p.bonus_gb > 0 && p.bonus_month == month_key()) ? p.bonus_gb : 0,
|
||||
expire_action: p.expire_action,
|
||||
dns_mode: p.dns_mode,
|
||||
expire_warn: p.expire_warn,
|
||||
paused: p.paused,
|
||||
hours: p.hours,
|
||||
state: states[p.id] ?? core.person_state(m, p, usage),
|
||||
month: live.month(u),
|
||||
total: u?.total ?? [ 0, 0 ],
|
||||
online: length(filter(devs, (d) => d.online)),
|
||||
last_seen: 0,
|
||||
devices: devs,
|
||||
};
|
||||
for (let d in devs)
|
||||
if (d.handshake > v.last_seen)
|
||||
v.last_seen = d.handshake;
|
||||
if (opts.admin) {
|
||||
// The code and links themselves are person_links' (admins only): the
|
||||
// code opens the person's portal and their devices' configurations.
|
||||
v.has_link = core.portal_url(m, p) != null;
|
||||
v.note = p.note;
|
||||
v.services = p.services;
|
||||
v.home_nets = p.home_nets;
|
||||
v.circle = p.circle;
|
||||
v.created = p.created;
|
||||
v.code_set = p.code_set;
|
||||
}
|
||||
return v;
|
||||
};
|
||||
|
||||
export function states(m, usage) {
|
||||
let out = {};
|
||||
for (let id, p in m.people)
|
||||
out[id] = core.person_state(m, p, usage);
|
||||
return out;
|
||||
};
|
||||
@@ -1,230 +0,0 @@
|
||||
// Dawnline — watchdog. The agent runs it every minute: it checks what keeps
|
||||
// people connected and repairs what it can, waiting longer after each repair
|
||||
// that did not help. What it finds goes to the service log (English, as
|
||||
// logged); its state to RUN_DIR for the Health page, whose chip names are put
|
||||
// in the admin's language when they are read (view()). A cron job
|
||||
// (libexec/dawnline/heartbeat) looks after the agent itself.
|
||||
|
||||
import { stat } from 'fs';
|
||||
import { _, L_ } from 'dawnline.i18n';
|
||||
import { sh_out, q, read_json, write_json, slog, slog_read, RUN_DIR } from 'dawnline.util';
|
||||
import * as apply from 'dawnline.apply';
|
||||
import * as loop from 'dawnline.loop';
|
||||
import * as health from 'dawnline.health';
|
||||
import * as awg from 'dawnline.awg';
|
||||
import * as nft from 'dawnline.nft';
|
||||
import * as netifd from 'dawnline.netifd';
|
||||
import * as upd from 'dawnline.updates';
|
||||
|
||||
export const STATE = `${RUN_DIR}/watchdog.json`;
|
||||
/** The agent writes the time here on every tick. */
|
||||
export const BEAT = `${RUN_DIR}/agent.beat`;
|
||||
// Seconds before the next repair, by the number of repairs so far.
|
||||
const WAIT = [ 0, 120, 600, 1800, 3600 ];
|
||||
// Good minutes in a row before a repaired target's count of repairs is
|
||||
// forgotten: one good minute between two failures is still flapping.
|
||||
const SETTLED = 10;
|
||||
|
||||
/** Why a tunnel is down (AmneziaWG; kept here for older callers). */
|
||||
export function iface_errors(ifname) {
|
||||
return awg.iface_errors(ifname);
|
||||
};
|
||||
|
||||
function restart_services() {
|
||||
// procd keeps unchanged instances (the agent running this) and starts missing ones.
|
||||
system('/etc/init.d/dawnline start >/dev/null 2>&1');
|
||||
}
|
||||
|
||||
/**
|
||||
* What to look at: { id (= the Health check id), src, name (chip, stored in English: view()
|
||||
* shows names() by id), who (the log's subject), ok,
|
||||
* problem, back, detail?, repair: { what, run }? }. The log reads "<who> <problem> - <what>"; its
|
||||
* words are L_(): English in the log, translated where it is shown.
|
||||
*/
|
||||
function probes(m) {
|
||||
let out = [];
|
||||
// The tunnels check themselves: AmneziaWG (packages, kernel module, netifd) and each server's interface.
|
||||
let tunnels = awg.probes(m, { udp_listening: health.udp_listening, reapply: () => apply.apply(), owner: (ifn) => netifd.owner(ifn) });
|
||||
push(out, ...tunnels);
|
||||
// A tunnel's interface edited outside Dawnline (LuCI, uci) keeps that until
|
||||
// the next apply: set up again with Dawnline's settings. Only while
|
||||
// AmneziaWG itself works (its target comes first, and is fixed first).
|
||||
if (filter(tunnels, (p) => p.id == 'awg')[0]?.ok)
|
||||
for (let id, diff in netifd.drift(m)) {
|
||||
let srv = m.servers[id];
|
||||
push(out, {
|
||||
id: `cfg_${id}`, src: 'awg', name: `${srv.name} · interface settings`, who: srv.name, ok: !length(diff),
|
||||
problem: sprintf(L_('interface %s was changed outside Dawnline'), srv.ifname),
|
||||
back: sprintf(L_("interface %s has Dawnline's settings again"), srv.ifname),
|
||||
// Option names only: their values hold the server's private key.
|
||||
detail: () => sprintf(L_('Changed: %s.'), join(', ', diff)),
|
||||
repair: { what: L_('setting it up again'), run: () => apply.apply() },
|
||||
});
|
||||
}
|
||||
if (length(m.servers))
|
||||
push(out, {
|
||||
id: 'nft', src: 'dawnline', name: 'Access rules', who: L_('Access rules'), ok: nft.present(),
|
||||
problem: L_('are missing'), back: L_('are loaded again'),
|
||||
repair: { what: L_('loading them again'), run: () => apply.apply() },
|
||||
});
|
||||
|
||||
if (length(filter(values(m.servers), (x) => x.private_key))) {
|
||||
let fw = apply.firewall_drift(m);
|
||||
push(out, {
|
||||
id: 'fw', src: 'dawnline', name: 'Firewall', who: L_('Firewall'), ok: fw == null,
|
||||
problem: fw == 'off' ? L_("isn't running on this router (fw4): tunnels and the portal are closed")
|
||||
: fw == 'changed' ? L_("rules for Dawnline were changed or removed") : L_("rules for Dawnline aren't loaded"),
|
||||
back: L_('rules for Dawnline are in place again'),
|
||||
// fw4 stopped on purpose is the admin's call: report it, do not start it.
|
||||
repair: fw == 'off' ? null : { what: L_('restoring them'), run: () => { apply.apply(); system('fw4 -q reload'); } },
|
||||
});
|
||||
}
|
||||
|
||||
let s = m.settings;
|
||||
if (s.portal_enabled && !length(health.needs().https) && !length(health.tcp_port_owners(s.portal_port)))
|
||||
push(out, {
|
||||
id: 'portal', src: 'portal', name: 'Portal', who: L_('Personal portal'), ok: health.tcp_listening(s.portal_port),
|
||||
problem: sprintf(L_("isn't answering on port %d"), s.portal_port), back: L_('answers again'),
|
||||
detail: () => sh_out("logread -l 400 2>/dev/null | grep 'uhttpd' | grep -iE 'error|fail|cert|key|bind|address' | tail -n 4"),
|
||||
repair: { what: L_('restarting it'), run: restart_services },
|
||||
});
|
||||
|
||||
// Only while a server is on: with all of them off it idles on loopback.
|
||||
if (stat(loop.RESOLVER.conf) && length(filter(values(m.servers), (x) => x.enabled && x.private_key)))
|
||||
push(out, {
|
||||
id: 'resolver', src: 'dawnline', name: 'Resolver', who: L_('DNS for Home-only devices'), ok: loop.resolver_running(),
|
||||
problem: L_("isn't running"), back: L_('runs again'),
|
||||
repair: { what: L_('restarting it'), run: restart_services },
|
||||
});
|
||||
|
||||
push(out, {
|
||||
id: 'rpc', src: 'dawnline', name: 'LuCI link', who: 'LuCI', ok: system('ubus list luci.dawnline >/dev/null 2>&1') == 0,
|
||||
problem: L_("can't reach Dawnline (luci.dawnline is missing in rpcd)"), back: L_('reaches Dawnline again'),
|
||||
detail: () => sh_out('logread -l 400 2>/dev/null | grep rpcd | tail -n 4'),
|
||||
// A reload keeps LuCI logins; a restart (second try) signs everyone out —
|
||||
// and after three it stops: restarting rpcd every hour for good would
|
||||
// sign every admin out for good.
|
||||
repair: { what: L_('reloading rpcd'), max: 3, run: (n) => system(n > 1 ? '/etc/init.d/rpcd restart >/dev/null 2>&1' : '/etc/init.d/rpcd reload >/dev/null 2>&1') },
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
/** One pass. With repairs off it only reports. */
|
||||
export function run(m) {
|
||||
let now = time();
|
||||
let fix = m.settings.watchdog;
|
||||
// An update is replacing packages or restarting tunnels: what looks broken
|
||||
// meanwhile is being worked on; repairs would only get in its way.
|
||||
let busy = upd.busy();
|
||||
let st = read_json(STATE, {});
|
||||
let t = type(st.targets) == 'object' ? st.targets : {};
|
||||
let repairs = filter(st.repairs ?? [], (r) => now - r.ts < 86400);
|
||||
let seen = {};
|
||||
for (let p in probes(m)) {
|
||||
seen[p.id] = true;
|
||||
let x = t[p.id] ??= { bad: 0, tries: 0, next: 0 };
|
||||
x.name = p.name;
|
||||
x.ok = p.ok;
|
||||
if (p.ok) {
|
||||
if (x.reported)
|
||||
slog(p.src, 'up', x.stuck ? sprintf(L_('%s · restored'), p.back)
|
||||
: x.tries ? sprintf(x.tries > 1 ? L_('%s (after %d repairs)') : L_('%s (after %d repair)'), p.back, x.tries) : p.back, null, p.who);
|
||||
x.bad = 0;
|
||||
x.next = 0;
|
||||
x.reported = false;
|
||||
x.stuck = false;
|
||||
// The repairs so far are forgotten only once it has stayed up a while:
|
||||
// a target that fails every other minute keeps its back-off.
|
||||
x.good = (x.good ?? 0) + 1;
|
||||
if (x.good >= SETTLED) {
|
||||
x.tries = 0;
|
||||
x.good = 0;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
x.good = 0;
|
||||
// One bad minute can be a restart in progress.
|
||||
if (++x.bad < 2)
|
||||
continue;
|
||||
if (busy)
|
||||
continue;
|
||||
if (p.repair?.max && x.tries >= p.repair.max && !x.reported) {
|
||||
slog(p.src, 'error', sprintf(L_("%s - not repaired again (%d repairs didn't help)"), p.problem, p.repair.max), p.detail?.(), p.who);
|
||||
x.reported = true;
|
||||
continue;
|
||||
}
|
||||
if (!fix || !p.repair || (p.repair.max && x.tries >= p.repair.max)) {
|
||||
if (!x.reported)
|
||||
slog(p.src, 'error', p.problem, p.detail?.(), p.who);
|
||||
x.reported = true;
|
||||
continue;
|
||||
}
|
||||
// Three repairs did not help: say so once, in red, and from then on keep
|
||||
// trying quietly until it works again (that is logged).
|
||||
if (x.tries >= 3 && !x.stuck && now - (x.last ?? 0) >= 50) {
|
||||
x.stuck = true;
|
||||
slog(p.src, 'error', sprintf(L_("%s - %d repairs didn't help"), p.problem, x.tries), p.detail?.(), p.who);
|
||||
}
|
||||
if (now < x.next)
|
||||
continue;
|
||||
x.tries++;
|
||||
x.last = now;
|
||||
x.next = now + WAIT[min(x.tries, length(WAIT) - 1)];
|
||||
x.reported = true;
|
||||
if (!x.stuck)
|
||||
slog(p.src, 'repair', sprintf(L_('%s - %s'), p.problem, p.repair.what), p.detail?.(), p.who);
|
||||
try {
|
||||
p.repair.run(x.tries);
|
||||
}
|
||||
catch (e) {
|
||||
if (!x.stuck)
|
||||
slog(p.src, 'error', sprintf(L_("couldn't be repaired: %s"), e.message), null, p.who);
|
||||
}
|
||||
if (!x.stuck)
|
||||
push(repairs, { ts: now, id: p.id, name: p.name });
|
||||
}
|
||||
for (let id in keys(t))
|
||||
if (!seen[id])
|
||||
delete t[id];
|
||||
write_json(STATE, { last: now, targets: t, repairs }, 0o644);
|
||||
};
|
||||
|
||||
/**
|
||||
* The chip names of probes() by target id, in the language of the call. The
|
||||
* others (AmneziaWG's, "Home · AmneziaWG") are names only and stay as stored.
|
||||
*/
|
||||
function names() {
|
||||
return {
|
||||
agent: _('Agent'), nft: _('Access rules'), fw: _('Firewall'), portal: _('Portal'),
|
||||
resolver: _('Resolver'), rpc: _('LuCI link'),
|
||||
};
|
||||
}
|
||||
|
||||
/** A target's chip name in the language of the call: names(), a server's interface settings, else as stored. */
|
||||
function chip_name(m, n, id, stored) {
|
||||
if (n[id])
|
||||
return n[id];
|
||||
let srv = substr(id, 0, 4) == 'cfg_' ? m.servers[substr(id, 4)] : null;
|
||||
return srv ? sprintf(_('%s · interface settings'), srv.name) : stored;
|
||||
}
|
||||
|
||||
/**
|
||||
* For the Health page: the watchdog's state and the newest log entries. Target
|
||||
* and repair names in the language of the call; the log as it was written.
|
||||
*/
|
||||
export function view(m) {
|
||||
let st = read_json(STATE, {});
|
||||
let beat = +(read_json(BEAT, 0) ?? 0);
|
||||
let n = names();
|
||||
// The agent itself: fine while it ticks (cron restarts it after 5 minutes without).
|
||||
let targets = [ { id: 'agent', name: n.agent, ok: beat > 0 && time() - beat < 180, tries: 0 } ];
|
||||
for (let id, x in (type(st.targets) == 'object' ? st.targets : {}))
|
||||
push(targets, { id, name: chip_name(m, n, id, x.name), ok: !!x.ok, tries: x.tries ?? 0 });
|
||||
return {
|
||||
watchdog: {
|
||||
enabled: m.settings.watchdog, last: st.last ?? null, beat: beat || null, now: time(),
|
||||
targets, repairs: map(st.repairs ?? [], (r) => ({ ...r, name: chip_name(m, n, r.id, r.name) })),
|
||||
},
|
||||
log: slog_read(200),
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user