feat: add launch guard and secure multi-user service ownership (#7569)

* feat(service): isolate macOS launch and owner runtime

* ci(windows): run owner security tests

* chore(deps): use kode-bridge v0.4.1 tag

* fix(macos): classify installed service state

* fix(macos): classify marked unavailable service

* fix(macos): gate core startup on service migration

* fix(macos): scope service init to supported platforms

* feat(macos): prompt before migrating installed service

* fix(macos): shorten sidecar IPC path

* build(service): pin helper protocol and artifacts

* fix(core): require confirmed service and sidecar readiness

* fix(macos): harden launch roots and singleton state

* fix(ui): refresh migration state before restart

* fix(macos): linearize service migration lifecycle

* fix(ui): keep service migration state fresh

* feat(service): track authenticated owner sessions

* fix(service): preserve newer owner sessions

* feat(macos): route service proxy changes through helper

* fix(macos): serialize service proxy guard

* fix(macos): close service proxy owner lifecycle

* fix(macos): close remaining proxy stop races

* fix(app): do not cancel controlled service stop

* fix(lifecycle): keep controlled updates cancellation-safe

* fix(macos): stage dev core for service

* test(macos): exercise staged core cleanup

* fix(dev): await controlled macOS exit

* chore: update changelog

* fix(macos): recover sidecar after service install failure

Allow the existing sidecar to continue when service installation leaves the manager in InstallRequired, then settle the session state as SidecarAllowed. Show neutral status text while the migration dialog waits for its final state.

* fix(macos): stage dev service tools before elevation

* fix(macos): localize launch guard prompts

* fix(service): address cross-platform integration issues

* feat: support seamless service migration

* fix: pass Windows service installer path safely

* fix(prebuild): derive service release from Cargo dependency

* fix(dev): preserve service installation state

* fix: resolve clippy warnings in service lifecycle
This commit is contained in:
wonfen
2026-07-24 09:01:49 +08:00
committed by GitHub
parent c079da6c99
commit 23dda47641
90 changed files with 9711 additions and 900 deletions
+21 -34
View File
@@ -1,46 +1,33 @@
import { useCallback } from 'react'
import { restartCore, stopCore, uninstallService } from '@/services/cmds'
import { uninstallService } from '@/services/cmds'
import { showNotice } from '@/services/notice-service'
const executeWithErrorHandling = async (
operation: () => Promise<void>,
loadingKey: string,
successKey?: string,
) => {
try {
showNotice.info(loadingKey)
await operation()
if (successKey) {
showNotice.success(successKey)
}
} catch (err) {
showNotice.error(err)
throw err
}
}
import { useSystemState } from './use-system-state'
export const useServiceUninstaller = () => {
const uninstallServiceAndRestartCore = useCallback(async () => {
const { mutateSystemState } = useSystemState()
const uninstallServiceAndStartSidecar = useCallback(async () => {
let uninstallError: unknown
showNotice.info('settings.statuses.clashService.uninstalling')
try {
await executeWithErrorHandling(
() => stopCore(),
'settings.statuses.clash.stopping',
)
await executeWithErrorHandling(
() => uninstallService(),
'settings.statuses.clashService.uninstalling',
await uninstallService()
showNotice.success(
'settings.feedback.notifications.clashService.uninstallSuccess',
)
} catch (ignore) {
} finally {
await executeWithErrorHandling(
() => restartCore(),
'settings.statuses.clash.restarting',
'settings.feedback.notifications.clash.restartSuccess',
)
} catch (error) {
uninstallError = error
}
}, [])
return { uninstallServiceAndRestartCore }
try {
await mutateSystemState()
} catch (error) {
if (!uninstallError) throw error
}
if (uninstallError) throw uninstallError
}, [mutateSystemState])
return { uninstallServiceAndStartSidecar }
}
+6 -2
View File
@@ -3,7 +3,7 @@ import { closeAllConnections } from 'tauri-plugin-mihomo-api'
import { useVerge } from '@/hooks/use-verge'
import { useClashConfigData, useSystemData } from '@/providers/app-data-context'
import { getAutotemProxy } from '@/services/cmds'
import { getAutotemProxy, getEmbeddedServerPort } from '@/services/cmds'
import { revalidateQueries, useQuery } from '@/services/query-client'
// 系统代理状态检测统一逻辑
@@ -17,6 +17,10 @@ export const useSystemProxyState = () => {
refetchOnWindowFocus: true,
refetchOnReconnect: true,
})
const { data: pacPort } = useQuery({
queryKey: ['getEmbeddedServerPort'],
queryFn: getEmbeddedServerPort,
})
const {
enable_system_proxy,
@@ -30,7 +34,7 @@ export const useSystemProxyState = () => {
const host = proxy_host || '127.0.0.1'
if (proxy_auto_config) {
if (!autoproxy?.enable) return false
const pacPort = import.meta.env.DEV ? 11233 : 33331
if (!pacPort) return false
return autoproxy.url === `http://${host}:${pacPort}/commands/pac`
} else {
if (!sysproxy?.enable) return false
+56 -13
View File
@@ -1,24 +1,58 @@
import { useEffect, useRef, useState } from 'react'
import { getRunningMode, isAdmin, isServiceAvailable } from '@/services/cmds'
import {
getRunningMode,
getServiceInstallState,
isAdmin,
isServiceAvailable,
type RunningMode,
type ServiceInstallState,
} from '@/services/cmds'
import { showNotice } from '@/services/notice-service'
import { useQuery } from '@/services/query-client'
import { useVerge } from './use-verge'
import { useVisibility } from './use-visibility'
interface SystemState {
runningMode: 'Sidecar' | 'Service'
export interface SystemState {
runningMode: RunningMode
isAdminMode: boolean
isServiceOk: boolean
serviceInstallState: ServiceInstallState | null
}
const defaultSystemState = {
runningMode: 'Sidecar',
isAdminMode: false,
isServiceOk: false,
serviceInstallState: null,
} as SystemState
export const fetchSystemState = async (): Promise<SystemState> => {
const [runningMode, isAdminMode, isServiceOk, serviceInstallState] =
await Promise.all([
getRunningMode(),
isAdmin(),
isServiceAvailable(),
getServiceInstallState().catch(() => null),
])
return {
runningMode,
isAdminMode,
isServiceOk,
serviceInstallState,
}
}
const isServiceChoicePending = (
state: ServiceInstallState | null,
serviceAvailable: boolean,
queryFailed: boolean,
) =>
queryFailed ||
state === 'sidecarAllowed' ||
!(state === 'notInstalled' || (state === 'ready' && serviceAvailable))
// Grace period for service initialization during startup
const STARTUP_GRACE_MS = 10_000
@@ -39,16 +73,10 @@ export function useSystemState() {
data: systemState = defaultSystemState,
refetch: mutateSystemState,
isLoading,
error: systemStateError,
} = useQuery({
queryKey: ['getSystemState'],
queryFn: async () => {
const [runningMode, isAdminMode, isServiceOk] = await Promise.all([
getRunningMode(),
isAdmin(),
isServiceAvailable(),
])
return { runningMode, isAdminMode, isServiceOk } as SystemState
},
queryFn: fetchSystemState,
refetchInterval: pageVisible ? (isStartingUp ? 2000 : 30000) : false,
refetchOnWindowFocus: true,
refetchOnReconnect: true,
@@ -57,6 +85,11 @@ export function useSystemState() {
const isSidecarMode = systemState.runningMode === 'Sidecar'
const isServiceMode = systemState.runningMode === 'Service'
const isTunModeAvailable = systemState.isAdminMode || systemState.isServiceOk
const serviceChoicePending = isServiceChoicePending(
systemState.serviceInstallState,
systemState.isServiceOk,
systemStateError !== undefined,
)
return {
runningMode: systemState.runningMode,
@@ -65,6 +98,7 @@ export function useSystemState() {
isSidecarMode,
isServiceMode,
isTunModeAvailable,
serviceChoicePending,
mutateSystemState,
isLoading,
isStartingUp,
@@ -73,7 +107,8 @@ export function useSystemState() {
export function useTunAvailabilityGuard() {
const { verge, patchVerge } = useVerge()
const { isTunModeAvailable, isLoading, isStartingUp } = useSystemState()
const { isTunModeAvailable, serviceChoicePending, isLoading, isStartingUp } =
useSystemState()
const disablingTunRef = useRef(false)
const cooldownTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
const enable_tun_mode = verge?.enable_tun_mode
@@ -85,6 +120,7 @@ export function useTunAvailabilityGuard() {
!disablingTunRef.current &&
enable_tun_mode &&
!isTunModeAvailable &&
!serviceChoicePending &&
!isLoading &&
!isStartingUp
) {
@@ -117,5 +153,12 @@ export function useTunAvailabilityGuard() {
disablingTunRef.current = false
}
}
}, [enable_tun_mode, isTunModeAvailable, patchVerge, isLoading, isStartingUp])
}, [
enable_tun_mode,
isTunModeAvailable,
serviceChoicePending,
patchVerge,
isLoading,
isStartingUp,
])
}