mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 06:03:45 +08:00
241 lines
5.8 KiB
TypeScript
241 lines
5.8 KiB
TypeScript
import dayjs from "dayjs";
|
|
import { invoke } from "@tauri-apps/api/tauri";
|
|
import { Notice } from "@/components/base";
|
|
|
|
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) {
|
|
const [_, _time, type, payload] = result;
|
|
const time = dayjs(_time).format("MM-DD HH:mm:ss");
|
|
return { time, type, payload };
|
|
}
|
|
|
|
const result2 = log.match(newRegex);
|
|
if (result2) {
|
|
const [_, time, type, payload] = result2;
|
|
return { time, type, payload };
|
|
}
|
|
return null;
|
|
})
|
|
.filter(Boolean) as ILogItem[];
|
|
}
|
|
|
|
export async function getProfiles() {
|
|
return invoke<IProfilesConfig>("get_profiles");
|
|
}
|
|
|
|
export async function enhanceProfiles() {
|
|
return invoke<void>("enhance_profiles");
|
|
}
|
|
|
|
export async function patchProfilesConfig(profiles: IProfilesConfig) {
|
|
return invoke<void>("patch_profiles_config", { profiles });
|
|
}
|
|
|
|
export async function createProfile(
|
|
item: Partial<IProfileItem>,
|
|
fileData?: string | null
|
|
) {
|
|
return invoke<void>("create_profile", { item, fileData });
|
|
}
|
|
|
|
export async function viewProfile(index: string) {
|
|
return invoke<void>("view_profile", { index });
|
|
}
|
|
|
|
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 });
|
|
}
|
|
|
|
export async function importProfile(url: string) {
|
|
return invoke<void>("import_profile", {
|
|
url,
|
|
option: { with_proxy: true },
|
|
});
|
|
}
|
|
|
|
export async function reorderProfile(activeId: string, overId: string) {
|
|
return invoke<void>("reorder_profile", {
|
|
activeId,
|
|
overId,
|
|
});
|
|
}
|
|
|
|
export async function updateProfile(index: string, option?: IProfileOption) {
|
|
return invoke<void>("update_profile", { index, option });
|
|
}
|
|
|
|
export async function deleteProfile(index: string) {
|
|
return invoke<void>("delete_profile", { index });
|
|
}
|
|
|
|
export async function patchProfile(
|
|
index: string,
|
|
profile: Partial<IProfileItem>
|
|
) {
|
|
return invoke<void>("patch_profile", { index, profile });
|
|
}
|
|
|
|
export async function getClashInfo() {
|
|
return invoke<IClashInfo | null>("get_clash_info");
|
|
}
|
|
|
|
// Get runtime config which controlled by verge
|
|
export async function getRuntimeConfig() {
|
|
return invoke<IConfigData | null>("get_runtime_config");
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
export async function patchClashConfig(payload: Partial<IConfigData>) {
|
|
return invoke<void>("patch_clash_config", { payload });
|
|
}
|
|
|
|
export async function getVergeConfig() {
|
|
return invoke<IVergeConfig>("get_verge_config");
|
|
}
|
|
|
|
export async function patchVergeConfig(payload: IVergeConfig) {
|
|
return invoke<void>("patch_verge_config", { payload });
|
|
}
|
|
|
|
export async function getSystemProxy() {
|
|
return invoke<{
|
|
enable: boolean;
|
|
server: string;
|
|
bypass: string;
|
|
}>("get_sys_proxy");
|
|
}
|
|
|
|
export async function getAutotemProxy() {
|
|
return invoke<{
|
|
enable: boolean;
|
|
url: string;
|
|
}>("get_auto_proxy");
|
|
}
|
|
|
|
export async function changeClashCore(clashCore: string) {
|
|
return invoke<any>("change_clash_core", { clashCore });
|
|
}
|
|
|
|
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");
|
|
}
|
|
|
|
export async function openAppDir() {
|
|
return invoke<void>("open_app_dir").catch((err) =>
|
|
Notice.error(err?.message || err.toString(), 1500)
|
|
);
|
|
}
|
|
|
|
export async function openCoreDir() {
|
|
return invoke<void>("open_core_dir").catch((err) =>
|
|
Notice.error(err?.message || err.toString(), 1500)
|
|
);
|
|
}
|
|
|
|
export async function openLogsDir() {
|
|
return invoke<void>("open_logs_dir").catch((err) =>
|
|
Notice.error(err?.message || err.toString(), 1500)
|
|
);
|
|
}
|
|
|
|
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,
|
|
});
|
|
}
|
|
|
|
export async function cmdTestDelay(url: string) {
|
|
return invoke<number>("test_delay", { url });
|
|
}
|
|
|
|
/// service mode
|
|
|
|
export async function checkService() {
|
|
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";
|
|
}
|
|
}
|
|
|
|
export async function installService() {
|
|
return invoke<void>("install_service");
|
|
}
|
|
|
|
export async function uninstallService() {
|
|
return invoke<void>("uninstall_service");
|
|
}
|
|
|
|
export async function invoke_uwp_tool() {
|
|
return invoke<void>("invoke_uwp_tool").catch((err) =>
|
|
Notice.error(err?.message || err.toString(), 1500)
|
|
);
|
|
}
|
|
|
|
export async function getPortableFlag() {
|
|
return invoke<boolean>("get_portable_flag");
|
|
}
|
|
|
|
export async function openDevTools() {
|
|
return invoke("open_devtools");
|
|
}
|
|
|
|
export async function exitApp() {
|
|
return invoke("exit_app");
|
|
}
|
|
|
|
export async function copyIconFile(
|
|
path: string,
|
|
name: "common" | "sysproxy" | "tun"
|
|
) {
|
|
return invoke<void>("copy_icon_file", { path, name });
|
|
}
|
|
|
|
export async function downloadIconCache(url: string, name: string) {
|
|
return invoke<string>("download_icon_cache", { url, name });
|
|
}
|