mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-26 23:30:09 +08:00
fix: disable tun mode menu on tray when tun mode is unavailable (#4975)
* fix: check if service installed when toggle tun mode on tray * chore: cargo fmt * fix: auto disable tun mode * docs: update UPDATELOG.md * fix: init Tun mode status * chore: update * feat: disable tun mode tray menu when tun mode is unavailable * fix: restart core when uninstall service is canceled * chore: remove check notification when toggle tun mode * chore: fix updatelog --------- Co-authored-by: Tunglies <77394545+Tunglies@users.noreply.github.com>
This commit is contained in:
@@ -117,13 +117,8 @@ const ProxyControlSwitches = ({
|
||||
const { uninstallServiceAndRestartCore } = useServiceUninstaller();
|
||||
const { actualState: systemProxyActualState, toggleSystemProxy } =
|
||||
useSystemProxyState();
|
||||
const {
|
||||
isServiceMode,
|
||||
isTunModeAvailable,
|
||||
mutateRunningMode,
|
||||
mutateServiceOk,
|
||||
mutateTunModeAvailable,
|
||||
} = useSystemState();
|
||||
const { isServiceOk, isTunModeAvailable, mutateSystemState } =
|
||||
useSystemState();
|
||||
|
||||
const sysproxyRef = useRef<DialogRef>(null);
|
||||
const tunRef = useRef<DialogRef>(null);
|
||||
@@ -148,9 +143,7 @@ const ProxyControlSwitches = ({
|
||||
const onInstallService = useLockFn(async () => {
|
||||
try {
|
||||
await installServiceAndRestartCore();
|
||||
await mutateRunningMode();
|
||||
await mutateServiceOk();
|
||||
await mutateTunModeAvailable();
|
||||
await mutateSystemState();
|
||||
} catch (err) {
|
||||
showNotice("error", (err as Error).message || String(err));
|
||||
}
|
||||
@@ -158,11 +151,11 @@ const ProxyControlSwitches = ({
|
||||
|
||||
const onUninstallService = useLockFn(async () => {
|
||||
try {
|
||||
await handleTunToggle(false);
|
||||
if (verge?.enable_tun_mode) {
|
||||
await handleTunToggle(false);
|
||||
}
|
||||
await uninstallServiceAndRestartCore();
|
||||
await mutateRunningMode();
|
||||
await mutateServiceOk();
|
||||
await mutateTunModeAvailable();
|
||||
await mutateSystemState();
|
||||
} catch (err) {
|
||||
showNotice("error", (err as Error).message || String(err));
|
||||
}
|
||||
@@ -198,22 +191,22 @@ const ProxyControlSwitches = ({
|
||||
extraIcons={
|
||||
<>
|
||||
{!isTunModeAvailable && (
|
||||
<TooltipIcon
|
||||
title={t("TUN requires Service Mode or Admin Mode")}
|
||||
icon={WarningRounded}
|
||||
sx={{ color: "warning.main", ml: 1 }}
|
||||
/>
|
||||
<>
|
||||
<TooltipIcon
|
||||
title={t("TUN requires Service Mode or Admin Mode")}
|
||||
icon={WarningRounded}
|
||||
sx={{ color: "warning.main", ml: 1 }}
|
||||
/>
|
||||
<TooltipIcon
|
||||
title={t("Install Service")}
|
||||
icon={BuildRounded}
|
||||
color="primary"
|
||||
onClick={onInstallService}
|
||||
sx={{ ml: 1 }}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
{!isTunModeAvailable && (
|
||||
<TooltipIcon
|
||||
title={t("Install Service")}
|
||||
icon={BuildRounded}
|
||||
color="primary"
|
||||
onClick={onInstallService}
|
||||
sx={{ ml: 1 }}
|
||||
/>
|
||||
)}
|
||||
{isServiceMode && (
|
||||
{isServiceOk && (
|
||||
<TooltipIcon
|
||||
title={t("Uninstall Service")}
|
||||
icon={DeleteForeverRounded}
|
||||
|
||||
@@ -1,73 +1,99 @@
|
||||
import { useEffect } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
|
||||
import { getRunningMode, isAdmin, isServiceAvailable } from "@/services/cmds";
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
|
||||
import { useVerge } from "./use-verge";
|
||||
|
||||
export interface SystemState {
|
||||
runningMode: "Sidecar" | "Service";
|
||||
isAdminMode: boolean;
|
||||
isServiceOk: boolean;
|
||||
}
|
||||
|
||||
const defaultSystemState = {
|
||||
runningMode: "Sidecar",
|
||||
isAdminMode: false,
|
||||
isServiceOk: false,
|
||||
} as SystemState;
|
||||
|
||||
let disablingTunMode = false;
|
||||
|
||||
/**
|
||||
* 自定义 hook 用于获取系统运行状态
|
||||
* 包括运行模式、管理员状态、系统服务是否可用
|
||||
*/
|
||||
export function useSystemState() {
|
||||
// 获取运行模式
|
||||
const {
|
||||
data: runningMode = "Sidecar",
|
||||
mutate: mutateRunningMode,
|
||||
isLoading: runningModeLoading,
|
||||
} = useSWR("getRunningMode", getRunningMode, {
|
||||
suspense: false,
|
||||
revalidateOnFocus: false,
|
||||
});
|
||||
const isSidecarMode = runningMode === "Sidecar";
|
||||
const isServiceMode = runningMode === "Service";
|
||||
const { t } = useTranslation();
|
||||
const { verge, patchVerge } = useVerge();
|
||||
|
||||
// 获取管理员状态
|
||||
const { data: isAdminMode = false, isLoading: isAdminLoading } = useSWR(
|
||||
"isAdmin",
|
||||
isAdmin,
|
||||
const {
|
||||
data: systemState,
|
||||
mutate: mutateSystemState,
|
||||
isLoading,
|
||||
} = useSWR(
|
||||
"getSystemState",
|
||||
async () => {
|
||||
const [runningMode, isAdminMode, isServiceOk] = await Promise.all([
|
||||
getRunningMode(),
|
||||
isAdmin(),
|
||||
isServiceAvailable(),
|
||||
]);
|
||||
return { runningMode, isAdminMode, isServiceOk } as SystemState;
|
||||
},
|
||||
{
|
||||
suspense: false,
|
||||
revalidateOnFocus: false,
|
||||
suspense: true,
|
||||
refreshInterval: 30000,
|
||||
fallback: defaultSystemState,
|
||||
},
|
||||
);
|
||||
|
||||
const {
|
||||
data: isServiceOk = false,
|
||||
mutate: mutateServiceOk,
|
||||
isLoading: isServiceLoading,
|
||||
} = useSWR(isServiceMode ? "isServiceAvailable" : null, isServiceAvailable, {
|
||||
suspense: false,
|
||||
revalidateOnFocus: false,
|
||||
onSuccess: (data) => {
|
||||
console.log("[useSystemState] 服务状态更新:", data);
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("[useSystemState] 服务状态检查失败:", error);
|
||||
},
|
||||
// isPaused: () => !isServiceMode, // 仅在非 Service 模式下暂停请求
|
||||
});
|
||||
const isSidecarMode = systemState.runningMode === "Sidecar";
|
||||
const isServiceMode = systemState.runningMode === "Service";
|
||||
const isTunModeAvailable = systemState.isAdminMode || systemState.isServiceOk;
|
||||
|
||||
const isLoading =
|
||||
runningModeLoading || isAdminLoading || (isServiceMode && isServiceLoading);
|
||||
const enable_tun_mode = verge?.enable_tun_mode;
|
||||
useEffect(() => {
|
||||
if (enable_tun_mode === undefined) return;
|
||||
|
||||
const { data: isTunModeAvailable = false, mutate: mutateTunModeAvailable } =
|
||||
useSWR(
|
||||
["isTunModeAvailable", isAdminMode, isServiceOk],
|
||||
() => isAdminMode || isServiceOk,
|
||||
{
|
||||
suspense: false,
|
||||
revalidateOnFocus: false,
|
||||
},
|
||||
);
|
||||
if (
|
||||
!disablingTunMode &&
|
||||
enable_tun_mode &&
|
||||
!isTunModeAvailable &&
|
||||
!isLoading
|
||||
) {
|
||||
disablingTunMode = true;
|
||||
patchVerge({ enable_tun_mode: false })
|
||||
.then(() => {
|
||||
showNotice(
|
||||
"info",
|
||||
t("TUN Mode automatically disabled due to service unavailable"),
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error("[useVerge] 自动关闭TUN模式失败:", err);
|
||||
showNotice("error", t("Failed to disable TUN Mode automatically"));
|
||||
})
|
||||
.finally(() => {
|
||||
const tid = setTimeout(() => {
|
||||
// 避免 verge 数据更新不及时导致重复执行关闭 Tun 模式
|
||||
disablingTunMode = false;
|
||||
clearTimeout(tid);
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
}, [enable_tun_mode, isTunModeAvailable, patchVerge, isLoading, t]);
|
||||
|
||||
return {
|
||||
runningMode,
|
||||
isAdminMode,
|
||||
runningMode: systemState.runningMode,
|
||||
isAdminMode: systemState.isAdminMode,
|
||||
isServiceOk: systemState.isServiceOk,
|
||||
isSidecarMode,
|
||||
isServiceMode,
|
||||
isServiceOk,
|
||||
isTunModeAvailable,
|
||||
mutateRunningMode,
|
||||
mutateServiceOk,
|
||||
mutateTunModeAvailable,
|
||||
mutateSystemState,
|
||||
isLoading,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,16 +1,8 @@
|
||||
import { useCallback, useEffect, useRef } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
|
||||
import { useSystemState } from "@/hooks/use-system-state";
|
||||
import { getVergeConfig, patchVergeConfig } from "@/services/cmds";
|
||||
import { showNotice } from "@/services/noticeService";
|
||||
|
||||
export const useVerge = () => {
|
||||
const { t } = useTranslation();
|
||||
const { isTunModeAvailable, isServiceMode, isLoading } = useSystemState();
|
||||
const disablingRef = useRef(false);
|
||||
|
||||
const { data: verge, mutate: mutateVerge } = useSWR(
|
||||
"getVergeConfig",
|
||||
async () => {
|
||||
@@ -24,53 +16,6 @@ export const useVerge = () => {
|
||||
mutateVerge();
|
||||
};
|
||||
|
||||
const { enable_tun_mode } = verge ?? {};
|
||||
|
||||
const mutateVergeRef = useRef(mutateVerge);
|
||||
const tRef = useRef(t);
|
||||
const enableTunRef = useRef(enable_tun_mode);
|
||||
const isLoadingRef = useRef(isLoading);
|
||||
const isServiceModeRef = useRef(isServiceMode);
|
||||
|
||||
mutateVergeRef.current = mutateVerge;
|
||||
tRef.current = t;
|
||||
enableTunRef.current = enable_tun_mode;
|
||||
isLoadingRef.current = isLoading;
|
||||
isServiceModeRef.current = isServiceMode;
|
||||
|
||||
const doDisable = useCallback(async () => {
|
||||
try {
|
||||
if (isServiceModeRef.current === true) return;
|
||||
await patchVergeConfig({ enable_tun_mode: false });
|
||||
await mutateVergeRef.current?.();
|
||||
showNotice(
|
||||
"info",
|
||||
tRef.current(
|
||||
"TUN Mode automatically disabled due to service unavailable",
|
||||
),
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("[useVerge] 自动关闭TUN模式失败:", err);
|
||||
showNotice(
|
||||
"error",
|
||||
tRef.current("Failed to disable TUN Mode automatically"),
|
||||
);
|
||||
} finally {
|
||||
disablingRef.current = false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (isTunModeAvailable === true) return;
|
||||
if (isLoadingRef.current === true) return;
|
||||
if (enableTunRef.current !== true) return;
|
||||
if (isServiceModeRef.current === true) return;
|
||||
if (disablingRef.current) return;
|
||||
|
||||
disablingRef.current = true;
|
||||
void doDisable();
|
||||
}, [isTunModeAvailable, doDisable]);
|
||||
|
||||
return {
|
||||
verge,
|
||||
mutateVerge,
|
||||
|
||||
@@ -25,7 +25,7 @@ const executeWithErrorHandling = async (
|
||||
};
|
||||
|
||||
export const useServiceInstaller = () => {
|
||||
const { mutateRunningMode, mutateServiceOk } = useSystemState();
|
||||
const { mutateSystemState } = useSystemState();
|
||||
|
||||
const installServiceAndRestartCore = useCallback(async () => {
|
||||
await executeWithErrorHandling(
|
||||
@@ -34,9 +34,13 @@ export const useServiceInstaller = () => {
|
||||
"Service Installed Successfully",
|
||||
);
|
||||
|
||||
await executeWithErrorHandling(() => restartCore(), "Restarting Core...");
|
||||
await mutateRunningMode();
|
||||
await mutateServiceOk();
|
||||
}, [mutateRunningMode, mutateServiceOk]);
|
||||
await executeWithErrorHandling(
|
||||
() => restartCore(),
|
||||
"Restarting Core...",
|
||||
"Clash Core Restarted",
|
||||
);
|
||||
|
||||
await mutateSystemState();
|
||||
}, [mutateSystemState]);
|
||||
return { installServiceAndRestartCore };
|
||||
};
|
||||
|
||||
@@ -25,21 +25,26 @@ const executeWithErrorHandling = async (
|
||||
};
|
||||
|
||||
export const useServiceUninstaller = () => {
|
||||
const { mutateRunningMode, mutateServiceOk } = useSystemState();
|
||||
const { mutateSystemState } = useSystemState();
|
||||
|
||||
const uninstallServiceAndRestartCore = useCallback(async () => {
|
||||
await executeWithErrorHandling(() => stopCore(), "Stopping Core...");
|
||||
|
||||
await executeWithErrorHandling(
|
||||
() => uninstallService(),
|
||||
"Uninstalling Service...",
|
||||
"Service Uninstalled Successfully",
|
||||
);
|
||||
|
||||
await executeWithErrorHandling(() => restartCore(), "Restarting Core...");
|
||||
await mutateRunningMode();
|
||||
await mutateServiceOk();
|
||||
}, [mutateRunningMode, mutateServiceOk]);
|
||||
try {
|
||||
await executeWithErrorHandling(() => stopCore(), "Stopping Core...");
|
||||
await executeWithErrorHandling(
|
||||
() => uninstallService(),
|
||||
"Uninstalling Service...",
|
||||
"Service Uninstalled Successfully",
|
||||
);
|
||||
} catch (ignore) {
|
||||
} finally {
|
||||
await executeWithErrorHandling(
|
||||
() => restartCore(),
|
||||
"Restarting Core...",
|
||||
"Clash Core Restarted",
|
||||
);
|
||||
await mutateSystemState();
|
||||
}
|
||||
}, [mutateSystemState]);
|
||||
|
||||
return { uninstallServiceAndRestartCore };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user