cmds.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. use crate::{
  2. core::{ClashInfo, ProfileItem, ProfilesConfig, VergeConfig},
  3. states::{ClashState, ProfilesState, VergeState},
  4. utils::{
  5. config::{read_clash, save_clash},
  6. fetch::fetch_profile,
  7. sysopt::SysProxyConfig,
  8. },
  9. };
  10. use serde_yaml::Mapping;
  11. use tauri::State;
  12. /// get all profiles from `profiles.yaml`
  13. /// do not acquire the lock of ProfileLock
  14. #[tauri::command]
  15. pub fn get_profiles(profiles: State<'_, ProfilesState>) -> Result<ProfilesConfig, String> {
  16. match profiles.0.lock() {
  17. Ok(profiles) => Ok(profiles.clone()),
  18. Err(_) => Err("failed to get profiles lock".into()),
  19. }
  20. }
  21. /// synchronize data irregularly
  22. #[tauri::command]
  23. pub fn sync_profiles(profiles: State<'_, ProfilesState>) -> Result<(), String> {
  24. match profiles.0.lock() {
  25. Ok(mut profiles) => profiles.sync_file(),
  26. Err(_) => Err("failed to get profiles lock".into()),
  27. }
  28. }
  29. /// Import the profile from url
  30. /// and save to `profiles.yaml`
  31. #[tauri::command]
  32. pub async fn import_profile(url: String, profiles: State<'_, ProfilesState>) -> Result<(), String> {
  33. match fetch_profile(&url).await {
  34. Some(result) => {
  35. let mut profiles = profiles.0.lock().unwrap();
  36. profiles.import_from_url(url, result)
  37. }
  38. None => Err(format!("failed to fetch profile from `{}`", url)),
  39. }
  40. }
  41. /// Update the profile
  42. /// and save to `profiles.yaml`
  43. /// http request firstly
  44. /// then acquire the lock of `profiles.yaml`
  45. #[tauri::command]
  46. pub async fn update_profile(
  47. index: usize,
  48. clash: State<'_, ClashState>,
  49. profiles: State<'_, ProfilesState>,
  50. ) -> Result<(), String> {
  51. // maybe we can get the url from the web app directly
  52. let url = match profiles.0.lock() {
  53. Ok(mut profile) => {
  54. let items = profile.items.take().unwrap_or(vec![]);
  55. if index >= items.len() {
  56. return Err("the index out of bound".into());
  57. }
  58. let url = match &items[index].url {
  59. Some(u) => u.clone(),
  60. None => return Err("failed to update profile for `invalid url`".into()),
  61. };
  62. profile.items = Some(items);
  63. url
  64. }
  65. Err(_) => return Err("failed to get profiles lock".into()),
  66. };
  67. match fetch_profile(&url).await {
  68. Some(result) => match profiles.0.lock() {
  69. Ok(mut profiles) => {
  70. profiles.update_item(index, result)?;
  71. // reactivate the profile
  72. let current = profiles.current.clone().unwrap_or(0);
  73. if current == index {
  74. let clash = clash.0.lock().unwrap();
  75. profiles.activate(clash.info.clone())
  76. } else {
  77. Ok(())
  78. }
  79. }
  80. Err(_) => Err("failed to get profiles lock".into()),
  81. },
  82. None => Err(format!("failed to fetch profile from `{}`", url)),
  83. }
  84. }
  85. /// change the current profile
  86. #[tauri::command]
  87. pub fn select_profile(
  88. index: usize,
  89. clash: State<'_, ClashState>,
  90. profiles: State<'_, ProfilesState>,
  91. ) -> Result<(), String> {
  92. let mut profiles = profiles.0.lock().unwrap();
  93. match profiles.put_current(index) {
  94. Ok(()) => {
  95. let clash = clash.0.lock().unwrap();
  96. profiles.activate(clash.info.clone())
  97. }
  98. Err(err) => Err(err),
  99. }
  100. }
  101. /// delete profile item
  102. #[tauri::command]
  103. pub fn delete_profile(
  104. index: usize,
  105. clash_state: State<'_, ClashState>,
  106. profiles_state: State<'_, ProfilesState>,
  107. ) -> Result<(), String> {
  108. let mut profiles = profiles_state.0.lock().unwrap();
  109. match profiles.delete_item(index) {
  110. Ok(change) => match change {
  111. true => {
  112. let clash = clash_state.0.lock().unwrap();
  113. profiles.activate(clash.info.clone())
  114. }
  115. false => Ok(()),
  116. },
  117. Err(err) => Err(err),
  118. }
  119. }
  120. /// patch the profile config
  121. #[tauri::command]
  122. pub fn patch_profile(
  123. index: usize,
  124. profile: ProfileItem,
  125. profiles: State<'_, ProfilesState>,
  126. ) -> Result<(), String> {
  127. match profiles.0.lock() {
  128. Ok(mut profiles) => profiles.patch_item(index, profile),
  129. Err(_) => Err("can not get profiles lock".into()),
  130. }
  131. }
  132. /// restart the sidecar
  133. #[tauri::command]
  134. pub fn restart_sidecar(
  135. clash_state: State<'_, ClashState>,
  136. profiles_state: State<'_, ProfilesState>,
  137. ) -> Result<(), String> {
  138. let mut clash = clash_state.0.lock().unwrap();
  139. match clash.restart_sidecar() {
  140. Ok(_) => {
  141. let profiles = profiles_state.0.lock().unwrap();
  142. match profiles.activate(clash.info.clone()) {
  143. Ok(()) => Ok(()),
  144. Err(err) => {
  145. log::error!("{}", err);
  146. Err(err)
  147. }
  148. }
  149. }
  150. Err(err) => {
  151. log::error!("{}", err);
  152. Err(err)
  153. }
  154. }
  155. }
  156. /// get the clash core info from the state
  157. /// the caller can also get the infomation by clash's api
  158. #[tauri::command]
  159. pub fn get_clash_info(clash_state: State<'_, ClashState>) -> Result<ClashInfo, String> {
  160. match clash_state.0.lock() {
  161. Ok(arc) => Ok(arc.info.clone()),
  162. Err(_) => Err("failed to get clash lock".into()),
  163. }
  164. }
  165. /// todo: need refactor
  166. /// update the clash core config
  167. /// after putting the change to the clash core
  168. /// then we should save the latest config
  169. #[tauri::command]
  170. pub fn patch_clash_config(payload: Mapping) -> Result<(), String> {
  171. let mut config = read_clash();
  172. for (key, value) in payload.iter() {
  173. if config.contains_key(key) {
  174. config[key] = value.clone();
  175. } else {
  176. config.insert(key.clone(), value.clone());
  177. }
  178. }
  179. save_clash(&config)
  180. }
  181. /// get the system proxy
  182. #[tauri::command]
  183. pub fn get_sys_proxy() -> Result<SysProxyConfig, String> {
  184. match SysProxyConfig::get_sys() {
  185. Ok(value) => Ok(value),
  186. Err(err) => Err(err.to_string()),
  187. }
  188. }
  189. /// get the current proxy config
  190. /// which may not the same as system proxy
  191. #[tauri::command]
  192. pub fn get_cur_proxy(verge_state: State<'_, VergeState>) -> Result<Option<SysProxyConfig>, String> {
  193. match verge_state.0.lock() {
  194. Ok(verge) => Ok(verge.cur_sysproxy.clone()),
  195. Err(_) => Err("failed to get verge lock".into()),
  196. }
  197. }
  198. /// get the verge config
  199. #[tauri::command]
  200. pub fn get_verge_config(verge_state: State<'_, VergeState>) -> Result<VergeConfig, String> {
  201. match verge_state.0.lock() {
  202. Ok(arc) => Ok(arc.config.clone()),
  203. Err(_) => Err("failed to get verge lock".into()),
  204. }
  205. }
  206. /// patch the verge config
  207. /// this command only save the config and not responsible for other things
  208. #[tauri::command]
  209. pub async fn patch_verge_config(
  210. payload: VergeConfig,
  211. verge_state: State<'_, VergeState>,
  212. ) -> Result<(), String> {
  213. let mut verge = verge_state.0.lock().unwrap();
  214. verge.patch_config(payload)
  215. }