feat: update script-verify.rs

This commit is contained in:
2025-09-09 23:11:08 +08:00
parent db816cc1e1
commit f74931f8bd
5 changed files with 113 additions and 32 deletions

View File

@@ -5,24 +5,45 @@
//! clap = { version = "4.5", features = ["derive"] }
//! rust_util = "0.6"
//! script-sign = "0.1"
//! serde = { version = "1.0", features = ["serde_derive"] }
//! serde_json = "1.0"
//! ```
use clap::Parser;
use rust_util::{failure, information, success, warning};
use rust_util::{failure, information, success, util_msg, warning};
use script_sign::{KeyMap, Script};
use std::fs;
use std::path::PathBuf;
use serde::Serialize;
/// Script signing tool
#[derive(Parser, Debug)]
#[command(version, about, long_about = None, bin_name = "script-verify.rs")]
struct Args {
/// JSON outputs
#[arg(long)]
json: bool,
/// Script file path
scripts: Vec<PathBuf>,
}
#[derive(Clone, Debug, Serialize)]
struct ScriptVerification {
name: String,
success: bool,
#[serde(skip_serializing_if = "Option::is_none")]
sign_key_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
sign_time: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
message: Option<String>,
}
fn main() {
let args = Args::parse();
if args.json {
util_msg::set_logger_std_out(false);
}
let key_map = KeyMap::system();
let total_scripts = args.scripts.len();
@@ -31,23 +52,40 @@ fn main() {
return;
}
let mut script_verifications = vec![];
for (i, script_path) in args.scripts.iter().enumerate() {
let script_path_display = script_path.display().to_string();
information!(
"Verifying {}/{}: {}",
(i + 1),
total_scripts,
script_path.display()
script_path_display
);
if !script_path.is_file() {
warning!("Not a file: {}", script_path.display());
warning!("Not a file: {}", script_path_display);
script_verifications.push(ScriptVerification {
name: script_path_display.clone(),
success: false,
sign_key_id: None,
sign_time: None,
message: Some(format!("Not a file: {}", script_path_display)),
});
continue;
}
let script_content = match fs::read_to_string(script_path) {
Ok(script_content) => script_content,
Err(e) => {
warning!("Read script: {} failed: {}", script_path.display(), e);
warning!("Read script: {} failed: {}", script_path_display, e);
script_verifications.push(ScriptVerification {
name: script_path_display.clone(),
success: false,
sign_key_id: None,
sign_time: None,
message: Some(format!("Read script: {} failed: {}", script_path_display, e)),
});
continue;
}
};
@@ -55,7 +93,14 @@ fn main() {
let script = match Script::parse(&script_content) {
Ok(script) => script,
Err(e) => {
warning!("Read script: {} failed: {}", script_path.display(), e);
warning!("Read script: {} failed: {}", script_path_display, e);
script_verifications.push(ScriptVerification {
name: script_path_display.clone(),
success: false,
sign_key_id: None,
sign_time: None,
message: Some(format!("Read script: {} failed: {}", script_path_display, e)),
});
continue;
}
};
@@ -65,23 +110,55 @@ fn main() {
Ok(true) => {
success!(
"Verify script success: {}, key ID: {}, sign date: {}",
script_path.display(),
script_path_display,
signature.key_id,
signature.time
);
script_verifications.push(ScriptVerification {
name: script_path_display.clone(),
success: true,
sign_key_id: Some(signature.key_id.clone()),
sign_time: Some(signature.time.clone()),
message: None,
});
}
Ok(false) => {
failure!("Verify script failed: {}", script_path.display());
failure!("Verify script failed: {}", script_path_display);
script_verifications.push(ScriptVerification {
name: script_path_display.clone(),
success: false,
sign_key_id: None,
sign_time: None,
message: Some(format!("Verify script failed: {}", script_path_display)),
});
}
Err(e) => {
warning!("Verify script: {} failed: {}", script_path.display(), e);
warning!("Verify script: {} failed: {}", script_path_display, e);
script_verifications.push(ScriptVerification {
name: script_path_display.clone(),
success: false,
sign_key_id: None,
sign_time: None,
message: Some(format!("Verify script: {} failed: {}", script_path_display, e)),
});
}
}
} else {
warning!("Script is not signed: {}", script_path.display());
warning!("Script is not signed: {}", script_path_display);
script_verifications.push(ScriptVerification {
name: script_path_display.clone(),
success: false,
sign_key_id: None,
sign_time: None,
message: Some(format!("Script is not signed: {}", script_path_display)),
});
}
}
if args.json {
println!("{}", serde_json::to_string_pretty(&script_verifications).unwrap());
}
}
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20250123T234559+08:00.MEUCIQDhj1MHHjBdFiK9lxMV
// EGE3RMJMcRR521i3y0ZifmIYVgIgMHdfszarXyn1fjyY2zo2y22OmXb7VTzFVbD/mdK0/mw=
// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20250909T231004+08:00.MEQCIDSni3F6bS8F9SD6tjdL
// 6P8BkVrqJn1FwKUhro7nbJHWAiAVvDq/M4qpb1LutG0sw8zqx2dNeOpzDRNneEkumUsOlg==