import { useQueryClient } from "@tanstack/react-query"; import type { CaStatus, SystemProxyStatus } from "@yaakapp-internal/proxy-lib"; import { Button, Checkbox, Dropdown, type DropdownItem, type DropdownRef, Icon, LoadingIcon, } from "@yaakapp-internal/ui"; import classNames from "classnames"; import { useAtom, useAtomValue } from "jotai"; import type { HTMLAttributes, ReactNode, Ref } from "react"; import { useEffect, useRef, useState } from "react"; import { useRpcEvent } from "../hooks/useRpcEvent"; import { useRpcMutation } from "../hooks/useRpcMutation"; import { useRpcQuery } from "../hooks/useRpcQuery"; import { helperRegisteredAtAtom } from "../lib/atoms"; import { encryptingPercent } from "../lib/encrypting"; import { environmentExports } from "../lib/env"; import { proxyStatusOf } from "../lib/proxyStatus"; import { proxyStateAtom, systemProxyEverOnAtom } from "../lib/store"; import { helperPollInterval, recordingLabel, systemProxySwitch } from "../lib/systemProxy"; import { onWindowFocus } from "../lib/tauri"; import { useShowToast } from "../lib/toast"; /** * The header's state pill and its menu: the listening address, which copies itself, the state, * Pause or Resume Recording, Override system proxy, and the certificate. The engine runs for as * long as the app does, so the pill shows whether flows are being stored. A certificate that is not trusted steps out of the * menu beside the pill until it is. Everything set once and left alone lives in Settings. */ export function ProxyStatus() { const dropdownRef = useRef(null); const [copied, setCopied] = useCopied(); const proxyState = useAtomValue(proxyStateAtom); const setup = useProxySetup(); const execute = useRpcMutation("execute_action"); const retry = useRpcMutation("take_over"); const { caStatus, trust, systemProxy, setSystemProxy } = setup; const systemSwitch = useSystemProxySwitch(setup); const running = proxyState?.state === "running"; const recording = proxyState?.recording !== false; const other = proxyState?.otherOwner ?? null; const port = proxyState?.port ?? other?.proxyPort ?? null; const untrusted = caStatus.data != null && !caStatus.data.trusted; // The same status the header's tint takes its colour from const status = proxyStatusOf(proxyState); const encrypting = status === "encrypting" ? encryptingPercent(proxyState) : null; const stateRow = status === "encrypting" ? ( } title={`Encrypting stored traffic, ${encrypting}`} description="Traffic captured before this version is being encrypted on disk. Recording starts when it's done." /> ) : status === "cli" && other != null ? ( } title="Running from the CLI" description={`Started by yaak proxy start, pid ${other.pid}, since ${timeOf(other.startedAt)}.${other.proxyPort == null ? "" : ` Port ${other.proxyPort}.`}`} /> ) : status === "recording" || status === "paused" ? ( } title={recording ? `Recording on port ${port}` : `Paused on port ${port}`} description={ recording ? "Every flow is stored as it passes." : "Traffic passes through untouched, and nothing is stored." } /> ) : ( } title="Not running" description={ {retry.error?.message ?? proxyState?.startError ?? "The proxy has not started."} } action={ } /> ); // Until the helper is approved, every change asks for the password; said where it is felt. const helperNotice = systemProxy.data != null && systemProxy.data.backend !== "helper" && systemProxy.data.helper !== "enabled" && systemProxy.data.helper !== "unavailable"; const address = port == null ? null : `127.0.0.1:${port}`; const items: DropdownItem[] = [ { label: {address}, hidden: address == null, keepOpenOnSelect: true, rightSlot: copied ? ( Copied ) : ( ), onSelect: () => { if (address == null) return; navigator.clipboard.writeText(address).then(setCopied, console.error); }, }, { type: "separator", hidden: address == null }, { type: "content", label: stateRow }, { label: recordingLabel(recording), icon: recording ? "circle_pause" : "circle_play", hidden: !running, hotKeyAction: "recording_toggle", hotKeyLabelOnly: true, onSelect: () => execute.mutate({ scope: "global", action: "recording_toggle" }), }, { type: "separator" }, { type: "content", hidden: systemProxy.data?.supported !== true, label: ( } title="Override system proxy" description={ <> {systemSwitch.description} {helperNotice && ( macOS asks for your password each time until the helper is approved in Settings. )} } action={ setSystemProxy.mutate({ enabled })} /> } /> ), }, { type: "content", label: caStatus.data?.trusted ? ( } title="Certificate trusted" description="Yaak Proxy CA, in your login keychain." /> ) : ( } title="Certificate not trusted" description={ trust.error != null ? ( {trust.error.message} ) : trust.isPending ? ( "Waiting for your password in the macOS prompt." ) : ( "So HTTPS reads as plain text. macOS asks for your login password once." ) } action={ } /> ), }, ]; return ( <> {status === "encrypting" ? ( <> Encrypting {encrypting} ) : status === "cli" && other != null ? ( <> CLI ) : status === "recording" || status === "paused" ? ( <> {recording ? "Recording" : "Paused"} ) : ( <> Not running )} {untrusted && ( dropdownRef.current?.open()}> Certificate not trusted )} ); } /** * The certificate and the system proxy, read once and shared by the header and the first-run * card, with the actions that change them. */ export function useProxySetup() { const queryClient = useQueryClient(); const caStatus = useRpcQuery("ca_status", {}); const registeredAt = useAtomValue(helperRegisteredAtAtom); const systemProxy = useRpcQuery( "system_proxy_status", {}, { // The helper is approved in System Settings, out of the app's sight. refetchInterval: (query) => helperPollInterval(query.state.data?.helper, registeredAt, Date.now()), }, ); // The user comes back from System Settings, maybe having just allowed the helper. const { refetch: refetchSystemProxy } = systemProxy; useEffect(() => onWindowFocus(() => void refetchSystemProxy()), [refetchSystemProxy]); const trust = useRpcMutation("ca_trust", { mutationKey: ["ca_trust"], onSuccess: (status) => queryClient.setQueryData(["ca_status", {}], status), }); const setSystemProxy = useRpcMutation("system_proxy_set", { mutationKey: ["system_proxy_set"], onSuccess: (status) => { queryClient.setQueryData(["system_proxy_status", {}], status); }, }); // The engine turns the system proxy on as it starts, and says so once the prompt is // answered, and turns it off as it stops. useRpcEvent("proxy_state_changed", () => void systemProxy.refetch()); // Setup counts as done once the system proxy has been on, whatever it is now. const [everOn, setEverOn] = useAtom(systemProxyEverOnAtom); const on = systemProxy.data?.enabled === true; useEffect(() => { if (on && !everOn) setEverOn(true); }, [on, everOn, setEverOn]); return { caStatus, trust, systemProxy, setSystemProxy }; } /** The system proxy switch's state, from where it points and the change in flight */ export function useSystemProxySwitch(setup: ReturnType) { const proxyState = useAtomValue(proxyStateAtom); const { systemProxy, setSystemProxy } = setup; return systemProxySwitch({ routing: systemProxy.data?.enabled === true, port: proxyState?.port ?? proxyState?.configuredPort ?? null, pending: setSystemProxy.isPending ? (setSystemProxy.variables?.enabled ?? null) : null, error: setSystemProxy.error?.message ?? null, helper: systemProxy.data?.backend === "helper", }); } /** The privileged helper: whether it is installed and approved, and which way changes go */ export function useCopyEnvironment() { const proxyState = useAtomValue(proxyStateAtom); const caStatus = useRpcQuery("ca_status", {}); const showToast = useShowToast(); return async () => { const port = proxyState?.port ?? proxyState?.otherOwner?.proxyPort ?? proxyState?.configuredPort; const caPath = caStatus.data?.certPath; if (port == null || caPath == null) return; try { await navigator.clipboard.writeText(environmentExports(port, caPath)); showToast({ message: "Copied the environment. Paste it into a terminal to proxy what runs there.", }); } catch (err) { showToast({ message: `Failed to copy: ${String(err)}`, color: "danger" }); } }; } /** Whether something was just copied, for a moment, and the call that says it was */ function useCopied(): [boolean, () => void] { const [copied, setCopied] = useState(false); useEffect(() => { if (!copied) return; const timeout = setTimeout(() => setCopied(false), 1500); return () => clearTimeout(timeout); }, [copied]); return [copied, () => setCopied(true)]; } /** Keeps a click on a control inside a menu row from closing the menu, as a row's click does */ function KeepMenuOpen({ children, className }: { children: ReactNode; className?: string }) { return ( // oxlint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions -- only stops the click reaching the row e.stopPropagation()}> {children} ); } function timeOf(iso: string): string { return new Date(iso).toLocaleTimeString([], { hour: "numeric", minute: "2-digit" }); } interface PillProps extends HTMLAttributes { color?: "warning"; ref?: Ref; } function Pill({ children, color, ref, ...props }: PillProps) { return ( ); } export function StateDot({ state }: { state: "recording" | "paused" | "cli" }) { return ( ); } function MenuRow({ icon, title, description, action, }: { icon: ReactNode; title: string; description: ReactNode; action?: ReactNode; }) { return (
{icon}
{title}
{description}
{action}
); }