tailcat-webui 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +29 -0
  3. package/README.en.md +112 -0
  4. package/README.md +101 -2
  5. package/bin/tailcat-webui.js +12 -0
  6. package/docs/CORE-API.md +343 -0
  7. package/docs/DESIGN.md +398 -0
  8. package/docs/assets/character.jpg +0 -0
  9. package/docs/assets/portrait.jpg +0 -0
  10. package/docs/screenshots/01-overview-light.png +0 -0
  11. package/docs/screenshots/02-overview-dark.png +0 -0
  12. package/docs/screenshots/03-ticket-light.png +0 -0
  13. package/docs/screenshots/04-ticket-qr-light.png +0 -0
  14. package/docs/screenshots/05-settings-light.png +0 -0
  15. package/docs/screenshots/06-diagnostics-light.png +0 -0
  16. package/docs/screenshots/07-mobile-overview.png +0 -0
  17. package/docs/screenshots/08-install-missing.png +0 -0
  18. package/docs/screenshots/09-keeper.png +0 -0
  19. package/package.json +56 -4
  20. package/src/cli.js +238 -0
  21. package/src/config.js +167 -0
  22. package/src/devices.js +163 -0
  23. package/src/errors.js +24 -0
  24. package/src/index.js +19 -0
  25. package/src/installer.js +907 -0
  26. package/src/manager.js +647 -0
  27. package/src/parse.js +207 -0
  28. package/src/paths.js +97 -0
  29. package/src/preferences.js +190 -0
  30. package/src/qr.js +37 -0
  31. package/src/relay.js +76 -0
  32. package/src/runtime/node.js +281 -0
  33. package/src/serve-targets.js +127 -0
  34. package/src/server.js +674 -0
  35. package/src/settings.js +262 -0
  36. package/src/supervisor.js +833 -0
  37. package/vendor/qrcode.mjs +2289 -0
  38. package/web/api.js +114 -0
  39. package/web/assets/portrait.jpg +0 -0
  40. package/web/boot.js +29 -0
  41. package/web/components.js +558 -0
  42. package/web/dict.js +663 -0
  43. package/web/drawer-access.js +318 -0
  44. package/web/drawer-address.js +271 -0
  45. package/web/drawer-automation.js +223 -0
  46. package/web/drawer-relay.js +128 -0
  47. package/web/drawer-tunnel.js +309 -0
  48. package/web/drawer.js +302 -0
  49. package/web/format.js +252 -0
  50. package/web/icon.png +0 -0
  51. package/web/index.html +29 -0
  52. package/web/main.js +471 -0
  53. package/web/room-diagnostics.js +426 -0
  54. package/web/room-overview.js +438 -0
  55. package/web/room-settings.js +71 -0
  56. package/web/store.js +168 -0
  57. package/web/styles.css +2873 -0
  58. package/web/ticket.js +212 -0
