mod.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // use self::handle::Handle;
  2. // use self::hotkey::Hotkey;
  3. // use self::sysopt::Sysopt;
  4. // use self::timer::Timer;
  5. // // use crate::data::*;
  6. // // use crate::enhance::enhance_config;
  7. // use crate::log_if_err;
  8. // use anyhow::{bail, Result};
  9. // use once_cell::sync::OnceCell;
  10. // use parking_lot::Mutex;
  11. // use serde_yaml::{Mapping, Value};
  12. // use std::sync::Arc;
  13. pub mod clash_api;
  14. mod core;
  15. pub mod core_service;
  16. pub mod handle;
  17. pub mod hotkey;
  18. pub mod logger;
  19. // pub mod service;
  20. pub mod sysopt;
  21. pub mod timer;
  22. pub mod tray;
  23. pub use self::core::*;
  24. // pub use self::service::*;
  25. #[derive(Clone)]
  26. pub struct Core {}
  27. impl Core {
  28. // pub fn global() -> &'static Core {
  29. // static CORE: OnceCell<Core> = OnceCell::new();
  30. // CORE.get_or_init(|| Core {
  31. // service: Arc::new(Mutex::new(Service::new())),
  32. // sysopt: Arc::new(Mutex::new(Sysopt::new())),
  33. // timer: Arc::new(Mutex::new(Timer::new())),
  34. // hotkey: Arc::new(Mutex::new(Hotkey::new())),
  35. // runtime: Arc::new(Mutex::new(RuntimeResult::default())),
  36. // handle: Arc::new(Mutex::new(Handle::default())),
  37. // })
  38. // }
  39. // /// initialize the core state
  40. // pub fn init(&self, app_handle: tauri::AppHandle) {
  41. // kill old clash process
  42. // Service::kill_old_clash();
  43. // let mut handle = self.handle.lock();
  44. // handle.set_inner(app_handle.clone());
  45. // drop(handle);
  46. // let mut service = self.service.lock();
  47. // log_if_err!(service.start());
  48. // drop(service);
  49. // log_if_err!(self.activate());
  50. // let mut sysopt = self.sysopt.lock();
  51. // log_if_err!(sysopt.init_launch());
  52. // log_if_err!(sysopt.init_sysproxy());
  53. // drop(sysopt);
  54. // let handle = self.handle.lock();
  55. // log_if_err!(handle.update_systray_part());
  56. // drop(handle);
  57. // let mut hotkey = self.hotkey.lock();
  58. // log_if_err!(hotkey.init(app_handle));
  59. // drop(hotkey);
  60. // // timer initialize
  61. // let mut timer = self.timer.lock();
  62. // log_if_err!(timer.restore());
  63. // }
  64. // /// restart the clash sidecar
  65. // pub fn restart_clash(&self) -> Result<()> {
  66. // let mut service = self.service.lock();
  67. // service.restart()?;
  68. // drop(service);
  69. // self.activate()
  70. // }
  71. // /// change the clash core
  72. // pub fn change_core(&self, clash_core: Option<String>) -> Result<()> {
  73. // let clash_core = clash_core.unwrap_or("clash".into());
  74. // if &clash_core != "clash" && &clash_core != "clash-meta" {
  75. // bail!("invalid clash core name \"{clash_core}\"");
  76. // }
  77. // let global = Data::global();
  78. // let mut verge = global.verge.lock();
  79. // verge.patch_config(Verge {
  80. // clash_core: Some(clash_core.clone()),
  81. // ..Verge::default()
  82. // })?;
  83. // drop(verge);
  84. // let mut service = self.service.lock();
  85. // service.clear_logs();
  86. // service.restart()?;
  87. // drop(service);
  88. // self.activate()
  89. // }
  90. // /// Patch Clash
  91. // /// handle the clash config changed
  92. // pub fn patch_clash(&self, patch: Mapping) -> Result<()> {
  93. // let patch_cloned = patch.clone();
  94. // let clash_mode = patch.get("mode");
  95. // let mixed_port = patch.get("mixed-port");
  96. // let external = patch.get("external-controller");
  97. // let secret = patch.get("secret");
  98. // let valid_port = {
  99. // let global = Data::global();
  100. // let mut clash = global.clash.lock();
  101. // clash.patch_config(patch_cloned)?;
  102. // clash.info.port.is_some()
  103. // };
  104. // // todo: port check
  105. // if (mixed_port.is_some() && valid_port) || external.is_some() || secret.is_some() {
  106. // let mut service = self.service.lock();
  107. // service.restart()?;
  108. // drop(service);
  109. // self.activate()?;
  110. // let mut sysopt = self.sysopt.lock();
  111. // sysopt.init_sysproxy()?;
  112. // }
  113. // if clash_mode.is_some() {
  114. // let handle = self.handle.lock();
  115. // handle.update_systray_part()?;
  116. // }
  117. // Ok(())
  118. // }
  119. // /// Patch Verge
  120. // pub fn patch_verge(&self, patch: Verge) -> Result<()> {
  121. // // save the patch
  122. // let global = Data::global();
  123. // let mut verge = global.verge.lock();
  124. // verge.patch_config(patch.clone())?;
  125. // drop(verge);
  126. // let tun_mode = patch.enable_tun_mode;
  127. // let auto_launch = patch.enable_auto_launch;
  128. // let system_proxy = patch.enable_system_proxy;
  129. // let proxy_bypass = patch.system_proxy_bypass;
  130. // let proxy_guard = patch.enable_proxy_guard;
  131. // let language = patch.language;
  132. // #[cfg(target_os = "windows")]
  133. // {
  134. // let service_mode = patch.enable_service_mode;
  135. // // 重启服务
  136. // if service_mode.is_some() {
  137. // let mut service = self.service.lock();
  138. // service.restart()?;
  139. // drop(service);
  140. // }
  141. // if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) {
  142. // let wintun_dll = crate::utils::dirs::app_home_dir().join("wintun.dll");
  143. // if !wintun_dll.exists() {
  144. // bail!("failed to enable TUN for missing `wintun.dll`");
  145. // }
  146. // }
  147. // if service_mode.is_some() || tun_mode.is_some() {
  148. // self.activate()?;
  149. // }
  150. // }
  151. // #[cfg(not(target_os = "windows"))]
  152. // if tun_mode.is_some() {
  153. // self.activate()?;
  154. // }
  155. // let mut sysopt = self.sysopt.lock();
  156. // if auto_launch.is_some() {
  157. // sysopt.update_launch()?;
  158. // }
  159. // if system_proxy.is_some() || proxy_bypass.is_some() {
  160. // sysopt.update_sysproxy()?;
  161. // sysopt.guard_proxy();
  162. // }
  163. // if proxy_guard.unwrap_or(false) {
  164. // sysopt.guard_proxy();
  165. // }
  166. // // 更新tray
  167. // if language.is_some() {
  168. // let handle = self.handle.lock();
  169. // handle.update_systray()?;
  170. // } else if system_proxy.is_some() || tun_mode.is_some() {
  171. // let handle = self.handle.lock();
  172. // handle.update_systray_part()?;
  173. // }
  174. // if patch.hotkeys.is_some() {
  175. // let mut hotkey = self.hotkey.lock();
  176. // hotkey.update(patch.hotkeys.unwrap())?;
  177. // }
  178. // Ok(())
  179. // }
  180. // /// update rule/global/direct/script mode
  181. // pub fn update_mode(&self, mode: &str) -> Result<()> {
  182. // // save config to file
  183. // let info = {
  184. // let global = Data::global();
  185. // let mut clash = global.clash.lock();
  186. // clash.config.insert(Value::from("mode"), Value::from(mode));
  187. // clash.save_config()?;
  188. // clash.info.clone()
  189. // };
  190. // let mut mapping = Mapping::new();
  191. // mapping.insert(Value::from("mode"), Value::from(mode));
  192. // let handle = self.handle.clone();
  193. // tauri::async_runtime::spawn(async move {
  194. // log_if_err!(Service::patch_config(info, mapping.to_owned()).await);
  195. // // update tray
  196. // let handle = handle.lock();
  197. // handle.refresh_clash();
  198. // log_if_err!(handle.update_systray_part());
  199. // });
  200. // Ok(())
  201. // }
  202. // /// activate the profile
  203. // /// auto activate enhanced profile
  204. // /// 触发clash配置更新
  205. // pub fn activate(&self) -> Result<()> {
  206. // let global = Data::global();
  207. // let verge = global.verge.lock();
  208. // let clash = global.clash.lock();
  209. // let profiles = global.profiles.lock();
  210. // let tun_mode = verge.enable_tun_mode.clone().unwrap_or(false);
  211. // let profile_activate = profiles.gen_activate()?;
  212. // let clash_config = clash.config.clone();
  213. // let clash_info = clash.info.clone();
  214. // drop(clash);
  215. // drop(verge);
  216. // drop(profiles);
  217. // let (config, exists_keys, logs) = enhance_config(
  218. // clash_config,
  219. // profile_activate.current,
  220. // profile_activate.chain,
  221. // profile_activate.valid,
  222. // tun_mode,
  223. // );
  224. // let mut runtime = self.runtime.lock();
  225. // *runtime = RuntimeResult {
  226. // config: Some(config.clone()),
  227. // config_yaml: Some(serde_yaml::to_string(&config).unwrap_or("".into())),
  228. // exists_keys,
  229. // chain_logs: logs,
  230. // };
  231. // drop(runtime);
  232. // let mut service = self.service.lock();
  233. // service.check_start()?;
  234. // drop(service);
  235. // let handle = self.handle.clone();
  236. // tauri::async_runtime::spawn(async move {
  237. // match Service::set_config(clash_info, config).await {
  238. // Ok(_) => {
  239. // let handle = handle.lock();
  240. // handle.refresh_clash();
  241. // handle.notice_message("set_config::ok".into(), "ok".into());
  242. // }
  243. // Err(err) => {
  244. // let handle = handle.lock();
  245. // handle.notice_message("set_config::error".into(), format!("{err}"));
  246. // log::error!(target: "app", "{err}")
  247. // }
  248. // }
  249. // });
  250. // Ok(())
  251. // }
  252. // /// Static function
  253. // /// update profile item
  254. // pub async fn update_profile_item(&self, uid: String, option: Option<PrfOption>) -> Result<()> {
  255. // let global = Data::global();
  256. // let (url, opt) = {
  257. // let profiles = global.profiles.lock();
  258. // let item = profiles.get_item(&uid)?;
  259. // if let Some(typ) = item.itype.as_ref() {
  260. // // maybe only valid for `local` profile
  261. // if *typ != "remote" {
  262. // // reactivate the config
  263. // if Some(uid) == profiles.get_current() {
  264. // drop(profiles);
  265. // self.activate()?;
  266. // }
  267. // return Ok(());
  268. // }
  269. // }
  270. // if item.url.is_none() {
  271. // bail!("failed to get the profile item url");
  272. // }
  273. // (item.url.clone().unwrap(), item.option.clone())
  274. // };
  275. // let merged_opt = PrfOption::merge(opt, option);
  276. // let item = PrfItem::from_url(&url, None, None, merged_opt).await?;
  277. // let mut profiles = global.profiles.lock();
  278. // profiles.update_item(uid.clone(), item)?;
  279. // // reactivate the profile
  280. // if Some(uid) == profiles.get_current() {
  281. // drop(profiles);
  282. // self.activate()?;
  283. // }
  284. // Ok(())
  285. // }
  286. }