fix(tun): make the TUN switch reflect what the Core actually runs

The switch read enable_tun_mode while the Core was given
enable_tun_mode && !session_suppression, so falling back to Sidecar left it
showing a TUN that was not running, with nothing to write the flag back.
Drop the session flag: the stored preference is the only truth, re-checked
at startup and written off when the conditions for it are not met, and the
switch renders it against current capability.
This commit is contained in:
wonfen
2026-08-31 17:09:36 +08:00
parent 8611fdc34f
commit e60285b4ae
8 changed files with 56 additions and 50 deletions
+10 -13
View File
@@ -13,13 +13,10 @@ use crate::{
/// Prevents recursive runtime disable writes.
static DISABLING_TUN: AtomicBool = AtomicBool::new(false);
/// Suppress TUN for this Windows session when startup confirms the Service is absent.
/// The saved preference remains available for a later Service install.
/// 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() {
if !cfg!(target_os = "windows") {
return;
}
// 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);
@@ -30,9 +27,13 @@ pub async fn reconcile_startup_tun_availability() {
logging!(
info,
Type::Setup,
"Windows Service is not installed; suppressing TUN mode so the Core can start on Sidecar"
"Service is not installed; turning TUN mode off so the Core can start on Sidecar"
);
Config::suppress_tun_for_session().await;
// 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.
@@ -45,15 +46,11 @@ pub async fn reconcile_tun_availability() {
let config_write = Config::lock_config_write().await;
let state = RUN_STATE.state();
// Startup uses the narrower session-suppression path above.
// Startup uses the narrower path above, before a Core is running.
if matches!(state.mode, RunningMode::NotRunning) {
return;
}
if Config::tun_suppressed_for_session() {
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) {