mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-30 01:20:07 +08:00
* 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
83 lines
2.7 KiB
Rust
83 lines
2.7 KiB
Rust
//! Re-apply the system proxy when macOS gains or switches its primary network service.
|
|
//!
|
|
//! Proxy settings live per network service. A write that found no service wrote nothing, and a
|
|
//! switch to another service leaves the proxy on the old one; both need the write repeated.
|
|
|
|
use crate::{
|
|
config::Config,
|
|
core::{CoreManager, proxy_control},
|
|
process::AsyncHandler,
|
|
};
|
|
use clash_verge_logging::{Type, logging};
|
|
use clash_verge_task::scheduled_task::{self, CoalescingJob};
|
|
use parking_lot::Mutex;
|
|
use std::sync::LazyLock;
|
|
use sysproxy::NetworkServiceMonitor;
|
|
|
|
static MONITOR: Mutex<Option<NetworkServiceMonitor>> = Mutex::new(None);
|
|
|
|
static REAPPLY: LazyLock<CoalescingJob> = LazyLock::new(|| {
|
|
AsyncHandler::start_driver(scheduled_task::coalescing(reapply, || {
|
|
logging!(error, Type::Core, "network proxy reapply panicked");
|
|
}))
|
|
});
|
|
|
|
pub fn is_armed() -> bool {
|
|
MONITOR.lock().is_some()
|
|
}
|
|
|
|
/// Subscribe to the primary IPv4 service on the main run loop. Armed on return, so a service
|
|
/// appearing from here on is never missed.
|
|
pub fn start() {
|
|
match NetworkServiceMonitor::start(|| {
|
|
REAPPLY.request();
|
|
}) {
|
|
Ok(monitor) => {
|
|
*MONITOR.lock() = Some(monitor);
|
|
logging!(
|
|
debug,
|
|
Type::Core,
|
|
"network watch armed; the system proxy is re-applied on network changes"
|
|
);
|
|
}
|
|
Err(error) => logging!(
|
|
warn,
|
|
Type::Core,
|
|
"could not watch the network; the proxy is not re-applied on changes: {error}"
|
|
),
|
|
}
|
|
}
|
|
|
|
async fn reapply() {
|
|
// Config lock first, then lifecycle: the repo's order. Holding both also means the config
|
|
// read below is committed state, not a patch that may still roll back.
|
|
let _config_write = Config::lock_config_write().await;
|
|
let manager = CoreManager::global();
|
|
let _life = manager.lifecycle_lock.lock().await;
|
|
// Checked under the locks: a task that queued behind a restart may be stale by now.
|
|
if !Config::verge()
|
|
.await
|
|
.latest_arc()
|
|
.enable_system_proxy
|
|
.unwrap_or_default()
|
|
{
|
|
crate::utils::resolve::dns::sync_public_dns().await;
|
|
return;
|
|
}
|
|
if !proxy_control::has_network_service().await {
|
|
logging!(
|
|
debug,
|
|
Type::Core,
|
|
"network changed without a service to write the proxy on; waiting for the next change"
|
|
);
|
|
return;
|
|
}
|
|
if let Err(error) = manager.apply_proxy_after_start().await {
|
|
logging!(
|
|
warn,
|
|
Type::Core,
|
|
"failed to re-apply the system proxy after a network change: {error:#}"
|
|
);
|
|
}
|
|
}
|