feat.rs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //!
  2. //! feat mod 里的函数主要用于
  3. //! - hotkey 快捷键
  4. //! - timer 定时器
  5. //! - cmds 页面调用
  6. //!
  7. use crate::config::*;
  8. use crate::core::*;
  9. use crate::log_err;
  10. use anyhow::{bail, Result};
  11. use serde_yaml::{Mapping, Value};
  12. // 重启clash
  13. pub fn restart_clash_core() {
  14. tauri::async_runtime::spawn(async {
  15. match CoreManager::global().run_core().await {
  16. Ok(_) => {
  17. handle::Handle::refresh_clash();
  18. handle::Handle::notice_message("set_config::ok", "ok");
  19. }
  20. Err(err) => {
  21. handle::Handle::notice_message("set_config::error", format!("{err}"));
  22. log::error!(target:"app", "{err}");
  23. }
  24. }
  25. });
  26. }
  27. // 切换模式 rule/global/direct/script mode
  28. pub fn change_clash_mode(mode: String) {
  29. let mut mapping = Mapping::new();
  30. mapping.insert(Value::from("mode"), mode.clone().into());
  31. tauri::async_runtime::spawn(async move {
  32. log::debug!(target: "app", "change clash mode to {mode}");
  33. match clash_api::patch_configs(&mapping).await {
  34. Ok(_) => {
  35. // 更新配置
  36. Config::clash().data().patch_config(mapping);
  37. if Config::clash().data().save_config().is_ok() {
  38. handle::Handle::refresh_clash();
  39. log_err!(handle::Handle::update_systray_part());
  40. }
  41. }
  42. Err(err) => log::error!(target: "app", "{err}"),
  43. }
  44. });
  45. }
  46. // 切换系统代理
  47. pub fn toggle_system_proxy() {
  48. let enable = Config::verge().draft().enable_system_proxy.clone();
  49. let enable = enable.unwrap_or(false);
  50. tauri::async_runtime::spawn(async move {
  51. match patch_verge(IVerge {
  52. enable_system_proxy: Some(!enable),
  53. ..IVerge::default()
  54. })
  55. .await
  56. {
  57. Ok(_) => handle::Handle::refresh_verge(),
  58. Err(err) => log::error!(target: "app", "{err}"),
  59. }
  60. });
  61. }
  62. // 打开系统代理
  63. pub fn enable_system_proxy() {
  64. tauri::async_runtime::spawn(async {
  65. match patch_verge(IVerge {
  66. enable_system_proxy: Some(true),
  67. ..IVerge::default()
  68. })
  69. .await
  70. {
  71. Ok(_) => handle::Handle::refresh_verge(),
  72. Err(err) => log::error!(target: "app", "{err}"),
  73. }
  74. });
  75. }
  76. // 关闭系统代理
  77. pub fn disable_system_proxy() {
  78. tauri::async_runtime::spawn(async {
  79. match patch_verge(IVerge {
  80. enable_system_proxy: Some(false),
  81. ..IVerge::default()
  82. })
  83. .await
  84. {
  85. Ok(_) => handle::Handle::refresh_verge(),
  86. Err(err) => log::error!(target: "app", "{err}"),
  87. }
  88. });
  89. }
  90. // 切换tun模式
  91. pub fn toggle_tun_mode() {
  92. let enable = Config::verge().data().enable_tun_mode.clone();
  93. let enable = enable.unwrap_or(false);
  94. tauri::async_runtime::spawn(async move {
  95. match patch_verge(IVerge {
  96. enable_tun_mode: Some(!enable),
  97. ..IVerge::default()
  98. })
  99. .await
  100. {
  101. Ok(_) => handle::Handle::refresh_verge(),
  102. Err(err) => log::error!(target: "app", "{err}"),
  103. }
  104. });
  105. }
  106. // 打开tun模式
  107. pub fn enable_tun_mode() {
  108. tauri::async_runtime::spawn(async {
  109. match patch_verge(IVerge {
  110. enable_tun_mode: Some(true),
  111. ..IVerge::default()
  112. })
  113. .await
  114. {
  115. Ok(_) => handle::Handle::refresh_verge(),
  116. Err(err) => log::error!(target: "app", "{err}"),
  117. }
  118. });
  119. }
  120. // 关闭tun模式
  121. pub fn disable_tun_mode() {
  122. tauri::async_runtime::spawn(async {
  123. match patch_verge(IVerge {
  124. enable_tun_mode: Some(false),
  125. ..IVerge::default()
  126. })
  127. .await
  128. {
  129. Ok(_) => handle::Handle::refresh_verge(),
  130. Err(err) => log::error!(target: "app", "{err}"),
  131. }
  132. });
  133. }
  134. /// 修改clash的配置
  135. pub async fn patch_clash(patch: Mapping) -> Result<()> {
  136. Config::clash().draft().patch_config(patch.clone());
  137. match {
  138. let mixed_port = patch.get("mixed-port");
  139. if mixed_port.is_some() {
  140. let changed = mixed_port != Config::clash().data().0.get("mixed-port");
  141. // 检查端口占用
  142. if changed {
  143. if let Some(port) = mixed_port.clone().unwrap().as_u64() {
  144. if !port_scanner::local_port_available(port as u16) {
  145. Config::clash().discard();
  146. bail!("port already in use");
  147. }
  148. }
  149. }
  150. };
  151. // 激活配置
  152. if mixed_port.is_some()
  153. || patch.get("secret").is_some()
  154. || patch.get("external-controller").is_some()
  155. {
  156. Config::generate()?;
  157. CoreManager::global().run_core().await?;
  158. handle::Handle::refresh_clash();
  159. }
  160. // 更新系统代理
  161. if mixed_port.is_some() {
  162. log_err!(sysopt::Sysopt::global().init_sysproxy());
  163. }
  164. if patch.get("mode").is_some() {
  165. log_err!(handle::Handle::update_systray_part());
  166. }
  167. Config::runtime().latest().patch_config(patch);
  168. <Result<()>>::Ok(())
  169. } {
  170. Ok(()) => {
  171. Config::clash().apply();
  172. Config::clash().data().save_config()?;
  173. Ok(())
  174. }
  175. Err(err) => {
  176. Config::clash().discard();
  177. Err(err)
  178. }
  179. }
  180. }
  181. /// 修改verge的配置
  182. /// 一般都是一个个的修改
  183. pub async fn patch_verge(patch: IVerge) -> Result<()> {
  184. Config::verge().draft().patch_config(patch.clone());
  185. let tun_mode = patch.enable_tun_mode;
  186. let auto_launch = patch.enable_auto_launch;
  187. let system_proxy = patch.enable_system_proxy;
  188. let proxy_bypass = patch.system_proxy_bypass;
  189. let language = patch.language;
  190. match {
  191. #[cfg(target_os = "windows")]
  192. {
  193. let service_mode = patch.enable_service_mode;
  194. if service_mode.is_some() {
  195. log::debug!(target: "app", "change service mode to {}", service_mode.unwrap());
  196. Config::generate()?;
  197. CoreManager::global().run_core().await?;
  198. } else if tun_mode.is_some() {
  199. update_core_config().await?;
  200. }
  201. }
  202. #[cfg(not(target_os = "windows"))]
  203. if tun_mode.is_some() {
  204. update_core_config().await?;
  205. }
  206. if auto_launch.is_some() {
  207. sysopt::Sysopt::global().update_launch()?;
  208. }
  209. if system_proxy.is_some() || proxy_bypass.is_some() {
  210. sysopt::Sysopt::global().update_sysproxy()?;
  211. sysopt::Sysopt::global().guard_proxy();
  212. }
  213. if let Some(true) = patch.enable_proxy_guard {
  214. sysopt::Sysopt::global().guard_proxy();
  215. }
  216. if let Some(hotkeys) = patch.hotkeys {
  217. hotkey::Hotkey::global().update(hotkeys)?;
  218. }
  219. if language.is_some() {
  220. handle::Handle::update_systray()?;
  221. } else if system_proxy.or(tun_mode).is_some() {
  222. handle::Handle::update_systray_part()?;
  223. }
  224. <Result<()>>::Ok(())
  225. } {
  226. Ok(()) => {
  227. Config::verge().apply();
  228. Config::verge().data().save_file()?;
  229. Ok(())
  230. }
  231. Err(err) => {
  232. Config::verge().discard();
  233. Err(err)
  234. }
  235. }
  236. }
  237. /// 更新某个profile
  238. /// 如果更新当前配置就激活配置
  239. pub async fn update_profile(uid: String, option: Option<PrfOption>) -> Result<()> {
  240. let url_opt = {
  241. let profiles = Config::profiles();
  242. let profiles = profiles.latest();
  243. let item = profiles.get_item(&uid)?;
  244. let is_remote = item.itype.as_ref().map_or(false, |s| s == "remote");
  245. if !is_remote {
  246. None // 直接更新
  247. } else if item.url.is_none() {
  248. bail!("failed to get the profile item url");
  249. } else {
  250. Some((item.url.clone().unwrap(), item.option.clone()))
  251. }
  252. };
  253. let should_update = match url_opt {
  254. Some((url, opt)) => {
  255. let merged_opt = PrfOption::merge(opt, option);
  256. let item = PrfItem::from_url(&url, None, None, merged_opt).await?;
  257. let profiles = Config::profiles();
  258. let mut profiles = profiles.latest();
  259. profiles.update_item(uid.clone(), item)?;
  260. Some(uid) == profiles.get_current()
  261. }
  262. None => true,
  263. };
  264. if should_update {
  265. update_core_config().await?;
  266. }
  267. Ok(())
  268. }
  269. /// 更新配置
  270. async fn update_core_config() -> Result<()> {
  271. match CoreManager::global().update_config().await {
  272. Ok(_) => {
  273. handle::Handle::refresh_clash();
  274. handle::Handle::notice_message("set_config::ok", "ok");
  275. Ok(())
  276. }
  277. Err(err) => {
  278. handle::Handle::notice_message("set_config::error", format!("{err}"));
  279. Err(err)
  280. }
  281. }
  282. }