mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-28 07:14:40 +08:00
refactor: optimize
This commit is contained in:
@@ -1,108 +1,71 @@
|
||||
use self::notice::Notice;
|
||||
use self::handle::Handle;
|
||||
use self::sysopt::Sysopt;
|
||||
use self::timer::Timer;
|
||||
use crate::config::enhance_config;
|
||||
use crate::data::*;
|
||||
use crate::log_if_err;
|
||||
use anyhow::{bail, Result};
|
||||
use once_cell::sync::Lazy;
|
||||
use parking_lot::Mutex;
|
||||
use serde_yaml::Mapping;
|
||||
use serde_yaml::Value;
|
||||
use serde_yaml::{Mapping, Value};
|
||||
use std::sync::Arc;
|
||||
use tauri::{AppHandle, Manager, Window};
|
||||
|
||||
mod clash;
|
||||
mod notice;
|
||||
mod prfitem;
|
||||
mod profiles;
|
||||
mod handle;
|
||||
mod service;
|
||||
mod sysopt;
|
||||
mod timer;
|
||||
mod verge;
|
||||
|
||||
pub use self::clash::*;
|
||||
pub use self::prfitem::*;
|
||||
pub use self::profiles::*;
|
||||
pub use self::service::*;
|
||||
pub use self::verge::*;
|
||||
|
||||
static CORE: Lazy<Core> = Lazy::new(|| Core {
|
||||
service: Arc::new(Mutex::new(Service::new())),
|
||||
sysopt: Arc::new(Mutex::new(Sysopt::new())),
|
||||
timer: Arc::new(Mutex::new(Timer::new())),
|
||||
runtime: Arc::new(Mutex::new(RuntimeResult::default())),
|
||||
handle: Handle::default(),
|
||||
});
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Core {
|
||||
pub clash: Arc<Mutex<Clash>>,
|
||||
pub verge: Arc<Mutex<Verge>>,
|
||||
pub profiles: Arc<Mutex<Profiles>>,
|
||||
pub service: Arc<Mutex<Service>>,
|
||||
pub sysopt: Arc<Mutex<Sysopt>>,
|
||||
pub timer: Arc<Mutex<Timer>>,
|
||||
pub runtime: Arc<Mutex<RuntimeResult>>,
|
||||
pub window: Arc<Mutex<Option<Window>>>,
|
||||
pub handle: Handle,
|
||||
}
|
||||
|
||||
impl Core {
|
||||
pub fn new() -> Core {
|
||||
Core {
|
||||
clash: Arc::new(Mutex::new(Clash::new())),
|
||||
verge: Arc::new(Mutex::new(Verge::new())),
|
||||
profiles: Arc::new(Mutex::new(Profiles::new())),
|
||||
service: Arc::new(Mutex::new(Service::new())),
|
||||
sysopt: Arc::new(Mutex::new(Sysopt::new())),
|
||||
timer: Arc::new(Mutex::new(Timer::new())),
|
||||
runtime: Arc::new(Mutex::new(RuntimeResult::default())),
|
||||
window: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
pub fn global() -> Core {
|
||||
CORE.clone()
|
||||
}
|
||||
|
||||
/// initialize the core state
|
||||
pub fn init(&self, app_handle: tauri::AppHandle) {
|
||||
pub fn init(&mut self, app_handle: tauri::AppHandle) {
|
||||
// kill old clash process
|
||||
Service::kill_old_clash();
|
||||
self.handle = Handle::from(Some(app_handle));
|
||||
|
||||
let verge = self.verge.lock();
|
||||
let clash_core = verge.clash_core.clone();
|
||||
|
||||
let mut service = self.service.lock();
|
||||
service.set_core(clash_core);
|
||||
|
||||
#[cfg(windows)]
|
||||
{
|
||||
let enable = verge.enable_service_mode.clone();
|
||||
service.set_mode(enable.unwrap_or(false));
|
||||
let mut service = self.service.lock();
|
||||
log_if_err!(service.start());
|
||||
}
|
||||
|
||||
log_if_err!(service.start());
|
||||
drop(verge);
|
||||
drop(service);
|
||||
|
||||
log_if_err!(self.activate());
|
||||
|
||||
let clash = self.clash.lock();
|
||||
let verge = self.verge.lock();
|
||||
{
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
log_if_err!(sysopt.init_launch());
|
||||
log_if_err!(sysopt.init_sysproxy());
|
||||
}
|
||||
|
||||
// let silent_start = verge.enable_silent_start.clone();
|
||||
let auto_launch = verge.enable_auto_launch.clone();
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
|
||||
sysopt.init_sysproxy(clash.info.port.clone(), &verge);
|
||||
|
||||
drop(clash);
|
||||
drop(verge);
|
||||
|
||||
log_if_err!(sysopt.init_launch(auto_launch));
|
||||
|
||||
log_if_err!(self.update_systray(&app_handle));
|
||||
log_if_err!(self.update_systray_clash(&app_handle));
|
||||
log_if_err!(self.handle.update_systray());
|
||||
log_if_err!(self.handle.update_systray_clash());
|
||||
|
||||
// timer initialize
|
||||
let mut timer = self.timer.lock();
|
||||
timer.set_core(self.clone());
|
||||
log_if_err!(timer.restore());
|
||||
}
|
||||
|
||||
/// save the window instance
|
||||
pub fn set_win(&self, win: Option<Window>) {
|
||||
let mut window = self.window.lock();
|
||||
*window = win;
|
||||
}
|
||||
|
||||
/// restart the clash sidecar
|
||||
pub fn restart_clash(&self) -> Result<()> {
|
||||
let mut service = self.service.lock();
|
||||
@@ -119,7 +82,8 @@ impl Core {
|
||||
bail!("invalid clash core name \"{clash_core}\"");
|
||||
}
|
||||
|
||||
let mut verge = self.verge.lock();
|
||||
let global = Data::global();
|
||||
let mut verge = global.verge.lock();
|
||||
verge.patch_config(Verge {
|
||||
clash_core: Some(clash_core.clone()),
|
||||
..Verge::default()
|
||||
@@ -127,10 +91,8 @@ impl Core {
|
||||
drop(verge);
|
||||
|
||||
let mut service = self.service.lock();
|
||||
service.stop()?;
|
||||
service.set_core(Some(clash_core));
|
||||
service.clear_logs();
|
||||
service.start()?;
|
||||
service.restart()?;
|
||||
drop(service);
|
||||
|
||||
self.activate()
|
||||
@@ -138,12 +100,13 @@ impl Core {
|
||||
|
||||
/// Patch Clash
|
||||
/// handle the clash config changed
|
||||
pub fn patch_clash(&self, patch: Mapping, app_handle: &AppHandle) -> Result<()> {
|
||||
pub fn patch_clash(&self, patch: Mapping) -> Result<()> {
|
||||
let has_port = patch.contains_key(&Value::from("mixed-port"));
|
||||
let has_mode = patch.contains_key(&Value::from("mode"));
|
||||
|
||||
let port = {
|
||||
let mut clash = self.clash.lock();
|
||||
let global = Data::global();
|
||||
let mut clash = global.clash.lock();
|
||||
clash.patch_config(patch)?;
|
||||
clash.info.port.clone()
|
||||
};
|
||||
@@ -157,175 +120,121 @@ impl Core {
|
||||
self.activate()?;
|
||||
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
let verge = self.verge.lock();
|
||||
sysopt.init_sysproxy(port, &verge);
|
||||
sysopt.init_sysproxy()?;
|
||||
}
|
||||
|
||||
if has_mode {
|
||||
self.update_systray_clash(app_handle)?;
|
||||
self.handle.update_systray_clash()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Patch Verge
|
||||
pub fn patch_verge(&self, patch: Verge, app_handle: &AppHandle) -> Result<()> {
|
||||
let tun_mode = patch.enable_tun_mode.clone();
|
||||
let auto_launch = patch.enable_auto_launch.clone();
|
||||
let system_proxy = patch.enable_system_proxy.clone();
|
||||
let proxy_bypass = patch.system_proxy_bypass.clone();
|
||||
let proxy_guard = patch.enable_proxy_guard.clone();
|
||||
pub fn patch_verge(&self, patch: Verge) -> Result<()> {
|
||||
// save the patch
|
||||
let global = Data::global();
|
||||
let mut verge = global.verge.lock();
|
||||
verge.patch_config(patch.clone())?;
|
||||
drop(verge);
|
||||
|
||||
#[cfg(windows)]
|
||||
let tun_mode = patch.enable_tun_mode;
|
||||
let auto_launch = patch.enable_auto_launch;
|
||||
let system_proxy = patch.enable_system_proxy;
|
||||
let proxy_bypass = patch.system_proxy_bypass;
|
||||
let proxy_guard = patch.enable_proxy_guard;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
let service_mode = patch.enable_service_mode.clone();
|
||||
let service_mode = patch.enable_service_mode;
|
||||
|
||||
// 重启服务
|
||||
if service_mode.is_some() {
|
||||
let service_mode = service_mode.unwrap();
|
||||
|
||||
let mut service = self.service.lock();
|
||||
service.stop()?;
|
||||
service.set_mode(service_mode);
|
||||
service.start()?;
|
||||
service.restart()?;
|
||||
drop(service);
|
||||
}
|
||||
|
||||
// self.activate_enhanced(false)?;
|
||||
if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) {
|
||||
let wintun_dll = crate::utils::dirs::app_home_dir().join("wintun.dll");
|
||||
if !wintun_dll.exists() {
|
||||
bail!("failed to enable TUN for missing `wintun.dll`");
|
||||
}
|
||||
}
|
||||
|
||||
if service_mode.is_some() || tun_mode.is_some() {
|
||||
self.activate()?;
|
||||
}
|
||||
}
|
||||
|
||||
if auto_launch.is_some() {
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
sysopt.update_launch(auto_launch)?;
|
||||
}
|
||||
|
||||
if system_proxy.is_some() || proxy_bypass.is_some() {
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
sysopt.update_sysproxy(system_proxy.clone(), proxy_bypass)?;
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
|
||||
if proxy_guard.unwrap_or(false) {
|
||||
let sysopt = self.sysopt.lock();
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
if tun_mode.is_some() && *tun_mode.as_ref().unwrap_or(&false) {
|
||||
let wintun_dll = crate::utils::dirs::app_home_dir().join("wintun.dll");
|
||||
if !wintun_dll.exists() {
|
||||
bail!("failed to enable TUN for missing `wintun.dll`");
|
||||
}
|
||||
}
|
||||
|
||||
// save the patch
|
||||
let mut verge = self.verge.lock();
|
||||
verge.patch_config(patch)?;
|
||||
drop(verge);
|
||||
|
||||
if system_proxy.is_some() || tun_mode.is_some() {
|
||||
self.update_systray(app_handle)?;
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
if tun_mode.is_some() {
|
||||
self.activate()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
let mut sysopt = self.sysopt.lock();
|
||||
|
||||
// update system tray state (clash config)
|
||||
pub fn update_systray_clash(&self, app_handle: &AppHandle) -> Result<()> {
|
||||
let clash = self.clash.lock();
|
||||
let mode = clash
|
||||
.config
|
||||
.get(&Value::from("mode"))
|
||||
.map(|val| val.as_str().unwrap_or("rule"))
|
||||
.unwrap_or("rule");
|
||||
if auto_launch.is_some() {
|
||||
sysopt.update_launch()?;
|
||||
}
|
||||
if system_proxy.is_some() || proxy_bypass.is_some() {
|
||||
sysopt.update_sysproxy()?;
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
if proxy_guard.unwrap_or(false) {
|
||||
sysopt.guard_proxy();
|
||||
}
|
||||
|
||||
let tray = app_handle.tray_handle();
|
||||
|
||||
tray.get_item("rule_mode").set_selected(mode == "rule")?;
|
||||
tray
|
||||
.get_item("global_mode")
|
||||
.set_selected(mode == "global")?;
|
||||
tray
|
||||
.get_item("direct_mode")
|
||||
.set_selected(mode == "direct")?;
|
||||
tray
|
||||
.get_item("script_mode")
|
||||
.set_selected(mode == "script")?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// update the system tray state (verge config)
|
||||
pub fn update_systray(&self, app_handle: &AppHandle) -> Result<()> {
|
||||
let verge = self.verge.lock();
|
||||
let tray = app_handle.tray_handle();
|
||||
|
||||
let system_proxy = verge.enable_system_proxy.as_ref();
|
||||
let tun_mode = verge.enable_tun_mode.as_ref();
|
||||
|
||||
tray
|
||||
.get_item("system_proxy")
|
||||
.set_selected(*system_proxy.unwrap_or(&false))?;
|
||||
tray
|
||||
.get_item("tun_mode")
|
||||
.set_selected(*tun_mode.unwrap_or(&false))?;
|
||||
|
||||
// update verge config
|
||||
let window = app_handle.get_window("main");
|
||||
let notice = Notice::from(window);
|
||||
notice.refresh_verge();
|
||||
if system_proxy.is_some() || tun_mode.is_some() {
|
||||
self.handle.update_systray()?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// update rule/global/direct/script mode
|
||||
pub fn update_mode(&self, app_handle: &AppHandle, mode: &str) -> Result<()> {
|
||||
pub fn update_mode(&self, mode: &str) -> Result<()> {
|
||||
// save config to file
|
||||
let mut clash = self.clash.lock();
|
||||
clash.config.insert(Value::from("mode"), Value::from(mode));
|
||||
clash.save_config()?;
|
||||
|
||||
let info = clash.info.clone();
|
||||
drop(clash);
|
||||
|
||||
let notice = {
|
||||
let window = self.window.lock();
|
||||
Notice::from(window.clone())
|
||||
let info = {
|
||||
let global = Data::global();
|
||||
let mut clash = global.clash.lock();
|
||||
clash.config.insert(Value::from("mode"), Value::from(mode));
|
||||
clash.save_config()?;
|
||||
clash.info.clone()
|
||||
};
|
||||
|
||||
let mut mapping = Mapping::new();
|
||||
mapping.insert(Value::from("mode"), Value::from(mode));
|
||||
|
||||
let service = self.service.lock();
|
||||
service.patch_config(info, mapping, notice)?;
|
||||
tauri::async_runtime::spawn(async move {
|
||||
log_if_err!(Service::patch_config(info, mapping.to_owned()).await);
|
||||
});
|
||||
|
||||
// update tray
|
||||
self.update_systray_clash(app_handle)?;
|
||||
self.handle.update_systray_clash()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// activate the profile
|
||||
/// auto activate enhanced profile
|
||||
/// 触发clash配置更新
|
||||
pub fn activate(&self) -> Result<()> {
|
||||
let profile_activate = {
|
||||
let profiles = self.profiles.lock();
|
||||
profiles.gen_activate()?
|
||||
};
|
||||
let global = Data::global();
|
||||
|
||||
let (clash_config, clash_info) = {
|
||||
let clash = self.clash.lock();
|
||||
(clash.config.clone(), clash.info.clone())
|
||||
};
|
||||
let verge = global.verge.lock();
|
||||
let clash = global.clash.lock();
|
||||
let profiles = global.profiles.lock();
|
||||
|
||||
let tun_mode = {
|
||||
let verge = self.verge.lock();
|
||||
verge.enable_tun_mode.unwrap_or(false)
|
||||
};
|
||||
let tun_mode = verge.enable_tun_mode.clone().unwrap_or(false);
|
||||
let profile_activate = profiles.gen_activate()?;
|
||||
|
||||
let clash_config = clash.config.clone();
|
||||
let clash_info = clash.info.clone();
|
||||
|
||||
drop(clash);
|
||||
drop(verge);
|
||||
drop(profiles);
|
||||
|
||||
let (config, exists_keys, logs) = enhance_config(
|
||||
clash_config,
|
||||
@@ -336,25 +245,36 @@ impl Core {
|
||||
);
|
||||
|
||||
let mut runtime = self.runtime.lock();
|
||||
runtime.config = Some(config.clone());
|
||||
runtime.config_yaml = Some(serde_yaml::to_string(&config).unwrap_or("".into()));
|
||||
runtime.exists_keys = exists_keys;
|
||||
runtime.chain_logs = logs;
|
||||
|
||||
let notice = {
|
||||
let window = self.window.lock();
|
||||
Notice::from(window.clone())
|
||||
*runtime = RuntimeResult {
|
||||
config: Some(config.clone()),
|
||||
config_yaml: Some(serde_yaml::to_string(&config).unwrap_or("".into())),
|
||||
exists_keys,
|
||||
chain_logs: logs,
|
||||
};
|
||||
drop(runtime);
|
||||
|
||||
let service = self.service.lock();
|
||||
service.set_config(clash_info, config, notice)
|
||||
let mut service = self.service.lock();
|
||||
service.check_start()?;
|
||||
drop(service);
|
||||
|
||||
let handle = self.handle.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
match Service::set_config(clash_info, config).await {
|
||||
Ok(_) => handle.refresh_clash(),
|
||||
Err(err) => log::error!(target: "app", "{err}"),
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Static function
|
||||
/// update profile item
|
||||
pub async fn update_profile_item(&self, uid: String, option: Option<PrfOption>) -> Result<()> {
|
||||
let global = Data::global();
|
||||
|
||||
let (url, opt) = {
|
||||
let profiles = self.profiles.lock();
|
||||
let profiles = global.profiles.lock();
|
||||
let item = profiles.get_item(&uid)?;
|
||||
|
||||
if let Some(typ) = item.itype.as_ref() {
|
||||
@@ -363,7 +283,7 @@ impl Core {
|
||||
// reactivate the config
|
||||
if Some(uid) == profiles.get_current() {
|
||||
drop(profiles);
|
||||
return self.activate();
|
||||
self.activate()?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
@@ -377,7 +297,7 @@ impl Core {
|
||||
let merged_opt = PrfOption::merge(opt, option);
|
||||
let item = PrfItem::from_url(&url, None, None, merged_opt).await?;
|
||||
|
||||
let mut profiles = self.profiles.lock();
|
||||
let mut profiles = global.profiles.lock();
|
||||
profiles.update_item(uid.clone(), item)?;
|
||||
|
||||
// reactivate the profile
|
||||
|
||||
Reference in New Issue
Block a user