cmds.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. #[tauri::command]
  151. pub fn grant_permission(_core: String) -> CmdResult {
  152. #[cfg(any(target_os = "macos", target_os = "linux"))]
  153. return wrap_err!(manager::grant_permission(_core));
  154. #[cfg(not(any(target_os = "macos", target_os = "linux")))]
  155. return Err("Unsupported target".into());
  156. }
  157. /// get the system proxy
  158. #[tauri::command]
  159. pub fn get_sys_proxy() -> CmdResult<Mapping> {
  160. let current = wrap_err!(Sysproxy::get_system_proxy())?;
  161. let mut map = Mapping::new();
  162. map.insert("enable".into(), current.enable.into());
  163. map.insert(
  164. "server".into(),
  165. format!("{}:{}", current.host, current.port).into(),
  166. );
  167. map.insert("bypass".into(), current.bypass.into());
  168. Ok(map)
  169. }
  170. #[tauri::command]
  171. pub fn get_clash_logs() -> CmdResult<VecDeque<String>> {
  172. Ok(logger::Logger::global().get_log())
  173. }
  174. #[tauri::command]
  175. pub fn open_app_dir() -> CmdResult<()> {
  176. let app_dir = wrap_err!(dirs::app_home_dir())?;
  177. wrap_err!(open::that(app_dir))
  178. }
  179. #[tauri::command]
  180. pub fn open_core_dir() -> CmdResult<()> {
  181. let core_dir = wrap_err!(tauri::utils::platform::current_exe())?;
  182. let core_dir = core_dir.parent().ok_or(format!("failed to get core dir"))?;
  183. wrap_err!(open::that(core_dir))
  184. }
  185. #[tauri::command]
  186. pub fn open_logs_dir() -> CmdResult<()> {
  187. let log_dir = wrap_err!(dirs::app_logs_dir())?;
  188. wrap_err!(open::that(log_dir))
  189. }
  190. #[tauri::command]
  191. pub fn open_web_url(url: String) -> CmdResult<()> {
  192. wrap_err!(open::that(url))
  193. }
  194. #[cfg(windows)]
  195. pub mod uwp {
  196. use super::*;
  197. use crate::core::win_uwp;
  198. #[tauri::command]
  199. pub async fn invoke_uwp_tool() -> CmdResult {
  200. wrap_err!(win_uwp::invoke_uwptools().await)
  201. }
  202. }
  203. #[tauri::command]
  204. pub async fn clash_api_get_proxy_delay(
  205. name: String,
  206. url: Option<String>,
  207. ) -> CmdResult<clash_api::DelayRes> {
  208. match clash_api::get_proxy_delay(name, url).await {
  209. Ok(res) => Ok(res),
  210. Err(err) => Err(format!("{}", err.to_string())),
  211. }
  212. }
  213. #[cfg(windows)]
  214. pub mod service {
  215. use super::*;
  216. use crate::core::win_service;
  217. #[tauri::command]
  218. pub async fn check_service() -> CmdResult<win_service::JsonResponse> {
  219. wrap_err!(win_service::check_service().await)
  220. }
  221. #[tauri::command]
  222. pub async fn install_service() -> CmdResult {
  223. wrap_err!(win_service::install_service().await)
  224. }
  225. #[tauri::command]
  226. pub async fn uninstall_service() -> CmdResult {
  227. wrap_err!(win_service::uninstall_service().await)
  228. }
  229. }
  230. #[cfg(not(windows))]
  231. pub mod service {
  232. use super::*;
  233. #[tauri::command]
  234. pub async fn check_service() -> CmdResult {
  235. Ok(())
  236. }
  237. #[tauri::command]
  238. pub async fn install_service() -> CmdResult {
  239. Ok(())
  240. }
  241. #[tauri::command]
  242. pub async fn uninstall_service() -> CmdResult {
  243. Ok(())
  244. }
  245. }
  246. #[cfg(not(windows))]
  247. pub mod uwp {
  248. use super::*;
  249. #[tauri::command]
  250. pub async fn invoke_uwp_tool() -> CmdResult {
  251. Ok(())
  252. }
  253. }