cmds.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { invoke } from "@tauri-apps/api/tauri";
  2. import Notice from "@/components/base/base-notice";
  3. export async function getClashLogs() {
  4. const regex = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/;
  5. const newRegex = /(.+?)\s+(.+?)\s+(.+)/;
  6. const logs = await invoke<string[]>("get_clash_logs");
  7. return logs
  8. .map((log) => {
  9. const result = log.match(regex);
  10. if (result) {
  11. const [_, time, type, payload] = result;
  12. return { time, type, payload };
  13. }
  14. const result2 = log.match(newRegex);
  15. if (result2) {
  16. const [_, time, type, payload] = result2;
  17. return { time, type, payload };
  18. }
  19. return null;
  20. })
  21. .filter(Boolean) as ApiType.LogItem[];
  22. }
  23. export async function getProfiles() {
  24. return invoke<CmdType.ProfilesConfig>("get_profiles");
  25. }
  26. export async function enhanceProfiles() {
  27. return invoke<void>("enhance_profiles");
  28. }
  29. export async function patchProfilesConfig(profiles: CmdType.ProfilesConfig) {
  30. return invoke<void>("patch_profiles_config");
  31. }
  32. export async function createProfile(
  33. item: Partial<CmdType.ProfileItem>,
  34. fileData?: string | null
  35. ) {
  36. return invoke<void>("create_profile", { item, fileData });
  37. }
  38. export async function viewProfile(index: string) {
  39. return invoke<void>("view_profile", { index });
  40. }
  41. export async function readProfileFile(index: string) {
  42. return invoke<string>("read_profile_file", { index });
  43. }
  44. export async function saveProfileFile(index: string, fileData: string) {
  45. return invoke<void>("save_profile_file", { index, fileData });
  46. }
  47. export async function importProfile(url: string) {
  48. return invoke<void>("import_profile", {
  49. url,
  50. option: { with_proxy: true },
  51. });
  52. }
  53. export async function updateProfile(
  54. index: string,
  55. option?: CmdType.ProfileOption
  56. ) {
  57. return invoke<void>("update_profile", { index, option });
  58. }
  59. export async function deleteProfile(index: string) {
  60. return invoke<void>("delete_profile", { index });
  61. }
  62. export async function patchProfile(
  63. index: string,
  64. profile: Partial<CmdType.ProfileItem>
  65. ) {
  66. return invoke<void>("patch_profile", { index, profile });
  67. }
  68. export async function getClashInfo() {
  69. return invoke<CmdType.ClashInfo | null>("get_clash_info");
  70. }
  71. export async function getRuntimeConfig() {
  72. return invoke<any | null>("get_runtime_config");
  73. }
  74. export async function getRuntimeYaml() {
  75. return invoke<string | null>("get_runtime_yaml");
  76. }
  77. export async function getRuntimeExists() {
  78. return invoke<string[]>("get_runtime_exists");
  79. }
  80. export async function getRuntimeLogs() {
  81. return invoke<Record<string, [string, string][]>>("get_runtime_logs");
  82. }
  83. export async function patchClashConfig(payload: Partial<ApiType.ConfigData>) {
  84. return invoke<void>("patch_clash_config", { payload });
  85. }
  86. export async function getVergeConfig() {
  87. return invoke<CmdType.VergeConfig>("get_verge_config");
  88. }
  89. export async function patchVergeConfig(payload: CmdType.VergeConfig) {
  90. return invoke<void>("patch_verge_config", { payload });
  91. }
  92. export async function getSystemProxy() {
  93. return invoke<{
  94. enable: boolean;
  95. server: string;
  96. bypass: string;
  97. }>("get_sys_proxy");
  98. }
  99. export async function changeClashCore(clashCore: string) {
  100. return invoke<any>("change_clash_core", { clashCore });
  101. }
  102. export async function restartSidecar() {
  103. return invoke<void>("restart_sidecar");
  104. }
  105. export async function openAppDir() {
  106. return invoke<void>("open_app_dir").catch((err) =>
  107. Notice.error(err?.message || err.toString(), 1500)
  108. );
  109. }
  110. export async function openLogsDir() {
  111. return invoke<void>("open_logs_dir").catch((err) =>
  112. Notice.error(err?.message || err.toString(), 1500)
  113. );
  114. }
  115. export async function openWebUrl(url: string) {
  116. return invoke<void>("open_web_url", { url });
  117. }
  118. /// service mode
  119. export async function checkService() {
  120. try {
  121. const result = await invoke<any>("check_service");
  122. if (result?.code === 0) return "active";
  123. if (result?.code === 400) return "installed";
  124. return "unknown";
  125. } catch (err: any) {
  126. return "uninstall";
  127. }
  128. }
  129. export async function installService() {
  130. return invoke<void>("install_service");
  131. }
  132. export async function uninstallService() {
  133. return invoke<void>("uninstall_service");
  134. }