feat: support to change external controller

This commit is contained in:
GyDi
2022-11-06 23:23:26 +08:00
parent 7f65c501c6
commit e66a89208d
5 changed files with 131 additions and 10 deletions

View File

@@ -0,0 +1,101 @@
import useSWR from "swr";
import { useState } from "react";
import { useLockFn } from "ahooks";
import { useTranslation } from "react-i18next";
import {
Button,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
List,
ListItem,
ListItemText,
TextField,
} from "@mui/material";
import { getClashInfo, patchClashConfig } from "@/services/cmds";
import { ModalHandler } from "@/hooks/use-modal-handler";
import { getAxios } from "@/services/api";
import Notice from "@/components/base/base-notice";
interface Props {
handler: ModalHandler;
}
const ControllerViewer = ({ handler }: Props) => {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const { data: clashInfo, mutate } = useSWR("getClashInfo", getClashInfo);
const [controller, setController] = useState(clashInfo?.server || "");
const [secret, setSecret] = useState(clashInfo?.secret || "");
if (handler) {
handler.current = {
open: () => {
setOpen(true);
setController(clashInfo?.server || "");
setSecret(clashInfo?.secret || "");
},
close: () => setOpen(false),
};
}
const onSave = useLockFn(async () => {
try {
await patchClashConfig({ "external-controller": controller, secret });
mutate();
// 刷新接口
getAxios(true);
Notice.success("Change Clash Config successfully!", 1000);
setOpen(false);
} catch (err) {
console.log(err);
}
});
return (
<Dialog open={open} onClose={() => setOpen(false)}>
<DialogTitle>{t("Clash Port")}</DialogTitle>
<DialogContent sx={{ width: 400 }}>
<List>
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary="External Controller" />
<TextField
size="small"
autoComplete="off"
sx={{ width: 175 }}
value={controller}
placeholder="Required"
onChange={(e) => setController(e.target.value)}
/>
</ListItem>
<ListItem sx={{ padding: "5px 2px" }}>
<ListItemText primary="Core Secret" />
<TextField
size="small"
autoComplete="off"
sx={{ width: 175 }}
value={secret}
placeholder="Recommanded"
onChange={(e) => setSecret(e.target.value)}
/>
</ListItem>
</List>
</DialogContent>
<DialogActions>
<Button variant="outlined" onClick={() => setOpen(false)}>
{t("Cancel")}
</Button>
<Button onClick={onSave} variant="contained">
{t("Save")}
</Button>
</DialogActions>
</Dialog>
);
};
export default ControllerViewer;

View File

@@ -18,6 +18,7 @@ import CoreSwitch from "./mods/core-switch";
import WebUIViewer from "./mods/web-ui-viewer";
import ClashFieldViewer from "./mods/clash-field-viewer";
import ClashPortViewer from "./mods/clash-port-viewer";
import ControllerViewer from "./mods/controller-viewer";
interface Props {
onError: (err: Error) => void;
@@ -42,6 +43,7 @@ const SettingClash = ({ onError }: Props) => {
const webUIHandler = useModalHandler();
const fieldHandler = useModalHandler();
const portHandler = useModalHandler();
const controllerHandler = useModalHandler();
const onSwitchFormat = (_e: any, value: boolean) => value;
const onChangeData = (patch: Partial<ApiType.ConfigData>) => {
@@ -62,6 +64,7 @@ const SettingClash = ({ onError }: Props) => {
<WebUIViewer handler={webUIHandler} onError={onError} />
<ClashFieldViewer handler={fieldHandler} />
<ClashPortViewer handler={portHandler} />
<ControllerViewer handler={controllerHandler} />
<SettingItem label={t("Allow Lan")}>
<GuardState
@@ -113,7 +116,7 @@ const SettingClash = ({ onError }: Props) => {
autoComplete="off"
size="small"
value={mixedPort ?? 0}
sx={{ width: 100, input: { py: "7.5px" } }}
sx={{ width: 100, input: { py: "7.5px", cursor: "pointer" } }}
onClick={(e) => {
portHandler.current.open();
(e.target as any).blur();
@@ -121,6 +124,17 @@ const SettingClash = ({ onError }: Props) => {
/>
</SettingItem>
<SettingItem label={t("External Controller")}>
<IconButton
color="inherit"
size="small"
sx={{ my: "2px" }}
onClick={() => controllerHandler.current.open()}
>
<ArrowForward />
</IconButton>
</SettingItem>
<SettingItem label={t("Web UI")}>
<IconButton
color="inherit"