Add companion proxy support

This commit is contained in:
Jackson Coxson
2025-08-11 16:40:04 -06:00
parent f8477ed77c
commit c79fb2226a
7 changed files with 326 additions and 159 deletions

View File

@@ -25,9 +25,9 @@ path = "src/instproxy.rs"
name = "mounter"
path = "src/mounter.rs"
[[bin]]
name = "core_device_proxy_tun"
path = "src/core_device_proxy_tun.rs"
# [[bin]]
# name = "core_device_proxy_tun"
# path = "src/core_device_proxy_tun.rs"
[[bin]]
name = "idevice_id"
@@ -93,12 +93,16 @@ path = "src/lockdown.rs"
name = "restore_service"
path = "src/restore_service.rs"
[[bin]]
name = "companion_proxy"
path = "src/companion_proxy.rs"
[dependencies]
idevice = { path = "../idevice", features = ["full"], default-features = false }
tokio = { version = "1.43", features = ["full"] }
log = { version = "0.4" }
env_logger = { version = "0.11" }
tun-rs = { version = "1.5", features = ["async"] }
# tun-rs = { version = "1.5", features = ["async"] }
sha2 = { version = "0.10" }
ureq = { version = "3" }
clap = { version = "4.5" }

View File

@@ -0,0 +1,150 @@
// Jackson Coxson
use clap::{arg, Arg, Command};
use idevice::{
companion_proxy::CompanionProxy, core_device_proxy::CoreDeviceProxy, pretty_print_dictionary,
pretty_print_plist, rsd::RsdHandshake, IdeviceService, RsdService,
};
mod common;
#[tokio::main]
async fn main() {
env_logger::init();
let matches = Command::new("companion_proxy")
.about("Apple Watch things")
.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)")
.index(1),
)
.arg(
Arg::new("about")
.long("about")
.help("Show about information")
.action(clap::ArgAction::SetTrue),
)
.subcommand(Command::new("list").about("List the companions on the device"))
.subcommand(Command::new("listen").about("Listen for devices"))
.subcommand(
Command::new("get")
.about("Gets a value")
.arg(arg!(-d --device_udid <STRING> "the device udid to get from").required(true))
.arg(arg!(-v --value <STRING> "the value to get").required(true)),
)
.subcommand(
Command::new("start")
.about("Starts a service")
.arg(arg!(-p --port <PORT> "the port").required(true))
.arg(arg!(-n --name <STRING> "the optional service name").required(false)),
)
.subcommand(
Command::new("stop")
.about("Starts a service")
.arg(arg!(-p --port <PORT> "the port").required(true)),
)
.get_matches();
if matches.get_flag("about") {
println!("companion_proxy");
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, "amfi-jkcoxson").await {
Ok(p) => p,
Err(e) => {
eprintln!("{e}");
return;
}
};
let proxy = CoreDeviceProxy::connect(&*provider)
.await
.expect("no core_device_proxy");
let rsd_port = proxy.handshake.server_rsd_port;
let mut provider = proxy
.create_software_tunnel()
.expect("no tunnel")
.to_async_handle();
let mut handshake = RsdHandshake::new(provider.connect(rsd_port).await.unwrap())
.await
.unwrap();
let mut proxy = CompanionProxy::connect_rsd(&mut provider, &mut handshake)
.await
.expect("no companion proxy connect");
// let mut proxy = CompanionProxy::connect(&*provider)
// .await
// .expect("Failed to connect to companion proxy");
if matches.subcommand_matches("list").is_some() {
proxy.get_device_registry().await.expect("Failed to show");
} else if matches.subcommand_matches("listen").is_some() {
let mut stream = proxy.listen_for_devices().await.expect("Failed to show");
while let Ok(v) = stream.next().await {
println!("{}", pretty_print_dictionary(&v));
}
} else if let Some(matches) = matches.subcommand_matches("get") {
let key = matches.get_one::<String>("value").expect("no value passed");
let udid = matches
.get_one::<String>("device_udid")
.expect("no AW udid passed");
match proxy.get_value(udid, key).await {
Ok(value) => {
println!("{}", pretty_print_plist(&value));
}
Err(e) => {
eprintln!("Error getting value: {e}");
}
}
} else if let Some(matches) = matches.subcommand_matches("start") {
let port: u16 = matches
.get_one::<String>("port")
.expect("no port passed")
.parse()
.expect("not a number");
let name = matches.get_one::<String>("name").map(|x| x.as_str());
match proxy.start_forwarding_service_port(port, name, None).await {
Ok(value) => {
println!("started on port {value}");
}
Err(e) => {
eprintln!("Error starting: {e}");
}
}
} else if let Some(matches) = matches.subcommand_matches("stop") {
let port: u16 = matches
.get_one::<String>("port")
.expect("no port passed")
.parse()
.expect("not a number");
if let Err(e) = proxy.stop_forwarding_service_port(port).await {
eprintln!("Error starting: {e}");
}
} else {
eprintln!("Invalid usage, pass -h for help");
}
return;
}