refactor: logic optimization

This commit is contained in:
huzibaca 2024-09-28 12:37:01 +08:00
parent c10d782524
commit dc5cb2e1b8
2 changed files with 53 additions and 39 deletions

View File

@ -88,13 +88,9 @@ impl CoreManager {
log_err!(clash_api::patch_configs(&disable).await); log_err!(clash_api::patch_configs(&disable).await);
// 服务模式 // 服务模式
let service_enable = { Config::verge().latest().enable_service_mode }; log::debug!(target: "app", "stop the core by service");
let service_enable = service_enable.unwrap_or(false); log_err!(service::stop_core_by_service().await);
if service_enable {
log::debug!(target: "app", "stop the core by service");
log_err!(service::stop_core_by_service().await);
return Ok(());
}
kill_processes_by_name(clash_core.as_str()); kill_processes_by_name(clash_core.as_str());
*running = false; *running = false;

View File

@ -170,46 +170,42 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
let http_enabled = patch.verge_http_enabled; let http_enabled = patch.verge_http_enabled;
let http_port = patch.verge_port; let http_port = patch.verge_port;
let res = { let res: std::result::Result<(), anyhow::Error> = {
let mut should_restart_core = false;
let mut should_update_clash_config = false;
let mut should_update_launch = false;
let mut should_update_sysproxy = false;
let mut should_update_guard_proxy = false;
let mut should_update_systray_part = false;
let service_mode = patch.enable_service_mode; let service_mode = patch.enable_service_mode;
let mut generated = false;
if service_mode.is_some() { if service_mode.is_some() {
log::debug!(target: "app", "change service mode to {}", service_mode.unwrap()); should_restart_core = true;
if !generated {
Config::generate().await?;
CoreManager::global().restart_core().await?;
generated = true;
}
} else if tun_mode.is_some() {
update_core_config(false).await?;
} }
if tun_mode.is_some() {
should_update_clash_config = true;
}
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
if (redir_enabled.is_some() || redir_port.is_some()) && !generated { if redir_enabled.is_some() || redir_port.is_some() {
Config::generate().await?; should_restart_core = true;
CoreManager::global().restart_core().await?;
generated = true;
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
if tproxy_enabled.is_some() || tproxy_port.is_some() { if tproxy_enabled.is_some() || tproxy_port.is_some() {
if !generated { should_restart_core = true;
Config::generate().await?;
CoreManager::global().restart_core().await?;
generated = true;
}
} }
if (socks_enabled.is_some() if socks_enabled.is_some()
|| http_enabled.is_some() || http_enabled.is_some()
|| socks_port.is_some() || socks_port.is_some()
|| http_port.is_some() || http_port.is_some()
|| mixed_port.is_some()) || mixed_port.is_some()
&& !generated
{ {
Config::generate().await?; should_restart_core = true;
CoreManager::global().restart_core().await?;
} }
if auto_launch.is_some() { if auto_launch.is_some() {
sysopt::Sysopt::global().update_launch()?; should_update_launch = true;
} }
if system_proxy.is_some() if system_proxy.is_some()
|| proxy_bypass.is_some() || proxy_bypass.is_some()
@ -217,16 +213,12 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
|| pac.is_some() || pac.is_some()
|| pac_content.is_some() || pac_content.is_some()
{ {
sysopt::Sysopt::global().update_sysproxy()?; should_update_sysproxy = true;
sysopt::Sysopt::global().guard_proxy(); should_update_guard_proxy = true;
} }
if let Some(true) = patch.enable_proxy_guard { if let Some(true) = patch.enable_proxy_guard {
sysopt::Sysopt::global().guard_proxy(); should_update_guard_proxy = true;
}
if let Some(hotkeys) = patch.hotkeys {
hotkey::Hotkey::global().update(hotkeys)?;
} }
if language.is_some() if language.is_some()
@ -237,6 +229,31 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
|| tun_tray_icon.is_some() || tun_tray_icon.is_some()
|| tray_icon.is_some() || tray_icon.is_some()
{ {
should_update_systray_part = true;
}
if should_restart_core {
Config::generate().await?;
CoreManager::global().restart_core().await?;
}
if should_update_clash_config {
update_core_config(false).await?;
}
if should_update_launch {
sysopt::Sysopt::global().update_launch()?;
}
if should_update_sysproxy {
sysopt::Sysopt::global().update_sysproxy()?;
}
if should_update_guard_proxy {
sysopt::Sysopt::global().guard_proxy();
}
if let Some(hotkeys) = patch.hotkeys {
hotkey::Hotkey::global().update(hotkeys)?;
}
if should_update_systray_part {
handle::Handle::update_systray_part()?; handle::Handle::update_systray_part()?;
} }
@ -246,6 +263,7 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> {
Ok(()) => { Ok(()) => {
Config::verge().apply(); Config::verge().apply();
Config::verge().data().save_file()?; Config::verge().data().save_file()?;
return Ok(()); return Ok(());
} }
Err(err) => { Err(err) => {