refactor: service reinstallation logic on detection failure

This commit is contained in:
wonfen 2025-03-25 00:51:26 +08:00
parent b5b5ae4e7b
commit cf78bb3686
6 changed files with 292 additions and 15 deletions

View File

@ -0,0 +1,8 @@
use super::CmdResult;
use crate::{core::CoreManager, wrap_err};
/// 修复系统服务
#[tauri::command]
pub async fn repair_service() -> CmdResult {
wrap_err!(CoreManager::global().repair_service().await)
}

View File

@ -6,6 +6,7 @@ pub type CmdResult<T = ()> = Result<T, String>;
// Command modules // Command modules
pub mod app; pub mod app;
pub mod clash; pub mod clash;
pub mod core;
pub mod media_unlock_checker; pub mod media_unlock_checker;
pub mod network; pub mod network;
pub mod profile; pub mod profile;
@ -22,6 +23,7 @@ pub mod lighteweight;
// Re-export all command functions for backwards compatibility // Re-export all command functions for backwards compatibility
pub use app::*; pub use app::*;
pub use clash::*; pub use clash::*;
pub use core::*;
pub use media_unlock_checker::*; pub use media_unlock_checker::*;
pub use network::*; pub use network::*;
pub use profile::*; pub use profile::*;

View File

@ -196,6 +196,9 @@ pub struct IVerge {
/// 自动进入轻量模式的延迟(分钟) /// 自动进入轻量模式的延迟(分钟)
pub auto_light_weight_minutes: Option<u64>, pub auto_light_weight_minutes: Option<u64>,
/// 服务状态跟踪
pub service_state: Option<crate::core::service::ServiceState>,
} }
#[derive(Default, Debug, Clone, Deserialize, Serialize)] #[derive(Default, Debug, Clone, Deserialize, Serialize)]
@ -303,6 +306,7 @@ impl IVerge {
auto_light_weight_minutes: Some(10), auto_light_weight_minutes: Some(10),
enable_dns_settings: Some(true), enable_dns_settings: Some(true),
home_cards: None, home_cards: None,
service_state: None,
..Self::default() ..Self::default()
} }
} }
@ -389,6 +393,7 @@ impl IVerge {
patch!(auto_light_weight_minutes); patch!(auto_light_weight_minutes);
patch!(enable_dns_settings); patch!(enable_dns_settings);
patch!(home_cards); patch!(home_cards);
patch!(service_state);
} }
/// 在初始化前尝试拿到单例端口的值 /// 在初始化前尝试拿到单例端口的值
@ -482,6 +487,7 @@ pub struct IVergeResponse {
pub auto_light_weight_minutes: Option<u64>, pub auto_light_weight_minutes: Option<u64>,
pub enable_dns_settings: Option<bool>, pub enable_dns_settings: Option<bool>,
pub home_cards: Option<serde_json::Value>, pub home_cards: Option<serde_json::Value>,
pub service_state: Option<crate::core::service::ServiceState>,
} }
impl From<IVerge> for IVergeResponse { impl From<IVerge> for IVergeResponse {
@ -549,6 +555,7 @@ impl From<IVerge> for IVergeResponse {
auto_light_weight_minutes: verge.auto_light_weight_minutes, auto_light_weight_minutes: verge.auto_light_weight_minutes,
enable_dns_settings: verge.enable_dns_settings, enable_dns_settings: verge.enable_dns_settings,
home_cards: verge.home_cards, home_cards: verge.home_cards,
service_state: verge.service_state,
} }
} }
} }

View File

