693 lines
25 KiB
TypeScript
693 lines
25 KiB
TypeScript
import type { DiskSample, ProbeSample } from '../../../shared/ws-protocol.js';
|
|
import { apiFetch } from '../api.js';
|
|
import { createDualRangeChartSvg, createRangeChartSvg } from '../charts.js';
|
|
import { confirmDialog } from '../dialogs.js';
|
|
import {
|
|
el,
|
|
extractErrorMessage,
|
|
fmtBytesPerSec,
|
|
fmtTime,
|
|
type ServerEntry,
|
|
} from '../state.js';
|
|
import { showToast } from '../toast.js';
|
|
|
|
type DetailTab = 'metrics' | 'inspector' | 'keys' | 'tunnels';
|
|
|
|
let activeTab: DetailTab = 'metrics';
|
|
let activeRange: '1h' | '6h' | '24h' | '7d' | '30d' = '1h';
|
|
let procSortBy: 'cpu' | 'mem' = 'cpu';
|
|
let inspectSudo = false;
|
|
let showAllKnownHosts = false;
|
|
|
|
export async function renderServerDetailView(
|
|
root: HTMLElement,
|
|
serverId: string,
|
|
onNavigateDashboard: () => void,
|
|
): Promise<void> {
|
|
root.replaceChildren();
|
|
|
|
const listRes = await apiFetch<{ servers: ServerEntry[] }>('/api/servers');
|
|
const entry = listRes.servers.find((s) => s.config.id === serverId);
|
|
if (!entry) {
|
|
root.append(
|
|
el(
|
|
'section',
|
|
{ class: 'panel' },
|
|
el('h1', {}, `Server "${serverId}" not found`),
|
|
el('a', { href: '#dashboard', class: 'btn btn-sm' }, 'Back to Dashboard'),
|
|
),
|
|
);
|
|
return;
|
|
}
|
|
|
|
const header = el('section', { class: 'panel' });
|
|
const testConnBtn = el(
|
|
'button',
|
|
{ type: 'button', class: 'btn btn-sm mutating-action' },
|
|
'Test SSH',
|
|
);
|
|
testConnBtn.addEventListener('click', async () => {
|
|
testConnBtn.disabled = true;
|
|
try {
|
|
const res = await apiFetch<{
|
|
ok: boolean;
|
|
latencyMs: number;
|
|
osDetected?: string;
|
|
hostFingerprint?: string;
|
|
error?: string;
|
|
}>('/api/servers/test-connection', {
|
|
method: 'POST',
|
|
body: entry.config,
|
|
});
|
|
if (res.ok) {
|
|
showToast(`SSH OK in ${Math.round(res.latencyMs)}ms (${res.osDetected ?? 'unknown'})`, 'ok');
|
|
} else {
|
|
showToast(`SSH test failed: ${res.error ?? 'unknown error'}`, 'err');
|
|
}
|
|
} catch (err) {
|
|
showToast(extractErrorMessage(err), 'err');
|
|
} finally {
|
|
testConnBtn.disabled = false;
|
|
}
|
|
});
|
|
|
|
const reconnBtn = el(
|
|
'button',
|
|
{ type: 'button', class: 'btn btn-sm mutating-action' },
|
|
'Reconnect SSH',
|
|
);
|
|
reconnBtn.addEventListener('click', async () => {
|
|
reconnBtn.disabled = true;
|
|
await apiFetch(`/api/servers/${encodeURIComponent(serverId)}/reconnect`, {
|
|
method: 'POST',
|
|
}).catch(() => undefined);
|
|
await renderServerDetailView(root, serverId, onNavigateDashboard);
|
|
});
|
|
|
|
const delBtn = el(
|
|
'button',
|
|
{ type: 'button', class: 'btn btn-sm btn-danger mutating-action' },
|
|
'Delete Server',
|
|
);
|
|
delBtn.addEventListener('click', async () => {
|
|
const ok = await confirmDialog(
|
|
'dialog-confirm',
|
|
`Delete server configuration "${serverId}"?`,
|
|
);
|
|
if (!ok) return;
|
|
await apiFetch(`/api/servers/${encodeURIComponent(serverId)}`, {
|
|
method: 'DELETE',
|
|
});
|
|
onNavigateDashboard();
|
|
});
|
|
|
|
const tabsRow = el(
|
|
'div',
|
|
{ class: 'row', style: 'margin-top:8px' },
|
|
...(['metrics', 'inspector', 'keys', 'tunnels'] as DetailTab[]).map((t) => {
|
|
const b = el(
|
|
'button',
|
|
{
|
|
type: 'button',
|
|
class: `btn btn-sm${activeTab === t ? ' btn-primary' : ''}`,
|
|
},
|
|
t === 'metrics'
|
|
? 'Metrics & Probes'
|
|
: t === 'inspector'
|
|
? 'Server Inspector'
|
|
: t === 'keys'
|
|
? 'SSH Keys & Rotation'
|
|
: 'Tunnels & Proxy',
|
|
);
|
|
b.addEventListener('click', () => {
|
|
activeTab = t;
|
|
void renderServerDetailView(root, serverId, onNavigateDashboard);
|
|
});
|
|
return b;
|
|
}),
|
|
el('a', { href: `#terminal?server=${encodeURIComponent(serverId)}`, class: 'btn btn-sm' }, 'Terminal'),
|
|
el('a', { href: `#sftp?server=${encodeURIComponent(serverId)}`, class: 'btn btn-sm' }, 'SFTP'),
|
|
el('a', { href: '#config', class: 'btn btn-sm' }, 'Edit JSON'),
|
|
);
|
|
|
|
header.append(
|
|
el(
|
|
'div',
|
|
{ class: 'row-between' },
|
|
el(
|
|
'div',
|
|
{},
|
|
el('h1', {}, `${entry.config.name} (${entry.config.id})`),
|
|
el('div', { class: 'mono muted' }, `${entry.config.username}@${entry.config.host}:${entry.config.port}${entry.config.jumpHostId ? ` via ${entry.config.jumpHostId}` : ''}`),
|
|
),
|
|
el(
|
|
'div',
|
|
{ class: 'row' },
|
|
el('span', { class: 'badge' }, `[${entry.runtime.status.toUpperCase()}]`),
|
|
testConnBtn,
|
|
reconnBtn,
|
|
delBtn,
|
|
el('a', { href: '#dashboard', class: 'btn btn-sm' }, '← Dashboard'),
|
|
),
|
|
),
|
|
tabsRow,
|
|
);
|
|
root.append(header);
|
|
|
|
const body = el('div', { class: 'stack' });
|
|
root.append(body);
|
|
|
|
if (activeTab === 'metrics') {
|
|
await renderMetricsTab(body, serverId);
|
|
} else if (activeTab === 'inspector') {
|
|
await renderInspectorTab(body, serverId);
|
|
} else if (activeTab === 'keys') {
|
|
await renderKeysTab(body, serverId, `${entry.config.host}:${entry.config.port}`);
|
|
} else {
|
|
const { renderTunnelsPanel } = await import('./tunnels-panel.js');
|
|
await renderTunnelsPanel(body, serverId);
|
|
}
|
|
}
|
|
|
|
async function renderMetricsTab(body: HTMLElement, serverId: string): Promise<void> {
|
|
const [data, probeHistRes] = await Promise.all([
|
|
apiFetch<{
|
|
range: string;
|
|
granularity: 'raw' | '1h' | '1d';
|
|
points: Array<Record<string, number>>;
|
|
latestDisks: DiskSample[];
|
|
latestProbes: Array<ProbeSample & { ts: number }>;
|
|
probeHistory?: Array<ProbeSample & { ts: number }>;
|
|
diskHistory?: Array<{ ts: number; mountPoint: string; usedPct: number; usedMb: number; totalMb: number }>;
|
|
}>(`/api/servers/${encodeURIComponent(serverId)}/metrics?range=${activeRange}`),
|
|
apiFetch<{ samples: Array<ProbeSample & { ts: number }> }>(
|
|
`/api/servers/${encodeURIComponent(serverId)}/probes/history?range=${activeRange}`,
|
|
).catch(() => ({ samples: [] })),
|
|
]);
|
|
|
|
const rangeBar = el(
|
|
'div',
|
|
{ class: 'panel row-between' },
|
|
el('strong', {}, `Time-Series (${data.granularity} granularity, ${data.points.length} samples)`),
|
|
el(
|
|
'div',
|
|
{ class: 'row' },
|
|
...(['1h', '6h', '24h', '7d', '30d'] as const).map((r) => {
|
|
const b = el(
|
|
'button',
|
|
{ type: 'button', class: `btn btn-sm${activeRange === r ? ' btn-primary' : ''}` },
|
|
r,
|
|
);
|
|
b.addEventListener('click', () => {
|
|
activeRange = r;
|
|
body.replaceChildren();
|
|
void renderMetricsTab(body, serverId);
|
|
});
|
|
return b;
|
|
}),
|
|
),
|
|
);
|
|
body.append(rangeBar);
|
|
|
|
const cpuSeries = data.points.map((p) => p.cpuPct ?? p.cpuAvg ?? 0);
|
|
const memSeries = data.points.map((p) => {
|
|
if (p.memAvgPct !== undefined) return p.memAvgPct;
|
|
return p.memTotalMb ? ((p.memUsedMb ?? 0) / p.memTotalMb) * 100 : 0;
|
|
});
|
|
const loadSeries = data.points.map((p) => p.load1m ?? p.loadMax ?? 0);
|
|
const netRxSeries = data.points.map((p) => p.netRxBps ?? p.netRxAvgBps ?? 0);
|
|
const netTxSeries = data.points.map((p) => p.netTxBps ?? p.netTxAvgBps ?? 0);
|
|
|
|
const diskMaxByTs = new Map<number, number>();
|
|
for (const d of data.diskHistory ?? []) {
|
|
const prev = diskMaxByTs.get(d.ts) ?? 0;
|
|
if (d.usedPct > prev) diskMaxByTs.set(d.ts, d.usedPct);
|
|
}
|
|
const diskSeries = data.points.some((p) => p.diskMaxPct !== undefined)
|
|
? data.points.map((p) => p.diskMaxPct ?? 0)
|
|
: [...diskMaxByTs.entries()].sort((a, b) => a[0] - b[0]).map((x) => x[1]);
|
|
|
|
const probeHistory = probeHistRes.samples.length > 0 ? probeHistRes.samples : (data.probeHistory ?? []);
|
|
const probeLatencySeries = probeHistory.map((p) => p.latencyMs ?? 0);
|
|
const lastRx = netRxSeries.at(-1) ?? 0;
|
|
const lastTx = netTxSeries.at(-1) ?? 0;
|
|
|
|
const chartsGrid = el(
|
|
'div',
|
|
{ class: 'grid-3' },
|
|
el('div', { class: 'panel' }, el('h2', {}, 'CPU %'), createRangeChartSvg(cpuSeries, { maxVal: 100 })),
|
|
el('div', { class: 'panel' }, el('h2', {}, 'Memory %'), createRangeChartSvg(memSeries, { maxVal: 100, color: 'var(--ok)' })),
|
|
el('div', { class: 'panel' }, el('h2', {}, 'Load Average (1m)'), createRangeChartSvg(loadSeries, { color: 'var(--warn)' })),
|
|
el(
|
|
'div',
|
|
{ class: 'panel' },
|
|
el('h2', {}, 'Net I/O (RX / TX)'),
|
|
el('div', { class: 'muted mono' }, `RX ${fmtBytesPerSec(lastRx)} | TX ${fmtBytesPerSec(lastTx)}`),
|
|
createDualRangeChartSvg(netRxSeries, netTxSeries, { colorA: 'var(--accent)', colorB: 'var(--warn)' }),
|
|
),
|
|
el(
|
|
'div',
|
|
{ class: 'panel' },
|
|
el('h2', {}, 'Max Disk %'),
|
|
createRangeChartSvg(diskSeries, { maxVal: 100, color: 'var(--err)' }),
|
|
),
|
|
el(
|
|
'div',
|
|
{ class: 'panel' },
|
|
el('h2', {}, 'Probe Latency (ms)'),
|
|
createRangeChartSvg(probeLatencySeries, { color: 'var(--ok)' }),
|
|
),
|
|
);
|
|
body.append(chartsGrid);
|
|
|
|
// Disks table
|
|
const diskPanel = el('section', { class: 'panel' });
|
|
diskPanel.append(el('h2', {}, 'Mounted Real Filesystems'));
|
|
const dTable = el('table');
|
|
dTable.append(el('thead', {}, el('tr', {}, el('th', {}, 'Mount'), el('th', {}, 'FS'), el('th', {}, 'Used / Total (MB)'), el('th', {}, 'Used %'))));
|
|
const dBody = el('tbody');
|
|
if (data.latestDisks.length === 0) {
|
|
dBody.append(el('tr', {}, el('td', { colspan: 4, class: 'muted' }, 'No disk samples recorded yet.')));
|
|
} else {
|
|
for (const d of data.latestDisks) {
|
|
dBody.append(
|
|
el('tr', {}, el('td', { class: 'mono' }, d.mountPoint), el('td', { class: 'mono' }, d.fsType), el('td', { class: 'mono' }, `${Math.round(d.usedMb)} / ${Math.round(d.totalMb)}`), el('td', { class: 'mono' }, `${d.usedPct}%`)),
|
|
);
|
|
}
|
|
}
|
|
dTable.append(dBody);
|
|
diskPanel.append(el('div', { class: 'table-wrap' }, dTable));
|
|
body.append(diskPanel);
|
|
|
|
// Probes table + Run Now + Probe History summary
|
|
const probePanel = el('section', { class: 'panel' });
|
|
const okProbes = probeHistory.filter((p) => p.ok).length;
|
|
const failProbes = probeHistory.length - okProbes;
|
|
probePanel.append(
|
|
el(
|
|
'div',
|
|
{ class: 'row-between' },
|
|
el('h2', {}, 'Endpoint Health Probes'),
|
|
el(
|
|
'span',
|
|
{ class: 'muted mono' },
|
|
probeHistory.length > 0 ? `${probeHistory.length} checks (${okProbes} ok / ${failProbes} fail in ${activeRange})` : 'No probe history in range',
|
|
),
|
|
),
|
|
);
|
|
const pTable = el('table');
|
|
pTable.append(
|
|
el(
|
|
'thead',
|
|
{},
|
|
el(
|
|
'tr',
|
|
{},
|
|
el('th', {}, 'Probe ID'),
|
|
el('th', {}, 'Status'),
|
|
el('th', {}, 'HTTP Code'),
|
|
el('th', {}, 'Latency'),
|
|
el('th', {}, 'TLS Expiry'),
|
|
el('th', {}, 'Checked At'),
|
|
el('th', {}, 'Detail'),
|
|
el('th', {}, 'Action'),
|
|
),
|
|
),
|
|
);
|
|
const pBody = el('tbody');
|
|
if (data.latestProbes.length === 0) {
|
|
pBody.append(el('tr', {}, el('td', { colspan: 8, class: 'muted' }, 'No probe results recorded yet.')));
|
|
} else {
|
|
for (const p of data.latestProbes) {
|
|
const tlsBadge =
|
|
p.certExpiryDays !== undefined
|
|
? el(
|
|
'span',
|
|
{
|
|
class: `badge ${
|
|
p.certExpiryDays <= 0
|
|
? 'badge-err'
|
|
: p.certExpiryDays <= 14
|
|
? 'badge-warn'
|
|
: 'badge-ok'
|
|
}`,
|
|
},
|
|
`${p.certExpiryDays}d`,
|
|
)
|
|
: el('span', { class: 'muted' }, '—');
|
|
const runBtn = el('button', { type: 'button', class: 'btn btn-sm' }, 'Run Now');
|
|
runBtn.addEventListener('click', async () => {
|
|
runBtn.disabled = true;
|
|
try {
|
|
const runRes = await apiFetch<{ sample: ProbeSample }>(
|
|
`/api/servers/${encodeURIComponent(serverId)}/probes/${encodeURIComponent(p.probeId)}/run`,
|
|
{ method: 'POST', body: {} },
|
|
);
|
|
showToast(
|
|
`Probe "${p.probeId}": ${runRes.sample.ok ? `OK (${Math.round(runRes.sample.latencyMs)}ms)` : runRes.sample.error ?? 'FAIL'}`,
|
|
runRes.sample.ok ? 'ok' : 'err',
|
|
);
|
|
body.replaceChildren();
|
|
await renderMetricsTab(body, serverId);
|
|
} catch (err) {
|
|
showToast(extractErrorMessage(err), 'err');
|
|
} finally {
|
|
runBtn.disabled = false;
|
|
}
|
|
});
|
|
pBody.append(
|
|
el(
|
|
'tr',
|
|
{},
|
|
el('td', { class: 'mono' }, p.probeId),
|
|
el('td', {}, el('span', { class: `badge ${p.ok ? 'badge-ok' : 'badge-err'}` }, p.ok ? 'OK' : 'FAIL')),
|
|
el('td', { class: 'mono' }, p.statusCode ? String(p.statusCode) : '—'),
|
|
el('td', { class: 'mono' }, `${Math.round(p.latencyMs)} ms`),
|
|
el('td', {}, tlsBadge),
|
|
el('td', { class: 'mono' }, fmtTime(p.ts)),
|
|
el('td', { class: 'mono err-text' }, p.error ?? '—'),
|
|
el('td', {}, runBtn),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
pTable.append(pBody);
|
|
probePanel.append(el('div', { class: 'table-wrap' }, pTable));
|
|
body.append(probePanel);
|
|
}
|
|
|
|
async function renderInspectorTab(body: HTMLElement, serverId: string): Promise<void> {
|
|
const sortCpuBtn = el('button', { type: 'button', class: `btn btn-sm${procSortBy === 'cpu' ? ' btn-primary' : ''}` }, 'Sort CPU');
|
|
const sortMemBtn = el('button', { type: 'button', class: `btn btn-sm${procSortBy === 'mem' ? ' btn-primary' : ''}` }, 'Sort RAM');
|
|
const sudoBtn = el(
|
|
'button',
|
|
{ type: 'button', class: `btn btn-sm mutating-action${inspectSudo ? ' btn-primary' : ''}` },
|
|
inspectSudo ? 'Sudo: ON' : '+Sudo',
|
|
);
|
|
const refreshBtn = el('button', { type: 'button', class: 'btn btn-sm' }, 'Refresh Snapshot');
|
|
|
|
sortCpuBtn.addEventListener('click', () => {
|
|
procSortBy = 'cpu';
|
|
body.replaceChildren();
|
|
void renderInspectorTab(body, serverId);
|
|
});
|
|
sortMemBtn.addEventListener('click', () => {
|
|
procSortBy = 'mem';
|
|
body.replaceChildren();
|
|
void renderInspectorTab(body, serverId);
|
|
});
|
|
sudoBtn.addEventListener('click', () => {
|
|
inspectSudo = !inspectSudo;
|
|
body.replaceChildren();
|
|
void renderInspectorTab(body, serverId);
|
|
});
|
|
refreshBtn.addEventListener('click', () => {
|
|
body.replaceChildren();
|
|
void renderInspectorTab(body, serverId);
|
|
});
|
|
|
|
body.append(
|
|
el('div', { class: 'panel row-between' }, el('strong', {}, 'On-Demand Server Inspector'), el('div', { class: 'row' }, sortCpuBtn, sortMemBtn, sudoBtn, refreshBtn)),
|
|
);
|
|
|
|
try {
|
|
const res = await apiFetch<{
|
|
os: string;
|
|
collectedAt: number;
|
|
processes: Array<{ pid: number; user: string; cpuPct: number; memPct: number; rssMb: number; command: string }>;
|
|
ports: Array<{ proto: string; localAddress: string; process: string }>;
|
|
thermals: Array<{ zone: string; tempC: number }>;
|
|
}>(`/api/servers/${encodeURIComponent(serverId)}/inspect?sort=${procSortBy}&sudo=${inspectSudo ? 'true' : 'false'}`);
|
|
|
|
const sortedProcs = [...res.processes].sort((a, b) =>
|
|
procSortBy === 'cpu' ? b.cpuPct - a.cpuPct : b.memPct - a.memPct,
|
|
);
|
|
|
|
const procTable = el('table');
|
|
procTable.append(el('thead', {}, el('tr', {}, el('th', {}, 'PID'), el('th', {}, 'User'), el('th', {}, 'CPU%'), el('th', {}, 'MEM%'), el('th', {}, 'RSS'), el('th', {}, 'Command'), el('th', {}, 'Signal'))));
|
|
const pBody = el('tbody');
|
|
for (const p of sortedProcs) {
|
|
const makeSigBtn = (sig: 'SIGTERM' | 'SIGKILL', cls: string) => {
|
|
const b = el('button', { type: 'button', class: `btn btn-sm ${cls} mutating-action` }, sig === 'SIGTERM' ? 'TERM' : 'KILL');
|
|
b.addEventListener('click', async () => {
|
|
const ok = await confirmDialog(
|
|
'dialog-confirm',
|
|
`Send ${sig} to PID ${p.pid} (${p.command.slice(0, 48)}) on ${serverId}?`,
|
|
);
|
|
if (!ok) return;
|
|
try {
|
|
const sigRes = await apiFetch<{ ok: boolean; error?: string }>(
|
|
`/api/servers/${encodeURIComponent(serverId)}/processes/${p.pid}/signal`,
|
|
{ method: 'POST', body: { signal: sig, sudo: inspectSudo } },
|
|
);
|
|
showToast(
|
|
sigRes.ok ? `Sent ${sig} to PID ${p.pid}.` : `Signal ${sig} failed: ${sigRes.error ?? 'error'}`,
|
|
sigRes.ok ? 'ok' : 'err',
|
|
);
|
|
body.replaceChildren();
|
|
await renderInspectorTab(body, serverId);
|
|
} catch (err) {
|
|
showToast(extractErrorMessage(err), 'err');
|
|
}
|
|
});
|
|
return b;
|
|
};
|
|
pBody.append(
|
|
el(
|
|
'tr',
|
|
{},
|
|
el('td', { class: 'mono' }, String(p.pid)),
|
|
el('td', { class: 'mono' }, p.user),
|
|
el('td', { class: 'mono' }, `${p.cpuPct}`),
|
|
el('td', { class: 'mono' }, `${p.memPct}`),
|
|
el('td', { class: 'mono' }, `${p.rssMb}M`),
|
|
el('td', { class: 'mono' }, p.command.slice(0, 90)),
|
|
el('td', {}, el('div', { class: 'row' }, makeSigBtn('SIGTERM', ''), makeSigBtn('SIGKILL', 'btn-danger'))),
|
|
),
|
|
);
|
|
}
|
|
procTable.append(pBody);
|
|
|
|
const portTable = el('table');
|
|
portTable.append(el('thead', {}, el('tr', {}, el('th', {}, 'Proto'), el('th', {}, 'Listen Address'), el('th', {}, 'Process'))));
|
|
const portBody = el('tbody');
|
|
for (const pt of res.ports) {
|
|
portBody.append(el('tr', {}, el('td', { class: 'mono' }, pt.proto), el('td', { class: 'mono' }, pt.localAddress), el('td', { class: 'mono' }, pt.process)));
|
|
}
|
|
portTable.append(portBody);
|
|
|
|
const thermPanel = el('section', { class: 'panel' });
|
|
thermPanel.append(el('h2', {}, `Thermal Sensors (${res.thermals.length})`));
|
|
if (res.thermals.length === 0) {
|
|
thermPanel.append(el('p', { class: 'muted' }, 'No thermal zones exposed on this host.'));
|
|
} else {
|
|
for (const th of res.thermals) {
|
|
thermPanel.append(el('div', { class: 'mono' }, `${th.zone}: ${th.tempC} °C`));
|
|
}
|
|
}
|
|
|
|
body.append(
|
|
el('section', { class: 'panel panel-region' }, el('h2', {}, `Top Processes (${res.os}, ${fmtTime(res.collectedAt)})`), el('div', { class: 'table-wrap' }, procTable)),
|
|
el('section', { class: 'panel panel-region' }, el('h2', {}, 'Listening Sockets'), el('div', { class: 'table-wrap' }, portTable)),
|
|
thermPanel,
|
|
);
|
|
} catch (err) {
|
|
body.append(el('div', { class: 'panel err-text' }, extractErrorMessage(err)));
|
|
}
|
|
}
|
|
|
|
async function renderKeysTab(body: HTMLElement, serverId: string, hostPortKey: string): Promise<void> {
|
|
const msg = el('p', { class: 'form-msg' });
|
|
const rotateBtn = el(
|
|
'button',
|
|
{ type: 'button', class: 'btn btn-primary btn-sm mutating-action' },
|
|
'Atomic Zero-Lockout Rotate Key',
|
|
);
|
|
rotateBtn.addEventListener('click', async () => {
|
|
const ok = await confirmDialog(
|
|
'dialog-confirm',
|
|
`Generate a new Ed25519 keypair, verify connection on "${serverId}", update server JSON, and remove the old key?`,
|
|
);
|
|
if (!ok) return;
|
|
msg.textContent = 'Rotating SSH key...';
|
|
try {
|
|
const res = await apiFetch<{
|
|
results: Array<{ ok: boolean; newCredentialId?: string; rolledBack?: boolean; error?: string }>;
|
|
}>('/api/servers/rotate-keys', {
|
|
method: 'POST',
|
|
body: { serverIds: [serverId] },
|
|
});
|
|
const r = res.results[0];
|
|
if (r?.ok) {
|
|
body.replaceChildren();
|
|
await renderKeysTab(body, serverId, hostPortKey);
|
|
} else {
|
|
msg.className = 'form-msg err-text';
|
|
msg.textContent = `Rotation failed (rolledBack=${String(r?.rolledBack)}): ${r?.error ?? 'unknown'}`;
|
|
}
|
|
} catch (err) {
|
|
msg.className = 'form-msg err-text';
|
|
msg.textContent = extractErrorMessage(err);
|
|
}
|
|
});
|
|
|
|
const deploySelect = el('select', { class: 'input-sm' });
|
|
const deployBtn = el('button', { type: 'button', class: 'btn btn-sm mutating-action' }, 'Deploy Vault Key');
|
|
try {
|
|
const creds = await apiFetch<{ credentials: Array<{ id: string; type: string }> }>('/api/vault/credentials');
|
|
for (const c of creds.credentials.filter((x) => x.type === 'ssh-key')) {
|
|
deploySelect.append(el('option', { value: c.id }, c.id));
|
|
}
|
|
} catch {
|
|
// Vault locked or empty
|
|
}
|
|
deployBtn.addEventListener('click', async () => {
|
|
if (!deploySelect.value) return;
|
|
try {
|
|
await apiFetch(`/api/servers/${encodeURIComponent(serverId)}/keys/deploy`, {
|
|
method: 'POST',
|
|
body: { credentialId: deploySelect.value },
|
|
});
|
|
body.replaceChildren();
|
|
await renderKeysTab(body, serverId, hostPortKey);
|
|
} catch (err) {
|
|
msg.className = 'form-msg err-text';
|
|
msg.textContent = extractErrorMessage(err);
|
|
}
|
|
});
|
|
|
|
body.append(
|
|
el(
|
|
'div',
|
|
{ class: 'panel row-between' },
|
|
el('div', { class: 'row' }, deploySelect, deployBtn),
|
|
rotateBtn,
|
|
),
|
|
msg,
|
|
);
|
|
|
|
// Known Hosts (Multi-Algo)
|
|
try {
|
|
const khRes = await apiFetch<{
|
|
knownHosts: Array<{ hostPort: string; algorithm: string; fingerprint: string; updatedAt: number }>;
|
|
}>('/api/servers/known-hosts');
|
|
const filteredKh = showAllKnownHosts
|
|
? khRes.knownHosts
|
|
: khRes.knownHosts.filter((kh) => kh.hostPort === hostPortKey);
|
|
const scopeBtn = el(
|
|
'button',
|
|
{ type: 'button', class: 'btn btn-sm' },
|
|
showAllKnownHosts ? `Filter ${hostPortKey}` : `Show All (${khRes.knownHosts.length})`,
|
|
);
|
|
scopeBtn.addEventListener('click', () => {
|
|
showAllKnownHosts = !showAllKnownHosts;
|
|
body.replaceChildren();
|
|
void renderKeysTab(body, serverId, hostPortKey);
|
|
});
|
|
const khTable = el('table');
|
|
khTable.append(el('thead', {}, el('tr', {}, el('th', {}, 'Host:Port'), el('th', {}, 'Algorithm'), el('th', {}, 'Fingerprint'), el('th', {}, 'Updated'), el('th', {}, 'Action'))));
|
|
const khBody = el('tbody');
|
|
if (filteredKh.length === 0) {
|
|
khBody.append(el('tr', {}, el('td', { colspan: 5, class: 'muted' }, 'No trusted host keys in scope.')));
|
|
} else {
|
|
for (const kh of filteredKh) {
|
|
const delKhBtn = el('button', { type: 'button', class: 'btn btn-sm btn-danger mutating-action' }, 'Delete');
|
|
delKhBtn.addEventListener('click', async () => {
|
|
const ok = await confirmDialog(
|
|
'dialog-confirm',
|
|
`Remove trusted ${kh.algorithm} key for ${kh.hostPort}? Next connection will require TOFU approval.`,
|
|
);
|
|
if (!ok) return;
|
|
await apiFetch(
|
|
`/api/servers/known-hosts?hostPort=${encodeURIComponent(kh.hostPort)}&algorithm=${encodeURIComponent(kh.algorithm)}`,
|
|
{ method: 'DELETE' },
|
|
);
|
|
showToast(`Deleted ${kh.algorithm} key for ${kh.hostPort}.`, 'ok');
|
|
body.replaceChildren();
|
|
await renderKeysTab(body, serverId, hostPortKey);
|
|
});
|
|
khBody.append(
|
|
el(
|
|
'tr',
|
|
{},
|
|
el('td', { class: 'mono' }, kh.hostPort),
|
|
el('td', { class: 'mono' }, kh.algorithm),
|
|
el('td', { class: 'mono' }, kh.fingerprint),
|
|
el('td', { class: 'mono' }, fmtTime(kh.updatedAt)),
|
|
el('td', {}, delKhBtn),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
khTable.append(khBody);
|
|
body.append(
|
|
el(
|
|
'section',
|
|
{ class: 'panel' },
|
|
el('div', { class: 'row-between' }, el('h2', {}, 'Trusted Host Keys (known_hosts)'), scopeBtn),
|
|
el('div', { class: 'table-wrap' }, khTable),
|
|
),
|
|
);
|
|
} catch (err) {
|
|
body.append(el('div', { class: 'panel err-text' }, extractErrorMessage(err)));
|
|
}
|
|
|
|
try {
|
|
const res = await apiFetch<{
|
|
entries: Array<{
|
|
lineNumber: number;
|
|
keyType: string;
|
|
fingerprint: string;
|
|
comment?: string;
|
|
isCurrentServerKey: boolean;
|
|
matchedVaultCredentialId?: string;
|
|
}>;
|
|
}>(`/api/servers/${encodeURIComponent(serverId)}/keys`);
|
|
|
|
const kTable = el('table');
|
|
kTable.append(el('thead', {}, el('tr', {}, el('th', {}, '#'), el('th', {}, 'Type'), el('th', {}, 'Fingerprint'), el('th', {}, 'Comment / Vault'), el('th', {}, 'Action'))));
|
|
const kBody = el('tbody');
|
|
for (const k of res.entries) {
|
|
const revBtn = el(
|
|
'button',
|
|
{
|
|
type: 'button',
|
|
class: 'btn btn-sm btn-danger mutating-action',
|
|
disabled: k.isCurrentServerKey ? true : undefined,
|
|
},
|
|
k.isCurrentServerKey ? 'Active Key' : 'Revoke',
|
|
);
|
|
if (!k.isCurrentServerKey) {
|
|
revBtn.addEventListener('click', async () => {
|
|
const ok = await confirmDialog('dialog-confirm', `Remove key ${k.fingerprint} from ~/.ssh/authorized_keys?`);
|
|
if (!ok) return;
|
|
await apiFetch(`/api/servers/${encodeURIComponent(serverId)}/keys/revoke`, {
|
|
method: 'POST',
|
|
body: { fingerprint: k.fingerprint },
|
|
});
|
|
body.replaceChildren();
|
|
await renderKeysTab(body, serverId, hostPortKey);
|
|
});
|
|
}
|
|
kBody.append(
|
|
el(
|
|
'tr',
|
|
{},
|
|
el('td', { class: 'mono' }, String(k.lineNumber)),
|
|
el('td', { class: 'mono' }, k.keyType),
|
|
el('td', { class: 'mono' }, k.fingerprint),
|
|
el(
|
|
'td',
|
|
{ class: 'mono' },
|
|
`${k.comment ?? '—'}${k.matchedVaultCredentialId ? ` [vault:${k.matchedVaultCredentialId}]` : ''}`,
|
|
),
|
|
el('td', {}, revBtn),
|
|
),
|
|
);
|
|
}
|
|
kTable.append(kBody);
|
|
body.append(el('section', { class: 'panel' }, el('h2', {}, '~/.ssh/authorized_keys'), el('div', { class: 'table-wrap' }, kTable)));
|
|
} catch (err) {
|
|
body.append(el('div', { class: 'panel err-text' }, extractErrorMessage(err)));
|
|
}
|
|
}
|