From 1550d528bd331e876abcf90084d2bc40c242afec Mon Sep 17 00:00:00 2001 From: GyDi Date: Tue, 22 Nov 2022 22:01:34 +0800 Subject: [PATCH] fix: windows service mode --- src-tauri/src/core/core.rs | 20 ++++++++++++------- src-tauri/src/core/win_service.rs | 4 ++-- src-tauri/src/feat.rs | 2 ++ .../setting/mods/service-viewer.tsx | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/core/core.rs b/src-tauri/src/core/core.rs index 830e51b..de5cbca 100644 --- a/src-tauri/src/core/core.rs +++ b/src-tauri/src/core/core.rs @@ -79,15 +79,22 @@ impl CoreManager { pub async fn run_core(&self) -> Result<()> { let config_path = Config::generate_file(ConfigType::Run)?; - let should_kill = match self.sidecar.lock().take() { + let mut should_kill = match self.sidecar.lock().take() { Some(child) => { - log::debug!(target: "app", "stop the core sidecar"); + log::debug!(target: "app", "stop the core by sidecar"); let _ = child.kill(); true } None => false, }; + #[cfg(target_os = "windows")] + if *self.use_service_mode.lock() { + log::debug!(target: "app", "stop the core by service"); + log_err!(super::win_service::stop_core_by_service().await); + should_kill = true; + } + // 这里得等一会儿 if should_kill { sleep(Duration::from_millis(500)).await; @@ -98,10 +105,8 @@ impl CoreManager { use super::win_service; // 服务模式 - let enable = { - let enable = Config::verge().data().enable_service_mode.clone(); - enable.unwrap_or(false) - }; + let enable = { Config::verge().latest().enable_service_mode.clone() }; + let enable = enable.unwrap_or(false); *self.use_service_mode.lock() = enable; @@ -189,7 +194,7 @@ impl CoreManager { pub fn stop_core(&self) -> Result<()> { #[cfg(target_os = "windows")] if *self.use_service_mode.lock() { - log::debug!(target: "app", "stop core by service"); + log::debug!(target: "app", "stop the core by service"); tauri::async_runtime::block_on(async move { log_err!(super::win_service::stop_core_by_service().await); }); @@ -198,6 +203,7 @@ impl CoreManager { let mut sidecar = self.sidecar.lock(); if let Some(child) = sidecar.take() { + log::debug!(target: "app", "stop the core by sidecar"); let _ = child.kill(); } Ok(()) diff --git a/src-tauri/src/core/win_service.rs b/src-tauri/src/core/win_service.rs index 16cf70c..865895b 100644 --- a/src-tauri/src/core/win_service.rs +++ b/src-tauri/src/core/win_service.rs @@ -44,7 +44,7 @@ pub async fn install_service() -> Result<()> { let level = token.privilege_level()?; let status = match level { - PrivilegeLevel::NotPrivileged => RunasCommand::new(install_path).status()?, + PrivilegeLevel::NotPrivileged => RunasCommand::new(install_path).show(false).status()?, _ => StdCommand::new(install_path) .creation_flags(0x08000000) .status()?, @@ -74,7 +74,7 @@ pub async fn uninstall_service() -> Result<()> { let level = token.privilege_level()?; let status = match level { - PrivilegeLevel::NotPrivileged => RunasCommand::new(uninstall_path).status()?, + PrivilegeLevel::NotPrivileged => RunasCommand::new(uninstall_path).show(false).status()?, _ => StdCommand::new(uninstall_path) .creation_flags(0x08000000) .status()?, diff --git a/src-tauri/src/feat.rs b/src-tauri/src/feat.rs index 4fc7be9..f6a1127 100644 --- a/src-tauri/src/feat.rs +++ b/src-tauri/src/feat.rs @@ -212,6 +212,8 @@ pub async fn patch_verge(patch: IVerge) -> Result<()> { let service_mode = patch.enable_service_mode; if service_mode.is_some() { + log::debug!(target: "app", "change service mode to {}", service_mode.unwrap()); + Config::generate()?; CoreManager::global().run_core().await?; } else if tun_mode.is_some() { diff --git a/src/components/setting/mods/service-viewer.tsx b/src/components/setting/mods/service-viewer.tsx index 2f9c3ca..2bfbeb0 100644 --- a/src/components/setting/mods/service-viewer.tsx +++ b/src/components/setting/mods/service-viewer.tsx @@ -48,7 +48,7 @@ export const ServiceViewer = forwardRef((props, ref) => { const onUninstall = useLockFn(async () => { try { - if (state === "active" && enable) { + if (enable) { await patchVergeConfig({ enable_service_mode: false }); }