Files
clash-proxy/src/services/cmds.ts

226 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-11-23 17:45:43 +08:00
import dayjs from "dayjs";
2021-12-25 22:33:29 +08:00
import { invoke } from "@tauri-apps/api/tauri";
import { Notice } from "@/components/base";
2021-12-25 22:33:29 +08:00
2022-09-05 20:30:39 +08:00
export async function getClashLogs() {
const regex = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/;
const newRegex = /(.+?)\s+(.+?)\s+(.+)/;
const logs = await invoke<string[]>("get_clash_logs");
return logs
.map((log) => {
const result = log.match(regex);
if (result) {
2022-11-23 17:45:43 +08:00
const [_, _time, type, payload] = result;
const time = dayjs(_time).format("MM-DD HH:mm:ss");
2022-09-05 20:30:39 +08:00
return { time, type, payload };
}
const result2 = log.match(newRegex);
if (result2) {
const [_, time, type, payload] = result2;
return { time, type, payload };
}
return null;
})
2022-11-19 17:22:29 +08:00
.filter(Boolean) as ILogItem[];
2022-09-05 20:30:39 +08:00
}
2022-01-05 02:00:59 +08:00
export async function getProfiles() {
2022-11-19 17:22:29 +08:00
return invoke<IProfilesConfig>("get_profiles");
2021-12-25 22:33:29 +08:00
}
2022-03-06 14:59:25 +08:00
export async function enhanceProfiles() {
return invoke<void>("enhance_profiles");
}
2022-11-19 17:22:29 +08:00
export async function patchProfilesConfig(profiles: IProfilesConfig) {
2022-11-18 18:26:55 +08:00
return invoke<void>("patch_profiles_config", { profiles });
2022-11-18 18:18:41 +08:00
}
export async function createProfile(
2022-11-19 17:22:29 +08:00
item: Partial<IProfileItem>,
fileData?: string | null
) {
return invoke<void>("create_profile", { item, fileData });
2022-02-07 17:26:05 +08:00
}
2022-03-01 08:58:47 +08:00
export async function viewProfile(index: string) {
2022-01-19 23:58:34 +08:00
return invoke<void>("view_profile", { index });
2022-01-17 02:16:17 +08:00
}
2022-03-27 00:58:17 +08:00
export async function readProfileFile(index: string) {
return invoke<string>("read_profile_file", { index });
}
export async function saveProfileFile(index: string, fileData: string) {
return invoke<void>("save_profile_file", { index, fileData });
}
2021-12-25 22:33:29 +08:00
export async function importProfile(url: string) {
return invoke<void>("import_profile", {
url,
option: { with_proxy: true },
});
2021-12-25 22:33:29 +08:00
}
export async function reorderProfile(activeId: string, overId: string) {
return invoke<void>("reorder_profile", {
activeId,
overId,
});
}
2022-11-19 17:22:29 +08:00
export async function updateProfile(index: string, option?: IProfileOption) {
return invoke<void>("update_profile", { index, option });
2021-12-25 22:33:29 +08:00
}
2022-03-01 08:58:47 +08:00
export async function deleteProfile(index: string) {
2022-01-05 02:00:59 +08:00
return invoke<void>("delete_profile", { index });
2021-12-25 22:33:29 +08:00
}
2022-01-05 02:00:59 +08:00
export async function patchProfile(
2022-03-01 08:58:47 +08:00
index: string,
2022-11-19 17:22:29 +08:00
profile: Partial<IProfileItem>
2022-01-05 02:00:59 +08:00
) {
return invoke<void>("patch_profile", { index, profile });
2021-12-25 22:33:29 +08:00
}
2022-01-05 02:00:59 +08:00
export async function getClashInfo() {
2022-11-19 17:22:29 +08:00
return invoke<IClashInfo | null>("get_clash_info");
2022-01-05 02:00:59 +08:00
}
2024-02-21 10:02:28 +08:00
// Get runtime config which controlled by verge
2022-08-12 03:20:55 +08:00
export async function getRuntimeConfig() {
2024-02-21 10:02:28 +08:00
return invoke<IConfigData | null>("get_runtime_config");
2022-08-12 03:20:55 +08:00
}
export async function getRuntimeYaml() {
return invoke<string | null>("get_runtime_yaml");
}
export async function getRuntimeExists() {
return invoke<string[]>("get_runtime_exists");
}
export async function getRuntimeLogs() {
return invoke<Record<string, [string, string][]>>("get_runtime_logs");
2022-07-25 01:20:13 +08:00
}
2022-11-19 17:22:29 +08:00
export async function patchClashConfig(payload: Partial<IConfigData>) {
2022-01-05 02:00:59 +08:00
return invoke<void>("patch_clash_config", { payload });
2021-12-25 22:33:29 +08:00
}
export async function getVergeConfig() {
2022-11-19 17:22:29 +08:00
return invoke<IVergeConfig>("get_verge_config");
2021-12-25 22:33:29 +08:00
}
2022-11-19 17:22:29 +08:00
export async function patchVergeConfig(payload: IVergeConfig) {
2021-12-25 22:33:29 +08:00
return invoke<void>("patch_verge_config", { payload });
}
2022-01-12 02:54:50 +08:00
export async function getSystemProxy() {
2022-09-07 01:51:43 +08:00
return invoke<{
enable: boolean;
server: string;
bypass: string;
}>("get_sys_proxy");
2022-01-12 02:54:50 +08:00
}
2022-02-16 03:21:34 +08:00
2022-05-17 01:59:49 +08:00
export async function changeClashCore(clashCore: string) {
return invoke<any>("change_clash_core", { clashCore });
}
2022-03-06 14:59:25 +08:00
export async function restartSidecar() {
return invoke<void>("restart_sidecar");
}
export async function grantPermission(core: string) {
return invoke<void>("grant_permission", { core });
}
export async function getAppDir() {
return invoke<string>("get_app_dir");
}
2022-02-16 03:21:34 +08:00
export async function openAppDir() {
2022-03-12 23:58:20 +08:00
return invoke<void>("open_app_dir").catch((err) =>
Notice.error(err?.message || err.toString(), 1500)
);
2022-02-16 03:21:34 +08:00
}
2022-12-13 00:44:24 +08:00
export async function openCoreDir() {
return invoke<void>("open_core_dir").catch((err) =>
Notice.error(err?.message || err.toString(), 1500)
);
}
2022-02-16 03:21:34 +08:00
export async function openLogsDir() {
2022-03-12 23:58:20 +08:00
return invoke<void>("open_logs_dir").catch((err) =>
Notice.error(err?.message || err.toString(), 1500)
);
2022-02-16 03:21:34 +08:00
}
2022-04-24 21:03:47 +08:00
2022-08-06 21:56:54 +08:00
export async function openWebUrl(url: string) {
return invoke<void>("open_web_url", { url });
}
export async function cmdGetProxyDelay(
name: string,
timeout: number,
url?: string
) {
name = encodeURIComponent(name);
return invoke<{ delay: number }>("clash_api_get_proxy_delay", {
name,
url,
timeout,
});
}
2024-01-17 11:02:17 +08:00
export async function cmdTestDelay(url: string) {
return invoke<number>("test_delay", { url });
}
2022-04-24 21:03:47 +08:00
/// service mode
export async function checkService() {
2022-04-25 16:12:04 +08:00
try {
const result = await invoke<any>("check_service");
if (result?.code === 0) return "active";
if (result?.code === 400) return "installed";
return "unknown";
} catch (err: any) {
return "uninstall";
}
2022-04-24 21:03:47 +08:00
}
export async function installService() {
return invoke<void>("install_service");
}
export async function uninstallService() {
return invoke<void>("uninstall_service");
}
2023-11-22 00:15:41 -08:00
export async function invoke_uwp_tool() {
return invoke<void>("invoke_uwp_tool").catch((err) =>
Notice.error(err?.message || err.toString(), 1500)
);
}
2023-12-15 21:39:34 +08:00
export async function getPortableFlag() {
return invoke<boolean>("get_portable_flag");
}
2024-02-02 16:32:19 +08:00
export async function exitApp() {
return invoke("exit_app");
}
export async function copyIconFile(
path: string,
name: "common.png" | "sysproxy.png" | "tun.png"
) {
return invoke<void>("copy_icon_file", { path, name });
}