mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 08:45:41 +08:00
feat: Modify startup logic and install services by default
This commit is contained in:
@@ -1,134 +0,0 @@
|
||||
import { KeyedMutator } from "swr";
|
||||
import { useState } from "react";
|
||||
import { useLockFn } from "ahooks";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { installService, uninstallService } from "@/services/cmds";
|
||||
import { Notice } from "@/components/base";
|
||||
import { LoadingButton } from "@mui/lab";
|
||||
import { PasswordInput } from "./password-input";
|
||||
import getSystem from "@/utils/get-system";
|
||||
|
||||
interface Props {
|
||||
status: "active" | "installed" | "unknown" | "uninstall";
|
||||
mutate: KeyedMutator<"active" | "installed" | "unknown" | "uninstall">;
|
||||
patchVerge: (value: Partial<IVergeConfig>) => Promise<void>;
|
||||
onChangeData: (patch: Partial<IVergeConfig>) => void;
|
||||
}
|
||||
|
||||
export const ServiceSwitcher = (props: Props) => {
|
||||
const { status, mutate, patchVerge, onChangeData } = props;
|
||||
const isWindows = getSystem() === "windows";
|
||||
const isActive = status === "active";
|
||||
const isInstalled = status === "installed";
|
||||
const isUninstall = status === "uninstall" || status === "unknown";
|
||||
|
||||
const { t } = useTranslation();
|
||||
const [serviceLoading, setServiceLoading] = useState(false);
|
||||
const [uninstallServiceLoaing, setUninstallServiceLoading] = useState(false);
|
||||
const [openInstall, setOpenInstall] = useState(false);
|
||||
const [openUninstall, setOpenUninstall] = useState(false);
|
||||
|
||||
async function install(passwd: string) {
|
||||
try {
|
||||
setOpenInstall(false);
|
||||
await installService(passwd);
|
||||
await mutate();
|
||||
setTimeout(() => {
|
||||
mutate();
|
||||
}, 2000);
|
||||
Notice.success(t("Service Installed Successfully"));
|
||||
setServiceLoading(false);
|
||||
} catch (err: any) {
|
||||
await mutate();
|
||||
setTimeout(() => {
|
||||
mutate();
|
||||
}, 2000);
|
||||
Notice.error(err.message || err.toString());
|
||||
setServiceLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function uninstall(passwd: string) {
|
||||
try {
|
||||
setOpenUninstall(false);
|
||||
await uninstallService(passwd);
|
||||
await mutate();
|
||||
setTimeout(() => {
|
||||
mutate();
|
||||
}, 2000);
|
||||
Notice.success(t("Service Uninstalled Successfully"));
|
||||
setUninstallServiceLoading(false);
|
||||
} catch (err: any) {
|
||||
await mutate();
|
||||
setTimeout(() => {
|
||||
mutate();
|
||||
}, 2000);
|
||||
Notice.error(err.message || err.toString());
|
||||
setUninstallServiceLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
const onInstallOrEnableService = useLockFn(async () => {
|
||||
setServiceLoading(true);
|
||||
if (isUninstall) {
|
||||
// install service
|
||||
if (isWindows) {
|
||||
await install("");
|
||||
} else {
|
||||
setOpenInstall(true);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
// enable or disable service
|
||||
await patchVerge({ enable_service_mode: !isActive });
|
||||
onChangeData({ enable_service_mode: !isActive });
|
||||
await mutate();
|
||||
setTimeout(() => {
|
||||
mutate();
|
||||
}, 2000);
|
||||
setServiceLoading(false);
|
||||
} catch (err: any) {
|
||||
await mutate();
|
||||
Notice.error(err.message || err.toString());
|
||||
setServiceLoading(false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const onUninstallService = useLockFn(async () => {
|
||||
setUninstallServiceLoading(true);
|
||||
if (isWindows) {
|
||||
await uninstall("");
|
||||
} else {
|
||||
setOpenUninstall(true);
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
{openInstall && <PasswordInput onConfirm={install} />}
|
||||
{openUninstall && <PasswordInput onConfirm={uninstall} />}
|
||||
|
||||
<LoadingButton
|
||||
size="small"
|
||||
variant={isUninstall ? "outlined" : "contained"}
|
||||
onClick={onInstallOrEnableService}
|
||||
loading={serviceLoading}
|
||||
>
|
||||
{isActive ? t("Disable") : isInstalled ? t("Enable") : t("Install")}
|
||||
</LoadingButton>
|
||||
{isInstalled && (
|
||||
<LoadingButton
|
||||
size="small"
|
||||
variant="outlined"
|
||||
color="error"
|
||||
sx={{ ml: 1 }}
|
||||
onClick={onUninstallService}
|
||||
loading={uninstallServiceLoaing}
|
||||
>
|
||||
{t("Uninstall")}
|
||||
</LoadingButton>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
@@ -7,7 +7,6 @@ import { useVerge } from "@/hooks/use-verge";
|
||||
import { DialogRef, Notice, Switch } from "@/components/base";
|
||||
import { SettingList, SettingItem } from "./mods/setting-comp";
|
||||
import { GuardState } from "./mods/guard-state";
|
||||
import { ServiceSwitcher } from "./mods/service-switcher";
|
||||
import { SysproxyViewer } from "./mods/sysproxy-viewer";
|
||||
import { TunViewer } from "./mods/tun-viewer";
|
||||
import { TooltipIcon } from "@/components/base/base-tooltip-icon";
|
||||
@@ -67,37 +66,15 @@ const SettingSystem = ({ onError }: Props) => {
|
||||
onCatch={onError}
|
||||
onFormat={onSwitchFormat}
|
||||
onChange={(e) => {
|
||||
if (serviceStatus !== "active") {
|
||||
onChangeData({ enable_tun_mode: false });
|
||||
} else {
|
||||
onChangeData({ enable_tun_mode: e });
|
||||
}
|
||||
onChangeData({ enable_tun_mode: e });
|
||||
}}
|
||||
onGuard={(e) => {
|
||||
if (serviceStatus !== "active" && e) {
|
||||
Notice.error(t("Please Enable Service Mode"));
|
||||
return Promise.resolve();
|
||||
} else {
|
||||
return patchVerge({ enable_tun_mode: e });
|
||||
}
|
||||
return patchVerge({ enable_tun_mode: e });
|
||||
}}
|
||||
>
|
||||
<Switch edge="end" />
|
||||
</GuardState>
|
||||
</SettingItem>
|
||||
|
||||
<SettingItem
|
||||
label={t("Service Mode")}
|
||||
extra={<TooltipIcon title={t("Service Mode Info")} />}
|
||||
>
|
||||
<ServiceSwitcher
|
||||
status={serviceStatus ?? "unknown"}
|
||||
mutate={mutateServiceStatus}
|
||||
patchVerge={patchVerge}
|
||||
onChangeData={onChangeData}
|
||||
/>
|
||||
</SettingItem>
|
||||
|
||||
<SettingItem
|
||||
label={t("System Proxy")}
|
||||
extra={
|
||||
|
||||
@@ -200,15 +200,6 @@ export async function checkService() {
|
||||
return "uninstall";
|
||||
}
|
||||
}
|
||||
|
||||
export async function installService(passwd: string) {
|
||||
return invoke<void>("install_service", { passwd });
|
||||
}
|
||||
|
||||
export async function uninstallService(passwd: string) {
|
||||
return invoke<void>("uninstall_service", { passwd });
|
||||
}
|
||||
|
||||
export async function invoke_uwp_tool() {
|
||||
return invoke<void>("invoke_uwp_tool").catch((err) =>
|
||||
Notice.error(err?.message || err.toString(), 1500)
|
||||
|
||||
Reference in New Issue
Block a user