refactor: Rewrite and improve sysproxy logic (#7744)

* fix(cmd): preserve anyhow context in coded errors

Use alternate error formatting so coded failures retain the full anyhow context chain without changing their wire format.

* fix(macos): skip redundant proxy disable writes

When both proxy modes are off, read macOS state first and skip networksetup if the OS is already clean. Update the guard target only after a successful write.

* feat(proxy): classify system proxy failures

Attach stable, actionable proxy failure codes to anyhow chains while preserving the original diagnostic causes.

* feat(cmd): return structured command failures

Replace encoded error strings with serializable code and detail fields, and update frontend notice consumers to handle the structured shape.

* fix(scripts): use the js-yaml namespace import

Import js-yaml through its ESM namespace so the i18n cleanup script can access load and dump.

* feat(proxy): localize classified proxy failures

Map privilege and direct-fallback codes to localized explanations, and let mapped causes outrank generic operation keys in notices.

* fix(ui): bound notice text and render failure details

Limit resolved notice text without truncating clipboard content, and normalize structured failures embedded in translation parameters.

* feat(proxy): persist unseen proxy failures

Store unresolved classified failures by code, notify the frontend when the table changes, and show each failure sequence once when a visible window can receive it.

* feat(proxy): report tray and hotkey toggle failures

Record classified failures from tray and hotkey proxy toggles so they appear when a visible window is available.

* fix(ui): restore the proxy switch after failed toggles

Refresh frontend proxy state after a failed toggle so the switch reflects the rolled-back configuration.

* feat(proxy): stop the guard after repeated failures

Report guard outcomes, stop after the failure threshold, and persist the classified failure for the frontend.

* fix(macos): preserve proxy during service handoff

Stop a sidecar being handed to the service without clearing proxy state that the new owner must inherit.

* feat(macos): classify misplaced sidecar state

Distinguish a refused sidecar proxy write from a machine that still needs the privileged service installed.

* feat(ui): recover misplaced sidecar notices

Offer a restart action for macOS proxy failures caused by a ready service while the core still runs as a sidecar.

* feat(ui): install the service from proxy notices

Let privilege notices install the macOS service, restart the core, and report the runtime mode that was actually reached.

* feat(cmd): attach requested proxy operations

Carry the requested proxy direction with structured command failures when the originating command knows it.

* feat(sysproxy): drain guards before OS access

Wait for the local proxy guard to stop before OS reads, service handoff, or final writes, and refuse transitions when it does not drain.

* feat(sysproxy): recover partial proxy writes

Use write progress to choose whether to restore the old guard or compensate with a disabled proxy state, while preserving actionable errors.

* feat(sysproxy): skip already-applied proxy writes

Compare macOS proxy snapshots with global, PAC, and disabled targets so matching state avoids redundant privileged writes.

* feat(proxy): report background proxy failures

Persist and surface classified proxy failures from lifecycle paths that have no direct caller awaiting the result.

* feat(ui): move proxy recovery into a dialog

Guide users through installing or switching to the service after a refused proxy write, and suppress duplicate toast reports.

* fix(ui): close proxy dialog after service recovery

Dismiss the recovery dialog once the core reaches service mode, even when the original proxy request remains pending.

* feat(ui): retry proxy request after service recovery

Replay the requested proxy direction after the core reaches service mode, while leaving incidental restores unchanged.

* fix(proxy): refresh state after successful apply

Publish the proxy refresh from the shared apply path after releasing the lifecycle lock, and remove the tray-specific duplicate.

* feat(ui): show proxy recovery progress

Display installation, restart, and proxy-apply stages while keeping the primary action spinner visible during recovery.

* feat(ui): route unavailable TUN through service recovery

Let the TUN switch request service recovery and restore its requested state after the core restarts in service mode.

* fix(ui): avoid duplicate TUN recovery errors

Roll back deferred TUN toggles without reporting an error, and publish configuration refreshes from the transaction commit path.

* chore: update changelog & pinlock

* fix(core): resolve platform-specific clippy warnings

* fix(core): detect Windows wildcard TCP conflicts

Windows permits a specific exclusive bind beside an existing non-exclusive wildcard listener. Consult the system TCP listener table so the probe reports that overlap as a conflict.
This commit is contained in:
wonfen
2026-08-14 23:52:37 +08:00
committed by GitHub
parent 0844ed30a8
commit 22947632c0
93 changed files with 5208 additions and 337 deletions
+2 -2
View File
@@ -428,8 +428,8 @@ impl CoreManager {
Ok(())
}
Err(err) => {
logging!(error, Type::Core, "Failed to restart core: {}", err);
Err(anyhow!("Failed to apply config: {}", err))
logging!(error, Type::Core, "Failed to restart core: {err:#}");
Err(err.context("failed to apply the configuration"))
}
}
}
+195 -38
View File
@@ -1,11 +1,10 @@
use super::{CoreManager, RunningMode};
use crate::cmd::StringifyErr as _;
use crate::config::{Config, IVerge};
use crate::core::handle::Handle;
use crate::core::manager::CLASH_LOGGER;
use crate::core::proxy_control;
use crate::core::service::{SERVICE_MANAGER, ServiceStatus};
use anyhow::Result;
use anyhow::{Result, ensure};
use clash_verge_logging::{Type, logging};
use scopeguard::defer;
use smartstring::alias::String;
@@ -25,6 +24,22 @@ enum StartupDecision {
Wait,
}
/// Proxy action captured before a controlled stop.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ProxyStopIntent {
Clear,
/// Let the service overwrite the inherited proxy state.
HandOverToService,
}
/// Select whether a controlled stop clears or hands over proxy state.
const fn proxy_stop_intent(is_macos: bool, running_mode: RunningMode, decision: StartupDecision) -> ProxyStopIntent {
match (is_macos, running_mode, decision) {
(true, RunningMode::Sidecar, StartupDecision::Service) => ProxyStopIntent::HandOverToService,
_ => ProxyStopIntent::Clear,
}
}
const fn startup_decision(status: &ServiceStatus, service_required: bool) -> StartupDecision {
match status {
ServiceStatus::Ready => StartupDecision::Service,
@@ -102,22 +117,31 @@ impl ProxyRestoreExpectation {
async fn run_controlled_stop_transition<StopGuard, StopGuardFuture, Clear, ClearFuture, Stop, StopFuture>(
is_macos: bool,
running_mode: RunningMode,
proxy_intent: ProxyStopIntent,
stop_guard: StopGuard,
clear_proxy: Clear,
stop_core: Stop,
) -> Result<()>
where
StopGuard: FnOnce() -> StopGuardFuture,
StopGuardFuture: std::future::Future<Output = ()>,
StopGuardFuture: std::future::Future<Output = bool>,
Clear: FnOnce() -> ClearFuture,
ClearFuture: std::future::Future<Output = Result<()>>,
Stop: FnOnce() -> StopFuture,
StopFuture: std::future::Future<Output = Result<()>>,
{
match running_mode {
RunningMode::NotRunning => {}
RunningMode::Service if is_macos => stop_guard().await,
RunningMode::Service | RunningMode::Sidecar => clear_proxy().await?,
match (running_mode, proxy_intent) {
(RunningMode::NotRunning, _) => {}
// Do not hand the OS to another owner while the old guard may still write.
(RunningMode::Sidecar, ProxyStopIntent::HandOverToService) => ensure!(
stop_guard().await,
"the system proxy guard did not stop in time; not handing the proxy to the service"
),
(RunningMode::Service, _) if is_macos => ensure!(
stop_guard().await,
"the system proxy guard did not stop in time; not stopping the service-owned core"
),
(RunningMode::Service | RunningMode::Sidecar, _) => clear_proxy().await?,
}
stop_core().await
}
@@ -197,7 +221,7 @@ async fn run_service_config_replacement_transition<
) -> Result<()>
where
StopGuard: FnOnce() -> StopGuardFuture,
StopGuardFuture: std::future::Future<Output = ()>,
StopGuardFuture: std::future::Future<Output = bool>,
Clear: FnOnce() -> ClearFuture,
ClearFuture: std::future::Future<Output = Result<()>>,
Stop: FnOnce() -> StopFuture,
@@ -208,7 +232,15 @@ where
Restore: FnOnce() -> RestoreFuture,
RestoreFuture: std::future::Future<Output = Result<()>>,
{
run_controlled_stop_transition(is_macos, RunningMode::Service, stop_guard, clear_proxy, stop_core).await?;
run_controlled_stop_transition(
is_macos,
RunningMode::Service,
ProxyStopIntent::Clear,
stop_guard,
clear_proxy,
stop_core,
)
.await?;
run_ready_core_start_transition(start_core, core_is_ready, restore_proxy).await
}
@@ -311,16 +343,16 @@ impl CoreManager {
self.apply_proxy_after_start().await
}
.await;
if let Err(error) = &result {
if let Err(error) = result {
// Revoke only the failed Sidecar allowance; its startup says nothing about Service health.
SERVICE_MANAGER.withdraw_sidecar_allowance();
if let Err(cleanup_error) = self.rollback_failed_sidecar_transition().await {
return Err(anyhow::anyhow!(
"Sidecar startup failed: {error:#}; failed to clear proxy before rollback: {cleanup_error:#}"
));
return Err(proxy_control::rollback_failure(error, cleanup_error)
.context("failed to clear the proxy before rolling back"));
}
return Err(error);
}
result
Ok(())
}
pub async fn uninstall_service_and_start_sidecar(&self) -> Result<()> {
@@ -354,14 +386,14 @@ impl CoreManager {
)
.await;
if let Err(error) = &result
&& let Err(cleanup_error) = self.rollback_failed_sidecar_transition().await
{
return Err(anyhow::anyhow!(
"Service uninstall transition failed: {error:#}; failed to clear proxy before rollback: {cleanup_error:#}"
));
if let Err(error) = result {
if let Err(cleanup_error) = self.rollback_failed_sidecar_transition().await {
return Err(proxy_control::rollback_failure(error, cleanup_error)
.context("failed to clear the proxy before rolling back"));
}
return Err(error);
}
result
Ok(())
}
async fn rollback_failed_sidecar_transition(&self) -> Result<()> {
@@ -483,10 +515,16 @@ impl CoreManager {
/// 调用者须已持有 `lifecycle_lock`。
pub(crate) async fn controlled_stop_core_inner(&self) -> Result<()> {
self.controlled_stop_core_with_intent(ProxyStopIntent::Clear).await
}
/// 调用者须已持有 `lifecycle_lock`。
async fn controlled_stop_core_with_intent(&self, proxy_intent: ProxyStopIntent) -> Result<()> {
let running_mode = *self.get_running_mode();
run_controlled_stop_transition(
cfg!(target_os = "macos"),
running_mode,
proxy_intent,
proxy_control::stop_guard,
proxy_control::clear,
|| self.stop_core_unprepared_inner(),
@@ -494,6 +532,15 @@ impl CoreManager {
.await
}
/// Capture handoff intent before stopping; startup state may change afterward.
async fn proxy_stop_intent(&self) -> ProxyStopIntent {
let running_mode = *self.get_running_mode();
if !cfg!(target_os = "macos") || !matches!(running_mode, RunningMode::Sidecar) {
return ProxyStopIntent::Clear;
}
proxy_stop_intent(cfg!(target_os = "macos"), running_mode, self.prepare_startup().await)
}
/// 调用者须已持有 `lifecycle_lock`,且已完成受控代理清理。
async fn stop_core_unprepared_inner(&self) -> Result<()> {
CLASH_LOGGER.clear_logs().await;
@@ -521,8 +568,9 @@ impl CoreManager {
// 持锁覆盖 stop+start,避免生命周期操作插入。
let _life = self.lifecycle_lock.lock().await;
logging!(info, Type::Core, "Restarting core");
let proxy_intent = self.proxy_stop_intent().await;
run_core_replacement_transition(
|| self.controlled_stop_core_inner(),
|| self.controlled_stop_core_with_intent(proxy_intent),
|| async {
self.start_core_inner().await?;
if matches!(*self.get_running_mode(), RunningMode::NotRunning) {
@@ -535,9 +583,10 @@ impl CoreManager {
.await
}
pub async fn change_core(&self, clash_core: &String) -> Result<(), String> {
/// Switch the core binary and reload configuration.
pub async fn change_core(&self, clash_core: &String) -> Result<()> {
if !IVerge::VALID_CLASH_CORES.contains(&clash_core.as_str()) {
return Err(format!("Invalid clash core: {}", clash_core).into());
anyhow::bail!("invalid clash core: {clash_core}");
}
Config::verge().await.edit_draft(|d| {
@@ -546,10 +595,8 @@ impl CoreManager {
Config::verge().await.apply();
let verge_data = Config::verge().await.latest_arc();
verge_data.save_file().await.map_err(|e| e.to_string())?;
self.update_config_checked().await.stringify_err()?;
Ok(())
verge_data.save_file().await?;
self.update_config_checked().await
}
async fn prepare_startup(&self) -> StartupDecision {
@@ -761,7 +808,7 @@ impl CoreManager {
#[cfg(test)]
mod tests {
use super::{
CoreManager, ProxyRestoreExpectation, StartupDecision, can_allow_sidecar_for_session,
CoreManager, ProxyRestoreExpectation, ProxyStopIntent, StartupDecision, can_allow_sidecar_for_session,
run_controlled_stop_transition, run_core_replacement_transition, run_core_start_transition,
run_ready_core_start_transition, run_service_config_replacement_transition, run_sidecar_termination_transition,
run_uninstall_transition, should_wait_for_service, startup_decision,
@@ -800,10 +847,11 @@ mod tests {
true
}
async fn stop_guard(&self, calls: &Mutex<Vec<&'static str>>) {
async fn stop_guard(&self, calls: &Mutex<Vec<&'static str>>) -> bool {
self.generation.fetch_add(1, Ordering::AcqRel);
let _operation = self.operation_lock.lock().await;
calls.lock().push("guard-stopped");
true
}
}
@@ -848,7 +896,8 @@ mod tests {
let result = run_controlled_stop_transition(
true,
RunningMode::Sidecar,
|| future::ready(()),
ProxyStopIntent::Clear,
|| future::ready(true),
|| transition_step(&calls, "proxy_clear", None),
|| transition_step(&calls, "core_stop", None),
)
@@ -858,6 +907,112 @@ mod tests {
assert_eq!(&*calls.lock(), &["proxy_clear", "core_stop"]);
}
#[test]
fn only_a_macos_sidecar_on_its_way_to_the_service_leaves_the_proxy_alone() {
use super::proxy_stop_intent;
assert_eq!(
proxy_stop_intent(true, RunningMode::Sidecar, StartupDecision::Service),
ProxyStopIntent::HandOverToService
);
for (is_macos, mode, decision) in [
(true, RunningMode::Sidecar, StartupDecision::Sidecar),
(true, RunningMode::Sidecar, StartupDecision::Wait),
(true, RunningMode::Service, StartupDecision::Service),
(true, RunningMode::NotRunning, StartupDecision::Service),
(false, RunningMode::Sidecar, StartupDecision::Service),
] {
assert_eq!(
proxy_stop_intent(is_macos, mode, decision),
ProxyStopIntent::Clear,
"{is_macos} {mode:?} {decision:?}"
);
}
}
#[tokio::test]
async fn a_sidecar_handed_to_the_service_stops_its_guard_and_leaves_the_proxy_alone() {
let calls = Arc::new(Mutex::new(Vec::new()));
let result = run_controlled_stop_transition(
true,
RunningMode::Sidecar,
ProxyStopIntent::HandOverToService,
|| {
calls.lock().push("guard-stopped");
future::ready(true)
},
|| transition_step(&calls, "unexpected-proxy-clear", None),
|| transition_step(&calls, "core_stop", None),
)
.await;
assert!(result.is_ok());
assert_eq!(&*calls.lock(), &["guard-stopped", "core_stop"]);
}
#[tokio::test]
async fn a_guard_that_would_not_stop_keeps_the_core_where_it_is() {
for (mode, intent) in [
(RunningMode::Sidecar, ProxyStopIntent::HandOverToService),
(RunningMode::Service, ProxyStopIntent::Clear),
] {
let calls = Arc::new(Mutex::new(Vec::new()));
let result = run_controlled_stop_transition(
true,
mode,
intent,
|| future::ready(false),
|| transition_step(&calls, "proxy_clear", None),
|| transition_step(&calls, "core_stop", None),
)
.await;
assert!(result.is_err(), "{mode:?} {intent:?}");
assert!(calls.lock().is_empty(), "{mode:?} {intent:?}: {:?}", &*calls.lock());
}
}
#[tokio::test]
async fn a_sidecar_handed_over_still_stops_even_when_a_user_space_clear_would_fail() {
let calls = Arc::new(Mutex::new(Vec::new()));
let result = run_controlled_stop_transition(
true,
RunningMode::Sidecar,
ProxyStopIntent::HandOverToService,
|| future::ready(true),
|| transition_step(&calls, "proxy_clear", Some("proxy_clear")),
|| transition_step(&calls, "core_stop", None),
)
.await;
assert!(result.is_ok());
assert_eq!(&*calls.lock(), &["core_stop"]);
}
#[tokio::test]
async fn an_ordinary_sidecar_stop_still_clears_even_off_macos() {
for is_macos in [true, false] {
let calls = Arc::new(Mutex::new(Vec::new()));
let result = run_controlled_stop_transition(
is_macos,
RunningMode::Sidecar,
ProxyStopIntent::Clear,
|| future::ready(true),
|| transition_step(&calls, "proxy_clear", None),
|| transition_step(&calls, "core_stop", None),
)
.await;
assert!(result.is_ok());
assert_eq!(&*calls.lock(), &["proxy_clear", "core_stop"]);
}
}
#[tokio::test]
async fn controlled_macos_service_stop_drains_guard_before_one_helper_transaction() {
let calls = Arc::new(Mutex::new(Vec::new()));
@@ -865,9 +1020,10 @@ mod tests {
let result = run_controlled_stop_transition(
true,
RunningMode::Service,
ProxyStopIntent::Clear,
|| {
calls.lock().push("guard-stopped");
future::ready(())
future::ready(true)
},
|| transition_step(&calls, "unexpected-proxy-clear", None),
|| transition_step(&calls, "service_stop_with_proxy_clear", None),
@@ -904,9 +1060,8 @@ mod tests {
let mut stop = Box::pin(run_controlled_stop_transition(
true,
RunningMode::Service,
|| async {
coordinator.stop_guard(&calls).await;
},
ProxyStopIntent::Clear,
|| coordinator.stop_guard(&calls),
|| transition_step(&calls, "unexpected-proxy-clear", None),
|| transition_step(&calls, "service-stop-failed", Some("service-stop-failed")),
));
@@ -934,7 +1089,8 @@ mod tests {
let result = run_controlled_stop_transition(
true,
RunningMode::Sidecar,
|| future::ready(()),
ProxyStopIntent::Clear,
|| future::ready(true),
|| transition_step(&calls, "proxy_clear", Some("proxy_clear")),
|| transition_step(&calls, "core_stop", None),
)
@@ -1037,7 +1193,8 @@ mod tests {
let result = run_controlled_stop_transition(
false,
RunningMode::Service,
|| future::ready(()),
ProxyStopIntent::Clear,
|| future::ready(true),
|| transition_step(&calls, "service-proxy-clear", Some("service-proxy-clear")),
|| {
service_alive.store(false, Ordering::Release);
@@ -1139,7 +1296,7 @@ mod tests {
is_macos,
|| {
calls.lock().push("guard-stopped");
future::ready(())
future::ready(true)
},
|| transition_step(&calls, "proxy-clear", None),
|| {
+3 -4
View File
@@ -280,10 +280,9 @@ impl CoreManager {
}
Err(fallback_error) => {
crate::config::Config::block_startup_core(&fallback_error);
return Err(anyhow::anyhow!(
"core startup failed: {start_error:#}; mixed proxy port fallback failed: \
{fallback_error:#}"
));
return Err(start_error.context(format!(
"the mixed proxy port fallback did not rescue core startup: {fallback_error:#}"
)));
}
}
}