diff --git a/UPDATELOG.md b/UPDATELOG.md index 78f5d858f..8e27fe67b 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -23,7 +23,7 @@ - 安装服务模式后无法立即开启 TUN 模式 #### 新增了: - - Mihomo(Meta)内核升级至 1.19.7 + - Mihomo(Meta)内核升级至 1.19.8 - 允许代理主机地址设置为非 127.0.0.1 对 WSL 代理友好 - 关闭系统代理时关闭已建立的网络连接 - 托盘显示当前轻量模式状态 @@ -43,6 +43,7 @@ - 支持手动卸载服务模式,回退到 Sidecar 模式 - 优化了其他语言的翻译问题! - 添加了土耳其语,日本语,德语,西班牙语的支持 向着 i18 更进了一步 (●ˇ∀ˇ●) (后续可添加其他国家语言) + - 卸载服务的按钮 #### 优化了: - 系统代理 Bypass 设置 diff --git a/src-tauri/src/cmd/clash.rs b/src-tauri/src/cmd/clash.rs index 23594e79e..c0a5e00c7 100644 --- a/src-tauri/src/cmd/clash.rs +++ b/src-tauri/src/cmd/clash.rs @@ -77,7 +77,6 @@ pub async fn stop_core() -> CmdResult { wrap_err!(CoreManager::global().stop_core().await) } - /// 重启核心 #[tauri::command] pub async fn restart_core() -> CmdResult { diff --git a/src/components/home/proxy-tun-card.tsx b/src/components/home/proxy-tun-card.tsx index 9c0e580c2..bdc02db45 100644 --- a/src/components/home/proxy-tun-card.tsx +++ b/src/components/home/proxy-tun-card.tsx @@ -9,7 +9,7 @@ import { useTheme, Fade, } from "@mui/material"; -import { useState, useMemo, memo, FC } from "react"; +import { useState, useMemo, memo, FC, useEffect } from "react"; import ProxyControlSwitches from "@/components/shared/ProxyControlSwitches"; import { ComputerRounded, @@ -20,6 +20,8 @@ import { import { useVerge } from "@/hooks/use-verge"; import { useSystemState } from "@/hooks/use-system-state"; import { showNotice } from "@/services/noticeService"; +import { isServiceAvailable } from "@/services/cmds"; +import { mutate } from "swr"; const LOCAL_STORAGE_TAB_KEY = "clash-verge-proxy-active-tab"; @@ -31,7 +33,7 @@ interface TabButtonProps { hasIndicator?: boolean; } -// 抽取Tab组件以减少重复代码 +// Tab组件 const TabButton: FC = memo( ({ isActive, onClick, icon: Icon, label, hasIndicator = false }) => ( = memo( ({ description, tooltipTitle }) => ( @@ -138,29 +140,42 @@ export const ProxyTunCard: FC = () => { const [activeTab, setActiveTab] = useState( () => localStorage.getItem(LOCAL_STORAGE_TAB_KEY) || "system", ); + + const [localServiceOk, setLocalServiceOk] = useState(false); - // 获取代理状态信息 const { verge } = useVerge(); - const { isSidecarMode, isAdminMode } = useSystemState(); + const { isAdminMode } = useSystemState(); - // 从verge配置中获取开关状态 const { enable_system_proxy, enable_tun_mode } = verge ?? {}; - // 判断Tun模式是否可用 - 当处于服务模式或管理员模式时可用 - const isTunAvailable = !isSidecarMode || isAdminMode; + const updateLocalStatus = async () => { + try { + const serviceStatus = await isServiceAvailable(); + setLocalServiceOk(serviceStatus); + mutate("isServiceAvailable", serviceStatus, false); + } catch (error) { + console.error("更新TUN状态失败:", error); + } + }; + + useEffect(() => { + updateLocalStatus(); + }, []); + + const isTunAvailable = localServiceOk || isAdminMode; - // 处理错误 const handleError = (err: Error) => { - showNotice('error', err.message || err.toString(), 3000); + showNotice('error', err.message || err.toString()); }; - // 处理标签切换并保存到localStorage const handleTabChange = (tab: string) => { setActiveTab(tab); localStorage.setItem(LOCAL_STORAGE_TAB_KEY, tab); + if (tab === "tun") { + updateLocalStatus(); + } }; - // 用户提示文本 - 使用useMemo避免重复计算 const tabDescription = useMemo(() => { if (activeTab === "system") { return { @@ -183,7 +198,6 @@ export const ProxyTunCard: FC = () => { return ( - {/* 选项卡 */} { /> - {/* 说明文本区域 */} { /> - {/* 控制开关部分 */} { const { data: sysproxy } = useSWR("getSystemProxy", getSystemProxy); const { data: autoproxy } = useSWR("getAutotemProxy", getAutotemProxy); - const { isAdminMode, isSidecarMode, mutateRunningMode, isServiceOk } = - useSystemState(); + const { isAdminMode, isSidecarMode, mutateRunningMode } = useSystemState(); + + // 添加本地服务状态 + const [localServiceOk, setLocalServiceOk] = useState(false); + + // 更新本地服务状态 + const updateLocalStatus = async () => { + try { + const serviceStatus = await isServiceAvailable(); + setLocalServiceOk(serviceStatus); + mutate("isServiceAvailable", serviceStatus, false); + } catch (error) { + console.error("更新TUN状态失败:", error); + } + }; + + // 组件挂载时更新状态 + useEffect(() => { + updateLocalStatus(); + }, []); // 判断Tun模式是否可用 - 当处于服务模式或管理员模式时可用 - const isTunAvailable = isServiceOk || (isSidecarMode && !isAdminMode); + const isTunAvailable = localServiceOk || isAdminMode; console.log("is tun isTunAvailable:", isTunAvailable); @@ -68,7 +86,6 @@ const SettingSystem = ({ onError }: Props) => { }; const updateProxyStatus = async () => { - // 等待一小段时间让系统代理状态变化 await new Promise((resolve) => setTimeout(resolve, 100)); await mutate("getSystemProxy"); await mutate("getAutotemProxy"); @@ -90,22 +107,24 @@ const SettingSystem = ({ onError }: Props) => { } ) => { try { - showNotice("info", t(beforeMsg), 1000); + showNotice("info", t(beforeMsg)); await stopCore(); - showNotice("info", t(actionMsg), 1000); + showNotice("info", t(actionMsg)); await action(); - showNotice("success", t(successMsg), 2000); - showNotice("info", t("Starting Core..."), 1000); - await startCore(); + showNotice("success", t(successMsg)); + showNotice("info", t("Restarting Core...")); + await restartCore(); await mutateRunningMode(); + await updateLocalStatus(); } catch (err: any) { - showNotice("error", err.message || err.toString(), 3000); + showNotice("error", err.message || err.toString()); try { - showNotice("info", t("Try running core as Sidecar..."), 1000); - await startCore(); + showNotice("info", t("Try running core as Sidecar...")); + await restartCore(); await mutateRunningMode(); + await updateLocalStatus(); } catch (e: any) { - showNotice("error", e?.message || e?.toString(), 3000); + showNotice("error", e?.message || e?.toString()); } } } @@ -114,19 +133,19 @@ const SettingSystem = ({ onError }: Props) => { // 安装系统服务 const onInstallService = () => handleServiceOperation({ - beforeMsg: "Stopping Core...", + beforeMsg: t("Stopping Core..."), action: installService, - actionMsg: "Installing Service...", - successMsg: "Service Installed Successfully", + actionMsg: t("Installing Service..."), + successMsg: t("Service Installed Successfully"), }); // 卸载系统服务 const onUninstallService = () => handleServiceOperation({ - beforeMsg: "Stopping Core...", + beforeMsg: t("Stopping Core..."), action: uninstallService, - actionMsg: "Uninstalling Service...", - successMsg: "Service Uninstalled Successfully", + actionMsg: t("Uninstalling Service..."), + successMsg: t("Service Uninstalled Successfully"), }); return ( @@ -143,12 +162,12 @@ const SettingSystem = ({ onError }: Props) => { icon={SettingsRounded} onClick={() => tunRef.current?.open()} /> - {!isServiceOk && !isAdminMode && ( - + {!isTunAvailable && ( + )} - {!isServiceOk && !isAdminMode && ( + {!localServiceOk && !isAdminMode && (