From 2b0ba4dc95058ca335ba5acdfdd511c5d2874ca7 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Sat, 15 Nov 2025 02:46:34 +0800 Subject: [PATCH] refactor: streamline config handling and logging mechanisms --- src-tauri/src/cmd/runtime.rs | 14 ++---- src-tauri/src/constants.rs | 12 +---- src-tauri/src/core/manager/config.rs | 67 +++---------------------- src-tauri/src/core/manager/lifecycle.rs | 12 ++--- src-tauri/src/module/lightweight.rs | 5 +- src-tauri/src/utils/logging.rs | 15 ------ 6 files changed, 17 insertions(+), 108 deletions(-) diff --git a/src-tauri/src/cmd/runtime.rs b/src-tauri/src/cmd/runtime.rs index d75e470d0..9ad80f456 100644 --- a/src-tauri/src/cmd/runtime.rs +++ b/src-tauri/src/cmd/runtime.rs @@ -1,9 +1,6 @@ use super::CmdResult; use crate::{ - cmd::StringifyErr as _, - config::{Config, ConfigType}, - core::CoreManager, - log_err, + cmd::StringifyErr as _, config::Config, core::CoreManager, logging_error, utils::logging::Type, }; use anyhow::{Context as _, anyhow}; use serde_yaml_ng::Mapping; @@ -104,14 +101,9 @@ pub async fn update_proxy_chain_config_in_runtime( { let runtime = Config::runtime().await; runtime.edit_draft(|d| d.update_proxy_chain_config(proxy_chain_config)); - runtime.apply(); + // 我们需要在 CoreManager 中验证并应用配置,这里不应该直接调用 runtime.apply() } - - // 生成新的运行配置文件并通知 Clash 核心重新加载 - let run_path = Config::generate_file(ConfigType::Run) - .await - .stringify_err()?; - log_err!(CoreManager::global().put_configs_force(run_path).await); + logging_error!(Type::Core, CoreManager::global().update_config().await); Ok(()) } diff --git a/src-tauri/src/constants.rs b/src-tauri/src/constants.rs index a07da7c15..37f0750ad 100644 --- a/src-tauri/src/constants.rs +++ b/src-tauri/src/constants.rs @@ -35,8 +35,7 @@ pub mod bypass { pub mod timing { use super::Duration; - pub const CONFIG_UPDATE_DEBOUNCE: Duration = Duration::from_millis(500); - pub const CONFIG_RELOAD_DELAY: Duration = Duration::from_millis(300); + pub const CONFIG_UPDATE_DEBOUNCE: Duration = Duration::from_millis(300); pub const EVENT_EMIT_DELAY: Duration = Duration::from_millis(20); pub const STARTUP_ERROR_DELAY: Duration = Duration::from_secs(2); pub const ERROR_BATCH_DELAY: Duration = Duration::from_millis(300); @@ -58,15 +57,6 @@ pub mod files { pub const WINDOW_STATE: &str = "window_state.json"; } -pub mod error_patterns { - pub const CONNECTION_ERRORS: &[&str] = &[ - "Failed to create connection", - "The system cannot find the file specified", - "operation timed out", - "connection refused", - ]; -} - pub mod tun { pub const DEFAULT_STACK: &str = "gvisor"; diff --git a/src-tauri/src/core/manager/config.rs b/src-tauri/src/core/manager/config.rs index 64283a590..c2d4dca27 100644 --- a/src-tauri/src/core/manager/config.rs +++ b/src-tauri/src/core/manager/config.rs @@ -10,7 +10,6 @@ use anyhow::{Result, anyhow}; use smartstring::alias::String; use std::{path::PathBuf, time::Instant}; use tauri_plugin_mihomo::Error as MihomoError; -use tokio::time::sleep; impl CoreManager { pub async fn use_default_config(&self, error_key: &str, error_msg: &str) -> Result<()> { @@ -37,25 +36,25 @@ impl CoreManager { return Ok((true, String::new())); } - if !self.should_update_config()? { + if !self.should_update_config() { return Ok((true, String::new())); } self.perform_config_update().await } - fn should_update_config(&self) -> Result { + fn should_update_config(&self) -> bool { let now = Instant::now(); let last = self.get_last_update(); if let Some(last_time) = last && now.duration_since(*last_time) < timing::CONFIG_UPDATE_DEBOUNCE { - return Ok(false); + return false; } self.set_last_update(now); - Ok(true) + true } async fn perform_config_update(&self) -> Result<(bool, String)> { @@ -78,22 +77,14 @@ impl CoreManager { } } - pub async fn put_configs_force(&self, path: PathBuf) -> Result<()> { - self.apply_config(path).await - } - - pub(super) async fn apply_config(&self, path: PathBuf) -> Result<()> { - let path_str = dirs::path_to_str(&path)?; - - match self.reload_config(path_str).await { + async fn apply_config(&self, path: PathBuf) -> Result<()> { + let path = dirs::path_to_str(&path)?; + match self.reload_config(path).await { Ok(_) => { Config::runtime().await.apply(); logging!(info, Type::Core, "Configuration applied"); Ok(()) } - Err(err) if Self::should_restart_on_error(&err) => { - self.retry_with_restart(path_str).await - } Err(err) => { Config::runtime().await.discard(); Err(anyhow!("Failed to apply config: {}", err)) @@ -101,54 +92,10 @@ impl CoreManager { } } - async fn retry_with_restart(&self, config_path: &str) -> Result<()> { - if handle::Handle::global().is_exiting() { - return Err(anyhow!("Application exiting")); - } - - logging!(warn, Type::Core, "Restarting core for config reload"); - self.restart_core().await?; - sleep(timing::CONFIG_RELOAD_DELAY).await; - - self.reload_config(config_path).await?; - Config::runtime().await.apply(); - logging!(info, Type::Core, "Configuration applied after restart"); - Ok(()) - } - async fn reload_config(&self, path: &str) -> Result<(), MihomoError> { handle::Handle::mihomo() .await .reload_config(true, path) .await } - - fn should_restart_on_error(err: &MihomoError) -> bool { - match err { - MihomoError::ConnectionFailed | MihomoError::ConnectionLost => true, - MihomoError::Io(io_err) => Self::is_connection_io_error(io_err.kind()), - MihomoError::Reqwest(req_err) => { - req_err.is_connect() - || req_err.is_timeout() - || Self::contains_error_pattern(&req_err.to_string()) - } - MihomoError::FailedResponse(msg) => Self::contains_error_pattern(msg), - _ => false, - } - } - - const fn is_connection_io_error(kind: std::io::ErrorKind) -> bool { - matches!( - kind, - std::io::ErrorKind::ConnectionAborted - | std::io::ErrorKind::ConnectionRefused - | std::io::ErrorKind::ConnectionReset - | std::io::ErrorKind::NotFound - ) - } - - fn contains_error_pattern(text: &str) -> bool { - use crate::constants::error_patterns::CONNECTION_ERRORS; - CONNECTION_ERRORS.iter().any(|p| text.contains(p)) - } } diff --git a/src-tauri/src/core/manager/lifecycle.rs b/src-tauri/src/core/manager/lifecycle.rs index 75574068c..fe524360a 100644 --- a/src-tauri/src/core/manager/lifecycle.rs +++ b/src-tauri/src/core/manager/lifecycle.rs @@ -1,5 +1,6 @@ use super::{CoreManager, RunningMode}; -use crate::config::{Config, ConfigType, IVerge}; +use crate::cmd::StringifyErr as _; +use crate::config::{Config, IVerge}; use crate::{ core::{ logger::CLASH_LOGGER, @@ -55,13 +56,8 @@ impl CoreManager { let verge_data = Config::verge().await.latest_arc(); verge_data.save_file().await.map_err(|e| e.to_string())?; - let run_path = Config::generate_file(ConfigType::Run) - .await - .map_err(|e| e.to_string())?; - - self.apply_config(run_path) - .await - .map_err(|e| e.to_string().into()) + self.update_config().await.stringify_err()?; + Ok(()) } async fn prepare_startup(&self) -> Result<()> { diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index 25978b163..20e26b144 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -1,12 +1,11 @@ use crate::{ config::Config, core::{handle, timer::Timer, tray::Tray}, - log_err, logging, + logging, process::AsyncHandler, utils::logging::Type, }; -#[cfg(target_os = "macos")] use crate::logging_error; use crate::utils::window_manager::WindowManager; @@ -184,7 +183,7 @@ fn cancel_window_close_listener() { fn setup_webview_focus_listener() { if let Some(window) = handle::Handle::get_window() { let handler_id = window.listen("tauri://focus", move |_event| { - log_err!(cancel_light_weight_timer()); + logging_error!(Type::Lightweight, cancel_light_weight_timer()); logging!( debug, Type::Lightweight, diff --git a/src-tauri/src/utils/logging.rs b/src-tauri/src/utils/logging.rs index 7b5de419b..f78c9a55a 100644 --- a/src-tauri/src/utils/logging.rs +++ b/src-tauri/src/utils/logging.rs @@ -65,21 +65,6 @@ macro_rules! error { }; } -#[macro_export] -macro_rules! log_err { - ($result: expr) => { - if let Err(err) = $result { - log::error!(target: "app", "{err}"); - } - }; - - ($result: expr, $err_str: expr) => { - if let Err(_) = $result { - log::error!(target: "app", "{}", $err_str); - } - }; -} - /// wrap the anyhow error /// transform the error to String #[macro_export]