cmds.rs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. use crate::{
  2. core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, Verge},
  3. utils::{dirs, help, sysopt::SysProxyConfig},
  4. };
  5. use crate::{log_if_err, ret_err, wrap_err};
  6. use anyhow::Result;
  7. use serde_yaml::Mapping;
  8. use tauri::{api, State};
  9. type CmdResult<T = ()> = Result<T, String>;
  10. /// get all profiles from `profiles.yaml`
  11. #[tauri::command]
  12. pub fn get_profiles(core: State<'_, Core>) -> CmdResult<Profiles> {
  13. let profiles = core.profiles.lock();
  14. Ok(profiles.clone())
  15. }
  16. /// manually exec enhanced profile
  17. #[tauri::command]
  18. pub fn enhance_profiles(core: State<'_, Core>) -> CmdResult {
  19. wrap_err!(core.activate_enhanced(false))
  20. }
  21. /// import the profile from url
  22. /// and save to `profiles.yaml`
  23. #[tauri::command]
  24. pub async fn import_profile(
  25. url: String,
  26. option: Option<PrfOption>,
  27. core: State<'_, Core>,
  28. ) -> CmdResult {
  29. let item = wrap_err!(PrfItem::from_url(&url, None, None, option).await)?;
  30. let mut profiles = core.profiles.lock();
  31. wrap_err!(profiles.append_item(item))
  32. }
  33. /// new a profile
  34. /// append a temp profile item file to the `profiles` dir
  35. /// view the temp profile file by using vscode or other editor
  36. #[tauri::command]
  37. pub async fn create_profile(
  38. item: PrfItem, // partial
  39. file_data: Option<String>,
  40. core: State<'_, Core>,
  41. ) -> CmdResult {
  42. let item = wrap_err!(PrfItem::from(item, file_data).await)?;
  43. let mut profiles = core.profiles.lock();
  44. wrap_err!(profiles.append_item(item))
  45. }
  46. /// Update the profile
  47. #[tauri::command]
  48. pub async fn update_profile(
  49. index: String,
  50. option: Option<PrfOption>,
  51. core: State<'_, Core>,
  52. ) -> CmdResult {
  53. wrap_err!(Core::update_profile_item(core.inner().clone(), index, option).await)
  54. }
  55. /// change the current profile
  56. #[tauri::command]
  57. pub fn select_profile(index: String, core: State<'_, Core>) -> CmdResult {
  58. let mut profiles = core.profiles.lock();
  59. wrap_err!(profiles.put_current(index))?;
  60. drop(profiles);
  61. wrap_err!(core.activate_enhanced(false))
  62. }
  63. /// change the profile chain
  64. #[tauri::command]
  65. pub fn change_profile_chain(chain: Option<Vec<String>>, core: State<'_, Core>) -> CmdResult {
  66. let mut profiles = core.profiles.lock();
  67. profiles.put_chain(chain);
  68. drop(profiles);
  69. wrap_err!(core.activate_enhanced(false))
  70. }
  71. /// change the profile valid fields
  72. #[tauri::command]
  73. pub fn change_profile_valid(valid: Option<Vec<String>>, core: State<Core>) -> CmdResult {
  74. let mut profiles = core.profiles.lock();
  75. profiles.put_valid(valid);
  76. drop(profiles);
  77. wrap_err!(core.activate_enhanced(false))
  78. }
  79. /// delete profile item
  80. #[tauri::command]
  81. pub fn delete_profile(index: String, core: State<'_, Core>) -> CmdResult {
  82. let mut profiles = core.profiles.lock();
  83. if wrap_err!(profiles.delete_item(index))? {
  84. drop(profiles);
  85. log_if_err!(core.activate_enhanced(false));
  86. }
  87. Ok(())
  88. }
  89. /// patch the profile config
  90. #[tauri::command]
  91. pub fn patch_profile(index: String, profile: PrfItem, core: State<'_, Core>) -> CmdResult {
  92. let mut profiles = core.profiles.lock();
  93. wrap_err!(profiles.patch_item(index, profile))?;
  94. drop(profiles);
  95. // update cron task
  96. let mut timer = core.timer.lock();
  97. wrap_err!(timer.refresh())
  98. }
  99. /// run vscode command to edit the profile
  100. #[tauri::command]
  101. pub fn view_profile(index: String, core: State<'_, Core>) -> CmdResult {
  102. let profiles = core.profiles.lock();
  103. let item = wrap_err!(profiles.get_item(&index))?;
  104. let file = item.file.clone();
  105. if file.is_none() {
  106. ret_err!("the file is null");
  107. }
  108. let path = dirs::app_profiles_dir().join(file.unwrap());
  109. if !path.exists() {
  110. ret_err!("the file not found");
  111. }
  112. wrap_err!(help::open_file(path))
  113. }
  114. /// read the profile item file data
  115. #[tauri::command]
  116. pub fn read_profile_file(index: String, core: State<'_, Core>) -> CmdResult<String> {
  117. let profiles = core.profiles.lock();
  118. let item = wrap_err!(profiles.get_item(&index))?;
  119. let data = wrap_err!(item.read_file())?;
  120. Ok(data)
  121. }
  122. /// save the profile item file data
  123. #[tauri::command]
  124. pub fn save_profile_file(
  125. index: String,
  126. file_data: Option<String>,
  127. core: State<'_, Core>,
  128. ) -> CmdResult {
  129. if file_data.is_none() {
  130. return Ok(());
  131. }
  132. let profiles = core.profiles.lock();
  133. let item = wrap_err!(profiles.get_item(&index))?;
  134. wrap_err!(item.save_file(file_data.unwrap()))
  135. }
  136. /// get the clash core info from the state
  137. /// the caller can also get the infomation by clash's api
  138. #[tauri::command]
  139. pub fn get_clash_info(core: State<'_, Core>) -> CmdResult<ClashInfo> {
  140. let clash = core.clash.lock();
  141. Ok(clash.info.clone())
  142. }
  143. /// update the clash core config
  144. /// after putting the change to the clash core
  145. /// then we should save the latest config
  146. #[tauri::command]
  147. pub fn patch_clash_config(payload: Mapping, core: State<'_, Core>) -> CmdResult {
  148. wrap_err!(core.patch_clash(payload))
  149. }
  150. /// get the verge config
  151. #[tauri::command]
  152. pub fn get_verge_config(core: State<'_, Core>) -> CmdResult<Verge> {
  153. let verge = core.verge.lock();
  154. Ok(verge.clone())
  155. }
  156. /// patch the verge config
  157. /// this command only save the config and not responsible for other things
  158. #[tauri::command]
  159. pub fn patch_verge_config(
  160. payload: Verge,
  161. app_handle: tauri::AppHandle,
  162. core: State<'_, Core>,
  163. ) -> CmdResult {
  164. wrap_err!(core.patch_verge(payload, &app_handle))
  165. }
  166. /// change clash core
  167. #[tauri::command]
  168. pub fn change_clash_core(core: State<'_, Core>, clash_core: Option<String>) -> CmdResult {
  169. wrap_err!(core.change_core(clash_core))
  170. }
  171. /// restart the sidecar
  172. #[tauri::command]
  173. pub fn restart_sidecar(core: State<'_, Core>) -> CmdResult {
  174. wrap_err!(core.restart_clash())
  175. }
  176. /// kill all sidecars when update app
  177. #[tauri::command]
  178. pub fn kill_sidecar() {
  179. api::process::kill_children();
  180. }
  181. /// get the system proxy
  182. #[tauri::command]
  183. pub fn get_sys_proxy() -> Result<SysProxyConfig, String> {
  184. wrap_err!(SysProxyConfig::get_sys())
  185. }
  186. /// get the current proxy config
  187. /// which may not the same as system proxy
  188. #[tauri::command]
  189. pub fn get_cur_proxy(core: State<'_, Core>) -> CmdResult<Option<SysProxyConfig>> {
  190. let sysopt = core.sysopt.lock();
  191. wrap_err!(sysopt.get_sysproxy())
  192. }
  193. /// open app config dir
  194. #[tauri::command]
  195. pub fn open_app_dir() -> Result<(), String> {
  196. let app_dir = dirs::app_home_dir();
  197. wrap_err!(open::that(app_dir))
  198. }
  199. /// open logs dir
  200. #[tauri::command]
  201. pub fn open_logs_dir() -> Result<(), String> {
  202. let log_dir = dirs::app_logs_dir();
  203. wrap_err!(open::that(log_dir))
  204. }
  205. /// service mode
  206. #[cfg(windows)]
  207. pub mod service {
  208. use super::*;
  209. use crate::core::win_service::JsonResponse;
  210. #[tauri::command]
  211. pub async fn start_service() -> Result<(), String> {
  212. wrap_err!(crate::core::Service::start_service().await)
  213. }
  214. #[tauri::command]
  215. pub async fn stop_service() -> Result<(), String> {
  216. wrap_err!(crate::core::Service::stop_service().await)
  217. }
  218. #[tauri::command]
  219. pub async fn check_service() -> Result<JsonResponse, String> {
  220. // no log
  221. match crate::core::Service::check_service().await {
  222. Ok(res) => Ok(res),
  223. Err(err) => Err(err.to_string()),
  224. }
  225. }
  226. #[tauri::command]
  227. pub async fn install_service() -> Result<(), String> {
  228. wrap_err!(crate::core::Service::install_service().await)
  229. }
  230. #[tauri::command]
  231. pub async fn uninstall_service() -> Result<(), String> {
  232. wrap_err!(crate::core::Service::uninstall_service().await)
  233. }
  234. }
  235. #[cfg(not(windows))]
  236. pub mod service {
  237. use super::*;
  238. #[tauri::command]
  239. pub async fn start_service() -> Result<(), String> {
  240. Ok(())
  241. }
  242. #[tauri::command]
  243. pub async fn stop_service() -> Result<(), String> {
  244. Ok(())
  245. }
  246. #[tauri::command]
  247. pub async fn check_service() -> Result<(), String> {
  248. Ok(())
  249. }
  250. #[tauri::command]
  251. pub async fn install_service() -> Result<(), String> {
  252. Ok(())
  253. }
  254. #[tauri::command]
  255. pub async fn uninstall_service() -> Result<(), String> {
  256. Ok(())
  257. }
  258. }