fix: service mode error

This commit is contained in:
GyDi 2022-05-13 02:11:03 +08:00
parent b93284bc2f
commit 31c6cbc0a2
No known key found for this signature in database
GPG key ID: 1C95E0D3467B3084
2 changed files with 131 additions and 108 deletions

View file

@ -300,7 +300,9 @@ pub mod win_service {
/// stop service /// stop service
pub async fn stop_service() -> Result<()> { pub async fn stop_service() -> Result<()> {
let url = format!("{SERVICE_URL}/stop_service"); let url = format!("{SERVICE_URL}/stop_service");
let res = reqwest::Client::new() let res = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.post(url) .post(url)
.send() .send()
.await? .await?
@ -318,7 +320,11 @@ pub mod win_service {
/// check the windows service status /// check the windows service status
pub async fn check_service() -> Result<JsonResponse> { pub async fn check_service() -> Result<JsonResponse> {
let url = format!("{SERVICE_URL}/get_clash"); let url = format!("{SERVICE_URL}/get_clash");
let response = reqwest::get(url) let response = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.get(url)
.send()
.await? .await?
.json::<JsonResponse>() .json::<JsonResponse>()
.await .await
@ -351,7 +357,9 @@ pub mod win_service {
map.insert("log_file", log_path); map.insert("log_file", log_path);
let url = format!("{SERVICE_URL}/start_clash"); let url = format!("{SERVICE_URL}/start_clash");
let res = reqwest::Client::new() let res = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.post(url) .post(url)
.json(&map) .json(&map)
.send() .send()
@ -370,7 +378,9 @@ pub mod win_service {
/// stop the clash by service /// stop the clash by service
pub(super) async fn stop_clash_by_service() -> Result<()> { pub(super) async fn stop_clash_by_service() -> Result<()> {
let url = format!("{SERVICE_URL}/stop_clash"); let url = format!("{SERVICE_URL}/stop_clash");
let res = reqwest::Client::new() let res = reqwest::ClientBuilder::new()
.no_proxy()
.build()?
.post(url) .post(url)
.send() .send()
.await? .await?

View file

@ -1,104 +1,117 @@
import useSWR, { useSWRConfig } from "swr"; import useSWR, { useSWRConfig } from "swr";
import { useLockFn } from "ahooks"; import { useLockFn } from "ahooks";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { import {
Button, Button,
Dialog, Dialog,
DialogContent, DialogContent,
DialogTitle, DialogTitle,
Stack, Stack,
Typography, Typography,
} from "@mui/material"; } from "@mui/material";
import { import {
checkService, checkService,
installService, installService,
uninstallService, uninstallService,
patchVergeConfig, patchVergeConfig,
} from "../../services/cmds"; } from "../../services/cmds";
import Notice from "../base/base-notice"; import Notice from "../base/base-notice";
import noop from "../../utils/noop"; import noop from "../../utils/noop";
interface Props { interface Props {
open: boolean; open: boolean;
enable: boolean; enable: boolean;
onClose: () => void; onClose: () => void;
onError?: (err: Error) => void; onError?: (err: Error) => void;
} }
const ServiceMode = (props: Props) => { const ServiceMode = (props: Props) => {
const { open, enable, onClose, onError = noop } = props; const { open, enable, onClose, onError = noop } = props;
const { t } = useTranslation(); const { t } = useTranslation();
const { mutate } = useSWRConfig(); const { mutate } = useSWRConfig();
const { data: status } = useSWR("checkService", checkService, { const { data: status } = useSWR("checkService", checkService, {
revalidateIfStale: true, revalidateIfStale: true,
shouldRetryOnError: false, shouldRetryOnError: false,
}); });
const state = status != null ? status : "pending"; const state = status != null ? status : "pending";
const onInstall = useLockFn(async () => { const onInstall = useLockFn(async () => {
try { try {
await installService(); await installService();
mutate("checkService"); mutate("checkService");
onClose(); onClose();
Notice.success("Service installed successfully"); Notice.success("Service installed successfully");
} catch (err: any) { } catch (err: any) {
mutate("checkService"); mutate("checkService");
onError(err); onError(err);
} }
}); });
const onUninstall = useLockFn(async () => { const onUninstall = useLockFn(async () => {
try { try {
if (state === "active" && enable) { if (state === "active" && enable) {
await patchVergeConfig({ enable_service_mode: false }); await patchVergeConfig({ enable_service_mode: false });
} }
await uninstallService(); await uninstallService();
mutate("checkService"); mutate("checkService");
onClose(); onClose();
Notice.success("Service uninstalled successfully"); Notice.success("Service uninstalled successfully");
} catch (err: any) { } catch (err: any) {
mutate("checkService"); mutate("checkService");
onError(err); onError(err);
} }
}); });
return ( // fix unhandle error of the service mode
<Dialog open={open} onClose={onClose}> const onDisable = useLockFn(async () => {
<DialogTitle>{t("Service Mode")}</DialogTitle> await patchVergeConfig({ enable_service_mode: false });
mutate("checkService");
<DialogContent sx={{ width: 360, userSelect: "text" }}> onClose();
<Typography>Current State: {state}</Typography> });
{(state === "unknown" || state === "uninstall") && ( return (
<Typography> <Dialog open={open} onClose={onClose}>
Infomation: Please make sure the Clash Verge Service is installed <DialogTitle>{t("Service Mode")}</DialogTitle>
and enabled
</Typography> <DialogContent sx={{ width: 360, userSelect: "text" }}>
)} <Typography>Current State: {state}</Typography>
<Stack {(state === "unknown" || state === "uninstall") && (
direction="row" <Typography>
spacing={1} Infomation: Please make sure the Clash Verge Service is installed
sx={{ mt: 4, justifyContent: "flex-end" }} and enabled
> </Typography>
{state === "uninstall" && ( )}
<Button variant="contained" onClick={onInstall}>
Install <Stack
</Button> direction="row"
)} spacing={1}
sx={{ mt: 4, justifyContent: "flex-end" }}
{(state === "active" || state === "installed") && ( >
<Button variant="outlined" onClick={onUninstall}> {state === "uninstall" && enable && (
Uninstall <Button variant="contained" onClick={onDisable}>
</Button> Disable Service Mode
)} </Button>
</Stack> )}
</DialogContent>
</Dialog> {state === "uninstall" && (
); <Button variant="contained" onClick={onInstall}>
}; Install
</Button>
export default ServiceMode; )}
{(state === "active" || state === "installed") && (
<Button variant="outlined" onClick={onUninstall}>
Uninstall
</Button>
)}
</Stack>
</DialogContent>
</Dialog>
);
};
export default ServiceMode;