From b5b99884b05caba42f65a2c3997a01f869411508 Mon Sep 17 00:00:00 2001 From: pentaoa Date: Tue, 10 Nov 2026 14:05:52 +0800 Subject: [PATCH] Release a system proxy left pointing at a dead core stop_core_service turns the system proxy off when the user stops the core, but nothing does when the core dies on its own: a crash, an OOM kill, a terminal that closes and takes the core's process group with it. The machine is then left pointing every proxy-aware app at a closed loopback port, which reads as "the whole machine has no internet" and is not something the user can guess. Terc now drops its own entry when it starts and finds the core unavailable, so opening terc once restores connectivity. Only an entry that is loopback on one of this config's ports is touched; a corporate proxy is left alone. The ports lookup the stop path already did becomes release_stale_sysproxy, which both call sites share. --- src/main.rs | 12 ++++++++++++ src/mihomo.rs | 22 +++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 502524d..c13e273 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1077,6 +1077,18 @@ impl App { &format!("pid_record={}", pid.trim()), ); } + // A system proxy still pointing at the core that just died sends + // every proxy-aware app on the machine to a closed port. Drop our + // own entry here; the user turns it back on once the core is up. + if snapshot.sysproxy_enabled { + if let Ok(Some(note)) = mihomo::release_stale_sysproxy(&config.controller) { + crate::diagnostics::record( + &config.controller, + "sysproxy-stale-released", + ¬e, + ); + } + } } let mut app = Self::from_snapshot(config, snapshot); app.load_core_log(); diff --git a/src/mihomo.rs b/src/mihomo.rs index c57ae8c..7159540 100644 --- a/src/mihomo.rs +++ b/src/mihomo.rs @@ -3922,13 +3922,7 @@ pub fn stop_core_service(cfg: &ControllerConfig) -> Result { "core-stop-complete", &format!("pid={pid} source=core-service"), ); - let (_, enabled) = detect_sysproxy(); - if enabled { - let ports = read_config_text(cfg) - .map(|raw| yaml_listener_ports(&raw)) - .unwrap_or_default(); - release_sysproxy(&ports)?; - } + release_stale_sysproxy(cfg)?; Ok("Stopped managed Mihomo core".into()) } @@ -3974,6 +3968,20 @@ fn refuse_sysproxy_in_tests() -> Result<(), String> { } } +/// Turn the system proxy off when it is ours and the core it points at is not +/// running. A stale entry sends every proxy-aware app to a closed port, so this +/// runs whenever terc finds the core gone: on a stop, and at launch. +pub fn release_stale_sysproxy(cfg: &ControllerConfig) -> Result, String> { + let (_, enabled) = detect_sysproxy(); + if !enabled { + return Ok(None); + } + let ports = read_config_text(cfg) + .map(|raw| yaml_listener_ports(&raw)) + .unwrap_or_default(); + release_sysproxy(&ports) +} + /// Turn off the system proxy entries that still point at this core. fn release_sysproxy(ports: &[u16]) -> Result, String> { refuse_sysproxy_in_tests()?;