2022-11-18 18:18:41 +08:00
|
|
|
|
use crate::utils::{dirs, help};
|
2022-11-14 01:26:33 +08:00
|
|
|
|
use anyhow::Result;
|
2023-07-22 08:53:37 +08:00
|
|
|
|
use log::LevelFilter;
|
2022-11-14 01:26:33 +08:00
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
|
|
|
|
/// ### `verge.yaml` schema
|
|
|
|
|
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
|
|
|
|
|
pub struct IVerge {
|
2022-11-16 01:26:41 +08:00
|
|
|
|
/// app listening port for app singleton
|
2022-11-14 01:26:33 +08:00
|
|
|
|
pub app_singleton_port: Option<u16>,
|
|
|
|
|
|
|
2023-07-22 08:53:37 +08:00
|
|
|
|
/// app log level
|
2023-07-22 09:25:54 +08:00
|
|
|
|
/// silent | error | warn | info | debug | trace
|
2023-07-22 08:53:37 +08:00
|
|
|
|
pub app_log_level: Option<String>,
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
// i18n
|
|
|
|
|
|
pub language: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
/// `light` or `dark` or `system`
|
|
|
|
|
|
pub theme_mode: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
/// enable blur mode
|
|
|
|
|
|
/// maybe be able to set the alpha
|
|
|
|
|
|
pub theme_blur: Option<bool>,
|
|
|
|
|
|
|
2023-11-28 07:49:44 +08:00
|
|
|
|
/// tray click event
|
|
|
|
|
|
pub tray_event: Option<String>,
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
/// enable traffic graph default is true
|
|
|
|
|
|
pub traffic_graph: Option<bool>,
|
|
|
|
|
|
|
2023-08-05 19:40:23 +08:00
|
|
|
|
/// show memory info (only for Clash Meta)
|
|
|
|
|
|
pub enable_memory_usage: Option<bool>,
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
/// clash tun mode
|
|
|
|
|
|
pub enable_tun_mode: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// windows service mode
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub enable_service_mode: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// can the app auto startup
|
|
|
|
|
|
pub enable_auto_launch: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// not show the window on launch
|
|
|
|
|
|
pub enable_silent_start: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// set system proxy
|
|
|
|
|
|
pub enable_system_proxy: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// enable proxy guard
|
|
|
|
|
|
pub enable_proxy_guard: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// set system proxy bypass
|
|
|
|
|
|
pub system_proxy_bypass: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
/// proxy guard duration
|
|
|
|
|
|
pub proxy_guard_duration: Option<u64>,
|
|
|
|
|
|
|
|
|
|
|
|
/// theme setting
|
|
|
|
|
|
pub theme_setting: Option<IVergeTheme>,
|
|
|
|
|
|
|
|
|
|
|
|
/// web ui list
|
|
|
|
|
|
pub web_ui_list: Option<Vec<String>>,
|
|
|
|
|
|
|
|
|
|
|
|
/// clash core path
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub clash_core: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
/// hotkey map
|
|
|
|
|
|
/// format: {func},{key}
|
|
|
|
|
|
pub hotkeys: Option<Vec<String>>,
|
|
|
|
|
|
|
|
|
|
|
|
/// 切换代理时自动关闭连接
|
|
|
|
|
|
pub auto_close_connection: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// 默认的延迟测试连接
|
|
|
|
|
|
pub default_latency_test: Option<String>,
|
2022-11-21 21:05:00 +08:00
|
|
|
|
|
2023-12-05 08:34:57 +08:00
|
|
|
|
/// 支持关闭字段过滤,避免meta的新字段都被过滤掉,默认为关闭
|
2023-02-16 23:52:55 +08:00
|
|
|
|
pub enable_clash_fields: Option<bool>,
|
|
|
|
|
|
|
2022-11-21 21:05:00 +08:00
|
|
|
|
/// 是否使用内部的脚本支持,默认为真
|
|
|
|
|
|
pub enable_builtin_enhanced: Option<bool>,
|
2022-12-13 17:34:39 +08:00
|
|
|
|
|
|
|
|
|
|
/// proxy 页面布局 列数
|
|
|
|
|
|
pub proxy_layout_column: Option<i32>,
|
2023-07-22 10:58:16 +08:00
|
|
|
|
|
2023-11-02 20:12:46 +08:00
|
|
|
|
/// 日志清理
|
|
|
|
|
|
/// 0: 不清理; 1: 7天; 2: 30天; 3: 90天
|
|
|
|
|
|
pub auto_log_clean: Option<i32>,
|
|
|
|
|
|
|
2023-07-22 10:58:16 +08:00
|
|
|
|
/// window size and position
|
|
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
|
|
pub window_size_position: Option<Vec<f64>>,
|
2023-12-01 12:56:18 +08:00
|
|
|
|
|
|
|
|
|
|
/// 是否启用随机端口
|
|
|
|
|
|
pub enable_random_port: Option<bool>,
|
|
|
|
|
|
|
|
|
|
|
|
/// verge mixed port 用于覆盖 clash 的 mixed port
|
|
|
|
|
|
pub verge_mixed_port: Option<u16>,
|
2022-11-14 01:26:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
|
|
|
|
|
|
pub struct IVergeTheme {
|
|
|
|
|
|
pub primary_color: Option<String>,
|
|
|
|
|
|
pub secondary_color: Option<String>,
|
|
|
|
|
|
pub primary_text: Option<String>,
|
|
|
|
|
|
pub secondary_text: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
pub info_color: Option<String>,
|
|
|
|
|
|
pub error_color: Option<String>,
|
|
|
|
|
|
pub warning_color: Option<String>,
|
|
|
|
|
|
pub success_color: Option<String>,
|
|
|
|
|
|
|
|
|
|
|
|
pub font_family: Option<String>,
|
|
|
|
|
|
pub css_injection: Option<String>,
|
|
|
|
|
|
}
|
2022-11-15 01:33:50 +08:00
|
|
|
|
|
|
|
|
|
|
impl IVerge {
|
|
|
|
|
|
pub fn new() -> Self {
|
2022-11-18 18:18:41 +08:00
|
|
|
|
match dirs::verge_path().and_then(|path| help::read_yaml::<IVerge>(&path)) {
|
2022-11-18 09:35:05 +08:00
|
|
|
|
Ok(config) => config,
|
|
|
|
|
|
Err(err) => {
|
|
|
|
|
|
log::error!(target: "app", "{err}");
|
|
|
|
|
|
Self::template()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn template() -> Self {
|
|
|
|
|
|
Self {
|
2022-11-24 10:26:41 +08:00
|
|
|
|
clash_core: match cfg!(feature = "default-meta") {
|
|
|
|
|
|
false => Some("clash".into()),
|
|
|
|
|
|
true => Some("clash-meta".into()),
|
|
|
|
|
|
},
|
|
|
|
|
|
language: match cfg!(feature = "default-meta") {
|
|
|
|
|
|
false => Some("en".into()),
|
|
|
|
|
|
true => Some("zh".into()),
|
|
|
|
|
|
},
|
2022-11-18 09:35:05 +08:00
|
|
|
|
theme_mode: Some("system".into()),
|
|
|
|
|
|
theme_blur: Some(false),
|
|
|
|
|
|
traffic_graph: Some(true),
|
2023-08-05 19:40:23 +08:00
|
|
|
|
enable_memory_usage: Some(true),
|
2022-11-18 09:35:05 +08:00
|
|
|
|
enable_auto_launch: Some(false),
|
|
|
|
|
|
enable_silent_start: Some(false),
|
|
|
|
|
|
enable_system_proxy: Some(false),
|
2023-12-01 12:56:18 +08:00
|
|
|
|
enable_random_port: Some(false),
|
2023-12-07 13:32:49 +08:00
|
|
|
|
verge_mixed_port: Some(7890),
|
2022-11-18 09:35:05 +08:00
|
|
|
|
enable_proxy_guard: Some(false),
|
|
|
|
|
|
proxy_guard_duration: Some(30),
|
|
|
|
|
|
auto_close_connection: Some(true),
|
2022-11-21 21:05:00 +08:00
|
|
|
|
enable_builtin_enhanced: Some(true),
|
2023-12-05 08:34:57 +08:00
|
|
|
|
enable_clash_fields: Some(false),
|
2023-11-02 20:12:46 +08:00
|
|
|
|
auto_log_clean: Some(3),
|
2022-11-18 09:35:05 +08:00
|
|
|
|
..Self::default()
|
|
|
|
|
|
}
|
2022-11-15 01:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Save IVerge App Config
|
|
|
|
|
|
pub fn save_file(&self) -> Result<()> {
|
2022-11-18 18:18:41 +08:00
|
|
|
|
help::save_yaml(&dirs::verge_path()?, &self, Some("# Clash Verge Config"))
|
2022-11-15 01:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-07-22 08:53:37 +08:00
|
|
|
|
patch!(app_log_level);
|
2022-11-15 01:33:50 +08:00
|
|
|
|
patch!(language);
|
|
|
|
|
|
patch!(theme_mode);
|
|
|
|
|
|
patch!(theme_blur);
|
2023-11-28 07:49:44 +08:00
|
|
|
|
patch!(tray_event);
|
2022-11-15 01:33:50 +08:00
|
|
|
|
patch!(traffic_graph);
|
2023-08-05 19:40:23 +08:00
|
|
|
|
patch!(enable_memory_usage);
|
2022-11-15 01:33:50 +08:00
|
|
|
|
|
|
|
|
|
|
patch!(enable_tun_mode);
|
|
|
|
|
|
patch!(enable_service_mode);
|
|
|
|
|
|
patch!(enable_auto_launch);
|
|
|
|
|
|
patch!(enable_silent_start);
|
2023-12-01 12:56:18 +08:00
|
|
|
|
patch!(enable_random_port);
|
|
|
|
|
|
patch!(verge_mixed_port);
|
2022-11-15 01:33:50 +08:00
|
|
|
|
patch!(enable_system_proxy);
|
|
|
|
|
|
patch!(enable_proxy_guard);
|
|
|
|
|
|
patch!(system_proxy_bypass);
|
|
|
|
|
|
patch!(proxy_guard_duration);
|
|
|
|
|
|
|
|
|
|
|
|
patch!(theme_setting);
|
|
|
|
|
|
patch!(web_ui_list);
|
|
|
|
|
|
patch!(clash_core);
|
|
|
|
|
|
patch!(hotkeys);
|
|
|
|
|
|
|
|
|
|
|
|
patch!(auto_close_connection);
|
|
|
|
|
|
patch!(default_latency_test);
|
2022-11-21 21:05:00 +08:00
|
|
|
|
patch!(enable_builtin_enhanced);
|
2022-12-13 17:34:39 +08:00
|
|
|
|
patch!(proxy_layout_column);
|
2023-02-16 23:52:55 +08:00
|
|
|
|
patch!(enable_clash_fields);
|
2023-11-02 20:12:46 +08:00
|
|
|
|
patch!(auto_log_clean);
|
2023-07-22 10:58:16 +08:00
|
|
|
|
patch!(window_size_position);
|
2022-11-15 01:33:50 +08:00
|
|
|
|
}
|
2022-11-17 17:07:13 +08:00
|
|
|
|
|
|
|
|
|
|
/// 在初始化前尝试拿到单例端口的值
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
2022-11-18 18:18:41 +08:00
|
|
|
|
match dirs::verge_path().and_then(|path| help::read_yaml::<IVerge>(&path)) {
|
2022-11-18 09:35:05 +08:00
|
|
|
|
Ok(config) => config.app_singleton_port.unwrap_or(SERVER_PORT),
|
|
|
|
|
|
Err(_) => SERVER_PORT, // 这里就不log错误了
|
|
|
|
|
|
}
|
2022-11-17 17:07:13 +08:00
|
|
|
|
}
|
2023-07-22 08:53:37 +08:00
|
|
|
|
|
|
|
|
|
|
/// 获取日志等级
|
|
|
|
|
|
pub fn get_log_level(&self) -> LevelFilter {
|
|
|
|
|
|
if let Some(level) = self.app_log_level.as_ref() {
|
|
|
|
|
|
match level.to_lowercase().as_str() {
|
2023-07-22 09:25:54 +08:00
|
|
|
|
"silent" => LevelFilter::Off,
|
2023-07-22 08:53:37 +08:00
|
|
|
|
"error" => LevelFilter::Error,
|
2023-07-22 09:25:54 +08:00
|
|
|
|
"warn" => LevelFilter::Warn,
|
|
|
|
|
|
"info" => LevelFilter::Info,
|
|
|
|
|
|
"debug" => LevelFilter::Debug,
|
|
|
|
|
|
"trace" => LevelFilter::Trace,
|
2023-07-22 08:53:37 +08:00
|
|
|
|
_ => LevelFilter::Info,
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
LevelFilter::Info
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-15 01:33:50 +08:00
|
|
|
|
}
|