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
+6 -126
View File
@@ -1,8 +1,8 @@
import { invoke } from '@tauri-apps/api/core'
import dayjs from 'dayjs'
import { getProxies, getProxyProviders } from 'tauri-plugin-mihomo-api'
import { showNotice } from '@/services/notice-service'
import type { ProxyViewV1 } from '@/types/proxy-view'
import { debugLog } from '@/utils/debug'
export async function copyClashEnv() {
@@ -93,10 +93,6 @@ export async function getRuntimeConfig() {
return invoke<IConfigData | null>('get_runtime_config')
}
async function getRuntimeProxyGroupOrder() {
return invoke<string[]>('get_runtime_proxy_group_order')
}
export async function getRuntimeYaml() {
return invoke<string | null>('get_runtime_yaml')
}
@@ -129,128 +125,12 @@ export async function syncTrayProxySelection() {
return invoke<void>('sync_tray_proxy_selection')
}
export async function calcuProxies(): Promise<{
global: IProxyGroupItem
direct: IProxyItem
groups: IProxyGroupItem[]
records: Record<string, IProxyItem>
proxies: IProxyItem[]
}> {
const [proxyResponse, providerResponse, runtimeGroupOrder] =
await Promise.all([
getProxies(),
calcuProxyProviders(),
getRuntimeProxyGroupOrder(),
])
const proxyRecord = proxyResponse.proxies
const providerRecord = providerResponse
// provider name map
const providerMap = Object.fromEntries(
Object.entries(providerRecord).flatMap(([provider, item]) =>
item!.proxies.map((p) => [p.name, { ...p, provider }]),
),
)
// compatible with proxy-providers
const generateItem = (name: string) => {
if (proxyRecord[name]) return proxyRecord[name]
if (providerMap[name]) return providerMap[name]
return {
name,
type: 'unknown',
udp: false,
xudp: false,
tfo: false,
mptcp: false,
smux: false,
history: [],
}
export async function getProxyView(): Promise<ProxyViewV1> {
const view = await invoke<ProxyViewV1>('get_proxy_view')
if (view.schemaVersion !== 1) {
throw new Error('Unsupported proxy view schema: ' + view.schemaVersion)
}
const { GLOBAL: global, DIRECT: direct, REJECT: reject } = proxyRecord
let groups: IProxyGroupItem[] = Object.values(proxyRecord).reduce<
IProxyGroupItem[]
>((acc, each) => {
if (each?.name !== 'GLOBAL' && each?.all) {
acc.push({
...each,
all: each.all!.map((item) => generateItem(item)),
})
}
return acc
}, [])
if (global?.all) {
const globalGroups: IProxyGroupItem[] = global.all.reduce<
IProxyGroupItem[]
>((acc, name) => {
if (proxyRecord[name]?.all) {
acc.push({
...proxyRecord[name],
all: proxyRecord[name].all!.map((item) => generateItem(item)),
})
}
return acc
}, [])
const globalNames = new Set(globalGroups.map((each) => each.name))
groups = groups
.filter((group) => {
return !globalNames.has(group.name)
})
.concat(globalGroups)
}
const groupOrder = new Map(
runtimeGroupOrder.map((name, index) => [name, index]),
)
groups.sort((a, b) => {
const aIndex = groupOrder.get(a.name) ?? Number.MAX_SAFE_INTEGER
const bIndex = groupOrder.get(b.name) ?? Number.MAX_SAFE_INTEGER
if (aIndex !== bIndex) return aIndex - bIndex
if (a.name < b.name) return -1
if (a.name > b.name) return 1
return 0
})
const proxies = [direct, reject].concat(
Object.values(proxyRecord).filter(
(p) => !p?.all?.length && p?.name !== 'DIRECT' && p?.name !== 'REJECT',
),
)
const _global = {
...global,
all: global?.all?.map((item) => generateItem(item)) || [],
}
// 原本的 records 拥有所有节点信息,新版本内核需要将 provider 的节点信息合并到 records 中, 同时兼容旧版本数据
const records = { ...proxyRecord, ...providerMap }
return {
global: _global as IProxyGroupItem,
direct: direct as IProxyItem,
groups,
records: records as Record<string, IProxyItem>,
proxies: (proxies as IProxyItem[]) ?? [],
}
}
export async function calcuProxyProviders() {
const providers = await getProxyProviders()
return Object.fromEntries(
Object.entries(providers.providers)
.sort()
.filter(
([_, item]) =>
item?.vehicleType === 'HTTP' || item?.vehicleType === 'File',
),
)
return view
}
export async function getClashLogs() {
+30 -20
View File
@@ -4,6 +4,12 @@ import {
type ProxyDelay,
} from 'tauri-plugin-mihomo-api'
import {
memberDetails,
providerNameOf,
type InteractableProxyMember,
type ResolvedProxyMember,
} from '@/types/proxy-view'
import { debugLog } from '@/utils/debug'
const hashKey = (name: string, group: string) => `${group ?? ''}::${name}`
@@ -185,19 +191,18 @@ class DelayManager {
return update ? update.delay : -1
}
/// 暂时修复provider的节点延迟排序的问题
getDelayFix(proxy: IProxyItem, group: string) {
if (!proxy.provider) {
const update = this.getDelayUpdate(proxy.name, group)
if (update && (update.delay >= 0 || update.delay === -2)) {
return update.delay
}
getDelayFix(member: ResolvedProxyMember, group: string) {
if (member.kind === 'unresolved') return -1
const details = memberDetails(member)
const name = member.ref.name
const update = this.getDelayUpdate(name, group)
if (update && (update.delay >= 0 || update.delay === -2)) {
return update.delay
}
// 添加 history 属性的安全检查
if (proxy.history && proxy.history.length > 0) {
if (details?.history && details.history.length > 0) {
// 0ms以error显示
return proxy.history[proxy.history.length - 1].delay || 1e6
return details.history[details.history.length - 1].delay || 1e6
}
return -1
}
@@ -215,11 +220,17 @@ class DelayManager {
}
async checkDelay(
name: string,
member: InteractableProxyMember,
group: string,
timeout: number,
providerName?: string,
): Promise<DelayUpdate> {
const name = member.ref.name
const providerName =
member.kind === 'node' ? providerNameOf(member.node) : undefined
const apiName =
member.kind === 'node' && member.node.source.kind === 'provider'
? member.node.source.proxyName
: name
debugLog(
`[DelayManager] 开始测试延迟,代理: ${name}, 组: ${group}, 超时: ${timeout}ms`,
)
@@ -240,7 +251,7 @@ class DelayManager {
// 使用Promise.race来实现超时控制
const result = await Promise.race([
this.unifiedDelayCheck(name, url, timeout, providerName),
this.unifiedDelayCheck(apiName, url, timeout, providerName),
timeoutPromise,
])
@@ -267,7 +278,7 @@ class DelayManager {
}
async checkListDelay(
proxies: IProxyItem[],
proxies: InteractableProxyMember[],
group: string,
timeout: number,
concurrency = 36,
@@ -275,7 +286,7 @@ class DelayManager {
debugLog(
`[DelayManager] 批量测试延迟开始,组: ${group}, 数量: ${proxies.length}, 并发数: ${concurrency}`,
)
const names = proxies.map((p) => p.name)
const names = proxies.map((member) => member.ref.name)
// 设置正在延迟测试中
names.forEach((name) => {
this.setDelay(name, group, -2)
@@ -286,10 +297,9 @@ class DelayManager {
const listener = this.groupListenerMap.get(group)
const help = async (): Promise<void> => {
const currProxy = proxies[index++]
if (!currProxy) return
const currName = currProxy.name
const currProviderName = currProxy.provider
const currMember = proxies[index++]
if (!currMember) return
const currName = currMember.ref.name
try {
// 确保API调用前状态为测试中
@@ -303,7 +313,7 @@ class DelayManager {
)
}
await this.checkDelay(currName, group, timeout, currProviderName)
await this.checkDelay(currMember, group, timeout)
if (listener) {
this.queueGroupNotification(group)
}