代理修改

This commit is contained in:
2026-07-18 17:46:21 +08:00
parent 2d9a8ddef6
commit e84958e0fc
71 changed files with 1221 additions and 134 deletions
+70 -5
View File
@@ -1,6 +1,7 @@
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { invoke } from '@tauri-apps/api/core'
import { listen, type UnlistenFn } from '@tauri-apps/api/event'
import { createLogger } from '@/lib/logger'
const logger = createLogger('proxy')
@@ -23,6 +24,18 @@ export interface ProxySettings {
autoSwitchInterval: number
autoSwitchGroup: string
autoSwitchRegion: string
/** 内核下载镜像源前缀列表(空串=直连 GitHub) */
kernelMirrors: string[]
}
/** 内核安装进度事件载荷,对应 Rust 端 InstallProgress */
export interface InstallProgress {
/** downloading | extracting | replacing | done | error */
stage: string
percent: number
downloadedBytes: number
totalBytes: number | null
message: string
}
export interface ProfileMeta {
@@ -85,6 +98,11 @@ export const useProxyStore = defineStore('proxy', () => {
const settings = ref<ProxySettings | null>(null)
const systemProxy = ref(false)
// ===== 内核安装进度 =====
const installing = ref(false)
const installProgress = ref<InstallProgress | null>(null)
let progressUnlisten: UnlistenFn | null = null
/** 内核信息(同时尝试从 resource 提取到 cores/ */
const refreshKernel = async () => {
try {
@@ -246,16 +264,59 @@ export const useProxyStore = defineStore('proxy', () => {
}
}
// ---------- 内核更新 ----------
// ---------- 内核更新 / 安装 ----------
const checkKernelUpdate = async (): Promise<KernelUpdateInfo> => {
return await invoke<KernelUpdateInfo>('proxy_check_kernel_update')
}
const updateKernel = async () => {
await invoke('proxy_update_kernel')
const updateKernel = async (mirrorPrefix: string = '') => {
await invoke('proxy_update_kernel', { mirrorPrefix })
await refreshKernel()
}
/**
* 首次安装内核:调用后端 install_kernel,监听 kernel-install-progress 事件更新进度
* @param mirrorPrefix 镜像源前缀(空串=GitHub 直连)
* 完成或出错后自动取消监听并清空进度(由调用方控制何时隐藏 UI)
*/
const installKernel = async (mirrorPrefix: string = ''): Promise<void> => {
if (installing.value) return
installing.value = true
installProgress.value = {
stage: 'downloading',
percent: 0,
downloadedBytes: 0,
totalBytes: null,
message: '准备开始下载...'
}
// 注册进度事件监听
if (!progressUnlisten) {
progressUnlisten = await listen<InstallProgress>('kernel-install-progress', (e) => {
installProgress.value = e.payload
})
}
try {
await invoke('proxy_install_kernel', { mirrorPrefix })
await refreshKernel()
} catch (e) {
// 错误事件已由后端 emit,这里仅记录日志
logger.error('内核安装失败: ' + e)
throw e
} finally {
// 保留 installProgress 一段时间供 UI 显示终态,由调用方负责清空
installing.value = false
if (progressUnlisten) {
progressUnlisten()
progressUnlisten = null
}
}
}
/** 清空进度状态(UI 在动画结束后调用) */
const clearInstallProgress = () => {
installProgress.value = null
}
return {
// state
kernel,
@@ -264,6 +325,8 @@ export const useProxyStore = defineStore('proxy', () => {
proxies,
settings,
systemProxy,
installing,
installProgress,
// kernel & process
refreshKernel,
refreshStatus,
@@ -289,8 +352,10 @@ export const useProxyStore = defineStore('proxy', () => {
setSystemProxy,
clearSystemProxy,
toggleSystemProxy,
// kernel update
// kernel update / install
checkKernelUpdate,
updateKernel
updateKernel,
installKernel,
clearInstallProgress
}
})