mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 05:23:44 +08:00
feat: ensure Mihomo and Verge services are running before executing commands
This commit is contained in:
parent
079591da59
commit
b8161b01c1
@ -1,8 +1,9 @@
|
||||
use super::CmdResult;
|
||||
use crate::module::mihomo::MihomoManager;
|
||||
use crate::{core::CoreManager, module::mihomo::MihomoManager};
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_proxies() -> CmdResult<serde_json::Value> {
|
||||
CoreManager::global().ensure_running_core().await;
|
||||
let mannager = MihomoManager::global();
|
||||
let proxies = mannager
|
||||
.refresh_proxies()
|
||||
@ -14,6 +15,7 @@ pub async fn get_proxies() -> CmdResult<serde_json::Value> {
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn get_providers_proxies() -> CmdResult<serde_json::Value> {
|
||||
CoreManager::global().ensure_running_core().await;
|
||||
let mannager = MihomoManager::global();
|
||||
let providers = mannager
|
||||
.refresh_providers_proxies()
|
||||
|
@ -8,11 +8,13 @@ use crate::{
|
||||
utils::{dirs, help},
|
||||
};
|
||||
use anyhow::{bail, Result};
|
||||
use fs2::FileExt;
|
||||
use once_cell::sync::OnceCell;
|
||||
use std::{path::PathBuf, sync::Arc, time::Duration};
|
||||
use tauri_plugin_shell::ShellExt;
|
||||
use tokio::{sync::Mutex, time::sleep};
|
||||
use fs2::FileExt;
|
||||
|
||||
use super::service::is_service_running;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CoreManager {
|
||||
@ -184,13 +186,21 @@ impl CoreManager {
|
||||
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) {
|
||||
// 获取配置中定义的端口
|
||||
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);
|
||||
|
||||
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启动] 端口已被占用,尝试终止已存在的进程");
|
||||
|
||||
// 尝试终止已存在的进程
|
||||
@ -657,6 +667,8 @@ impl CoreManager {
|
||||
// 5. 应用新配置
|
||||
println!("[core配置更新] 应用新配置");
|
||||
for i in 0..3 {
|
||||
CoreManager::global().ensure_running_core().await;
|
||||
|
||||
match MihomoManager::global().put_configs_force(run_path).await {
|
||||
Ok(_) => {
|
||||
println!("[core配置更新] 配置应用成功");
|
||||
@ -808,10 +820,7 @@ impl CoreManager {
|
||||
use std::process::Command;
|
||||
|
||||
println!("[进程检查] Linux系统,使用pgrep命令");
|
||||
let output = Command::new("pgrep")
|
||||
.arg("-f")
|
||||
.arg(process_name)
|
||||
.output()?;
|
||||
let output = Command::new("pgrep").arg("-f").arg(process_name).output()?;
|
||||
|
||||
let output = String::from_utf8_lossy(&output.stdout);
|
||||
let mut pids = Vec::new();
|
||||
@ -916,9 +925,7 @@ impl CoreManager {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
use std::process::Command;
|
||||
let output = Command::new("kill")
|
||||
.args(["-9", &pid.to_string()])
|
||||
.output();
|
||||
let output = Command::new("kill").args(["-9", &pid.to_string()]).output();
|
||||
|
||||
match output {
|
||||
Ok(output) => {
|
||||
@ -938,9 +945,7 @@ impl CoreManager {
|
||||
#[cfg(target_os = "macos")]
|
||||
{
|
||||
use std::process::Command;
|
||||
let output = Command::new("kill")
|
||||
.args(["-9", &pid.to_string()])
|
||||
.output();
|
||||
let output = Command::new("kill").args(["-9", &pid.to_string()]).output();
|
||||
|
||||
match output {
|
||||
Ok(output) => {
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ pub fn change_clash_mode(mode: String) {
|
||||
});
|
||||
tauri::async_runtime::spawn(async move {
|
||||
log::debug!(target: "app", "change clash mode to {mode}");
|
||||
CoreManager::global().ensure_running_core().await;
|
||||
|
||||
match MihomoManager::global().patch_configs(json_value).await {
|
||||
Ok(_) => {
|
||||
@ -66,6 +67,7 @@ pub fn change_clash_mode(mode: String) {
|
||||
|
||||
/// Test connection delay to a URL
|
||||
pub async fn test_delay(url: String) -> anyhow::Result<u32> {
|
||||
CoreManager::global().ensure_running_core().await;
|
||||
use tokio::time::{Duration, Instant};
|
||||
let mut builder = reqwest::ClientBuilder::new().use_rustls_tls().no_proxy();
|
||||
|
||||
|
@ -98,6 +98,12 @@ 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> {
|
||||
let url = format!("{}/configs?force=true", self.mihomo_server);
|
||||
let payload = serde_json::json!({
|
||||
|
Loading…
x
Reference in New Issue
Block a user