cmds.rs 6.9 KB

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