Skip to content

Commit

Permalink
Use the newest idevice for async and heartbeat intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
jkcoxson committed Jan 23, 2025
1 parent c547d30 commit d705b0b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 50 deletions.
19 changes: 16 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "netmuxd"
version = "0.2.0"
version = "0.2.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -22,7 +22,7 @@ futures-util = { version = "0.3.21" }
zeroconf = { git = "https://github.com/zeyugao/zeroconf-rs", optional = true }
mdns = "3.0.0"

idevice = "0.1.3"
idevice = "0.1.7"
plist = "1.7"

log = { version = "0.4.16" }
Expand Down
9 changes: 2 additions & 7 deletions src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl SharedDevices {
paired_udids: Vec::new(),
}
}
pub fn add_network_device(
pub async fn add_network_device(
&mut self,
udid: String,
network_address: IpAddr,
Expand All @@ -88,12 +88,7 @@ impl SharedDevices {
let pairing_file = idevice::pairing_file::PairingFile::from_bytes(&pairing_file)?;

let handle = if self.use_heartbeat {
Some(heartbeat::heartbeat(
network_address,
udid.clone(),
pairing_file,
data,
)?)
Some(heartbeat::heartbeat(network_address, udid.clone(), pairing_file, data).await?)
} else {
None
};
Expand Down
57 changes: 33 additions & 24 deletions src/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tokio::sync::mpsc::UnboundedSender;

use crate::devices::SharedDevices;

pub fn heartbeat(
pub async fn heartbeat(
ip_addr: IpAddr,
udid: String,
pairing_file: idevice::pairing_file::PairingFile,
Expand All @@ -22,43 +22,52 @@ pub fn heartbeat(

let socket = SocketAddr::new(ip_addr, idevice::lockdownd::LOCKDOWND_PORT);

let socket = std::net::TcpStream::connect(socket)?;
let socket = tokio::net::TcpStream::connect(socket).await?;
let socket = Box::new(socket);
let idevice = Idevice::new(socket, "netmuxd");

let mut lockdown_client = LockdowndClient { idevice };
lockdown_client.start_session(&pairing_file)?;
lockdown_client.start_session(&pairing_file).await?;

let (port, _) = lockdown_client
.start_service("com.apple.mobile.heartbeat")
.await
.unwrap();

let socket = SocketAddr::new(ip_addr, port);
let socket = std::net::TcpStream::connect(socket)?;
let socket = tokio::net::TcpStream::connect(socket).await?;
let socket = Box::new(socket);
let mut idevice = Idevice::new(socket, "heartbeat_client");

idevice.start_session(&pairing_file)?;
idevice.start_session(&pairing_file).await?;

let mut heartbeat_client = HeartbeatClient { idevice };

tokio::task::spawn_blocking(move || loop {
if let Err(e) = heartbeat_client.get_marco() {
info!("Heartbeat recv failed: {:?}", e);
tokio::spawn(async move {
remove_from_data(data, udid).await;
});
break;
}
if *pls_stop.lock().unwrap() {
break;
}
if let Err(e) = heartbeat_client.send_polo() {
info!("Heartbeat send failed: {:?}", e);
tokio::spawn(async move {
remove_from_data(data, udid).await;
});
return;
tokio::spawn(async {
let mut interval = 15;
let mut heartbeat_client = HeartbeatClient { idevice };
let pls_stop = pls_stop;
loop {
match heartbeat_client.get_marco(interval).await {
Ok(i) => {
interval = i;
}
Err(e) => {
info!("Heartbeat recv failed: {:?}", e);
tokio::spawn(async move {
remove_from_data(data, udid).await;
});
break;
}
}
if *pls_stop.lock().unwrap() {
break;
}
if let Err(e) = heartbeat_client.send_polo().await {
info!("Heartbeat send failed: {:?}", e);
tokio::spawn(async move {
remove_from_data(data, udid).await;
});
return;
}
}
});
tokio::spawn(async move {
Expand Down
17 changes: 10 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,16 @@ async fn handle_stream(
return;
}
};
let res = match central_data.add_network_device(
udid.to_owned(),
ip_address,
service_name.to_owned(),
connection_type.to_owned(),
data.clone(),
) {
let res = match central_data
.add_network_device(
udid.to_owned(),
ip_address,
service_name.to_owned(),
connection_type.to_owned(),
data.clone(),
)
.await
{
Ok(_) => 1,
Err(e) => {
error!("Failed to add requested device to muxer: {e:?}");
Expand Down
18 changes: 11 additions & 7 deletions src/mdns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@ pub async fn discover(data: Arc<Mutex<SharedDevices>>) {
}
println!("Adding device {}", udid);

if let Err(e) = lock.add_network_device(
udid.clone(),
addr,
service_name.clone(),
"Network".to_string(),
data.clone(),
) {
if let Err(e) = lock
.add_network_device(
udid.clone(),
addr,
service_name.clone(),
"Network".to_string(),
data.clone(),
)
.await
{
warn!("Failed to add {udid} to muxer: {e:?}");
}
}
Expand Down Expand Up @@ -112,6 +115,7 @@ pub async fn discover(data: Arc<Mutex<SharedDevices>>) {
"Network".to_string(),
data.clone(),
)
.await
.unwrap();
}
}
Expand Down

0 comments on commit d705b0b

Please sign in to comment.