feat: Improve core change configuration validation and error handling

This commit is contained in:
wonfen 2025-02-24 07:34:03 +08:00
parent 23f75598e5
commit 34f450fcdb
4 changed files with 79 additions and 68 deletions

View File

@ -71,32 +71,31 @@ impl Config {
crate::log_err!(Self::generate().await); crate::log_err!(Self::generate().await);
// 生成运行时配置文件并验证 // 生成运行时配置文件并验证
let runtime_path = dirs::app_home_dir()?.join(RUNTIME_CONFIG);
let config_result = Self::generate_file(ConfigType::Run); let config_result = Self::generate_file(ConfigType::Run);
let validation_result = if let Ok(_) = config_result { let validation_result = if let Ok(_) = config_result {
// 验证配置文件 // 验证配置文件
println!("[首次启动] 开始验证配置文件"); println!("[首次启动] 开始验证配置");
match CoreManager::global().validate_config().await { match CoreManager::global().validate_config().await {
Ok((is_valid, error_msg)) => { Ok((is_valid, error_msg)) => {
if !is_valid { if !is_valid {
println!("[首次启动] 配置验证失败,使用默认配置启动 {}", error_msg); println!("[首次启动] 配置验证失败,使用默认最小配置启动{}", error_msg);
// 使用默认配置
*Config::runtime().draft() = IRuntime {
config: Some(Config::clash().latest().0.clone()),
exists_keys: vec![],
chain_logs: Default::default(),
};
help::save_yaml(
&runtime_path,
&Config::clash().latest().0,
Some("# Clash Verge Runtime"),
)?;
if error_msg.is_empty() { if error_msg.is_empty() {
CoreManager::global()
.use_default_config(
"config_validate::boot_error",
"",
)
.await?;
Some(("config_validate::boot_error", String::new())) Some(("config_validate::boot_error", String::new()))
} else { } else {
CoreManager::global()
.use_default_config(
"config_validate::stderr_error",
&error_msg,
)
.await?;
Some(("config_validate::stderr_error", error_msg)) Some(("config_validate::stderr_error", error_msg))
} }
} else { } else {
@ -105,37 +104,24 @@ impl Config {
} }
} }
Err(err) => { Err(err) => {
println!("[首次启动] 验证进程执行失败 {}", err); println!("[首次启动] 验证进程执行失败: {}", err);
// 使用默认配置 CoreManager::global()
*Config::runtime().draft() = IRuntime { .use_default_config(
config: Some(Config::clash().latest().0.clone()), "config_validate::process_terminated",
exists_keys: vec![], "",
chain_logs: Default::default(), )
}; .await?;
help::save_yaml(
&runtime_path,
&Config::clash().latest().0,
Some("# Clash Verge Runtime"),
)?;
Some(("config_validate::process_terminated", String::new())) Some(("config_validate::process_terminated", String::new()))
} }
} }
} else { } else {
println!("[首次启动] 生成配置文件失败,使用默认配置"); println!("[首次启动] 生成配置文件失败,使用默认配置");
// 如果生成失败就将默认的clash文件拿过来 CoreManager::global()
*Config::runtime().draft() = IRuntime { .use_default_config(
config: Some(Config::clash().latest().0.clone()), "config_validate::error",
exists_keys: vec![], "",
chain_logs: Default::default(), )
}; .await?;
if !runtime_path.exists() {
help::save_yaml(
&runtime_path,
&Config::clash().latest().0,
Some("# Clash Verge Runtime"),
)?;
}
Some(("config_validate::error", String::new())) Some(("config_validate::error", String::new()))
}; };

View File

@ -2,7 +2,7 @@ use crate::config::*;
use crate::core::{clash_api, handle, service}; use crate::core::{clash_api, handle, service};
use crate::core::tray::Tray; use crate::core::tray::Tray;
use crate::log_err; use crate::log_err;
use crate::utils::dirs; use crate::utils::{dirs, help};
use anyhow::{bail, Result}; use anyhow::{bail, Result};
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use serde_yaml::Mapping; use serde_yaml::Mapping;
@ -32,28 +32,6 @@ impl CoreManager {
Ok(()) Ok(())
} }
/// 检查订阅是否正确
pub async fn check_config(&self) -> Result<()> {
let config_path = Config::generate_file(ConfigType::Check)?;
let config_path = dirs::path_to_str(&config_path)?;
let clash_core = { Config::verge().latest().clash_core.clone() };
let clash_core = clash_core.unwrap_or("verge-mihomo".into());
let test_dir = dirs::app_home_dir()?.join("test");
let test_dir = dirs::path_to_str(&test_dir)?;
let app_handle = handle::Handle::global().app_handle().unwrap();
let _ = app_handle
.shell()
.sidecar(clash_core)?
.args(["-t", "-d", test_dir, "-f", config_path])
.output()
.await?;
Ok(())
}
/// 停止核心运行 /// 停止核心运行
pub async fn stop_core(&self) -> Result<()> { pub async fn stop_core(&self) -> Result<()> {
let mut running = self.running.lock().await; let mut running = self.running.lock().await;
@ -112,6 +90,23 @@ impl CoreManager {
Ok(()) Ok(())
} }
/// 使用默认配置
pub async fn use_default_config(&self, msg_type: &str, msg_content: &str) -> Result<()> {
let runtime_path = dirs::app_home_dir()?.join(RUNTIME_CONFIG);
*Config::runtime().draft() = IRuntime {
config: Some(Config::clash().latest().0.clone()),
exists_keys: vec![],
chain_logs: Default::default(),
};
help::save_yaml(
&runtime_path,
&Config::clash().latest().0,
Some("# Clash Verge Runtime"),
)?;
handle::Handle::notice_message(msg_type, msg_content);
Ok(())
}
/// 切换核心 /// 切换核心
pub async fn change_core(&self, clash_core: Option<String>) -> Result<()> { pub async fn change_core(&self, clash_core: Option<String>) -> Result<()> {
let clash_core = clash_core.ok_or(anyhow::anyhow!("clash core is null"))?; let clash_core = clash_core.ok_or(anyhow::anyhow!("clash core is null"))?;
@ -122,13 +117,39 @@ impl CoreManager {
} }
log::info!(target: "app", "change core to `{clash_core}`"); log::info!(target: "app", "change core to `{clash_core}`");
Config::verge().draft().clash_core = Some(clash_core); Config::verge().draft().clash_core = Some(clash_core);
// 更新订阅
Config::generate().await?; Config::generate().await?;
self.check_config().await?; // 验证配置
println!("[切换内核] 开始验证配置");
match self.validate_config().await {
Ok((is_valid, error_msg)) => {
if !is_valid {
println!("[切换内核] 配置验证失败: {}", error_msg);
if error_msg.is_empty() {
self.use_default_config(
"config_validate::core_change",
"",
).await?;
} else {
self.use_default_config(
"config_validate::stderr_error",
&error_msg,
).await?;
}
} else {
println!("[切换内核] 配置验证成功");
handle::Handle::notice_message("config_validate::success", "");
}
}
Err(err) => {
println!("[切换内核] 验证进程执行失败: {}", err);
self.use_default_config(
"config_validate::process_terminated",
"",
).await?;
}
}
match self.restart_core().await { match self.restart_core().await {
Ok(_) => { Ok(_) => {

View File

@ -438,5 +438,6 @@
"Lite Mode Info": "关闭GUI界面仅保留内核运行", "Lite Mode Info": "关闭GUI界面仅保留内核运行",
"Config Validation Failed": "订阅配置校验失败,请检查订阅配置文件,修改已回滚", "Config Validation Failed": "订阅配置校验失败,请检查订阅配置文件,修改已回滚",
"Boot Config Validation Failed": "启动订阅配置校验失败,使用默认配置启动,请检查订阅配置文件", "Boot Config Validation Failed": "启动订阅配置校验失败,使用默认配置启动,请检查订阅配置文件",
"Core Change Config Validation Failed": "切换内核时配置校验失败,使用默认配置启动,请检查订阅配置文件",
"Config Validation Process Terminated": "验证进程被终止" "Config Validation Process Terminated": "验证进程被终止"
} }

View File

@ -60,6 +60,9 @@ const handleNoticeMessage = (
case "config_validate::boot_error": case "config_validate::boot_error":
Notice.error(t("Boot Config Validation Failed")); Notice.error(t("Boot Config Validation Failed"));
break; break;
case "config_validate::core_change":
Notice.error(t("Core Change Config Validation Failed"));
break;
case "config_validate::error": case "config_validate::error":
Notice.error(t("Config Validation Failed")); Notice.error(t("Config Validation Failed"));
break; break;