cmds.rs 8.0 KB

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