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
+777 -31
View File
@@ -1,10 +1,21 @@
use crate::{
config::{Config, IVerge, MixedPort},
core::{CoreManager, manager::RunningMode, service, sysopt::Sysopt},
core::{
CoreManager,
manager::RunningMode,
notification::{self, FailedOperation},
runstate::RUN_STATE,
service,
sysopt::Sysopt,
},
process::AsyncHandler,
utils::server,
utils::{
notification::{NotificationEvent, needs_system_notification, notify_event},
server,
window_manager::WindowManager,
},
};
use anyhow::{Result, bail, ensure};
use anyhow::{Result, ensure};
use clash_verge_logging::{Type, logging};
use clash_verge_service_ipc::{MacosProxyConfig, OwnerSessionProof, ProxyApplyOutcome};
use std::{
@@ -12,7 +23,146 @@ use std::{
sync::atomic::{AtomicU64, Ordering},
time::Duration,
};
use tokio::sync::Mutex;
use tokio::{sync::Mutex, time::timeout};
/// Actionable system-proxy failure attached to an `anyhow` chain.
///
/// Classification remains downcastable while the original error stays available for diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SysproxyFailure {
/// A local `networksetup` write lacked privileges.
PrivilegeRequired,
/// The service failed to apply the proxy and attempted direct fallback.
DirectFallback { detail: String },
/// A ready service can replace the refused sidecar write.
SidecarWhileServiceReady,
/// The proxy guard stopped after repeated failures.
GuardStopped { detail: String },
}
impl SysproxyFailure {
/// Stable frontend error code.
#[inline]
pub const fn code(&self) -> &'static str {
match self {
Self::PrivilegeRequired => "SYSPROXY_PRIVILEGE_REQUIRED",
Self::DirectFallback { .. } => "SYSPROXY_DIRECT_FALLBACK",
Self::SidecarWhileServiceReady => "SYSPROXY_SIDECAR_WHILE_SERVICE_READY",
Self::GuardStopped { .. } => "SYSPROXY_GUARD_STOPPED",
}
}
/// Find a classification using `anyhow`'s context-aware downcast.
#[inline]
pub fn from_chain(error: &anyhow::Error) -> Option<&Self> {
error.downcast_ref::<Self>()
}
}
impl std::fmt::Display for SysproxyFailure {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::PrivilegeRequired => f.write_str("system proxy write requires elevated privileges"),
Self::DirectFallback { detail } => {
write!(f, "system proxy failed and fell back to direct: {detail}")
}
Self::SidecarWhileServiceReady => {
f.write_str("system proxy write was refused locally while the service was ready")
}
Self::GuardStopped { detail } => {
write!(f, "system proxy guard stopped after repeated failures: {detail}")
}
}
}
}
impl std::error::Error for SysproxyFailure {}
/// Marker for a partially applied system proxy write.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SystemProxyStateUnknown;
impl SystemProxyStateUnknown {
/// Whether this marker appears in the error chain.
#[inline]
pub fn is_in(error: &anyhow::Error) -> bool {
error.downcast_ref::<Self>().is_some()
}
}
impl std::fmt::Display for SystemProxyStateUnknown {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("the system proxy was left partly applied; turning it off was attempted")
}
}
impl std::error::Error for SystemProxyStateUnknown {}
/// Whether the error carries a frontend-mappable classification.
#[inline]
fn carries_a_mappable_code(error: &anyhow::Error) -> bool {
SysproxyFailure::from_chain(error).is_some()
}
/// Combine a cause with rollback failure without hiding actionable classifications.
pub fn rollback_failure(caused_by: anyhow::Error, rollback: anyhow::Error) -> anyhow::Error {
let state_unknown = SystemProxyStateUnknown::is_in(&caused_by) || SystemProxyStateUnknown::is_in(&rollback);
let combined = if carries_a_mappable_code(&caused_by) || !carries_a_mappable_code(&rollback) {
caused_by.context(format!("the rollback that followed also failed: {rollback:#}"))
} else {
rollback.context(format!("this rollback followed an earlier failure: {caused_by:#}"))
};
if state_unknown && !SystemProxyStateUnknown::is_in(&combined) {
return combined.context(SystemProxyStateUnknown);
}
combined
}
/// Classify local macOS proxy failures without replacing their source chain.
fn classify_local_failure(error: anyhow::Error) -> anyhow::Error {
if !was_refused_locally(&error) {
return error;
}
error.context(SysproxyFailure::PrivilegeRequired)
}
/// Whether macOS refused a local proxy write.
fn was_refused_locally(error: &anyhow::Error) -> bool {
cfg!(target_os = "macos")
&& error.chain().any(|cause| {
matches!(
cause.downcast_ref::<sysproxy::Error>(),
Some(sysproxy::Error::RequiresAdminPrivileges)
)
})
}
/// Distinguish a misplaced sidecar from a missing privileged service.
async fn classify_local_apply_failure(error: anyhow::Error) -> anyhow::Error {
// Preserve any existing, more specific classification.
if SysproxyFailure::from_chain(&error).is_some() || !was_refused_locally(&error) {
return error;
}
let probed = timeout(SERVICE_PROBE_TIMEOUT, RUN_STATE.probe())
.await
.ok()
.and_then(|probe| probe.ok())
.map(|fresh| fresh.service_usable());
error.context(refusal_classification(probed))
}
/// Bound the diagnostic service probe.
const SERVICE_PROBE_TIMEOUT: Duration = Duration::from_secs(2);
/// Classify a refusal from a fresh service probe only.
const fn refusal_classification(service_usable: Option<bool>) -> SysproxyFailure {
match service_usable {
Some(true) => SysproxyFailure::SidecarWhileServiceReady,
Some(false) | None => SysproxyFailure::PrivilegeRequired,
}
}
const LOOPBACK_HOST: &str = "127.0.0.1";
const MACOS_DEFAULT_BYPASS: &str =
@@ -56,20 +206,33 @@ impl ServiceProxyOperations {
self.run_service_operation(operation).await
}
async fn cancel_and_drain<F, Fut>(&self, cancel: F) -> u64
/// Invalidate the guard generation and wait for cancellation.
async fn cancel_and_drain<F, Fut, T>(&self, cancel: F) -> (u64, T)
where
F: FnOnce() -> Fut,
Fut: Future<Output = ()>,
Fut: Future<Output = T>,
{
let generation = self.invalidate_guard();
self.run_service_operation(cancel).await;
generation
let cancelled = self.run_service_operation(cancel).await;
(generation, cancelled)
}
/// Record under the operation lock only for the current guard generation.
async fn record_if_current<Record>(&self, captured_generation: u64, record: Record) -> bool
where
Record: FnOnce() -> bool,
{
self.run_service_operation(|| async move {
guard_generation_is_current(&self.guard_generation, captured_generation) && record()
})
.await
}
async fn run_guard_request<Mode, ActiveProof, Request, RequestFuture>(
&self,
captured_generation: u64,
captured_proof: &OwnerSessionProof,
requested: &MacosProxyConfig,
is_service_mode: Mode,
active_proof: ActiveProof,
request: Request,
@@ -87,7 +250,7 @@ impl ServiceProxyOperations {
{
return Ok(false);
}
service_apply_result(request().await?)?;
service_apply_result(requested, request().await?)?;
Ok(true)
})
.await
@@ -158,10 +321,14 @@ fn service_proxy_config(verge: &IVerge, mixed_port: u16, pac_port: u16) -> Resul
}
}
fn service_apply_result(outcome: ProxyApplyOutcome) -> Result<()> {
fn service_apply_result(requested: &MacosProxyConfig, outcome: ProxyApplyOutcome) -> Result<()> {
match outcome {
ProxyApplyOutcome::Applied | ProxyApplyOutcome::NotRequested => Ok(()),
ProxyApplyOutcome::DirectFallback { message } => bail!(message),
// Direct fallback satisfies a disable request.
ProxyApplyOutcome::DirectFallback { .. } if matches!(requested, MacosProxyConfig::Disabled) => Ok(()),
ProxyApplyOutcome::DirectFallback { message } => {
Err(SysproxyFailure::DirectFallback { detail: message }.into())
}
}
}
@@ -177,30 +344,140 @@ async fn current_service_proxy_config(verge: &IVerge) -> Result<MacosProxyConfig
service_proxy_config(verge, mixed_port, 0)
}
pub async fn apply() -> Result<()> {
let running_mode = CoreManager::global().get_running_mode();
match proxy_backend_route(cfg!(target_os = "macos"), &running_mode) {
ProxyBackendRoute::Local => Sysopt::global().update_sysproxy().await,
ProxyBackendRoute::Service => {
let verge = Config::verge().await.latest_arc();
let proxy = current_service_proxy_config(&verge).await?;
SERVICE_PROXY_OPERATIONS
.run_final_service_operation(|| async {
service_apply_result(service::set_system_proxy_by_service(&proxy).await?)
})
.await
/// Return whether a classified failure remains pending.
pub fn is_reportable(error: &anyhow::Error) -> bool {
is_reportable_given(error, notification::has_pending_failure)
}
fn is_reportable_given(error: &anyhow::Error, still_pending: impl FnOnce(&str) -> bool) -> bool {
SysproxyFailure::from_chain(error).is_some_and(|failure| still_pending(failure.code()))
}
pub fn report_failure(operation: FailedOperation, error: &anyhow::Error) -> bool {
let Some(failure) = SysproxyFailure::from_chain(error) else {
return false;
};
notification::record_failure(operation, failure.code(), format!("{error:#}"));
true
}
/// Drain any local guard before writing through the service.
async fn apply_through_service<Drain, DrainFuture, Write, WriteFuture>(
drain_local_guard: Drain,
write: Write,
) -> Result<()>
where
Drain: FnOnce() -> DrainFuture,
DrainFuture: Future<Output = bool>,
Write: FnOnce() -> WriteFuture,
WriteFuture: Future<Output = Result<()>>,
{
ensure!(
drain_local_guard().await,
"the system proxy guard did not stop in time; not writing the system proxy through the service"
);
write().await
}
#[derive(Debug)]
enum TableEffect<'a> {
Retire { enabled: bool },
File(FailedOperation, &'a anyhow::Error),
Nothing,
}
/// A successful clear does not resolve a failed request to enable the proxy.
fn clear_table_effect(result: &Result<()>) -> TableEffect<'_> {
match result {
Err(error) if SysproxyFailure::from_chain(error).is_some() => {
TableEffect::File(notification::what_was_asked(), error)
}
Ok(()) | Err(_) => TableEffect::Nothing,
}
}
fn settle_table<Retire, File>(effect: TableEffect<'_>, retire: Retire, file: File)
where
Retire: FnOnce(bool),
File: FnOnce(FailedOperation, &anyhow::Error),
{
match effect {
TableEffect::Retire { enabled } => retire(enabled),
TableEffect::File(operation, error) => file(operation, error),
TableEffect::Nothing => {}
}
}
fn table_effect(result: &Result<()>, enabled: bool) -> TableEffect<'_> {
match result {
Ok(()) => TableEffect::Retire { enabled },
Err(error) if SysproxyFailure::from_chain(error).is_some() => {
TableEffect::File(notification::what_was_asked(), error)
}
Err(_) => TableEffect::Nothing,
}
}
pub async fn apply() -> Result<()> {
let running_mode = CoreManager::global().get_running_mode();
// Use the pre-apply snapshot when retiring matching failures.
let verge = Config::verge().await.latest_arc();
let enabled = verge.enable_system_proxy.unwrap_or_default();
let result = match proxy_backend_route(cfg!(target_os = "macos"), &running_mode) {
ProxyBackendRoute::Local => match Sysopt::global().update_sysproxy().await {
Ok(()) => Ok(()),
Err(error) => Err(classify_local_apply_failure(error).await),
},
ProxyBackendRoute::Service => {
let proxy = current_service_proxy_config(&verge).await?;
apply_through_service(
|| Sysopt::global().stop_proxy_guard(),
|| async {
SERVICE_PROXY_OPERATIONS
.run_final_service_operation(|| async {
service_apply_result(&proxy, service::set_system_proxy_by_service(&proxy).await?)
})
.await
},
)
.await
}
};
// Settle failures where every proxy-apply path converges.
settle_table(
table_effect(&result, enabled),
notification::retire_system_proxy_failures,
|operation, error| {
report_failure(operation, error);
},
);
result
}
pub async fn clear() -> Result<()> {
let result = clear_inner().await;
// Clear failures must be filed even when a restart aborts before apply.
settle_table(
clear_table_effect(&result),
notification::retire_system_proxy_failures,
|operation, error| {
report_failure(operation, error);
},
);
result
}
async fn clear_inner() -> Result<()> {
let running_mode = CoreManager::global().get_running_mode();
match proxy_backend_route(cfg!(target_os = "macos"), &running_mode) {
ProxyBackendRoute::Local => Sysopt::global().reset_sysproxy().await,
ProxyBackendRoute::Local => Sysopt::global().reset_sysproxy().await.map_err(classify_local_failure),
ProxyBackendRoute::Service => {
SERVICE_PROXY_OPERATIONS
.run_final_service_operation(|| async {
service_apply_result(service::set_system_proxy_by_service(&MacosProxyConfig::Disabled).await?)
service_apply_result(
&MacosProxyConfig::Disabled,
service::set_system_proxy_by_service(&MacosProxyConfig::Disabled).await?,
)
})
.await
}
@@ -208,7 +485,7 @@ pub async fn clear() -> Result<()> {
}
pub async fn refresh_guard() -> Result<()> {
let generation = SERVICE_PROXY_OPERATIONS
let (generation, _drained) = SERVICE_PROXY_OPERATIONS
.cancel_and_drain(|| Sysopt::global().stop_proxy_guard())
.await;
let running_mode = CoreManager::global().get_running_mode();
@@ -216,12 +493,16 @@ pub async fn refresh_guard() -> Result<()> {
proxy_backend_route(cfg!(target_os = "macos"), &running_mode),
ProxyBackendRoute::Local
) {
Sysopt::global().refresh_guard().await;
// Retire the warning only after a guard is enforcing.
if Sysopt::global().refresh_guard().await {
notification::retire_guard_failures();
}
return Ok(());
}
let verge = Config::verge().await.latest_arc();
if !verge.enable_system_proxy.unwrap_or_default() || !verge.enable_proxy_guard.unwrap_or_default() {
notification::retire_guard_failures();
return Ok(());
}
@@ -229,31 +510,71 @@ pub async fn refresh_guard() -> Result<()> {
let proof = service::active_service_session()?;
let interval = Duration::from_secs(verge.proxy_guard_duration.unwrap_or(30).max(1));
AsyncHandler::spawn(move || async move {
let mut consecutive_failures = 0u32;
loop {
tokio::time::sleep(interval).await;
match SERVICE_PROXY_OPERATIONS
.run_guard_request(
generation,
&proof,
&proxy,
|| matches!(*CoreManager::global().get_running_mode(), RunningMode::Service),
service::active_service_session,
|| service::set_system_proxy_by_service_with_session(&proxy, &proof),
)
.await
{
Ok(true) => {}
Ok(true) => consecutive_failures = 0,
Ok(false) => break,
Err(_) => logging!(warn, Type::Core, "failed to refresh system proxy through Service"),
Err(error) => {
consecutive_failures += 1;
logging!(
warn,
Type::Core,
"failed to refresh system proxy through Service ({}/{}): {:#}",
consecutive_failures,
GUARD_FAILURES_BEFORE_STOPPING,
error
);
if consecutive_failures >= GUARD_FAILURES_BEFORE_STOPPING {
report_guard_stopped(generation, &error).await;
break;
}
}
}
}
});
// Retire only after the replacement guard starts successfully.
notification::retire_guard_failures();
Ok(())
}
pub async fn stop_guard() {
/// Consecutive failures allowed before the guard stops.
const GUARD_FAILURES_BEFORE_STOPPING: u32 = 3;
/// Report that the current guard stopped.
async fn report_guard_stopped(captured_generation: u64, error: &anyhow::Error) {
let stopped = anyhow::Error::new(SysproxyFailure::GuardStopped {
detail: format!("{error:#}"),
});
// Do not publish a late failure from a replaced guard.
let recorded = SERVICE_PROXY_OPERATIONS
.record_if_current(captured_generation, || {
report_failure(FailedOperation::SystemProxyGuard, &stopped)
})
.await;
if recorded && needs_system_notification(WindowManager::get_main_window_state()) {
notify_event(NotificationEvent::SystemProxyGuardStopped).await;
}
}
/// Stop the local guard and report whether it drained.
pub async fn stop_guard() -> bool {
SERVICE_PROXY_OPERATIONS
.cancel_and_drain(|| Sysopt::global().stop_proxy_guard())
.await;
.await
.1
}
#[cfg(test)]
@@ -272,6 +593,15 @@ mod tests {
use std::task::Poll;
use tokio::sync::Barrier;
/// Proxy state maintained by a running guard.
fn guarded_proxy() -> MacosProxyConfig {
MacosProxyConfig::Global {
host: "127.0.0.1".to_owned(),
port: 7897,
bypass: "localhost".to_owned(),
}
}
fn proof(generation: u64, token: &str) -> OwnerSessionProof {
OwnerSessionProof {
generation,
@@ -327,6 +657,7 @@ mod tests {
.run_guard_request(
generation,
&captured_proof,
&guarded_proxy(),
|| true,
|| Ok(active_proof.lock().clone()),
|| async {
@@ -379,9 +710,11 @@ mod tests {
blocker_started.wait().await;
let rpc_called = AtomicBool::new(false);
let guarded = guarded_proxy();
let mut guard_request = Box::pin(operations.run_guard_request(
generation,
&captured_proof,
&guarded,
|| true,
|| Ok(captured_proof.clone()),
|| async {
@@ -424,9 +757,11 @@ mod tests {
};
blocker_started.wait().await;
let guarded = guarded_proxy();
let mut guard_request = Box::pin(operations.run_guard_request(
generation,
&captured_proof,
&guarded,
|| true,
|| Ok(active_proof.lock().clone()),
|| async {
@@ -453,6 +788,7 @@ mod tests {
.run_guard_request(
generation,
&captured_proof,
&guarded_proxy(),
|| true,
|| Ok(captured_proof.clone()),
|| async {
@@ -466,6 +802,97 @@ mod tests {
assert!(refreshed.is_err());
}
#[tokio::test]
async fn a_replaced_guard_does_not_get_to_report_its_last_failure() {
let operations = ServiceProxyOperations::new();
let generation = operations.invalidate_guard();
operations.invalidate_guard();
assert!(!operations.record_if_current(generation, || true).await);
}
#[tokio::test]
async fn a_report_waiting_on_the_lock_says_nothing_if_it_is_invalidated_meanwhile() {
// The generation check must happen after acquiring the operation lock.
let operations = Arc::new(ServiceProxyOperations::new());
let generation = operations.invalidate_guard();
let blocker_started = Arc::new(Barrier::new(2));
let release_blocker = Arc::new(Barrier::new(2));
let blocker = {
let operations = Arc::clone(&operations);
let blocker_started = Arc::clone(&blocker_started);
let release_blocker = Arc::clone(&release_blocker);
tokio::spawn(async move {
operations
.run_service_operation(|| async {
blocker_started.wait().await;
release_blocker.wait().await;
})
.await
})
};
blocker_started.wait().await;
let recorded = AtomicBool::new(false);
let mut report = Box::pin(operations.record_if_current(generation, || {
recorded.store(true, Ordering::Release);
true
}));
assert!(matches!(futures::poll!(report.as_mut()), Poll::Pending));
operations.invalidate_guard();
release_blocker.wait().await;
assert!(blocker.await.is_ok());
assert!(!report.await);
assert!(!recorded.load(Ordering::Acquire));
}
#[tokio::test]
async fn a_current_guard_still_reports() {
let operations = ServiceProxyOperations::new();
let generation = operations.invalidate_guard();
assert!(operations.record_if_current(generation, || true).await);
}
async fn record_service_apply(drained: bool, write_succeeds: bool) -> (Vec<&'static str>, bool) {
let calls = Mutex::new(Vec::new());
let result = super::apply_through_service(
|| {
calls.lock().push("drain");
async move { drained }
},
|| {
calls.lock().push("write");
async move {
if write_succeeds {
Ok(())
} else {
anyhow::bail!("the helper refused")
}
}
},
)
.await;
(calls.into_inner(), result.is_ok())
}
#[tokio::test]
async fn the_service_writes_only_once_the_local_guard_is_gone() {
assert_eq!(record_service_apply(true, true).await, (vec!["drain", "write"], true));
}
#[tokio::test]
async fn a_write_the_service_refused_is_still_a_failure() {
assert_eq!(record_service_apply(true, false).await, (vec!["drain", "write"], false));
}
#[tokio::test]
async fn a_local_guard_that_would_not_stop_keeps_root_from_writing() {
assert_eq!(record_service_apply(false, true).await, (vec!["drain"], false));
}
#[test]
fn service_proxy_config_forces_loopback_targets_and_bounds_bypass() {
let verge = IVerge {
@@ -498,4 +925,323 @@ mod tests {
}
);
}
use super::{FailedOperation, SysproxyFailure, classify_local_failure};
fn wrapped_privilege_failure() -> anyhow::Error {
anyhow::Error::new(sysproxy::Error::RequiresAdminPrivileges).context("failed to apply the system proxy")
}
fn classified_privilege_failure() -> anyhow::Error {
wrapped_privilege_failure().context(SysproxyFailure::PrivilegeRequired)
}
#[test]
fn a_classification_survives_the_anyhow_layers_above_it() {
let classified = classified_privilege_failure()
.context("failed to apply system proxy after start")
.context("failed to restart the core");
assert_eq!(
SysproxyFailure::from_chain(&classified).map(SysproxyFailure::code),
Some("SYSPROXY_PRIVILEGE_REQUIRED")
);
}
#[test]
fn classifying_keeps_the_original_failure_underneath() {
let classified = classified_privilege_failure();
assert!(
classified.chain().any(|cause| matches!(
cause.downcast_ref::<sysproxy::Error>(),
Some(sysproxy::Error::RequiresAdminPrivileges)
)),
"the original error must survive classification: {classified:#}"
);
assert!(format!("{classified:#}").contains("failed to apply the system proxy"));
}
#[test]
fn an_unrelated_failure_is_left_alone() {
let untouched = classify_local_failure(anyhow::anyhow!("port already in use"));
assert!(SysproxyFailure::from_chain(&untouched).is_none());
assert_eq!(format!("{untouched:#}"), "port already in use");
}
#[cfg(target_os = "macos")]
#[test]
fn a_refusal_wrapped_in_write_progress_is_still_classified() {
let wrapped = anyhow::Error::new(sysproxy::Error::ProxyWrite {
progress: sysproxy::WriteProgress::new(0, 7),
source: Box::new(sysproxy::Error::RequiresAdminPrivileges),
})
.context("failed to apply the system proxy");
let classified = classify_local_failure(wrapped);
assert!(matches!(
SysproxyFailure::from_chain(&classified),
Some(SysproxyFailure::PrivilegeRequired)
));
}
#[cfg(target_os = "macos")]
#[test]
fn a_refused_write_is_classified_on_macos() {
let classified = classify_local_failure(wrapped_privilege_failure());
assert!(matches!(
SysproxyFailure::from_chain(&classified),
Some(SysproxyFailure::PrivilegeRequired)
));
assert!(
classified.chain().any(|cause| matches!(
cause.downcast_ref::<sysproxy::Error>(),
Some(sysproxy::Error::RequiresAdminPrivileges)
)),
"classifying must keep the original failure reachable: {classified:#}"
);
assert!(format!("{classified:#}").contains("failed to apply the system proxy"));
}
#[cfg(not(target_os = "macos"))]
#[test]
fn nothing_is_classified_off_macos() {
let classified = classify_local_failure(wrapped_privilege_failure());
assert!(SysproxyFailure::from_chain(&classified).is_none());
}
#[test]
fn only_a_classified_failure_is_worth_recording() {
let before = crate::core::notification::pending_failures().len();
assert!(!super::report_failure(
FailedOperation::SystemProxyRestore,
&anyhow::anyhow!("port already in use"),
));
assert_eq!(crate::core::notification::pending_failures().len(), before);
assert!(super::report_failure(
FailedOperation::SystemProxyRestore,
&classified_privilege_failure().context("failed to apply system proxy after start"),
));
let recorded = crate::core::notification::pending_failures();
let failure = recorded
.iter()
.find(|failure| failure.code == "SYSPROXY_PRIVILEGE_REQUIRED")
.unwrap_or_else(|| unreachable!("a classified failure must be recorded"));
assert!(
failure.detail.contains("failed to apply system proxy after start"),
"{failure:?}"
);
assert!(failure.detail.contains("admin privileges"), "{failure:?}");
}
#[test]
fn a_refusal_only_blames_the_sidecar_when_a_probe_actually_answered() {
use super::refusal_classification;
assert_eq!(
refusal_classification(Some(true)),
SysproxyFailure::SidecarWhileServiceReady
);
assert_eq!(refusal_classification(Some(false)), SysproxyFailure::PrivilegeRequired);
assert_eq!(refusal_classification(None), SysproxyFailure::PrivilegeRequired);
}
use super::{SystemProxyStateUnknown, is_reportable_given, rollback_failure};
fn mappable() -> anyhow::Error {
anyhow::anyhow!("networksetup refused").context(SysproxyFailure::PrivilegeRequired)
}
fn record_settlement(effect: super::TableEffect<'_>) -> Vec<String> {
let events = Mutex::new(Vec::new());
super::settle_table(
effect,
|enabled| events.lock().push(format!("retire:{enabled}")),
|operation, _| events.lock().push(format!("file:{operation:?}")),
);
events.into_inner()
}
#[test]
fn each_effect_reaches_the_table_it_names() {
assert_eq!(
record_settlement(super::TableEffect::Retire { enabled: true }),
vec!["retire:true"]
);
assert_eq!(
record_settlement(super::TableEffect::File(
FailedOperation::SystemProxyRestore,
&mappable()
)),
vec!["file:SystemProxyRestore"]
);
assert!(record_settlement(super::TableEffect::Nothing).is_empty());
}
#[test]
fn a_clear_files_its_failure_and_says_nothing_when_it_works() {
assert!(matches!(
super::clear_table_effect(&Err(mappable())),
super::TableEffect::File(FailedOperation::SystemProxyRestore, _)
));
assert!(matches!(
super::clear_table_effect(&Ok(())),
super::TableEffect::Nothing
));
assert!(matches!(
super::clear_table_effect(&Err(anyhow::anyhow!("the core was already gone"))),
super::TableEffect::Nothing
));
}
#[test]
fn a_clean_apply_retires_in_the_direction_it_reached() {
assert!(matches!(
super::table_effect(&Ok(()), true),
super::TableEffect::Retire { enabled: true }
));
assert!(matches!(
super::table_effect(&Ok(()), false),
super::TableEffect::Retire { enabled: false }
));
}
#[test]
fn a_failure_that_can_be_explained_is_filed_here_as_a_restore() {
assert!(matches!(
super::table_effect(&Err(mappable()), true),
super::TableEffect::File(FailedOperation::SystemProxyRestore, _)
));
}
#[tokio::test]
async fn a_failure_is_filed_as_whatever_the_caller_declared() {
let (applied, cleared) = super::notification::asking_for(FailedOperation::SystemProxyEnable, async {
let (apply_error, clear_error) = (mappable(), mappable());
(
format!("{:?}", super::table_effect(&Err(apply_error), true)),
format!("{:?}", super::clear_table_effect(&Err(clear_error))),
)
})
.await;
assert!(applied.contains("File(SystemProxyEnable"), "{applied}");
assert!(cleared.contains("File(SystemProxyEnable"), "{cleared}");
}
#[test]
fn telling_the_user_to_look_needs_something_there_to_find() {
assert!(is_reportable_given(&mappable(), |_| true));
assert!(!is_reportable_given(&mappable(), |_| false));
assert!(!is_reportable_given(&anyhow::anyhow!("the core exited"), |_| true));
}
#[test]
fn a_failure_with_nothing_to_say_is_not_filed() {
assert!(matches!(
super::table_effect(&Err(anyhow::anyhow!("the core exited immediately")), true),
super::TableEffect::Nothing
));
}
#[test]
fn a_rollback_that_can_be_explained_becomes_the_source() {
let combined = rollback_failure(anyhow::anyhow!("could not write the file"), mappable());
assert!(SysproxyFailure::from_chain(&combined).is_some());
assert!(format!("{combined:#}").contains("could not write the file"));
}
#[test]
fn the_causing_error_stays_the_source_when_it_is_the_one_that_can_be_explained() {
let combined = rollback_failure(mappable(), anyhow::anyhow!("could not restore the file"));
assert!(matches!(
SysproxyFailure::from_chain(&combined),
Some(SysproxyFailure::PrivilegeRequired)
));
assert!(format!("{combined:#}").contains("could not restore the file"));
}
#[test]
fn a_state_marker_does_not_outrank_a_code_but_is_not_lost_to_it() {
let combined = rollback_failure(
anyhow::anyhow!("half applied").context(SystemProxyStateUnknown),
mappable(),
);
assert!(SysproxyFailure::from_chain(&combined).is_some());
assert!(SystemProxyStateUnknown::is_in(&combined));
}
#[test]
fn a_state_marker_on_the_source_is_left_where_it_is() {
let combined = rollback_failure(
mappable().context(SystemProxyStateUnknown),
anyhow::anyhow!("could not restore the file"),
);
assert!(SystemProxyStateUnknown::is_in(&combined));
assert!(SysproxyFailure::from_chain(&combined).is_some());
}
#[test]
fn with_both_mappable_the_causing_error_wins() {
let combined = rollback_failure(
mappable(),
anyhow::anyhow!("the service could not set it").context(SysproxyFailure::DirectFallback {
detail: "fell back to direct".to_owned(),
}),
);
assert!(matches!(
SysproxyFailure::from_chain(&combined),
Some(SysproxyFailure::PrivilegeRequired)
));
assert!(format!("{combined:#}").contains("fell back to direct"));
}
#[test]
fn with_neither_mappable_the_causing_error_is_kept() {
let combined = rollback_failure(anyhow::anyhow!("persisting failed"), anyhow::anyhow!("rollback failed"));
assert!(SysproxyFailure::from_chain(&combined).is_none());
assert_eq!(
format!("{combined:#}"),
"the rollback that followed also failed: rollback failed: persisting failed"
);
}
#[test]
fn a_disable_that_ended_up_direct_is_what_was_asked_for() {
let outcome = ProxyApplyOutcome::DirectFallback {
message: "first attempt failed".to_owned(),
};
assert!(super::service_apply_result(&MacosProxyConfig::Disabled, outcome).is_ok());
}
#[test]
fn a_direct_fallback_reports_that_traffic_may_be_going_direct() {
let outcome = ProxyApplyOutcome::DirectFallback {
message: "service could not set the proxy".to_owned(),
};
let error = super::service_apply_result(&guarded_proxy(), outcome)
.err()
.unwrap_or_else(|| anyhow::anyhow!("a fallback outcome must be reported as a failure"));
assert_eq!(
SysproxyFailure::from_chain(&error).map(SysproxyFailure::code),
Some("SYSPROXY_DIRECT_FALLBACK")
);
assert!(format!("{error:#}").contains("service could not set the proxy"));
}
}