Files
idevice/tools/src/afc.rs
Jackson Coxson 1a7534a106 afc write file
2025-04-06 11:53:45 -06:00

162 lines
5.7 KiB
Rust

// Jackson Coxson
use std::path::PathBuf;
use clap::{value_parser, Arg, Command};
use idevice::{
afc::{opcode::AfcFopenMode, AfcClient},
IdeviceService,
};
mod common;
#[tokio::main]
async fn main() {
env_logger::init();
let matches = Command::new("afc")
.about("Start a tunnel")
.arg(
Arg::new("host")
.long("host")
.value_name("HOST")
.help("IP address of the device"),
)
.arg(
Arg::new("pairing_file")
.long("pairing-file")
.value_name("PATH")
.help("Path to the pairing file"),
)
.arg(
Arg::new("udid")
.value_name("UDID")
.help("UDID of the device (overrides host/pairing file)"),
)
.arg(
Arg::new("about")
.long("about")
.help("Show about information")
.action(clap::ArgAction::SetTrue),
)
.subcommand(
Command::new("list")
.about("Lists the items in the directory")
.arg(Arg::new("path").required(true).index(1)),
)
.subcommand(
Command::new("download")
.about("Creates a directory")
.arg(Arg::new("path").required(true).index(1))
.arg(Arg::new("save").required(true).index(2)),
)
.subcommand(
Command::new("upload")
.about("Creates a directory")
.arg(
Arg::new("file")
.required(true)
.index(1)
.value_parser(value_parser!(PathBuf)),
)
.arg(Arg::new("path").required(true).index(2)),
)
.subcommand(
Command::new("mkdir")
.about("Creates a directory")
.arg(Arg::new("path").required(true).index(1)),
)
.subcommand(
Command::new("remove")
.about("Remove a provisioning profile")
.arg(Arg::new("path").required(true).index(1)),
)
.subcommand(
Command::new("remove_all")
.about("Remove a provisioning profile")
.arg(Arg::new("path").required(true).index(1)),
)
.subcommand(
Command::new("info")
.about("Get info about a file")
.arg(Arg::new("path").required(true).index(1)),
)
.subcommand(Command::new("device_info").about("Get info about the device"))
.get_matches();
if matches.get_flag("about") {
println!("afc");
println!("Copyright (c) 2025 Jackson Coxson");
return;
}
let udid = matches.get_one::<String>("udid");
let host = matches.get_one::<String>("host");
let pairing_file = matches.get_one::<String>("pairing_file");
let provider = match common::get_provider(udid, host, pairing_file, "afc-jkcoxson").await {
Ok(p) => p,
Err(e) => {
eprintln!("{e}");
return;
}
};
let mut afc_client = AfcClient::connect(&*provider)
.await
.expect("Unable to connect to misagent");
if let Some(matches) = matches.subcommand_matches("list") {
let path = matches.get_one::<String>("path").expect("No path passed");
let res = afc_client.list_dir(path).await.expect("Failed to read dir");
println!("{path}\n{res:#?}");
} else if let Some(matches) = matches.subcommand_matches("mkdir") {
let path = matches.get_one::<String>("path").expect("No path passed");
afc_client.mk_dir(path).await.expect("Failed to mkdir");
} else if let Some(matches) = matches.subcommand_matches("download") {
let path = matches.get_one::<String>("path").expect("No path passed");
let save = matches.get_one::<String>("save").expect("No path passed");
let mut file = afc_client
.open(path, AfcFopenMode::RdOnly)
.await
.expect("Failed to open");
let res = file.read().await.expect("Failed to read");
tokio::fs::write(save, res)
.await
.expect("Failed to write to file");
} else if let Some(matches) = matches.subcommand_matches("upload") {
let file = matches.get_one::<PathBuf>("file").expect("No path passed");
let path = matches.get_one::<String>("path").expect("No path passed");
let bytes = tokio::fs::read(file).await.expect("Failed to read file");
let mut file = afc_client
.open(path, AfcFopenMode::WrOnly)
.await
.expect("Failed to open");
file.write(&bytes).await.expect("Failed to upload bytes");
} else if let Some(matches) = matches.subcommand_matches("remove") {
let path = matches.get_one::<String>("path").expect("No path passed");
afc_client.remove(path).await.expect("Failed to remove");
} else if let Some(matches) = matches.subcommand_matches("remove_all") {
let path = matches.get_one::<String>("path").expect("No path passed");
afc_client.remove_all(path).await.expect("Failed to remove");
} else if let Some(matches) = matches.subcommand_matches("info") {
let path = matches.get_one::<String>("path").expect("No path passed");
let res = afc_client
.get_file_info(path)
.await
.expect("Failed to get file info");
println!("{res:#?}");
} else if matches.subcommand_matches("device_info").is_some() {
let res = afc_client
.get_device_info()
.await
.expect("Failed to get file info");
println!("{res:#?}");
} else {
eprintln!("Invalid usage, pass -h for help");
}
}