Files
clash-proxy/src-tauri/src/core/mod.rs

350 lines
9.0 KiB
Rust
Raw Normal View History

2022-09-11 20:58:55 +08:00
use self::handle::Handle;
2022-09-14 01:19:02 +08:00
use self::hotkey::Hotkey;
2022-04-20 01:44:47 +08:00
use self::sysopt::Sysopt;
2022-04-20 20:37:16 +08:00
use self::timer::Timer;
2022-08-12 03:20:55 +08:00
use crate::config::enhance_config;
2022-09-11 20:58:55 +08:00
use crate::data::*;
2022-04-19 01:41:20 +08:00
use crate::log_if_err;
use anyhow::{bail, Result};
2022-10-28 01:02:47 +08:00
use once_cell::sync::OnceCell;
2022-04-19 14:38:59 +08:00
use parking_lot::Mutex;
2022-09-11 20:58:55 +08:00
use serde_yaml::{Mapping, Value};
2022-04-19 14:38:59 +08:00
use std::sync::Arc;
2022-04-19 01:41:20 +08:00
2022-09-11 20:58:55 +08:00
mod handle;
2022-09-14 01:19:02 +08:00
mod hotkey;
2022-04-19 01:41:20 +08:00
mod service;
2022-04-20 01:44:47 +08:00
mod sysopt;
2022-04-19 01:41:20 +08:00
mod timer;
2022-10-28 00:40:29 +08:00
pub mod tray;
2021-12-14 00:40:41 +08:00
2022-04-24 21:00:17 +08:00
pub use self::service::*;
2022-09-11 20:58:55 +08:00
2022-04-19 01:41:20 +08:00
#[derive(Clone)]
pub struct Core {
2022-07-13 00:54:47 +08:00
pub service: Arc<Mutex<Service>>,
pub sysopt: Arc<Mutex<Sysopt>>,
pub timer: Arc<Mutex<Timer>>,
2022-09-14 01:19:02 +08:00
pub hotkey: Arc<Mutex<Hotkey>>,
2022-08-12 03:20:55 +08:00
pub runtime: Arc<Mutex<RuntimeResult>>,
2022-09-12 00:45:19 +08:00
pub handle: Arc<Mutex<Handle>>,
2022-04-19 01:41:20 +08:00
}
impl Core {
2022-10-28 01:02:47 +08:00
pub fn global() -> &'static Core {
static CORE: OnceCell<Core> = OnceCell::new();
CORE.get_or_init(|| Core {
service: Arc::new(Mutex::new(Service::new())),
sysopt: Arc::new(Mutex::new(Sysopt::new())),
timer: Arc::new(Mutex::new(Timer::new())),
hotkey: Arc::new(Mutex::new(Hotkey::new())),
runtime: Arc::new(Mutex::new(RuntimeResult::default())),
handle: Arc::new(Mutex::new(Handle::default())),
})
2022-07-13 00:54:47 +08:00
}
2022-04-24 21:00:17 +08:00
2022-07-13 00:54:47 +08:00
/// initialize the core state
2022-09-12 00:45:19 +08:00
pub fn init(&self, app_handle: tauri::AppHandle) {
2022-09-05 16:30:29 +08:00
// kill old clash process
Service::kill_old_clash();
2022-09-12 00:45:19 +08:00
2022-09-14 01:19:02 +08:00
let mut handle = self.handle.lock();
handle.set_inner(app_handle.clone());
2022-09-14 01:19:02 +08:00
drop(handle);
2022-09-05 16:30:29 +08:00
2022-09-14 01:19:02 +08:00
let mut service = self.service.lock();
log_if_err!(service.start());
drop(service);
2022-04-19 01:41:20 +08:00
2022-07-13 00:54:47 +08:00
log_if_err!(self.activate());
2022-04-19 14:38:59 +08:00
2022-09-14 01:19:02 +08:00
let mut sysopt = self.sysopt.lock();
log_if_err!(sysopt.init_launch());
log_if_err!(sysopt.init_sysproxy());
drop(sysopt);
let handle = self.handle.lock();
2022-10-28 00:40:29 +08:00
log_if_err!(handle.update_systray_part());
2022-09-14 01:19:02 +08:00
drop(handle);
2022-04-19 01:41:20 +08:00
2022-09-14 01:19:02 +08:00
let mut hotkey = self.hotkey.lock();
log_if_err!(hotkey.init(app_handle));
2022-09-14 01:19:02 +08:00
drop(hotkey);
2022-04-19 01:41:20 +08:00
2022-07-13 00:54:47 +08:00
// timer initialize
let mut timer = self.timer.lock();
2022-08-21 13:33:12 +08:00
log_if_err!(timer.restore());
2022-07-13 00:54:47 +08:00
}
/// restart the clash sidecar
pub fn restart_clash(&self) -> Result<()> {
let mut service = self.service.lock();
service.restart()?;
drop(service);
2022-08-11 02:55:10 +08:00
self.activate()
2022-07-13 00:54:47 +08:00
}
2022-05-17 01:59:49 +08:00
2022-07-13 00:54:47 +08:00
/// change the clash core
pub fn change_core(&self, clash_core: Option<String>) -> Result<()> {
let clash_core = clash_core.unwrap_or("clash".into());
if &clash_core != "clash" && &clash_core != "clash-meta" {
bail!("invalid clash core name \"{clash_core}\"");
2022-04-20 01:44:47 +08:00
}
2022-09-11 20:58:55 +08:00
let global = Data::global();
let mut verge = global.verge.lock();
2022-07-13 00:54:47 +08:00
verge.patch_config(Verge {
clash_core: Some(clash_core.clone()),
..Verge::default()
})?;
drop(verge);
let mut service = self.service.lock();
2022-09-05 20:30:39 +08:00
service.clear_logs();
2022-09-11 20:58:55 +08:00
service.restart()?;
2022-07-13 00:54:47 +08:00
drop(service);
2022-08-11 02:55:10 +08:00
self.activate()
2022-07-13 00:54:47 +08:00
}
/// Patch Clash
/// handle the clash config changed
2022-09-11 20:58:55 +08:00
pub fn patch_clash(&self, patch: Mapping) -> Result<()> {
let patch_cloned = patch.clone();
let clash_mode = patch.get("mode");
let mixed_port = patch.get("mixed-port");
let external = patch.get("external-controller");
let secret = patch.get("secret");
2022-08-15 20:21:43 +08:00
let valid_port = {
2022-09-11 20:58:55 +08:00
let global = Data::global();
let mut clash = global.clash.lock();
clash.patch_config(patch_cloned)?;
clash.info.port.is_some()
2022-07-13 00:54:47 +08:00
};
// todo: port check
if (mixed_port.is_some() && valid_port) || external.is_some() || secret.is_some() {
2022-07-13 00:54:47 +08:00
let mut service = self.service.lock();
service.restart()?;
drop(service);
self.activate()?;
let mut sysopt = self.sysopt.lock();
2022-09-11 20:58:55 +08:00
sysopt.init_sysproxy()?;
2022-07-13 00:54:47 +08:00
}
2022-04-20 01:44:47 +08:00
if clash_mode.is_some() {
2022-09-12 00:45:19 +08:00
let handle = self.handle.lock();
2022-10-28 00:40:29 +08:00
handle.update_systray_part()?;
}
2022-04-20 01:44:47 +08:00
2022-07-13 00:54:47 +08:00
Ok(())
}
2022-04-24 21:00:17 +08:00
2022-07-13 00:54:47 +08:00
/// Patch Verge
2022-09-11 20:58:55 +08:00
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);
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;
2022-10-28 00:40:29 +08:00
let language = patch.language;
2022-09-11 20:58:55 +08:00
#[cfg(target_os = "windows")]
2022-07-13 00:54:47 +08:00
{
2022-09-11 20:58:55 +08:00
let service_mode = patch.enable_service_mode;
2022-07-13 00:54:47 +08:00
2022-09-11 20:58:55 +08:00
// 重启服务
2022-07-13 00:54:47 +08:00
if service_mode.is_some() {
2022-04-24 21:00:17 +08:00
let mut service = self.service.lock();
2022-09-11 20:58:55 +08:00
service.restart()?;
2022-04-24 21:00:17 +08:00
drop(service);
2022-09-11 20:58:55 +08:00
}
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`");
}
}
2022-04-24 21:00:17 +08:00
2022-09-11 20:58:55 +08:00
if service_mode.is_some() || tun_mode.is_some() {
2022-08-11 02:55:10 +08:00
self.activate()?;
2022-07-13 00:54:47 +08:00
}
2022-04-24 21:00:17 +08:00
}
2022-09-11 20:58:55 +08:00
#[cfg(not(target_os = "windows"))]
if tun_mode.is_some() {
self.activate()?;
2022-04-20 01:44:47 +08:00
}
2022-09-11 20:58:55 +08:00
let mut sysopt = self.sysopt.lock();
if auto_launch.is_some() {
sysopt.update_launch()?;
}
2022-07-13 00:54:47 +08:00
if system_proxy.is_some() || proxy_bypass.is_some() {
2022-09-11 20:58:55 +08:00
sysopt.update_sysproxy()?;
2022-07-13 00:54:47 +08:00
sysopt.guard_proxy();
}
if proxy_guard.unwrap_or(false) {
sysopt.guard_proxy();
}
2022-10-28 00:40:29 +08:00
// 更新tray
if language.is_some() {
2022-09-12 00:45:19 +08:00
let handle = self.handle.lock();
handle.update_systray()?;
2022-10-28 00:40:29 +08:00
} else if system_proxy.is_some() || tun_mode.is_some() {
let handle = self.handle.lock();
handle.update_systray_part()?;
2022-07-13 00:54:47 +08:00
}
2022-09-18 15:50:03 +08:00
if patch.hotkeys.is_some() {
let mut hotkey = self.hotkey.lock();
hotkey.update(patch.hotkeys.unwrap())?;
}
2022-07-13 00:54:47 +08:00
Ok(())
}
2022-07-13 02:26:54 +08:00
// update rule/global/direct/script mode
2022-09-11 20:58:55 +08:00
pub fn update_mode(&self, mode: &str) -> Result<()> {
2022-07-13 02:26:54 +08:00
// save config to file
2022-09-11 20:58:55 +08:00
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()
2022-07-13 02:26:54 +08:00
};
let mut mapping = Mapping::new();
mapping.insert(Value::from("mode"), Value::from(mode));
2022-09-24 14:01:28 +08:00
let handle = self.handle.clone();
2022-09-11 20:58:55 +08:00
tauri::async_runtime::spawn(async move {
log_if_err!(Service::patch_config(info, mapping.to_owned()).await);
2022-07-13 02:26:54 +08:00
2022-09-24 14:01:28 +08:00
// update tray
let handle = handle.lock();
handle.refresh_clash();
2022-10-28 00:40:29 +08:00
log_if_err!(handle.update_systray_part());
2022-09-24 14:01:28 +08:00
});
2022-07-13 02:26:54 +08:00
Ok(())
}
2022-07-13 00:54:47 +08:00
/// activate the profile
/// auto activate enhanced profile
2022-09-11 20:58:55 +08:00
/// 触发clash配置更新
2022-07-13 00:54:47 +08:00
pub fn activate(&self) -> Result<()> {
2022-09-11 20:58:55 +08:00
let global = Data::global();
2022-08-12 03:20:55 +08:00
2022-09-11 20:58:55 +08:00
let verge = global.verge.lock();
let clash = global.clash.lock();
let profiles = global.profiles.lock();
2022-08-11 02:55:10 +08:00
2022-09-11 20:58:55 +08:00
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);
2022-07-13 00:54:47 +08:00
2022-08-12 03:20:55 +08:00
let (config, exists_keys, logs) = enhance_config(
2022-08-11 02:55:10 +08:00
clash_config,
2022-08-12 03:20:55 +08:00
profile_activate.current,
profile_activate.chain,
profile_activate.valid,
2022-08-11 03:26:08 +08:00
tun_mode,
2022-08-11 02:55:10 +08:00
);
2022-07-13 00:54:47 +08:00
2022-08-12 03:20:55 +08:00
let mut runtime = self.runtime.lock();
2022-09-11 20:58:55 +08:00
*runtime = RuntimeResult {
config: Some(config.clone()),
config_yaml: Some(serde_yaml::to_string(&config).unwrap_or("".into())),
exists_keys,
chain_logs: logs,
2022-07-13 00:54:47 +08:00
};
2022-09-11 20:58:55 +08:00
drop(runtime);
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 {
2022-09-12 00:45:19 +08:00
Ok(_) => {
let handle = handle.lock();
2022-09-26 20:46:29 +08:00
handle.refresh_clash();
handle.notice_message("set_config::ok".into(), "ok".into());
}
Err(err) => {
let handle = handle.lock();
handle.notice_message("set_config::error".into(), format!("{err}"));
2022-11-10 22:58:46 +08:00
log::error!(target: "app", "{err}")
2022-09-12 00:45:19 +08:00
}
2022-09-11 20:58:55 +08:00
}
});
2022-07-13 00:54:47 +08:00
2022-09-11 20:58:55 +08:00
Ok(())
2022-07-13 00:54:47 +08:00
}
/// Static function
/// update profile item
2022-08-12 03:20:55 +08:00
pub async fn update_profile_item(&self, uid: String, option: Option<PrfOption>) -> Result<()> {
2022-09-11 20:58:55 +08:00
let global = Data::global();
2022-07-13 00:54:47 +08:00
let (url, opt) = {
2022-09-11 20:58:55 +08:00
let profiles = global.profiles.lock();
2022-07-13 00:54:47 +08:00
let item = profiles.get_item(&uid)?;
if let Some(typ) = item.itype.as_ref() {
// maybe only valid for `local` profile
if *typ != "remote" {
// reactivate the config
if Some(uid) == profiles.get_current() {
drop(profiles);
2022-09-11 20:58:55 +08:00
self.activate()?;
2022-07-13 00:54:47 +08:00
}
return Ok(());
}
}
if item.url.is_none() {
bail!("failed to get the profile item url");
}
(item.url.clone().unwrap(), item.option.clone())
};
2022-07-13 00:54:47 +08:00
let merged_opt = PrfOption::merge(opt, option);
let item = PrfItem::from_url(&url, None, None, merged_opt).await?;
2022-04-20 20:37:16 +08:00
2022-09-11 20:58:55 +08:00
let mut profiles = global.profiles.lock();
2022-07-13 00:54:47 +08:00
profiles.update_item(uid.clone(), item)?;
2022-07-13 00:54:47 +08:00
// reactivate the profile
if Some(uid) == profiles.get_current() {
drop(profiles);
2022-08-12 03:20:55 +08:00
self.activate()?;
}
2022-07-13 00:54:47 +08:00
Ok(())
}
2022-04-20 20:37:16 +08:00
}