refactor: streamline proxy availability checks and remove redundant methods

This commit is contained in:
wonfen 2026-03-17 05:52:12 +08:00
parent 2043b24e4b
commit 68ca01cfea
No known key found for this signature in database
GPG Key ID: CEAFD6C73AB2001F
2 changed files with 31 additions and 53 deletions

View File

@ -33,6 +33,7 @@ import {
TooltipIcon,
} from '@/components/base'
import { EditorViewer } from '@/components/profile/editor-viewer'
import { useSystemProxyState } from '@/hooks/use-system-proxy-state'
import { useVerge } from '@/hooks/use-verge'
import { useAppData } from '@/providers/app-data-context'
import {
@ -108,13 +109,8 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
const { verge, patchVerge, mutateVerge } = useVerge()
const [hostOptions, setHostOptions] = useState<string[]>([])
type SysProxy = Awaited<ReturnType<typeof getSystemProxy>>
const [sysproxy, setSysproxy] = useState<SysProxy>()
type AutoProxy = Awaited<ReturnType<typeof getAutotemProxy>>
const [autoproxy, setAutoproxy] = useState<AutoProxy>()
const { clashConfig } = useAppData()
const { indicator: isProxyReallyEnabled } = useSystemProxyState()
const {
enable_system_proxy: enabled,
@ -248,8 +244,6 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
pac_content: pac_file_content ?? DEFAULT_PAC,
proxy_host: proxy_host ?? '127.0.0.1',
})
getSystemProxy().then((p) => setSysproxy(p))
getAutotemProxy().then((p) => setAutoproxy(p))
fetchNetworkInterfaces()
},
close: () => setOpen(false),
@ -478,13 +472,9 @@ export const SysproxyViewer = forwardRef<DialogRef>((props, ref) => {
{t('settings.modals.sysproxy.fields.enableStatus')}
</Typography>
<Typography className="value">
{value.pac
? autoproxy?.enable
? t('shared.statuses.enabled')
: t('shared.statuses.disabled')
: sysproxy?.enable
? t('shared.statuses.enabled')
: t('shared.statuses.disabled')}
{isProxyReallyEnabled
? t('shared.statuses.enabled')
: t('shared.statuses.disabled')}
</Typography>
</FlexBox>
{!value.pac && (

View File

@ -9,71 +9,59 @@ import { getAutotemProxy } from '@/services/cmds'
// 系统代理状态检测统一逻辑
export const useSystemProxyState = () => {
const { verge, mutateVerge, patchVerge } = useVerge()
const { sysproxy } = useAppData()
const { sysproxy, clashConfig } = useAppData()
const { data: autoproxy } = useSWR('getAutotemProxy', getAutotemProxy, {
revalidateOnFocus: true,
revalidateOnReconnect: true,
})
const { enable_system_proxy, proxy_auto_config } = verge ?? {}
const {
enable_system_proxy,
proxy_auto_config,
proxy_host,
verge_mixed_port,
} = verge ?? {}
const getSystemProxyActualState = () => {
const userEnabled = enable_system_proxy ?? false
// 用户配置状态应该与系统实际状态一致
// 如果用户启用了系统代理,检查实际的系统状态
if (userEnabled) {
if (proxy_auto_config) {
return autoproxy?.enable ?? false
} else {
return sysproxy?.enable ?? false
}
}
// 用户没有启用时,返回 false
return false
}
const getSystemProxyIndicator = () => {
// OS 实际状态enable + 地址匹配本应用
const indicator = (() => {
const host = proxy_host || '127.0.0.1'
if (proxy_auto_config) {
return autoproxy?.enable ?? false
if (!autoproxy?.enable) return false
const pacPort = import.meta.env.DEV ? 11233 : 33331
return autoproxy.url === `http://${host}:${pacPort}/commands/pac`
} else {
return sysproxy?.enable ?? false
if (!sysproxy?.enable) return false
const port = verge_mixed_port || clashConfig?.mixedPort || 7897
return sysproxy.server === `${host}:${port}`
}
}
const updateProxyStatus = async (isEnabling: boolean) => {
// 关闭时更快响应,开启时等待系统确认
const delay = isEnabling ? 20 : 10
await new Promise((resolve) => setTimeout(resolve, delay))
await mutate('getSystemProxy')
await mutate('getAutotemProxy')
}
})()
const toggleSystemProxy = useLockFn(async (enabled: boolean) => {
mutateVerge({ ...verge, enable_system_proxy: enabled }, false)
const updateProxyStatus = async () => {
await new Promise((resolve) => setTimeout(resolve, enabled ? 20 : 10))
await mutate('getSystemProxy')
await mutate('getAutotemProxy')
}
try {
if (!enabled && verge?.auto_close_connection) {
await closeAllConnections()
}
await patchVerge({ enable_system_proxy: enabled })
await updateProxyStatus(enabled)
await updateProxyStatus()
} catch (error) {
console.warn('[useSystemProxyState] toggleSystemProxy failed:', error)
mutateVerge({ ...verge, enable_system_proxy: !enabled }, false)
await updateProxyStatus(!enabled)
await updateProxyStatus()
throw error
}
})
return {
actualState: getSystemProxyActualState(),
indicator: getSystemProxyIndicator(),
indicator,
configState: enable_system_proxy ?? false,
sysproxy,
autoproxy,
proxy_auto_config,
toggleSystemProxy,
}
}