Privileged helper for system proxy (#796)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Gregory SchierandClaude Opus 5.5 authored and GitHub committed 2026-10-30 00:41:00 -07:00
1 parent 4d38a482f6
commit f7aea62c07
33 files changed
+3536 -176

No files matched your search

@@ -0,0 +1,96 @@
import { useQueryClient } from "@tanstack/react-query";
import type { SystemProxyStatus } from "@yaakapp-internal/proxy-lib";
import { Banner, Dialog, DialogFooter, LoadingIcon } from "@yaakapp-internal/ui";
import { useAtom, useSetAtom } from "jotai";
import { useEffect } from "react";
import loginItems from "../assets/login-items.png";
import { useRpcMutation } from "../hooks/useRpcMutation";
import { useRpcQuery } from "../hooks/useRpcQuery";
import { helperDialogOpenAtom, helperRegisteredAtAtom } from "../lib/atoms";
/** Yaak Proxy's row in `login-items.png`, as percentages of the image, for the highlight */
const YAAK_PROXY_ROW = { left: 1, top: 34, width: 98, height: 32 };
/**
* Walks through allowing the helper: registering it opens System Settings, where the user turns
* it on. Stays open while macOS awaits that approval, and closes itself once it is given.
*/
export function HelperDialog() {
const [open, setOpen] = useAtom(helperDialogOpenAtom);
const queryClient = useQueryClient();
const setRegisteredAt = useSetAtom(helperRegisteredAtAtom);
// Polled by the header while approval is pending, so this sees it as soon as it is given.
const status = useRpcQuery("system_proxy_status", {});
const register = useRpcMutation("helper_register", {
onSuccess: (status) => {
setRegisteredAt(Date.now());
queryClient.setQueryData<SystemProxyStatus>(["system_proxy_status", {}], status);
},
});
const helper = status.data?.helper;
const waiting = helper === "requires_approval";
const { reset } = register;
useEffect(() => {
if (open && helper === "enabled") {
setOpen(false);
reset();
}
}, [open, helper, setOpen, reset]);
const close = () => {
setOpen(false);
reset();
};
return (
<Dialog
open={open}
onClose={close}
size="dynamic"
title="Turn on Yaak Proxy under Allow in the Background"
description="In System Settings > General > Login Items. Newer macOS calls the list Background App Activity."
className="w-[34rem]"
>
<div className="grid gap-3 pb-4">
<div className="relative">
<img
src={loginItems}
alt="System Settings, with Yaak Proxy's toggle in the list of apps allowed in the background"
className="block w-full rounded-md border border-border-subtle"
/>
<div
aria-hidden
className="absolute rounded-md ring-2 ring-primary bg-primary/10 pointer-events-none"
style={{
left: `${YAAK_PROXY_ROW.left}%`,
top: `${YAAK_PROXY_ROW.top}%`,
width: `${YAAK_PROXY_ROW.width}%`,
height: `${YAAK_PROXY_ROW.height}%`,
}}
/>
</div>
{register.error != null && <Banner color="danger">{register.error.message}</Banner>}
</div>
<DialogFooter
leftSlot={
waiting && (
<span className="flex items-center gap-2 text-sm text-text-subtle">
<LoadingIcon size="xs" />
Waiting for approval
</span>
)
}
actions={[
{ label: waiting ? "Close" : "Cancel", onClick: close },
{
label: "Open System Settings",
color: "primary",
isLoading: register.isPending,
onClick: () => register.mutate({}),
},
]}
/>
</Dialog>
);
}
@@ -18,6 +18,7 @@ import { FirstRun } from "./FirstRun";
import { FlowDetail } from "./FlowDetail";
import { FlowList } from "./FlowList";
import { ProxyStatus } from "./ProxyStatus";
import { HelperDialog } from "./HelperDialog";
import { SaveDialog } from "./SaveDialog";
import { selectedSidebarLabelAtom, Sidebar } from "./Sidebar";
import { TakeOverBanner } from "./TakeOverBanner";
@@ -139,6 +140,7 @@ export function ProxyLayout() {
</div>
</SidebarLayout>
<SaveDialog />
<HelperDialog />
<Toasts />
</div>
);
+91 -4
View File
@@ -9,15 +9,22 @@ import {
Icon,
} from "@yaakapp-internal/ui";
import classNames from "classnames";
import { useAtomValue, useStore } from "jotai";
import { useAtomValue, useSetAtom, useStore } from "jotai";
import type { HTMLAttributes, ReactNode, Ref } from "react";
import { useRef } from "react";
import { useEffect, useRef } from "react";
import { useRpcEvent } from "../hooks/useRpcEvent";
import { useRpcMutation } from "../hooks/useRpcMutation";
import { useRpcQuery } from "../hooks/useRpcQuery";
import { helperDialogOpenAtom, helperRegisteredAtAtom } from "../lib/atoms";
import { environmentExports } from "../lib/env";
import { proxyStateAtom, refreshProxyState } from "../lib/store";
import { recordingLabel, systemProxySwitch } from "../lib/systemProxy";
import {
helperPollInterval,
helperRow,
recordingLabel,
systemProxySwitch,
} from "../lib/systemProxy";
import { onWindowFocus } from "../lib/tauri";
import { useShowToast } from "../lib/toast";
/**
@@ -34,6 +41,7 @@ export function ProxyStatus() {
const setAtLaunch = useSetSystemProxyAtLaunch();
const setHttpVersion = useSetHttpVersion();
const copyEnvironment = useCopyEnvironment();
const setHelperDialogOpen = useSetAtom(helperDialogOpenAtom);
const { caStatus, trust, systemProxy, setSystemProxy } = setup;
const systemSwitch = useSystemProxySwitch(setup);
@@ -131,6 +139,19 @@ export function ProxyStatus() {
</KeepMenuOpen>
),
},
{
type: "content",
hidden: systemProxy.data?.supported !== true,
label: systemProxy.data != null && (
<HelperMenuRow
status={systemProxy.data}
onOpenDialog={() => {
dropdownRef.current?.close?.();
setHelperDialogOpen(true);
}}
/>
),
},
{
type: "content",
label: caStatus.data?.trusted ? (
@@ -253,7 +274,19 @@ export function ProxyStatus() {
export function useProxySetup() {
const queryClient = useQueryClient();
const caStatus = useRpcQuery("ca_status", {});
const systemProxy = useRpcQuery("system_proxy_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<CaStatus>(["ca_status", {}], status),
@@ -279,9 +312,63 @@ export function useSystemProxySwitch(setup: ReturnType<typeof useProxySetup>) {
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 */
function HelperMenuRow({
status,
onOpenDialog,
}: {
status: SystemProxyStatus;
onOpenDialog: () => void;
}) {
const queryClient = useQueryClient();
const unregister = useRpcMutation("helper_unregister", {
onSuccess: (status) =>
queryClient.setQueryData<SystemProxyStatus>(["system_proxy_status", {}], status),
});
const row = helperRow(status.helper, status.backend);
const error = unregister.error;
return (
<MenuRow
icon={
<Icon
icon={status.helper === "enabled" ? "badge_check" : "lock"}
size="sm"
color={status.helper === "enabled" ? "success" : "secondary"}
/>
}
title={row.title}
description={
error != null ? <span className="text-danger">{error.message}</span> : row.description
}
action={
row.action != null && (
<Button
size="2xs"
variant="border"
color={row.action === "unregister" ? "default" : "primary"}
isLoading={unregister.isPending}
onClick={(e) => {
e.stopPropagation();
if (row.action === "unregister") unregister.mutate({});
else onOpenDialog();
}}
>
{row.action === "install"
? "Install"
: row.action === "approve"
? "Approve"
: "Unregister"}
</Button>
)
}
/>
);
}
/** Saves whether the system proxy turns on as the app launches */
function useSetSystemProxyAtLaunch() {
const store = useStore();