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("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("get_profiles"); } export async function enhanceProfiles() { return invoke("enhance_profiles"); } export async function patchProfilesConfig(profiles: IProfilesConfig) { return invoke("patch_profiles_config", { profiles }); } export async function createProfile( item: Partial, fileData?: string | null ) { return invoke("create_profile", { item, fileData }); } export async function viewProfile(index: string) { return invoke("view_profile", { index }); } export async function readProfileFile(index: string) { return invoke("read_profile_file", { index }); } export async function saveProfileFile(index: string, fileData: string) { return invoke("save_profile_file", { index, fileData }); } export async function importProfile(url: string) { return invoke("import_profile", { url, option: { with_proxy: true }, }); } export async function updateProfile(index: string, option?: IProfileOption) { return invoke("update_profile", { index, option }); } export async function deleteProfile(index: string) { return invoke("delete_profile", { index }); } export async function patchProfile( index: string, profile: Partial ) { return invoke("patch_profile", { index, profile }); } export async function getClashInfo() { return invoke("get_clash_info"); } export async function getRuntimeConfig() { return invoke("get_runtime_config"); } export async function getRuntimeYaml() { return invoke("get_runtime_yaml"); } export async function getRuntimeExists() { return invoke("get_runtime_exists"); } export async function getRuntimeLogs() { return invoke>("get_runtime_logs"); } export async function patchClashConfig(payload: Partial) { return invoke("patch_clash_config", { payload }); } export async function getVergeConfig() { return invoke("get_verge_config"); } export async function patchVergeConfig(payload: IVergeConfig) { return invoke("patch_verge_config", { payload }); } export async function getSystemProxy() { return invoke<{ enable: boolean; server: string; bypass: string; }>("get_sys_proxy"); } export async function changeClashCore(clashCore: string) { return invoke("change_clash_core", { clashCore }); } export async function restartSidecar() { return invoke("restart_sidecar"); } export async function grantPermission(core: string) { return invoke("grant_permission", { core }); } export async function openAppDir() { return invoke("open_app_dir").catch((err) => Notice.error(err?.message || err.toString(), 1500) ); } export async function openCoreDir() { return invoke("open_core_dir").catch((err) => Notice.error(err?.message || err.toString(), 1500) ); } export async function openLogsDir() { return invoke("open_logs_dir").catch((err) => Notice.error(err?.message || err.toString(), 1500) ); } export async function openWebUrl(url: string) { return invoke("open_web_url", { url }); } /// service mode export async function checkService() { try { const result = await invoke("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("install_service"); } export async function uninstallService() { return invoke("uninstall_service"); }