fix: ensure TUN is disabled before core exit

This commit is contained in:
wonfen
2025-08-11 23:19:49 +08:00
Unverified
parent 160ed05178
commit 67ee41c5ea

View File

@@ -98,31 +98,30 @@ async fn clean_async() -> bool {
logging!(info, Type::System, true, "开始执行异步清理操作...");
// 1. 处理TUN模式
let tun_task = async {
if Config::verge().data_mut().enable_tun_mode.unwrap_or(false) {
let disable_tun = serde_json::json!({
"tun": {
"enable": false
}
});
match timeout(
Duration::from_secs(2),
IpcManager::global().patch_configs(disable_tun),
)
.await
{
Ok(_) => {
log::info!(target: "app", "TUN模式已禁用");
true
}
Err(_) => {
log::warn!(target: "app", "禁用TUN模式超时");
false
}
let tun_success = if Config::verge().data_mut().enable_tun_mode.unwrap_or(false) {
let disable_tun = serde_json::json!({"tun": {"enable": false}});
match timeout(
Duration::from_secs(3),
IpcManager::global().patch_configs(disable_tun),
)
.await
{
Ok(Ok(_)) => {
log::info!(target: "app", "TUN模式已禁用");
tokio::time::sleep(Duration::from_millis(300)).await;
true
}
Ok(Err(e)) => {
log::warn!(target: "app", "禁用TUN模式失败: {}", e);
false
}
Err(_) => {
log::warn!(target: "app", "禁用TUN模式超时");
false
}
} else {
true
}
} else {
true
};
// 2. 系统代理重置
@@ -178,8 +177,8 @@ async fn clean_async() -> bool {
}
};
// 并行执行所有清理任务
let (tun_success, proxy_success, core_success) = tokio::join!(tun_task, proxy_task, core_task);
// 并行执行剩余清理任务
let (proxy_success, core_success) = tokio::join!(proxy_task, core_task);
#[cfg(target_os = "macos")]
let dns_success = dns_task.await;
@@ -192,7 +191,7 @@ async fn clean_async() -> bool {
info,
Type::System,
true,
"异步清理操作完成 - TUN: {}, 代理: {}, 核心: {}, DNS: {}, 总体: {}",
"异步关闭操作完成 - TUN: {}, 代理: {}, 核心: {}, DNS: {}, 总体: {}",
tun_success,
proxy_success,
core_success,
@@ -209,7 +208,7 @@ pub fn clean() -> bool {
let (tx, rx) = std::sync::mpsc::channel();
AsyncHandler::spawn(move || async move {
logging!(info, Type::System, true, "开始执行清理操作...");
logging!(info, Type::System, true, "开始执行关闭操作...");
// 使用已有的异步清理函数
let cleanup_result = clean_async().await;
@@ -220,7 +219,7 @@ pub fn clean() -> bool {
match rx.recv_timeout(std::time::Duration::from_secs(8)) {
Ok(result) => {
logging!(info, Type::System, true, "清理操作完成,结果: {}", result);
logging!(info, Type::System, true, "关闭操作完成,结果: {}", result);
result
}
Err(_) => {