import { formatSize } from "@yaakapp-internal/lib/formatSize"; import { useQueryClient } from "@tanstack/react-query"; import type { Flow, PassthroughResponse } from "@yaakapp-internal/proxy-lib"; import { Button, Dropdown, formatMillis, Icon, IconButton, type IconProps, InlineCode, Tooltip, CONTROL_TOOLTIP_DELAY, } from "@yaakapp-internal/ui"; import classNames from "classnames"; import { type ReactNode, useEffect, useState } from "react"; import { canTrustHere, useDecrypt, usePassthrough, useTrustCertificate } from "../hooks/useDecrypt"; import { useDecryptList } from "../hooks/useDecryptList"; import { useRpcMutation } from "../hooks/useRpcMutation"; import { useRpcQuery } from "../hooks/useRpcQuery"; import { type CardAction, connectionOf, encryptedCard, isOpen, isRejection, type Line, } from "../lib/encryptedCard"; import { useFlowActions } from "../lib/flowActions"; import { utcTimestamp } from "../lib/flowViews"; import { useShowToast } from "../lib/toast"; import { AppIcon } from "./AppIcon"; import { useCopyEnvironment } from "./ProxyStatus"; /** * The card for HTTPS the proxy did not decrypt, or a client's refused handshake: the flow card's * header lines, with ENCRYPTED or REJECTED where the status goes, then why in one line and the * changes that would decrypt the next connection like it, each showing the entry it writes. */ export function EncryptedCard({ flow }: { flow: Flow }) { const list = useDecryptList(); const caStatus = useRpcQuery("ca_status", {}); const passthrough = usePassthrough(); const rejection = isRejection(flow); // A tunnel passed through after a rejection names the client that refused. const rejectedById = flow.tlsRejected == null ? flow.tunnelRejectedBy : null; const rejectedBy = useRpcQuery( "get_flows", { ids: rejectedById == null ? [] : [rejectedById] }, { enabled: rejectedById != null }, ).data?.[0]; const model = encryptedCard({ flow, list, trusted: caStatus.data?.trusted ?? null, canTrust: canTrustHere(), passthrough, rejectedBy, }); const { apply, trusting } = useDecrypt(); const trust = useTrustCertificate(); const copyEnvironment = useCopyEnvironment(); const showToast = useShowToast(); const fail = (err: Error) => showToast({ id: "encrypted-card", color: "danger", message: err.message }); const queryClient = useQueryClient(); // Read from the answer too: an engine the CLI owns tells this app nothing of the change. const forget = useRpcMutation("forget_passthrough", { onError: fail, onSuccess: ({ addresses }) => queryClient.setQueryData(["list_passthrough", {}], { addresses }), }); const reconnect = useRpcMutation("reconnect_tunnels", { onError: fail }); const connection = connectionOf(flow); const [busy, setBusy] = useState(null); const run = async (action: CardAction) => { switch (action.kind) { case "decrypt": case "stop": if (action.edit == null) return; setBusy(action.label); await apply({ edit: action.edit, message: action.toast?.message ?? action.label, sub: action.toast?.sub, }); setBusy(null); return; case "reconnect": reconnect.mutate({ flowId: flow.id }); return; case "trust": trust.mutate({}); return; case "try_again": forget.mutate({ address: connection.address }); return; case "copy_environment": await copyEnvironment(); forget.mutate({ address: connection.address }); return; } }; return (
{connection.host.includes(":") ? `[${connection.host}]` : connection.host} :{connection.port}
{model.actions.map((action) => ( void run(action)} /> ))} {model.foot.length > 0 && (
{model.foot.join(" ")}
)} {flow.error != null && flow.tlsRejected == null && (
{flow.error}
)}
); } /** What the info button beside the why line says */ const ABOUT = "HTTPS is encrypted between the app and the server. Yaak Proxy shows requests only for hosts and apps you decrypt, from their next connection on."; /** A why or fact line, its hosts, apps and entries shown as code */ function LineText({ line }: { line: Line }) { return ( <> {line.map((part, i) => typeof part === "string" ? ( // oxlint-disable-next-line react/no-array-index-key -- the parts of one fixed line {part} ) : "bold" in part ? ( // oxlint-disable-next-line react/no-array-index-key -- the parts of one fixed line {part.bold} ) : ( // oxlint-disable-next-line react/no-array-index-key -- the parts of one fixed line {part.code} ), )} ); } const ICONS: Record, IconProps["icon"]> = { host: "globe", everything: "layers", lock: "lock", lock_open: "lock_open", refresh: "refresh", shield: "shield", terminal: "square_terminal", }; function ActionButton({ action, flow, loading, disabled, onClick, }: { action: CardAction; flow: Flow; loading: boolean; disabled: boolean; onClick: () => void; }) { const icon: ReactNode = action.icon === "app" ? ( flow.clientPath == null ? ( ) : ( ) ) : ( ); return ( ); } /** ENCRYPTED or REJECTED, the duration, or how long it has been open, and the bytes each way */ function StatusLine({ flow, rejected }: { flow: Flow; rejected: boolean }) { const flowActions = useFlowActions(); const open = isOpen(flow); const openFor = useOpenFor(flow, open); return (
{rejected ? "REJECTED" : "ENCRYPTED"} • {open ? ( open {openFor == null ? "" : formatMillis(openFor)} ) : ( <> {flow.elapsedMs == null ? "--" : formatMillis(flow.elapsedMs)} • {formatSize(flow.requestSize)} {formatSize(flow.responseSize)} )}
); } /** How long an open tunnel has been open, ticking each second */ function useOpenFor(flow: Flow, open: boolean): number | null { const [now, setNow] = useState(() => Date.now()); useEffect(() => { if (!open) return; const timer = setInterval(() => setNow(Date.now()), 1000); return () => clearInterval(timer); }, [open]); const started = Date.parse(utcTimestamp(flow.startedAt)); return Number.isFinite(started) ? Math.max(0, now - started) : null; }