diff --git a/Cargo.toml b/Cargo.toml index 006a0f4..243e3f4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,6 @@ winapi = { version = "0.3", features = ["winnt"] } [dependencies] blake3 = "1.5.1" toml = "0.8" + +[workspace] +members = ["installer"] diff --git a/installer/Cargo.toml b/installer/Cargo.toml new file mode 100644 index 0000000..380f7cf --- /dev/null +++ b/installer/Cargo.toml @@ -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" diff --git a/installer/src/main.rs b/installer/src/main.rs new file mode 100644 index 0000000..39ad467 --- /dev/null +++ b/installer/src/main.rs @@ -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, + pub verbose: Option, + pub chdir: Option, + pub bin: Option, + pub bin_arg: Option, + pub config: Option, +} + + +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!() + } + +} diff --git a/src/reader.rs b/src/reader.rs index 6641cb7..82e385c 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -29,7 +29,7 @@ pub fn read_self() -> Option { ); 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()