mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
feat: enhance script validation and error handling
This commit is contained in:
@@ -213,7 +213,22 @@ pub async fn save_profile_file(index: String, file_data: Option<String>) -> CmdR
|
||||
println!("[cmd配置save] 验证失败: {}", error_msg);
|
||||
// 恢复原始配置文件
|
||||
wrap_err!(fs::write(&file_path, original_content))?;
|
||||
handle::Handle::notice_message("config_validate::error", &error_msg.to_string()); // 保存文件弹出此提示
|
||||
|
||||
// 智能判断是否为脚本错误
|
||||
let is_script_error = file_path_str.ends_with(".js") ||
|
||||
error_msg.contains("Script syntax error") ||
|
||||
error_msg.contains("Script must contain a main function") ||
|
||||
error_msg.contains("Failed to read script file");
|
||||
|
||||
if is_script_error {
|
||||
// 脚本错误使用专门的通知处理
|
||||
let result = (false, error_msg.clone());
|
||||
handle_script_validation_notice(&result, "脚本文件");
|
||||
} else {
|
||||
// 普通配置错误使用一般通知
|
||||
handle::Handle::notice_message("config_validate::error", &error_msg);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -281,8 +296,23 @@ pub async fn patch_verge_config(payload: IVerge) -> CmdResult {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn change_clash_core(clash_core: Option<String>) -> CmdResult {
|
||||
wrap_err!(CoreManager::global().change_core(clash_core).await)
|
||||
pub async fn change_clash_core(clash_core: String) -> CmdResult<Option<String>> {
|
||||
log::info!(target: "app", "changing core to {clash_core}");
|
||||
|
||||
match CoreManager::global().change_core(Some(clash_core.clone())).await {
|
||||
Ok(_) => {
|
||||
log::info!(target: "app", "core changed to {clash_core}");
|
||||
handle::Handle::notice_message("config_core::change_success", &clash_core);
|
||||
handle::Handle::refresh_clash();
|
||||
Ok(None)
|
||||
}
|
||||
Err(err) => {
|
||||
let error_msg = err.to_string();
|
||||
log::error!(target: "app", "failed to change core: {error_msg}");
|
||||
handle::Handle::notice_message("config_core::change_error", &error_msg);
|
||||
Ok(Some(error_msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// restart the sidecar
|
||||
@@ -526,3 +556,53 @@ pub mod uwp {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn script_validate_notice(status: String, msg: String) -> CmdResult {
|
||||
handle::Handle::notice_message(&status, &msg);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 处理脚本验证相关的所有消息通知
|
||||
/// 统一通知接口,保持消息类型一致性
|
||||
pub fn handle_script_validation_notice(result: &(bool, String), file_type: &str) {
|
||||
if !result.0 {
|
||||
let error_msg = &result.1;
|
||||
|
||||
// 根据错误消息内容判断错误类型
|
||||
let status = if error_msg.starts_with("File not found:") {
|
||||
"config_validate::file_not_found"
|
||||
} else if error_msg.starts_with("Failed to read script file:") {
|
||||
"config_validate::script_error"
|
||||
} else if error_msg.starts_with("Script syntax error:") {
|
||||
"config_validate::script_syntax_error"
|
||||
} else if error_msg == "Script must contain a main function" {
|
||||
"config_validate::script_missing_main"
|
||||
} else {
|
||||
// 如果是其他类型错误,作为一般脚本错误处理
|
||||
"config_validate::script_error"
|
||||
};
|
||||
|
||||
log::warn!(target: "app", "{} 验证失败: {}", file_type, error_msg);
|
||||
handle::Handle::notice_message(status, error_msg);
|
||||
}
|
||||
}
|
||||
|
||||
/// 验证指定脚本文件
|
||||
#[tauri::command]
|
||||
pub async fn validate_script_file(file_path: String) -> CmdResult<bool> {
|
||||
log::info!(target: "app", "验证脚本文件: {}", file_path);
|
||||
|
||||
match CoreManager::global().validate_config_file(&file_path).await {
|
||||
Ok(result) => {
|
||||
handle_script_validation_notice(&result, "脚本文件");
|
||||
Ok(result.0) // 返回验证结果布尔值
|
||||
},
|
||||
Err(e) => {
|
||||
let error_msg = e.to_string();
|
||||
log::error!(target: "app", "验证脚本文件过程发生错误: {}", error_msg);
|
||||
handle::Handle::notice_message("config_validate::process_terminated", &error_msg);
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,9 +240,101 @@ impl CoreManager {
|
||||
|
||||
/// 验证指定的配置文件
|
||||
pub async fn validate_config_file(&self, config_path: &str) -> Result<(bool, String)> {
|
||||
// 检查文件是否存在
|
||||
if !std::path::Path::new(config_path).exists() {
|
||||
let error_msg = format!("File not found: {}", config_path);
|
||||
//handle::Handle::notice_message("config_validate::file_not_found", &error_msg);
|
||||
return Ok((false, error_msg));
|
||||
}
|
||||
|
||||
// 检查是否为脚本文件
|
||||
let is_script = if config_path.ends_with(".js") {
|
||||
true
|
||||
} else {
|
||||
match self.is_script_file(config_path) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
// 如果无法确定文件类型,尝试使用Clash内核验证
|
||||
log::warn!(target: "app", "无法确定文件类型: {}, 错误: {}", config_path, err);
|
||||
return self.validate_config_internal(config_path).await;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if is_script {
|
||||
log::info!(target: "app", "检测到脚本文件,使用JavaScript验证: {}", config_path);
|
||||
return self.validate_script_file(config_path).await;
|
||||
}
|
||||
|
||||
// 对YAML配置文件使用Clash内核验证
|
||||
log::info!(target: "app", "使用Clash内核验证配置文件: {}", config_path);
|
||||
self.validate_config_internal(config_path).await
|
||||
}
|
||||
|
||||
/// 检查文件是否为脚本文件
|
||||
fn is_script_file(&self, path: &str) -> Result<bool> {
|
||||
let content = match std::fs::read_to_string(path) {
|
||||
Ok(content) => content,
|
||||
Err(err) => {
|
||||
log::warn!(target: "app", "无法读取文件以检测类型: {}, 错误: {}", path, err);
|
||||
return Err(anyhow::anyhow!("Failed to read file to detect type: {}", err));
|
||||
}
|
||||
};
|
||||
|
||||
// 检查文件前几行是否包含JavaScript特征
|
||||
let first_lines = content.lines().take(5).collect::<String>();
|
||||
Ok(first_lines.contains("function") ||
|
||||
first_lines.contains("//") ||
|
||||
first_lines.contains("/*") ||
|
||||
first_lines.contains("import") ||
|
||||
first_lines.contains("export") ||
|
||||
first_lines.contains("const ") ||
|
||||
first_lines.contains("let "))
|
||||
}
|
||||
|
||||
/// 验证脚本文件语法
|
||||
async fn validate_script_file(&self, path: &str) -> Result<(bool, String)> {
|
||||
// 读取脚本内容
|
||||
let content = match std::fs::read_to_string(path) {
|
||||
Ok(content) => content,
|
||||
Err(err) => {
|
||||
let error_msg = format!("Failed to read script file: {}", err);
|
||||
//handle::Handle::notice_message("config_validate::script_error", &error_msg);
|
||||
return Ok((false, error_msg));
|
||||
}
|
||||
};
|
||||
|
||||
log::debug!(target: "app", "验证脚本文件: {}", path);
|
||||
|
||||
// 使用boa引擎进行基本语法检查
|
||||
use boa_engine::{Context, Source};
|
||||
|
||||
let mut context = Context::default();
|
||||
let result = context.eval(Source::from_bytes(&content));
|
||||
|
||||
match result {
|
||||
Ok(_) => {
|
||||
log::debug!(target: "app", "脚本语法验证通过: {}", path);
|
||||
|
||||
// 检查脚本是否包含main函数
|
||||
if !content.contains("function main") && !content.contains("const main") && !content.contains("let main") {
|
||||
let error_msg = "Script must contain a main function";
|
||||
log::warn!(target: "app", "脚本缺少main函数: {}", path);
|
||||
//handle::Handle::notice_message("config_validate::script_missing_main", error_msg);
|
||||
return Ok((false, error_msg.to_string()));
|
||||
}
|
||||
|
||||
Ok((true, String::new()))
|
||||
},
|
||||
Err(err) => {
|
||||
let error_msg = format!("Script syntax error: {}", err);
|
||||
log::warn!(target: "app", "脚本语法错误: {}", err);
|
||||
//handle::Handle::notice_message("config_validate::script_syntax_error", &error_msg);
|
||||
Ok((false, error_msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 更新proxies等配置
|
||||
pub async fn update_config(&self) -> Result<(bool, String)> {
|
||||
println!("[core配置更新] 开始更新配置");
|
||||
@@ -303,3 +395,84 @@ impl CoreManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
async fn create_test_script() -> Result<String> {
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let script_path = temp_dir.join("test_script.js");
|
||||
let script_content = r#"
|
||||
// This is a test script
|
||||
function main(config) {
|
||||
console.log("Testing script");
|
||||
return config;
|
||||
}
|
||||
"#;
|
||||
|
||||
fs::write(&script_path, script_content)?;
|
||||
Ok(script_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
async fn create_invalid_script() -> Result<String> {
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let script_path = temp_dir.join("invalid_script.js");
|
||||
let script_content = r#"
|
||||
// This is an invalid script
|
||||
function main(config { // Missing closing parenthesis
|
||||
console.log("Testing script");
|
||||
return config;
|
||||
}
|
||||
"#;
|
||||
|
||||
fs::write(&script_path, script_content)?;
|
||||
Ok(script_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
async fn create_no_main_script() -> Result<String> {
|
||||
let temp_dir = std::env::temp_dir();
|
||||
let script_path = temp_dir.join("no_main_script.js");
|
||||
let script_content = r#"
|
||||
// This script has no main function
|
||||
function helper(config) {
|
||||
console.log("Testing script");
|
||||
return config;
|
||||
}
|
||||
"#;
|
||||
|
||||
fs::write(&script_path, script_content)?;
|
||||
Ok(script_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_validate_script_file() -> Result<()> {
|
||||
let core_manager = CoreManager::global();
|
||||
|
||||
// 测试有效脚本
|
||||
let script_path = create_test_script().await?;
|
||||
let result = core_manager.validate_config_file(&script_path).await?;
|
||||
assert!(result.0, "有效脚本应该通过验证");
|
||||
|
||||
// 测试无效脚本
|
||||
let invalid_script_path = create_invalid_script().await?;
|
||||
let result = core_manager.validate_config_file(&invalid_script_path).await?;
|
||||
assert!(!result.0, "无效脚本不应该通过验证");
|
||||
assert!(result.1.contains("脚本语法错误"), "无效脚本应该返回语法错误");
|
||||
|
||||
// 测试缺少main函数的脚本
|
||||
let no_main_script_path = create_no_main_script().await?;
|
||||
let result = core_manager.validate_config_file(&no_main_script_path).await?;
|
||||
assert!(!result.0, "缺少main函数的脚本不应该通过验证");
|
||||
assert!(result.1.contains("缺少main函数"), "应该提示缺少main函数");
|
||||
|
||||
// 清理测试文件
|
||||
let _ = fs::remove_file(script_path);
|
||||
let _ = fs::remove_file(invalid_script_path);
|
||||
let _ = fs::remove_file(no_main_script_path);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,6 +114,9 @@ pub fn run() {
|
||||
cmds::delete_profile,
|
||||
cmds::read_profile_file,
|
||||
cmds::save_profile_file,
|
||||
// script validation
|
||||
cmds::script_validate_notice,
|
||||
cmds::validate_script_file,
|
||||
// clash api
|
||||
cmds::clash_api_get_proxy_delay,
|
||||
// backup
|
||||
|
||||
Reference in New Issue
Block a user