verge.rs 4.2 KB

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