Files
dawnline/package/luci-app-dawnline/root/usr/share/ucode/dawnline/rpc/servers.uc
T
Aemeath dc0f159447
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
Dawnline 0.14.0
- A device is renamed where its name stands on its card: a click on the
  name, Return or Space on it, or Rename in the ⋯ menu turns it into a
  field with the name selected. Return saves, and the words shimmer until
  the router answers. Esc or a click elsewhere puts the old name back. The
  rename window is gone, and the page no longer redraws under a name being
  edited
- Device names take up to 20 characters: letters of any language, digits,
  spaces and - _ . The router refuses anything else when a device is added
  or renamed, so a name can't carry symbols into configurations, file
  names or the AmneziaVPN key. The portal and the admin turn a character
  like that away as it is typed: the field shakes, its outline goes red
  and a short line says why. Suggested names (a phone's model, "Phone 2")
  are stripped of such characters and cut to fit. Names set before this
  stay as they are
- LAN Bypass: Add a device, in the portal and in the admin, has a switch
  that keeps the device's own local network (printer, TV, NAS) reachable
  while it is connected. It is off by default and stays as chosen; the
  admin's device drawer shows it. With home and internet access the switch
  says which addresses still lead to the home network
- Health warns when a server's home network is on one of the subnets home
  routers come with (192.168.0.x, 192.168.1.x and others). People with
  home access who connect from a network on the same addresses reach your
  home instead of their own, LAN Bypass or not
- The router tells when a device can't connect properly. After the problem
  lasts three minutes, the portal card and the admin drawer name it:
  can't finish connecting (the network may block it), connected but no
  data (an outdated configuration, and the card offers Reconnect), on two
  devices at once, or a connection that keeps dropping. Activity lists
  these under Problems, one line per device per hour, and says when the
  device connects normally again
- The set-up sheet's connection line follows what the device does:
  waiting, can't finish, connected, or connected but no data. The sheet
  closes by itself only once data comes through. On the device itself and
  for a router, the steps regroup to what fixes the problem: "Set it up
  again" when the handshake never finishes, "Add the connection again"
  ("Import the configuration again" for a router) when no data comes
- Set-up in the portal: a QR code too long for a camera to read gives its
  place to a code that opens the set-up link (a LAN Bypass configuration
  or a long AmneziaVPN key can be that long). A TV, box or NAS added from
  a phone gets the steps for setting up from another device. Back from
  "Open on the device" keeps the kind, LAN Bypass and the name typed. The
  packet-size note under the kinds is gone
- The portal on phones keeps its place: nothing jumps when an access row
  opens or folds, a notice comes or goes, or Safari redraws the cards. A
  reload, or coming back from the VPN app, opens where the page was. Cards
  glide to their new height, and an animation interrupted half-way goes on
  from where it is instead of snapping. A double click no longer closes
  the sheet it just opened
- In the admin, a device's drawer has its own MTU and keep-alive (empty
  means the server's), each with a Change dialog. A change touches only
  that device's configuration and restarts nothing on the router. Opening
  the drawer no longer counts as handing out the configuration: "Update
  config" stays until the new configuration is shown as a QR code,
  downloaded or copied ("Show the new configuration"). New keys set the
  device up again, as the portal's Reconnect does
- "Speed now" on a server's Overview is live: one point a second over the
  last two minutes, all there as soon as the Overview opens. The agent
  keeps those two minutes in memory and writes them out only while someone
  is looking
- Disguise values that AmneziaWG refuses or crashes on are stopped before
  they reach the kernel. That covers overlapping H1-H4, Jmin above Jmax,
  broken I1-I5 tags, padding under 12 with header protection, and more.
  Import and restore refuse them. After a hand edit in uci, the server's
  tunnel stays down and Health and the service log say why and what to do.
  Errors are full sentences in the reader's language
- The header protection key no longer shows in the status that read-only
  users can see: the Server page says only that the key is set, and each
  device's configuration still has it. The server no longer turns off
  WireGuard's protection against handshake floods (devices keep
  DisableCookies). A device can't be added with a server's own key
- An AmneziaWG update whose kernel module can't be swapped (another
  AmneziaWG interface holds the old one) no longer fails. It is installed,
  the tunnels come back on the old module, and Health and Components say
  the update starts after a reboot. On apk firmware, an install that apk
  left at the old version counts as failed. A module installed by hand is
  recorded, so after a firmware upgrade Health can tell that it is gone
- Dawnline reads only its own tunnels, each one separately. Another
  AmneziaWG interface on the router no longer slows the agent's
  once-a-second check, has its keys read or breaks Dawnline's reads. A
  portal request reads only that person's server: one process however many
  servers there are
- Fewer false disconnects: a tunnel that netifd sets up again no longer
  logs everyone as leaving and coming back, and a device with a long
  keep-alive stays online between handshakes. One configuration on two
  devices isn't logged as roaming every few seconds, and new keys end the
  old session at once. A tunnel that is missing devices is synced within a
  minute, and traffic across a tunnel restart is no longer undercounted
- Renaming a server no longer restarts its tunnel. If a tunnel's interface
  is edited outside Dawnline (LuCI, uci), the watchdog sets it up again
  with Dawnline's settings and names only the changed options. The routing
  rules for "Internet like a home device" change one at a time, new ones
  first, so traffic never falls between them. Health and the Server page
  warn when the address's AAAA record isn't this router's, or the router
  has no public IPv6: devices that prefer IPv6 fail to connect
- The README screenshots come in English and Russian; the portal's are
  taken from its demo. The demo has a switch beside × in the set-up sheet
  to show each state of the connection line. In Russian, long labels in
  the admin's wide option buttons wrap under their icon. DESIGN.md
  describes the switch, renaming on the card, the shimmer, the shake and
  how the portal scrolls. New tests cover link states, the meter, access,
  Health, firewall rules, network settings and packages
2026-10-22 00:11:50 +03:00

663 lines
24 KiB
Ucode
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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) },
};