mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 02:13:44 +08:00
feat: Improve core change configuration validation and error handling
This commit is contained in:
parent
5ab8e7a7c7
commit
befc856207
@ -71,32 +71,31 @@ impl Config {
|
||||
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 validation_result = if let Ok(_) = config_result {
|
||||
// 验证配置文件
|
||||
println!("[首次启动] 开始验证配置文件");
|
||||
println!("[首次启动] 开始验证配置");
|
||||
|
||||
match CoreManager::global().validate_config().await {
|
||||
Ok((is_valid, error_msg)) => {
|
||||
if !is_valid {
|
||||
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"),
|
||||
)?;
|
||||
|
||||
println!("[首次启动] 配置验证失败,使用默认最小配置启动{}", error_msg);
|
||||
if error_msg.is_empty() {
|
||||
CoreManager::global()
|
||||
.use_default_config(
|
||||
"config_validate::boot_error",
|
||||
"",
|
||||
)
|
||||
.await?;
|
||||
Some(("config_validate::boot_error", String::new()))
|
||||
} else {
|
||||
CoreManager::global()
|
||||
.use_default_config(
|
||||
"config_validate::stderr_error",
|
||||
&error_msg,
|
||||
)
|
||||
.await?;
|
||||
Some(("config_validate::stderr_error", error_msg))
|
||||
}
|
||||
} else {
|
||||
@ -105,37 +104,24 @@ impl Config {
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
println!("[首次启动] 验证进程执行失败 {}", err);
|
||||
// 使用默认配置
|
||||
*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"),
|
||||
)?;
|
||||
|
||||
println!("[首次启动] 验证进程执行失败: {}", err);
|
||||
CoreManager::global()
|
||||
.use_default_config(
|
||||
"config_validate::process_terminated",
|
||||
"",
|
||||
)
|
||||
.await?;
|
||||
Some(("config_validate::process_terminated", String::new()))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("[首次启动] 生成配置文件失败,使用默认配置");
|
||||
// 如果生成失败就将默认的clash文件拿过来
|
||||
*Config::runtime().draft() = IRuntime {
|
||||
config: Some(Config::clash().latest().0.clone()),
|
||||
exists_keys: vec![],
|
||||
chain_logs: Default::default(),
|
||||
};
|
||||
if !runtime_path.exists() {
|
||||
help::save_yaml(
|
||||
&runtime_path,
|
||||
&Config::clash().latest().0,
|
||||
Some("# Clash Verge Runtime"),
|
||||
)?;
|
||||
}
|
||||
CoreManager::global()
|
||||
.use_default_config(
|
||||
"config_validate::error",
|
||||
"",
|
||||
)
|
||||
.await?;
|
||||
Some(("config_validate::error", String::new()))
|
||||
};
|
||||
|
||||
|
@ -2,7 +2,7 @@ use crate::config::*;
|
||||
use crate::core::{clash_api, handle, service};
|
||||
use crate::core::tray::Tray;
|
||||
use crate::log_err;
|
||||
use crate::utils::dirs;
|
||||
use crate::utils::{dirs, help};
|
||||
use anyhow::{bail, Result};
|
||||
use once_cell::sync::OnceCell;
|
||||
use serde_yaml::Mapping;
|
||||
@ -32,28 +32,6 @@ impl CoreManager {
|
||||
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<()> {
|
||||
let mut running = self.running.lock().await;
|
||||
@ -112,6 +90,23 @@ impl CoreManager {
|
||||
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<()> {
|
||||
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}`");
|
||||
|
||||
Config::verge().draft().clash_core = Some(clash_core);
|
||||
|
||||
// 更新订阅
|
||||
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 {
|
||||
Ok(_) => {
|
||||
|
@ -438,5 +438,6 @@
|
||||
"Lite Mode Info": "关闭GUI界面,仅保留内核运行",
|
||||
"Config Validation Failed": "订阅配置校验失败,请检查订阅配置文件,修改已回滚",
|
||||
"Boot Config Validation Failed": "启动订阅配置校验失败,使用默认配置启动,请检查订阅配置文件",
|
||||
"Core Change Config Validation Failed": "切换内核时配置校验失败,使用默认配置启动,请检查订阅配置文件",
|
||||
"Config Validation Process Terminated": "验证进程被终止"
|
||||
}
|
||||
|
@ -60,6 +60,9 @@ const handleNoticeMessage = (
|
||||
case "config_validate::boot_error":
|
||||
Notice.error(t("Boot Config Validation Failed"));
|
||||
break;
|
||||
case "config_validate::core_change":
|
||||
Notice.error(t("Core Change Config Validation Failed"));
|
||||
break;
|
||||
case "config_validate::error":
|
||||
Notice.error(t("Config Validation Failed"));
|
||||
break;
|
||||
|
Loading…
x
Reference in New Issue
Block a user