Dawnline 0.4.0
- Components tab replaces Updates: Dawnline, the AmneziaWG build for this firmware (Slava-Shchipunov 2.0 or 2Grey 3.1, Russian translation) and the system packages Dawnline adds, each checked, installed, reinstalled or rolled back in place, with What's new, versions and an activity log - Automatic updates: daily check, install at a set time or right away, per component; AmneziaWG is left out by default and an automatic run never switches its build or generation - Downloads verified against GitHub's sha256; mirrors are used only when GitHub is unreachable and the hash is known - New installer: checks firmware, clock, space and DNS, lets you pick a release, shows the plan and asks first; --dry-run - New uninstaller removes everything Dawnline added (cron, nft, firewall, routing, interfaces, packages it installed); --dry-run - Works without AmneziaWG on the router: Overview, the setup wizard, Health and Components say what is missing and offer to install it - Overview shows failed health checks as notices with their fix - People list no longer reads the config from the browser: private keys stay on the router; personal links are fetched on demand - Config changes are serialized under one lock, the nft table loads atomically and fails closed, and Dawnline never touches network sections it did not create - The portal never opens system ports, keeps disabled devices disabled, rate-limits per address and counts days by the router's clock - Settings and Diagnostics load their heavy parts in the background - Release notes are written from the commits since the previous release - Backend and admin UI split into small single-purpose modules
This commit is contained in:
1 parent
70a932b8ea
commit
34293fb8e5
158 files changed
+17418
-10022
No files matched your search
@@ -0,0 +1,151 @@
|
||||
// Person page: where they connected from (one line, the map on request) and their activity.
|
||||
|
||||
import { E } from '../../e';
|
||||
import { button } from '../../ui/button';
|
||||
import { dropdown } from '../../ui/menu';
|
||||
import { icon } from '../../../shared/icons';
|
||||
import { ConnectionMap, rangeControl } from '../../map';
|
||||
import { eventRow, eventText } from '../../events/render';
|
||||
import { clearLogDialog, exportEvents } from '../../logs';
|
||||
import { openDevice } from '../../device';
|
||||
import type { PersonPage } from './index';
|
||||
|
||||
function mapCard(page: PersonPage): HTMLElement {
|
||||
const st = page.st;
|
||||
const head = E('div', { class: 'dl-panel-head pad' }, [
|
||||
E('h3', {}, 'Locations'),
|
||||
E('span', { class: 'dl-spacer' }),
|
||||
st.geo.enabled ? rangeControl('30d', (r) => page.map?.setRange(r), false) : '',
|
||||
button(
|
||||
'Hide',
|
||||
() => {
|
||||
page.mapOpen = false;
|
||||
page.map?.destroy();
|
||||
page.map = null;
|
||||
page.paintPlaces();
|
||||
},
|
||||
'neutral',
|
||||
),
|
||||
]);
|
||||
if (!st.geo.enabled)
|
||||
return E('div', { class: 'dl-panel flush' }, [
|
||||
head,
|
||||
E('div', { class: 'dl-map-off' }, [
|
||||
'Locations are off. ',
|
||||
E('a', { href: L.url('admin/network/dawnline/settings') }, 'Turn them on in Settings'),
|
||||
'.',
|
||||
]),
|
||||
]);
|
||||
const map = new ConnectionMap(
|
||||
{
|
||||
openPerson: () => undefined,
|
||||
openDevice: (_pid, did) => {
|
||||
const d = page.p.devices.find((x) => x.id === did);
|
||||
if (d) void openDevice(page.p, d, page.deviceOpts());
|
||||
},
|
||||
},
|
||||
'30d',
|
||||
280,
|
||||
);
|
||||
page.map = map;
|
||||
requestAnimationFrame(() => map.mount(page.mapData()));
|
||||
return E('div', { class: 'dl-panel flush' }, [head, map.el]);
|
||||
}
|
||||
|
||||
/** Where they connected from: one line, the map on request; null while the open map is already there. */
|
||||
export function placesPart(page: PersonPage): HTMLElement | null {
|
||||
const st = page.st;
|
||||
if (!st.geo.enabled)
|
||||
return E('div', { class: 'dl-places' }, [
|
||||
icon('pin'),
|
||||
E('div', { class: 'dl-stack' }, [
|
||||
E('span', {}, 'Locations are off'),
|
||||
E('span', { class: 'dl-sub' }, 'City-level, from the client IP'),
|
||||
]),
|
||||
E('span', { class: 'dl-spacer' }),
|
||||
E('a', { class: 'cbi-button cbi-button-neutral', href: L.url('admin/network/dawnline/settings') }, 'Turn on'),
|
||||
]);
|
||||
if (page.mapOpen) return page.map && page.parts.places.contains(page.map.el) ? null : mapCard(page);
|
||||
const since = st.now - 30 * 86400;
|
||||
const pts = st.geo.points.filter((g) => g.person === page.p.id && g.ts >= since).sort((a, b) => b.ts - a.ts);
|
||||
const places = [...new Set(pts.map((g) => g.city || g.country).filter(Boolean))];
|
||||
const last = pts[0];
|
||||
return E('div', { class: 'dl-places' }, [
|
||||
icon('pin'),
|
||||
E('div', { class: 'dl-stack' }, [
|
||||
E('span', {}, last ? last.city || last.country : 'No locations yet'),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
last
|
||||
? `${places.length} place${places.length === 1 ? '' : 's'} in 30 days${last.org ? ` · ${last.org}` : ''}`
|
||||
: 'Shown after a device connects',
|
||||
),
|
||||
]),
|
||||
E('span', { class: 'dl-spacer' }),
|
||||
pts.length
|
||||
? button(
|
||||
'Show map',
|
||||
() => {
|
||||
page.mapOpen = true;
|
||||
page.paintPlaces();
|
||||
},
|
||||
'neutral',
|
||||
)
|
||||
: '',
|
||||
]);
|
||||
}
|
||||
|
||||
/** The latest 10 events about them; back-and-forth network changes (Wi-Fi ⇄ mobile) fold into one row. */
|
||||
export function activityPart(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const st = page.st;
|
||||
const all = page.events.filter((e) => e.person === p.id);
|
||||
const rows: HTMLElement[] = [];
|
||||
const latest = [...all].reverse();
|
||||
for (let i = 0; i < latest.length && rows.length < 10; i++) {
|
||||
const e = latest[i];
|
||||
let n = 1;
|
||||
while (e.type === 'roam' && latest[i + n]?.type === 'roam' && latest[i + n].device === e.device) n++;
|
||||
const row = eventRow(st, e, { server: false });
|
||||
if (n > 1) {
|
||||
const main = row.querySelector('.dl-row-main') as HTMLElement;
|
||||
dom.content(main, [...eventText(st, e).slice(0, 3), E('span', { class: 'dl-muted' }, `${n} times`)]);
|
||||
main.title = latest
|
||||
.slice(i, i + n)
|
||||
.map((x) => x.detail ?? '')
|
||||
.join('\n');
|
||||
}
|
||||
rows.push(row);
|
||||
i += n - 1;
|
||||
}
|
||||
const more = button(
|
||||
[icon('more')],
|
||||
() =>
|
||||
dropdown(more, [
|
||||
{ label: 'Export CSV', icon: icon('download'), run: () => exportEvents(st, all, 'csv', p.id) },
|
||||
{ label: 'Export JSON', icon: icon('download'), run: () => exportEvents(st, all, 'json', p.id) },
|
||||
null,
|
||||
{
|
||||
label: `Clear ${p.name}'s activity`,
|
||||
icon: icon('trash'),
|
||||
danger: true,
|
||||
run: () =>
|
||||
clearLogDialog(`Clear ${p.name}'s activity`, `Removes events about ${p.name} and their devices.`, { person: p.id }, () =>
|
||||
page.refresh(),
|
||||
),
|
||||
},
|
||||
]),
|
||||
'neutral',
|
||||
{ title: 'Export or clear', class: 'cbi-button cbi-button-neutral dl-icon-only' },
|
||||
);
|
||||
return E('div', { class: 'dl-panel flush' }, [
|
||||
E('div', { class: 'dl-panel-head pad' }, [
|
||||
E('h3', {}, 'Activity'),
|
||||
E('span', { class: 'dl-sub' }, all.length > 10 ? `latest 10 of ${all.length}` : ''),
|
||||
E('span', { class: 'dl-spacer' }),
|
||||
more,
|
||||
]),
|
||||
rows.length ? rows : E('div', { class: 'dl-row dl-muted' }, 'Nothing yet.'),
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
// Person page: the device cards (click one for its drawer) and the Add device slot.
|
||||
|
||||
import type { Device } from '../../../shared/types';
|
||||
import { E } from '../../e';
|
||||
import { api } from '../../api';
|
||||
import { button } from '../../ui/button';
|
||||
import { cancelButton, modal } from '../../ui/modal';
|
||||
import { downloadFile } from '../../ui/files';
|
||||
import { attempt, notify } from '../../ui/toast';
|
||||
import { deviceStatus } from '../../ui/badges';
|
||||
import { icon } from '../../../shared/icons';
|
||||
import { KIND_LABEL, ago, byDeviceState, bytes } from '../../../shared/format';
|
||||
import { addDevice, openDevice, quickMenu } from '../../device';
|
||||
import { dayBars } from '../../widgets/tiles';
|
||||
import type { PersonPage } from './index';
|
||||
|
||||
/** Setting up a device: its QR code (key and file one tap away). */
|
||||
async function showDeviceQr(page: PersonPage, d: Device) {
|
||||
const cfg = await attempt(() => api.deviceConfig(d.id));
|
||||
if (!cfg) return;
|
||||
modal(`Set up ${d.name}`, [await page.core.connectBlock(cfg, 'qr')], [cancelButton('Close')]);
|
||||
}
|
||||
|
||||
async function downloadConfig(page: PersonPage, d: Device) {
|
||||
const cfg = await attempt(() => api.deviceConfig(d.id));
|
||||
if (!cfg) return;
|
||||
if (!cfg.has_private_key) {
|
||||
notify('This device made its own keys in the browser; its configuration only exists there.', 'warning');
|
||||
return;
|
||||
}
|
||||
const f = page.core.file.make(cfg);
|
||||
downloadFile(f.name, f.text, 'application/octet-stream');
|
||||
}
|
||||
|
||||
export function devicesPart(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const now = page.st.now;
|
||||
const open = (d: Device) => openDevice(p, d, page.deviceOpts());
|
||||
// The card opens the device's panel; its buttons (⋯, setup chips) do their own thing.
|
||||
const clickCard = (d: Device) => (ev: Event) => {
|
||||
if (!(ev.target as HTMLElement).closest('button:not(.dl-dcard-title), a')) void open(d);
|
||||
};
|
||||
const more = (d: Device) =>
|
||||
button([icon('more')], (ev: MouseEvent) => quickMenu(ev.currentTarget as HTMLElement, p, d, page.deviceOpts()), 'neutral', {
|
||||
title: 'QR, file, key, rename, new keys, remove',
|
||||
class: 'cbi-button cbi-button-neutral dl-icon-only',
|
||||
});
|
||||
const cards = byDeviceState(p.devices).map((d) => {
|
||||
const head = E('div', { class: 'dl-dcard-head' }, [
|
||||
E('span', { class: 'dl-dcard-ic' }, icon(d.kind)),
|
||||
E('button', { type: 'button', class: 'dl-dcard-title', click: () => open(d) }, [
|
||||
E('b', {}, d.name),
|
||||
deviceStatus(d, now),
|
||||
d.config_stale ? E('span', { class: 'dl-pill warn dl-stale' }, 'Update config') : '',
|
||||
]),
|
||||
more(d),
|
||||
]);
|
||||
const trend = E('div', { class: 'dl-dcard-trend' }, [
|
||||
E('div', { class: 'dl-stack' }, [E('b', {}, bytes(d.month[0] + d.month[1])), E('span', { class: 'dl-sub' }, 'this month')]),
|
||||
dayBars(page.devSeries[d.id] ?? []),
|
||||
]);
|
||||
return E('article', { class: `dl-dcard clickable${d.online ? ' online' : ''}${d.disabled ? ' off' : ''}`, click: clickCard(d) }, [
|
||||
head,
|
||||
d.handshake
|
||||
? trend
|
||||
: d.disabled
|
||||
? E(
|
||||
'div',
|
||||
{ class: 'dl-sub' },
|
||||
`${KIND_LABEL[d.kind]} · created ${ago(d.created, now)} ${d.created_by === 'portal' ? 'in the portal' : 'by you'}.`,
|
||||
)
|
||||
: E('div', { class: 'dl-chips-row' }, [
|
||||
E('button', { type: 'button', class: 'dl-chipbtn', click: () => showDeviceQr(page, d) }, [icon('qr'), 'Show QR']),
|
||||
E('button', { type: 'button', class: 'dl-chipbtn', click: () => downloadConfig(page, d) }, [icon('download'), 'Config']),
|
||||
]),
|
||||
]);
|
||||
});
|
||||
// Always the last card: admins may add over the person's limit, it says so.
|
||||
const over = p.devices.length >= p.device_limit;
|
||||
cards.push(
|
||||
E('button', { type: 'button', class: 'dl-dslot add', click: () => addDevice(p, page.deviceOpts()) }, [
|
||||
E('span', { class: 'dl-dslot-ic' }, icon('plus')),
|
||||
E('span', { class: 'dl-stack' }, [
|
||||
E('b', {}, 'Add device'),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
over ? `Over the limit · ${p.devices.length} of ${p.device_limit}` : `${p.devices.length} of ${p.device_limit}`,
|
||||
),
|
||||
]),
|
||||
]),
|
||||
);
|
||||
return E('section', { class: 'dl-devsec' }, [
|
||||
E('div', { class: 'dl-panel-head' }, [E('h3', {}, 'Devices'), E('span', { class: 'dl-sub' }, `${p.online} online`)]),
|
||||
E('div', { class: 'dl-dgrid' }, cards),
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
// Person page dialogs: rename, note, when access ends, new link, invite, delete.
|
||||
|
||||
import { E } from '../../e';
|
||||
import { api } from '../../api';
|
||||
import { button } from '../../ui/button';
|
||||
import { cancelButton, closeModal, confirmDialog, modal } from '../../ui/modal';
|
||||
import { copyText } from '../../ui/files';
|
||||
import { attempt } from '../../ui/toast';
|
||||
import { inviteCard } from '../../invite';
|
||||
import { peopleUrl } from '../../urls';
|
||||
import type { PersonPage } from './index';
|
||||
|
||||
const field = (label: string, el: Node) =>
|
||||
E('div', { class: 'cbi-value' }, [E('label', { class: 'cbi-value-title' }, label), E('div', { class: 'cbi-value-field' }, el)]);
|
||||
|
||||
export function invite(page: PersonPage) {
|
||||
const server = page.st.servers.find((s) => s.id === page.p.server)?.name ?? 'Home';
|
||||
void inviteCard(page.p, server);
|
||||
}
|
||||
|
||||
export async function newLink(page: PersonPage) {
|
||||
const p = page.p;
|
||||
if (!(await confirmDialog('Create a new link?', 'The old link stops working right away.', 'Create new link', true))) return;
|
||||
const r = await attempt(() => api.personRegen(p.id), 'New link created');
|
||||
if (r?.link) await copyText(r.link, 'New link copied');
|
||||
}
|
||||
|
||||
export function editNote(page: PersonPage) {
|
||||
const input = E(
|
||||
'textarea',
|
||||
{ class: 'cbi-input-textarea dl-note-input', rows: 3, maxlength: 500, placeholder: 'Only you see this' },
|
||||
page.p.note ?? '',
|
||||
) as HTMLTextAreaElement;
|
||||
modal(
|
||||
'Note',
|
||||
[input],
|
||||
[
|
||||
cancelButton(),
|
||||
' ',
|
||||
button(
|
||||
'Save',
|
||||
async () => {
|
||||
closeModal();
|
||||
await page.patch({ note: input.value.trim() }, input.value.trim() ? 'Note saved' : 'Note removed');
|
||||
},
|
||||
'positive',
|
||||
),
|
||||
],
|
||||
);
|
||||
setTimeout(() => input.focus(), 50);
|
||||
}
|
||||
|
||||
export function endSettings(page: PersonPage) {
|
||||
const p = page.p;
|
||||
const act = E('select', { class: 'cbi-input-select' }, [
|
||||
E('option', { value: 'pause', selected: p.expire_action === 'pause' ? 'selected' : null }, 'Pause access'),
|
||||
E('option', { value: 'notify', selected: p.expire_action === 'notify' ? 'selected' : null }, 'Keep access'),
|
||||
E('option', { value: 'remove', selected: p.expire_action === 'remove' ? 'selected' : null }, 'Remove their devices'),
|
||||
]) as HTMLSelectElement;
|
||||
const warn = E(
|
||||
'select',
|
||||
{ class: 'cbi-input-select' },
|
||||
[0, 1, 3, 7, 14].map((n) =>
|
||||
E(
|
||||
'option',
|
||||
{ value: n, selected: p.expire_warn === n ? 'selected' : null },
|
||||
n ? `${n} day${n === 1 ? '' : 's'} before` : 'Don’t warn',
|
||||
),
|
||||
),
|
||||
) as HTMLSelectElement;
|
||||
modal(
|
||||
'When access ends',
|
||||
[
|
||||
field('Then', act),
|
||||
E('div', { class: 'cbi-value' }, [
|
||||
E('label', { class: 'cbi-value-title' }, 'Warn'),
|
||||
E('div', { class: 'cbi-value-field' }, [
|
||||
warn,
|
||||
E('div', { class: 'cbi-value-description' }, 'Shown in their portal and on the Overview.'),
|
||||
]),
|
||||
]),
|
||||
],
|
||||
[
|
||||
cancelButton(),
|
||||
' ',
|
||||
button(
|
||||
'Save',
|
||||
async () => {
|
||||
closeModal();
|
||||
await page.patch({ expire_action: act.value, expire_warn: warn.value }, 'Saved');
|
||||
},
|
||||
'positive',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
export function rename(page: PersonPage) {
|
||||
const input = E('input', { type: 'text', class: 'cbi-input-text', value: page.p.name, maxlength: 48 }) as HTMLInputElement;
|
||||
modal(
|
||||
'Rename',
|
||||
[field('Name', input)],
|
||||
[
|
||||
cancelButton(),
|
||||
' ',
|
||||
button(
|
||||
'Save',
|
||||
async () => {
|
||||
closeModal();
|
||||
await page.patch({ name: input.value }, 'Renamed');
|
||||
},
|
||||
'positive',
|
||||
),
|
||||
],
|
||||
);
|
||||
setTimeout(() => input.select(), 50);
|
||||
}
|
||||
|
||||
export async function remove(page: PersonPage) {
|
||||
const p = page.p;
|
||||
const ok = await confirmDialog(
|
||||
`Delete ${p.name}?`,
|
||||
`All ${p.devices.length} devices are disconnected immediately and the personal link stops working.`,
|
||||
'Delete',
|
||||
true,
|
||||
);
|
||||
if (ok && (await attempt(() => api.personDelete(p.id), 'Deleted'))) location.href = peopleUrl(p.server);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
// Person page: the name block with its actions, and the traffic tiles.
|
||||
|
||||
import { E } from '../../e';
|
||||
import { iconButton } from '../../ui/button';
|
||||
import { dropdown, type MenuItem } from '../../ui/menu';
|
||||
import { stateBadge } from '../../ui/badges';
|
||||
import { icon } from '../../../shared/icons';
|
||||
import { ago, bytes, date, initials, isoDay } from '../../../shared/format';
|
||||
import { copyPersonLink } from '../../invite';
|
||||
import { daysTile, speedTile } from '../../widgets/tiles';
|
||||
import { editNote, invite, newLink, remove, rename } from './dialogs';
|
||||
import type { PersonPage } from './index';
|
||||
|
||||
export function headPart(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const st = page.st;
|
||||
const lastSeen = p.online ? 'online now' : p.last_seen ? `last seen ${ago(p.last_seen, st.now)}` : 'never connected';
|
||||
const added = p.created ? `Added ${date(isoDay(new Date(p.created * 1000)))}` : '';
|
||||
// The personal link is not in the status: its actions ask for it while there is one.
|
||||
const portal = p.has_link ?? st.settings.portal_enabled;
|
||||
const menu: (MenuItem | null)[] = [
|
||||
{ label: 'Rename', icon: icon('edit'), run: () => rename(page) },
|
||||
{ label: p.note ? 'Edit note' : 'Add note', icon: icon('file'), run: () => editNote(page) },
|
||||
...(portal
|
||||
? [
|
||||
null,
|
||||
{ label: 'Invite card', icon: icon('qr'), run: () => invite(page) },
|
||||
{ label: 'New link', icon: icon('key'), run: () => newLink(page) },
|
||||
]
|
||||
: []),
|
||||
null,
|
||||
{
|
||||
label: p.paused ? 'Resume' : 'Pause',
|
||||
icon: icon(p.paused ? 'play' : 'pause'),
|
||||
warn: !p.paused,
|
||||
run: () => page.patch({ paused: !p.paused }, p.paused ? 'Resumed' : 'Paused'),
|
||||
},
|
||||
{ label: 'Delete', icon: icon('trash'), danger: true, run: () => remove(page) },
|
||||
];
|
||||
return E('div', { class: 'dl-person-head' }, [
|
||||
E('div', { class: 'dl-pagehead' }, [
|
||||
E('div', { class: 'dl-person-id' }, [
|
||||
E('span', { class: `dl-avatar lg${p.online ? ' online' : ''}` }, initials(p.name)),
|
||||
E('div', { class: 'dl-stack' }, [
|
||||
E('div', { class: 'dl-title-row' }, [E('h2', { class: 'dl-h1' }, p.name), stateBadge(p)]),
|
||||
E('span', { class: 'dl-faint', title: added }, `${p.devices.length} of ${p.device_limit} devices · ${lastSeen}`),
|
||||
]),
|
||||
]),
|
||||
E('div', { class: 'dl-pcard-actions' }, [
|
||||
portal ? iconButton('link', 'Copy link', () => copyPersonLink(p.id), 'neutral', 'dl-icon-only') : '',
|
||||
iconButton('more', 'More', (ev: MouseEvent) => dropdown(ev.currentTarget as HTMLElement, menu), 'neutral', 'dl-icon-only'),
|
||||
]),
|
||||
]),
|
||||
// Under the name block, not inside it, so the avatar centres on name + status; indented to the name.
|
||||
p.note
|
||||
? E('button', { type: 'button', class: 'dl-pnote', title: 'Edit note', click: () => editNote(page) }, [
|
||||
icon('file'),
|
||||
E('span', {}, p.note),
|
||||
])
|
||||
: '',
|
||||
]);
|
||||
}
|
||||
|
||||
/** Speed now and the last 30 days, the same tiles as the server Overview; speed only while a device is online. */
|
||||
export function trafficPart(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
// Today on the router: the last day of the series.
|
||||
const todayUse = page.series[page.series.length - 1];
|
||||
return E('div', { class: `dl-person-live${p.online ? '' : ' solo'}` }, [
|
||||
p.online ? speedTile(p.rate ?? [], page.st.now, todayUse ? [todayUse.rx, todayUse.tx] : [0, 0]) : '',
|
||||
daysTile(page.series, `${bytes(p.month[0] + p.month[1])} this month`),
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
// Person page (admin/network/dawnline/person?id=…): devices as cards,
|
||||
// locations, activity, and access & limits that apply immediately.
|
||||
// The parts live in ./header, ./devices, ./limits, ./pickers, ./activity and
|
||||
// ./dialogs; this class holds the state, the poller and the edits.
|
||||
|
||||
import type { DayUsage, LogEvent, Person, Status } from '../../../shared/types';
|
||||
import { E } from '../../e';
|
||||
import { api, type PersonPatch } from '../../api';
|
||||
import { attempt } from '../../ui/toast';
|
||||
import { todayOf } from '../../../shared/format';
|
||||
import { ConnectionMap } from '../../map';
|
||||
import { peopleUrl } from '../../urls';
|
||||
import { addDevice, openDevice, type DeviceOpts } from '../../device';
|
||||
import { coreFor, type Core } from '../../cores';
|
||||
import { createPoller, repaint, type Poller } from '../../state/poller';
|
||||
import { headPart, trafficPart } from './header';
|
||||
import { devicesPart } from './devices';
|
||||
import { limitsPart } from './limits';
|
||||
import { activityPart, placesPart } from './activity';
|
||||
|
||||
type Part = 'head' | 'devices' | 'traffic' | 'places' | 'activity' | 'limits';
|
||||
|
||||
interface Loaded {
|
||||
st: Status;
|
||||
ev: LogEvent[] | null;
|
||||
use: { person: DayUsage[]; devices: Record<string, DayUsage[]> } | null;
|
||||
}
|
||||
|
||||
export class PersonPage {
|
||||
st!: Status;
|
||||
id = '';
|
||||
p!: Person;
|
||||
events: LogEvent[] = [];
|
||||
/** The map stays folded into one line until asked for. */
|
||||
mapOpen = false;
|
||||
map: ConnectionMap | null = null;
|
||||
parts = {} as Record<Part, HTMLElement>;
|
||||
/** Daily traffic for the last 30 days. */
|
||||
series: DayUsage[] = [];
|
||||
/** The same per device. */
|
||||
devSeries: Record<string, DayUsage[]> = {};
|
||||
poller?: Poller<Loaded>;
|
||||
/** Status every 15 s; the log and the traffic history every fourth time (and after a change). */
|
||||
private ticks = 0;
|
||||
private fullNext = false;
|
||||
|
||||
/** Status, and (`full`) the activity log and traffic history. */
|
||||
async fetch(full: boolean): Promise<Loaded> {
|
||||
const [st, ev, use] = await Promise.all([
|
||||
api.status(30),
|
||||
full ? api.events(5000).catch(() => null) : Promise.resolve(null),
|
||||
full ? api.personUsage(this.id, 30).catch(() => null) : Promise.resolve(null),
|
||||
]);
|
||||
return { st, ev: ev?.events ?? null, use };
|
||||
}
|
||||
|
||||
async load(): Promise<Loaded> {
|
||||
this.id = new URLSearchParams(location.search).get('id') ?? '';
|
||||
return this.fetch(true);
|
||||
}
|
||||
|
||||
render(d: Loaded): HTMLElement {
|
||||
this.take(d);
|
||||
const p = this.st.people.find((x) => x.id === this.id);
|
||||
if (!p)
|
||||
return E('div', { class: 'dl-empty' }, [
|
||||
E('h3', {}, 'Person not found'),
|
||||
E('p', {}, 'They may have been removed.'),
|
||||
E('a', { class: 'cbi-button', href: L.url('admin/network/dawnline/server') }, 'Back to Servers'),
|
||||
]);
|
||||
this.p = p;
|
||||
for (const k of ['head', 'devices', 'traffic', 'places', 'activity', 'limits'] as Part[]) this.parts[k] = E('div', {});
|
||||
this.parts.devices.className = 'dl-person-devices';
|
||||
const root = E('div', {}, [
|
||||
E('nav', { class: 'dl-crumbs', 'aria-label': 'Breadcrumb' }, [
|
||||
E('a', { href: peopleUrl(p.server) }, this.st.servers.find((x) => x.id === p.server)?.name ?? 'Servers'),
|
||||
E('span', {}, '/'),
|
||||
E('a', { href: peopleUrl(p.server) }, 'People'),
|
||||
E('span', {}, '/'),
|
||||
E('span', {}, p.name),
|
||||
]),
|
||||
this.parts.head,
|
||||
E('div', { class: 'dl-grid-person' }, [
|
||||
E('div', { class: 'dl-col' }, [this.parts.devices, this.parts.traffic, this.parts.places, this.parts.activity]),
|
||||
E('div', { class: 'dl-col' }, [this.parts.limits]),
|
||||
]),
|
||||
]);
|
||||
this.paint();
|
||||
const q = new URLSearchParams(location.search);
|
||||
const dev = q.get('device') && p.devices.find((x) => x.id === q.get('device'));
|
||||
if (dev) setTimeout(() => openDevice(this.p, dev, this.deviceOpts()), 50);
|
||||
else if (q.get('add')) setTimeout(() => addDevice(this.p, this.deviceOpts()), 50);
|
||||
this.poller = createPoller({
|
||||
fetch: () => {
|
||||
const full = this.fullNext || ++this.ticks % 4 === 0;
|
||||
this.fullNext = false;
|
||||
return this.fetch(full);
|
||||
},
|
||||
every: 15,
|
||||
active: () => root.isConnected,
|
||||
apply: (data, quiet) => this.apply(data, quiet),
|
||||
});
|
||||
return root;
|
||||
}
|
||||
|
||||
private take(d: Loaded) {
|
||||
this.st = d.st;
|
||||
if (d.ev) this.events = d.ev;
|
||||
if (d.use) {
|
||||
this.series = d.use.person;
|
||||
this.devSeries = d.use.devices;
|
||||
}
|
||||
}
|
||||
|
||||
private apply(d: Loaded, quiet: boolean) {
|
||||
this.take(d);
|
||||
const p = this.st.people.find((x) => x.id === this.id);
|
||||
if (!p) {
|
||||
if (!quiet) location.href = peopleUrl(this.p?.server);
|
||||
return;
|
||||
}
|
||||
this.p = p;
|
||||
this.paint(quiet);
|
||||
this.map?.update(this.mapData());
|
||||
}
|
||||
|
||||
/** Everything again, the log and traffic too (after a change). Newest answer wins. */
|
||||
async refresh() {
|
||||
this.fullNext = true;
|
||||
await this.poller?.refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a change now and repaint from the router's answer. Answers of
|
||||
* refreshes already on their way are dropped, so they can't paint the old state over it.
|
||||
*/
|
||||
async patch(patch: PersonPatch, ok?: string) {
|
||||
this.poller?.claim();
|
||||
const r = await attempt(() => api.personUpdate(this.p.id, patch), ok);
|
||||
if (!r) return;
|
||||
this.poller?.claim();
|
||||
const i = this.st.people.findIndex((x) => x.id === this.p.id);
|
||||
r.person.rate ??= this.p.rate;
|
||||
if (i >= 0) this.st.people[i] = r.person;
|
||||
this.p = r.person;
|
||||
this.paint();
|
||||
}
|
||||
|
||||
/** Today on the router (the last day of its traffic series). */
|
||||
get today(): string {
|
||||
return todayOf(this.series.length ? this.series : this.st.series);
|
||||
}
|
||||
|
||||
get core(): Core {
|
||||
return coreFor(this.st.servers, this.p.server);
|
||||
}
|
||||
|
||||
/** What a device drawer opened from here gets: this page's log, clock and core. */
|
||||
deviceOpts(): DeviceOpts {
|
||||
return {
|
||||
refresh: () => this.refresh(),
|
||||
geo: this.st.geo,
|
||||
now: this.st.now,
|
||||
today: this.today,
|
||||
core: this.core,
|
||||
events: () => this.events,
|
||||
};
|
||||
}
|
||||
|
||||
mapData() {
|
||||
return { geo: this.st.geo, people: this.st.people, now: this.st.now, person: this.p.id };
|
||||
}
|
||||
|
||||
/** Every part; a `quiet` (periodic) repaint leaves alone the parts that have the keyboard focus. */
|
||||
paint(quiet = false) {
|
||||
repaint(this.parts.head, headPart(this), quiet);
|
||||
repaint(this.parts.traffic, trafficPart(this), quiet);
|
||||
repaint(this.parts.devices, devicesPart(this), quiet);
|
||||
repaint(this.parts.limits, limitsPart(this), quiet);
|
||||
this.paintPlaces(quiet);
|
||||
repaint(this.parts.activity, activityPart(this), quiet);
|
||||
}
|
||||
|
||||
paintPlaces(quiet = false) {
|
||||
const el = placesPart(this);
|
||||
// A new map card always goes in (the map instance is made with it).
|
||||
if (el) repaint(this.parts.places, el, quiet && !this.mapOpen);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
// Person page: Access & limits — what they reach, then the tiles (devices,
|
||||
// circle, end date, data, hours). Every choice applies at once.
|
||||
|
||||
import type { Person } from '../../../shared/types';
|
||||
import { E } from '../../e';
|
||||
import { notify } from '../../ui/toast';
|
||||
import { seg } from '../../ui/controls';
|
||||
import { attachTip } from '../../ui/tooltip';
|
||||
import { icon } from '../../../shared/icons';
|
||||
import { ACCESS_LABEL, bytes, date, daysLeft, shiftDay } from '../../../shared/format';
|
||||
import { resetUsageDialog } from '../../logs';
|
||||
import { homeAccess } from '../../access';
|
||||
import { endSettings } from './dialogs';
|
||||
import { circleTile, devTile, dnsPicker, netsPicker, servicesPicker } from './pickers';
|
||||
import type { PersonPage } from './index';
|
||||
|
||||
const ACCESS_TEXT: Record<Person['access'], string> = {
|
||||
internet: 'Browse the internet through your router. No access to devices at home, and home cannot reach theirs.',
|
||||
lan: 'Reach devices at home, and home reaches theirs. Internet stays on their own connection.',
|
||||
both: 'Home devices and the internet, all through your router. Home reaches their devices too.',
|
||||
};
|
||||
|
||||
const TIME = /^([01]\d|2[0-3]):[0-5]\d$/;
|
||||
|
||||
function endTile(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const today = page.today;
|
||||
const left = daysLeft(p.expires, today);
|
||||
const dateInput = E('input', {
|
||||
type: 'date',
|
||||
class: 'dl-date-hidden',
|
||||
value: p.expires,
|
||||
tabindex: '-1',
|
||||
'aria-hidden': 'true',
|
||||
}) as HTMLInputElement;
|
||||
dateInput.addEventListener(
|
||||
'change',
|
||||
() => dateInput.value && page.patch({ expires: dateInput.value }, `Access until ${date(dateInput.value)}`),
|
||||
);
|
||||
// Extend from the current end, or from today when there is none or it has passed.
|
||||
const extend = (step: 'week' | 'month' | 'year', n: number) => {
|
||||
const next = shiftDay(p.expires && (left ?? -1) >= 0 ? p.expires : '', step, n, today);
|
||||
void page.patch({ expires: next }, `Access until ${date(next)}`);
|
||||
};
|
||||
const ext = (
|
||||
[
|
||||
['week', 1, '+1 week'],
|
||||
['month', 1, '+1 month'],
|
||||
['month', 3, '+3 months'],
|
||||
['year', 1, '+1 year'],
|
||||
] as const
|
||||
).map(([step, n, label]) => E('button', { type: 'button', class: 'dl-chipbtn', click: () => extend(step, n) }, label));
|
||||
// The native date input stays hidden behind the chip and only opens its picker. It takes the
|
||||
// focus first, so the page's background refresh leaves this tile (and the open picker) alone.
|
||||
const pick = E('span', { class: 'dl-datechip' }, [
|
||||
E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: 'dl-chipbtn',
|
||||
click: () => {
|
||||
dateInput.focus({ preventScroll: true });
|
||||
try {
|
||||
dateInput.showPicker();
|
||||
} catch {
|
||||
/* focused is the best this browser offers */
|
||||
}
|
||||
},
|
||||
},
|
||||
[icon('calendar'), p.expires ? date(p.expires) : 'Date'],
|
||||
),
|
||||
dateInput,
|
||||
]);
|
||||
const action = { pause: 'pause access', notify: 'keep access', remove: 'remove their devices' }[p.expire_action];
|
||||
return E('div', { class: 'dl-tile2' }, [
|
||||
E('div', { class: 'dl-tile2-label' }, [
|
||||
E('span', {}, 'Access ends'),
|
||||
p.expires
|
||||
? E(
|
||||
'span',
|
||||
{ class: left != null && left < 0 ? 'dl-bad' : left != null && left <= p.expire_warn ? 'dl-warn' : 'dl-ok' },
|
||||
left != null && left < 0 ? 'ended' : `${left} day${left === 1 ? '' : 's'} left`,
|
||||
)
|
||||
: E('span', { class: 'dl-faint' }, 'no end date'),
|
||||
]),
|
||||
E('div', { class: 'dl-chips-row' }, [
|
||||
...ext,
|
||||
pick,
|
||||
p.expires
|
||||
? E('button', { type: 'button', class: 'dl-chipbtn', click: () => page.patch({ expires: '' }, 'No end date') }, 'No end')
|
||||
: '',
|
||||
]),
|
||||
E('div', { class: 'dl-tile2-foot' }, [
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-muted' },
|
||||
`When it ends: ${action}${p.expire_warn ? `, warn ${p.expire_warn} day${p.expire_warn === 1 ? '' : 's'} before` : ''}`,
|
||||
),
|
||||
E(
|
||||
'a',
|
||||
{
|
||||
href: '#',
|
||||
click: (ev: Event) => {
|
||||
ev.preventDefault();
|
||||
endSettings(page);
|
||||
},
|
||||
},
|
||||
'Change',
|
||||
),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
function dataTile(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const used = p.quota_used || p.month[0] + p.month[1];
|
||||
const pct = p.quota_limit ? Math.min(100, (p.quota_used / p.quota_limit) * 100) : 0;
|
||||
const bar = E('div', { class: `dl-meter${pct >= 100 ? ' bad' : pct >= 90 ? ' warn' : ''}` }, [
|
||||
E('i'),
|
||||
p.quota_limit ? E('span', { class: 'mark', title: 'Alert at 90%' }) : '',
|
||||
]);
|
||||
(bar.firstChild as HTMLElement).style.width = `${pct}%`;
|
||||
if (p.quota_limit)
|
||||
attachTip(bar, () => [
|
||||
E('div', { class: 'dl-tip-title' }, `${pct.toFixed(1)}% used`),
|
||||
E(
|
||||
'div',
|
||||
{ class: 'dl-muted' },
|
||||
`${bytes(p.quota_used)} of ${bytes(p.quota_limit)} · ${bytes(Math.max(0, p.quota_limit - p.quota_used))} left`,
|
||||
),
|
||||
E('div', { class: 'dl-faint' }, 'Alert at 90%'),
|
||||
]);
|
||||
const period = seg(
|
||||
[
|
||||
['month', 'Monthly'],
|
||||
['total', 'Total'],
|
||||
],
|
||||
p.quota_period,
|
||||
{ label: 'Data limit period', cls: 'sm', optimistic: false, onChange: (v) => page.patch({ quota_period: v }) },
|
||||
);
|
||||
const presets = [10, 50, 100].map((gb) =>
|
||||
E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: `dl-chipbtn${p.quota_gb === gb ? ' on' : ''}`,
|
||||
click: () => page.patch({ quota_gb: String(gb) }, `Data limit ${gb} GB`),
|
||||
},
|
||||
`${gb} GB`,
|
||||
),
|
||||
);
|
||||
presets.push(
|
||||
E(
|
||||
'button',
|
||||
{ type: 'button', class: `dl-chipbtn${!p.quota_gb ? ' on' : ''}`, click: () => page.patch({ quota_gb: '' }, 'No data limit') },
|
||||
'Unlimited',
|
||||
),
|
||||
);
|
||||
presets.push(
|
||||
E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: 'dl-chipbtn',
|
||||
click: () => {
|
||||
const v = prompt('Data limit in GB', p.quota_gb ? String(p.quota_gb) : '');
|
||||
if (v != null && v.trim() !== '' && Number(v) > 0) void page.patch({ quota_gb: v.trim() }, `Data limit ${v.trim()} GB`);
|
||||
},
|
||||
},
|
||||
'Custom',
|
||||
),
|
||||
);
|
||||
return E('div', { class: 'dl-tile2' }, [
|
||||
E('div', { class: 'dl-tile2-label' }, [E('span', {}, 'Data'), period]),
|
||||
E('div', { class: 'dl-tile2-row' }, [
|
||||
E('span', { class: 'dl-tile2-value' }, [
|
||||
bytes(used),
|
||||
p.quota_limit ? E('span', { class: 'dl-faint' }, ` of ${bytes(p.quota_limit)}`) : E('span', { class: 'dl-faint' }, ' · no limit'),
|
||||
]),
|
||||
p.quota_period === 'month' ? E('span', { class: 'dl-sub' }, 'resets on the 1st') : '',
|
||||
]),
|
||||
p.quota_limit ? bar : '',
|
||||
E('div', { class: 'dl-chips-row' }, presets),
|
||||
p.quota_gb && p.quota_period === 'month'
|
||||
? E('div', { class: 'dl-tile2-foot' }, [
|
||||
E('span', { class: 'dl-muted' }, p.bonus_gb ? `Includes +${p.bonus_gb} GB extra this month` : 'Need a little more this month?'),
|
||||
E(
|
||||
'a',
|
||||
{
|
||||
href: '#',
|
||||
click: (ev: Event) => {
|
||||
ev.preventDefault();
|
||||
void page.patch({ bonus_gb: String((p.bonus_gb || 0) + 10) }, '+10 GB this month');
|
||||
},
|
||||
},
|
||||
'+10 GB this month',
|
||||
),
|
||||
])
|
||||
: '',
|
||||
E('div', { class: 'dl-tile2-foot' }, [
|
||||
E('span', { class: 'dl-muted' }, `${bytes(p.total[0] + p.total[1])} in total`),
|
||||
E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: 'dl-linkbtn danger',
|
||||
click: () => resetUsageDialog('person', p.id, p.name, () => setTimeout(() => page.refresh(), 300)),
|
||||
},
|
||||
'Reset usage',
|
||||
),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allowed hours. The times save when the focus leaves them (or on Enter), not
|
||||
* on every change: a half-typed time would otherwise be saved and the tile
|
||||
* repainted under the cursor. Both are checked first.
|
||||
*/
|
||||
function hoursTile(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const hoursOn = !!p.hours;
|
||||
const [h1, h2] = (p.hours || '07:00-23:00').split('-');
|
||||
const from = E('input', { type: 'time', class: 'cbi-input-text', value: h1, 'aria-label': 'From' }) as HTMLInputElement;
|
||||
const to = E('input', { type: 'time', class: 'cbi-input-text', value: h2, 'aria-label': 'To' }) as HTMLInputElement;
|
||||
const times = E('div', { class: 'dl-tile2-row' }, [from, E('span', { class: 'dl-faint' }, 'to'), to]);
|
||||
const commit = () => {
|
||||
const v = `${from.value}-${to.value}`;
|
||||
if (v === p.hours) return;
|
||||
if (!TIME.test(from.value) || !TIME.test(to.value)) return notify('Enter both times, like 07:00 and 23:00.', 'warning');
|
||||
if (from.value === to.value) return notify('The start and the end must differ.', 'warning');
|
||||
void page.patch({ hours: v }, `Allowed ${from.value}–${to.value}`);
|
||||
};
|
||||
times.addEventListener('focusout', (ev: FocusEvent) => {
|
||||
if (!times.contains(ev.relatedTarget as Node | null)) commit();
|
||||
});
|
||||
for (const t of [from, to])
|
||||
t.addEventListener('keydown', (ev: KeyboardEvent) => {
|
||||
if (ev.key === 'Enter') {
|
||||
ev.preventDefault();
|
||||
commit();
|
||||
}
|
||||
});
|
||||
const sw = E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
role: 'switch',
|
||||
'aria-checked': hoursOn ? 'true' : 'false',
|
||||
class: `dl-switch${hoursOn ? ' on' : ''}`,
|
||||
'aria-label': 'Limit hours',
|
||||
click: () => page.patch({ hours: hoursOn ? '' : '07:00-23:00' }, hoursOn ? 'Any time' : 'Allowed 07:00–23:00'),
|
||||
},
|
||||
E('i'),
|
||||
);
|
||||
return E('div', { class: 'dl-tile2' }, [
|
||||
E('div', { class: 'dl-tile2-row' }, [
|
||||
E('div', { class: 'dl-stack' }, [
|
||||
E('span', { class: 'dl-tile2-label' }, 'Allowed hours'),
|
||||
E('span', {}, hoursOn ? `${h1} – ${h2} (router time)` : 'Any time'),
|
||||
]),
|
||||
sw,
|
||||
]),
|
||||
hoursOn ? times : '',
|
||||
]);
|
||||
}
|
||||
|
||||
export function limitsPart(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const access = seg(
|
||||
(['internet', 'lan', 'both'] as const).map((a) => [
|
||||
a,
|
||||
[icon(a === 'internet' ? 'globe' : a === 'lan' ? 'home' : 'shield'), a === 'internet' ? 'Internet' : a === 'lan' ? 'Home' : 'Both'],
|
||||
]),
|
||||
p.access,
|
||||
{ label: 'Access', cls: 'wide', optimistic: false, onChange: (a) => page.patch({ access: a }, `Access: ${ACCESS_LABEL[a]}`) },
|
||||
);
|
||||
// Heading above the card, like "Devices", so both columns line up (as on the portal).
|
||||
return E('section', { class: 'dl-devsec', 'aria-label': 'Access and limits' }, [
|
||||
E('div', { class: 'dl-panel-head' }, [E('h3', {}, 'Access & limits')]),
|
||||
E('div', { class: 'dl-panel' }, [
|
||||
E('div', { class: 'dl-stack gap8' }, [
|
||||
E('span', { class: 'dl-tile2-label' }, 'Can reach'),
|
||||
access,
|
||||
E('span', { class: 'dl-sub' }, ACCESS_TEXT[p.access]),
|
||||
p.access === 'lan' && page.st.services.length ? servicesPicker(page) : '',
|
||||
homeAccess(p) ? netsPicker(page) : '',
|
||||
p.access === 'lan' ? dnsPicker(page) : '',
|
||||
]),
|
||||
E('div', { class: 'dl-tiles2' }, [devTile(page), circleTile(page), endTile(page), dataTile(page), hoursTile(page)]),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
// Person page: the finer access choices (home services, home networks, DNS)
|
||||
// and the Devices and circle tiles.
|
||||
|
||||
import { E } from '../../e';
|
||||
import { notify } from '../../ui/toast';
|
||||
import { chipToggle, seg } from '../../ui/controls';
|
||||
import { dropdown } from '../../ui/menu';
|
||||
import { attachTip } from '../../ui/tooltip';
|
||||
import { icon } from '../../../shared/icons';
|
||||
import { ago, initials } from '../../../shared/format';
|
||||
import { circleDialog, circleDot, homeAccess } from '../../access';
|
||||
import type { PersonPage } from './index';
|
||||
|
||||
/**
|
||||
* DNS for Home-only access. The internet does not go through the tunnel, so
|
||||
* by default the device keeps its own DNS; home names are opt-in.
|
||||
*/
|
||||
export function dnsPicker(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const choice = seg(
|
||||
[
|
||||
['device', 'Device’s own DNS'],
|
||||
['home', 'Home names'],
|
||||
],
|
||||
p.dns_mode,
|
||||
{
|
||||
label: 'DNS',
|
||||
cls: 'sm',
|
||||
optimistic: false,
|
||||
onChange: (v) =>
|
||||
page.patch(
|
||||
{ dns_mode: v },
|
||||
v === 'home' ? 'Home names on — devices need their configuration again' : 'Device DNS — devices need their configuration again',
|
||||
),
|
||||
},
|
||||
);
|
||||
const fb = page.st.settings.home_dns_fallback;
|
||||
return E('div', { class: 'dl-stack gap4 dl-dnspick' }, [
|
||||
E('div', { class: 'dl-row-actions' }, [E('span', { class: 'dl-sub' }, 'DNS'), choice]),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
p.dns_mode === 'home'
|
||||
? [
|
||||
'Names like ',
|
||||
E('span', { class: 'dl-mono' }, `nas.${page.st.settings.domain}`),
|
||||
' work. Internet names are resolved at home, so they depend on the tunnel',
|
||||
fb
|
||||
? [' (', E('span', { class: 'dl-mono' }, fb), ' takes over when it is down).']
|
||||
: ' — with no fallback set, they stop when it is down.',
|
||||
]
|
||||
: 'Internet keeps working exactly as without the VPN. Reach home devices by IP address.',
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/** Home networks this person reaches, when the server has more than one. */
|
||||
export function netsPicker(page: PersonPage): HTMLElement | '' {
|
||||
const p = page.p;
|
||||
const srv = page.st.servers.find((x) => x.id === p.server);
|
||||
const all = srv?.home ?? [];
|
||||
if (all.length < 2) return '';
|
||||
const picked = new Set(p.home_nets?.length ? p.home_nets : all.map((h) => h.dev));
|
||||
return E('div', { class: 'dl-stack gap4' }, [
|
||||
E(
|
||||
'div',
|
||||
{ class: 'dl-chips-row' },
|
||||
all.map((h) =>
|
||||
chipToggle(
|
||||
h.dev,
|
||||
picked.has(h.dev),
|
||||
() => {
|
||||
const next = new Set(picked);
|
||||
if (!next.delete(h.dev)) next.add(h.dev);
|
||||
if (!next.size) return notify('Keep at least one network, or choose Internet only.', 'warning', 5000);
|
||||
const list = next.size === all.length ? [] : [...next];
|
||||
void page.patch(
|
||||
{ home_nets: list },
|
||||
list.length ? `${p.name} reaches ${list.join(', ')}` : `${p.name} reaches every home network`,
|
||||
);
|
||||
},
|
||||
{ title: h.nets.join(', ') },
|
||||
),
|
||||
),
|
||||
),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
p.home_nets?.length
|
||||
? `Home networks ${p.name} reaches. Networks added to the server later stay out.`
|
||||
: `Home networks ${p.name} reaches: all of them. Untick one to keep ${p.name} out of it.`,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/** Optional narrowing of home-network access to selected services; the request gets a copy of the choice. */
|
||||
export function servicesPicker(page: PersonPage): HTMLElement {
|
||||
const chosen = new Set(page.p.services ?? []);
|
||||
return E('div', { class: 'dl-stack gap4' }, [
|
||||
E(
|
||||
'div',
|
||||
{ class: 'dl-chips-row' },
|
||||
page.st.services.map((sv) =>
|
||||
chipToggle(
|
||||
sv.name,
|
||||
chosen.has(sv.id),
|
||||
() => {
|
||||
// A copy: the chips show the saved choice until the router has it (a failed save changes nothing).
|
||||
const next = new Set(chosen);
|
||||
if (!next.delete(sv.id)) next.add(sv.id);
|
||||
void page.patch({ services: [...next] }, next.size ? 'Home services updated' : 'Whole home network');
|
||||
},
|
||||
{ title: `${sv.address}${sv.ports.length ? ` · ${sv.ports.join(', ')}` : ''}` },
|
||||
),
|
||||
),
|
||||
),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
chosen.size ? 'Only the selected services at home.' : 'Whole home network. Pick services to narrow it down.',
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/** The circle this person's devices share (servers where devices reach each other in circles). */
|
||||
export function circleTile(page: PersonPage): HTMLElement | '' {
|
||||
const p = page.p;
|
||||
const st = page.st;
|
||||
const srv = st.servers.find((x) => x.id === p.server);
|
||||
if (srv?.mesh !== 'circles' || !homeAccess(p)) return '';
|
||||
const circles = st.circles.filter((c) => c.server === p.server);
|
||||
const ci = circles.find((c) => c.id === p.circle);
|
||||
const mates = ci ? st.people.filter((x) => x.id !== p.id && x.circle === ci.id && homeAccess(x)).map((x) => x.name) : [];
|
||||
const change: HTMLElement = E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: 'dl-chipbtn',
|
||||
click: () =>
|
||||
dropdown(change, [
|
||||
...circles.map((c) => ({
|
||||
label: c.name,
|
||||
icon: circleDot(c),
|
||||
run: () => c.id !== p.circle && page.patch({ circle: c.id }, `${p.name} is in ${c.name}`),
|
||||
})),
|
||||
p.circle ? { label: 'No circle', icon: icon('x'), run: () => page.patch({ circle: '' }, `${p.name} is in no circle`) } : null,
|
||||
{ label: 'New circle', icon: icon('plus'), run: () => circleDialog(st, p.server, null, () => page.refresh()) },
|
||||
]),
|
||||
},
|
||||
'Change',
|
||||
);
|
||||
// As the other tiles: the name is the value; who is in it as avatars.
|
||||
const people = ci ? st.people.filter((x) => x.circle === ci.id && homeAccess(x)) : [p];
|
||||
return E('div', { class: 'dl-tile2' }, [
|
||||
E('div', { class: 'dl-tile2-label' }, [E('span', {}, 'Devices see each other'), change]),
|
||||
E('div', { class: 'dl-tile2-row' }, [
|
||||
E('span', { class: 'dl-tile2-value' }, ci ? ci.name : 'No circle'),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-avstack', title: people.map((x) => x.name).join(', ') },
|
||||
people.slice(0, 5).map((x) => E('span', { class: 'dl-avatar sm' }, initials(x.name))),
|
||||
),
|
||||
]),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
!ci ? `Only ${p.name}'s own devices` : mates.length ? [p.name, ...mates].join(', ') : `Nobody else in ${ci.name} yet`,
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/** Device limit with a slot per device; never below the devices in use. */
|
||||
export function devTile(page: PersonPage): HTMLElement {
|
||||
const p = page.p;
|
||||
const now = page.st.now;
|
||||
const slots = E('div', { class: 'dl-slots' });
|
||||
// Devices added over the limit get their own soft orange slots after the allowed ones.
|
||||
for (let i = 0; i < Math.max(p.device_limit, p.devices.length); i++) {
|
||||
const d = p.devices[i];
|
||||
const over = i >= p.device_limit;
|
||||
const slot = E('i', { class: over ? 'over' : d ? 'used' : '' });
|
||||
attachTip(slot, () => [
|
||||
E('div', { class: 'dl-tip-title' }, d ? `${d.name}${over ? ' · over the limit' : ''}` : 'Free slot'),
|
||||
E(
|
||||
'div',
|
||||
{ class: 'dl-muted' },
|
||||
d ? (d.online ? 'Connected' : d.handshake ? `Last seen ${ago(d.handshake, now)}` : 'Not set up yet') : 'They can add a device here',
|
||||
),
|
||||
]);
|
||||
slots.appendChild(slot);
|
||||
}
|
||||
const floor = Math.max(1, p.devices.length);
|
||||
const minus = E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
'aria-label': 'Allow fewer devices',
|
||||
disabled: p.device_limit <= floor ? 'disabled' : null,
|
||||
click: () => page.patch({ device_limit: String(p.device_limit - 1) }),
|
||||
},
|
||||
'−',
|
||||
);
|
||||
if (p.device_limit <= floor && p.devices.length > 1)
|
||||
attachTip(minus, () => [
|
||||
E('div', { class: 'dl-tip-title' }, `${p.devices.length} devices in use`),
|
||||
E('div', { class: 'dl-muted' }, 'Remove a device first to lower the limit'),
|
||||
]);
|
||||
return E('div', { class: 'dl-tile2' }, [
|
||||
E('div', { class: 'dl-tile2-label' }, 'Devices'),
|
||||
E('div', { class: 'dl-tile2-row' }, [
|
||||
E('span', { class: 'dl-tile2-value' }, [String(p.devices.length), E('span', { class: 'dl-faint' }, ` / ${p.device_limit}`)]),
|
||||
E('div', { class: 'dl-stepper' }, [
|
||||
minus,
|
||||
E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
'aria-label': 'Allow more devices',
|
||||
disabled: p.device_limit >= 32 ? 'disabled' : null,
|
||||
click: () => page.patch({ device_limit: String(p.device_limit + 1) }),
|
||||
},
|
||||
'+',
|
||||
),
|
||||
]),
|
||||
]),
|
||||
slots,
|
||||
]);
|
||||
}
|
||||
Reference in new issue
Block a user