feat: ensure Mihomo and Verge services are running before executing commands

This commit is contained in:
Tunglies 2025-03-22 16:59:32 +08:00
parent 079591da59
commit b8161b01c1
4 changed files with 81 additions and 51 deletions

View File

@ -1,8 +1,9 @@
use super::CmdResult; use super::CmdResult;
use crate::module::mihomo::MihomoManager; use crate::{core::CoreManager, module::mihomo::MihomoManager};
#[tauri::command] #[tauri::command]
pub async fn get_proxies() -> CmdResult<serde_json::Value> { pub async fn get_proxies() -> CmdResult<serde_json::Value> {
CoreManager::global().ensure_running_core().await;
let mannager = MihomoManager::global(); let mannager = MihomoManager::global();
let proxies = mannager let proxies = mannager
.refresh_proxies() .refresh_proxies()
@ -14,6 +15,7 @@ pub async fn get_proxies() -> CmdResult<serde_json::Value> {
#[tauri::command] #[tauri::command]
pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> { pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> {
CoreManager::global().ensure_running_core().await;
let mannager = MihomoManager::global(); let mannager = MihomoManager::global();
let providers = mannager let providers = mannager
.refresh_providers_proxies() .refresh_providers_proxies()

View File

@ -8,11 +8,13 @@ use crate::{
utils::{dirs, help}, utils::{dirs, help},
}; };
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use fs2::FileExt;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use std::{path::PathBuf, sync::Arc, time::Duration}; use std::{path::PathBuf, sync::Arc, time::Duration};
use tauri_plugin_shell::ShellExt; use tauri_plugin_shell::ShellExt;
use tokio::{sync::Mutex, time::sleep}; use tokio::{sync::Mutex, time::sleep};
use fs2::FileExt;
use super::service::is_service_running;
#[derive(Debug)] #[derive(Debug)]
pub struct CoreManager { pub struct CoreManager {
@ -111,11 +113,11 @@ impl CoreManager {
if let Some(process) = handle::Handle::global().take_core_process() { if let Some(process) = handle::Handle::global().take_core_process() {
println!("[停止sidecar] 发现sidecar进程准备停止"); println!("[停止sidecar] 发现sidecar进程准备停止");
log::info!(target: "app", "stopping core process in sidecar mode"); log::info!(target: "app", "stopping core process in sidecar mode");
// 尝试获取进程ID // 尝试获取进程ID
let pid = process.pid(); let pid = process.pid();
println!("[停止sidecar] 进程PID: {}", pid); println!("[停止sidecar] 进程PID: {}", pid);
// 尝试终止进程 // 尝试终止进程
if let Err(e) = process.kill() { if let Err(e) = process.kill() {
println!("[停止sidecar] 终止sidecar进程失败: {}", e); println!("[停止sidecar] 终止sidecar进程失败: {}", e);
@ -184,21 +186,29 @@ impl CoreManager {
if let Ok(config_content) = std::fs::read_to_string(config_path) { if let Ok(config_content) = std::fs::read_to_string(config_path) {
if let Ok(config) = serde_yaml::from_str::<serde_yaml::Value>(&config_content) { if let Ok(config) = serde_yaml::from_str::<serde_yaml::Value>(&config_content) {
// 获取配置中定义的端口 // 获取配置中定义的端口
let mixed_port = config.get("mixed-port").and_then(|v| v.as_u64()).unwrap_or(7890); let mixed_port = config
.get("mixed-port")
.and_then(|v| v.as_u64())
.unwrap_or(7890);
let http_port = config.get("port").and_then(|v| v.as_u64()).unwrap_or(7890); let http_port = config.get("port").and_then(|v| v.as_u64()).unwrap_or(7890);
println!("[sidecar启动] 检查端口占用: HTTP端口={}, 混合端口={}", http_port, mixed_port); println!(
"[sidecar启动] 检查端口占用: HTTP端口={}, 混合端口={}",
http_port, mixed_port
);
// 检查端口是否被占用 // 检查端口是否被占用
if self.is_port_in_use(mixed_port as u16).await || self.is_port_in_use(http_port as u16).await { if self.is_port_in_use(mixed_port as u16).await
|| self.is_port_in_use(http_port as u16).await
{
println!("[sidecar启动] 端口已被占用,尝试终止已存在的进程"); println!("[sidecar启动] 端口已被占用,尝试终止已存在的进程");
// 尝试终止已存在的进程 // 尝试终止已存在的进程
for pid in pids { for pid in pids {
println!("[sidecar启动] 尝试终止进程 PID: {}", pid); println!("[sidecar启动] 尝试终止进程 PID: {}", pid);
self.terminate_process(pid).await; self.terminate_process(pid).await;
} }
// 等待短暂时间让资源释放 // 等待短暂时间让资源释放
println!("[sidecar启动] 等待500ms让资源释放"); println!("[sidecar启动] 等待500ms让资源释放");
sleep(Duration::from_millis(500)).await; sleep(Duration::from_millis(500)).await;
@ -213,20 +223,20 @@ impl CoreManager {
// 创建锁文件路径 // 创建锁文件路径
let lock_file = dirs::app_home_dir()?.join(format!("{}.lock", clash_core)); let lock_file = dirs::app_home_dir()?.join(format!("{}.lock", clash_core));
println!("[sidecar启动] 锁文件路径: {:?}", lock_file); println!("[sidecar启动] 锁文件路径: {:?}", lock_file);
// 尝试获取文件锁 // 尝试获取文件锁
println!("[sidecar启动] 尝试获取文件锁"); println!("[sidecar启动] 尝试获取文件锁");
let file = std::fs::OpenOptions::new() let file = std::fs::OpenOptions::new()
.write(true) .write(true)
.create(true) .create(true)
.open(&lock_file)?; .open(&lock_file)?;
match file.try_lock_exclusive() { match file.try_lock_exclusive() {
Ok(_) => { Ok(_) => {
// 成功获取锁,说明没有其他实例运行 // 成功获取锁,说明没有其他实例运行
println!("[sidecar启动] 成功获取文件锁,没有检测到其他运行的实例"); println!("[sidecar启动] 成功获取文件锁,没有检测到其他运行的实例");
log::info!(target: "app", "acquired lock for core process"); log::info!(target: "app", "acquired lock for core process");
// 保存锁对象到全局防止被Drop // 保存锁对象到全局防止被Drop
handle::Handle::global().set_core_lock(file); handle::Handle::global().set_core_lock(file);
} }
@ -235,7 +245,7 @@ impl CoreManager {
println!("[sidecar启动] 无法获取文件锁,检测到其他实例可能正在运行"); println!("[sidecar启动] 无法获取文件锁,检测到其他实例可能正在运行");
println!("[sidecar启动] 错误信息: {:?}", err); println!("[sidecar启动] 错误信息: {:?}", err);
log::warn!(target: "app", "another core process appears to be running"); log::warn!(target: "app", "another core process appears to be running");
// 尝试强制获取锁(可能会导致其他进程崩溃) // 尝试强制获取锁(可能会导致其他进程崩溃)
println!("[sidecar启动] 尝试强制删除并重新创建锁文件"); println!("[sidecar启动] 尝试强制删除并重新创建锁文件");
std::fs::remove_file(&lock_file)?; std::fs::remove_file(&lock_file)?;
@ -243,17 +253,17 @@ impl CoreManager {
.write(true) .write(true)
.create(true) .create(true)
.open(&lock_file)?; .open(&lock_file)?;
println!("[sidecar启动] 尝试强制获取锁"); println!("[sidecar启动] 尝试强制获取锁");
match file.lock_exclusive() { match file.lock_exclusive() {
Ok(_) => println!("[sidecar启动] 成功强制获取锁"), Ok(_) => println!("[sidecar启动] 成功强制获取锁"),
Err(e) => println!("[sidecar启动] 强制获取锁失败: {:?}", e), Err(e) => println!("[sidecar启动] 强制获取锁失败: {:?}", e),
} }
file.lock_exclusive()?; file.lock_exclusive()?;
// 保存新锁 // 保存新锁
handle::Handle::global().set_core_lock(file); handle::Handle::global().set_core_lock(file);
// 等待可能的其他进程退出 // 等待可能的其他进程退出
println!("[sidecar启动] 等待500ms让可能的其他进程退出"); println!("[sidecar启动] 等待500ms让可能的其他进程退出");
sleep(Duration::from_millis(500)).await; sleep(Duration::from_millis(500)).await;
@ -657,6 +667,8 @@ impl CoreManager {
// 5. 应用新配置 // 5. 应用新配置
println!("[core配置更新] 应用新配置"); println!("[core配置更新] 应用新配置");
for i in 0..3 { for i in 0..3 {
CoreManager::global().ensure_running_core().await;
match MihomoManager::global().put_configs_force(run_path).await { match MihomoManager::global().put_configs_force(run_path).await {
Ok(_) => { Ok(_) => {
println!("[core配置更新] 配置应用成功"); println!("[core配置更新] 配置应用成功");
@ -772,19 +784,19 @@ impl CoreManager {
/// 检查系统中是否存在同名进程 /// 检查系统中是否存在同名进程
async fn check_existing_processes(&self, process_name: &str) -> Result<Vec<u32>> { async fn check_existing_processes(&self, process_name: &str) -> Result<Vec<u32>> {
println!("[进程检查] 检查系统中是否存在进程: {}", process_name); println!("[进程检查] 检查系统中是否存在进程: {}", process_name);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
use std::process::Command; use std::process::Command;
println!("[进程检查] Windows系统使用tasklist命令"); println!("[进程检查] Windows系统使用tasklist命令");
let output = Command::new("tasklist") let output = Command::new("tasklist")
.args(["/FO", "CSV", "/NH"]) .args(["/FO", "CSV", "/NH"])
.output()?; .output()?;
let output = String::from_utf8_lossy(&output.stdout); let output = String::from_utf8_lossy(&output.stdout);
let mut pids = Vec::new(); let mut pids = Vec::new();
for line in output.lines() { for line in output.lines() {
if line.contains(process_name) { if line.contains(process_name) {
println!("[进程检查] 发现匹配行: {}", line); println!("[进程检查] 发现匹配行: {}", line);
@ -798,47 +810,44 @@ impl CoreManager {
} }
} }
} }
println!("[进程检查] 共发现 {} 个相关进程", pids.len()); println!("[进程检查] 共发现 {} 个相关进程", pids.len());
Ok(pids) Ok(pids)
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
use std::process::Command; use std::process::Command;
println!("[进程检查] Linux系统使用pgrep命令"); println!("[进程检查] Linux系统使用pgrep命令");
let output = Command::new("pgrep") let output = Command::new("pgrep").arg("-f").arg(process_name).output()?;
.arg("-f")
.arg(process_name)
.output()?;
let output = String::from_utf8_lossy(&output.stdout); let output = String::from_utf8_lossy(&output.stdout);
let mut pids = Vec::new(); let mut pids = Vec::new();
for line in output.lines() { for line in output.lines() {
if let Ok(pid) = line.trim().parse::<u32>() { if let Ok(pid) = line.trim().parse::<u32>() {
println!("[进程检查] 发现进程 PID: {}", pid); println!("[进程检查] 发现进程 PID: {}", pid);
pids.push(pid); pids.push(pid);
} }
} }
println!("[进程检查] 共发现 {} 个相关进程", pids.len()); println!("[进程检查] 共发现 {} 个相关进程", pids.len());
Ok(pids) Ok(pids)
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
use std::process::Command; use std::process::Command;
println!("[进程检查] macOS系统使用ps命令"); println!("[进程检查] macOS系统使用ps命令");
let output = Command::new("ps") let output = Command::new("ps")
.args(["-ax", "-o", "pid,command"]) .args(["-ax", "-o", "pid,command"])
.output()?; .output()?;
let output = String::from_utf8_lossy(&output.stdout); let output = String::from_utf8_lossy(&output.stdout);
let mut pids = Vec::new(); let mut pids = Vec::new();
for line in output.lines() { for line in output.lines() {
if line.contains(process_name) { if line.contains(process_name) {
println!("[进程检查] 发现匹配行: {}", line); println!("[进程检查] 发现匹配行: {}", line);
@ -851,7 +860,7 @@ impl CoreManager {
} }
} }
} }
println!("[进程检查] 共发现 {} 个相关进程", pids.len()); println!("[进程检查] 共发现 {} 个相关进程", pids.len());
Ok(pids) Ok(pids)
} }
@ -860,9 +869,9 @@ impl CoreManager {
/// 检查端口是否被占用 /// 检查端口是否被占用
async fn is_port_in_use(&self, port: u16) -> bool { async fn is_port_in_use(&self, port: u16) -> bool {
println!("[端口检查] 检查端口 {} 是否被占用", port); println!("[端口检查] 检查端口 {} 是否被占用", port);
use tokio::net::TcpSocket; use tokio::net::TcpSocket;
match TcpSocket::new_v4() { match TcpSocket::new_v4() {
Ok(socket) => { Ok(socket) => {
let addr = format!("127.0.0.1:{}", port).parse().unwrap(); let addr = format!("127.0.0.1:{}", port).parse().unwrap();
@ -890,14 +899,14 @@ impl CoreManager {
/// 终止进程 /// 终止进程
async fn terminate_process(&self, pid: u32) { async fn terminate_process(&self, pid: u32) {
println!("[进程终止] 尝试终止进程 PID: {}", pid); println!("[进程终止] 尝试终止进程 PID: {}", pid);
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
use std::process::Command; use std::process::Command;
let output = Command::new("taskkill") let output = Command::new("taskkill")
.args(["/F", "/PID", &pid.to_string()]) .args(["/F", "/PID", &pid.to_string()])
.output(); .output();
match output { match output {
Ok(output) => { Ok(output) => {
if output.status.success() { if output.status.success() {
@ -912,14 +921,12 @@ impl CoreManager {
} }
} }
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
use std::process::Command; use std::process::Command;
let output = Command::new("kill") let output = Command::new("kill").args(["-9", &pid.to_string()]).output();
.args(["-9", &pid.to_string()])
.output();
match output { match output {
Ok(output) => { Ok(output) => {
if output.status.success() { if output.status.success() {
@ -934,14 +941,12 @@ impl CoreManager {
} }
} }
} }
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
use std::process::Command; use std::process::Command;
let output = Command::new("kill") let output = Command::new("kill").args(["-9", &pid.to_string()]).output();
.args(["-9", &pid.to_string()])
.output();
match output { match output {
Ok(output) => { Ok(output) => {
if output.status.success() { if output.status.success() {
@ -957,4 +962,19 @@ impl CoreManager {
} }
} }
} }
/// 确保 Mihomo 和 Verge service 都在运行
pub async fn ensure_running_core(&self) {
if MihomoManager::global().is_mihomo_running().await.is_err() {
log_err!(self.restart_core().await);
}
match is_service_running().await {
Ok(false) => log_err!(self.restart_core().await),
Ok(true) => {
if MihomoManager::global().is_mihomo_running().await.is_err() {
log_err!(self.restart_core().await);
}
}
_ => {}
}
}
} }

View File

@ -47,6 +47,7 @@ pub fn change_clash_mode(mode: String) {
}); });
tauri::async_runtime::spawn(async move { tauri::async_runtime::spawn(async move {
log::debug!(target: "app", "change clash mode to {mode}"); log::debug!(target: "app", "change clash mode to {mode}");
CoreManager::global().ensure_running_core().await;
match MihomoManager::global().patch_configs(json_value).await { match MihomoManager::global().patch_configs(json_value).await {
Ok(_) => { Ok(_) => {
@ -66,6 +67,7 @@ pub fn change_clash_mode(mode: String) {
/// Test connection delay to a URL /// Test connection delay to a URL
pub async fn test_delay(url: String) -> anyhow::Result<u32> { pub async fn test_delay(url: String) -> anyhow::Result<u32> {
CoreManager::global().ensure_running_core().await;
use tokio::time::{Duration, Instant}; use tokio::time::{Duration, Instant};
let mut builder = reqwest::ClientBuilder::new().use_rustls_tls().no_proxy(); let mut builder = reqwest::ClientBuilder::new().use_rustls_tls().no_proxy();

View File

@ -98,6 +98,12 @@ impl MihomoManager {
} }
impl MihomoManager { impl MihomoManager {
pub async fn is_mihomo_running(&self) -> Result<(), String> {
let url = format!("{}/version", self.mihomo_server);
let _response = self.send_request(Method::GET, url, None).await?;
Ok(())
}
pub async fn put_configs_force(&self, clash_config_path: &str) -> Result<(), String> { pub async fn put_configs_force(&self, clash_config_path: &str) -> Result<(), String> {
let url = format!("{}/configs?force=true", self.mihomo_server); let url = format!("{}/configs?force=true", self.mihomo_server);
let payload = serde_json::json!({ let payload = serde_json::json!({