Files
clash-verge-rev/src-tauri/src/feat/tun.rs
T
Tunglies 929bb6a0a1 refactor(backend): share single-owner drivers for background tasks (#8759)
* docs(backend): define background task ownership

* refactor(backend): give background operations single task owners

* test(backend): remove actor migration regression cases

* docs(changelog): drop backend actor migration entry

* refactor(backend): extract workspace task drivers

* fix(timer): omit profile identifier from panic log

* docs(backend): define workspace crate boundaries

* refactor(lightweight): use the task countdown driver

* refactor(lightweight): name countdown callbacks

* refactor(task): simplify driver startup and coalescing requests

* refactor(core): bind service handoff to the sidecar generation

* refactor(tun): coalesce background run state reconciliation

* refactor(core): own sidecar exit resources in one session

* refactor(core): remove automatic Windows service handoff

* refactor(task): simplify coalescing job signals

* refactor(runstate): keep operation status with service state

* refactor(dns): coalesce background override synchronization

* refactor(sysproxy): own the local guard under its operation lock

* fix(tray): read menu settings at publication

* fix(dns): retain script lock across caller cancellation

* refactor(profiles): own selected node restoration in one task

* refactor(timer): own subscription schedules in the scheduler

* test(timer): remove the added application test

* fix(service): renew provider sync retry budget on refresh
2026-11-29 00:42:52 +00:00

107 lines
3.5 KiB
Rust

//! Reconciles TUN with current Core and Service availability.
use crate::core::notify::NoticeStatus;
use std::{
mem::DropGuard,
sync::{
LazyLock,
atomic::{AtomicBool, Ordering},
},
};
use clash_verge_logging::{Type, logging};
use clash_verge_task::scheduled_task::{self, CoalescingJob};
use crate::{
config::{Config, IVerge},
core::{handle::Handle, manager::RunningMode, runstate::RUN_STATE},
process::AsyncHandler,
};
/// Prevents recursive runtime disable writes.
static DISABLING_TUN: AtomicBool = AtomicBool::new(false);
static TUN_RECONCILE: LazyLock<CoalescingJob> = LazyLock::new(|| {
AsyncHandler::start_driver(scheduled_task::coalescing(reconcile_tun_availability, || {
logging!(error, Type::Core, "TUN reconciliation panicked");
}))
});
pub(crate) fn request_tun_reconciliation() {
// The internal disable can publish Run State as part of its own effects.
if !DISABLING_TUN.load(Ordering::Acquire) {
TUN_RECONCILE.request();
}
}
/// Turn TUN off when startup confirms the Service is absent, so the Core can start on Sidecar.
///
/// Runs before the runtime config is generated.
pub async fn reconcile_startup_tun_availability() {
// Read fresh state after prior config writes complete.
let _config_write = Config::lock_config_write().await;
let tun_enabled = Config::verge().await.data_arc().enable_tun_mode.unwrap_or(false);
if !RUN_STATE.state().startup_tun_should_be_disabled(tun_enabled) {
return;
}
logging!(
info,
Type::Setup,
"Service is not installed; turning TUN mode off so the Core can start on Sidecar"
);
// Write the preference only: a patch would reach update_config_checked, which with no Core to
// reload starts one ahead of the rest of startup, and rolls the write back if that fails.
if let Err(error) = Config::disable_tun_and_persist().await {
logging!(error, Type::Setup, "failed to turn TUN mode off at startup: {error:#}");
}
}
/// Disable TUN when the current settled Run State cannot support it.
pub async fn reconcile_tun_availability() {
if DISABLING_TUN.load(Ordering::Acquire) {
return;
}
// Re-read state and committed config after prior writes complete.
let config_write = Config::lock_config_write().await;
let state = RUN_STATE.state();
// Startup uses the narrower path above, before a Core is running.
if matches!(state.mode, RunningMode::NotRunning) {
return;
}
// Legacy writers may still hold an uncommitted draft.
let tun_enabled = Config::verge().await.data_arc().enable_tun_mode.unwrap_or(false);
if !state.tun_should_be_disabled(tun_enabled) {
return;
}
// Suppress recursive requests while the config lock serializes writers.
DISABLING_TUN.store(true, Ordering::Release);
let _disabling_tun = DropGuard::new((), |()| {
DISABLING_TUN.store(false, Ordering::Release);
});
logging!(
info,
Type::Core,
"TUN mode cannot work in the current run state; turning it off"
);
let patch = IVerge {
enable_tun_mode: Some(false),
..IVerge::default()
};
// Skip recursive reconciliation for this internal patch.
match super::apply_verge_patch_locked(&config_write, &patch, false).await {
Ok(()) => Handle::notice(NoticeStatus::TunModeAutoDisabled, ""),
Err(error) => {
logging!(error, Type::Core, "failed to turn TUN mode off: {error:#}");
Handle::notice(NoticeStatus::TunModeAutoDisableFailed, "");
}
}
}