verge.rs 4.5 KB

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