cmds.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. use crate::{
  2. config::*,
  3. core::*,
  4. feat,
  5. utils::{dirs, help, resolve},
  6. };
  7. use crate::{ret_err, wrap_err};
  8. use anyhow::{Context, Result};
  9. use network_interface::NetworkInterface;
  10. use serde_yaml::Mapping;
  11. use std::collections::{HashMap, VecDeque};
  12. use sysproxy::{Autoproxy, Sysproxy};
  13. use tauri::{api, Manager};
  14. type CmdResult<T = ()> = Result<T, String>;
  15. #[tauri::command]
  16. pub fn copy_clash_env(app_handle: tauri::AppHandle) -> CmdResult {
  17. feat::copy_clash_env(&app_handle);
  18. Ok(())
  19. }
  20. #[tauri::command]
  21. pub fn get_profiles() -> CmdResult<IProfiles> {
  22. Ok(Config::profiles().data().clone())
  23. }
  24. #[tauri::command]
  25. pub async fn enhance_profiles() -> CmdResult {
  26. wrap_err!(CoreManager::global().update_config().await)?;
  27. handle::Handle::refresh_clash();
  28. Ok(())
  29. }
  30. #[tauri::command]
  31. pub async fn import_profile(url: String, option: Option<PrfOption>) -> CmdResult {
  32. let item = wrap_err!(PrfItem::from_url(&url, None, None, option).await)?;
  33. wrap_err!(Config::profiles().data().append_item(item))
  34. }
  35. #[tauri::command]
  36. pub async fn reorder_profile(active_id: String, over_id: String) -> CmdResult {
  37. wrap_err!(Config::profiles().data().reorder(active_id, over_id))
  38. }
  39. #[tauri::command]
  40. pub async fn create_profile(item: PrfItem, file_data: Option<String>) -> CmdResult {
  41. let item = wrap_err!(PrfItem::from(item, file_data).await)?;
  42. wrap_err!(Config::profiles().data().append_item(item))
  43. }
  44. #[tauri::command]
  45. pub async fn update_profile(index: String, option: Option<PrfOption>) -> CmdResult {
  46. wrap_err!(feat::update_profile(index, option).await)
  47. }
  48. #[tauri::command]
  49. pub async fn delete_profile(index: String) -> CmdResult {
  50. let should_update = wrap_err!({ Config::profiles().data().delete_item(index) })?;
  51. if should_update {
  52. wrap_err!(CoreManager::global().update_config().await)?;
  53. handle::Handle::refresh_clash();
  54. }
  55. Ok(())
  56. }
  57. /// 修改profiles的
  58. #[tauri::command]
  59. pub async fn patch_profiles_config(profiles: IProfiles) -> CmdResult {
  60. wrap_err!({ Config::profiles().draft().patch_config(profiles) })?;
  61. match CoreManager::global().update_config().await {
  62. Ok(_) => {
  63. handle::Handle::refresh_clash();
  64. let _ = handle::Handle::update_systray_part();
  65. Config::profiles().apply();
  66. wrap_err!(Config::profiles().data().save_file())?;
  67. Ok(())
  68. }
  69. Err(err) => {
  70. Config::profiles().discard();
  71. log::error!(target: "app", "{err}");
  72. Err(format!("{err}"))
  73. }
  74. }
  75. }
  76. /// 修改某个profile item的
  77. #[tauri::command]
  78. pub fn patch_profile(index: String, profile: PrfItem) -> CmdResult {
  79. wrap_err!(Config::profiles().data().patch_item(index, profile))?;
  80. wrap_err!(timer::Timer::global().refresh())
  81. }
  82. #[tauri::command]
  83. pub fn view_profile(app_handle: tauri::AppHandle, index: String) -> CmdResult {
  84. let file = {
  85. wrap_err!(Config::profiles().latest().get_item(&index))?
  86. .file
  87. .clone()
  88. .ok_or("the file field is null")
  89. }?;
  90. let path = wrap_err!(dirs::app_profiles_dir())?.join(file);
  91. if !path.exists() {
  92. ret_err!("the file not found");
  93. }
  94. wrap_err!(help::open_file(app_handle, path))
  95. }
  96. #[tauri::command]
  97. pub fn read_profile_file(index: String) -> CmdResult<String> {
  98. let profiles = Config::profiles();
  99. let profiles = profiles.latest();
  100. let item = wrap_err!(profiles.get_item(&index))?;
  101. let data = wrap_err!(item.read_file())?;
  102. Ok(data)
  103. }
  104. #[tauri::command]
  105. pub fn save_profile_file(index: String, file_data: Option<String>) -> CmdResult {
  106. if file_data.is_none() {
  107. return Ok(());
  108. }
  109. let profiles = Config::profiles();
  110. let profiles = profiles.latest();
  111. let item = wrap_err!(profiles.get_item(&index))?;
  112. wrap_err!(item.save_file(file_data.unwrap()))
  113. }
  114. #[tauri::command]
  115. pub fn get_clash_info() -> CmdResult<ClashInfo> {
  116. Ok(Config::clash().latest().get_client_info())
  117. }
  118. #[tauri::command]
  119. pub fn get_runtime_config() -> CmdResult<Option<Mapping>> {
  120. Ok(Config::runtime().latest().config.clone())
  121. }
  122. #[tauri::command]
  123. pub fn get_runtime_yaml() -> CmdResult<String> {
  124. let runtime = Config::runtime();
  125. let runtime = runtime.latest();
  126. let config = runtime.config.as_ref();
  127. wrap_err!(config
  128. .ok_or(anyhow::anyhow!("failed to parse config to yaml file"))
  129. .and_then(
  130. |config| serde_yaml::to_string(config).context("failed to convert config to yaml")
  131. ))
  132. }
  133. #[tauri::command]
  134. pub fn get_runtime_exists() -> CmdResult<Vec<String>> {
  135. Ok(Config::runtime().latest().exists_keys.clone())
  136. }
  137. #[tauri::command]
  138. pub fn get_runtime_logs() -> CmdResult<HashMap<String, Vec<(String, String)>>> {
  139. Ok(Config::runtime().latest().chain_logs.clone())
  140. }
  141. #[tauri::command]
  142. pub async fn patch_clash_config(payload: Mapping) -> CmdResult {
  143. wrap_err!(feat::patch_clash(payload).await)
  144. }
  145. #[tauri::command]
  146. pub fn get_verge_config() -> CmdResult<IVerge> {
  147. Ok(Config::verge().data().clone())
  148. }
  149. #[tauri::command]
  150. pub async fn patch_verge_config(payload: IVerge) -> CmdResult {
  151. wrap_err!(feat::patch_verge(payload).await)
  152. }
  153. #[tauri::command]
  154. pub async fn change_clash_core(clash_core: Option<String>) -> CmdResult {
  155. wrap_err!(CoreManager::global().change_core(clash_core).await)
  156. }
  157. /// restart the sidecar
  158. #[tauri::command]
  159. pub async fn restart_sidecar() -> CmdResult {
  160. wrap_err!(CoreManager::global().run_core().await)
  161. }
  162. /// get the system proxy
  163. #[tauri::command]
  164. pub fn get_sys_proxy() -> CmdResult<Mapping> {
  165. let current = wrap_err!(Sysproxy::get_system_proxy())?;
  166. let mut map = Mapping::new();
  167. map.insert("enable".into(), current.enable.into());
  168. map.insert(
  169. "server".into(),
  170. format!("{}:{}", current.host, current.port).into(),
  171. );
  172. map.insert("bypass".into(), current.bypass.into());
  173. Ok(map)
  174. }
  175. /// get the system proxy
  176. #[tauri::command]
  177. pub fn get_auto_proxy() -> CmdResult<Mapping> {
  178. let current = wrap_err!(Autoproxy::get_auto_proxy())?;
  179. let mut map = Mapping::new();
  180. map.insert("enable".into(), current.enable.into());
  181. map.insert("url".into(), current.url.into());
  182. Ok(map)
  183. }
  184. #[tauri::command]
  185. pub fn get_clash_logs() -> CmdResult<VecDeque<String>> {
  186. Ok(logger::Logger::global().get_log())
  187. }
  188. #[tauri::command]
  189. pub fn open_app_dir() -> CmdResult<()> {
  190. let app_dir = wrap_err!(dirs::app_home_dir())?;
  191. wrap_err!(open::that(app_dir))
  192. }
  193. #[tauri::command]
  194. pub fn open_core_dir() -> CmdResult<()> {
  195. let core_dir = wrap_err!(tauri::utils::platform::current_exe())?;
  196. let core_dir = core_dir.parent().ok_or("failed to get core dir")?;
  197. wrap_err!(open::that(core_dir))
  198. }
  199. #[tauri::command]
  200. pub fn open_logs_dir() -> CmdResult<()> {
  201. let log_dir = wrap_err!(dirs::app_logs_dir())?;
  202. wrap_err!(open::that(log_dir))
  203. }
  204. #[tauri::command]
  205. pub fn open_web_url(url: String) -> CmdResult<()> {
  206. wrap_err!(open::that(url))
  207. }
  208. #[cfg(windows)]
  209. pub mod uwp {
  210. use super::*;
  211. use crate::core::win_uwp;
  212. #[tauri::command]
  213. pub async fn invoke_uwp_tool() -> CmdResult {
  214. wrap_err!(win_uwp::invoke_uwptools().await)
  215. }
  216. }
  217. #[tauri::command]
  218. pub async fn clash_api_get_proxy_delay(
  219. name: String,
  220. url: Option<String>,
  221. timeout: i32,
  222. ) -> CmdResult<clash_api::DelayRes> {
  223. match clash_api::get_proxy_delay(name, url, timeout).await {
  224. Ok(res) => Ok(res),
  225. Err(err) => Err(err.to_string()),
  226. }
  227. }
  228. #[tauri::command]
  229. pub fn get_portable_flag() -> CmdResult<bool> {
  230. Ok(*dirs::PORTABLE_FLAG.get().unwrap_or(&false))
  231. }
  232. #[tauri::command]
  233. pub async fn test_delay(url: String) -> CmdResult<u32> {
  234. Ok(feat::test_delay(url).await.unwrap_or(10000u32))
  235. }
  236. #[tauri::command]
  237. pub fn get_app_dir() -> CmdResult<String> {
  238. let app_home_dir = wrap_err!(dirs::app_home_dir())?
  239. .to_string_lossy()
  240. .to_string();
  241. Ok(app_home_dir)
  242. }
  243. #[tauri::command]
  244. pub async fn download_icon_cache(url: String, name: String) -> CmdResult<String> {
  245. let icon_cache_dir = wrap_err!(dirs::app_home_dir())?.join("icons").join("cache");
  246. let icon_path = icon_cache_dir.join(name);
  247. if !icon_cache_dir.exists() {
  248. let _ = std::fs::create_dir_all(&icon_cache_dir);
  249. }
  250. if !icon_path.exists() {
  251. let response = wrap_err!(reqwest::get(url).await)?;
  252. let mut file = wrap_err!(std::fs::File::create(&icon_path))?;
  253. let content = wrap_err!(response.bytes().await)?;
  254. wrap_err!(std::io::copy(&mut content.as_ref(), &mut file))?;
  255. }
  256. Ok(icon_path.to_string_lossy().to_string())
  257. }
  258. #[tauri::command]
  259. pub fn copy_icon_file(path: String, name: String) -> CmdResult<String> {
  260. let file_path = std::path::Path::new(&path);
  261. let icon_dir = wrap_err!(dirs::app_home_dir())?.join("icons");
  262. if !icon_dir.exists() {
  263. let _ = std::fs::create_dir_all(&icon_dir);
  264. }
  265. let ext = match file_path.extension() {
  266. Some(e) => e.to_string_lossy().to_string(),
  267. None => "ico".to_string(),
  268. };
  269. let png_dest_path = icon_dir.join(format!("{name}.png"));
  270. let ico_dest_path = icon_dir.join(format!("{name}.ico"));
  271. let dest_path = icon_dir.join(format!("{name}.{ext}"));
  272. if file_path.exists() {
  273. std::fs::remove_file(png_dest_path).unwrap_or_default();
  274. std::fs::remove_file(ico_dest_path).unwrap_or_default();
  275. match std::fs::copy(file_path, &dest_path) {
  276. Ok(_) => Ok(dest_path.to_string_lossy().to_string()),
  277. Err(err) => Err(err.to_string()),
  278. }
  279. } else {
  280. Err("file not found".to_string())
  281. }
  282. }
  283. #[tauri::command]
  284. pub fn get_network_interfaces() -> Vec<String> {
  285. use sysinfo::Networks;
  286. let mut result = Vec::new();
  287. let networks = Networks::new_with_refreshed_list();
  288. for (interface_name, _) in &networks {
  289. result.push(interface_name.clone());
  290. }
  291. return result;
  292. }
  293. #[tauri::command]
  294. pub fn get_network_interfaces_info() -> CmdResult<Vec<NetworkInterface>> {
  295. use network_interface::NetworkInterface;
  296. use network_interface::NetworkInterfaceConfig;
  297. let names = get_network_interfaces();
  298. let interfaces = wrap_err!(NetworkInterface::show())?;
  299. let mut result = Vec::new();
  300. for interface in interfaces {
  301. if names.contains(&interface.name) {
  302. result.push(interface);
  303. }
  304. }
  305. Ok(result)
  306. }
  307. #[tauri::command]
  308. pub fn open_devtools(app_handle: tauri::AppHandle) {
  309. if let Some(window) = app_handle.get_window("main") {
  310. if !window.is_devtools_open() {
  311. window.open_devtools();
  312. } else {
  313. window.close_devtools();
  314. }
  315. }
  316. }
  317. #[tauri::command]
  318. pub fn exit_app(app_handle: tauri::AppHandle) {
  319. let _ = resolve::save_window_size_position(&app_handle, true);
  320. resolve::resolve_reset();
  321. api::process::kill_children();
  322. app_handle.exit(0);
  323. std::process::exit(0);
  324. }
  325. pub mod service {
  326. use super::*;
  327. use crate::core::service;
  328. #[tauri::command]
  329. pub async fn check_service() -> CmdResult<service::JsonResponse> {
  330. wrap_err!(service::check_service().await)
  331. }
  332. #[tauri::command]
  333. pub async fn install_service() -> CmdResult {
  334. wrap_err!(service::install_service().await)
  335. }
  336. #[tauri::command]
  337. pub async fn uninstall_service() -> CmdResult {
  338. wrap_err!(service::uninstall_service().await)
  339. }
  340. }
  341. #[cfg(not(windows))]
  342. pub mod uwp {
  343. use super::*;
  344. #[tauri::command]
  345. pub async fn invoke_uwp_tool() -> CmdResult {
  346. Ok(())
  347. }
  348. }