cmds.rs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. use crate::{
  2. core::{ClashInfo, ProfileItem, Profiles, VergeConfig},
  3. states::{ClashState, ProfilesState, VergeState},
  4. utils::{dirs::app_home_dir, fetch::fetch_profile, sysopt::SysProxyConfig},
  5. };
  6. use serde_yaml::Mapping;
  7. use std::process::Command;
  8. use tauri::State;
  9. /// get all profiles from `profiles.yaml`
  10. /// do not acquire the lock of ProfileLock
  11. #[tauri::command]
  12. pub fn get_profiles(profiles_state: State<'_, ProfilesState>) -> Result<Profiles, String> {
  13. match profiles_state.0.lock() {
  14. Ok(profiles) => Ok(profiles.clone()),
  15. Err(_) => Err("failed to get profiles lock".into()),
  16. }
  17. }
  18. /// synchronize data irregularly
  19. #[tauri::command]
  20. pub fn sync_profiles(profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
  21. match profiles_state.0.lock() {
  22. Ok(mut profiles) => profiles.sync_file(),
  23. Err(_) => Err("failed to get profiles lock".into()),
  24. }
  25. }
  26. /// import the profile from url
  27. /// and save to `profiles.yaml`
  28. #[tauri::command]
  29. pub async fn import_profile(
  30. url: String,
  31. with_proxy: bool,
  32. profiles_state: State<'_, ProfilesState>,
  33. ) -> Result<(), String> {
  34. match fetch_profile(&url, with_proxy).await {
  35. Some(result) => {
  36. let mut profiles = profiles_state.0.lock().unwrap();
  37. profiles.import_from_url(url, result)
  38. }
  39. None => Err(format!("failed to fetch profile from `{}`", url)),
  40. }
  41. }
  42. /// new a profile
  43. /// append a temp profile item file to the `profiles` dir
  44. /// view the temp profile file by using vscode or other editor
  45. #[tauri::command]
  46. pub async fn new_profile(
  47. name: String,
  48. desc: String,
  49. profiles_state: State<'_, ProfilesState>,
  50. ) -> Result<(), String> {
  51. let mut profiles = profiles_state.0.lock().unwrap();
  52. profiles.append_item(name, desc)?;
  53. Ok(())
  54. }
  55. /// Update the profile
  56. #[tauri::command]
  57. pub async fn update_profile(
  58. index: usize,
  59. with_proxy: bool,
  60. clash_state: State<'_, ClashState>,
  61. profiles_state: State<'_, ProfilesState>,
  62. ) -> Result<(), String> {
  63. // maybe we can get the url from the web app directly
  64. let url = match profiles_state.0.lock() {
  65. Ok(mut profile) => {
  66. let items = profile.items.take().unwrap_or(vec![]);
  67. if index >= items.len() {
  68. return Err("the index out of bound".into());
  69. }
  70. let url = match &items[index].url {
  71. Some(u) => u.clone(),
  72. None => return Err("failed to update profile for `invalid url`".into()),
  73. };
  74. profile.items = Some(items);
  75. url
  76. }
  77. Err(_) => return Err("failed to get profiles lock".into()),
  78. };
  79. match fetch_profile(&url, with_proxy).await {
  80. Some(result) => match profiles_state.0.lock() {
  81. Ok(mut profiles) => {
  82. profiles.update_item(index, result)?;
  83. // reactivate the profile
  84. let current = profiles.current.clone().unwrap_or(0);
  85. if current == index {
  86. let clash = clash_state.0.lock().unwrap();
  87. profiles.activate(&clash)
  88. } else {
  89. Ok(())
  90. }
  91. }
  92. Err(_) => Err("failed to get profiles lock".into()),
  93. },
  94. None => Err(format!("failed to fetch profile from `{}`", url)),
  95. }
  96. }
  97. /// change the current profile
  98. #[tauri::command]
  99. pub fn select_profile(
  100. index: usize,
  101. clash_state: State<'_, ClashState>,
  102. profiles_state: State<'_, ProfilesState>,
  103. ) -> Result<(), String> {
  104. let mut profiles = profiles_state.0.lock().unwrap();
  105. match profiles.put_current(index) {
  106. Ok(()) => {
  107. let clash = clash_state.0.lock().unwrap();
  108. profiles.activate(&clash)
  109. }
  110. Err(err) => Err(err),
  111. }
  112. }
  113. /// delete profile item
  114. #[tauri::command]
  115. pub fn delete_profile(
  116. index: usize,
  117. clash_state: State<'_, ClashState>,
  118. profiles_state: State<'_, ProfilesState>,
  119. ) -> Result<(), String> {
  120. let mut profiles = profiles_state.0.lock().unwrap();
  121. match profiles.delete_item(index) {
  122. Ok(change) => match change {
  123. true => {
  124. let clash = clash_state.0.lock().unwrap();
  125. profiles.activate(&clash)
  126. }
  127. false => Ok(()),
  128. },
  129. Err(err) => Err(err),
  130. }
  131. }
  132. /// patch the profile config
  133. #[tauri::command]
  134. pub fn patch_profile(
  135. index: usize,
  136. profile: ProfileItem,
  137. profiles_state: State<'_, ProfilesState>,
  138. ) -> Result<(), String> {
  139. match profiles_state.0.lock() {
  140. Ok(mut profiles) => profiles.patch_item(index, profile),
  141. Err(_) => Err("can not get profiles lock".into()),
  142. }
  143. }
  144. /// run vscode command to edit the profile
  145. #[tauri::command]
  146. pub fn view_profile(index: usize, profiles_state: State<'_, ProfilesState>) -> Result<(), String> {
  147. let mut profiles = profiles_state.0.lock().unwrap();
  148. let items = profiles.items.take().unwrap_or(vec![]);
  149. if index >= items.len() {
  150. profiles.items = Some(items);
  151. return Err("the index out of bound".into());
  152. }
  153. let file = items[index].file.clone().unwrap_or("".into());
  154. profiles.items = Some(items);
  155. let path = app_home_dir().join("profiles").join(file);
  156. if !path.exists() {
  157. return Err("the file not found".into());
  158. }
  159. // use vscode first
  160. if let Ok(code) = which::which("code") {
  161. return match Command::new(code).arg(path).status() {
  162. Ok(_) => Ok(()),
  163. Err(_) => Err("failed to open file by VScode".into()),
  164. };
  165. }
  166. // use `open` command
  167. if let Ok(open) = which::which("open") {
  168. return match Command::new(open).arg(path).status() {
  169. Ok(_) => Ok(()),
  170. Err(_) => Err("failed to open file by `open`".into()),
  171. };
  172. }
  173. // recommand to use vscode
  174. // todo: support other editors
  175. return Err("please install VScode".into());
  176. }
  177. /// restart the sidecar
  178. #[tauri::command]
  179. pub fn restart_sidecar(
  180. clash_state: State<'_, ClashState>,
  181. profiles_state: State<'_, ProfilesState>,
  182. ) -> Result<(), String> {
  183. let mut clash = clash_state.0.lock().unwrap();
  184. let mut profiles = profiles_state.0.lock().unwrap();
  185. match clash.restart_sidecar(&mut profiles) {
  186. Ok(_) => Ok(()),
  187. Err(err) => {
  188. log::error!("{}", err);
  189. Err(err)
  190. }
  191. }
  192. }
  193. /// get the clash core info from the state
  194. /// the caller can also get the infomation by clash's api
  195. #[tauri::command]
  196. pub fn get_clash_info(clash_state: State<'_, ClashState>) -> Result<ClashInfo, String> {
  197. match clash_state.0.lock() {
  198. Ok(clash) => Ok(clash.info.clone()),
  199. Err(_) => Err("failed to get clash lock".into()),
  200. }
  201. }
  202. /// update the clash core config
  203. /// after putting the change to the clash core
  204. /// then we should save the latest config
  205. #[tauri::command]
  206. pub fn patch_clash_config(
  207. payload: Mapping,
  208. clash_state: State<'_, ClashState>,
  209. verge_state: State<'_, VergeState>,
  210. profiles_state: State<'_, ProfilesState>,
  211. ) -> Result<(), String> {
  212. let mut clash = clash_state.0.lock().unwrap();
  213. let mut verge = verge_state.0.lock().unwrap();
  214. let mut profiles = profiles_state.0.lock().unwrap();
  215. clash.patch_config(payload, &mut verge, &mut profiles)
  216. }
  217. /// get the system proxy
  218. #[tauri::command]
  219. pub fn get_sys_proxy() -> Result<SysProxyConfig, String> {
  220. match SysProxyConfig::get_sys() {
  221. Ok(value) => Ok(value),
  222. Err(err) => Err(err.to_string()),
  223. }
  224. }
  225. /// get the current proxy config
  226. /// which may not the same as system proxy
  227. #[tauri::command]
  228. pub fn get_cur_proxy(verge_state: State<'_, VergeState>) -> Result<Option<SysProxyConfig>, String> {
  229. match verge_state.0.lock() {
  230. Ok(verge) => Ok(verge.cur_sysproxy.clone()),
  231. Err(_) => Err("failed to get verge lock".into()),
  232. }
  233. }
  234. /// get the verge config
  235. #[tauri::command]
  236. pub fn get_verge_config(verge_state: State<'_, VergeState>) -> Result<VergeConfig, String> {
  237. let verge = verge_state.0.lock().unwrap();
  238. let mut config = verge.config.clone();
  239. if config.system_proxy_bypass.is_none() && verge.cur_sysproxy.is_some() {
  240. config.system_proxy_bypass = Some(verge.cur_sysproxy.clone().unwrap().bypass)
  241. }
  242. Ok(config)
  243. }
  244. /// patch the verge config
  245. /// this command only save the config and not responsible for other things
  246. #[tauri::command]
  247. pub async fn patch_verge_config(
  248. payload: VergeConfig,
  249. verge_state: State<'_, VergeState>,
  250. ) -> Result<(), String> {
  251. let mut verge = verge_state.0.lock().unwrap();
  252. verge.patch_config(payload)
  253. }