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

View File

@ -9,71 +9,59 @@ import { getAutotemProxy } from '@/services/cmds'
// 系统代理状态检测统一逻辑 // 系统代理状态检测统一逻辑
export const useSystemProxyState = () => { export const useSystemProxyState = () => {
const { verge, mutateVerge, patchVerge } = useVerge() const { verge, mutateVerge, patchVerge } = useVerge()
const { sysproxy } = useAppData() const { sysproxy, clashConfig } = useAppData()
const { data: autoproxy } = useSWR('getAutotemProxy', getAutotemProxy, { const { data: autoproxy } = useSWR('getAutotemProxy', getAutotemProxy, {
revalidateOnFocus: true, revalidateOnFocus: true,
revalidateOnReconnect: 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 = () => { // OS 实际状态enable + 地址匹配本应用
const userEnabled = enable_system_proxy ?? false const indicator = (() => {
const host = proxy_host || '127.0.0.1'
// 用户配置状态应该与系统实际状态一致
// 如果用户启用了系统代理,检查实际的系统状态
if (userEnabled) {
if (proxy_auto_config) {
return autoproxy?.enable ?? false
} else {
return sysproxy?.enable ?? false
}
}
// 用户没有启用时,返回 false
return false
}
const getSystemProxyIndicator = () => {
if (proxy_auto_config) { 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 { } 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) => { const toggleSystemProxy = useLockFn(async (enabled: boolean) => {
mutateVerge({ ...verge, enable_system_proxy: enabled }, false) 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 { try {
if (!enabled && verge?.auto_close_connection) { if (!enabled && verge?.auto_close_connection) {
await closeAllConnections() await closeAllConnections()
} }
await patchVerge({ enable_system_proxy: enabled }) await patchVerge({ enable_system_proxy: enabled })
await updateProxyStatus(enabled) await updateProxyStatus()
} catch (error) { } catch (error) {
console.warn('[useSystemProxyState] toggleSystemProxy failed:', error) console.warn('[useSystemProxyState] toggleSystemProxy failed:', error)
mutateVerge({ ...verge, enable_system_proxy: !enabled }, false) mutateVerge({ ...verge, enable_system_proxy: !enabled }, false)
await updateProxyStatus(!enabled) await updateProxyStatus()
throw error throw error
} }
}) })
return { return {
actualState: getSystemProxyActualState(), indicator,
indicator: getSystemProxyIndicator(),
configState: enable_system_proxy ?? false, configState: enable_system_proxy ?? false,
sysproxy,
autoproxy,
proxy_auto_config,
toggleSystemProxy, toggleSystemProxy,
} }
} }