perf(proxy): consolidate proxy data into a single IPC (#7542)

* feat(proxy): build deterministic proxy view

* feat(proxy): expose ordered proxy view command

* feat(proxy): add proxy view client contract

* fix(lint): allow Node test files

* feat(proxy): switch UI to ordered proxy view

* fix(proxy): harden ordered proxy view consumers

* fix(proxy): compare chain duplicates by current id

* fix(proxy): release mihomo guard after fetch

* fix(proxy): preserve semantic selections across refresh

* fix(proxy): block global chain without GLOBAL

* fix(proxy): handle proxy view edge states
This commit is contained in:
wonfen
2026-07-21 00:11:59 +08:00
committed by GitHub
parent fd70d4c62b
commit 304f5bade9
26 changed files with 2737 additions and 1459 deletions
+94 -90
View File
@@ -42,16 +42,18 @@ import {
} from 'tauri-plugin-mihomo-api'
import { TooltipIcon } from '@/components/base'
import { useRuntimeConfig } from '@/hooks/use-clash'
import { useAppRefreshers, useProxiesData } from '@/providers/app-data-context'
import { updateProxyChainConfigInRuntime } from '@/services/cmds'
import {
selectGlobalChainNodes,
selectRuleChainMembers,
} from '@/types/proxy-view'
import { debugLog } from '@/utils/debug'
interface ProxyChainItem {
id: string
name: string
type?: string
delay?: number
}
import { rebindProxyChainItems, type ProxyChainItem } from './proxy-chain-model'
type RuntimeConfigWithProxySequence = IConfigData & { proxies?: unknown }
interface ParsedChainConfig {
proxies?: Array<{
@@ -147,6 +149,7 @@ const SortableItem = ({
: `1px solid ${theme.palette.divider}`,
boxShadow: isDragging ? theme.shadows[4] : theme.shadows[1],
transition: 'box-shadow 0.2s, background-color 0.2s',
opacity: proxy.recordId === undefined ? 0.55 : undefined,
}}
>
<Box
@@ -254,47 +257,78 @@ export const ProxyChain = ({
const theme = useTheme()
const { t } = useTranslation()
const chainWarning = t('proxies.page.chain.warning')
const { proxies } = useProxiesData()
const { proxyView } = useProxiesData()
const { refreshProxy } = useAppRefreshers()
const { data: runtimeConfig } = useRuntimeConfig(true)
const [isConnecting, setIsConnecting] = useState(false)
const markUnsavedChanges = useCallback(() => {
onMarkUnsavedChanges?.()
}, [onMarkUnsavedChanges])
const candidates = useMemo(() => {
if (!proxyView) return []
if (mode === 'rule' && selectedGroup) {
return selectRuleChainMembers(proxyView, selectedGroup).flatMap(
({ member }) => (member.kind === 'node' ? [member.node] : []),
)
}
if (!runtimeConfig) return []
const runtimeProxies = (
runtimeConfig as RuntimeConfigWithProxySequence | null
)?.proxies
return selectGlobalChainNodes(proxyView, runtimeProxies)
}, [mode, proxyView, runtimeConfig, selectedGroup])
const currentProxyChain = useMemo(
() =>
proxyView
? rebindProxyChainItems(proxyChain, candidates, proxyView)
: proxyChain.map((item) => ({
...item,
recordId: undefined,
delay: undefined,
})),
[candidates, proxyChain, proxyView],
)
const isConnected = useMemo(() => {
if (!proxies || proxyChain.length < 2) {
if (!proxyView || currentProxyChain.length === 0) {
return false
}
const lastNode = proxyChain[proxyChain.length - 1]
const lastNode = currentProxyChain[currentProxyChain.length - 1]
if (localStorage.getItem('proxy-chain-exit-node') === lastNode.name) {
return true
}
if (currentProxyChain.length < 2) return false
if (mode === 'global') {
return proxies.global?.now === lastNode.name
return proxyView.global?.now === lastNode.name
}
if (!selectedGroup || !Array.isArray(proxies.groups)) {
if (!selectedGroup) {
return false
}
const proxyChainGroup = proxies.groups.find(
(group: { name: string }) => group.name === selectedGroup,
const proxyChainGroup = proxyView.groups.find(
(group) => group.name === selectedGroup,
)
return proxyChainGroup?.now === lastNode.name
}, [proxies, proxyChain, mode, selectedGroup])
}, [proxyView, currentProxyChain, mode, selectedGroup])
// 监听链的变化,但排除从配置加载的情况
const chainLengthRef = useRef(proxyChain.length)
const chainLengthRef = useRef(currentProxyChain.length)
useEffect(() => {
// 只有当链长度发生变化且不是初始加载时,才标记为未保存
if (
chainLengthRef.current !== proxyChain.length &&
chainLengthRef.current !== currentProxyChain.length &&
chainLengthRef.current !== 0
) {
markUnsavedChanges()
}
chainLengthRef.current = proxyChain.length
}, [proxyChain.length, markUnsavedChanges])
chainLengthRef.current = currentProxyChain.length
}, [currentProxyChain.length, markUnsavedChanges])
const sensors = useSensors(
useSensor(PointerSensor, {
@@ -310,23 +344,27 @@ export const ProxyChain = ({
const { active, over } = event
if (active.id !== over?.id) {
const oldIndex = proxyChain.findIndex((item) => item.id === active.id)
const newIndex = proxyChain.findIndex((item) => item.id === over?.id)
const oldIndex = currentProxyChain.findIndex(
(item) => item.id === active.id,
)
const newIndex = currentProxyChain.findIndex(
(item) => item.id === over?.id,
)
onUpdateChain(arrayMove(proxyChain, oldIndex, newIndex))
onUpdateChain(arrayMove(currentProxyChain, oldIndex, newIndex))
markUnsavedChanges()
}
},
[proxyChain, onUpdateChain, markUnsavedChanges],
[currentProxyChain, onUpdateChain, markUnsavedChanges],
)
const handleRemoveProxy = useCallback(
(id: string) => {
const newChain = proxyChain.filter((item) => item.id !== id)
const newChain = currentProxyChain.filter((item) => item.id !== id)
onUpdateChain(newChain)
markUnsavedChanges()
},
[proxyChain, onUpdateChain, markUnsavedChanges],
[currentProxyChain, onUpdateChain, markUnsavedChanges],
)
const handleConnect = useCallback(async () => {
@@ -344,9 +382,9 @@ export const ProxyChain = ({
try {
await selectNodeForGroup(targetGroup, 'DIRECT')
} catch {
if (proxyChain.length >= 1) {
if (currentProxyChain.length >= 1) {
try {
await selectNodeForGroup(targetGroup, proxyChain[0].name)
await selectNodeForGroup(targetGroup, currentProxyChain[0].name)
} catch {
// ignore
}
@@ -371,7 +409,15 @@ export const ProxyChain = ({
return
}
if (proxyChain.length < 2) {
if (mode === 'global' && proxyView?.global === null) {
alert(t('proxies.page.chain.connectFailed') || '连接链式代理失败')
return
}
if (
currentProxyChain.length < 2 ||
currentProxyChain.some(({ recordId }) => !recordId)
) {
alert(t('proxies.page.chain.minimumNodes') || '链式代理至少需要2个节点')
return
}
@@ -379,13 +425,13 @@ export const ProxyChain = ({
setIsConnecting(true)
try {
// 第一步:保存链式代理配置
const chainProxies = proxyChain.map((node) => node.name)
const chainProxies = currentProxyChain.map((node) => node.name)
debugLog('Saving chain config:', chainProxies)
await updateProxyChainConfigInRuntime(chainProxies)
debugLog('Chain configuration saved successfully')
// 第二步:连接到代理链的最后一个节点
const lastNode = proxyChain[proxyChain.length - 1]
const lastNode = currentProxyChain[currentProxyChain.length - 1]
debugLog(`Connecting to proxy chain, last node: ${lastNode.name}`)
// 根据模式确定使用的代理组名称
@@ -409,23 +455,16 @@ export const ProxyChain = ({
setIsConnecting(false)
}
}, [
proxyChain,
currentProxyChain,
isConnected,
t,
refreshProxy,
mode,
proxyView,
selectedGroup,
onUpdateChain,
])
const proxyChainRef = useRef(proxyChain)
const onUpdateChainRef = useRef(onUpdateChain)
useEffect(() => {
proxyChainRef.current = proxyChain
onUpdateChainRef.current = onUpdateChain
}, [proxyChain, onUpdateChain])
// 处理链式代理配置数据
useEffect(() => {
if (chainConfigData) {
@@ -443,47 +482,6 @@ export const ProxyChain = ({
}
}, [chainConfigData, onUpdateChain])
// 定时更新延迟数据
useEffect(() => {
if (!proxies?.records) return
const updateDelays = () => {
const currentChain = proxyChainRef.current
if (currentChain.length === 0) return
const updatedChain = currentChain.map((item) => {
const proxyRecord = proxies.records[item.name]
if (
proxyRecord &&
proxyRecord.history &&
proxyRecord.history.length > 0
) {
const latestDelay =
proxyRecord.history[proxyRecord.history.length - 1].delay
return { ...item, delay: latestDelay }
}
return item
})
// 只有在延迟数据确实发生变化时才更新
const hasChanged = updatedChain.some(
(item, index) => item.delay !== currentChain[index]?.delay,
)
if (hasChanged) {
onUpdateChainRef.current(updatedChain)
}
}
// 立即更新一次延迟
updateDelays()
// 设置定时器,每5秒更新一次延迟
const interval = setInterval(updateDelays, 5000)
return () => clearInterval(interval)
}, [proxies?.records]) // 只依赖proxies.records
return (
<Paper
elevation={1}
@@ -512,7 +510,7 @@ export const ProxyChain = ({
/>
</Box>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
{proxyChain.length > 0 && (
{currentProxyChain.length > 0 && (
<IconButton
size="small"
onClick={() => {
@@ -542,15 +540,20 @@ export const ProxyChain = ({
onClick={handleConnect}
disabled={
isConnecting ||
proxyChain.length < 2 ||
(mode !== 'global' && !selectedGroup)
(!isConnected &&
(currentProxyChain.length < 2 ||
currentProxyChain.some(
({ recordId }) => recordId === undefined,
) ||
(mode === 'global' && proxyView?.global === null) ||
(mode !== 'global' && !selectedGroup)))
}
color={isConnected ? 'error' : 'success'}
sx={{
minWidth: 90,
}}
title={
proxyChain.length < 2
!isConnected && currentProxyChain.length < 2
? t('proxies.page.chain.minimumNodes') ||
'链式代理至少需要2个节点'
: undefined
@@ -566,10 +569,10 @@ export const ProxyChain = ({
</Box>
<Alert
severity={proxyChain.length === 1 ? 'warning' : 'info'}
severity={currentProxyChain.length === 1 ? 'warning' : 'info'}
sx={{ mb: 2 }}
>
{proxyChain.length === 1
{currentProxyChain.length === 1
? t('proxies.page.chain.minimumNodesHint') ||
'链式代理至少需要2个节点,请再添加一个节点。'
: t('proxies.page.chain.instruction') ||
@@ -577,7 +580,7 @@ export const ProxyChain = ({
</Alert>
<Box sx={{ flex: 1, overflow: 'auto' }}>
{proxyChain.length === 0 ? (
{currentProxyChain.length === 0 ? (
<Box
sx={{
display: 'flex',
@@ -596,7 +599,7 @@ export const ProxyChain = ({
onDragEnd={handleDragEnd}
>
<SortableContext
items={proxyChain.map((proxy) => proxy.id)}
items={currentProxyChain.map((proxy) => proxy.id)}
strategy={verticalListSortingStrategy}
>
<Box
@@ -606,18 +609,19 @@ export const ProxyChain = ({
p: 1,
}}
>
{proxyChain.map((proxy, index) => (
{currentProxyChain.map((proxy, index) => (
<Box key={proxy.id}>
<SortableItem
proxy={proxy}
index={index}
isFirst={index === 0}
isLast={
index === proxyChain.length - 1 && proxyChain.length > 1
index === currentProxyChain.length - 1 &&
currentProxyChain.length > 1
}
onRemove={handleRemoveProxy}
/>
{index < proxyChain.length - 1 && (
{index < currentProxyChain.length - 1 && (
<Box
sx={{
display: 'flex',