verge.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. use crate::utils::{dirs, help};
  2. use anyhow::Result;
  3. use serde::{Deserialize, Serialize};
  4. /// ### `verge.yaml` schema
  5. #[derive(Default, Debug, Clone, Deserialize, Serialize)]
  6. pub struct IVerge {
  7. /// app listening port for app singleton
  8. pub app_singleton_port: Option<u16>,
  9. // i18n
  10. pub language: Option<String>,
  11. /// `light` or `dark` or `system`
  12. pub theme_mode: Option<String>,
  13. /// enable blur mode
  14. /// maybe be able to set the alpha
  15. pub theme_blur: Option<bool>,
  16. /// enable traffic graph default is true
  17. pub traffic_graph: Option<bool>,
  18. /// clash tun mode
  19. pub enable_tun_mode: Option<bool>,
  20. /// windows service mode
  21. #[serde(skip_serializing_if = "Option::is_none")]
  22. pub enable_service_mode: Option<bool>,
  23. /// can the app auto startup
  24. pub enable_auto_launch: Option<bool>,
  25. /// not show the window on launch
  26. pub enable_silent_start: Option<bool>,
  27. /// set system proxy
  28. pub enable_system_proxy: Option<bool>,
  29. /// enable proxy guard
  30. pub enable_proxy_guard: Option<bool>,
  31. /// set system proxy bypass
  32. pub system_proxy_bypass: Option<String>,
  33. /// proxy guard duration
  34. pub proxy_guard_duration: Option<u64>,
  35. /// theme setting
  36. pub theme_setting: Option<IVergeTheme>,
  37. /// web ui list
  38. pub web_ui_list: Option<Vec<String>>,
  39. /// clash core path
  40. #[serde(skip_serializing_if = "Option::is_none")]
  41. pub clash_core: Option<String>,
  42. /// hotkey map
  43. /// format: {func},{key}
  44. pub hotkeys: Option<Vec<String>>,
  45. /// 切换代理时自动关闭连接
  46. pub auto_close_connection: Option<bool>,
  47. /// 默认的延迟测试连接
  48. pub default_latency_test: Option<String>,
  49. /// 是否使用内部的脚本支持,默认为真
  50. pub enable_builtin_enhanced: Option<bool>,
  51. }
  52. #[derive(Default, Debug, Clone, Deserialize, Serialize)]
  53. pub struct IVergeTheme {
  54. pub primary_color: Option<String>,
  55. pub secondary_color: Option<String>,
  56. pub primary_text: Option<String>,
  57. pub secondary_text: Option<String>,
  58. pub info_color: Option<String>,
  59. pub error_color: Option<String>,
  60. pub warning_color: Option<String>,
  61. pub success_color: Option<String>,
  62. pub font_family: Option<String>,
  63. pub css_injection: Option<String>,
  64. }
  65. impl IVerge {
  66. pub fn new() -> Self {
  67. match dirs::verge_path().and_then(|path| help::read_yaml::<IVerge>(&path)) {
  68. Ok(config) => config,
  69. Err(err) => {
  70. log::error!(target: "app", "{err}");
  71. Self::template()
  72. }
  73. }
  74. }
  75. pub fn template() -> Self {
  76. Self {
  77. clash_core: Some("clash".into()),
  78. language: Some("en".into()),
  79. theme_mode: Some("system".into()),
  80. theme_blur: Some(false),
  81. traffic_graph: Some(true),
  82. enable_auto_launch: Some(false),
  83. enable_silent_start: Some(false),
  84. enable_system_proxy: Some(false),
  85. enable_proxy_guard: Some(false),
  86. proxy_guard_duration: Some(30),
  87. auto_close_connection: Some(true),
  88. enable_builtin_enhanced: Some(true),
  89. ..Self::default()
  90. }
  91. }
  92. /// Save IVerge App Config
  93. pub fn save_file(&self) -> Result<()> {
  94. help::save_yaml(&dirs::verge_path()?, &self, Some("# Clash Verge Config"))
  95. }
  96. /// patch verge config
  97. /// only save to file
  98. pub fn patch_config(&mut self, patch: IVerge) {
  99. macro_rules! patch {
  100. ($key: tt) => {
  101. if patch.$key.is_some() {
  102. self.$key = patch.$key;
  103. }
  104. };
  105. }
  106. patch!(language);
  107. patch!(theme_mode);
  108. patch!(theme_blur);
  109. patch!(traffic_graph);
  110. patch!(enable_tun_mode);
  111. patch!(enable_service_mode);
  112. patch!(enable_auto_launch);
  113. patch!(enable_silent_start);
  114. patch!(enable_system_proxy);
  115. patch!(enable_proxy_guard);
  116. patch!(system_proxy_bypass);
  117. patch!(proxy_guard_duration);
  118. patch!(theme_setting);
  119. patch!(web_ui_list);
  120. patch!(clash_core);
  121. patch!(hotkeys);
  122. patch!(auto_close_connection);
  123. patch!(default_latency_test);
  124. patch!(enable_builtin_enhanced);
  125. }
  126. /// 在初始化前尝试拿到单例端口的值
  127. pub fn get_singleton_port() -> u16 {
  128. #[cfg(not(feature = "verge-dev"))]
  129. const SERVER_PORT: u16 = 33331;
  130. #[cfg(feature = "verge-dev")]
  131. const SERVER_PORT: u16 = 11233;
  132. match dirs::verge_path().and_then(|path| help::read_yaml::<IVerge>(&path)) {
  133. Ok(config) => config.app_singleton_port.unwrap_or(SERVER_PORT),
  134. Err(_) => SERVER_PORT, // 这里就不log错误了
  135. }
  136. }
  137. }