* feat: add tauri-plugin-mihomo
* refactor: invock mihomo api by use tauri-plugin-mihomo
* chore: todo
* chore: update
* chore: update
* chore: update
* chore: update
* fix: incorrect delay status and update pretty config
* chore: update
* chore: remove cache
* chore: update
* chore: update
* fix: app freezed when change group proxy
* chore: update
* chore: update
* chore: add rustfmt.toml to tauri-plugin-mihomo
* chore: happy clippy
* refactor: connect mihomo websocket
* chore: update
* chore: update
* fix: parse bigint to number
* chore: update
* Revert "fix: parse bigint to number"
This reverts commit 74c006522e.
* chore: use number instead of bigint
* chore: cleanup
* fix: rule data not refresh when switch profile
* chore: update
* chore: cleanup
* chore: update
* fix: traffic graph data display
* feat: add ipc connection pool
* chore: update
* chore: clippy
* fix: incorrect delay status
* fix: typo
* fix: empty proxies tray menu
* chore: clippy
* chore: import tauri-plugin-mihomo by using git repo
* chore: cleanup
* fix: mihomo api
* fix: incorrect delay status
* chore: update tauri-plugin-mihomo dep
chore: update
132 lines
2.9 KiB
TypeScript
132 lines
2.9 KiB
TypeScript
import { useLockFn } from "ahooks";
|
|
import useSWR, { mutate } from "swr";
|
|
import { getVersion } from "tauri-plugin-mihomo-api";
|
|
|
|
import {
|
|
getClashInfo,
|
|
patchClashConfig,
|
|
getRuntimeConfig,
|
|
} from "@/services/cmds";
|
|
|
|
export const useClash = () => {
|
|
const { data: clash, mutate: mutateClash } = useSWR(
|
|
"getRuntimeConfig",
|
|
getRuntimeConfig,
|
|
);
|
|
|
|
const { data: versionData, mutate: mutateVersion } = useSWR(
|
|
"getVersion",
|
|
getVersion,
|
|
);
|
|
|
|
const patchClash = useLockFn(async (patch: Partial<IConfigData>) => {
|
|
await patchClashConfig(patch);
|
|
mutateClash();
|
|
});
|
|
|
|
const version = versionData?.meta
|
|
? `${versionData.version} Mihomo`
|
|
: versionData?.version || "-";
|
|
|
|
return {
|
|
clash,
|
|
version,
|
|
mutateClash,
|
|
mutateVersion,
|
|
patchClash,
|
|
};
|
|
};
|
|
|
|
export const useClashInfo = () => {
|
|
const { data: clashInfo, mutate: mutateInfo } = useSWR(
|
|
"getClashInfo",
|
|
getClashInfo,
|
|
);
|
|
|
|
const patchInfo = async (
|
|
patch: Partial<
|
|
Pick<
|
|
IConfigData,
|
|
| "port"
|
|
| "socks-port"
|
|
| "mixed-port"
|
|
| "redir-port"
|
|
| "tproxy-port"
|
|
| "external-controller"
|
|
| "secret"
|
|
>
|
|
>,
|
|
) => {
|
|
const hasInfo =
|
|
patch["redir-port"] != null ||
|
|
patch["tproxy-port"] != null ||
|
|
patch["mixed-port"] != null ||
|
|
patch["socks-port"] != null ||
|
|
patch["port"] != null ||
|
|
patch["external-controller"] != null ||
|
|
patch.secret != null;
|
|
|
|
if (!hasInfo) return;
|
|
|
|
if (patch["redir-port"]) {
|
|
const port = patch["redir-port"];
|
|
if (port < 1111) {
|
|
throw new Error("The port should not < 1111");
|
|
}
|
|
if (port > 65536) {
|
|
throw new Error("The port should not > 65536");
|
|
}
|
|
}
|
|
|
|
if (patch["tproxy-port"]) {
|
|
const port = patch["tproxy-port"];
|
|
if (port < 1111) {
|
|
throw new Error("The port should not < 1111");
|
|
}
|
|
if (port > 65536) {
|
|
throw new Error("The port should not > 65536");
|
|
}
|
|
}
|
|
|
|
if (patch["mixed-port"]) {
|
|
const port = patch["mixed-port"];
|
|
if (port < 1111) {
|
|
throw new Error("The port should not < 1111");
|
|
}
|
|
if (port > 65536) {
|
|
throw new Error("The port should not > 65536");
|
|
}
|
|
}
|
|
|
|
if (patch["socks-port"]) {
|
|
const port = patch["socks-port"];
|
|
if (port < 1111) {
|
|
throw new Error("The port should not < 1111");
|
|
}
|
|
if (port > 65536) {
|
|
throw new Error("The port should not > 65536");
|
|
}
|
|
}
|
|
|
|
if (patch["port"]) {
|
|
const port = patch["port"];
|
|
if (port < 1111) {
|
|
throw new Error("The port should not < 1111");
|
|
}
|
|
if (port > 65536) {
|
|
throw new Error("The port should not > 65536");
|
|
}
|
|
}
|
|
|
|
await patchClashConfig(patch);
|
|
mutateInfo();
|
|
mutate("getClashConfig");
|
|
};
|
|
|
|
return {
|
|
clashInfo,
|
|
mutateInfo,
|
|
patchInfo,
|
|
};
|
|
};
|