2024-06-19 10:04:28 +08:00
|
|
|
|
use crate::config::*;
|
|
|
|
|
|
use crate::core::{clash_api, handle, logger::Logger, service};
|
2022-11-18 18:18:41 +08:00
|
|
|
|
use crate::log_err;
|
2024-06-19 10:04:28 +08:00
|
|
|
|
use crate::utils::dirs;
|
2024-06-18 12:09:54 +08:00
|
|
|
|
use anyhow::{bail, Result};
|
2022-11-14 01:26:33 +08:00
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
|
|
use parking_lot::Mutex;
|
2024-05-12 11:06:44 +08:00
|
|
|
|
use serde_yaml::Mapping;
|
2024-06-18 12:09:54 +08:00
|
|
|
|
use std::{sync::Arc, time::Duration};
|
|
|
|
|
|
use sysinfo::System;
|
2022-11-14 01:26:33 +08:00
|
|
|
|
use tauri::api::process::{Command, CommandChild, CommandEvent};
|
|
|
|
|
|
use tokio::time::sleep;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
pub struct CoreManager {
|
|
|
|
|
|
sidecar: Arc<Mutex<Option<CommandChild>>>,
|
|
|
|
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
|
use_service_mode: Arc<Mutex<bool>>,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl CoreManager {
|
|
|
|
|
|
pub fn global() -> &'static CoreManager {
|
|
|
|
|
|
static CORE_MANAGER: OnceCell<CoreManager> = OnceCell::new();
|
|
|
|
|
|
|
|
|
|
|
|
CORE_MANAGER.get_or_init(|| CoreManager {
|
|
|
|
|
|
sidecar: Arc::new(Mutex::new(None)),
|
|
|
|
|
|
use_service_mode: Arc::new(Mutex::new(false)),
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn init(&self) -> Result<()> {
|
|
|
|
|
|
tauri::async_runtime::spawn(async {
|
2022-11-17 20:19:40 +08:00
|
|
|
|
// 启动clash
|
2022-11-18 18:18:41 +08:00
|
|
|
|
log_err!(Self::global().run_core().await);
|
2022-11-14 01:26:33 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-22 02:56:47 +08:00
|
|
|
|
/// 检查订阅是否正确
|
2022-11-14 01:26:33 +08:00
|
|
|
|
pub fn check_config(&self) -> Result<()> {
|
2022-11-18 18:18:41 +08:00
|
|
|
|
let config_path = Config::generate_file(ConfigType::Check)?;
|
2022-11-14 01:26:33 +08:00
|
|
|
|
let config_path = dirs::path_to_str(&config_path)?;
|
|
|
|
|
|
|
2022-11-16 01:26:41 +08:00
|
|
|
|
let clash_core = { Config::verge().latest().clash_core.clone() };
|
|
|
|
|
|
let clash_core = clash_core.unwrap_or("clash".into());
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2022-11-21 23:11:56 +08:00
|
|
|
|
let app_dir = dirs::app_home_dir()?;
|
|
|
|
|
|
let app_dir = dirs::path_to_str(&app_dir)?;
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
let output = Command::new_sidecar(clash_core)?
|
2022-11-21 23:11:56 +08:00
|
|
|
|
.args(["-t", "-d", app_dir, "-f", config_path])
|
2022-11-14 01:26:33 +08:00
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
|
|
|
|
if !output.status.success() {
|
2022-11-18 22:08:06 +08:00
|
|
|
|
let error = clash_api::parse_check_output(output.stdout.clone());
|
2024-01-10 17:36:35 +08:00
|
|
|
|
let error = match !error.is_empty() {
|
2023-05-28 17:35:00 +08:00
|
|
|
|
true => error,
|
|
|
|
|
|
false => output.stdout.clone(),
|
|
|
|
|
|
};
|
2022-11-18 22:08:06 +08:00
|
|
|
|
Logger::global().set_log(output.stdout);
|
|
|
|
|
|
bail!("{error}");
|
2022-11-14 01:26:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 启动核心
|
2022-11-17 20:19:40 +08:00
|
|
|
|
pub async fn run_core(&self) -> Result<()> {
|
2022-11-18 18:18:41 +08:00
|
|
|
|
let config_path = Config::generate_file(ConfigType::Run)?;
|
|
|
|
|
|
|
2022-11-22 23:02:18 +08:00
|
|
|
|
#[allow(unused_mut)]
|
2022-11-22 22:01:34 +08:00
|
|
|
|
let mut should_kill = match self.sidecar.lock().take() {
|
2024-06-19 10:04:28 +08:00
|
|
|
|
Some(_) => true,
|
2022-11-18 22:08:06 +08:00
|
|
|
|
None => false,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-06-19 10:43:58 +08:00
|
|
|
|
// 关闭tun模式
|
|
|
|
|
|
let mut disable = Mapping::new();
|
|
|
|
|
|
let mut tun = Mapping::new();
|
|
|
|
|
|
tun.insert("enable".into(), false.into());
|
|
|
|
|
|
disable.insert("tun".into(), tun.into());
|
|
|
|
|
|
log::debug!(target: "app", "disable tun mode");
|
|
|
|
|
|
let _ = clash_api::patch_configs(&disable).await;
|
|
|
|
|
|
|
|
|
|
|
|
let mut system = System::new();
|
|
|
|
|
|
system.refresh_all();
|
|
|
|
|
|
let procs = system.processes_by_name("verge-mihomo");
|
|
|
|
|
|
for proc in procs {
|
|
|
|
|
|
log::debug!(target: "app", "kill all clash process");
|
|
|
|
|
|
proc.kill();
|
|
|
|
|
|
}
|
2024-06-18 12:09:54 +08:00
|
|
|
|
|
2022-11-22 22:01:34 +08:00
|
|
|
|
if *self.use_service_mode.lock() {
|
|
|
|
|
|
log::debug!(target: "app", "stop the core by service");
|
2024-03-31 21:44:34 +08:00
|
|
|
|
log_err!(service::stop_core_by_service().await);
|
2022-11-22 22:01:34 +08:00
|
|
|
|
should_kill = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-18 22:08:06 +08:00
|
|
|
|
// 这里得等一会儿
|
|
|
|
|
|
if should_kill {
|
|
|
|
|
|
sleep(Duration::from_millis(500)).await;
|
2022-11-18 20:15:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-31 21:44:34 +08:00
|
|
|
|
// 服务模式
|
|
|
|
|
|
let enable = { Config::verge().latest().enable_service_mode };
|
|
|
|
|
|
let enable = enable.unwrap_or(false);
|
|
|
|
|
|
|
|
|
|
|
|
*self.use_service_mode.lock() = enable;
|
|
|
|
|
|
|
|
|
|
|
|
if enable {
|
|
|
|
|
|
// 服务模式启动失败就直接运行sidecar
|
|
|
|
|
|
log::debug!(target: "app", "try to run core in service mode");
|
|
|
|
|
|
|
2024-06-12 10:00:22 +08:00
|
|
|
|
let res = async {
|
2024-03-31 21:44:34 +08:00
|
|
|
|
service::check_service().await?;
|
|
|
|
|
|
service::run_core_by_service(&config_path).await
|
2024-06-12 10:00:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
.await;
|
|
|
|
|
|
match res {
|
2024-03-31 21:44:34 +08:00
|
|
|
|
Ok(_) => return Ok(()),
|
|
|
|
|
|
Err(err) => {
|
|
|
|
|
|
// 修改这个值,免得stop出错
|
|
|
|
|
|
*self.use_service_mode.lock() = false;
|
|
|
|
|
|
log::error!(target: "app", "{err}");
|
2022-11-17 20:19:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2022-11-18 09:35:05 +08:00
|
|
|
|
let app_dir = dirs::app_home_dir()?;
|
2022-11-14 01:26:33 +08:00
|
|
|
|
let app_dir = dirs::path_to_str(&app_dir)?;
|
|
|
|
|
|
|
2022-11-16 01:26:41 +08:00
|
|
|
|
let clash_core = { Config::verge().latest().clash_core.clone() };
|
2024-06-19 10:04:28 +08:00
|
|
|
|
let mut clash_core = clash_core.unwrap_or("verge-mihomo".into());
|
|
|
|
|
|
|
|
|
|
|
|
// compatibility
|
|
|
|
|
|
if clash_core.contains("clash") {
|
|
|
|
|
|
clash_core = "verge-mihomo".to_string();
|
|
|
|
|
|
Config::verge().draft().patch_config(IVerge {
|
|
|
|
|
|
clash_core: Some("verge-mihomo".to_string()),
|
|
|
|
|
|
..IVerge::default()
|
|
|
|
|
|
});
|
|
|
|
|
|
Config::verge().apply();
|
|
|
|
|
|
match Config::verge().data().save_file() {
|
|
|
|
|
|
Ok(_) => handle::Handle::refresh_verge(),
|
|
|
|
|
|
Err(err) => log::error!(target: "app", "{err}"),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2022-11-18 18:18:41 +08:00
|
|
|
|
let config_path = dirs::path_to_str(&config_path)?;
|
|
|
|
|
|
|
2024-06-19 10:04:28 +08:00
|
|
|
|
let args = vec!["-d", app_dir, "-f", config_path];
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
|
|
|
|
|
let cmd = Command::new_sidecar(clash_core)?;
|
|
|
|
|
|
let (mut rx, cmd_child) = cmd.args(args).spawn()?;
|
|
|
|
|
|
|
2022-11-18 20:15:34 +08:00
|
|
|
|
let mut sidecar = self.sidecar.lock();
|
2022-11-14 01:26:33 +08:00
|
|
|
|
*sidecar = Some(cmd_child);
|
2023-01-14 11:45:47 +08:00
|
|
|
|
drop(sidecar);
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
while let Some(event) = rx.recv().await {
|
|
|
|
|
|
match event {
|
|
|
|
|
|
CommandEvent::Stdout(line) => {
|
2024-06-19 10:04:28 +08:00
|
|
|
|
log::info!(target: "app", "[mihomo]: {line}");
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Logger::global().set_log(line);
|
|
|
|
|
|
}
|
|
|
|
|
|
CommandEvent::Stderr(err) => {
|
2024-06-19 10:04:28 +08:00
|
|
|
|
log::error!(target: "app", "[mihomo]: {err}");
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Logger::global().set_log(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
CommandEvent::Error(err) => {
|
2024-06-19 10:04:28 +08:00
|
|
|
|
log::error!(target: "app", "[mihomo]: {err}");
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Logger::global().set_log(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
CommandEvent::Terminated(_) => {
|
2024-06-19 10:04:28 +08:00
|
|
|
|
log::info!(target: "app", "mihomo core terminated");
|
2023-01-14 11:45:47 +08:00
|
|
|
|
let _ = CoreManager::global().recover_core();
|
2022-11-14 01:26:33 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-14 11:45:47 +08:00
|
|
|
|
/// 重启内核
|
|
|
|
|
|
pub fn recover_core(&'static self) -> Result<()> {
|
|
|
|
|
|
// 服务模式不管
|
|
|
|
|
|
if *self.use_service_mode.lock() {
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清空原来的sidecar值
|
2024-06-19 10:04:28 +08:00
|
|
|
|
let _ = self.sidecar.lock().take();
|
2023-01-14 11:45:47 +08:00
|
|
|
|
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
// 6秒之后再查看服务是否正常 (时间随便搞的)
|
|
|
|
|
|
// terminated 可能是切换内核 (切换内核已经有500ms的延迟)
|
|
|
|
|
|
sleep(Duration::from_millis(6666)).await;
|
|
|
|
|
|
|
|
|
|
|
|
if self.sidecar.lock().is_none() {
|
|
|
|
|
|
log::info!(target: "app", "recover clash core");
|
|
|
|
|
|
|
|
|
|
|
|
// 重新启动app
|
2023-01-15 19:11:51 +08:00
|
|
|
|
if let Err(err) = self.run_core().await {
|
|
|
|
|
|
log::error!(target: "app", "failed to recover clash core");
|
|
|
|
|
|
log::error!(target: "app", "{err}");
|
|
|
|
|
|
|
2023-01-14 11:45:47 +08:00
|
|
|
|
let _ = self.recover_core();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
/// 停止核心运行
|
2024-06-29 19:02:37 +08:00
|
|
|
|
pub async fn stop_core(&self) -> Result<()> {
|
2024-05-12 11:06:44 +08:00
|
|
|
|
// 关闭tun模式
|
2024-06-29 19:02:37 +08:00
|
|
|
|
let mut disable = Mapping::new();
|
|
|
|
|
|
let mut tun = Mapping::new();
|
|
|
|
|
|
tun.insert("enable".into(), false.into());
|
|
|
|
|
|
disable.insert("tun".into(), tun.into());
|
|
|
|
|
|
log::debug!(target: "app", "disable tun mode");
|
|
|
|
|
|
let _ = clash_api::patch_configs(&disable).await;
|
2024-05-12 11:06:44 +08:00
|
|
|
|
|
2022-11-17 20:19:40 +08:00
|
|
|
|
if *self.use_service_mode.lock() {
|
2022-11-22 22:01:34 +08:00
|
|
|
|
log::debug!(target: "app", "stop the core by service");
|
2024-06-29 19:02:37 +08:00
|
|
|
|
log_err!(service::stop_core_by_service().await);
|
2022-11-17 20:19:40 +08:00
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
2024-03-11 14:55:00 +08:00
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
let mut sidecar = self.sidecar.lock();
|
2024-06-19 10:04:28 +08:00
|
|
|
|
let _ = sidecar.take();
|
|
|
|
|
|
|
2024-06-18 12:09:54 +08:00
|
|
|
|
let mut system = System::new();
|
|
|
|
|
|
system.refresh_all();
|
2024-06-19 10:04:28 +08:00
|
|
|
|
let procs = system.processes_by_name("verge-mihomo");
|
2024-06-18 12:09:54 +08:00
|
|
|
|
for proc in procs {
|
|
|
|
|
|
log::debug!(target: "app", "kill all clash process");
|
|
|
|
|
|
proc.kill();
|
|
|
|
|
|
}
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 切换核心
|
|
|
|
|
|
pub async fn change_core(&self, clash_core: Option<String>) -> Result<()> {
|
|
|
|
|
|
let clash_core = clash_core.ok_or(anyhow::anyhow!("clash core is null"))?;
|
2024-06-19 10:04:28 +08:00
|
|
|
|
const CLASH_CORES: [&str; 2] = ["verge-mihomo", "verge-mihomo-alpha"];
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2023-12-03 14:26:03 +08:00
|
|
|
|
if !CLASH_CORES.contains(&clash_core.as_str()) {
|
2022-11-14 01:26:33 +08:00
|
|
|
|
bail!("invalid clash core name \"{clash_core}\"");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-18 20:15:34 +08:00
|
|
|
|
log::debug!(target: "app", "change core to `{clash_core}`");
|
|
|
|
|
|
|
2022-11-18 18:18:41 +08:00
|
|
|
|
Config::verge().draft().clash_core = Some(clash_core);
|
|
|
|
|
|
|
2023-11-22 02:56:47 +08:00
|
|
|
|
// 更新订阅
|
2024-06-29 19:02:37 +08:00
|
|
|
|
Config::generate().await?;
|
2022-11-23 09:57:21 +08:00
|
|
|
|
|
2022-11-21 22:27:55 +08:00
|
|
|
|
self.check_config()?;
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
// 清掉旧日志
|
|
|
|
|
|
Logger::global().clear_log();
|
|
|
|
|
|
|
2022-11-17 20:19:40 +08:00
|
|
|
|
match self.run_core().await {
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Ok(_) => {
|
2022-11-18 18:18:41 +08:00
|
|
|
|
Config::verge().apply();
|
|
|
|
|
|
Config::runtime().apply();
|
|
|
|
|
|
log_err!(Config::verge().latest().save_file());
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(err) => {
|
2022-11-16 01:26:41 +08:00
|
|
|
|
Config::verge().discard();
|
2022-11-18 18:18:41 +08:00
|
|
|
|
Config::runtime().discard();
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Err(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-18 18:18:41 +08:00
|
|
|
|
/// 更新proxies那些
|
|
|
|
|
|
/// 如果涉及端口和外部控制则需要重启
|
|
|
|
|
|
pub async fn update_config(&self) -> Result<()> {
|
2022-11-18 22:08:06 +08:00
|
|
|
|
log::debug!(target: "app", "try to update clash config");
|
|
|
|
|
|
|
2023-11-22 02:56:47 +08:00
|
|
|
|
// 更新订阅
|
2024-06-29 19:02:37 +08:00
|
|
|
|
Config::generate().await?;
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2023-11-22 02:56:47 +08:00
|
|
|
|
// 检查订阅是否正常
|
2022-11-14 01:26:33 +08:00
|
|
|
|
self.check_config()?;
|
|
|
|
|
|
|
2023-11-22 02:56:47 +08:00
|
|
|
|
// 更新运行时订阅
|
2022-11-18 18:18:41 +08:00
|
|
|
|
let path = Config::generate_file(ConfigType::Run)?;
|
|
|
|
|
|
let path = dirs::path_to_str(&path)?;
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
// 发送请求 发送5次
|
|
|
|
|
|
for i in 0..5 {
|
2022-11-18 18:18:41 +08:00
|
|
|
|
match clash_api::put_configs(path).await {
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Ok(_) => break,
|
|
|
|
|
|
Err(err) => {
|
|
|
|
|
|
if i < 4 {
|
2022-11-18 22:08:06 +08:00
|
|
|
|
log::info!(target: "app", "{err}");
|
2022-11-14 01:26:33 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
bail!(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
sleep(Duration::from_millis(250)).await;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|