// The DNS query log from AdGuard Home, with search, result filter and client filter. import { debounce, el, segmented } from '../ui.js'; import { get } from '../api.js'; import { ms, when } from '../fmt.js'; import { adguardProblem, clientLink, domainLink, resultChip, screen, fill } from '../common.js'; export function mount(root, ctx) { const params = ctx.query; let status = params.get('status') || 'all'; const client = params.get('client') || ''; const ip = params.get('ip') || ''; const domain = params.get('domain') || ''; const search = el('input.input', { type: 'search', value: params.get('q') || '', placeholder: 'Domain or client', 'aria-label': 'Search queries' }); const seg = segmented([['all', 'All'], ['blocked', 'Blocked'], ['allowed', 'Allowed']], status, (v) => { status = v; sync(); load(true); }, 'Result'); const filterChip = client || ip || domain ? el('span.chip.accent', {}, `${client ? 'Client' : ip ? 'IP' : 'Domain'}: ${client || ip || domain} `, el('a', { href: '/queries', 'aria-label': 'Clear filter', style: 'color:inherit;text-decoration:none' }, '✕')) : null; const ui = screen(root, [search, seg, filterChip, el('span.spacer'), el('button.btn', { type: 'button', onclick: () => load(true) }, 'Refresh')]); const tbody = el('tbody'); const more = el('button.btn', { type: 'button', onclick: () => load() }, 'Load older'); let cursor = '', count = 0, seq = 0; function sync() { const u = new URLSearchParams(location.search); search.value ? u.set('q', search.value) : u.delete('q'); status !== 'all' ? u.set('status', status) : u.delete('status'); history.replaceState({}, '', `/queries${u.size ? `?${u}` : ''}`); } search.addEventListener('input', debounce(() => { sync(); load(true); }, 350)); async function load(reset = false) { const my = ++seq; if (reset) { cursor = ''; count = 0; } more.disabled = true; ui.setStatus('Loading…'); try { const r = await get('/api/dns/queries', { search: search.value.trim(), status, client, ip, domain, older_than: cursor, limit: 100 }); if (my !== seq) return; if (reset) tbody.replaceChildren(); cursor = r.oldest; count += r.rows.length; tbody.append(...r.rows.map((q) => el(`tr${q.device?.suspicious ? '.flagged' : ''}`, {}, el('td.nowrap', { title: q.time }, when(q.ts)), el('td', {}, clientLink(q.client, q.device, q.client_name)), el('td.domain', {}, domainLink(q.domain)), el('td.mono', {}, q.type), el('td', {}, resultChip(q)), el('td.num', {}, ms(q.elapsed_ms))))); if (!ui.body.querySelector('table')) { fill(ui.body, el('div.table-wrap', {}, el('table.table', {}, el('thead', {}, el('tr', {}, ...['Time', 'Client', 'Domain', 'Type', 'Result', { t: 'Response', c: 'num' }].map((h) => el('th', h.c ? { class: h.c } : {}, h.t || h)))), tbody)), el('div.more', {}, more)); } if (!count) fill(ui.body, el('div.empty', {}, 'No queries match.')); more.hidden = r.done || !cursor; ui.setStatus(`${count} queries${r.done ? ' · end of the AdGuard log' : ''}`); } catch (err) { if (my !== seq) return; fill(ui.body, adguardProblem(err)); ui.setStatus(err.message, 'error'); } finally { more.disabled = false; } } load(true); }