@@ -0,0 +1,426 @@
1
+ /**
2
+ * Room: diagnostics.
3
+ *
4
+ * Read-only runtime facts, the tailcat program state, recent connections and
5
+ * refusals, warnings, and the collapsed log. Every section re-reads the
6
+ * snapshot and only rebuilds a list when its content actually changed.
7
+ */
8
+
9
+ import {
10
+ h,
11
+ button,
12
+ alertBox,
13
+ setText,
14
+ setHidden,
15
+ setDisabled,
16
+ syncSignature,
17
+ } from './components.js';
18
+ import {
19
+ ADDRESS_MASK,
20
+ phaseOf,
21
+ phaseKey,
22
+ tailcatOf,
23
+ installerOf,
24
+ settingsOf,
25
+ pairingOf,
26
+ targetsOf,
27
+ formatRegion,
28
+ formatDuration,
29
+ formatClock,
30
+ formatBytes,
31
+ parseMapping,
32
+ failureText,
33
+ } from './format.js';
34
+
35
+ const INSTALL_PHASE_KEYS = {
36
+ downloading: 'programPhaseDownload',
37
+ checksums: 'programPhaseChecksums',
38
+ verifying: 'programPhaseVerify',
39
+ verify: 'programPhaseVerify',
40
+ extracting: 'programPhaseExtract',
41
+ extract: 'programPhaseExtract',
42
+ installing: 'programPhaseInstall',
43
+ install: 'programPhaseInstall',
44
+ homebrew: 'programPhaseHomebrew',
45
+ };
46
+
47
+ const MANUAL_URL = 'https://github.com/tailscale/tailcat/releases';
48
+
49
+ /**
50
+ * @param {Function} t - translator.
51
+ * @param {string} labelKey - label copy key.
52
+ * @param {{ mono?: boolean, testid?: string }} [options] - value style.
53
+ * @returns {{ row: HTMLElement, value: HTMLElement }} one fact row.
54
+ */
55
+ function factRow(t, labelKey, options = {}) {
56
+ const value = h('span', {
57
+ class: `fact-value${options.mono ? ' is-mono' : ''}`,
58
+ ...(options.testid ? { 'data-testid': options.testid } : {}),
59
+ });
60
+ const row = h('div', { class: 'fact' },
61
+ h('span', { class: 'fact-label' }, t(labelKey)),
62
+ value);
63
+ return { row, value };
64
+ }
65
+
66
+ /**
67
+ * @param {Function} t - translator.
68
+ * @param {'zh' | 'en'} lang - language code.
69
+ * @param {object} entry - connection record.
70
+ * @returns {HTMLTableRowElement} table row.
71
+ */
72
+ function connectionRow(t, lang, entry) {
73
+ const peer = typeof entry.peer === 'string' && entry.peer !== '' ? entry.peer : '—';
74
+ const port = entry.port === null || entry.port === undefined ? '—' : String(entry.port);
75
+ const via = typeof entry.via === 'string' && entry.via !== '' ? entry.via : '—';
76
+ return h('tr', null,
77
+ h('td', { 'data-label': t('connTime') }, formatClock(entry.at, lang)),
78
+ h('td', { 'data-label': t('connPeer'), class: 'is-mono' }, peer),
79
+ h('td', { 'data-label': t('connPort'), class: 'is-mono' }, port),
80
+ h('td', { 'data-label': t('connVia'), class: 'is-mono' }, via),
81
+ h('td', { 'data-label': t('connDuration') }, formatDuration(entry.durationMs, t)));
82
+ }
83
+
84
+ /**
85
+ * @param {Function} t - translator.
86
+ * @param {string | null} reason - machine reason from tailcat.
87
+ * @returns {string} localized reason.
88
+ */
89
+ function refusalReason(t, reason) {
90
+ if (reason === 'not-in-allow') return t('refusalNotInAllow');
91
+ if (reason === 'port-not-served') return t('refusalPortNotServed');
92
+ if (typeof reason === 'string' && reason !== '') return reason;
93
+ return t('refusalUnknown');
94
+ }
95
+
96
+ /**
97
+ * @param {Function} t - translator.
98
+ * @param {'zh' | 'en'} lang - language code.
99
+ * @param {object} entry - refusal record.
100
+ * @returns {HTMLTableRowElement} table row.
101
+ */
102
+ function refusalRow(t, lang, entry) {
103
+ const peer = typeof entry.peer === 'string' && entry.peer !== '' ? entry.peer : t('refusalUnknownPeer');
104
+ const port = entry.port === null || entry.port === undefined ? '—' : String(entry.port);
105
+ return h('tr', null,
106
+ h('td', { 'data-label': t('refusalTime') }, formatClock(entry.at, lang)),
107
+ h('td', { 'data-label': t('refusalPeer'), class: 'is-mono' }, peer),
108
+ h('td', { 'data-label': t('refusalReason') }, refusalReason(t, entry.reason)),
109
+ h('td', { 'data-label': t('refusalPort'), class: 'is-mono' }, port));
110
+ }
111
+
112
+ /**
113
+ * @param {object | null} state - snapshot.
114
+ * @returns {boolean} whether the interface should point at the installer.
115
+ */
116
+ function programMissing(state) {
117
+ const installer = installerOf(state);
118
+ if (installer.installed === true) return false;
119
+ const system = installer.system && typeof installer.system === 'object' ? installer.system : {};
120
+ if (system.status === 'found') return false;
121
+ const binary = tailcatOf(state).binary && typeof tailcatOf(state).binary === 'object' ? tailcatOf(state).binary : {};
122
+ if (binary.resolved) return false;
123
+ return system.status === 'missing';
124
+ }
125
+
126
+ /**
127
+ * @param {object} ctx - shell context.
128
+ * @returns {{ el: HTMLElement, update: (state: object | null) => void }} room.
129
+ */
130
+ export function createDiagnosticsRoom(ctx) {
131
+ const t = ctx.t;
132
+ const lang = ctx.lang;
133
+
134
+ /* Errors and warnings --------------------------------------------------- */
135
+ const lastErrorText = h('p', { class: 'error-text' });
136
+ const lastErrorAlert = alertBox({ tone: 'danger', title: t('lastErrorTitle'), children: lastErrorText });
137
+ setHidden(lastErrorAlert, true);
138
+ const warningSlot = h('div', { class: 'warning-slot' });
139
+
140
+ /* Runtime facts --------------------------------------------------------- */
141
+ const phaseFact = factRow(t, 'factPhase', { testid: 'diag-phase' });
142
+ const addressFact = factRow(t, 'factAddress', { mono: true });
143
+ const targetsFact = factRow(t, 'factTargets', { mono: true });
144
+ const keyModeFact = factRow(t, 'factKeyMode');
145
+ const formatFact = factRow(t, 'factAddressFormat');
146
+ const versionFact = factRow(t, 'factVersion', { mono: true });
147
+ const binaryFact = factRow(t, 'factBinary', { mono: true });
148
+ const regionFact = factRow(t, 'factRegion');
149
+ const restartsFact = factRow(t, 'factRestarts');
150
+ const refusalsFact = factRow(t, 'factRefusals');
151
+ const stateFileFact = factRow(t, 'factStateFile', { mono: true });
152
+
153
+ const factsPanel = h('section', { class: 'panel' },
154
+ h('h3', { class: 'panel-title' }, t('factsTitle')),
155
+ h('div', { class: 'facts' },
156
+ phaseFact.row,
157
+ addressFact.row,
158
+ targetsFact.row,
159
+ keyModeFact.row,
160
+ formatFact.row,
161
+ regionFact.row,
162
+ versionFact.row,
163
+ binaryFact.row,
164
+ restartsFact.row,
165
+ refusalsFact.row,
166
+ stateFileFact.row));
167
+
168
+ /* Program --------------------------------------------------------------- */
169
+ const programStatus = h('p', { class: 'program-status' });
170
+ const programDetail = h('p', { class: 'program-detail' });
171
+ const programExtra = h('div', { class: 'program-extra' });
172
+ const progressBar = h('div', { class: 'progress-bar' });
173
+ const progressText = h('p', { class: 'progress-text' });
174
+ const progressBox = h('div', { class: 'progress', hidden: true },
175
+ h('div', { class: 'progress-track' }, progressBar),
176
+ progressText);
177
+ const installErrorSlot = h('div', { class: 'install-error-slot' });
178
+
179
+ const installButton = button({ label: t('programInstall'), variant: 'primary', testid: 'install-button' });
180
+ const redetectButton = button({ label: t('programRedetect'), variant: 'secondary', testid: 'redetect-button' });
181
+ const manualLink = h('a', {
182
+ class: 'manual-link',
183
+ href: MANUAL_URL,
184
+ target: '_blank',
185
+ rel: 'noopener noreferrer',
186
+ }, t('programManual'));
187
+ const programActions = h('div', { class: 'program-actions' }, installButton, redetectButton, manualLink);
188
+
189
+ const programPanel = h('section', { class: 'panel' },
190
+ h('h3', { class: 'panel-title' }, t('programTitle')),
191
+ h('div', { class: 'program-head' }, h('span', { class: 'program-dot', 'aria-hidden': 'true' }), programStatus),
192
+ programDetail,
193
+ programExtra,
194
+ installErrorSlot,
195
+ progressBox,
196
+ programActions);
197
+
198
+ installButton.addEventListener('click', () => {
199
+ void (async () => {
200
+ const state = ctx.state();
201
+ const installer = installerOf(state);
202
+ const system = installer.system && typeof installer.system === 'object' ? installer.system : {};
203
+ const force = installer.installed === true || system.status === 'found';
204
+ const result = await ctx.act('install', force ? { force: true } : {});
205
+ if (result.ok) ctx.notify('ok', t('programInstalledNotice'));
206
+ else ctx.notify('error', failureText(result.error, t, 'actionFailed'));
207
+ update(ctx.state());
208
+ })();
209
+ });
210
+ redetectButton.addEventListener('click', () => {
211
+ void (async () => {
212
+ const state = ctx.state();
213
+ const phase = phaseOf(state);
214
+ const running = phase === 'online' || phase === 'starting' || phase === 'restarting' || phase === 'stopping';
215
+ if (running) {
216
+ const result = await ctx.act('restart', {});
217
+ if (result.ok) ctx.notify('ok', t('noticeRestarted'));
218
+ else ctx.notify('error', failureText(result.error, t, 'actionFailed'));
219
+ } else {
220
+ await ctx.refresh();
221
+ ctx.notify('info', t('noticeRefreshed'));
222
+ }
223
+ update(ctx.state());
224
+ })();
225
+ });
226
+
227
+ /* Recent connections ---------------------------------------------------- */
228
+ const connectionsSlot = h('div', { class: 'table-slot' });
229
+ const connectionsPanel = h('section', { class: 'panel' },
230
+ h('h3', { class: 'panel-title' }, t('connectionsTitle')),
231
+ connectionsSlot);
232
+
233
+ /* Refusals -------------------------------------------------------------- */
234
+ const refusalsSlot = h('div', { class: 'table-slot' });
235
+ const refusalsPanel = h('section', { class: 'panel' },
236
+ h('h3', { class: 'panel-title' }, t('refusalsTitle')),
237
+ refusalsSlot);
238
+
239
+ /* Log ------------------------------------------------------------------- */
240
+ const logText = h('pre', { class: 'log-text' });
241
+ const logDetails = h('details', { class: 'panel log-panel' },
242
+ h('summary', { class: 'log-summary' }, t('logTitle')),
243
+ h('p', { class: 'field-hint' }, t('logHint')),
244
+ logText);
245
+
246
+ const el = h('section', {
247
+ class: 'room',
248
+ 'data-testid': 'room-panel-diagnostics',
249
+ id: 'room-diagnostics',
250
+ role: 'tabpanel',
251
+ 'aria-labelledby': 'tab-diagnostics',
252
+ },
253
+ h('header', { class: 'room-head' },
254
+ h('h2', { class: 'room-title' }, t('diagTitle')),
255
+ h('p', { class: 'room-subtitle' }, t('diagSubtitle'))),
256
+ lastErrorAlert,
257
+ warningSlot,
258
+ factsPanel,
259
+ programPanel,
260
+ connectionsPanel,
261
+ refusalsPanel,
262
+ logDetails);
263
+
264
+ /**
265
+ * @param {object | null} state - latest snapshot.
266
+ */
267
+ function update(state) {
268
+ if (!state) return;
269
+ const tailcat = tailcatOf(state);
270
+ const installer = installerOf(state);
271
+ const settings = settingsOf(state);
272
+ const pairing = pairingOf(state);
273
+ const phase = phaseOf(state);
274
+
275
+ /* Errors and warnings */
276
+ const lastError = typeof tailcat.lastError === 'string' && tailcat.lastError !== '' ? tailcat.lastError : '';
277
+ setHidden(lastErrorAlert, lastError === '');
278
+ if (lastError !== '') setText(lastErrorText, lastError);
279
+ const warnings = [
280
+ ...(Array.isArray(state.warnings) ? state.warnings : []),
281
+ ...(Array.isArray(tailcat.warnings) ? tailcat.warnings : []),
282
+ ].filter((warning) => typeof warning === 'string');
283
+ syncSignature(warningSlot, warnings, () => (warnings.length === 0
284
+ ? []
285
+ : [alertBox({
286
+ tone: 'warn',
287
+ title: t('warningsTitle'),
288
+ children: h('ul', { class: 'warning-list' }, warnings.map((warning) => h('li', null, warning))),
289
+ })]));
290
+
291
+ /* Facts */
292
+ setText(phaseFact.value, t(phaseKey(phase)));
293
+ setText(addressFact.value, pairing && typeof pairing.address === 'string' ? ADDRESS_MASK : '—');
294
+ const mappings = targetsOf(state).map(parseMapping);
295
+ setText(targetsFact.value, mappings.length > 0
296
+ ? mappings.map((row) => row.virtual).join(t('listJoin'))
297
+ : t('factTargetsEmpty'));
298
+ targetsFact.value.title = mappings.map((row) => `${row.virtual}:${row.target}`).join('\n');
299
+ setText(keyModeFact.value, settings.addressMode === 'ephemeral' ? t('factModeEphemeral') : t('factModeStable'));
300
+ setText(formatFact.value, settings.fullAddress === true ? t('factFullAddress') : t('factShortAddress'));
301
+ const region = formatRegion(tailcat.region);
302
+ setText(regionFact.value, region !== '' ? region : '—');
303
+ const binary = tailcat.binary && typeof tailcat.binary === 'object' ? tailcat.binary : {};
304
+ const version = binary.version || installer.installedVersion || '';
305
+ setText(versionFact.value, version !== '' ? String(version) : '—');
306
+ setText(binaryFact.value, typeof binary.resolved === 'string' && binary.resolved !== '' ? binary.resolved : '—');
307
+ setText(restartsFact.value, String(Number.isFinite(Number(tailcat.restarts)) ? Number(tailcat.restarts) : 0));
308
+ setText(refusalsFact.value, String(Number.isFinite(Number(tailcat.refusals)) ? Number(tailcat.refusals) : 0));
309
+ const preferences = state.preferences && typeof state.preferences === 'object' ? state.preferences : {};
310
+ setText(stateFileFact.value, typeof preferences.file === 'string' ? preferences.file : '—');
311
+
312
+ /* Program */
313
+ const system = installer.system && typeof installer.system === 'object' ? installer.system : {};
314
+ const found = system.status === 'found';
315
+ const checking = system.status === 'checking';
316
+ const compatible = system.compatible !== false;
317
+ const installed = installer.installed === true;
318
+ const systemVersion = typeof system.version === 'string' ? system.version : t('unknown');
319
+ const systemPath = typeof system.path === 'string' ? system.path : '—';
320
+ if (checking) {
321
+ setText(programStatus, t('programChecking'));
322
+ setText(programDetail, '');
323
+ } else if (found && !compatible) {
324
+ setText(programStatus, t('programOutdated', { version: systemVersion }));
325
+ setText(programDetail, t('programOutdatedBody'));
326
+ } else if (found) {
327
+ setText(programStatus, t('programFound'));
328
+ setText(programDetail, t('programFoundBody', { version: systemVersion, path: systemPath }));
329
+ } else if (installed) {
330
+ setText(programStatus, t('programInstalled'));
331
+ setText(programDetail, t('programInstalledBody', { path: installer.binaryPath || '—' }));
332
+ } else {
333
+ setText(programStatus, t('programMissing'));
334
+ setText(programDetail, t('programMissingBody'));
335
+ }
336
+ const programDotMissing = programMissing(state);
337
+ programPanel.classList.toggle('is-missing', programDotMissing);
338
+
339
+ const config = state.config && typeof state.config === 'object' ? state.config : {};
340
+ const configOverride = typeof config.binaryPath === 'string' && config.binaryPath !== '' && config.binaryPath !== 'tailcat';
341
+ const extras = [];
342
+ if (configOverride) extras.push(t('programConfigOverride', { path: config.binaryPath }));
343
+ if (installer.method === 'brew' && typeof installer.command === 'string' && installer.command !== '') {
344
+ extras.push(t('programBrew', { command: installer.command }));
345
+ extras.push(t('programBrewNote'));
346
+ } else if (installer.method === 'archive' && typeof installer.asset === 'string' && installer.asset !== '') {
347
+ extras.push(t('programDownload', { asset: installer.asset }));
348
+ }
349
+ if (installer.supported === false) {
350
+ extras.push(t('programUnsupported', { platform: installer.platform || '', arch: installer.arch || '' }));
351
+ }
352
+ syncSignature(programExtra, extras, () => extras.map((text) => h('p', { class: 'program-note' }, text)));
353
+
354
+ const installBusy = installer.busy === true || ctx.busy() === 'install';
355
+ const showInstall = !configOverride && installer.supported !== false && (found ? !compatible : true);
356
+ setHidden(installButton, !showInstall);
357
+ setDisabled(installButton, installBusy);
358
+ const installLabel = found && !compatible
359
+ ? t('programUpgrade')
360
+ : installed
361
+ ? t('programReinstall')
362
+ : t('programInstall');
363
+ setText(installButton, installBusy ? t('programInstalling') : installLabel);
364
+ setDisabled(redetectButton, installBusy || checking);
365
+ if (typeof installer.manualUrl === 'string' && installer.manualUrl !== '' && manualLink.href !== installer.manualUrl) {
366
+ manualLink.href = installer.manualUrl;
367
+ }
368
+
369
+ const installError = typeof installer.error === 'string' && installer.error !== '' ? installer.error : '';
370
+ syncSignature(installErrorSlot, installError, () => (installError === ''
371
+ ? []
372
+ : [alertBox({ tone: 'danger', title: t('programError'), children: h('p', { class: 'error-text' }, installError) })]));
373
+
374
+ const progress = installer.progress && typeof installer.progress === 'object' ? installer.progress : null;
375
+ setHidden(progressBox, !(installBusy && progress));
376
+ if (installBusy && progress) {
377
+ const received = Number(progress.received);
378
+ const total = Number(progress.total);
379
+ let percent = Number(progress.percent);
380
+ if (!Number.isFinite(percent)) percent = total > 0 && Number.isFinite(received) ? Math.round((received / total) * 100) : Number.NaN;
381
+ const clamped = Number.isFinite(percent) ? Math.min(100, Math.max(0, percent)) : 0;
382
+ progressBar.style.width = `${clamped}%`;
383
+ const phaseText = t(INSTALL_PHASE_KEYS[progress.phase] || 'programPhaseDownload');
384
+ const size = Number.isFinite(received) && Number.isFinite(total) && total > 0
385
+ ? t('programProgress', { percent: clamped, received: formatBytes(received), total: formatBytes(total) })
386
+ : Number.isFinite(received) && received > 0
387
+ ? t('programProgressUnknown', { received: formatBytes(received) })
388
+ : '';
389
+ setText(progressText, size === '' ? phaseText : `${phaseText} ${size}`);
390
+ }
391
+
392
+ /* Connections */
393
+ const recent = Array.isArray(tailcat.recent) ? tailcat.recent : [];
394
+ const connections = recent.filter((entry) => entry && entry.kind !== 'refused');
395
+ const refusals = recent.filter((entry) => entry && entry.kind === 'refused');
396
+ syncSignature(connectionsSlot, connections, () => (connections.length === 0
397
+ ? [h('p', { class: 'empty-line' }, t('connectionsEmpty'))]
398
+ : [h('table', { class: 'data-table' },
399
+ h('thead', null,
400
+ h('tr', null,
401
+ h('th', { scope: 'col' }, t('connTime')),
402
+ h('th', { scope: 'col' }, t('connPeer')),
403
+ h('th', { scope: 'col' }, t('connPort')),
404
+ h('th', { scope: 'col' }, t('connVia')),
405
+ h('th', { scope: 'col' }, t('connDuration')))),
406
+ h('tbody', null, connections.map((entry) => connectionRow(t, lang, entry))))]));
407
+ syncSignature(refusalsSlot, refusals, () => (refusals.length === 0
408
+ ? [h('p', { class: 'empty-line' }, t('refusalsEmpty'))]
409
+ : [h('table', { class: 'data-table' },
410
+ h('thead', null,
411
+ h('tr', null,
412
+ h('th', { scope: 'col' }, t('refusalTime')),
413
+ h('th', { scope: 'col' }, t('refusalPeer')),
414
+ h('th', { scope: 'col' }, t('refusalReason')),
415
+ h('th', { scope: 'col' }, t('refusalPort')))),
416
+ h('tbody', null, refusals.map((entry) => refusalRow(t, lang, entry))))]));
417
+
418
+ /* Log */
419
+ const stderr = Array.isArray(tailcat.stderrTail) ? tailcat.stderrTail.filter((line) => typeof line === 'string') : [];
420
+ const logTextValue = stderr.length > 0 ? stderr.join('\n') : t('logEmpty');
421
+ setText(logText, logTextValue);
422
+ }
423
+
424
+ update(ctx.state());
425
+ return { el, update };
426
+ }