in src/main.rs [101:133]
fn main() -> std::io::Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("imds_get <path>");
std::process::exit(1);
}
let sub_uri = args[1].clone();
// First let's check if imdsv2 is enabled
let imdsv2 = match request("GET", "/", HashMap::new()) {
Ok((status, _)) => status == 401,
Err(e) => e.to_string().to_lowercase().contains("unauthorized"),
};
let mut headers: HashMap<String, String> = HashMap::new();
if imdsv2 {
imdsv2_handle(&mut headers)?;
}
let (status_code, bytes) = request("GET", sub_uri.as_str(), headers)?;
if status_code == 404 {
std::process::exit(1);
}
let mut stdout = std::io::stdout();
stdout
.write_all(bytes.as_slice())
.expect("failed to write imdsv2 data to output");
stdout.flush().expect("failed to flush std output");
Ok(())
}