refactor: verge

This commit is contained in:
GyDi 2022-04-20 11:17:54 +08:00 committed by GitHub
parent b8ad328cde
commit 5f7a1fa5cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 64 deletions

View File

@ -1,5 +1,5 @@
use crate::{ use crate::{
core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, VergeConfig}, core::{ClashInfo, Core, PrfItem, PrfOption, Profiles, Verge},
utils::{dirs, help, sysopt::SysProxyConfig}, utils::{dirs, help, sysopt::SysProxyConfig},
}; };
use crate::{log_if_err, ret_err, wrap_err}; 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 /// get the verge config
#[tauri::command] #[tauri::command]
pub fn get_verge_config(core: State<'_, Core>) -> CmdResult<VergeConfig> { pub fn get_verge_config(core: State<'_, Core>) -> CmdResult<Verge> {
let verge = core.verge.lock(); let verge = core.verge.lock();
let config = verge.config.clone(); let config = verge.clone();
Ok(config) Ok(config)
} }
@ -222,7 +222,7 @@ pub fn get_verge_config(core: State<'_, Core>) -> CmdResult<VergeConfig> {
/// this command only save the config and not responsible for other things /// this command only save the config and not responsible for other things
#[tauri::command] #[tauri::command]
pub fn patch_verge_config( pub fn patch_verge_config(
payload: VergeConfig, payload: Verge,
app_handle: tauri::AppHandle, app_handle: tauri::AppHandle,
core: State<'_, Core>, core: State<'_, Core>,
) -> Result<(), String> { ) -> Result<(), String> {

View File

@ -70,8 +70,8 @@ impl Core {
let clash = self.clash.lock(); let clash = self.clash.lock();
let verge = self.verge.lock(); let verge = self.verge.lock();
let silent_start = verge.config.enable_silent_start.clone(); let silent_start = verge.enable_silent_start.clone();
let auto_launch = verge.config.enable_auto_launch.clone(); let auto_launch = verge.enable_auto_launch.clone();
// silent start // silent start
if silent_start.unwrap_or(false) { if silent_start.unwrap_or(false) {
@ -143,7 +143,7 @@ impl Core {
} }
/// Patch Verge /// 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 tun_mode = patch.enable_tun_mode.clone();
let auto_launch = patch.enable_auto_launch.clone(); let auto_launch = patch.enable_auto_launch.clone();
let system_proxy = patch.enable_system_proxy.clone(); let system_proxy = patch.enable_system_proxy.clone();
@ -195,8 +195,8 @@ impl Core {
let verge = self.verge.lock(); let verge = self.verge.lock();
let tray = app_handle.tray_handle(); let tray = app_handle.tray_handle();
let system_proxy = verge.config.enable_system_proxy.as_ref(); let system_proxy = verge.enable_system_proxy.as_ref();
let tun_mode = verge.config.enable_tun_mode.as_ref(); let tun_mode = verge.enable_tun_mode.as_ref();
tray tray
.get_item("system_proxy") .get_item("system_proxy")
@ -235,7 +235,7 @@ impl Core {
let config = { let config = {
let verge = self.verge.lock(); 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) Clash::_tun_mode(config, tun_mode)
}; };
@ -277,7 +277,7 @@ impl Core {
let tun_mode = { let tun_mode = {
let verge = self.verge.lock(); let verge = self.verge.lock();
verge.config.enable_tun_mode.unwrap_or(false) verge.enable_tun_mode.unwrap_or(false)
}; };
let info = { let info = {

View File

@ -34,14 +34,14 @@ impl Sysopt {
/// init the sysproxy /// init the sysproxy
pub fn init_sysproxy(&mut self, port: Option<String>, verge: &Verge) { pub fn init_sysproxy(&mut self, port: Option<String>, verge: &Verge) {
if let Some(port) = port { 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() { self.old_sysproxy = match SysProxyConfig::get_sys() {
Ok(proxy) => Some(proxy), Ok(proxy) => Some(proxy),
Err(_) => None, Err(_) => None,
}; };
let bypass = verge.config.system_proxy_bypass.clone(); let bypass = verge.system_proxy_bypass.clone();
let sysproxy = SysProxyConfig::new(enable, port, bypass); let sysproxy = SysProxyConfig::new(enable, port, bypass);
if enable { if enable {
@ -173,9 +173,9 @@ impl Sysopt {
let verge = Verge::new(); let verge = Verge::new();
let enable_proxy = verge.config.enable_system_proxy.unwrap_or(false); let enable_proxy = verge.enable_system_proxy.unwrap_or(false);
let enable_guard = verge.config.enable_proxy_guard.unwrap_or(false); let enable_guard = verge.enable_proxy_guard.unwrap_or(false);
let guard_duration = verge.config.proxy_guard_duration.unwrap_or(10); let guard_duration = verge.proxy_guard_duration.unwrap_or(10);
// update duration // update duration
wait_secs = guard_duration; wait_secs = guard_duration;
@ -191,7 +191,7 @@ impl Sysopt {
match &clash.info.port { match &clash.info.port {
Some(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); let sysproxy = SysProxyConfig::new(true, port.clone(), bypass);
log_if_err!(sysproxy.set_sys()); log_if_err!(sysproxy.set_sys());

View File

@ -1,11 +1,10 @@
use crate::log_if_err;
use crate::utils::{config, dirs}; use crate::utils::{config, dirs};
use anyhow::{bail, Result}; use anyhow::Result;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
/// ### `verge.yaml` schema /// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)] #[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct VergeConfig { pub struct Verge {
// i18n // i18n
pub language: Option<String>, pub language: Option<String>,
@ -60,9 +59,9 @@ pub struct VergeTheme {
pub css_injection: Option<String>, pub css_injection: Option<String>,
} }
impl VergeConfig { impl Verge {
pub fn new() -> Self { pub fn new() -> Self {
config::read_yaml::<VergeConfig>(dirs::verge_path()) config::read_yaml::<Verge>(dirs::verge_path())
} }
/// Save Verge App Config /// Save Verge App Config
@ -73,75 +72,54 @@ impl VergeConfig {
Some("# The Config for Clash Verge App\n\n"), 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 /// patch verge config
/// only save to file /// 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 // only change it
if patch.language.is_some() { if patch.language.is_some() {
self.config.language = patch.language; self.language = patch.language;
} }
if patch.theme_mode.is_some() { 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() { 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() { 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() { if patch.traffic_graph.is_some() {
self.config.traffic_graph = patch.traffic_graph; self.traffic_graph = patch.traffic_graph;
} }
// system setting // system setting
if patch.enable_silent_start.is_some() { 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() { 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 // proxy
if patch.enable_system_proxy.is_some() { 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() { 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() { 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() { 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 // tun mode
if patch.enable_tun_mode.is_some() { 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()
} }
} }

View File

@ -8,7 +8,7 @@ mod core;
mod utils; mod utils;
use crate::{ use crate::{
core::VergeConfig, core::Verge,
utils::{resolve, server}, utils::{resolve, server},
}; };
use tauri::{ use tauri::{
@ -54,12 +54,12 @@ fn main() -> std::io::Result<()> {
let new_value = { let new_value = {
let verge = core.verge.lock(); 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), enable_system_proxy: Some(new_value),
..VergeConfig::default() ..Verge::default()
}; };
crate::log_if_err!(core.patch_verge(patch, app_handle)); crate::log_if_err!(core.patch_verge(patch, app_handle));
@ -69,12 +69,12 @@ fn main() -> std::io::Result<()> {
let new_value = { let new_value = {
let verge = core.verge.lock(); 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), enable_tun_mode: Some(new_value),
..VergeConfig::default() ..Verge::default()
}; };
crate::log_if_err!(core.patch_verge(patch, app_handle)); crate::log_if_err!(core.patch_verge(patch, app_handle));