cmds.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. #[deprecated]
  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 create_profile(item: PrfItem, file_data: Option<String>) -> CmdResult {
  31. let item = wrap_err!(PrfItem::from(item, file_data).await)?;
  32. wrap_err!(Config::profiles().data().append_item(item))
  33. }
  34. #[tauri::command]
  35. pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult {
  36. wrap_err!(feat::update_profile(index, option).await)
  37. }
  38. #[tauri::command]
  39. pub async fn delete_profile(index: String) -> CmdResult {
  40. let should_update = wrap_err!({ Config::profiles().data().delete_item(index) })?;
  41. if should_update {
  42. wrap_err!(CoreManager::global().update_config().await)?;
  43. handle::Handle::refresh_clash();
  44. }
  45. Ok(())
  46. }
  47. /// 修改profiles的
  48. #[tauri::command]
  49. pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult {
  50. wrap_err!({ Config::profiles().draft().patch_config(profiles) })?;
  51. match CoreManager::global().update_config().await {
  52. Ok(_) => {
  53. handle::Handle::refresh_clash();
  54. Config::profiles().apply();
  55. wrap_err!(Config::profiles().data().save_file())?;
  56. Ok(())
  57. }
  58. Err(err) => {
  59. Config::profiles().discard();
  60. log::error!(target: "app", "{err}");
  61. Err(format!("{err}"))
  62. }
  63. }
  64. }
  65. /// 修改某个profile item的
  66. #[tauri::command]
  67. pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
  68. wrap_err!(Config::profiles().data().patch_item(index, profile))?;
  69. wrap_err!(timer::Timer::global().refresh())
  70. }
  71. #[tauri::command]
  72. pub fn view_profile(index: String) -> CmdResult {
  73. let file = {
  74. wrap_err!(Config::profiles().latest().get_item(&index))?
  75. .file
  76. .clone()
  77. .ok_or("the file field is null")
  78. }?;
  79. let path = wrap_err!(dirs::app_profiles_dir())?.join(file);
  80. if !path.exists() {
  81. ret_err!("the file not found");
  82. }
  83. wrap_err!(help::open_file(path))
  84. }
  85. #[tauri::command]
  86. pub fn read_profile_file(index: String) -> CmdResult<String> {
  87. let profiles = Config::profiles();
  88. let profiles = profiles.latest();
  89. let item = wrap_err!(profiles.get_item(&index))?;
  90. let data = wrap_err!(item.read_file())?;
  91. Ok(data)
  92. }
  93. #[tauri::command]
  94. pub fn save_profile_file(index: String, file_data: Option<String>) -> CmdResult {
  95. if file_data.is_none() {
  96. return Ok(());
  97. }
  98. let profiles = Config::profiles();
  99. let profiles = profiles.latest();
  100. let item = wrap_err!(profiles.get_item(&index))?;
  101. wrap_err!(item.save_file(file_data.unwrap()))
  102. }
  103. #[tauri::command]
  104. pub fn get_clash_info() -> CmdResult<ClashInfo> {
  105. Ok(Config::clash().latest().get_client_info())
  106. }
  107. #[tauri::command]
  108. pub fn get_runtime_config() -> CmdResult<Option<Mapping>> {
  109. Ok(Config::runtime().latest().config.clone())
  110. }
  111. #[tauri::command]
  112. pub fn get_runtime_yaml() -> CmdResult<String> {
  113. let runtime = Config::runtime();
  114. let runtime = runtime.latest();
  115. let config = runtime.config.as_ref();
  116. wrap_err!(config
  117. .ok_or(anyhow::anyhow!("failed to parse config to yaml file"))
  118. .and_then(
  119. |config| serde_yaml::to_string(config).context("failed to convert config to yaml")
  120. ))
  121. }
  122. #[tauri::command]
  123. pub fn get_runtime_exists() -> CmdResult<Vec<String>> {
  124. Ok(Config::runtime().latest().exists_keys.clone())
  125. }
  126. #[tauri::command]
  127. pub fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String)>>> {
  128. Ok(Config::runtime().latest().chain_logs.clone())
  129. }
  130. #[tauri::command]
  131. pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
  132. wrap_err!(feat::patch_clash(payload).await)
  133. }
  134. #[tauri::command]
  135. pub fn get_verge_config() -> CmdResult<IVerge> {
  136. Ok(Config::verge().data().clone())
  137. }
  138. #[tauri::command]
  139. pub async fn patch_verge_config(payload: IVerge) -> CmdResult {
  140. wrap_err!(feat::patch_verge(payload).await)
  141. }
  142. #[tauri::command]
  143. pub async fn change_clash_core(clash_core: Option<String>) -> CmdResult {
  144. wrap_err!(CoreManager::global().change_core(clash_core).await)
  145. }
  146. /// restart the sidecar
  147. #[tauri::command]
  148. pub async fn restart_sidecar() -> CmdResult {
  149. wrap_err!(CoreManager::global().run_core().await)
  150. }
  151. /// get the system proxy
  152. #[tauri::command]
  153. pub fn get_sys_proxy() -> CmdResult<Mapping> {
  154. let current = wrap_err!(Sysproxy::get_system_proxy())?;
  155. let mut map = Mapping::new();
  156. map.insert("enable".into(), current.enable.into());
  157. map.insert(
  158. "server".into(),
  159. format!("{}:{}", current.host, current.port).into(),
  160. );
  161. map.insert("bypass".into(), current.bypass.into());
  162. Ok(map)
  163. }
  164. #[tauri::command]
  165. pub fn get_clash_logs() -> CmdResult<VecDeque<String>> {
  166. Ok(logger::Logger::global().get_log())
  167. }
  168. #[tauri::command]
  169. pub fn open_app_dir() -> CmdResult<()> {
  170. let app_dir = wrap_err!(dirs::app_home_dir())?;
  171. wrap_err!(open::that(app_dir))
  172. }
  173. #[tauri::command]
  174. pub fn open_logs_dir() -> CmdResult<()> {
  175. let log_dir = wrap_err!(dirs::app_logs_dir())?;
  176. wrap_err!(open::that(log_dir))
  177. }
  178. #[tauri::command]
  179. pub fn open_web_url(url: String) -> CmdResult<()> {
  180. wrap_err!(open::that(url))
  181. }
  182. #[cfg(windows)]
  183. pub mod service {
  184. use super::*;
  185. use crate::core::win_service;
  186. #[tauri::command]
  187. pub async fn check_service() -> CmdResult<win_service::JsonResponse> {
  188. wrap_err!(win_service::check_service().await)
  189. }
  190. #[tauri::command]
  191. pub async fn install_service() -> CmdResult {
  192. wrap_err!(win_service::install_service().await)
  193. }
  194. #[tauri::command]
  195. pub async fn uninstall_service() -> CmdResult {
  196. wrap_err!(win_service::uninstall_service().await)
  197. }
  198. }
  199. #[cfg(not(windows))]
  200. pub mod service {
  201. use super::*;
  202. #[tauri::command]
  203. pub async fn check_service() -> CmdResult {
  204. Ok(())
  205. }
  206. #[tauri::command]
  207. pub async fn install_service() -> CmdResult {
  208. Ok(())
  209. }
  210. #[tauri::command]
  211. pub async fn uninstall_service() -> CmdResult {
  212. Ok(())
  213. }
  214. }