Files
dawnline/package/luci-app-dawnline/root/usr/share/ucode/dawnline/access.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

334 lines
14 KiB
Ucode

// 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 };
};