feat: support to change external controller

This commit is contained in:
GyDi 2022-11-06 23:23:26 +08:00
parent 7f65c501c6
commit e66a89208d
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A
5 changed files with 131 additions and 10 deletions

View File

@ -112,18 +112,21 @@ impl Core {
/// Patch Clash /// Patch Clash
/// handle the clash config changed /// handle the clash config changed
pub fn patch_clash(&self, patch: Mapping) -> Result<()> { pub fn patch_clash(&self, patch: Mapping) -> Result<()> {
let has_port = patch.contains_key(&Value::from("mixed-port")); let patch_cloned = patch.clone();
let has_mode = patch.contains_key(&Value::from("mode")); let clash_mode = patch.get("mode");
let mixed_port = patch.get("mixed-port");
let external = patch.get("external-controller");
let secret = patch.get("secret");
let port = { let valid_port = {
let global = Data::global(); let global = Data::global();
let mut clash = global.clash.lock(); let mut clash = global.clash.lock();
clash.patch_config(patch)?; clash.patch_config(patch_cloned)?;
clash.info.port.clone() clash.info.port.is_some()
}; };
// todo: port check // todo: port check
if has_port && port.is_some() { if (mixed_port.is_some() && valid_port) || external.is_some() || secret.is_some() {
let mut service = self.service.lock(); let mut service = self.service.lock();
service.restart()?; service.restart()?;
drop(service); drop(service);
@ -134,7 +137,7 @@ impl Core {
sysopt.init_sysproxy()?; sysopt.init_sysproxy()?;
} }
if has_mode { if clash_mode.is_some() {
let handle = self.handle.lock(); let handle = self.handle.lock();
handle.update_systray_part()?; handle.update_systray_part()?;
} }

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

View File

@ -60,6 +60,7 @@
"IPv6": "IPv6", "IPv6": "IPv6",
"Log Level": "日志等级", "Log Level": "日志等级",
"Mixed Port": "端口设置", "Mixed Port": "端口设置",
"External Controller": "外部控制",
"Clash Core": "Clash 内核", "Clash Core": "Clash 内核",
"Tun Mode": "Tun 模式", "Tun Mode": "Tun 模式",
"Service Mode": "服务模式", "Service Mode": "服务模式",

View File

@ -13,6 +13,8 @@ declare namespace ApiType {
"redir-port": number; "redir-port": number;
"socks-port": number; "socks-port": number;
"tproxy-port": number; "tproxy-port": number;
"external-controller": string;
secret: string;
} }
interface RuleItem { interface RuleItem {
@ -95,8 +97,8 @@ declare namespace CmdType {
interface ClashInfo { interface ClashInfo {
status: string; status: string;
port?: string; port?: string; // clash mixed port
server?: string; server?: string; // external-controller
secret?: string; secret?: string;
} }