mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 10:53:44 +08:00
feat: clean proxy settings before exit (#3404)
This commit is contained in:
parent
55cde38562
commit
7383459b9a
@ -64,42 +64,7 @@ pub fn quit() {
|
|||||||
|
|
||||||
// 在单独线程中处理资源清理,避免阻塞主线程
|
// 在单独线程中处理资源清理,避免阻塞主线程
|
||||||
std::thread::spawn(move || {
|
std::thread::spawn(move || {
|
||||||
use tokio::time::{timeout, Duration};
|
let cleanup_result = clean();
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
|
||||||
let cleanup_result = rt.block_on(async {
|
|
||||||
// 1. 处理TUN模式
|
|
||||||
let tun_success = if Config::verge().data().enable_tun_mode.unwrap_or(false) {
|
|
||||||
let disable_tun = serde_json::json!({
|
|
||||||
"tun": {
|
|
||||||
"enable": false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
timeout(
|
|
||||||
Duration::from_secs(1),
|
|
||||||
MihomoManager::global().patch_configs(disable_tun),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.is_ok()
|
|
||||||
} else {
|
|
||||||
true
|
|
||||||
};
|
|
||||||
|
|
||||||
// 2. 顺序执行关键清理
|
|
||||||
let proxy_res = timeout(
|
|
||||||
Duration::from_secs(1),
|
|
||||||
sysopt::Sysopt::global().reset_sysproxy(),
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
|
|
||||||
let core_res = timeout(Duration::from_secs(1), CoreManager::global().stop_core()).await;
|
|
||||||
|
|
||||||
// 3. 平台特定清理
|
|
||||||
#[cfg(target_os = "macos")]
|
|
||||||
let _dns_res = timeout(Duration::from_millis(500), resolve::restore_public_dns()).await;
|
|
||||||
|
|
||||||
tun_success && proxy_res.is_ok() && core_res.is_ok()
|
|
||||||
});
|
|
||||||
|
|
||||||
app_handle.exit(match cleanup_result {
|
app_handle.exit(match cleanup_result {
|
||||||
true => 0,
|
true => 0,
|
||||||
false => 1,
|
false => 1,
|
||||||
@ -107,6 +72,45 @@ pub fn quit() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn clean() -> bool {
|
||||||
|
use tokio::time::{timeout, Duration};
|
||||||
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
|
let cleanup_result = rt.block_on(async {
|
||||||
|
// 1. 处理TUN模式
|
||||||
|
let tun_success = if Config::verge().data().enable_tun_mode.unwrap_or(false) {
|
||||||
|
let disable_tun = serde_json::json!({
|
||||||
|
"tun": {
|
||||||
|
"enable": false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
timeout(
|
||||||
|
Duration::from_secs(1),
|
||||||
|
MihomoManager::global().patch_configs(disable_tun),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.is_ok()
|
||||||
|
} else {
|
||||||
|
true
|
||||||
|
};
|
||||||
|
|
||||||
|
// 2. 顺序执行关键清理
|
||||||
|
let proxy_res = timeout(
|
||||||
|
Duration::from_secs(1),
|
||||||
|
sysopt::Sysopt::global().reset_sysproxy(),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let core_res = timeout(Duration::from_secs(1), CoreManager::global().stop_core()).await;
|
||||||
|
|
||||||
|
// 3. 平台特定清理
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
let _dns_res = timeout(Duration::from_millis(500), resolve::restore_public_dns()).await;
|
||||||
|
|
||||||
|
tun_success && proxy_res.is_ok() && core_res.is_ok()
|
||||||
|
});
|
||||||
|
cleanup_result
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
pub fn hide() {
|
pub fn hide() {
|
||||||
use crate::module::lightweight::add_light_weight_timer;
|
use crate::module::lightweight::add_light_weight_timer;
|
||||||
|
@ -302,6 +302,13 @@ pub fn run() {
|
|||||||
api.prevent_exit();
|
api.prevent_exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
tauri::RunEvent::Exit => {
|
||||||
|
// avoid duplicate cleanup
|
||||||
|
if core::handle::Handle::global().is_exiting() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
feat::clean();
|
||||||
|
}
|
||||||
tauri::RunEvent::WindowEvent { label, event, .. } => {
|
tauri::RunEvent::WindowEvent { label, event, .. } => {
|
||||||
if label == "main" {
|
if label == "main" {
|
||||||
match event {
|
match event {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user