cmds.rs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. use crate::{
  2. config::*,
  3. core::*,
  4. feat,
  5. utils::{dirs, help, resolve},
  6. };
  7. use crate::{ret_err, wrap_err};
  8. use anyhow::{Context, Result};
  9. use serde_yaml::Mapping;
  10. use std::collections::{HashMap, VecDeque};
  11. use sysproxy::Sysproxy;
  12. use tauri::api;
  13. type CmdResult<T = ()> = Result<T, String>;
  14. #[tauri::command]
  15. pub fn get_profiles() -> CmdResult<IProfiles> {
  16. Ok(Config::profiles().data().clone())
  17. }
  18. #[tauri::command]
  19. pub async fn enhance_profiles() -> CmdResult {
  20. wrap_err!(CoreManager::global().update_config().await)?;
  21. handle::Handle::refresh_clash();
  22. Ok(())
  23. }
  24. #[tauri::command]
  25. pub async fn import_profile(url: String, option: Option<PrfOption>) -> CmdResult {
  26. let item = wrap_err!(PrfItem::from_url(&url, None, None, option).await)?;
  27. wrap_err!(Config::profiles().data().append_item(item))
  28. }
  29. #[tauri::command]
  30. pub async fn reorder_profile(active_id: String, over_id: String) -> CmdResult {
  31. wrap_err!(Config::profiles().data().reorder(active_id, over_id))
  32. }
  33. #[tauri::command]
  34. pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResult {
  35. let item = wrap_err!(PrfItem::from(item, file_data).await)?;
  36. wrap_err!(Config::profiles().data().append_item(item))
  37. }
  38. #[tauri::command]
  39. pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult {
  40. wrap_err!(feat::update_profile(index, option).await)
  41. }
  42. #[tauri::command]
  43. pub async fn delete_profile(index: String) -> CmdResult {
  44. let should_update = wrap_err!({ Config::profiles().data().delete_item(index) })?;
  45. if should_update {
  46. wrap_err!(CoreManager::global().update_config().await)?;
  47. handle::Handle::refresh_clash();
  48. }
  49. Ok(())
  50. }
  51. /// 修改profiles的
  52. #[tauri::command]
  53. pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult {
  54. wrap_err!({ Config::profiles().draft().patch_config(profiles) })?;
  55. match CoreManager::global().update_config().await {
  56. Ok(_) => {
  57. handle::Handle::refresh_clash();
  58. Config::profiles().apply();
  59. wrap_err!(Config::profiles().data().save_file())?;
  60. Ok(())
  61. }
  62. Err(err) => {
  63. Config::profiles().discard();
  64. log::error!(target: "app", "{err}");
  65. Err(format!("{err}"))
  66. }
  67. }
  68. }
  69. /// 修改某个profile item的
  70. #[tauri::command]
  71. pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
  72. wrap_err!(Config::profiles().data().patch_item(index, profile))?;
  73. wrap_err!(timer::Timer::global().refresh())
  74. }
  75. #[tauri::command]
  76. pub fn view_profile(app_handle: tauri::AppHandle, index: String) -> CmdResult {
  77. let file = {
  78. wrap_err!(Config::profiles().latest().get_item(&index))?
  79. .file
  80. .clone()
  81. .ok_or("the file field is null")
  82. }?;
  83. let path = wrap_err!(dirs::app_profiles_dir())?.join(file);
  84. if !path.exists() {
  85. ret_err!("the file not found");
  86. }
  87. wrap_err!(help::open_file(app_handle, path))
  88. }
  89. #[tauri::command]
  90. pub fn read_profile_file(index: String) -> CmdResult<String> {
  91. let profiles = Config::profiles();
  92. let profiles = profiles.latest();
  93. let item = wrap_err!(profiles.get_item(&index))?;
  94. let data = wrap_err!(item.read_file())?;
  95. Ok(data)
  96. }
  97. #[tauri::command]
  98. pub fn save_profile_file(index: String, file_data: Option<String>) -> CmdResult {
  99. if file_data.is_none() {
  100. return Ok(());
  101. }
  102. let profiles = Config::profiles();
  103. let profiles = profiles.latest();
  104. let item = wrap_err!(profiles.get_item(&index))?;
  105. wrap_err!(item.save_file(file_data.unwrap()))
  106. }
  107. #[tauri::command]
  108. pub fn get_clash_info() -> CmdResult<ClashInfo> {
  109. Ok(Config::clash().latest().get_client_info())
  110. }
  111. #[tauri::command]
  112. pub fn get_runtime_config() -> CmdResult<Option<Mapping>> {
  113. Ok(Config::runtime().latest().config.clone())
  114. }
  115. #[tauri::command]
  116. pub fn get_runtime_yaml() -> CmdResult<String> {
  117. let runtime = Config::runtime();
  118. let runtime = runtime.latest();
  119. let config = runtime.config.as_ref();
  120. wrap_err!(config
  121. .ok_or(anyhow::anyhow!("failed to parse config to yaml file"))
  122. .and_then(
  123. |config| serde_yaml::to_string(config).context("failed to convert config to yaml")
  124. ))
  125. }
  126. #[tauri::command]
  127. pub fn get_runtime_exists() -> CmdResult<Vec<String>> {
  128. Ok(Config::runtime().latest().exists_keys.clone())
  129. }
  130. #[tauri::command]
  131. pub fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String)>>> {
  132. Ok(Config::runtime().latest().chain_logs.clone())
  133. }
  134. #[tauri::command]
  135. pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
  136. wrap_err!(feat::patch_clash(payload).await)
  137. }
  138. #[tauri::command]
  139. pub fn get_verge_config() -> CmdResult<IVerge> {
  140. Ok(Config::verge().data().clone())
  141. }
  142. #[tauri::command]
  143. pub async fn patch_verge_config(payload: IVerge) -> CmdResult {
  144. wrap_err!(feat::patch_verge(payload).await)
  145. }
  146. #[tauri::command]
  147. pub async fn change_clash_core(clash_core: Option<String>) -> CmdResult {
  148. wrap_err!(CoreManager::global().change_core(clash_core).await)
  149. }
  150. /// restart the sidecar
  151. #[tauri::command]
  152. pub async fn restart_sidecar() -> CmdResult {
  153. wrap_err!(CoreManager::global().run_core().await)
  154. }
  155. #[tauri::command]
  156. pub fn grant_permission(_core: String) -> CmdResult {
  157. #[cfg(any(target_os = "macos", target_os = "linux"))]
  158. return wrap_err!(manager::grant_permission(_core));
  159. #[cfg(not(any(target_os = "macos", target_os = "linux")))]
  160. return Err("Unsupported target".into());
  161. }
  162. /// get the system proxy
  163. #[tauri::command]
  164. pub fn get_sys_proxy() -> CmdResult<Mapping> {
  165. let current = wrap_err!(Sysproxy::get_system_proxy())?;
  166. let mut map = Mapping::new();
  167. map.insert("enable".into(), current.enable.into());
  168. map.insert(
  169. "server".into(),
  170. format!("{}:{}", current.host, current.port).into(),
  171. );
  172. map.insert("bypass".into(), current.bypass.into());
  173. Ok(map)
  174. }
  175. #[tauri::command]
  176. pub fn get_clash_logs() -> CmdResult<VecDeque<String>> {
  177. Ok(logger::Logger::global().get_log())
  178. }
  179. #[tauri::command]
  180. pub fn open_app_dir() -> CmdResult<()> {
  181. let app_dir = wrap_err!(dirs::app_home_dir())?;
  182. wrap_err!(open::that(app_dir))
  183. }
  184. #[tauri::command]
  185. pub fn open_core_dir() -> CmdResult<()> {
  186. let core_dir = wrap_err!(tauri::utils::platform::current_exe())?;
  187. let core_dir = core_dir.parent().ok_or("failed to get core dir")?;
  188. wrap_err!(open::that(core_dir))
  189. }
  190. #[tauri::command]
  191. pub fn open_logs_dir() -> CmdResult<()> {
  192. let log_dir = wrap_err!(dirs::app_logs_dir())?;
  193. wrap_err!(open::that(log_dir))
  194. }
  195. #[tauri::command]
  196. pub fn open_web_url(url: String) -> CmdResult<()> {
  197. wrap_err!(open::that(url))
  198. }
  199. #[cfg(windows)]
  200. pub mod uwp {
  201. use super::*;
  202. use crate::core::win_uwp;
  203. #[tauri::command]
  204. pub async fn invoke_uwp_tool() -> CmdResult {
  205. wrap_err!(win_uwp::invoke_uwptools().await)
  206. }
  207. }
  208. #[tauri::command]
  209. pub async fn clash_api_get_proxy_delay(
  210. name: String,
  211. url: Option<String>,
  212. timeout: i32,
  213. ) -> CmdResult<clash_api::DelayRes> {
  214. match clash_api::get_proxy_delay(name, url, timeout).await {
  215. Ok(res) => Ok(res),
  216. Err(err) => Err(err.to_string()),
  217. }
  218. }
  219. #[tauri::command]
  220. pub fn get_portable_flag() -> CmdResult<bool> {
  221. Ok(*dirs::PORTABLE_FLAG.get().unwrap_or(&false))
  222. }
  223. #[tauri::command]
  224. pub async fn test_delay(url: String) -> CmdResult<u32> {
  225. Ok(feat::test_delay(url).await.unwrap_or(10000u32))
  226. }
  227. #[tauri::command]
  228. pub fn exit_app(app_handle: tauri::AppHandle) {
  229. let _ = resolve::save_window_size_position(&app_handle, true);
  230. resolve::resolve_reset();
  231. api::process::kill_children();
  232. app_handle.exit(0);
  233. std::process::exit(0);
  234. }
  235. #[cfg(windows)]
  236. pub mod service {
  237. use super::*;
  238. use crate::core::win_service;
  239. #[tauri::command]
  240. pub async fn check_service() -> CmdResult<win_service::JsonResponse> {
  241. wrap_err!(win_service::check_service().await)
  242. }
  243. #[tauri::command]
  244. pub async fn install_service() -> CmdResult {
  245. wrap_err!(win_service::install_service().await)
  246. }
  247. #[tauri::command]
  248. pub async fn uninstall_service() -> CmdResult {
  249. wrap_err!(win_service::uninstall_service().await)
  250. }
  251. }
  252. #[cfg(not(windows))]
  253. pub mod service {
  254. use super::*;
  255. #[tauri::command]
  256. pub async fn check_service() -> CmdResult {
  257. Ok(())
  258. }
  259. #[tauri::command]
  260. pub async fn install_service() -> CmdResult {
  261. Ok(())
  262. }
  263. #[tauri::command]
  264. pub async fn uninstall_service() -> CmdResult {
  265. Ok(())
  266. }
  267. }
  268. #[cfg(not(windows))]
  269. pub mod uwp {
  270. use super::*;
  271. #[tauri::command]
  272. pub async fn invoke_uwp_tool() -> CmdResult {
  273. Ok(())
  274. }
  275. }