2025-02-19 13:56:22 +08:00
|
|
|
|
import useSWR, { mutate } from "swr";
|
2025-03-17 09:48:44 +08:00
|
|
|
|
import { useRef, useEffect } from "react";
|
2022-03-12 23:07:45 +08:00
|
|
|
|
import { useTranslation } from "react-i18next";
|
2025-02-19 13:56:22 +08:00
|
|
|
|
import {
|
|
|
|
|
|
SettingsRounded,
|
|
|
|
|
|
PlayArrowRounded,
|
|
|
|
|
|
PauseRounded,
|
2025-03-03 14:42:31 +08:00
|
|
|
|
WarningRounded,
|
|
|
|
|
|
BuildRounded,
|
2025-05-13 01:56:19 +08:00
|
|
|
|
DeleteForeverRounded,
|
2025-02-19 13:56:22 +08:00
|
|
|
|
} from "@mui/icons-material";
|
2022-11-20 20:12:58 +08:00
|
|
|
|
import { useVerge } from "@/hooks/use-verge";
|
2025-05-04 22:17:08 +08:00
|
|
|
|
import { DialogRef, Switch } from "@/components/base";
|
2022-11-20 21:48:39 +08:00
|
|
|
|
import { SettingList, SettingItem } from "./mods/setting-comp";
|
|
|
|
|
|
import { GuardState } from "./mods/guard-state";
|
|
|
|
|
|
import { SysproxyViewer } from "./mods/sysproxy-viewer";
|
2024-02-20 23:27:03 +08:00
|
|
|
|
import { TunViewer } from "./mods/tun-viewer";
|
2024-06-26 08:10:18 +08:00
|
|
|
|
import { TooltipIcon } from "@/components/base/base-tooltip-icon";
|
2025-03-03 14:42:31 +08:00
|
|
|
|
import {
|
|
|
|
|
|
getSystemProxy,
|
|
|
|
|
|
getAutotemProxy,
|
|
|
|
|
|
installService,
|
2025-05-13 01:56:19 +08:00
|
|
|
|
uninstallService,
|
2025-05-12 19:04:08 +08:00
|
|
|
|
restartCore,
|
2025-05-13 01:56:19 +08:00
|
|
|
|
startCore,
|
|
|
|
|
|
stopCore,
|
2025-03-03 14:42:31 +08:00
|
|
|
|
} from "@/services/cmds";
|
|
|
|
|
|
import { useLockFn } from "ahooks";
|
2025-03-31 08:16:14 +08:00
|
|
|
|
import { Button, Tooltip } from "@mui/material";
|
|
|
|
|
|
import { useSystemState } from "@/hooks/use-system-state";
|
2025-04-19 15:10:49 +08:00
|
|
|
|
import { closeAllConnections } from "@/services/api";
|
2025-05-04 22:17:08 +08:00
|
|
|
|
import { showNotice } from "@/services/noticeService";
|
2022-01-17 02:42:52 +08:00
|
|
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
|
onError?: (err: Error) => void;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const SettingSystem = ({ onError }: Props) => {
|
2022-03-12 23:07:45 +08:00
|
|
|
|
const { t } = useTranslation();
|
2022-09-07 01:51:43 +08:00
|
|
|
|
|
2022-11-20 20:12:58 +08:00
|
|
|
|
const { verge, mutateVerge, patchVerge } = useVerge();
|
2022-04-25 16:12:04 +08:00
|
|
|
|
|
2025-02-19 13:56:22 +08:00
|
|
|
|
const { data: sysproxy } = useSWR("getSystemProxy", getSystemProxy);
|
|
|
|
|
|
const { data: autoproxy } = useSWR("getAutotemProxy", getAutotemProxy);
|
2025-03-31 08:16:14 +08:00
|
|
|
|
|
2025-05-12 19:04:08 +08:00
|
|
|
|
const { isAdminMode, isSidecarMode, mutateRunningMode, isServiceOk } =
|
|
|
|
|
|
useSystemState();
|
|
|
|
|
|
|
2025-03-31 08:16:14 +08:00
|
|
|
|
// 判断Tun模式是否可用 - 当处于服务模式或管理员模式时可用
|
2025-05-12 19:04:08 +08:00
|
|
|
|
const isTunAvailable = isServiceOk || (isSidecarMode && !isAdminMode);
|
|
|
|
|
|
|
|
|
|
|
|
console.log("is tun isTunAvailable:", isTunAvailable);
|
2025-03-03 14:42:31 +08:00
|
|
|
|
|
2022-11-20 21:48:39 +08:00
|
|
|
|
const sysproxyRef = useRef<DialogRef>(null);
|
2024-02-20 23:27:03 +08:00
|
|
|
|
const tunRef = useRef<DialogRef>(null);
|
2022-11-20 21:48:39 +08:00
|
|
|
|
|
2022-01-17 02:42:52 +08:00
|
|
|
|
const {
|
2022-03-12 23:07:45 +08:00
|
|
|
|
enable_tun_mode,
|
|
|
|
|
|
enable_auto_launch,
|
2022-03-26 18:56:16 +08:00
|
|
|
|
enable_silent_start,
|
2022-03-12 23:07:45 +08:00
|
|
|
|
enable_system_proxy,
|
2025-02-19 13:56:22 +08:00
|
|
|
|
proxy_auto_config,
|
2022-11-20 20:12:58 +08:00
|
|
|
|
} = verge ?? {};
|
2022-01-17 02:42:52 +08:00
|
|
|
|
|
|
|
|
|
|
const onSwitchFormat = (_e: any, value: boolean) => value;
|
2022-11-19 17:22:29 +08:00
|
|
|
|
const onChangeData = (patch: Partial<IVergeConfig>) => {
|
2022-11-20 20:12:58 +08:00
|
|
|
|
mutateVerge({ ...verge, ...patch }, false);
|
2022-01-17 02:42:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2025-02-19 13:56:22 +08:00
|
|
|
|
const updateProxyStatus = async () => {
|
|
|
|
|
|
// 等待一小段时间让系统代理状态变化
|
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
|
|
|
|
await mutate("getSystemProxy");
|
|
|
|
|
|
await mutate("getAutotemProxy");
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-05-13 01:56:19 +08:00
|
|
|
|
// 抽象服务操作逻辑
|
|
|
|
|
|
const handleServiceOperation = useLockFn(
|
|
|
|
|
|
async (
|
|
|
|
|
|
{
|
|
|
|
|
|
beforeMsg,
|
|
|
|
|
|
action,
|
|
|
|
|
|
actionMsg,
|
|
|
|
|
|
successMsg,
|
|
|
|
|
|
}: {
|
|
|
|
|
|
beforeMsg: string;
|
|
|
|
|
|
action: () => Promise<void>;
|
|
|
|
|
|
actionMsg: string;
|
|
|
|
|
|
successMsg: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
showNotice("info", t(beforeMsg), 1000);
|
|
|
|
|
|
await stopCore();
|
|
|
|
|
|
showNotice("info", t(actionMsg), 1000);
|
|
|
|
|
|
await action();
|
|
|
|
|
|
showNotice("success", t(successMsg), 2000);
|
|
|
|
|
|
showNotice("info", t("Starting Core..."), 1000);
|
|
|
|
|
|
await startCore();
|
|
|
|
|
|
await mutateRunningMode();
|
|
|
|
|
|
} catch (err: any) {
|
|
|
|
|
|
showNotice("error", err.message || err.toString(), 3000);
|
|
|
|
|
|
try {
|
|
|
|
|
|
showNotice("info", t("Try running core as Sidecar..."), 1000);
|
|
|
|
|
|
await startCore();
|
|
|
|
|
|
await mutateRunningMode();
|
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
|
showNotice("error", e?.message || e?.toString(), 3000);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-03 14:42:31 +08:00
|
|
|
|
}
|
2025-05-13 01:56:19 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 安装系统服务
|
|
|
|
|
|
const onInstallService = () =>
|
|
|
|
|
|
handleServiceOperation({
|
|
|
|
|
|
beforeMsg: "Stopping Core...",
|
|
|
|
|
|
action: installService,
|
|
|
|
|
|
actionMsg: "Installing Service...",
|
|
|
|
|
|
successMsg: "Service Installed Successfully",
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 卸载系统服务
|
|
|
|
|
|
const onUninstallService = () =>
|
|
|
|
|
|
handleServiceOperation({
|
|
|
|
|
|
beforeMsg: "Stopping Core...",
|
|
|
|
|
|
action: uninstallService,
|
|
|
|
|
|
actionMsg: "Uninstalling Service...",
|
|
|
|
|
|
successMsg: "Service Uninstalled Successfully",
|
|
|
|
|
|
});
|
2025-03-03 14:42:31 +08:00
|
|
|
|
|
2022-01-17 02:42:52 +08:00
|
|
|
|
return (
|
2022-03-12 23:07:45 +08:00
|
|
|
|
<SettingList title={t("System Setting")}>
|
2022-11-20 21:48:39 +08:00
|
|
|
|
<SysproxyViewer ref={sysproxyRef} />
|
2024-02-20 23:27:03 +08:00
|
|
|
|
<TunViewer ref={tunRef} />
|
2022-09-07 01:51:43 +08:00
|
|
|
|
|
2024-02-19 18:10:10 +08:00
|
|
|
|
<SettingItem
|
|
|
|
|
|
label={t("Tun Mode")}
|
|
|
|
|
|
extra={
|
2025-03-03 14:42:31 +08:00
|
|
|
|
<>
|
|
|
|
|
|
<TooltipIcon
|
|
|
|
|
|
title={t("Tun Mode Info")}
|
|
|
|
|
|
icon={SettingsRounded}
|
|
|
|
|
|
onClick={() => tunRef.current?.open()}
|
|
|
|
|
|
/>
|
2025-05-12 19:04:08 +08:00
|
|
|
|
{!isServiceOk && !isAdminMode && (
|
2025-03-03 14:42:31 +08:00
|
|
|
|
<Tooltip title={t("TUN requires Service Mode")}>
|
|
|
|
|
|
<WarningRounded sx={{ color: "warning.main", mr: 1 }} />
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
)}
|
2025-05-12 19:04:08 +08:00
|
|
|
|
{!isServiceOk && !isAdminMode && (
|
2025-03-03 14:42:31 +08:00
|
|
|
|
<Tooltip title={t("Install Service")}>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="outlined"
|
|
|
|
|
|
color="primary"
|
|
|
|
|
|
size="small"
|
|
|
|
|
|
onClick={onInstallService}
|
|
|
|
|
|
sx={{ mr: 1, minWidth: "32px", p: "4px" }}
|
|
|
|
|
|
>
|
|
|
|
|
|
<BuildRounded fontSize="small" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
)}
|
2025-05-13 01:56:19 +08:00
|
|
|
|
{
|
|
|
|
|
|
isServiceOk && (
|
|
|
|
|
|
<Tooltip title={t("Uninstall Service")}>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
// variant="outlined"
|
|
|
|
|
|
color="secondary"
|
|
|
|
|
|
size="small"
|
|
|
|
|
|
onClick={onUninstallService}
|
|
|
|
|
|
sx={{ mr: 1, minWidth: "32px", p: "4px" }}
|
|
|
|
|
|
>
|
|
|
|
|
|
<DeleteForeverRounded fontSize="small" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
2025-03-03 14:42:31 +08:00
|
|
|
|
</>
|
2024-02-19 18:10:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
>
|
2022-02-25 02:09:39 +08:00
|
|
|
|
<GuardState
|
2022-03-12 23:07:45 +08:00
|
|
|
|
value={enable_tun_mode ?? false}
|
2022-02-25 02:09:39 +08:00
|
|
|
|
valueProps="checked"
|
|
|
|
|
|
onCatch={onError}
|
|
|
|
|
|
onFormat={onSwitchFormat}
|
2024-07-15 19:58:05 +08:00
|
|
|
|
onChange={(e) => {
|
2025-05-12 19:04:08 +08:00
|
|
|
|
// 非 Service 状态禁止切换
|
|
|
|
|
|
if (!isServiceOk) return;
|
2024-10-10 00:34:36 +08:00
|
|
|
|
onChangeData({ enable_tun_mode: e });
|
2024-10-06 01:09:59 +08:00
|
|
|
|
}}
|
|
|
|
|
|
onGuard={(e) => {
|
2025-05-12 19:04:08 +08:00
|
|
|
|
// 非 Service 状态禁止切换
|
|
|
|
|
|
if (!isServiceOk) {
|
|
|
|
|
|
showNotice("error", t("TUN requires Service Mode"), 2000);
|
2025-03-03 14:42:31 +08:00
|
|
|
|
return Promise.reject(new Error(t("TUN requires Service Mode")));
|
|
|
|
|
|
}
|
2024-10-10 00:34:36 +08:00
|
|
|
|
return patchVerge({ enable_tun_mode: e });
|
2024-07-15 19:58:05 +08:00
|
|
|
|
}}
|
2022-02-25 02:09:39 +08:00
|
|
|
|
>
|
2025-05-13 01:11:20 +08:00
|
|
|
|
<Switch edge="end" disabled={!isServiceOk} />
|
2022-02-25 02:09:39 +08:00
|
|
|
|
</GuardState>
|
|
|
|
|
|
</SettingItem>
|
2022-09-07 01:51:43 +08:00
|
|
|
|
<SettingItem
|
|
|
|
|
|
label={t("System Proxy")}
|
|
|
|
|
|
extra={
|
2024-06-07 10:32:27 +08:00
|
|
|
|
<>
|
2024-06-26 08:10:18 +08:00
|
|
|
|
<TooltipIcon
|
|
|
|
|
|
title={t("System Proxy Info")}
|
2024-07-13 19:01:16 +08:00
|
|
|
|
icon={SettingsRounded}
|
2024-06-07 10:32:27 +08:00
|
|
|
|
onClick={() => sysproxyRef.current?.open()}
|
2024-06-26 08:10:18 +08:00
|
|
|
|
/>
|
2025-03-04 06:57:42 +08:00
|
|
|
|
{proxy_auto_config ? (
|
|
|
|
|
|
autoproxy?.enable ? (
|
|
|
|
|
|
<PlayArrowRounded sx={{ color: "success.main", mr: 1 }} />
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<PauseRounded sx={{ color: "error.main", mr: 1 }} />
|
|
|
|
|
|
)
|
|
|
|
|
|
) : sysproxy?.enable ? (
|
2025-02-19 13:56:22 +08:00
|
|
|
|
<PlayArrowRounded sx={{ color: "success.main", mr: 1 }} />
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<PauseRounded sx={{ color: "error.main", mr: 1 }} />
|
|
|
|
|
|
)}
|
2024-06-07 10:32:27 +08:00
|
|
|
|
</>
|
2022-09-07 01:51:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
>
|
2022-01-17 02:42:52 +08:00
|
|
|
|
<GuardState
|
2022-09-07 01:51:43 +08:00
|
|
|
|
value={enable_system_proxy ?? false}
|
2022-01-17 02:42:52 +08:00
|
|
|
|
valueProps="checked"
|
|
|
|
|
|
onCatch={onError}
|
|
|
|
|
|
onFormat={onSwitchFormat}
|
2022-09-07 01:51:43 +08:00
|
|
|
|
onChange={(e) => onChangeData({ enable_system_proxy: e })}
|
2025-02-19 13:56:22 +08:00
|
|
|
|
onGuard={async (e) => {
|
2025-04-19 15:10:49 +08:00
|
|
|
|
if (!e && verge?.auto_close_connection) {
|
|
|
|
|
|
closeAllConnections();
|
|
|
|
|
|
}
|
2025-02-19 13:56:22 +08:00
|
|
|
|
await patchVerge({ enable_system_proxy: e });
|
|
|
|
|
|
await updateProxyStatus();
|
|
|
|
|
|
}}
|
2022-01-17 02:42:52 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Switch edge="end" />
|
|
|
|
|
|
</GuardState>
|
|
|
|
|
|
</SettingItem>
|
|
|
|
|
|
|
2025-05-12 19:04:08 +08:00
|
|
|
|
<SettingItem
|
2025-03-31 03:22:24 +08:00
|
|
|
|
label={t("Auto Launch")}
|
|
|
|
|
|
extra={
|
|
|
|
|
|
isAdminMode && (
|
2025-05-12 19:04:08 +08:00
|
|
|
|
<Tooltip
|
|
|
|
|
|
title={t("Administrator mode may not support auto launch")}
|
|
|
|
|
|
>
|
2025-03-31 03:22:24 +08:00
|
|
|
|
<WarningRounded sx={{ color: "warning.main", mr: 1 }} />
|
|
|
|
|
|
</Tooltip>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
>
|
2022-03-26 18:56:16 +08:00
|
|
|
|
<GuardState
|
2022-09-07 01:51:43 +08:00
|
|
|
|
value={enable_auto_launch ?? false}
|
2022-03-26 18:56:16 +08:00
|
|
|
|
valueProps="checked"
|
|
|
|
|
|
onCatch={onError}
|
|
|
|
|
|
onFormat={onSwitchFormat}
|
2025-03-31 03:22:24 +08:00
|
|
|
|
onChange={(e) => {
|
2025-04-19 12:37:01 +08:00
|
|
|
|
// 移除管理员模式检查提示
|
2025-03-31 03:22:24 +08:00
|
|
|
|
onChangeData({ enable_auto_launch: e });
|
|
|
|
|
|
}}
|
2025-03-17 13:51:52 +08:00
|
|
|
|
onGuard={async (e) => {
|
2025-03-31 03:22:24 +08:00
|
|
|
|
if (isAdminMode) {
|
2025-05-12 19:04:08 +08:00
|
|
|
|
showNotice(
|
|
|
|
|
|
"info",
|
|
|
|
|
|
t("Administrator mode may not support auto launch"),
|
|
|
|
|
|
2000,
|
|
|
|
|
|
);
|
2025-03-31 03:22:24 +08:00
|
|
|
|
}
|
2025-05-12 19:04:08 +08:00
|
|
|
|
|
2025-03-17 13:51:52 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 在应用更改之前先触发UI更新,让用户立即看到反馈
|
|
|
|
|
|
onChangeData({ enable_auto_launch: e });
|
|
|
|
|
|
await patchVerge({ enable_auto_launch: e });
|
|
|
|
|
|
// 更新实际状态
|
|
|
|
|
|
await mutate("getAutoLaunchStatus");
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
// 如果出错,恢复原始状态
|
|
|
|
|
|
onChangeData({ enable_auto_launch: !e });
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
|
}
|
|
|
|
|
|
}}
|
2022-03-26 18:56:16 +08:00
|
|
|
|
>
|
2025-04-19 12:37:01 +08:00
|
|
|
|
<Switch edge="end" />
|
2022-03-26 18:56:16 +08:00
|
|
|
|
</GuardState>
|
|
|
|
|
|
</SettingItem>
|
|
|
|
|
|
|
2024-06-26 05:33:06 +08:00
|
|
|
|
<SettingItem
|
|
|
|
|
|
label={t("Silent Start")}
|
2024-11-14 03:01:37 +08:00
|
|
|
|
extra={
|
|
|
|
|
|
<TooltipIcon title={t("Silent Start Info")} sx={{ opacity: "0.7" }} />
|
|
|
|
|
|
}
|
2024-06-26 05:33:06 +08:00
|
|
|
|
>
|
2022-01-17 02:42:52 +08:00
|
|
|
|
<GuardState
|
2022-09-07 01:51:43 +08:00
|
|
|
|
value={enable_silent_start ?? false}
|
2022-01-17 02:42:52 +08:00
|
|
|
|
valueProps="checked"
|
|
|
|
|
|
onCatch={onError}
|
|
|
|
|
|
onFormat={onSwitchFormat}
|
2022-09-07 01:51:43 +08:00
|
|
|
|
onChange={(e) => onChangeData({ enable_silent_start: e })}
|
2022-11-20 20:12:58 +08:00
|
|
|
|
onGuard={(e) => patchVerge({ enable_silent_start: e })}
|
2022-01-17 02:42:52 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Switch edge="end" />
|
|
|
|
|
|
</GuardState>
|
|
|
|
|
|
</SettingItem>
|
|
|
|
|
|
</SettingList>
|
|
|
|
|
|
);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default SettingSystem;
|