use crate::config::DEFAULT_PAC; use crate::utils::{dirs, help}; use anyhow::Result; use log::LevelFilter; use serde::{Deserialize, Serialize}; /// ### `verge.yaml` schema #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct IVerge { /// app listening port for app singleton pub app_singleton_port: Option, /// app log level /// silent | error | warn | info | debug | trace pub app_log_level: Option, // i18n pub language: Option, /// `light` or `dark` or `system` pub theme_mode: Option, /// tray click event pub tray_event: Option, /// copy env type pub env_type: Option, /// start page pub start_page: Option, /// startup script path pub startup_script: Option, /// enable traffic graph default is true pub traffic_graph: Option, /// show memory info (only for Clash Meta) pub enable_memory_usage: Option, /// enable group icon pub enable_group_icon: Option, /// common tray icon pub common_tray_icon: Option, /// tray icon #[cfg(target_os = "macos")] pub tray_icon: Option, /// menu icon pub menu_icon: Option, /// sysproxy tray icon pub sysproxy_tray_icon: Option, /// tun tray icon pub tun_tray_icon: Option, /// clash tun mode pub enable_tun_mode: Option, /// windows service mode #[serde(skip_serializing_if = "Option::is_none")] pub enable_service_mode: Option, /// can the app auto startup pub enable_auto_launch: Option, /// not show the window on launch pub enable_silent_start: Option, /// set system proxy pub enable_system_proxy: Option, /// enable proxy guard pub enable_proxy_guard: Option, /// set system proxy bypass pub system_proxy_bypass: Option, /// proxy guard duration pub proxy_guard_duration: Option, /// use pac mode pub proxy_auto_config: Option, /// pac script content pub pac_file_content: Option, /// theme setting pub theme_setting: Option, /// web ui list pub web_ui_list: Option>, /// clash core path #[serde(skip_serializing_if = "Option::is_none")] pub clash_core: Option, /// hotkey map /// format: {func},{key} pub hotkeys: Option>, /// 切换代理时自动关闭连接 pub auto_close_connection: Option, /// 是否自动检查更新 pub auto_check_update: Option, /// 默认的延迟测试连接 pub default_latency_test: Option, /// 默认的延迟测试超时时间 pub default_latency_timeout: Option, /// 是否使用内部的脚本支持,默认为真 pub enable_builtin_enhanced: Option, /// proxy 页面布局 列数 pub proxy_layout_column: Option, /// 测试网站列表 pub test_list: Option>, /// 日志清理 /// 0: 不清理; 1: 7天; 2: 30天; 3: 90天 pub auto_log_clean: Option, /// window size and position #[serde(skip_serializing_if = "Option::is_none")] pub window_size_position: Option>, /// window size and position #[serde(skip_serializing_if = "Option::is_none")] pub window_is_maximized: Option, /// 是否启用随机端口 pub enable_random_port: Option, /// verge 的各种 port 用于覆盖 clash 的各种 port #[cfg(not(target_os = "windows"))] pub verge_redir_port: Option, #[cfg(not(target_os = "windows"))] pub verge_redir_enabled: Option, #[cfg(target_os = "linux")] pub verge_tproxy_port: Option, #[cfg(target_os = "linux")] pub verge_tproxy_enabled: Option, pub verge_mixed_port: Option, pub verge_socks_port: Option, pub verge_socks_enabled: Option, pub verge_port: Option, pub verge_http_enabled: Option, } #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct IVergeTestItem { pub uid: Option, pub name: Option, pub icon: Option, pub url: Option, } #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct IVergeTheme { pub primary_color: Option, pub secondary_color: Option, pub primary_text: Option, pub secondary_text: Option, pub info_color: Option, pub error_color: Option, pub warning_color: Option, pub success_color: Option, pub font_family: Option, pub css_injection: Option, } impl IVerge { pub fn new() -> Self { match dirs::verge_path().and_then(|path| help::read_yaml::(&path)) { Ok(config) => config, Err(err) => { log::error!(target: "app", "{err}"); Self::template() } } } pub fn template() -> Self { Self { clash_core: Some("verge-mihomo".into()), language: Some("zh".into()), theme_mode: Some("system".into()), #[cfg(not(target_os = "windows"))] env_type: Some("bash".into()), #[cfg(target_os = "windows")] env_type: Some("powershell".into()), start_page: Some("/".into()), traffic_graph: Some(true), enable_memory_usage: Some(true), enable_group_icon: Some(true), #[cfg(target_os = "macos")] tray_icon: Some("monochrome".into()), menu_icon: Some("monochrome".into()), common_tray_icon: Some(false), sysproxy_tray_icon: Some(false), tun_tray_icon: Some(false), enable_auto_launch: Some(false), enable_silent_start: Some(false), enable_system_proxy: Some(false), proxy_auto_config: Some(false), pac_file_content: Some(DEFAULT_PAC.into()), enable_random_port: Some(false), #[cfg(not(target_os = "windows"))] verge_redir_port: Some(7895), #[cfg(not(target_os = "windows"))] verge_redir_enabled: Some(true), #[cfg(target_os = "linux")] verge_tproxy_port: Some(7896), #[cfg(target_os = "linux")] verge_tproxy_enabled: Some(true), verge_mixed_port: Some(7897), verge_socks_port: Some(7898), verge_socks_enabled: Some(true), verge_port: Some(7899), verge_http_enabled: Some(true), enable_proxy_guard: Some(false), proxy_guard_duration: Some(30), auto_close_connection: Some(true), auto_check_update: Some(true), enable_builtin_enhanced: Some(true), auto_log_clean: Some(3), ..Self::default() } } /// Save IVerge App Config pub fn save_file(&self) -> Result<()> { help::save_yaml(&dirs::verge_path()?, &self, Some("# Clash Verge Config")) } /// patch verge config /// only save to file pub fn patch_config(&mut self, patch: IVerge) { macro_rules! patch { ($key: tt) => { if patch.$key.is_some() { self.$key = patch.$key; } }; } patch!(app_log_level); patch!(language); patch!(theme_mode); patch!(tray_event); patch!(env_type); patch!(start_page); patch!(startup_script); patch!(traffic_graph); patch!(enable_memory_usage); patch!(enable_group_icon); #[cfg(target_os = "macos")] patch!(tray_icon); patch!(menu_icon); patch!(common_tray_icon); patch!(sysproxy_tray_icon); patch!(tun_tray_icon); patch!(enable_tun_mode); patch!(enable_service_mode); patch!(enable_auto_launch); patch!(enable_silent_start); patch!(enable_random_port); #[cfg(not(target_os = "windows"))] patch!(verge_redir_port); #[cfg(not(target_os = "windows"))] patch!(verge_redir_enabled); #[cfg(target_os = "linux")] patch!(verge_tproxy_port); #[cfg(target_os = "linux")] patch!(verge_tproxy_enabled); patch!(verge_mixed_port); patch!(verge_socks_port); patch!(verge_socks_enabled); patch!(verge_port); patch!(verge_http_enabled); patch!(enable_system_proxy); patch!(enable_proxy_guard); patch!(system_proxy_bypass); patch!(proxy_guard_duration); patch!(proxy_auto_config); patch!(pac_file_content); patch!(theme_setting); patch!(web_ui_list); patch!(clash_core); patch!(hotkeys); patch!(auto_close_connection); patch!(auto_check_update); patch!(default_latency_test); patch!(default_latency_timeout); patch!(enable_builtin_enhanced); patch!(proxy_layout_column); patch!(test_list); patch!(auto_log_clean); patch!(window_size_position); patch!(window_is_maximized); } /// 在初始化前尝试拿到单例端口的值 pub fn get_singleton_port() -> u16 { #[cfg(not(feature = "verge-dev"))] const SERVER_PORT: u16 = 33331; #[cfg(feature = "verge-dev")] const SERVER_PORT: u16 = 11233; match dirs::verge_path().and_then(|path| help::read_yaml::(&path)) { Ok(config) => config.app_singleton_port.unwrap_or(SERVER_PORT), Err(_) => SERVER_PORT, // 这里就不log错误了 } } /// 获取日志等级 pub fn get_log_level(&self) -> LevelFilter { if let Some(level) = self.app_log_level.as_ref() { match level.to_lowercase().as_str() { "silent" => LevelFilter::Off, "error" => LevelFilter::Error, "warn" => LevelFilter::Warn, "info" => LevelFilter::Info, "debug" => LevelFilter::Debug, "trace" => LevelFilter::Trace, _ => LevelFilter::Info, } } else { LevelFilter::Info } } }