Files
dawnline/web/src/portal/ui/codes.ts
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

212 lines
8.8 KiB
TypeScript

// What goes to another device: a QR code and the words that go with it, and a
// link as text with Copy and Share. Codes follow the theme: the night look
// (light dots on black) on a dark one, the same dots dark on white on a light
// one, redrawn in place when the system's theme changes. A dense code (an
// AmneziaVPN key) fills its row; the sheet widens for it (ui/sheet.ts). A code
// too long for a camera gives its place to a stand-in (the device's set-up link).
import { denseQr, qrSvg, scannable } from '../../shared/qr';
import { h, ic, markup } from '../dom';
import { EASE_IN, EASE_OUT, SMOOTH, play } from '../motion';
import { placeName } from '../state';
import { copy } from './feedback';
const LIGHT = matchMedia('(prefers-color-scheme: light)');
const look = () => (LIGHT.matches ? 'day' : 'night');
/** The code for `text` in the theme's look, tagged with what it says. */
function drawn(text: string): SVGSVGElement {
const svg = markup(qrSvg(text, 3, true, look())) as SVGSVGElement;
svg.dataset.qr = text;
return svg;
}
/** The box shows `svg` (or is about to): its size, and the sheet's width, follow that code. */
function holds(box: HTMLElement, svg: SVGSVGElement) {
box.dataset.qr = svg.dataset.qr;
box.classList.toggle('dense', denseQr(svg.dataset.qr!));
box.classList.toggle('light', LIGHT.matches);
// The modules across: how wide the sheet must be for a camera to read it.
box.dataset.mods = String(svg.viewBox.baseVal.width);
}
export function qrBox(text: string, big = false): HTMLElement {
const box = h('div', { class: `qrbox${big ? ' big' : ''}` });
const svg = drawn(text);
box.append(svg);
holds(box, svg);
return box;
}
// Each code is redrawn in the other look where it stands: a cross-fade under way carries on.
LIGHT.addEventListener('change', () =>
document.querySelectorAll<HTMLElement>('.qrbox[data-qr]').forEach((b) => {
b.querySelectorAll<SVGSVGElement>(':scope > svg[data-qr]').forEach((s) => s.replaceChildren(...drawn(s.dataset.qr!).childNodes));
b.classList.toggle('light', LIGHT.matches);
}),
);
/** Where an element's fade stands now, mid-fade included. */
function now(el: Element): Keyframe {
const s = getComputedStyle(el);
return { opacity: s.opacity, filter: s.filter === 'none' ? 'blur(0px)' : s.filter };
}
export interface CodeRow {
el: HTMLElement;
/** Another code in the same place: it comes in over the old one, which then goes; new words too, if given (else they stay, to change in place). */
swap(next: string, words?: Node[]): void;
}
/** What a code row shows instead of a code a camera cannot read: another code, and words that say why (made each time it shows). */
export interface StandIn {
text: string;
words: () => Node[];
}
/**
* A code and its words: side by side, or a dense code full width with the
* words under it. With a stand-in, a code too long to scan shows the stand-in
* and its words; the row's own words come back with a code that fits.
*/
export function codeRow(text: string, words: Node[], standIn?: StandIn): CodeRow {
const instead = (t: string) => !!standIn && !scannable(t);
let own = words;
let out = instead(text);
const box = qrBox(out ? standIn!.text : text);
let shown = h('div', { class: 'words' }, out ? standIn!.words() : own);
const side = h('div', { class: 'side' }, shown);
const el = h('div', { class: `coderow${box.classList.contains('dense') ? ' stacked' : ''}` }, box, side);
/**
* Another code in the box: it comes in under the one showing, which lies over
* it and goes after it, so the place is never empty. Nothing on screen changes
* its place in the stack: the codes lie in the box in the order they paint (the
* one in flow first, under the rest). A swap under way carries on from where
* each code stands; a code turned back to fades up again where it lies, what
* lies over it going, and what lies under it goes once it shows whole. A code
* of the other size (dense or not) takes the box from its size on screen to its
* own on SMOOTH, the codes in it scaling with it, while the sheet glides the
* rest (glide() in ui/sheet.ts).
*/
let sizing: Animation | null = null;
/** The codes on their way out: a later swap leaves their fade as it is. */
const going = new WeakSet<Element>();
const cross = (code: string) => {
const svgs = [...box.querySelectorAll<SVGSVGElement>(':scope > svg')];
const op = new Map(svgs.map((s) => [s, +getComputedStyle(s).opacity]));
const cs = getComputedStyle(box);
const r0 = box.getBoundingClientRect();
// Its corners as they look now: a resize under way scales them with the box.
const round0 = parseFloat(cs.borderTopLeftRadius) * new DOMMatrix(cs.transform).a;
const neu = svgs.find((s) => s.dataset.qr === code) ?? drawn(code);
const at = svgs.indexOf(neu);
// A new code goes under them all; one still in the box stays where it lies.
if (at < 0) box.prepend(neu);
const over = svgs.slice(at + 1).filter((s) => !going.has(s));
const under = svgs.slice(0, Math.max(0, at));
const fades = [neu, ...over].flatMap((s) => s.getAnimations());
going.delete(neu);
holds(box, neu);
el.classList.toggle('stacked', box.classList.contains('dense'));
const o0 = op.get(neu) ?? 0;
play(neu, [{ opacity: o0 }, { opacity: 1 }], { duration: 200 * (1 - o0), easing: EASE_OUT }).onfinish = () => {
// Whole: what lies under it, hidden now, goes, and it takes the box's flow under what still fades over it.
under.forEach((s) => s.remove());
neu.classList.remove('old');
};
// What lies over it goes from how it shows, where it lies (the code that showed, over the new one); one already going goes on.
over.forEach((s) => {
s.classList.add('old');
going.add(s);
play(s, [{ opacity: op.get(s)! }, { opacity: 0 }], {
duration: 220 * op.get(s)!,
delay: 110,
easing: EASE_IN,
fill: 'both',
}).onfinish = () => s.remove();
});
fades.forEach((a) => a.cancel());
sizing?.cancel();
sizing = null;
const r1 = box.getBoundingClientRect();
const sx = r0.width / r1.width;
const sy = r0.height / r1.height;
// Not on screen (a folded row): nothing to watch move.
if (r0.width && r1.width && (Math.abs(sx - 1) > 0.005 || Math.abs(sy - 1) > 0.005))
sizing = play(
box,
[
{ transform: `scale(${sx}, ${sy})`, transformOrigin: '0 0', borderRadius: `${round0 / sx}px` },
{ transform: 'none', transformOrigin: '0 0', borderRadius: getComputedStyle(box).borderTopLeftRadius },
],
{ duration: SMOOTH.duration, easing: SMOOTH.easing },
);
};
return {
el,
swap(next, nextWords) {
if (nextWords) own = nextWords;
const was = out;
out = instead(next);
const code = out ? standIn!.text : next;
// The same code for another app (AmneziaWG and WireGuard take one file): only the words change.
if (box.dataset.qr !== code) cross(code);
// The stand-in's words stay while they say the same (they may follow what it stands in for); the row's own
// come back with a code that fits.
let put = out ? standIn!.words() : (nextWords ?? (was ? own : null));
if (out && was && put!.map((n) => n.textContent).join('') === shown.textContent) put = null;
if (!put) return;
const oldWords = shown;
// From where they stand: words still coming in (a pick right after a pick) go from there.
const from = now(oldWords);
const fades = oldWords.getAnimations();
shown = h('div', { class: 'words' }, put);
side.appendChild(shown);
oldWords.classList.add('old');
play(
shown,
[
{ opacity: 0, filter: 'blur(3px)' },
{ opacity: 1, filter: 'blur(0px)' },
],
{ duration: 220, easing: EASE_OUT },
);
play(oldWords, [from, { opacity: 0, filter: 'blur(3px)' }], { duration: 200, delay: 90, easing: EASE_IN, fill: 'both' }).onfinish =
() => oldWords.remove();
fades.forEach((a) => a.cancel());
},
};
}
/** A link as text with Copy and, where the browser has one, the share sheet. */
export function linkRow(url: string): HTMLElement {
const share = typeof navigator.share === 'function';
return h(
'div',
{ class: 'linkrow' },
h('span', { class: 'mono' }, url.replace(/^https?:\/\//, '')),
h(
'button',
{
type: 'button',
class: 'knob',
'aria-label': _('Copy the link'),
onclick: (ev: Event) => copy(url, _('Link copied'), ev.currentTarget),
},
ic('copy'),
),
share
? h(
'button',
{
type: 'button',
class: 'knob',
'aria-label': _('Share the link'),
onclick: () => navigator.share({ url, title: placeName() }).catch(() => undefined),
},
ic('share'),
)
: null,
);
}