From 5f7a1fa5cd22497d5c769c74bc6495f331394e08 Mon Sep 17 00:00:00 2001 From: GyDi Date: Wed, 20 Apr 2022 11:17:54 +0800 Subject: [PATCH] refactor: verge --- src-tauri/src/cmds.rs | 8 ++--- src-tauri/src/core/mod.rs | 14 ++++----- src-tauri/src/core/sysopt.rs | 12 ++++---- src-tauri/src/core/verge.rs | 58 +++++++++++------------------------- src-tauri/src/main.rs | 14 ++++----- 5 files changed, 42 insertions(+), 64 deletions(-) diff --git a/src-tauri/src/cmds.rs b/src-tauri/src/cmds.rs index 0b6604d..2bef9e9 100644 --- a/src-tauri/src/cmds.rs +++ b/src-tauri/src/cmds.rs @@ -1,5 +1,5 @@ use crate::{ - core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, VergeConfig}, + core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, Verge}, utils::{dirs, help, sysopt::SysProxyConfig}, }; use crate::{log_if_err, ret_err, wrap_err}; @@ -211,9 +211,9 @@ pub fn patch_clash_config(payload: Mapping, core: State<'_, Core>) -> CmdResult /// get the verge config #[tauri::command] -pub fn get_verge_config(core: State<'_, Core>) -> CmdResult { +pub fn get_verge_config(core: State<'_, Core>) -> CmdResult { let verge = core.verge.lock(); - let config = verge.config.clone(); + let config = verge.clone(); Ok(config) } @@ -222,7 +222,7 @@ pub fn get_verge_config(core: State<'_, Core>) -> CmdResult { /// this command only save the config and not responsible for other things #[tauri::command] pub fn patch_verge_config( - payload: VergeConfig, + payload: Verge, app_handle: tauri::AppHandle, core: State<'_, Core>, ) -> Result<(), String> { diff --git a/src-tauri/src/core/mod.rs b/src-tauri/src/core/mod.rs index c15179a..3867b2d 100644 --- a/src-tauri/src/core/mod.rs +++ b/src-tauri/src/core/mod.rs @@ -70,8 +70,8 @@ impl Core { let clash = self.clash.lock(); let verge = self.verge.lock(); - let silent_start = verge.config.enable_silent_start.clone(); - let auto_launch = verge.config.enable_auto_launch.clone(); + let silent_start = verge.enable_silent_start.clone(); + let auto_launch = verge.enable_auto_launch.clone(); // silent start if silent_start.unwrap_or(false) { @@ -143,7 +143,7 @@ impl Core { } /// Patch Verge - pub fn patch_verge(&self, patch: VergeConfig, app_handle: &AppHandle) -> Result<()> { + pub fn patch_verge(&self, patch: Verge, app_handle: &AppHandle) -> Result<()> { let tun_mode = patch.enable_tun_mode.clone(); let auto_launch = patch.enable_auto_launch.clone(); let system_proxy = patch.enable_system_proxy.clone(); @@ -195,8 +195,8 @@ impl Core { let verge = self.verge.lock(); let tray = app_handle.tray_handle(); - let system_proxy = verge.config.enable_system_proxy.as_ref(); - let tun_mode = verge.config.enable_tun_mode.as_ref(); + let system_proxy = verge.enable_system_proxy.as_ref(); + let tun_mode = verge.enable_tun_mode.as_ref(); tray .get_item("system_proxy") @@ -235,7 +235,7 @@ impl Core { let config = { let verge = self.verge.lock(); - let tun_mode = verge.config.enable_tun_mode.unwrap_or(false); + let tun_mode = verge.enable_tun_mode.unwrap_or(false); Clash::_tun_mode(config, tun_mode) }; @@ -277,7 +277,7 @@ impl Core { let tun_mode = { let verge = self.verge.lock(); - verge.config.enable_tun_mode.unwrap_or(false) + verge.enable_tun_mode.unwrap_or(false) }; let info = { diff --git a/src-tauri/src/core/sysopt.rs b/src-tauri/src/core/sysopt.rs index 8a6d7a8..83957ba 100644 --- a/src-tauri/src/core/sysopt.rs +++ b/src-tauri/src/core/sysopt.rs @@ -34,14 +34,14 @@ impl Sysopt { /// init the sysproxy pub fn init_sysproxy(&mut self, port: Option, verge: &Verge) { if let Some(port) = port { - let enable = verge.config.enable_system_proxy.clone().unwrap_or(false); + let enable = verge.enable_system_proxy.clone().unwrap_or(false); self.old_sysproxy = match SysProxyConfig::get_sys() { Ok(proxy) => Some(proxy), Err(_) => None, }; - let bypass = verge.config.system_proxy_bypass.clone(); + let bypass = verge.system_proxy_bypass.clone(); let sysproxy = SysProxyConfig::new(enable, port, bypass); if enable { @@ -173,9 +173,9 @@ impl Sysopt { let verge = Verge::new(); - let enable_proxy = verge.config.enable_system_proxy.unwrap_or(false); - let enable_guard = verge.config.enable_proxy_guard.unwrap_or(false); - let guard_duration = verge.config.proxy_guard_duration.unwrap_or(10); + let enable_proxy = verge.enable_system_proxy.unwrap_or(false); + let enable_guard = verge.enable_proxy_guard.unwrap_or(false); + let guard_duration = verge.proxy_guard_duration.unwrap_or(10); // update duration wait_secs = guard_duration; @@ -191,7 +191,7 @@ impl Sysopt { match &clash.info.port { Some(port) => { - let bypass = verge.config.system_proxy_bypass.clone(); + let bypass = verge.system_proxy_bypass.clone(); let sysproxy = SysProxyConfig::new(true, port.clone(), bypass); log_if_err!(sysproxy.set_sys()); diff --git a/src-tauri/src/core/verge.rs b/src-tauri/src/core/verge.rs index f96b40f..1826590 100644 --- a/src-tauri/src/core/verge.rs +++ b/src-tauri/src/core/verge.rs @@ -1,11 +1,10 @@ -use crate::log_if_err; use crate::utils::{config, dirs}; -use anyhow::{bail, Result}; +use anyhow::Result; use serde::{Deserialize, Serialize}; /// ### `verge.yaml` schema #[derive(Default, Debug, Clone, Deserialize, Serialize)] -pub struct VergeConfig { +pub struct Verge { // i18n pub language: Option, @@ -60,9 +59,9 @@ pub struct VergeTheme { pub css_injection: Option, } -impl VergeConfig { +impl Verge { pub fn new() -> Self { - config::read_yaml::(dirs::verge_path()) + config::read_yaml::(dirs::verge_path()) } /// Save Verge App Config @@ -73,75 +72,54 @@ impl VergeConfig { Some("# The Config for Clash Verge App\n\n"), ) } -} - -/// Verge App abilities -#[derive(Debug)] -pub struct Verge { - /// manage the verge config - pub config: VergeConfig, -} - -impl Default for Verge { - fn default() -> Self { - Verge::new() - } -} - -impl Verge { - pub fn new() -> Self { - Verge { - config: VergeConfig::new(), - } - } /// patch verge config /// only save to file - pub fn patch_config(&mut self, patch: VergeConfig) -> Result<()> { + pub fn patch_config(&mut self, patch: Verge) -> Result<()> { // only change it if patch.language.is_some() { - self.config.language = patch.language; + self.language = patch.language; } if patch.theme_mode.is_some() { - self.config.theme_mode = patch.theme_mode; + self.theme_mode = patch.theme_mode; } if patch.theme_blur.is_some() { - self.config.theme_blur = patch.theme_blur; + self.theme_blur = patch.theme_blur; } if patch.theme_setting.is_some() { - self.config.theme_setting = patch.theme_setting; + self.theme_setting = patch.theme_setting; } if patch.traffic_graph.is_some() { - self.config.traffic_graph = patch.traffic_graph; + self.traffic_graph = patch.traffic_graph; } // system setting if patch.enable_silent_start.is_some() { - self.config.enable_silent_start = patch.enable_silent_start; + self.enable_silent_start = patch.enable_silent_start; } if patch.enable_auto_launch.is_some() { - self.config.enable_auto_launch = patch.enable_auto_launch; + self.enable_auto_launch = patch.enable_auto_launch; } // proxy if patch.enable_system_proxy.is_some() { - self.config.enable_system_proxy = patch.enable_system_proxy; + self.enable_system_proxy = patch.enable_system_proxy; } if patch.system_proxy_bypass.is_some() { - self.config.system_proxy_bypass = patch.system_proxy_bypass; + self.system_proxy_bypass = patch.system_proxy_bypass; } if patch.enable_proxy_guard.is_some() { - self.config.enable_proxy_guard = patch.enable_proxy_guard; + self.enable_proxy_guard = patch.enable_proxy_guard; } if patch.proxy_guard_duration.is_some() { - self.config.proxy_guard_duration = patch.proxy_guard_duration; + self.proxy_guard_duration = patch.proxy_guard_duration; } // tun mode if patch.enable_tun_mode.is_some() { - self.config.enable_tun_mode = patch.enable_tun_mode; + self.enable_tun_mode = patch.enable_tun_mode; } - self.config.save_file() + self.save_file() } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 9e0e0a8..906c5f0 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -8,7 +8,7 @@ mod core; mod utils; use crate::{ - core::VergeConfig, + core::Verge, utils::{resolve, server}, }; use tauri::{ @@ -54,12 +54,12 @@ fn main() -> std::io::Result<()> { let new_value = { let verge = core.verge.lock(); - !verge.config.enable_system_proxy.clone().unwrap_or(false) + !verge.enable_system_proxy.clone().unwrap_or(false) }; - let patch = VergeConfig { + let patch = Verge { enable_system_proxy: Some(new_value), - ..VergeConfig::default() + ..Verge::default() }; crate::log_if_err!(core.patch_verge(patch, app_handle)); @@ -69,12 +69,12 @@ fn main() -> std::io::Result<()> { let new_value = { let verge = core.verge.lock(); - !verge.config.enable_tun_mode.clone().unwrap_or(false) + !verge.enable_tun_mode.clone().unwrap_or(false) }; - let patch = VergeConfig { + let patch = Verge { enable_tun_mode: Some(new_value), - ..VergeConfig::default() + ..Verge::default() }; crate::log_if_err!(core.patch_verge(patch, app_handle));