diff --git a/src-tauri/src/cmd/clash.rs b/src-tauri/src/cmd/clash.rs index b6dbc1fa..1bcadea9 100644 --- a/src-tauri/src/cmd/clash.rs +++ b/src-tauri/src/cmd/clash.rs @@ -1,7 +1,9 @@ use super::CmdResult; +use crate::utils::dirs; use crate::{ cmd::StringifyErr, config::Config, + constants, core::{CoreManager, handle, validate::CoreConfigValidator}, }; use crate::{config::*, feat, logging, utils::logging::Type}; @@ -126,7 +128,7 @@ pub async fn save_dns_config(dns_config: Mapping) -> CmdResult { // 获取DNS配置文件路径 let dns_path = dirs::app_home_dir() .stringify_err()? - .join("dns_config.yaml"); + .join(constants::files::DNS_CONFIG); // 保存DNS配置到文件 let yaml_str = serde_yaml_ng::to_string(&dns_config).stringify_err()?; @@ -149,7 +151,7 @@ pub async fn apply_dns_config(apply: bool) -> CmdResult { // 读取DNS配置文件 let dns_path = dirs::app_home_dir() .stringify_err()? - .join("dns_config.yaml"); + .join(constants::files::DNS_CONFIG); if !dns_path.exists() { logging!(warn, Type::Config, "DNS config file not found"); @@ -227,7 +229,7 @@ pub fn check_dns_config_exists() -> CmdResult { let dns_path = dirs::app_home_dir() .stringify_err()? - .join("dns_config.yaml"); + .join(constants::files::DNS_CONFIG); Ok(dns_path.exists()) } @@ -240,7 +242,7 @@ pub async fn get_dns_config_content() -> CmdResult { let dns_path = dirs::app_home_dir() .stringify_err()? - .join("dns_config.yaml"); + .join(constants::files::DNS_CONFIG); if !fs::try_exists(&dns_path).await.stringify_err()? { return Err("DNS config file not found".into()); @@ -253,10 +255,8 @@ pub async fn get_dns_config_content() -> CmdResult { /// 验证DNS配置文件 #[tauri::command] pub async fn validate_dns_config() -> CmdResult<(bool, String)> { - use crate::utils::dirs; - let app_dir = dirs::app_home_dir().stringify_err()?; - let dns_path = app_dir.join("dns_config.yaml"); + let dns_path = app_dir.join(constants::files::DNS_CONFIG); let dns_path_str = dns_path.to_str().unwrap_or_default(); if !dns_path.exists() { diff --git a/src-tauri/src/config/clash.rs b/src-tauri/src/config/clash.rs index 9ca6276a..8b1c072a 100644 --- a/src-tauri/src/config/clash.rs +++ b/src-tauri/src/config/clash.rs @@ -1,4 +1,5 @@ use crate::config::Config; +use crate::constants::{network, tun as tun_const}; use crate::utils::dirs::{ipc_path, path_to_str}; use crate::utils::{dirs, help}; use crate::{logging, utils::logging::Type}; @@ -48,8 +49,6 @@ impl IClashTemp { } pub fn template() -> Self { - use crate::constants::{network, tun as tun_const}; - let mut map = Mapping::new(); let mut tun_config = Mapping::new(); let mut cors_map = Mapping::new(); @@ -215,9 +214,9 @@ impl IClashTemp { Value::Number(val_num) => val_num.as_u64().map(|u| u as u16), _ => None, }) - .unwrap_or(7896); + .unwrap_or(network::ports::DEFAULT_TPROXY); if port == 0 { - port = 7896; + port = network::ports::DEFAULT_TPROXY; } port } diff --git a/src-tauri/src/constants.rs b/src-tauri/src/constants.rs index e3ea41fd..3195ea40 100644 --- a/src-tauri/src/constants.rs +++ b/src-tauri/src/constants.rs @@ -5,15 +5,13 @@ pub mod network { pub const DEFAULT_EXTERNAL_CONTROLLER: &str = "127.0.0.1:9097"; pub mod ports { - #[allow(dead_code)] + #[cfg(not(target_os = "windows"))] pub const DEFAULT_REDIR: u16 = 7895; - #[allow(dead_code)] + #[cfg(target_os = "linux")] pub const DEFAULT_TPROXY: u16 = 7896; pub const DEFAULT_MIXED: u16 = 7897; pub const DEFAULT_SOCKS: u16 = 7898; pub const DEFAULT_HTTP: u16 = 7899; - #[allow(dead_code)] - pub const DEFAULT_EXTERNAL_CONTROLLER: u16 = 9097; #[cfg(not(feature = "verge-dev"))] pub const SINGLETON_SERVER: u16 = 33331; @@ -39,11 +37,8 @@ pub mod timing { pub const CONFIG_UPDATE_DEBOUNCE: Duration = Duration::from_millis(500); pub const CONFIG_RELOAD_DELAY: Duration = Duration::from_millis(300); - pub const PROCESS_VERIFY_DELAY: Duration = Duration::from_millis(100); - #[allow(dead_code)] pub const EVENT_EMIT_DELAY: Duration = Duration::from_millis(20); pub const STARTUP_ERROR_DELAY: Duration = Duration::from_secs(2); - #[allow(dead_code)] pub const ERROR_BATCH_DELAY: Duration = Duration::from_millis(300); #[cfg(target_os = "windows")] @@ -53,40 +48,16 @@ pub mod timing { } pub mod retry { - #[allow(dead_code)] pub const EVENT_EMIT_THRESHOLD: u64 = 10; - #[allow(dead_code)] - pub const SWR_ERROR_RETRY: usize = 2; } pub mod files { pub const RUNTIME_CONFIG: &str = "clash-verge.yaml"; pub const CHECK_CONFIG: &str = "clash-verge-check.yaml"; - #[allow(dead_code)] pub const DNS_CONFIG: &str = "dns_config.yaml"; - #[allow(dead_code)] pub const WINDOW_STATE: &str = "window_state.json"; } -pub mod process { - pub const VERGE_MIHOMO: &str = "verge-mihomo"; - pub const VERGE_MIHOMO_ALPHA: &str = "verge-mihomo-alpha"; - - pub fn process_names() -> [&'static str; 2] { - [VERGE_MIHOMO, VERGE_MIHOMO_ALPHA] - } - - #[cfg(windows)] - pub fn with_extension(name: &str) -> String { - format!("{}.exe", name) - } - - #[cfg(not(windows))] - pub fn with_extension(name: &str) -> String { - name.to_string() - } -} - pub mod error_patterns { pub const CONNECTION_ERRORS: &[&str] = &[ "Failed to create connection", diff --git a/src-tauri/src/core/backup.rs b/src-tauri/src/core/backup.rs index 167fbdd1..cf90bdd1 100644 --- a/src-tauri/src/core/backup.rs +++ b/src-tauri/src/core/backup.rs @@ -1,3 +1,4 @@ +use crate::constants::files::DNS_CONFIG; use crate::{ config::Config, logging, @@ -306,9 +307,9 @@ pub async fn create_backup() -> Result<(String, PathBuf), Error> { zip.start_file(dirs::VERGE_CONFIG, options)?; zip.write_all(serde_yaml_ng::to_string(&verge_config)?.as_bytes())?; - let dns_config_path = dirs::app_home_dir()?.join(dirs::DNS_CONFIG); + let dns_config_path = dirs::app_home_dir()?.join(DNS_CONFIG); if dns_config_path.exists() { - zip.start_file(dirs::DNS_CONFIG, options)?; + zip.start_file(DNS_CONFIG, options)?; zip.write_all(fs::read(&dns_config_path).await?.as_slice())?; } diff --git a/src-tauri/src/enhance/mod.rs b/src-tauri/src/enhance/mod.rs index 1deb49e5..e9183170 100644 --- a/src-tauri/src/enhance/mod.rs +++ b/src-tauri/src/enhance/mod.rs @@ -6,6 +6,7 @@ pub mod seq; mod tun; use self::{chain::*, field::*, merge::*, script::*, seq::*, tun::*}; +use crate::constants; use crate::utils::dirs; use crate::{config::Config, utils::tmpl}; use crate::{logging, utils::logging::Type}; @@ -442,7 +443,7 @@ fn apply_builtin_scripts( async fn apply_dns_settings(mut config: Mapping, enable_dns_settings: bool) -> Mapping { if enable_dns_settings && let Ok(app_dir) = dirs::app_home_dir() { - let dns_path = app_dir.join("dns_config.yaml"); + let dns_path = app_dir.join(constants::files::DNS_CONFIG); if dns_path.exists() && let Ok(dns_yaml) = fs::read_to_string(&dns_path).await diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 84cb533a..c6db41c0 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -10,6 +10,7 @@ mod feat; mod module; mod process; pub mod utils; +use crate::constants::files; #[cfg(target_os = "macos")] use crate::module::lightweight; #[cfg(target_os = "linux")] @@ -21,6 +22,7 @@ use crate::{ process::AsyncHandler, utils::{resolve, server}, }; +use anyhow::Result; use config::Config; use once_cell::sync::OnceCell; use tauri::{AppHandle, Manager}; @@ -32,8 +34,6 @@ use utils::logging::Type; pub static APP_HANDLE: OnceCell = OnceCell::new(); /// Application initialization helper functions mod app_init { - use anyhow::Result; - use super::*; /// Initialize singleton monitoring for other instances @@ -126,7 +126,7 @@ mod app_init { pub fn setup_window_state(app: &tauri::App) -> Result<(), Box> { logging!(info, Type::Setup, "初始化窗口状态管理..."); let window_state_plugin = tauri_plugin_window_state::Builder::new() - .with_filename("window_state.json") + .with_filename(files::WINDOW_STATE) .with_state_flags(tauri_plugin_window_state::StateFlags::default()) .build(); app.handle().plugin(window_state_plugin)?; diff --git a/src-tauri/src/utils/dirs.rs b/src-tauri/src/utils/dirs.rs index b16d3d8e..18ea6fac 100644 --- a/src-tauri/src/utils/dirs.rs +++ b/src-tauri/src/utils/dirs.rs @@ -24,7 +24,6 @@ pub static PORTABLE_FLAG: OnceCell = OnceCell::new(); pub static CLASH_CONFIG: &str = "config.yaml"; pub static VERGE_CONFIG: &str = "verge.yaml"; pub static PROFILE_YAML: &str = "profiles.yaml"; -pub static DNS_CONFIG: &str = "dns_config.yaml"; /// init portable flag pub fn init_portable_flag() -> Result<()> { diff --git a/src-tauri/src/utils/init.rs b/src-tauri/src/utils/init.rs index 72f19b08..61d4288c 100644 --- a/src-tauri/src/utils/init.rs +++ b/src-tauri/src/utils/init.rs @@ -3,6 +3,7 @@ use crate::utils::logging::NoModuleFilter; use crate::{ config::*, + constants, core::handle, logging, process::AsyncHandler, @@ -304,7 +305,7 @@ async fn init_dns_config() -> Result<()> { // 检查DNS配置文件是否存在 let app_dir = dirs::app_home_dir()?; - let dns_path = app_dir.join("dns_config.yaml"); + let dns_path = app_dir.join(constants::files::DNS_CONFIG); if !dns_path.exists() { logging!(info, Type::Setup, "Creating default DNS config file"); diff --git a/src-tauri/src/utils/logging.rs b/src-tauri/src/utils/logging.rs index a73b18eb..9f73c634 100644 --- a/src-tauri/src/utils/logging.rs +++ b/src-tauri/src/utils/logging.rs @@ -80,15 +80,6 @@ macro_rules! log_err { }; } -#[macro_export] -macro_rules! trace_err { - ($result: expr, $err_str: expr) => { - if let Err(err) = $result { - log::trace!(target: "app", "{}, err {}", $err_str, err); - } - } -} - /// wrap the anyhow error /// transform the error to String #[macro_export]