diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 3f78dad..24f9943 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -4505,7 +4505,6 @@ dependencies = [ "base64 0.22.1", "bytes", "encoding_rs", - "futures-channel", "futures-core", "futures-util", "h2 0.4.5", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 64a201f..2f00f8d 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -35,7 +35,7 @@ percent-encoding = "2.3.1" window-shadows = { version = "0.2" } tokio = { version = "1", features = ["full"] } serde = { version = "1.0", features = ["derive"] } -reqwest = { version = "0.12", features = ["json", "rustls-tls", "blocking"] } +reqwest = { version = "0.12", features = ["json", "rustls-tls"] } sysproxy = { git="https://github.com/zzzgydi/sysproxy-rs", branch = "main" } tauri = { git="https://github.com/tauri-apps/tauri",branch = "1.x", features = [ "fs-read-file", "fs-exists", "path-all", "protocol-asset", "dialog-open", "notification-all", "icon-png", "icon-ico", "clipboard-all", "global-shortcut-all", "process-all", "shell-all", "system-tray", "updater", "window-all", "devtools"] } [target.'cfg(windows)'.dependencies] diff --git a/src-tauri/src/config/config.rs b/src-tauri/src/config/config.rs index 22436be..8664596 100644 --- a/src-tauri/src/config/config.rs +++ b/src-tauri/src/config/config.rs @@ -46,8 +46,8 @@ impl Config { } /// 初始化订阅 - pub fn init_config() -> Result<()> { - crate::log_err!(Self::generate()); + pub async fn init_config() -> Result<()> { + crate::log_err!(Self::generate().await); if let Err(err) = Self::generate_file(ConfigType::Run) { log::error!(target: "app", "{err}"); @@ -83,8 +83,8 @@ impl Config { } /// 生成订阅存好 - pub fn generate() -> Result<()> { - let (config, exists_keys, logs) = enhance::enhance(); + pub async fn generate() -> Result<()> { + let (config, exists_keys, logs) = enhance::enhance().await; *Config::runtime().draft() = IRuntime { config: Some(config), diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index 51e665d..1fa7612 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -219,22 +219,18 @@ impl CoreManager { } /// 停止核心运行 - pub fn stop_core(&self) -> Result<()> { + pub async fn stop_core(&self) -> Result<()> { // 关闭tun模式 - tauri::async_runtime::block_on(async move { - let mut disable = Mapping::new(); - let mut tun = Mapping::new(); - tun.insert("enable".into(), false.into()); - disable.insert("tun".into(), tun.into()); - log::debug!(target: "app", "disable tun mode"); - let _ = clash_api::patch_configs(&disable).await; - }); + let mut disable = Mapping::new(); + let mut tun = Mapping::new(); + tun.insert("enable".into(), false.into()); + disable.insert("tun".into(), tun.into()); + log::debug!(target: "app", "disable tun mode"); + let _ = clash_api::patch_configs(&disable).await; if *self.use_service_mode.lock() { log::debug!(target: "app", "stop the core by service"); - tauri::async_runtime::block_on(async move { - log_err!(service::stop_core_by_service().await); - }); + log_err!(service::stop_core_by_service().await); return Ok(()); } @@ -265,7 +261,7 @@ impl CoreManager { Config::verge().draft().clash_core = Some(clash_core); // 更新订阅 - Config::generate()?; + Config::generate().await?; self.check_config()?; @@ -293,7 +289,7 @@ impl CoreManager { log::debug!(target: "app", "try to update clash config"); // 更新订阅 - Config::generate()?; + Config::generate().await?; // 检查订阅是否正常 self.check_config()?; diff --git a/src-tauri/src/core/service.rs b/src-tauri/src/core/service.rs index cf00895..4f9db5f 100644 --- a/src-tauri/src/core/service.rs +++ b/src-tauri/src/core/service.rs @@ -314,14 +314,16 @@ pub(super) async fn stop_core_by_service() -> Result<()> { } /// set dns by service -pub fn set_dns_by_service() -> Result<()> { +pub async fn set_dns_by_service() -> Result<()> { let url = format!("{SERVICE_URL}/set_dns"); - let res = reqwest::blocking::ClientBuilder::new() + let res = reqwest::ClientBuilder::new() .no_proxy() .build()? .post(url) - .send()? + .send() + .await? .json::() + .await .context("failed to connect to the Clash Verge Service")?; if res.code != 0 { @@ -332,14 +334,16 @@ pub fn set_dns_by_service() -> Result<()> { } /// unset dns by service -pub fn unset_dns_by_service() -> Result<()> { +pub async fn unset_dns_by_service() -> Result<()> { let url = format!("{SERVICE_URL}/unset_dns"); - let res = reqwest::blocking::ClientBuilder::new() + let res = reqwest::ClientBuilder::new() .no_proxy() .build()? .post(url) - .send()? + .send() + .await? .json::() + .await .context("failed to connect to the Clash Verge Service")?; if res.code != 0 { diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index f93e841..5a6b534 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -18,7 +18,7 @@ type ResultLog = Vec<(String, String)>; /// Enhance mode /// 返回最终订阅、该订阅包含的键、和script执行的结果 -pub fn enhance() -> (Mapping, Vec, HashMap) { +pub async fn enhance() -> (Mapping, Vec, HashMap) { // config.yaml 的订阅 let clash_config = { Config::clash().latest().0.clone() }; @@ -149,7 +149,7 @@ pub fn enhance() -> (Mapping, Vec, HashMap) { }); } - config = use_tun(config, enable_tun); + config = use_tun(config, enable_tun).await; config = use_sort(config); let mut exists_set = HashSet::new(); diff --git a/src-tauri/src/enhance/tun.rs b/src-tauri/src/enhance/tun.rs index 17e74a2..2e9de25 100644 --- a/src-tauri/src/enhance/tun.rs +++ b/src-tauri/src/enhance/tun.rs @@ -1,4 +1,4 @@ -use crate::core::service; +use crate::{core::service, log_err}; use serde_yaml::{Mapping, Value}; macro_rules! revise { @@ -18,7 +18,7 @@ macro_rules! append { }; } -pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping { +pub async fn use_tun(mut config: Mapping, enable: bool) -> Mapping { let tun_key = Value::from("tun"); let tun_val = config.get(&tun_key); @@ -35,10 +35,10 @@ pub fn use_tun(mut config: Mapping, enable: bool) -> Mapping { revise!(config, "tun", tun_val); if enable { - let _ = service::set_dns_by_service(); + log_err!(service::set_dns_by_service().await); use_dns_for_tun(config) } else { - let _ = service::unset_dns_by_service(); + log_err!(service::unset_dns_by_service().await); config } } diff --git a/src-tauri/src/feat.rs b/src-tauri/src/feat.rs index 8ac9f09..93c388f 100644 --- a/src-tauri/src/feat.rs +++ b/src-tauri/src/feat.rs @@ -139,7 +139,7 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> { || patch.get("secret").is_some() || patch.get("external-controller").is_some() { - Config::generate()?; + Config::generate().await?; CoreManager::global().run_core().await?; handle::Handle::refresh_clash(); } @@ -200,23 +200,23 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> { if service_mode.is_some() { log::debug!(target: "app", "change service mode to {}", service_mode.unwrap()); - Config::generate()?; + Config::generate().await?; CoreManager::global().run_core().await?; } else if tun_mode.is_some() { update_core_config().await?; } #[cfg(not(target_os = "windows"))] if redir_enabled.is_some() { - Config::generate()?; + Config::generate().await?; CoreManager::global().run_core().await?; } #[cfg(target_os = "linux")] if tproxy_enabled.is_some() { - Config::generate()?; + Config::generate().await?; CoreManager::global().run_core().await?; } if socks_enabled.is_some() || http_enabled.is_some() { - Config::generate()?; + Config::generate().await?; CoreManager::global().run_core().await?; } if auto_launch.is_some() { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 032f176..dbdd162 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -15,8 +15,15 @@ use tauri::{api, SystemTray}; fn main() -> std::io::Result<()> { // 单例检测 - if server::check_singleton().is_err() { - println!("app exists"); + let app_exists: bool = tauri::async_runtime::block_on(async move { + if server::check_singleton().await.is_err() { + println!("app exists"); + true + } else { + false + } + }); + if app_exists { return Ok(()); } @@ -29,7 +36,9 @@ fn main() -> std::io::Result<()> { let mut builder = tauri::Builder::default() .system_tray(SystemTray::new()) .setup(|app| { - resolve::resolve_setup(app); + tauri::async_runtime::block_on(async move { + resolve::resolve_setup(app).await; + }); Ok(()) }) .on_system_tray_event(core::tray::Tray::on_system_tray_event) diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index 82a39a7..01afcf0 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -34,7 +34,7 @@ pub fn find_unused_port() -> Result { } /// handle something when start app -pub fn resolve_setup(app: &mut App) { +pub async fn resolve_setup(app: &mut App) { #[cfg(target_os = "macos")] app.set_activation_policy(tauri::ActivationPolicy::Accessory); let version = app.package_info().version.to_string(); @@ -73,7 +73,8 @@ pub fn resolve_setup(app: &mut App) { // 启动核心 log::trace!("init config"); - log_err!(Config::init_config()); + + log_err!(Config::init_config().await); log::trace!("launch core"); log_err!(CoreManager::global().init()); @@ -101,9 +102,7 @@ pub fn resolve_setup(app: &mut App) { if argvs.len() > 1 { let param = argvs[1].as_str(); if param.starts_with("clash:") { - tauri::async_runtime::block_on(async { - resolve_scheme(argvs[1].to_owned()).await; - }); + resolve_scheme(argvs[1].to_owned()).await; } } } @@ -111,8 +110,10 @@ pub fn resolve_setup(app: &mut App) { /// reset system proxy pub fn resolve_reset() { log_err!(sysopt::Sysopt::global().reset_sysproxy()); - log_err!(CoreManager::global().stop_core()); - let _ = service::unset_dns_by_service(); + tauri::async_runtime::block_on(async move { + log_err!(CoreManager::global().stop_core().await); + log_err!(service::unset_dns_by_service().await); + }); } /// create main window diff --git a/src-tauri/src/utils/server.rs b/src-tauri/src/utils/server.rs index daf9fb8..4a02f10 100644 --- a/src-tauri/src/utils/server.rs +++ b/src-tauri/src/utils/server.rs @@ -14,40 +14,38 @@ struct QueryParam { } /// check whether there is already exists -pub fn check_singleton() -> Result<()> { +pub async fn check_singleton() -> Result<()> { let port = IVerge::get_singleton_port(); if !local_port_available(port) { - tauri::async_runtime::block_on(async { - let resp = reqwest::get(format!("http://127.0.0.1:{port}/commands/ping")) - .await? - .text() - .await?; + let resp = reqwest::get(format!("http://127.0.0.1:{port}/commands/ping")) + .await? + .text() + .await?; - if &resp == "ok" { - let argvs: Vec = std::env::args().collect(); - if argvs.len() > 1 { - let param = argvs[1].as_str(); - if param.starts_with("clash:") { - reqwest::get(format!( - "http://127.0.0.1:{port}/commands/scheme?param={param}" - )) - .await? - .text() - .await?; - } - } else { - reqwest::get(format!("http://127.0.0.1:{port}/commands/visible")) - .await? - .text() - .await?; + if &resp == "ok" { + let argvs: Vec = std::env::args().collect(); + if argvs.len() > 1 { + let param = argvs[1].as_str(); + if param.starts_with("clash:") { + reqwest::get(format!( + "http://127.0.0.1:{port}/commands/scheme?param={param}" + )) + .await? + .text() + .await?; } - bail!("app exists"); + } else { + reqwest::get(format!("http://127.0.0.1:{port}/commands/visible")) + .await? + .text() + .await?; } + bail!("app exists"); + } - log::error!("failed to setup singleton listen server"); - Ok(()) - }) + log::error!("failed to setup singleton listen server"); + Ok(()) } else { Ok(()) }