network-monitor/app/static/js/views/domains.js
neil 54a765510a Network monitor: hub, probes, Docker and native installers
Hub (FastAPI + no-build web UI):
- Network health: live probe/target matrix, status strips, incidents
- Probes: add (with install command), rename, remove, latency and throughput charts
- Clients merged from AdGuard, DHCP, query log and probe ARP tables; names,
  categories, suspicious flag, notes, per-client DNS requests
- Query log, domain detail (clients, related subdomains), blocked domains via
  the AdGuard Home /control API

Probe: tests peers (ping, HTTP, throughput), gateway, LAN and public DNS,
internet URLs and an optional VPS; buffers results while the hub is down.
Gateway and ARP from /proc/net on Linux, route/arp on macOS.

Install: install.sh (hub) and install-probe.sh (generated from it), each in
docker mode (compose; default on Linux hosts) or native mode (venv + systemd
or launchd; default on macOS and in LXC). Both modes deploy with an update
command that checks the new commit and rolls back on failure.

CLAUDE.md carries shared context for Claude Code sessions on each machine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 18:10:29 -08:00

107 lines
6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Domains: the most requested and most blocked, and one domain's detail.
import { el } from '../ui.js';
import { get } from '../api.js';
import { seriesLegend, stackedBars, hideTip } from '../charts.js';
import { ago, hm, ms, num, pct, when } from '../fmt.js';
import { adguardProblem, clientLink, domainLink, resultChip, screen, section, table, fill } from '../common.js';
export function mount(root, ctx) {
return ctx.rest ? detail(root, ctx, decodeURIComponent(ctx.rest)) : overview(root, ctx);
}
function openBox(ctx) {
const input = el('input.input', { type: 'search', placeholder: 'example.com', 'aria-label': 'Domain to open' });
const form = el('form', { style: 'display:contents', onsubmit: (e) => {
e.preventDefault();
const d = input.value.trim().toLowerCase().replace(/^https?:\/\//, '').split('/')[0];
if (d) ctx.navigate(`/domains/${encodeURIComponent(d)}`);
} }, input, el('button.btn', { type: 'submit' }, 'Open domain'));
return form;
}
function overview(root, ctx) {
const ui = screen(root, [openBox(ctx), el('span.spacer'), el('button.btn', { type: 'button', onclick: () => load() }, 'Refresh')]);
async function load() {
try {
const d = await get('/api/domains');
const list = (rows) => table(['Domain', { text: 'Requests', props: { class: 'num' } }],
rows.map((r) => el('tr', {}, el('td', {}, domainLink(r.domain)), el('td.num', {}, num(r.count)))), { empty: 'Nothing yet.' });
fill(ui.body, el('div.cols', {},
el('div', {}, section('Most requested'), list(d.top_queried)),
el('div', {}, section('Most blocked'), list(d.top_blocked))));
ui.setStatus(`${num(d.num_queries)} queries · ${num(d.num_blocked)} blocked in AdGuard’s stats period`);
} catch (err) {
fill(ui.body, adguardProblem(err));
ui.setStatus(err.message, 'error');
}
}
load();
}
function detail(root, ctx, name) {
ctx.setTitle(name);
const ui = screen(root, [
openBox(ctx),
el('a.btn', { href: `/queries?domain=${encodeURIComponent(name)}` }, 'Open in query log'),
el('span.spacer'),
el('button.btn', { type: 'button', onclick: () => load() }, 'Refresh'),
]);
async function load() {
ui.body.classList.add('stale');
ui.setStatus('Reading the query log…');
try {
const d = await get(`/api/domains/${encodeURIComponent(name)}`);
render(d);
ui.setStatus(`Based on ${num(d.sample_rows)} queries for *.${d.base}${d.since ? ` since ${when(d.since)}` : ''}${d.complete ? ' (the whole AdGuard log)' : ''}`);
} catch (err) {
fill(ui.body, adguardProblem(err));
ui.setStatus(err.message, 'error');
} finally {
ui.body.classList.remove('stale');
}
}
function render(d) {
const now = Date.now() / 1000;
const hourLabel = (i, full) => {
const t = now - (23 - i) * 3600;
return full ? `${hm(t - 3600)} – ${hm(t)}` : i === 23 ? 'now' : `${23 - i} h ago`;
};
fill(ui.body,
el('div.title-row', {}, el('h2', {}, d.domain),
d.blocked ? el('span.chip.danger', {}, d.blocked === d.count ? 'Always blocked' : 'Sometimes blocked') : d.count ? el('span.chip.ok', {}, 'Allowed') : null,
d.domain !== d.base ? el('a.chip', { href: `/domains/${encodeURIComponent(d.base)}` }, `part of ${d.base}`) : null),
el('div.facts', {},
fact('Requests', num(d.count), 'in the sample below'),
fact('Blocked', d.count ? `${num(d.blocked)} (${pct(d.blocked / d.count)})` : '—'),
fact('Clients', num(d.clients.length)),
fact('Avg response', ms(d.avg_ms)),
fact('Last request', d.last ? ago(d.last) : '—'),
d.stats_count != null ? fact('AdGuard stats', num(d.stats_count), 'top-domain count for its stats period') : null),
d.count ? el('div', {},
section('Last 24 hours', 'Requests per hour'),
seriesLegend([{ name: 'Allowed', color: 'var(--viz-1)' }, { name: 'Blocked', color: 'var(--viz-2)' }], 'box'),
stackedBars({ n: 24, xLabel: hourLabel, height: 150, label: `Requests per hour for ${d.domain}`,
series: [{ name: 'Allowed', color: 'var(--viz-1)', values: d.hours.map((h, i) => h - d.blocked_hours[i]) },
{ name: 'Blocked', color: 'var(--viz-2)', values: d.blocked_hours }] })) : el('div.callout', { style: 'margin-top:12px' }, `No requests for ${d.domain} in the sample. Related domains under ${d.base} are below.`),
el('div.cols', {},
el('div', {}, section('Clients requesting it'),
table(['Client', { text: 'Requests', props: { class: 'num' } }, { text: 'Blocked', props: { class: 'num' } }, 'Last'],
d.clients.map((c) => el(`tr${c.device?.suspicious ? '.flagged' : ''}`, {},
el('td', {}, clientLink(c.ip, c.device, c.name)), el('td.num', {}, num(c.count)), el('td.num', {}, c.blocked ? num(c.blocked) : ''),
el('td.nowrap', {}, ago(c.last)))), { empty: 'No clients in the sample.' })),
el('div', {}, section(`Other names under ${d.base}`, 'Same domain, different subdomains'),
table(['Domain', { text: 'Requests', props: { class: 'num' } }, { text: 'Blocked', props: { class: 'num' } }],
d.related.map((r) => el('tr', {}, el('td', {}, domainLink(r.domain)), el('td.num', {}, num(r.count)), el('td.num', {}, r.blocked ? num(r.blocked) : ''))),
{ empty: 'No other names under this domain in the sample.' }))),
d.rules.length ? el('div', {}, section('Matching rules'), el('ul.mono', {}, ...d.rules.map(([r, n]) => el('li', {}, `${r} ×${n}`)))) : null,
section('Recent requests'),
table(['Time', 'Client', 'Type', 'Result', { text: 'Response', props: { class: 'num' } }],
d.recent.map((q) => el('tr', {}, el('td.nowrap', {}, when(q.ts)), el('td', {}, clientLink(q.client, q.device, q.client_name)),
el('td.mono', {}, q.type), el('td', {}, resultChip(q)), el('td.num', {}, ms(q.elapsed_ms)))), { empty: 'None in the sample.' }),
);
}
const fact = (label, value, title) => el('div.fact', { title: title || '' }, el('span.label', {}, label), el('strong', {}, value));
load();
return hideTip;
}