mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-04-18 08:21:34 +08:00
refactor(sysopt): replace Mutex with RwLock for inner_proxy management
This commit is contained in:
parent
f2ad62e446
commit
a0fd24bb90
@ -7,8 +7,6 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use clash_verge_logging::{Type, logging, logging_error};
|
use clash_verge_logging::{Type, logging, logging_error};
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
use parking_lot::Mutex;
|
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
use scopeguard::defer;
|
use scopeguard::defer;
|
||||||
use smartstring::alias::String;
|
use smartstring::alias::String;
|
||||||
@ -26,9 +24,7 @@ pub struct Sysopt {
|
|||||||
update_sysproxy: AtomicBool,
|
update_sysproxy: AtomicBool,
|
||||||
reset_sysproxy: AtomicBool,
|
reset_sysproxy: AtomicBool,
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
sysproxy: Arc<Mutex<Sysproxy>>,
|
inner_proxy: Arc<RwLock<(Sysproxy, Autoproxy)>>,
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
autoproxy: Arc<Mutex<Autoproxy>>,
|
|
||||||
guard: Arc<RwLock<GuardMonitor>>,
|
guard: Arc<RwLock<GuardMonitor>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,9 +34,7 @@ impl Default for Sysopt {
|
|||||||
update_sysproxy: AtomicBool::new(false),
|
update_sysproxy: AtomicBool::new(false),
|
||||||
reset_sysproxy: AtomicBool::new(false),
|
reset_sysproxy: AtomicBool::new(false),
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
sysproxy: Arc::new(Mutex::new(Sysproxy::default())),
|
inner_proxy: Arc::new(RwLock::new((Sysproxy::default(), Autoproxy::default()))),
|
||||||
#[cfg(not(target_os = "windows"))]
|
|
||||||
autoproxy: Arc::new(Mutex::new(Autoproxy::default())),
|
|
||||||
guard: Arc::new(RwLock::new(GuardMonitor::new(
|
guard: Arc::new(RwLock::new(GuardMonitor::new(
|
||||||
GuardType::None,
|
GuardType::None,
|
||||||
Duration::from_secs(30),
|
Duration::from_secs(30),
|
||||||
@ -198,13 +192,12 @@ impl Sysopt {
|
|||||||
// 先 await, 避免持有锁导致的 Send 问题
|
// 先 await, 避免持有锁导致的 Send 问题
|
||||||
let bypass = get_bypass().await;
|
let bypass = get_bypass().await;
|
||||||
|
|
||||||
let mut sys = self.sysproxy.lock();
|
let (sys, auto) = &mut *self.inner_proxy.write();
|
||||||
sys.enable = false;
|
sys.enable = false;
|
||||||
sys.host = proxy_host.clone().into();
|
sys.host = proxy_host.clone().into();
|
||||||
sys.port = port;
|
sys.port = port;
|
||||||
sys.bypass = bypass.into();
|
sys.bypass = bypass.into();
|
||||||
|
|
||||||
let mut auto = self.autoproxy.lock();
|
|
||||||
auto.enable = false;
|
auto.enable = false;
|
||||||
auto.url = format!("http://{proxy_host}:{pac_port}/commands/pac");
|
auto.url = format!("http://{proxy_host}:{pac_port}/commands/pac");
|
||||||
|
|
||||||
@ -233,11 +226,9 @@ impl Sysopt {
|
|||||||
sys.enable = true;
|
sys.enable = true;
|
||||||
auto.set_auto_proxy()?;
|
auto.set_auto_proxy()?;
|
||||||
sys.set_system_proxy()?;
|
sys.set_system_proxy()?;
|
||||||
drop(auto);
|
|
||||||
self.access_guard()
|
self.access_guard()
|
||||||
.write()
|
.write()
|
||||||
.set_guard_type(GuardType::Sysproxy(sys.clone()));
|
.set_guard_type(GuardType::Sysproxy(sys.clone()));
|
||||||
drop(sys);
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -296,13 +287,11 @@ impl Sysopt {
|
|||||||
//直接关闭所有代理
|
//直接关闭所有代理
|
||||||
#[cfg(not(target_os = "windows"))]
|
#[cfg(not(target_os = "windows"))]
|
||||||
{
|
{
|
||||||
let mut sysproxy = self.sysproxy.lock();
|
let (sys, auto) = &mut *self.inner_proxy.write();
|
||||||
sysproxy.enable = false;
|
sys.enable = false;
|
||||||
sysproxy.set_system_proxy()?;
|
sys.set_system_proxy()?;
|
||||||
drop(sysproxy);
|
auto.enable = false;
|
||||||
let mut autoproxy = self.autoproxy.lock();
|
auto.set_auto_proxy()?;
|
||||||
autoproxy.enable = false;
|
|
||||||
autoproxy.set_auto_proxy()?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user