添加installer

This commit is contained in:
shenjack 2024-04-13 17:51:57 +08:00
parent a70961e3f3
commit c6e6ad413c
Signed by: shenjack
GPG Key ID: 7B1134A979775551
4 changed files with 82 additions and 1 deletions

View File

@ -24,3 +24,6 @@ winapi = { version = "0.3", features = ["winnt"] }
[dependencies]
blake3 = "1.5.1"
toml = "0.8"
[workspace]
members = ["installer"]

10
installer/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "installer"
version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = { version = "1.0.82", features = ["backtrace"] }
blake3 = "1.5.1"
clap = { version = "4.5.4", features = ["derive"] }
toml = "0.8.12"

68
installer/src/main.rs Normal file
View File

@ -0,0 +1,68 @@
use clap::Parser;
use blake3::Hasher;
#[derive(Clone, Parser, Debug)]
#[command(version, about)]
pub struct CliArg {
#[arg(long, short = 't')]
pub target_bin: String,
#[arg(long, short = 'c')]
pub check: bool,
}
#[derive(Clone)]
pub struct RawConfig {
pub show_console: Option<bool>,
pub verbose: Option<bool>,
pub chdir: Option<String>,
pub bin: Option<String>,
pub bin_arg: Option<String>,
pub config: Option<String>,
}
fn check_only(config: CliArg) -> anyhow::Result<()> {
let target = config.target_bin;
// 读取 target
let target_bin = std::fs::read(target)?;
// 读取最后 32 bit 作为校验码
let (data, verify_data) = target_bin.split_at(target_bin.len() - 32);
let mut hasher = Hasher::new();
hasher.update(data);
let hash = hasher.finalize();
if hash.as_bytes() != verify_data {
anyhow::bail!("校验码不匹配\n预期:{:?}\n实际:{:?}", hash.as_bytes(), verify_data);
}
let (data, data_len) = data.split_at(data.len() - 4);
let data_len = u32::from_le_bytes(data_len.try_into().unwrap()) as usize;
// 校验长度
if data_len > data.len() {
anyhow::bail!("长度不匹配 {} {}", data_len, data.len());
}
let (_, data) = data.split_at(data_len);
let data = std::str::from_utf8(data)?;
let config_value: toml::Value = toml::from_str(data)?;
println!("{:#?}", config_value);
Ok(())
}
fn main() -> anyhow::Result<()> {
let args = CliArg::parse();
if args.check {
check_only(args)
} else {
todo!()
}
}

View File

@ -29,7 +29,7 @@ pub fn read_self() -> Option<crate::config::RawConfig> {
);
return None;
}
let (data, data_len) = data.split_at(4);
let (data, data_len) = data.split_at(data.len() - 4);
let data_len = u32::from_le_bytes(data_len.try_into().unwrap()) as usize;
// 校验长度
// 长度不应大于 data.len()