@@ -6,6 +6,7 @@ use std::{fs, os::unix::prelude::PermissionsExt};
66
77use crate :: raw_packet:: RawPacket ;
88use devices:: SharedDevices ;
9+ use idevice:: pairing_file:: PairingFile ;
910use log:: { debug, error, info, trace, warn} ;
1011use tokio:: {
1112 io:: { AsyncRead , AsyncReadExt , AsyncWrite , AsyncWriteExt } ,
@@ -110,9 +111,7 @@ async fn main() {
110111 }
111112 info ! ( "Collected arguments, proceeding" ) ;
112113
113- let data = Arc :: new ( Mutex :: new (
114- devices:: SharedDevices :: new ( plist_storage, use_heartbeat) . await ,
115- ) ) ;
114+ let data = Arc :: new ( Mutex :: new ( devices:: SharedDevices :: new ( plist_storage) . await ) ) ;
116115 info ! ( "Created new central data" ) ;
117116 let data_clone = data. clone ( ) ;
118117
@@ -137,7 +136,7 @@ async fn main() {
137136 }
138137 } ;
139138
140- handle_stream ( socket, data. clone ( ) ) . await ;
139+ handle_stream ( socket, data. clone ( ) , use_heartbeat ) . await ;
141140 }
142141 } ) ;
143142 }
@@ -168,15 +167,15 @@ async fn main() {
168167 }
169168 } ;
170169
171- handle_stream ( socket, data. clone ( ) ) . await ;
170+ handle_stream ( socket, data. clone ( ) , use_heartbeat ) . await ;
172171 }
173172 } ) ;
174173 }
175174
176175 if use_mdns {
177176 let local = tokio:: task:: LocalSet :: new ( ) ;
178177 local. spawn_local ( async move {
179- mdns:: discover ( data_clone) . await ;
178+ mdns:: discover ( data_clone, use_heartbeat ) . await ;
180179 error ! ( "mDNS discovery stopped, how the heck did you break this" ) ;
181180 } ) ;
182181 local. await ;
@@ -197,6 +196,7 @@ enum Directions {
197196async fn handle_stream (
198197 mut socket : impl AsyncRead + AsyncWrite + Unpin + Send + ' static ,
199198 data : Arc < Mutex < SharedDevices > > ,
199+ use_heartbeat : bool ,
200200) {
201201 tokio:: spawn ( async move {
202202 let mut current_directions = Directions :: None ;
@@ -297,6 +297,14 @@ async fn handle_stream(
297297 }
298298 } ;
299299
300+ let ip_address = match ip_address. parse ( ) {
301+ Ok ( i) => i,
302+ Err ( _) => {
303+ warn ! ( "Bad IP requested: {ip_address}" ) ;
304+ return ;
305+ }
306+ } ;
307+
300308 let udid = match parsed. plist . get ( "DeviceID" ) {
301309 Some ( plist:: Value :: String ( u) ) => u,
302310 _ => {
@@ -305,21 +313,50 @@ async fn handle_stream(
305313 }
306314 } ;
307315
308- let mut central_data = data. lock ( ) . await ;
309- let ip_address = match ip_address. parse ( ) {
310- Ok ( i) => i,
311- Err ( _) => {
312- warn ! ( "Bad IP requested: {ip_address}" ) ;
313- return ;
314- }
316+ let heartbeat_handle = if use_heartbeat {
317+ let pairing_file =
318+ match data. lock ( ) . await . get_pairing_record ( udid) . await {
319+ Ok ( p) => match PairingFile :: from_bytes ( & p) {
320+ Ok ( p) => p,
321+ Err ( e) => {
322+ log:: error!( "Failed to parse pair record: {e:?}" ) ;
323+ return ;
324+ }
325+ } ,
326+ Err ( e) => {
327+ log:: error!(
328+ "Failed to get pairing file for device: {e:?}"
329+ ) ;
330+ return ;
331+ }
332+ } ;
333+ let heartbeat_handle = match heartbeat:: heartbeat (
334+ ip_address,
335+ udid. clone ( ) ,
336+ pairing_file,
337+ data. clone ( ) ,
338+ )
339+ . await
340+ {
341+ Ok ( h) => h,
342+ Err ( e) => {
343+ warn ! ( "Failed to start heartbeat: {e:?}" ) ;
344+ return ;
345+ }
346+ } ;
347+ Some ( heartbeat_handle)
348+ } else {
349+ None
315350 } ;
351+
352+ let mut central_data = data. lock ( ) . await ;
316353 let res = match central_data
317354 . add_network_device (
318355 udid. to_owned ( ) ,
319356 ip_address,
320357 service_name. to_owned ( ) ,
321358 connection_type. to_owned ( ) ,
322- data . clone ( ) ,
359+ heartbeat_handle ,
323360 )
324361 . await
325362 {
@@ -391,7 +428,7 @@ async fn handle_stream(
391428 let lock = data. lock ( ) . await ;
392429 let pair_file = match lock
393430 . get_pairing_record ( match parsed. plist . get ( "PairRecordID" ) {
394- Some ( plist:: Value :: String ( p) ) => p. to_owned ( ) ,
431+ Some ( plist:: Value :: String ( p) ) => p,
395432 _ => {
396433 warn ! ( "Request did not contain PairRecordID" ) ;
397434 return ;
0 commit comments