Files
clash-proxy/src/hooks/use-clash.ts

136 lines
3.0 KiB
TypeScript
Raw Normal View History

2022-11-23 17:44:40 +08:00
import useSWR, { mutate } from "swr";
import { useLockFn } from "ahooks";
2024-02-21 10:02:28 +08:00
import { getAxios, getVersion, updateConfigs } from "@/services/api";
2022-11-23 17:44:40 +08:00
import {
2024-02-21 10:02:28 +08:00
getClashInfo,
patchClashConfig,
getRuntimeConfig,
} from "@/services/cmds";
2022-11-23 17:44:40 +08:00
export const useClash = () => {
const { data: clash, mutate: mutateClash } = useSWR(
2024-02-21 10:02:28 +08:00
"getRuntimeConfig",
getRuntimeConfig
2022-11-23 17:44:40 +08:00
);
const { data: versionData, mutate: mutateVersion } = useSWR(
"getVersion",
getVersion
);
const patchClash = useLockFn(async (patch: Partial<IConfigData>) => {
await updateConfigs(patch);
await patchClashConfig(patch);
mutateClash();
});
const version = versionData?.premium
? `${versionData.version} Premium`
: versionData?.meta
? `${versionData.version} Meta`
: versionData?.version || "-";
return {
clash,
version,
mutateClash,
mutateVersion,
patchClash,
};
};
export const useClashInfo = () => {
const { data: clashInfo, mutate: mutateInfo } = useSWR(
"getClashInfo",
getClashInfo
);
const patchInfo = async (
patch: Partial<
2024-02-05 16:47:40 +08:00
Pick<
IConfigData,
| "port"
| "socks-port"
| "mixed-port"
| "redir-port"
| "tproxy-port"
| "external-controller"
| "secret"
2024-02-05 16:47:40 +08:00
>
2022-11-23 17:44:40 +08:00
>
) => {
const hasInfo =
patch["redir-port"] != null ||
patch["tproxy-port"] != null ||
2022-11-23 17:44:40 +08:00
patch["mixed-port"] != null ||
2024-02-05 16:47:40 +08:00
patch["socks-port"] != null ||
patch["port"] != null ||
2022-11-23 17:44:40 +08:00
patch["external-controller"] != null ||
patch.secret != null;
if (!hasInfo) return;
if (patch["redir-port"]) {
const port = patch["redir-port"];
if (port < 1000) {
throw new Error("The port should not < 1000");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
if (patch["tproxy-port"]) {
const port = patch["tproxy-port"];
if (port < 1000) {
throw new Error("The port should not < 1000");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
2022-11-23 17:44:40 +08:00
if (patch["mixed-port"]) {
const port = patch["mixed-port"];
if (port < 1000) {
throw new Error("The port should not < 1000");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
2024-02-05 16:47:40 +08:00
if (patch["socks-port"]) {
const port = patch["socks-port"];
if (port < 1000) {
throw new Error("The port should not < 1000");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
if (patch["port"]) {
const port = patch["port"];
if (port < 1000) {
throw new Error("The port should not < 1000");
}
if (port > 65536) {
throw new Error("The port should not > 65536");
}
}
2022-11-23 17:44:40 +08:00
await patchClashConfig(patch);
mutateInfo();
mutate("getClashConfig");
// 刷新接口
2024-01-15 10:18:04 +08:00
getAxios(true);
2022-11-23 17:44:40 +08:00
};
return {
clashInfo,
mutateInfo,
patchInfo,
};
};