- Access split into Internet and Home network cards; network tiles inline, Auto-detect LAN beside them, other interfaces folded by kind - Devices reach each other: Off, Everyone or Circles (per server); circles with names, colours and members; a person's own devices always see each other while it is not off - Per-person home networks: people can be kept out of some of the server's networks (nft lan_part set, AllowedIPs follow) - Home services moved from Settings to Access, added from the devices the router sees over DHCP (ports they announce over Bonjour when umdns runs) - Who reaches what (rules summed up per access) and Check access (walks the same rules as nft and the client configuration, step by step) - Find home devices (Bonjour): umdns services published as DNS-SD records in dnsmasq; installs umdns from Access - Connections from home show the DHCP name with the address in brackets - Repaints keep the scroll position
This commit is contained in:
1 parent
82ab86848e
commit
aa8256bc60
26 files changed
+1661
-289
No files matched your search
@@ -0,0 +1,441 @@
|
||||
// Server → Access pieces: home services (picked from devices at home), circles,
|
||||
// "Who reaches what" and "Check access".
|
||||
|
||||
import type { Circle, Person, Server, Status } from '../shared/types';
|
||||
import { E } from './e';
|
||||
import { api, type HomeHost } from './api';
|
||||
import { attempt, button, closeModal, confirmDialog, modal } from './ui';
|
||||
import { input } from './kit';
|
||||
import { icon } from '../shared/icons';
|
||||
|
||||
type Service = Status['services'][number];
|
||||
|
||||
/** Circle colours; the circle stores an index. */
|
||||
export const CIRCLE_COLORS = ['#3a83f7', '#a78bfa', '#f472b6', '#f59e0b', '#10b981', '#22d3ee', '#ef4444', '#84cc16'];
|
||||
|
||||
export const circleDot = (c: Circle | undefined) =>
|
||||
E('i', { class: 'dl-circle-dot', style: `background:${CIRCLE_COLORS[c?.color ?? 0] ?? CIRCLE_COLORS[0]}` });
|
||||
|
||||
/** People whose devices can be reached at home (the mesh and home devices). */
|
||||
export const homeAccess = (p: Person) => p.access === 'both' || (p.access === 'lan' && !p.services?.length);
|
||||
|
||||
/* ---------- Home services ---------- */
|
||||
|
||||
export function serviceDialog(sv: Service | null, prefill: Partial<Service>, onSaved: () => void) {
|
||||
const name = input(sv?.name ?? prefill.name ?? '', { placeholder: 'NAS' });
|
||||
const addr = input(sv?.address ?? prefill.address ?? '', { placeholder: '192.168.1.10', class: 'cbi-input-text dl-mono' });
|
||||
const ports = input((sv?.ports ?? prefill.ports ?? []).join(', '), {
|
||||
placeholder: 'All ports, or e.g. 445, 5000',
|
||||
class: 'cbi-input-text dl-mono',
|
||||
});
|
||||
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)]);
|
||||
modal(
|
||||
sv ? `Edit ${sv.name}` : 'Add service',
|
||||
[field('Name', name), field('Address', addr), field('Ports', ports)],
|
||||
[
|
||||
E('button', { class: 'cbi-button cbi-button-neutral', click: closeModal }, 'Cancel'),
|
||||
' ',
|
||||
button(
|
||||
'Save',
|
||||
async () => {
|
||||
const list = ports.value.split(/[ ,]+/).filter(Boolean);
|
||||
if (await attempt(() => api.serviceSet(sv?.id ?? '', name.value, addr.value, list), 'Saved')) {
|
||||
closeModal();
|
||||
onSaved();
|
||||
}
|
||||
},
|
||||
'positive',
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/** Port groups a device at home offers: what it announces over Bonjour, or the usual ones. */
|
||||
const MDNS_LABEL: Record<string, string> = {
|
||||
_smb: 'SMB',
|
||||
_afpovertcp: 'AFP',
|
||||
_http: 'Web',
|
||||
_https: 'Web',
|
||||
_ssh: 'SSH',
|
||||
_sftp: 'SFTP',
|
||||
'_sftp-ssh': 'SFTP',
|
||||
_ipp: 'Printer',
|
||||
_ipps: 'Printer',
|
||||
_printer: 'Printer',
|
||||
'_pdl-datastream': 'Printer',
|
||||
_airplay: 'AirPlay',
|
||||
_raop: 'AirPlay',
|
||||
_rfb: 'Screen sharing',
|
||||
_googlecast: 'Cast',
|
||||
_hap: 'HomeKit',
|
||||
_rdp: 'Remote desktop',
|
||||
_ftp: 'FTP',
|
||||
};
|
||||
const DEFAULT_PORTS: [string, string[]][] = [
|
||||
['Web', ['80', '443']],
|
||||
['SMB', ['445']],
|
||||
['SSH', ['22']],
|
||||
];
|
||||
|
||||
function portGroups(h: HomeHost): [string, string[]][] {
|
||||
const by = new Map<string, Set<string>>();
|
||||
for (const s of h.services) {
|
||||
const label = MDNS_LABEL[s.type.split('.')[0]];
|
||||
if (!label || !s.port) continue;
|
||||
(by.get(label) ?? by.set(label, new Set()).get(label)!).add(String(s.port));
|
||||
}
|
||||
return by.size ? [...by].map(([l, p]) => [l, [...p].sort((a, b) => +a - +b)]) : DEFAULT_PORTS;
|
||||
}
|
||||
|
||||
/** Services shared by all servers, and the devices at home to add them from. */
|
||||
export function homeServices(st: Status, serverId: string, reload: () => void): HTMLElement {
|
||||
const services = st.services;
|
||||
const picker = E('div', { class: 'dl-stack gap8' }, E('span', { class: 'dl-sub' }, 'Looking for devices at home…'));
|
||||
|
||||
const rows = services.length
|
||||
? services.map((sv) =>
|
||||
E('div', { class: 'dl-row' }, [
|
||||
icon('home'),
|
||||
E('span', { class: 'dl-row-main dl-stack' }, [
|
||||
E('b', {}, sv.name),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-mono dl-faint' },
|
||||
`${sv.address}${sv.ports.length ? ` · ports ${sv.ports.join(', ')}` : ' · all ports'}`,
|
||||
),
|
||||
]),
|
||||
button('Edit', () => serviceDialog(sv, {}, reload)),
|
||||
button(
|
||||
[icon('trash')],
|
||||
async () => {
|
||||
if (
|
||||
(await confirmDialog(`Remove ${sv.name}?`, 'People limited to this service lose access to it.', 'Remove', true)) &&
|
||||
(await attempt(() => api.serviceDelete(sv.id), 'Removed'))
|
||||
)
|
||||
reload();
|
||||
},
|
||||
'neutral',
|
||||
{ title: 'Remove', 'aria-label': `Remove ${sv.name}` },
|
||||
),
|
||||
]),
|
||||
)
|
||||
: [E('div', { class: 'dl-row dl-muted' }, 'No services yet: pick ports of a device below.')];
|
||||
|
||||
/** Ports chosen for a device: add, change or remove its service at once. */
|
||||
async function choose(h: HomeHost, ports: string[] | null) {
|
||||
const sv = services.find((s) => s.address === h.address);
|
||||
const ok =
|
||||
ports === null
|
||||
? sv && (await attempt(() => api.serviceDelete(sv.id), `${sv.name} removed`))
|
||||
: await attempt(
|
||||
() => api.serviceSet(sv?.id ?? '', sv?.name ?? (h.name || h.address), h.address, ports),
|
||||
sv ? `${sv.name} updated` : `${h.name || h.address} added`,
|
||||
);
|
||||
if (ok) reload();
|
||||
}
|
||||
|
||||
function hostRow(h: HomeHost): HTMLElement {
|
||||
const sv = services.find((s) => s.address === h.address);
|
||||
const all = !!sv && !sv.ports.length;
|
||||
const chosen = new Set(sv?.ports ?? []);
|
||||
const chip = (label: string, on: boolean, click: () => void) =>
|
||||
E('button', { type: 'button', class: `dl-chipbtn${on ? ' on' : ''}`, 'aria-pressed': on ? 'true' : 'false', click }, label);
|
||||
return E('div', { class: 'dl-row' }, [
|
||||
icon(h.services.some((s) => s.type.startsWith('_ipp') || s.type.startsWith('_printer')) ? 'file' : 'computer'),
|
||||
E('span', { class: 'dl-row-main dl-stack' }, [E('b', {}, h.name || h.address), E('span', { class: 'dl-mono dl-faint' }, h.address)]),
|
||||
E('div', { class: 'dl-chips-row dl-host-ports' }, [
|
||||
chip('All ports', all, () => choose(h, all ? null : [])),
|
||||
...portGroups(h).map(([label, ports]) => {
|
||||
const on = !all && ports.every((p) => chosen.has(p));
|
||||
return chip(`${label} ${ports.join(', ')}`, on, () => {
|
||||
const next = new Set(all ? [] : chosen);
|
||||
for (const p of ports) {
|
||||
if (on) next.delete(p);
|
||||
else next.add(p);
|
||||
}
|
||||
choose(h, next.size ? [...next] : null);
|
||||
});
|
||||
}),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
api
|
||||
.homeHosts(serverId)
|
||||
.then((r) => {
|
||||
const open = r.hosts.some((h) => services.some((s) => s.address === h.address)) && !services.length;
|
||||
dom.content(
|
||||
picker,
|
||||
r.hosts.length
|
||||
? E('details', { class: 'dl-params', open: open ? '' : null }, [
|
||||
E('summary', {}, [
|
||||
icon('chevron'),
|
||||
E('span', {}, 'Add from devices at home'),
|
||||
E('span', { class: 'dl-pcount' }, String(r.hosts.length)),
|
||||
]),
|
||||
E('div', { class: 'dl-params-body' }, [
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
r.mdns
|
||||
? 'Ports they announce over Bonjour. Pick what to open, or all ports.'
|
||||
: 'Devices that got an address from this router. Pick the ports to open, or all ports.',
|
||||
),
|
||||
E('div', { class: 'dl-list' }, r.hosts.map(hostRow)),
|
||||
]),
|
||||
])
|
||||
: E('span', { class: 'dl-sub' }, 'No devices found in the home network right now.'),
|
||||
);
|
||||
})
|
||||
.catch(() => dom.content(picker, E('span', { class: 'dl-sub' }, "Couldn't read the devices at home.")));
|
||||
|
||||
return E('div', { class: 'dl-stack gap8' }, [
|
||||
E('div', { class: 'dl-iflead' }, [
|
||||
E('div', { class: 'dl-iflead-row' }, [
|
||||
E('b', {}, 'Home services'),
|
||||
button([icon('plus'), ' Add service'], () => serviceDialog(null, {}, reload)),
|
||||
]),
|
||||
E(
|
||||
'span',
|
||||
{ class: 'dl-sub' },
|
||||
'Home-only people can be limited to these instead of whole networks (chosen on their page). Shared by all servers.',
|
||||
),
|
||||
]),
|
||||
E('div', { class: 'dl-list' }, rows),
|
||||
picker,
|
||||
]);
|
||||
}
|
||||
|
||||
/* ---------- Circles ---------- */
|
||||
|
||||
export function circleDialog(st: Status, serverId: string, ci: Circle | null, onDone: () => void) {
|
||||
const used = new Set(st.circles.filter((c) => c.server === serverId).map((c) => c.color));
|
||||
let color = ci?.color ?? CIRCLE_COLORS.findIndex((_, i) => !used.has(i));
|
||||
if (color < 0) color = 0;
|
||||
const name = input(ci?.name ?? '', { placeholder: 'Family' });
|
||||
const people = st.people.filter((p) => p.server === serverId).sort((a, b) => a.name.localeCompare(b.name));
|
||||
const members = new Set(people.filter((p) => ci && p.circle === ci.id).map((p) => p.id));
|
||||
|
||||
const swatches = E('div', { class: 'dl-swatches', role: 'radiogroup', 'aria-label': 'Colour' });
|
||||
const paintSwatches = () =>
|
||||
dom.content(
|
||||
swatches,
|
||||
CIRCLE_COLORS.map((c, i) =>
|
||||
E('button', {
|
||||
type: 'button',
|
||||
role: 'radio',
|
||||
class: `dl-swatch${i === color ? ' on' : ''}`,
|
||||
style: `--sw:${c}`,
|
||||
'aria-checked': i === color ? 'true' : 'false',
|
||||
'aria-label': `Colour ${i + 1}`,
|
||||
click: () => {
|
||||
color = i;
|
||||
paintSwatches();
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
paintSwatches();
|
||||
|
||||
const chips = E(
|
||||
'div',
|
||||
{ class: 'dl-chips-row' },
|
||||
people.map((p) => {
|
||||
const other = p.circle && p.circle !== ci?.id ? st.circles.find((c) => c.id === p.circle) : undefined;
|
||||
return E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: `dl-chipbtn${members.has(p.id) ? ' on' : ''}`,
|
||||
title: !homeAccess(p)
|
||||
? `${p.name} has no home access: their devices aren't reachable anyway`
|
||||
: other
|
||||
? `Now in ${other.name}`
|
||||
: '',
|
||||
click: (ev: Event) => {
|
||||
if (!members.delete(p.id)) members.add(p.id);
|
||||
(ev.currentTarget as HTMLElement).classList.toggle('on', members.has(p.id));
|
||||
},
|
||||
},
|
||||
[p.name, !homeAccess(p) ? E('b', {}, 'internet') : other ? E('b', {}, other.name) : ''],
|
||||
);
|
||||
}),
|
||||
);
|
||||
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)]);
|
||||
|
||||
modal(
|
||||
ci ? `Edit ${ci.name}` : 'New circle',
|
||||
[
|
||||
field('Name', name),
|
||||
field('Colour', swatches),
|
||||
field('People', chips),
|
||||
E('p', { class: 'dl-sub' }, 'Devices of people in one circle reach each other. A person is in one circle at a time.'),
|
||||
],
|
||||
[
|
||||
ci
|
||||
? button(
|
||||
'Delete',
|
||||
async () => {
|
||||
if (await attempt(() => api.circleDelete(ci.id), `${ci.name} deleted`)) {
|
||||
closeModal();
|
||||
onDone();
|
||||
}
|
||||
},
|
||||
'negative',
|
||||
)
|
||||
: '',
|
||||
E('span', { class: 'dl-spacer' }),
|
||||
E('button', { class: 'cbi-button cbi-button-neutral', click: closeModal }, 'Cancel'),
|
||||
' ',
|
||||
button(
|
||||
'Save',
|
||||
async () => {
|
||||
if (await attempt(() => api.circleSet(ci?.id ?? '', serverId, name.value, color, [...members]), 'Saved')) {
|
||||
closeModal();
|
||||
onDone();
|
||||
}
|
||||
},
|
||||
'positive',
|
||||
),
|
||||
],
|
||||
'dl-circle-modal',
|
||||
);
|
||||
}
|
||||
|
||||
/** The server's circles as chips (click to edit) and a round "+" to add one. */
|
||||
export function circleChips(st: Status, serverId: string, onDone: () => void): HTMLElement {
|
||||
const list = st.circles.filter((c) => c.server === serverId);
|
||||
return E('div', { class: 'dl-chips-row dl-circles' }, [
|
||||
...list.map((c) => {
|
||||
const who = st.people.filter((p) => p.circle === c.id).map((p) => p.name);
|
||||
return E(
|
||||
'button',
|
||||
{ type: 'button', class: 'dl-chip', title: `Edit ${c.name}`, click: () => circleDialog(st, serverId, c, onDone) },
|
||||
[circleDot(c), E('b', {}, c.name), E('span', { class: 'dl-faint' }, who.length ? who.join(', ') : 'nobody yet')],
|
||||
);
|
||||
}),
|
||||
E(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
class: 'dl-chip add',
|
||||
title: 'New circle',
|
||||
'aria-label': 'New circle',
|
||||
click: () => circleDialog(st, serverId, null, onDone),
|
||||
},
|
||||
icon('plus'),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/* ---------- Who reaches what ---------- */
|
||||
|
||||
export function whoReaches(st: Status, srv: Server): HTMLElement {
|
||||
const people = st.people.filter((p) => p.server === srv.id);
|
||||
const yes = (t = '') => E('span', { class: 'dl-yes' }, [icon('check'), t]);
|
||||
const no = () => E('span', { class: 'dl-faint' }, '—');
|
||||
const home = (list: Person[]) => {
|
||||
const kinds = new Set(list.map((p) => (p.access === 'lan' && p.services?.length ? 'services' : p.home_nets?.length ? 'some' : 'all')));
|
||||
if (!kinds.size) kinds.add('all');
|
||||
return yes([...kinds].map((k) => ({ all: 'all networks', some: 'their networks', services: 'services' })[k]).join(' / '));
|
||||
};
|
||||
const each = () => (srv.mesh === 'off' ? no() : srv.mesh === 'circles' ? yes('circle') : yes());
|
||||
const net = srv.lan_like ? 'like home' : 'direct';
|
||||
const row = (label: string, list: Person[], cells: Node[]) =>
|
||||
E('tr', {}, [
|
||||
E('td', {}, [E('b', {}, label), E('div', { class: 'dl-sub' }, `${list.length} ${list.length === 1 ? 'person' : 'people'}`)]),
|
||||
...cells.map((c) => E('td', {}, c)),
|
||||
]);
|
||||
const by = (a: string) => people.filter((p) => p.access === a);
|
||||
return E('section', { class: 'dl-setsec' }, [
|
||||
E('div', { class: 'dl-setsec-body' }, [
|
||||
E('div', { class: 'dl-panel-head' }, [
|
||||
E('h3', {}, 'Who reaches what'),
|
||||
E('span', { class: 'dl-spacer' }),
|
||||
E('span', { class: 'dl-sub' }, 'From the rules on this server'),
|
||||
]),
|
||||
E(
|
||||
'div',
|
||||
{ class: 'dl-reach-wrap' },
|
||||
E('table', { class: 'dl-table dl-reach' }, [
|
||||
E(
|
||||
'tr',
|
||||
{},
|
||||
['', 'Internet', 'Home', 'This router', 'Each other'].map((t) => E('th', {}, t)),
|
||||
),
|
||||
row('Internet', by('internet'), [yes(net), no(), E('span', { class: 'dl-faint' }, 'DNS'), no()]),
|
||||
row('Home', by('lan'), [E('span', { class: 'dl-faint' }, 'their own'), home(by('lan')), yes(), each()]),
|
||||
row('Both', by('both'), [yes(net), home(by('both')), yes(), each()]),
|
||||
]),
|
||||
),
|
||||
E(
|
||||
'p',
|
||||
{ class: 'dl-sub' },
|
||||
'Home devices reach the devices of people with home access, on the networks those people reach. Paused, ended and over-the-limit people reach nothing.',
|
||||
),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
/* ---------- Check access ---------- */
|
||||
|
||||
export function checkAccess(st: Status, srv: Server): HTMLElement {
|
||||
const devs = st.people
|
||||
.filter((p) => p.server === srv.id)
|
||||
.flatMap((p) => p.devices.map((d) => ({ id: d.id, label: `${p.name} · ${d.name}` })))
|
||||
.sort((a, b) => a.label.localeCompare(b.label));
|
||||
const ADDR = '@addr';
|
||||
/** A device, or an address typed in. */
|
||||
const end = (placeholder: string, initial: string) => {
|
||||
const sel = E('select', { class: 'cbi-input-select', 'aria-label': placeholder }, [
|
||||
...devs.map((d) => E('option', { value: d.id, selected: d.id === initial ? 'selected' : null }, d.label)),
|
||||
E('option', { value: ADDR, selected: initial === ADDR ? 'selected' : null }, 'Address'),
|
||||
]) as HTMLSelectElement;
|
||||
const addr = input('', { placeholder, class: 'cbi-input-text dl-mono' });
|
||||
const wrap = E('div', { class: 'dl-check-end' }, [sel, addr]);
|
||||
const sync = () => (addr.hidden = sel.value !== ADDR);
|
||||
sel.addEventListener('change', sync);
|
||||
sync();
|
||||
return { el: wrap, value: () => (sel.value === ADDR ? addr.value.trim() : sel.value) };
|
||||
};
|
||||
const from = end('10.0.1.11', devs[0]?.id ?? ADDR);
|
||||
const to = end('10.0.1.20:445', ADDR);
|
||||
const out = E('div', { class: 'dl-stack gap8' });
|
||||
const run = async () => {
|
||||
const r = await attempt(() => api.accessCheck(from.value(), to.value()));
|
||||
if (!r) return;
|
||||
const stop = r.steps.findIndex((s) => !s.ok);
|
||||
dom.content(out, [
|
||||
E('div', { class: `dl-verdict ${r.ok ? 'ok' : 'bad'}` }, [
|
||||
E('b', {}, r.ok ? 'Allowed' : 'Blocked'),
|
||||
E('span', { class: 'dl-sub' }, r.ok ? 'Every step lets it through' : `Stops at step ${stop + 1} of ${r.steps.length}`),
|
||||
]),
|
||||
E(
|
||||
'ul',
|
||||
{ class: 'dl-checks' },
|
||||
r.steps.map((s) =>
|
||||
E('li', {}, [
|
||||
E('span', { class: `dl-check-icon ${s.ok ? 'ok' : 'error'}` }, icon(s.ok ? 'check' : 'x')),
|
||||
E('div', { class: 'dl-check-body' }, [
|
||||
E('div', { class: 'dl-check-title' }, s.title),
|
||||
s.detail ? E('div', { class: 'dl-check-detail' }, s.detail) : '',
|
||||
]),
|
||||
]),
|
||||
),
|
||||
),
|
||||
]);
|
||||
};
|
||||
return E('section', { class: 'dl-setsec' }, [
|
||||
E('div', { class: 'dl-setsec-body' }, [
|
||||
E('div', { class: 'dl-panel-head' }, [
|
||||
E('h3', {}, 'Check access'),
|
||||
E('span', { class: 'dl-spacer' }),
|
||||
E('span', { class: 'dl-sub' }, 'Why something does or does not connect'),
|
||||
]),
|
||||
E('div', { class: 'dl-check-form' }, [from.el, E('span', { class: 'dl-faint' }, '→'), to.el, button('Check', run)]),
|
||||
out,
|
||||
]),
|
||||
]);
|
||||
}
|
||||
Reference in new issue
Block a user