refactor: clash-verge-service management (#4674)

* refactor: clash-verge-service management

* fix: correct service state checks in ProxyControlSwitches component
refactor: improve logging in service state update functions

* fix: add missing async handler for Windows and adjust logging import for macOS

* fix: streamline logging imports and add missing async handler for Windows

* refactor: remove unused useServiceStateSync hook and update imports in _layout

* refactor: remove unused useServiceStateSync import and clean up code in ProxyControlSwitches and _layout

* refactor: simplify service status checks and reduce wait time in useServiceInstaller hook

* refactor: remove unnecessary logging statements in service checks and IPC connection

* refactor: extract SwitchRow component for better code organization and readability

* refactor: enhance service state management and update related mutations in layout

* refactor: streamline core stopping logic and improve IPC connection logging

* refactor: consolidate service uninstallation logic and improve error handling

* fix: simplify conditional statements in CoreManager and service functions

* feat: add backoff dependency and implement retry strategy for IPC requests

* refactor: remove redundant Windows conditional and improve error handling in IPC tests

* test: improve error handling in IPC tests for message signing and verification

* fix: adjust IPC backoff retry parameters

* refactor: Remove service state tracking and related logic from service management

* feat: Enhance service status handling with logging and running mode updates

* fix: Improve service status handling with enhanced error logging

* fix: Ensure proper handling of service operations with error propagation

* refactor: Simplify service operation execution and enhance service status handling

* fix: Improve error message formatting in service operation execution and simplify service status retrieval

* refactor: Replace Cache with CacheProxy in multiple modules and update CacheEntry to be generic

* fix: Remove unnecessary success message from config validation

* refactor: Comment out logging statements in service version check and IPC request handling
This commit is contained in:
Tunglies
2025-09-17 22:59:02 +08:00
committed by GitHub
parent 6724f1ae35
commit c207516b47
20 changed files with 781 additions and 1386 deletions

View File

@@ -1,95 +1,27 @@
use crate::{
cache::{CacheService, SHORT_TERM_TTL},
config::Config,
core::service_ipc::{IpcCommand, send_ipc_request},
logging,
logging, logging_error,
utils::{dirs, logging::Type},
};
use anyhow::{Context, Result, bail};
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use std::{
env::current_exe,
path::PathBuf,
process::Command as StdCommand,
time::{SystemTime, UNIX_EPOCH},
};
use std::{env::current_exe, path::PathBuf, process::Command as StdCommand};
use tokio::sync::Mutex;
const REQUIRED_SERVICE_VERSION: &str = "1.1.2"; // 定义所需的服务版本号
// 限制重装时间和次数的常量
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>, // 上次错误信息
pub prefer_sidecar: bool, // 用户是否偏好sidecar模式如拒绝安装服务或安装失败
}
impl ServiceState {
// 获取当前的服务状态
pub async fn get() -> Self {
if let Some(state) = Config::verge().await.latest_ref().service_state.clone() {
return state;
}
Self::default()
}
// 保存服务状态
pub async fn save(&self) -> Result<()> {
let config = Config::verge().await;
let mut latest = config.latest_ref().clone();
latest.service_state = Some(self.clone());
*config.draft_mut() = latest;
config.apply();
// 先获取数据再异步保存避免跨await持有锁
let verge_data = config.latest_ref().clone();
drop(config); // 显式释放锁
verge_data.save_file().await
}
// 更新安装信息
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, Clone, PartialEq, Eq)]
pub enum ServiceStatus {
Ready,
NeedsReinstall,
InstallRequired,
UninstallRequired,
ReinstallRequired,
ForceReinstallRequired,
Unavailable(String),
}
// 保留核心数据结构但将HTTP特定的结构体合并为通用结构体
@@ -101,12 +33,6 @@ pub struct ResponseBody {
pub log_file: String,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct VersionResponse {
pub service: String,
pub version: String,
}
// 保留通用的响应结构体用于IPC通信后的数据解析
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct JsonResponse {
@@ -115,8 +41,12 @@ pub struct JsonResponse {
pub data: Option<ResponseBody>,
}
#[derive(Clone)]
pub struct ServiceManager(ServiceStatus);
#[allow(clippy::unused_async)]
#[cfg(target_os = "windows")]
pub async fn uninstall_service() -> Result<()> {
async fn uninstall_service() -> Result<()> {
logging!(info, Type::Service, true, "uninstall service");
use deelevate::{PrivilegeLevel, Token};
@@ -149,8 +79,9 @@ pub async fn uninstall_service() -> Result<()> {
Ok(())
}
#[allow(clippy::unused_async)]
#[cfg(target_os = "windows")]
pub async fn install_service() -> Result<()> {
async fn install_service() -> Result<()> {
logging!(info, Type::Service, true, "install service");
use deelevate::{PrivilegeLevel, Token};
@@ -184,23 +115,9 @@ pub async fn install_service() -> Result<()> {
}
#[cfg(target_os = "windows")]
pub async fn reinstall_service() -> Result<()> {
async fn reinstall_service() -> Result<()> {
logging!(info, Type::Service, true, "reinstall service");
// 获取当前服务状态
let mut service_state = ServiceState::get().await;
// 检查是否允许重装
if !service_state.can_reinstall() {
logging!(
warn,
Type::Service,
true,
"service reinstall rejected: cooldown period or max attempts reached"
);
bail!("Service reinstallation is rate limited. Please try again later.");
}
// 先卸载服务
if let Err(err) = uninstall_service().await {
logging!(
@@ -214,26 +131,16 @@ pub async fn reinstall_service() -> Result<()> {
// 再安装服务
match install_service().await {
Ok(_) => {
// 记录安装信息并保存
service_state.record_install();
service_state.last_error = None;
service_state.save().await?;
Ok(())
}
Ok(_) => Ok(()),
Err(err) => {
let error = format!("failed to install service: {err}");
service_state.last_error = Some(error.clone());
service_state.prefer_sidecar = true;
service_state.save().await?;
bail!(error)
bail!(format!("failed to install service: {err}"))
}
}
}
#[allow(clippy::unused_async)]
#[cfg(target_os = "linux")]
pub async fn uninstall_service() -> Result<()> {
async fn uninstall_service() -> Result<()> {
logging!(info, Type::Service, true, "uninstall service");
use users::get_effective_uid;
@@ -273,7 +180,8 @@ pub async fn uninstall_service() -> Result<()> {
}
#[cfg(target_os = "linux")]
pub async fn install_service() -> Result<()> {
#[allow(clippy::unused_async)]
async fn install_service() -> Result<()> {
logging!(info, Type::Service, true, "install service");
use users::get_effective_uid;
@@ -313,23 +221,9 @@ pub async fn install_service() -> Result<()> {
}
#[cfg(target_os = "linux")]
pub async fn reinstall_service() -> Result<()> {
async fn reinstall_service() -> Result<()> {
logging!(info, Type::Service, true, "reinstall service");
// 获取当前服务状态
let mut service_state = ServiceState::get().await;
// 检查是否允许重装
if !service_state.can_reinstall() {
logging!(
warn,
Type::Service,
true,
"service reinstall rejected: cooldown period or max attempts reached"
);
bail!("Service reinstallation is rate limited. Please try again later.");
}
// 先卸载服务
if let Err(err) = uninstall_service().await {
logging!(
@@ -343,25 +237,15 @@ pub async fn reinstall_service() -> Result<()> {
// 再安装服务
match install_service().await {
Ok(_) => {
// 记录安装信息并保存
service_state.record_install();
service_state.last_error = None;
service_state.save().await?;
Ok(())
}
Ok(_) => Ok(()),
Err(err) => {
let error = format!("failed to install service: {err}");
service_state.last_error = Some(error.clone());
service_state.prefer_sidecar = true;
service_state.save().await?;
bail!(error)
bail!(format!("failed to install service: {err}"))
}
}
}
#[cfg(target_os = "macos")]
pub async fn uninstall_service() -> Result<()> {
async fn uninstall_service() -> Result<()> {
use crate::utils::i18n::t;
logging!(info, Type::Service, true, "uninstall service");
@@ -397,7 +281,7 @@ pub async fn uninstall_service() -> Result<()> {
}
#[cfg(target_os = "macos")]
pub async fn install_service() -> Result<()> {
async fn install_service() -> Result<()> {
use crate::utils::i18n::t;
logging!(info, Type::Service, true, "install service");
@@ -433,23 +317,9 @@ pub async fn install_service() -> Result<()> {
}
#[cfg(target_os = "macos")]
pub async fn reinstall_service() -> Result<()> {
async fn reinstall_service() -> Result<()> {
logging!(info, Type::Service, true, "reinstall service");
// 获取当前服务状态
let mut service_state = ServiceState::get().await;
// 检查是否允许重装
if !service_state.can_reinstall() {
logging!(
warn,
Type::Service,
true,
"service reinstall rejected: cooldown period or max attempts reached"
);
bail!("Service reinstallation is rate limited. Please try again later.");
}
// 先卸载服务
if let Err(err) = uninstall_service().await {
logging!(
@@ -463,440 +333,114 @@ pub async fn reinstall_service() -> Result<()> {
// 再安装服务
match install_service().await {
Ok(_) => {
// 记录安装信息并保存
service_state.record_install();
service_state.last_error = None;
service_state.save().await?;
Ok(())
}
Ok(_) => Ok(()),
Err(err) => {
let error = format!("failed to install service: {err}");
service_state.last_error = Some(error.clone());
service_state.prefer_sidecar = true;
service_state.save().await?;
bail!(error)
bail!(format!("failed to install service: {err}"))
}
}
}
/// 检查服务状态 - 使用IPC通信
pub async fn check_ipc_service_status() -> Result<JsonResponse> {
logging!(info, Type::Service, true, "开始检查服务状态 (IPC)");
// 使用IPC通信
let payload = serde_json::json!({});
// logging!(debug, Type::Service, true, "发送GetClash请求");
match send_ipc_request(IpcCommand::GetClash, payload).await {
Ok(response) => {
/* logging!(
debug,
Type::Service,
true,
"收到GetClash响应: success={}, error={:?}",
response.success,
response.error
); */
if !response.success {
let err_msg = response.error.unwrap_or_else(|| "未知服务错误".to_string());
logging!(error, Type::Service, true, "服务响应错误: {}", err_msg);
bail!(err_msg);
}
match response.data {
Some(data) => {
// 检查嵌套结构
if let (Some(code), Some(msg)) = (data.get("code"), data.get("msg")) {
let code_value = code.as_u64().unwrap_or(0);
let msg_value = msg.as_str().unwrap_or("ok").to_string();
// 提取嵌套的data字段并解析为ResponseBody
let response_body = if let Some(nested_data) = data.get("data") {
match serde_json::from_value::<ResponseBody>(nested_data.clone()) {
Ok(body) => Some(body),
Err(e) => {
logging!(
warn,
Type::Service,
true,
"解析嵌套的ResponseBody失败: {}; 尝试其他方式",
e
);
None
}
}
} else {
None
};
let json_response = JsonResponse {
code: code_value,
msg: msg_value,
data: response_body,
};
logging!(
info,
Type::Service,
true,
"服务检测成功: code={}, msg={}, data存在={}",
json_response.code,
json_response.msg,
json_response.data.is_some()
);
Ok(json_response)
} else {
// 尝试直接解析
match serde_json::from_value::<JsonResponse>(data.clone()) {
Ok(json_response) => {
logging!(
info,
Type::Service,
true,
"服务检测成功: code={}, msg={}",
json_response.code,
json_response.msg
);
Ok(json_response)
}
Err(e) => {
logging!(
error,
Type::Service,
true,
"解析服务响应失败: {}; 原始数据: {:?}",
e,
data
);
bail!("无法解析服务响应数据: {}", e)
}
}
}
}
None => {
logging!(error, Type::Service, true, "服务响应中没有数据");
bail!("服务响应中没有数据")
}
}
}
Err(e) => {
logging!(error, Type::Service, true, "IPC通信失败: {}", e);
bail!("无法连接到Clash Verge Service: {}", e)
}
}
/// 强制重装服务UI修复按钮
pub async fn force_reinstall_service() -> Result<()> {
logging!(info, Type::Service, true, "用户请求强制重装服务");
reinstall_service().await.map_err(|err| {
logging!(error, Type::Service, true, "强制重装服务失败: {}", err);
err
})
}
/// 检查服务版本 - 使用IPC通信
pub async fn check_service_version() -> Result<String> {
logging!(info, Type::Service, true, "开始检查服务版本 (IPC)");
async fn check_service_version() -> Result<String> {
let cache = CacheService::global();
let key = CacheService::make_key("service", "version");
let version_arc = cache
.get_or_fetch(key, SHORT_TERM_TTL, || async {
logging!(info, Type::Service, true, "开始检查服务版本 (IPC)");
let payload = serde_json::json!({});
let response = send_ipc_request(IpcCommand::GetVersion, payload).await?;
let payload = serde_json::json!({});
// logging!(debug, Type::Service, true, "发送GetVersion请求");
let data = response
.data
.ok_or_else(|| anyhow::anyhow!("服务版本响应中没有数据"))?;
match send_ipc_request(IpcCommand::GetVersion, payload).await {
Ok(response) => {
/* logging!(
debug,
Type::Service,
true,
"收到GetVersion响应: success={}, error={:?}",
response.success,
response.error
); */
if !response.success {
let err_msg = response
.error
.unwrap_or_else(|| "获取服务版本失败".to_string());
logging!(error, Type::Service, true, "获取版本错误: {}", err_msg);
bail!(err_msg);
if let Some(nested_data) = data.get("data")
&& let Some(version) = nested_data.get("version").and_then(|v| v.as_str())
{
// logging!(info, Type::Service, true, "获取到服务版本: {}", version);
return Ok(version.to_string());
}
match response.data {
Some(data) => {
if let Some(nested_data) = data.get("data") {
if let Some(version) = nested_data.get("version")
&& let Some(version_str) = version.as_str()
{
logging!(info, Type::Service, true, "获取到服务版本: {}", version_str);
return Ok(version_str.to_string());
}
logging!(
error,
Type::Service,
true,
"嵌套数据中没有version字段: {:?}",
nested_data
);
} else {
// 兼容旧格式
match serde_json::from_value::<VersionResponse>(data.clone()) {
Ok(version_response) => {
logging!(
info,
Type::Service,
true,
"获取到服务版本: {}",
version_response.version
);
return Ok(version_response.version);
}
Err(e) => {
logging!(
error,
Type::Service,
true,
"解析版本响应失败: {}; 原始数据: {:?}",
e,
data
);
bail!("无法解析服务版本数据: {}", e)
}
}
}
bail!("响应中未找到有效的版本信息")
}
None => {
logging!(error, Type::Service, true, "版本响应中没有数据");
bail!("服务版本响应中没有数据")
}
}
}
Err(e) => {
logging!(error, Type::Service, true, "IPC通信失败: {}", e);
bail!("无法连接到Clash Verge Service: {}", e)
}
Ok("unknown".to_string())
})
.await;
match version_arc.as_ref() {
Ok(v) => Ok(v.clone()),
Err(e) => Err(anyhow::Error::msg(e.to_string())),
}
}
/// 检查服务是否需要重装
pub async fn check_service_needs_reinstall() -> bool {
logging!(info, Type::Service, true, "开始检查服务是否需要重装");
let service_state = ServiceState::get().await;
if !service_state.can_reinstall() {
log::info!(target: "app", "服务重装检查: 处于冷却期或已达最大尝试次数");
return false;
}
// 检查版本和可用性
match check_service_version().await {
Ok(version) => {
log::info!(target: "app", "服务版本检测:当前={version}, 要求={REQUIRED_SERVICE_VERSION}");
/* logging!(
info,
Type::Service,
true,
"服务版本检测:当前={}, 要求={}",
version,
REQUIRED_SERVICE_VERSION
); */
let needs_reinstall = version != REQUIRED_SERVICE_VERSION;
if needs_reinstall {
log::warn!(target: "app", "发现服务版本不匹配,需要重装! 当前={version}, 要求={REQUIRED_SERVICE_VERSION}");
logging!(warn, Type::Service, true, "服务版本不匹配,需要重装");
// log::debug!(target: "app", "当前版本字节: {:?}", version.as_bytes());
// log::debug!(target: "app", "要求版本字节: {:?}", REQUIRED_SERVICE_VERSION.as_bytes());
} else {
log::info!(target: "app", "服务版本匹配,无需重装");
// logging!(info, Type::Service, true, "服务版本匹配,无需重装");
}
needs_reinstall
}
Err(err) => {
logging!(error, Type::Service, true, "检查服务版本失败: {}", err);
// 检查服务是否可用
match is_service_available().await {
Ok(()) => {
log::info!(target: "app", "服务正在运行但版本检查失败: {err}");
/* logging!(
info,
Type::Service,
true,
"服务正在运行但版本检查失败: {}",
err
); */
false
}
_ => {
log::info!(target: "app", "服务不可用或未运行,需要重装");
// logging!(info, Type::Service, true, "服务不可用或未运行,需要重装");
true
}
}
}
Ok(version) => version != REQUIRED_SERVICE_VERSION,
Err(_) => false,
}
}
/// 尝试使用服务启动core
pub(super) async fn start_with_existing_service(config_file: &PathBuf) -> Result<()> {
log::info!(target:"app", "尝试使用现有服务启动核心 (IPC)");
// logging!(info, Type::Service, true, "尝试使用现有服务启动核心");
logging!(info, Type::Service, true, "尝试使用现有服务启动核心");
let clash_core = Config::verge().await.latest_ref().get_valid_clash_core();
let verge_config = Config::verge().await;
let clash_core = verge_config.latest_ref().get_valid_clash_core();
drop(verge_config);
let bin_ext = if cfg!(windows) { ".exe" } else { "" };
let clash_bin = format!("{clash_core}{bin_ext}");
let bin_path = current_exe()?.with_file_name(clash_bin);
let bin_path = dirs::path_to_str(&bin_path)?;
let bin_path = current_exe()?.with_file_name(format!("{clash_core}{bin_ext}"));
let config_dir = dirs::app_home_dir()?;
let config_dir = dirs::path_to_str(&config_dir)?;
let log_path = dirs::service_log_file()?;
let log_path = dirs::path_to_str(&log_path)?;
let config_file = dirs::path_to_str(config_file)?;
// 构建启动参数
let payload = serde_json::json!({
"core_type": clash_core,
"bin_path": bin_path,
"config_dir": config_dir,
"config_file": config_file,
"log_file": log_path,
"bin_path": dirs::path_to_str(&bin_path)?,
"config_dir": dirs::path_to_str(&dirs::app_home_dir()?)?,
"config_file": dirs::path_to_str(config_file)?,
"log_file": dirs::path_to_str(&dirs::service_log_file()?)?,
});
// log::info!(target:"app", "启动服务参数: {:?}", payload);
// logging!(info, Type::Service, true, "发送StartClash请求");
let response = send_ipc_request(IpcCommand::StartClash, payload)
.await
.context("无法连接到Clash Verge Service")?;
// 使用IPC通信
match send_ipc_request(IpcCommand::StartClash, payload).await {
Ok(response) => {
/* logging!(
info,
Type::Service,
true,
"收到StartClash响应: success={}, error={:?}",
response.success,
response.error
); */
if !response.success {
let err_msg = response.error.unwrap_or_else(|| "启动核心失败".to_string());
logging!(error, Type::Service, true, "启动核心失败: {}", err_msg);
bail!(err_msg);
}
// 添加对嵌套JSON结构的处理
if let Some(data) = &response.data
&& let Some(code) = data.get("code")
{
let code_value = code.as_u64().unwrap_or(1);
let msg = data
.get("msg")
.and_then(|m| m.as_str())
.unwrap_or("未知错误");
if code_value != 0 {
logging!(
error,
Type::Service,
true,
"启动核心返回错误: code={}, msg={}",
code_value,
msg
);
bail!("启动核心失败: {}", msg);
}
}
logging!(info, Type::Service, true, "服务成功启动核心");
Ok(())
}
Err(e) => {
logging!(error, Type::Service, true, "启动核心IPC通信失败: {}", e);
bail!("无法连接到Clash Verge Service: {}", e)
}
if !response.success {
let err_msg = response.error.unwrap_or_else(|| "启动核心失败".to_string());
bail!(err_msg);
}
if let Some(data) = &response.data
&& let Some(code) = data.get("code").and_then(|c| c.as_u64())
&& code != 0
{
let msg = data
.get("msg")
.and_then(|m| m.as_str())
.unwrap_or("未知错误");
bail!("启动核心失败: {}", msg);
}
logging!(info, Type::Service, true, "服务成功启动核心");
Ok(())
}
// 以服务启动core
pub(super) async fn run_core_by_service(config_file: &PathBuf) -> Result<()> {
log::info!(target: "app", "正在尝试通过服务启动核心");
logging!(info, Type::Service, true, "正在尝试通过服务启动核心");
// 先检查服务版本,不受冷却期限制
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 && is_service_available().await.is_ok() {
log::info!(target: "app", "服务已在运行且版本匹配,尝试使用");
return start_with_existing_service(config_file).await;
}
if !version_check {
log::info!(target: "app", "服务版本不匹配,尝试重装");
let service_state = ServiceState::get().await;
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(());
}
bail!("服务版本不匹配且无法重装,启动失败");
}
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", "服务重装成功,尝试启动");
return start_with_existing_service(config_file).await;
}
// 检查服务状态
match check_ipc_service_status().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")
reinstall_service().await?;
}
logging!(info, Type::Service, true, "服务已运行且版本匹配,直接使用");
start_with_existing_service(config_file).await
}
/// 通过服务停止core
@@ -909,7 +453,9 @@ pub(super) async fn stop_core_by_service() -> Result<()> {
.context("无法连接到Clash Verge Service")?;
if !response.success {
bail!(response.error.unwrap_or_else(|| "停止核心失败".to_string()));
let err_msg = response.error.unwrap_or_else(|| "停止核心失败".to_string());
logging!(error, Type::Service, true, "停止核心失败: {}", err_msg);
bail!(err_msg);
}
if let Some(data) = &response.data
@@ -934,54 +480,105 @@ pub(super) async fn stop_core_by_service() -> Result<()> {
}
}
logging!(info, Type::Service, true, "服务成功停止核心");
Ok(())
}
/// 检查服务是否正在运行
pub async fn is_service_available() -> Result<()> {
logging!(info, Type::Service, true, "开始检查服务是否正在运行");
check_service_version().await?;
Ok(())
}
match check_ipc_service_status().await {
Ok(resp) => {
if resp.code == 0 && resp.msg == "ok" && resp.data.is_some() {
logging!(info, Type::Service, true, "服务正在运行");
Ok(())
} else {
logging!(
warn,
Type::Service,
true,
"服务未正常运行: code={}, msg={}",
resp.code,
resp.msg
);
Ok(())
impl ServiceManager {
pub fn default() -> Self {
Self(ServiceStatus::Unavailable("Need Checks".into()))
}
pub fn current(&self) -> ServiceStatus {
self.0.clone()
}
pub async fn refresh(&mut self) -> Result<()> {
let status = self.check_service_comprehensive().await;
logging_error!(
Type::Service,
true,
self.handle_service_status(&status).await
);
self.0 = status;
Ok(())
}
/// 综合服务状态检查(一次性完成所有检查)
pub async fn check_service_comprehensive(&self) -> ServiceStatus {
match is_service_available().await {
Ok(_) => {
logging!(info, Type::Service, true, "服务当前可用,检查是否需要重装");
if check_service_needs_reinstall().await {
logging!(info, Type::Service, true, "服务需要重装且允许重装");
ServiceStatus::NeedsReinstall
} else {
ServiceStatus::Ready
}
}
Err(err) => {
logging!(warn, Type::Service, true, "服务不可用,检查安装状态");
ServiceStatus::Unavailable(err.to_string())
}
}
Err(err) => {
logging!(error, Type::Service, true, "检查服务运行状态失败: {}", err);
Err(err)
}
/// 根据服务状态执行相应操作
pub async fn handle_service_status(&mut self, status: &ServiceStatus) -> Result<()> {
match status {
ServiceStatus::Ready => {
logging!(info, Type::Service, true, "服务就绪,直接启动");
Ok(())
}
ServiceStatus::NeedsReinstall | ServiceStatus::ReinstallRequired => {
logging!(info, Type::Service, true, "服务需要重装,执行重装流程");
reinstall_service().await?;
self.0 = ServiceStatus::Ready;
Ok(())
}
ServiceStatus::ForceReinstallRequired => {
logging!(
info,
Type::Service,
true,
"服务需要强制重装,执行强制重装流程"
);
force_reinstall_service().await?;
self.0 = ServiceStatus::Ready;
Ok(())
}
ServiceStatus::InstallRequired => {
logging!(info, Type::Service, true, "需要安装服务,执行安装流程");
install_service().await?;
self.0 = ServiceStatus::Ready;
Ok(())
}
ServiceStatus::UninstallRequired => {
logging!(info, Type::Service, true, "服务需要卸载,执行卸载流程");
uninstall_service().await?;
self.0 = ServiceStatus::Unavailable("Service Uninstalled".into());
Ok(())
}
ServiceStatus::Unavailable(reason) => {
logging!(
info,
Type::Service,
true,
"服务不可用: {}将使用Sidecar模式",
reason
);
self.0 = ServiceStatus::Unavailable(reason.clone());
Err(anyhow::anyhow!("服务不可用: {}", reason))
}
}
}
}
/// 强制重装服务UI修复按钮
pub async fn force_reinstall_service() -> Result<()> {
log::info!(target: "app", "用户请求强制重装服务");
let service_state = ServiceState::default();
service_state.save().await?;
log::info!(target: "app", "已重置服务状态,开始执行重装");
match reinstall_service().await {
Ok(()) => {
log::info!(target: "app", "服务重装成功");
Ok(())
}
Err(err) => {
log::error!(target: "app", "强制重装服务失败: {err}");
bail!("强制重装服务失败: {}", err)
}
}
}
pub static SERVICE_MANAGER: Lazy<Mutex<ServiceManager>> =
Lazy::new(|| Mutex::new(ServiceManager::default()));