cmds.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import dayjs from "dayjs";
  2. import { invoke } from "@tauri-apps/api/tauri";
  3. import { Notice } from "@/components/base";
  4. export async function getClashLogs() {
  5. const regex = /time="(.+?)"\s+level=(.+?)\s+msg="(.+?)"/;
  6. const newRegex = /(.+?)\s+(.+?)\s+(.+)/;
  7. const logs = await invoke<string[]>("get_clash_logs");
  8. return logs
  9. .map((log) => {
  10. const result = log.match(regex);
  11. if (result) {
  12. const [_, _time, type, payload] = result;
  13. const time = dayjs(_time).format("MM-DD HH:mm:ss");
  14. return { time, type, payload };
  15. }
  16. const result2 = log.match(newRegex);
  17. if (result2) {
  18. const [_, time, type, payload] = result2;
  19. return { time, type, payload };
  20. }
  21. return null;
  22. })
  23. .filter(Boolean) as ILogItem[];
  24. }
  25. export async function getProfiles() {
  26. return invoke<IProfilesConfig>("get_profiles");
  27. }
  28. export async function enhanceProfiles() {
  29. return invoke<void>("enhance_profiles");
  30. }
  31. export async function patchProfilesConfig(profiles: IProfilesConfig) {
  32. return invoke<void>("patch_profiles_config", { profiles });
  33. }
  34. export async function createProfile(
  35. item: Partial<IProfileItem>,
  36. fileData?: string | null
  37. ) {
  38. return invoke<void>("create_profile", { item, fileData });
  39. }
  40. export async function viewProfile(index: string) {
  41. return invoke<void>("view_profile", { index });
  42. }
  43. export async function readProfileFile(index: string) {
  44. return invoke<string>("read_profile_file", { index });
  45. }
  46. export async function saveProfileFile(index: string, fileData: string) {
  47. return invoke<void>("save_profile_file", { index, fileData });
  48. }
  49. export async function importProfile(url: string) {
  50. return invoke<void>("import_profile", {
  51. url,
  52. option: { with_proxy: true },
  53. });
  54. }
  55. export async function reorderProfile(activeId: string, overId: string) {
  56. return invoke<void>("reorder_profile", {
  57. activeId,
  58. overId,
  59. });
  60. }
  61. export async function updateProfile(index: string, option?: IProfileOption) {
  62. return invoke<void>("update_profile", { index, option });
  63. }
  64. export async function deleteProfile(index: string) {
  65. return invoke<void>("delete_profile", { index });
  66. }
  67. export async function patchProfile(
  68. index: string,
  69. profile: Partial<IProfileItem>
  70. ) {
  71. return invoke<void>("patch_profile", { index, profile });
  72. }
  73. export async function getClashInfo() {
  74. return invoke<IClashInfo | null>("get_clash_info");
  75. }
  76. export async function getRuntimeConfig() {
  77. return invoke<any | null>("get_runtime_config");
  78. }
  79. export async function getRuntimeYaml() {
  80. return invoke<string | null>("get_runtime_yaml");
  81. }
  82. export async function getRuntimeExists() {
  83. return invoke<string[]>("get_runtime_exists");
  84. }
  85. export async function getRuntimeLogs() {
  86. return invoke<Record<string, [string, string][]>>("get_runtime_logs");
  87. }
  88. export async function patchClashConfig(payload: Partial<IConfigData>) {
  89. return invoke<void>("patch_clash_config", { payload });
  90. }
  91. export async function getVergeConfig() {
  92. return invoke<IVergeConfig>("get_verge_config");
  93. }
  94. export async function patchVergeConfig(payload: IVergeConfig) {
  95. return invoke<void>("patch_verge_config", { payload });
  96. }
  97. export async function getSystemProxy() {
  98. return invoke<{
  99. enable: boolean;
  100. server: string;
  101. bypass: string;
  102. }>("get_sys_proxy");
  103. }
  104. export async function changeClashCore(clashCore: string) {
  105. return invoke<any>("change_clash_core", { clashCore });
  106. }
  107. export async function restartSidecar() {
  108. return invoke<void>("restart_sidecar");
  109. }
  110. export async function grantPermission(core: string) {
  111. return invoke<void>("grant_permission", { core });
  112. }
  113. export async function openAppDir() {
  114. return invoke<void>("open_app_dir").catch((err) =>
  115. Notice.error(err?.message || err.toString(), 1500)
  116. );
  117. }
  118. export async function openCoreDir() {
  119. return invoke<void>("open_core_dir").catch((err) =>
  120. Notice.error(err?.message || err.toString(), 1500)
  121. );
  122. }
  123. export async function openLogsDir() {
  124. return invoke<void>("open_logs_dir").catch((err) =>
  125. Notice.error(err?.message || err.toString(), 1500)
  126. );
  127. }
  128. export async function openWebUrl(url: string) {
  129. return invoke<void>("open_web_url", { url });
  130. }
  131. export async function cmdGetProxyDelay(
  132. name: string,
  133. timeout: number,
  134. url?: string
  135. ) {
  136. name = encodeURIComponent(name);
  137. return invoke<{ delay: number }>("clash_api_get_proxy_delay", {
  138. name,
  139. url,
  140. timeout,
  141. });
  142. }
  143. export async function cmdTestDelay(url: string) {
  144. return invoke<number>("test_delay", { url });
  145. }
  146. /// service mode
  147. export async function checkService() {
  148. try {
  149. const result = await invoke<any>("check_service");
  150. if (result?.code === 0) return "active";
  151. if (result?.code === 400) return "installed";
  152. return "unknown";
  153. } catch (err: any) {
  154. return "uninstall";
  155. }
  156. }
  157. export async function installService() {
  158. return invoke<void>("install_service");
  159. }
  160. export async function uninstallService() {
  161. return invoke<void>("uninstall_service");
  162. }
  163. export async function invoke_uwp_tool() {
  164. return invoke<void>("invoke_uwp_tool").catch((err) =>
  165. Notice.error(err?.message || err.toString(), 1500)
  166. );
  167. }
  168. export async function getPortableFlag() {
  169. return invoke<boolean>("get_portable_flag");
  170. }
  171. export async function exitApp() {
  172. return invoke("exit_app");
  173. }