File
Blob: ui/operator/App.svelte
| 1 | <script lang="ts"> |
| 2 | import { createQuery } from '@tanstack/svelte-query'; |
| 3 | import Button from '../shared/Button.svelte'; |
| 4 | import Shell from '../shared/Shell.svelte'; |
| 5 | import { RequestError, requestJSON } from '../shared/http'; |
| 6 | import Overview from './Overview.svelte'; |
| 7 | import Clients from './Clients.svelte'; |
| 8 | import ClientTunnels from './ClientTunnels.svelte'; |
| 9 | import DNSConfiguration from './DNSConfiguration.svelte'; |
| 10 | import WAFConfiguration from './WAFConfiguration.svelte'; |
| 11 | import type { ClientList, Overview as OverviewData, Snapshot, TunnelDetail } from './types'; |
| 12 | |
| 13 | const path = window.location.pathname; |
| 14 | const kind = /^\/_internal\/config\/dns\/?$/.test(path) |
| 15 | ? 'dns' |
| 16 | : /^\/_internal\/config\/waf\/?$/.test(path) |
| 17 | ? 'waf' |
| 18 | : /^\/_internal\/tun\/.+/.test(path) |
| 19 | ? 'tunnels' |
| 20 | : /^\/_internal\/tun\/?$/.test(path) |
| 21 | ? 'clients' |
| 22 | : 'overview'; |
| 23 | const endpoint = kind === 'overview' |
| 24 | ? '/_internal/overview.json' |
| 25 | : path.replace(/^\/_internal\/tun/, '/_internal/api/tun'); |
| 26 | const title = kind === 'dns' ? 'ACME DNS' : kind === 'waf' ? 'Web protection' : kind === 'overview' ? 'Server overview' : kind === 'clients' ? 'Connected clients' : 'Client tunnels'; |
| 27 | const navigation = [ |
| 28 | { label: 'Overview', href: '/_internal/', active: kind === 'overview' }, |
| 29 | { label: 'Clients', href: '/_internal/tun/', active: kind === 'clients' || kind === 'tunnels' }, |
| 30 | { label: 'ACME DNS', href: '/_internal/config/dns', active: kind === 'dns' }, |
| 31 | { label: 'Web protection', href: '/_internal/config/waf', active: kind === 'waf' }, |
| 32 | { label: 'Endpoint reference', href: '/_internal/endpoints' }, |
| 33 | ]; |
| 34 | |
| 35 | const query = createQuery(() => ({ |
| 36 | queryKey: ['operator', 'snapshot', endpoint], |
| 37 | enabled: kind !== 'dns' && kind !== 'waf', |
| 38 | queryFn: async ({ signal }): Promise<Snapshot> => { |
| 39 | const data = await requestJSON<unknown>(endpoint, { signal }); |
| 40 | if (!data || typeof data !== 'object' || !Array.isArray((data as Record<string, unknown>)[kind === 'overview' ? 'vnodes' : kind === 'clients' ? 'clients' : 'tunnels'])) { |
| 41 | throw new Error('Unexpected server response.'); |
| 42 | } |
| 43 | return kind === 'overview' |
| 44 | ? { kind, data: data as OverviewData } |
| 45 | : kind === 'clients' |
| 46 | ? { kind, data: data as ClientList } |
| 47 | : { kind: 'tunnels', data: data as TunnelDetail }; |
| 48 | }, |
| 49 | })); |
| 50 | const snapshot = $derived(query.data); |
| 51 | const loading = $derived(query.isFetching); |
| 52 | const error = $derived(loading ? '' : query.error instanceof RequestError && query.error.status === 401 |
| 53 | ? 'Authentication required (401).' |
| 54 | : query.error instanceof RequestError && query.error.status === 403 ? 'Access denied (403).' : query.error?.message ?? ''); |
| 55 | </script> |
| 56 | |
| 57 | <svelte:head><title>{title} · Specter</title></svelte:head> |
| 58 | |
| 59 | <Shell label="Operator" home="/_internal/" {navigation}> |
| 60 | {#if kind === 'dns'} |
| 61 | <DNSConfiguration /> |
| 62 | {:else if kind === 'waf'} |
| 63 | <WAFConfiguration /> |
| 64 | {:else} |
| 65 | {#if kind === 'tunnels'} |
| 66 | <a class="mb-2 inline-flex min-h-10 items-center text-accent-300 underline underline-offset-4 hover:text-accent-100" href="/_internal/tun/">← Back to clients</a> |
| 67 | {/if} |
| 68 | <div class="mb-2 flex flex-wrap items-center justify-between gap-3"> |
| 69 | <h1 class="text-[22px] leading-tight font-semibold tracking-tight sm:text-2xl">{title}</h1> |
| 70 | <div class="flex flex-wrap items-center gap-2"> |
| 71 | {#if kind === 'overview'} |
| 72 | <a class="inline-flex min-h-10 items-center rounded-md border border-zinc-500 bg-surface px-3 py-1.5 text-sm leading-5 font-semibold text-foreground hover:border-zinc-400 hover:bg-zinc-700" href={endpoint}>View JSON</a> |
| 73 | {/if} |
| 74 | <Button variant="secondary" onclick={() => query.refetch()} disabled={loading}>{loading ? 'Refreshing…' : 'Refresh'}</Button> |
| 75 | </div> |
| 76 | </div> |
| 77 | {#if error} |
| 78 | <div class="my-4 rounded-md border border-danger/40 bg-surface px-4 py-3" role="alert"> |
| 79 | <p class="font-medium text-danger">{snapshot ? 'Refresh failed. Showing previous snapshot.' : 'Could not load this view.'}</p> |
| 80 | <details class="mt-1"> |
| 81 | <summary class="min-h-8 cursor-pointer py-1 text-muted">Request error</summary> |
| 82 | <p class="mt-1 max-w-[72ch] font-mono text-xs wrap-anywhere">{error}</p> |
| 83 | </details> |
| 84 | </div> |
| 85 | {/if} |
| 86 | <div aria-busy={loading}> |
| 87 | {#if snapshot?.kind === 'overview'} |
| 88 | <Overview data={snapshot.data} /> |
| 89 | {:else if snapshot?.kind === 'clients'} |
| 90 | <Clients data={snapshot.data} /> |
| 91 | {:else if snapshot?.kind === 'tunnels'} |
| 92 | <ClientTunnels data={snapshot.data} /> |
| 93 | {:else if loading} |
| 94 | <div class="mt-5 space-y-3" role="status"> |
| 95 | <span class="sr-only">Loading {title.toLowerCase()}…</span> |
| 96 | <div class="h-4 w-48 max-w-full rounded bg-surface" aria-hidden="true"></div> |
| 97 | <div class="h-36 rounded-md border border-line bg-chrome" aria-hidden="true"></div> |
| 98 | </div> |
| 99 | {/if} |
| 100 | </div> |
| 101 | {/if} |
| 102 | </Shell> |