// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The Network applet (Applet.tsx): the guest's network and its Tailscale // node together. // - The item (#tb-net): a globe and two bars, received and sent, on a log // scale from NET_LO to NET_HI B/s (stats.ts's light samples, empty tracks // before the first one or while the guest is not read); the globe struck // through and the item dim when the network is off this boot. While // Tailscale is set up and not off (TailscalePanel.tsx tailscaleBar()), its // mark follows them (in a span with data-ts, the kind): its shape and the // item's tone follow tailscale.ts tailscaleKind() (amber when it needs the // user: a login, an approval, a link's settings, an exit node it cannot // use; red on an error). From sm up only, but always while Tailscale needs // the user. The tooltip has the rates and Tailscale's short state. // - The panel (#tb-net-panel, the full sampling rate while open), top to // bottom: the header (the globe, "Network", a pill only while the guest's // eth0 says it is down: Link down); the mode (#tb-net-mode), how the // guest's network works, as three segments (radios), Wisp tunnel // (#tb-net-mode-wisp: the network on, Tailscale off), Off (-off: no network // device) and Tailscale (-tailscale: the network on and the Tailscale // option on). They are boot options (Settings, Network: Options.net and // Options.tailscale), so the mode this boot runs (config: cfg.net, // cfg.tailscaleParam) is drawn filled, and a different one picked for the // next boot is the checked radio, drawn with a dashed amber outline, with // "Applies when you reboot" and Reboot to apply (#tb-net-reboot, // ui.confirmReboot(), which asks while the system runs) under the row; // picking this boot's mode again takes the change back. A pick sets the // options at once (ui.setOption(), as Settings' controls do; Wisp keeps // custom servers typed in Settings) and keeps the panel open; Tailscale // not set up (neither on this boot nor a node saved in this browser, as // Settings tells Setup from Configured) opens Settings at its setup // instead. The Tailscale segment carries a dot in the node's state // (tailscaleKind()) while it runs this boot, and is amber while an exit // node it cannot use blocks the guest's internet. The Tailscale mode is // not the internet's path: without an exit node the guest's internet still // goes through Wisp and only the tailnet through Tailscale, so under the // row a small sketch of the path (#tb-net-path, PathSketch below) shows // where the traffic goes: Wisp and its server (or the fastest of several), // Tailscale's exit node by name, or that the exit node blocks it (amber), // with the tailnet as a branch. PATH_SKETCH false puts a line in its place // (#tb-net-via) that says the same in words. // Then the traffic: the // current rates in and out, a sparkline of both (the last three minutes, // with its scale and a crosshair), then the facts: the interfaces and their // state (guest operstate; no addresses: the page does not read them), the // totals since boot and the plain-HTTP hosts the page answers with // fetch() (config.ts DEFAULT_ROUTES); nothing of it without the network. // Then Tailscale's section (TailscalePanel.tsx TailscaleSection: // a compact row while it is off and not set up, else its state, facts, // devices, exit node and actions). The header's Monitor (System's // Overview) and Settings (Settings, Network) icon buttons. import { fmtBytes, fmtRate, fmtRateShort } from "../../format.ts"; import { KW } from "../../kw.ts"; import { DEFAULT_WISP_SERVERS } from "../../net.ts"; import { useStats } from "../../stats.ts"; import { useStore, type Options, type TailscaleView } from "../../store.ts"; import { exitBlocked, exitLine, exitName, type TailscaleKind } from "../../tailscale.ts"; import { SECOND, Spark, TONE } from "../Charts.tsx"; import { Sep, type PopoverProps } from "../ControlCenter.tsx"; import type { MenuItem } from "../Menu.tsx"; import { NetIcon, TailscaleMark } from "../icons.tsx"; import { openTailscaleSettings, TailscaleSection, tailscaleBar } from "../TailscalePanel.tsx"; import { AppletButton, AppletPanel, Bars, ICON, type AppletItemProps, type AppletTone } from "./Applet.tsx"; /** Network rates map to a bar's fill on a log scale: NET_LO B/s and below empty, NET_HI B/s and above full. */ const NET_LO = 100, NET_HI = 10485760; const netLevel = (n: number | null) => (n == null ? null : Math.min(1, Math.max(0, Math.log10(Math.max(1, n) / NET_LO) / Math.log10(NET_HI / NET_LO)))); /** Wide enough for Tailscale's IPv6 address and a DERP region's name on one line. */ const NET_W = 380; /** Why there are no figures: not sampled (and why), or no sample yet. */ const whyNot = (idle: string | null | undefined, short = false) => (idle ? (short ? `not sampled (${idle})` : `Not sampled: ${idle}`) : short ? "waiting for the guest" : "Waiting for the guest…"); export function NetworkItem(props: AppletItemProps) { const st = useStats("lite"); const v = useStore((s) => s.tailscale); const net = KW.config.net; const g = !st.waiting && !st.idle ? st.guest : null; const rx = g?.derived.rx ?? null, tx = g?.derived.tx ?? null; const ts = tailscaleBar(v); const netText = !net ? "Network: off" : rx != null && tx != null ? `Network ↓ ${fmtRateShort(rx)} · ↑ ${fmtRateShort(tx)}` : `Network: on; ${whyNot(st.idle, true)}`; const title = ts.shown ? `${netText}\nTailscale: ${ts.short}` : netText; const tone: AppletTone | undefined = !ts.shown ? undefined : ts.kind === "attention" ? "warn" : ts.kind === "error" ? "err" : undefined; return ( {net && } {ts.shown && } ); } /** A URL's host (as written when it does not parse). */ function hostOf(u: string): string { try { return new URL(u).host || u; } catch { return u; } } /** Where the guest's internet goes this boot, as far as the page knows (#tb-net-via); null without the network. */ function internet(v: TailscaleView): { text: string; title: string; warn?: boolean } | null { const cfg = KW.config; if (!cfg.net) return null; if (exitBlocked(v)) return { text: `Internet blocked: exit node ${exitName(v)}`, title: `Tailscale exit node ${exitName(v)}: ${exitLine(v)}`, warn: true }; if (v.state === "running" && v.exitNode?.usable) return { text: `Internet through exit node ${exitName(v)}`, title: `Tailscale exit node ${exitName(v)}: ${exitLine(v)}` }; const servers = cfg.wisp ?? DEFAULT_WISP_SERVERS; const title = `Wisp: ${servers.join(", ")}${servers.length > 1 ? " (the first to answer)" : ""}`; const wisp = servers.length === 1 ? hostOf(servers[0]!) : `the fastest of ${servers.length} servers`; return { text: `Internet through Wisp, ${wisp}${cfg.tailscaleParam ? "; the tailnet through Tailscale" : ""}`, title }; } /** The path sketch (#tb-net-path) in place of the line under the mode row (#tb-net-via); false: the line. */ const PATH_SKETCH = true; /** Traffic (in and out together) above which the path's active links pulse, B/s. */ const PULSE_FLOOR = 1024; type Tone = "on" | "idle" | "pending" | "warn" | "err"; const NODE: Record = { on: "border-ink/20 text-fg", idle: "border-ink/12 text-faint", pending: "border-dashed border-ink/25 text-muted", warn: "border-warn/60 text-warn", err: "border-err/55 text-err", }; const LINK: Record = { on: "bg-ink/40", idle: "bg-ink/15", pending: "bg-ink/25", warn: "bg-warn/70", err: "bg-err/60" }; /** A hop of the path: a small pill, its short label, the whole value as its title. */ function Hop({ label, title, tone = "on", className = "" }: { label: string; title: string; tone?: Tone; className?: string }) { return {label}; } /** A link between two hops: a line (dashed while pending), pulsing with traffic; `cut`: broken, with a cross. */ function Link({ tone = "on", cut, pulse }: { tone?: Tone; cut?: boolean; pulse?: boolean }) { return (