@ -141,15 +141,17 @@ impl CoreManager {
let config_path = Config::generate_file(ConfigType::Run)?; let config_path = Config::generate_file(ConfigType::Run)?;
// 先尝试服务模式 // 先检查服务状态
if service::check_service().await.is_ok() { let service_available = service::check_service().await.is_ok();
if service_available {
log::info!(target: "app", "try to run core in service mode"); log::info!(target: "app", "try to run core in service mode");
match service::run_core_by_service(&config_path).await { match service::run_core_by_service(&config_path).await {
Ok(_) => { Ok(_) => {
log::info!(target: "app", "core started successfully in service mode"); log::info!(target: "app", "core started successfully in service mode");
} }
Err(err) => { Err(err) => {
// 服务启动失败,尝试sidecar模式 // 服务启动失败,直接尝试sidecar模式,不再尝试重装服务
log::warn!(target: "app", "failed to start core in service mode: {}", err); log::warn!(target: "app", "failed to start core in service mode: {}", err);
log::info!(target: "app", "trying to run core in sidecar mode"); log::info!(target: "app", "trying to run core in sidecar mode");
self.run_core_by_sidecar(&config_path).await?; self.run_core_by_sidecar(&config_path).await?;
@ -308,6 +310,19 @@ impl CoreManager {
Ok(()) Ok(())
} }
/// 强制重新安装服务供UI调用用户主动修复服务
pub async fn repair_service(&self) -> Result<()> {
log::info!(target: "app", "user requested service repair");
// 调用强制重装服务
service::force_reinstall_service().await?;
// 重启核心
self.restart_core().await?;
Ok(())
}
/// 使用默认配置 /// 使用默认配置
pub async fn use_default_config(&self, msg_type: &str, msg_content: &str) -> Result<()> { pub async fn use_default_config(&self, msg_type: &str, msg_content: &str) -> Result<()> {
let runtime_path = dirs::app_home_dir()?.join(RUNTIME_CONFIG); let runtime_path = dirs::app_home_dir()?.join(RUNTIME_CONFIG);

View File

@ -1,13 +1,82 @@
use crate::{config::Config, utils::dirs}; use crate::{config::Config, utils::dirs};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, env::current_exe, path::PathBuf, process::Command as StdCommand}; use std::{collections::HashMap, env::current_exe, path::PathBuf, process::Command as StdCommand, time::{SystemTime, UNIX_EPOCH}};
use tokio::time::Duration; use tokio::time::Duration;
// Windows only // Windows only
const SERVICE_URL: &str = "http://127.0.0.1:33211"; const SERVICE_URL: &str = "http://127.0.0.1:33211";
const REQUIRED_SERVICE_VERSION: &str = "1.0.4"; // 定义所需的服务版本号 const REQUIRED_SERVICE_VERSION: &str = "1.0.5"; // 定义所需的服务版本号
// 限制重装时间和次数的常量
const REINSTALL_COOLDOWN_SECS: u64 = 300; // 5分钟冷却期
const MAX_REINSTALLS_PER_DAY: u32 = 3; // 每24小时最多重装3次
const ONE_DAY_SECS: u64 = 86400; // 24小时的秒数
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
pub struct ServiceState {
pub last_install_time: u64, // 上次安装时间戳 (Unix 时间戳,秒)
pub install_count: u32, // 24小时内安装次数
pub last_check_time: u64, // 上次检查时间
pub last_error: Option<String>, // 上次错误信息
}
impl ServiceState {
// 获取当前的服务状态
pub fn get() -> Self {
if let Some(state) = Config::verge().latest().service_state.clone() {
return state;
}
Self::default()
}
// 保存服务状态
pub fn save(&self) -> Result<()> {
let config = Config::verge();
let mut latest = config.latest().clone();
latest.service_state = Some(self.clone());
*config.draft() = latest;
config.apply();
Config::verge().latest().save_file()
}
// 更新安装信息
pub fn record_install(&mut self) {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
// 检查是否需要重置计数器24小时已过
if now - self.last_install_time > ONE_DAY_SECS {
self.install_count = 0;
}
self.last_install_time = now;
self.install_count += 1;
}
// 检查是否可以重新安装
pub fn can_reinstall(&self) -> bool {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
// 如果在冷却期内,不允许重装
if now - self.last_install_time < REINSTALL_COOLDOWN_SECS {
return false;
}
// 如果24小时内安装次数过多也不允许
if now - self.last_install_time < ONE_DAY_SECS && self.install_count >= MAX_REINSTALLS_PER_DAY {
return false;
}
true
}
}
#[derive(Debug, Deserialize, Serialize, Clone)] #[derive(Debug, Deserialize, Serialize, Clone)]
pub struct ResponseBody { pub struct ResponseBody {
@ -41,6 +110,15 @@ pub struct VersionJsonResponse {
pub async fn reinstall_service() -> Result<()> { pub async fn reinstall_service() -> Result<()> {
log::info!(target:"app", "reinstall service"); log::info!(target:"app", "reinstall service");
// 获取当前服务状态
let mut service_state = ServiceState::get();
// 检查是否允许重装
if !service_state.can_reinstall() {
log::warn!(target:"app", "service reinstall rejected: cooldown period or max attempts reached");
bail!("Service reinstallation is rate limited. Please try again later.");
}
use deelevate::{PrivilegeLevel, Token}; use deelevate::{PrivilegeLevel, Token};
use runas::Command as RunasCommand; use runas::Command as RunasCommand;
use std::os::windows::process::CommandExt; use std::os::windows::process::CommandExt;
@ -74,12 +152,20 @@ pub async fn reinstall_service() -> Result<()> {
}; };
if !status.success() { if !status.success() {
bail!( let error = format!(
"failed to install service with status {}", "failed to install service with status {}",
status.code().unwrap() status.code().unwrap()
); );
service_state.last_error = Some(error.clone());
service_state.save()?;
bail!(error);
} }
// 记录安装信息并保存
service_state.record_install();
service_state.last_error = None;
service_state.save()?;
Ok(()) Ok(())
} }
@ -226,19 +312,54 @@ pub async fn check_service_version() -> Result<String> {
/// check if service needs to be reinstalled /// check if service needs to be reinstalled
pub async fn check_service_needs_reinstall() -> bool { pub async fn check_service_needs_reinstall() -> bool {
// 获取当前服务状态
let service_state = ServiceState::get();
// 首先检查是否在冷却期或超过重装次数限制
if !service_state.can_reinstall() {
log::info!(target: "app", "service reinstall check: in cooldown period or max attempts reached");
return false;
}
// 然后才检查版本和可用性
match check_service_version().await { match check_service_version().await {
Ok(version) => version != REQUIRED_SERVICE_VERSION, Ok(version) => {
Err(_) => true, // 如果无法获取版本或服务未运行,也需要重新安装 // 打印更详细的日志,方便排查问题
log::info!(target: "app", "服务版本检测:当前={}, 要求={}", version, REQUIRED_SERVICE_VERSION);
let needs_reinstall = version != REQUIRED_SERVICE_VERSION;
if needs_reinstall {
log::warn!(target: "app", "发现服务版本不匹配,需要重装! 当前={}, 要求={}",
version, REQUIRED_SERVICE_VERSION);
// 打印版本字符串的原始字节,确认没有隐藏字符
log::debug!(target: "app", "当前版本字节: {:?}", version.as_bytes());
log::debug!(target: "app", "要求版本字节: {:?}", REQUIRED_SERVICE_VERSION.as_bytes());
} else {
log::info!(target: "app", "服务版本匹配,无需重装");
}
needs_reinstall
},
Err(err) => {
// 检查服务是否可用如果可用但版本检查失败可能只是版本API有问题
match is_service_running().await {
Ok(true) => {
log::info!(target: "app", "service is running but version check failed: {}", err);
false // 服务在运行,不需要重装
}
_ => {
log::info!(target: "app", "service is not running or unavailable");
true // 服务不可用,需要重装
}
}
}
} }
} }
/// start the clash by service /// 尝试使用现有服务启动核心,不进行重装
pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> { pub(super) async fn start_with_existing_service(config_file: &PathBuf) -> Result<()> {
// 检查服务版本,如果不匹配则重新安装 log::info!(target:"app", "attempting to start core with existing service");
if check_service_needs_reinstall().await {
log::info!(target: "app", "service version mismatch, reinstalling");
reinstall_service().await?;
}
let clash_core = { Config::verge().latest().clash_core.clone() }; let clash_core = { Config::verge().latest().clash_core.clone() };
let clash_core = clash_core.unwrap_or("verge-mihomo".into()); let clash_core = clash_core.unwrap_or("verge-mihomo".into());
@ -280,6 +401,106 @@ pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
Ok(()) Ok(())
} }
/// start the clash by service
pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
log::info!(target: "app", "正在尝试通过服务启动核心");
// 先检查服务版本,不受冷却期限制
let version_check = match check_service_version().await {
Ok(version) => {
log::info!(target: "app", "检测到服务版本: {}, 要求版本: {}",
version, REQUIRED_SERVICE_VERSION);
// 通过字节比较确保完全匹配
if version.as_bytes() != REQUIRED_SERVICE_VERSION.as_bytes() {
log::warn!(target: "app", "服务版本不匹配,需要重装");
false // 版本不匹配
} else {
log::info!(target: "app", "服务版本匹配");
true // 版本匹配
}
},
Err(err) => {
log::warn!(target: "app", "无法获取服务版本: {}", err);
false // 无法获取版本
}
};
// 先尝试直接启动服务,如果服务可用且版本匹配
if version_check {
if let Ok(true) = is_service_running().await {
// 服务正在运行且版本匹配,直接使用
log::info!(target: "app", "服务已在运行且版本匹配,尝试使用");
return start_with_existing_service(config_file).await;
}
}
// 强制执行版本检查,如果版本不匹配则重装
if !version_check {
log::info!(target: "app", "服务版本不匹配,尝试重装");
// 获取服务状态,检查是否可以重装
let service_state = ServiceState::get();
if !service_state.can_reinstall() {
log::warn!(target: "app", "由于限制无法重装服务");
// 尝试直接启动,即使版本不匹配
if let Ok(()) = start_with_existing_service(config_file).await {
log::info!(target: "app", "尽管版本不匹配,但成功启动了服务");
return Ok(());
} else {
bail!("服务版本不匹配且无法重装,启动失败");
}
}
// 尝试重装
log::info!(target: "app", "开始重装服务");
if let Err(err) = reinstall_service().await {
log::warn!(target: "app", "服务重装失败: {}", err);
// 尝试使用现有服务
log::info!(target: "app", "尝试使用现有服务");
return start_with_existing_service(config_file).await;
}
// 重装成功,尝试启动
log::info!(target: "app", "服务重装成功,尝试启动");
return start_with_existing_service(config_file).await;
}
// 检查服务状态
match check_service().await {
Ok(_) => {
// 服务可访问但可能没有运行核心,尝试直接启动
log::info!(target: "app", "服务可用但未运行核心,尝试启动");
if let Ok(()) = start_with_existing_service(config_file).await {
return Ok(());
}
},
Err(err) => {
log::warn!(target: "app", "服务检查失败: {}", err);
}
}
// 服务不可用或启动失败,检查是否需要重装
if check_service_needs_reinstall().await {
log::info!(target: "app", "服务需要重装");
// 尝试重装
if let Err(err) = reinstall_service().await {
log::warn!(target: "app", "服务重装失败: {}", err);
bail!("Failed to reinstall service: {}", err);
}
// 重装后再次尝试启动
log::info!(target: "app", "服务重装完成,尝试启动核心");
start_with_existing_service(config_file).await
} else {
// 不需要或不能重装,返回错误
log::warn!(target: "app", "服务不可用且无法重装");
bail!("Service is not available and cannot be reinstalled at this time")
}
}
/// stop the clash by service /// stop the clash by service
pub(super) async fn stop_core_by_service() -> Result<()> { pub(super) async fn stop_core_by_service() -> Result<()> {
let url = format!("{SERVICE_URL}/stop_clash"); let url = format!("{SERVICE_URL}/stop_clash");
@ -305,3 +526,26 @@ pub async fn is_service_running() -> Result<bool> {
Ok(false) Ok(false)
} }
} }
/// 强制重装服务用于UI中的修复服务按钮
pub async fn force_reinstall_service() -> Result<()> {
log::info!(target: "app", "用户请求强制重装服务");
// 创建默认服务状态(重置所有限制)
let service_state = ServiceState::default();
service_state.save()?;
log::info!(target: "app", "已重置服务状态,开始执行重装");
// 执行重装
match reinstall_service().await {
Ok(()) => {
log::info!(target: "app", "服务重装成功");
Ok(())
},
Err(err) => {
log::error!(target: "app", "强制重装服务失败: {}", err);
bail!("强制重装服务失败: {}", err)
}
}
}

View File

@ -152,6 +152,7 @@ pub fn run() {
// 添加新的命令 // 添加新的命令
cmd::get_running_mode, cmd::get_running_mode,
cmd::install_service, cmd::install_service,
cmd::repair_service,
cmd::get_app_uptime, cmd::get_app_uptime,
cmd::get_auto_launch_status, cmd::get_auto_launch_status,
// clash // clash