idevice 0.1.61

A Rust library to interact with services on iOS devices.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use async_zip::base::read::seek::ZipFileReader;
use futures::AsyncReadExt as _;
use plist_macro::plist;
use std::{io::Cursor, path::Path};
use tokio::io::{AsyncBufRead, AsyncSeek, BufReader};

use crate::{
    IdeviceError, IdeviceService,
    afc::{AfcClient, opcode::AfcFopenMode},
    installation_proxy::InstallationProxyError,
    provider::IdeviceProvider,
};

#[cfg(feature = "rsd")]
use crate::{RsdService, provider::RsdProvider, rsd};

pub const PUBLIC_STAGING: &str = "PublicStaging";

pub const IPCC_REMOTE_FILE: &str = "idevice.ipcc";

pub const IPA_REMOTE_FILE: &str = "idevice.ipa";

/// Result of a prepared upload, containing the remote path to use in Install/Upgrade
pub struct InstallPackage {
    /// Path inside the AFC jail for InstallationProxy `PackagePath`
    pub remote_package_path: String,

    // Each package type has a special option that has to be passed
    pub options: plist::Value,
}

/// Represent the type of package being installed.
pub enum PackageType {
    Ipcc, // Carrier bundle package
    // an IPA package needs the build id to be installed
    Ipa(String), // iOS app package
    Unknown,
}

impl PackageType {
    pub fn get_remote_file(&self) -> Result<&'static str, IdeviceError> {
        match self {
            Self::Ipcc => Ok(IPCC_REMOTE_FILE),
            Self::Ipa(_) => Ok(IPA_REMOTE_FILE),
            Self::Unknown => {
                Err(InstallationProxyError::OperationFailed("invalid package".into()).into())
            }
        }
    }
}

/// Ensure `PublicStaging` exists on device via AFC
pub async fn ensure_public_staging(afc: &mut AfcClient) -> Result<(), IdeviceError> {
    // Try to stat and if it fails, create directory
    match afc.get_file_info(PUBLIC_STAGING).await {
        Ok(_) => Ok(()),
        Err(_) => afc.mk_dir(PUBLIC_STAGING).await,
    }
}

// Get the bundle id of a package by looping through it's files and looking inside of the
// `Info.plist`
pub async fn get_bundle_id<T>(file: &mut T) -> Result<String, IdeviceError>
where
    T: AsyncBufRead + AsyncSeek + Unpin,
{
    let mut zip_file = ZipFileReader::with_tokio(file)
        .await
        .map_err(InstallationProxyError::from)?;

    for i in 0..zip_file.file().entries().len() {
        let mut entry_reader = zip_file
            .reader_with_entry(i)
            .await
            .map_err(InstallationProxyError::from)?;
        let entry = entry_reader.entry();

        let inner_file_path = entry
            .filename()
            .as_str()
            .map_err(|_| IdeviceError::Utf8Error)?
            .trim_end_matches('/');

        let path_segments_count = inner_file_path.split('/').count();

        // there's multiple `Info.plist` files, we only need the one that's in the root of the
        // package
        //
        //                           1             2              3
        // which is in this case: Playload -> APP_NAME.app -> Info.plist
        if inner_file_path.ends_with("Info.plist") && path_segments_count == 3 {
            let mut info_plist_bytes = Vec::new();
            entry_reader.read_to_end(&mut info_plist_bytes).await?;

            let info_plist: plist::Value = plist::from_bytes(&info_plist_bytes)?;

            if let Some(bundle_id) = info_plist
                .as_dictionary()
                .and_then(|dict| dict.get("CFBundleIdentifier"))
                .and_then(|v| v.as_string())
            {
                return Ok(bundle_id.to_string());
            }
        }
    }

    Err(IdeviceError::NotFound)
}

/// Determines the type of package based on its content (IPA or IPCC).
pub async fn determine_package_type<P: AsRef<[u8]>>(
    package: &P,
) -> Result<PackageType, IdeviceError> {
    let mut package_cursor = BufReader::new(Cursor::new(package.as_ref()));

    // Zip entry order isn't guaranteed and directory entries are optional, so we can't
    // trust any fixed index to point at `Payload/<name>.{app,bundle}/`. Scan the central
    // directory for the first entry under `Payload/` whose top segment carries the
    // expected extension.
    let folder_name = {
        let archive = ZipFileReader::with_tokio(&mut package_cursor)
            .await
            .map_err(InstallationProxyError::from)?;

        let mut found: Option<String> = None;
        for entry in archive.file().entries() {
            let path = entry
                .filename()
                .as_str()
                .map_err(|_| IdeviceError::Utf8Error)?;

            let Some(rest) = path.strip_prefix("Payload/") else {
                continue;
            };
            let Some(segment) = rest.split('/').next().filter(|s| !s.is_empty()) else {
                continue;
            };
            if segment.ends_with(".app") || segment.ends_with(".bundle") {
                found = Some(segment.to_string());
                break;
            }
        }
        found
    };

    let Some(folder_name) = folder_name else {
        return Ok(PackageType::Unknown);
    };

    if folder_name.ends_with(".bundle") {
        Ok(PackageType::Ipcc)
    } else if folder_name.ends_with(".app") {
        let bundle_id = get_bundle_id(&mut package_cursor).await?;
        Ok(PackageType::Ipa(bundle_id))
    } else {
        Ok(PackageType::Unknown)
    }
}

/// Upload a single file to a destination path on device using AFC
pub async fn afc_upload_file<F: AsRef<[u8]>>(
    afc: &mut AfcClient,
    file: F,
    remote_path: &str,
) -> Result<(), IdeviceError> {
    let mut fd = afc.open(remote_path, AfcFopenMode::WrOnly).await?;
    fd.write_entire(file.as_ref()).await?;
    fd.close().await
}

/// Recursively upload a directory to device via AFC (mirror contents)
pub async fn afc_upload_dir(
    afc: &mut AfcClient,
    local_dir: &Path,
    remote_dir: &str,
) -> Result<(), IdeviceError> {
    use std::collections::VecDeque;
    afc.mk_dir(remote_dir).await.ok();

    let mut queue: VecDeque<(std::path::PathBuf, String)> = VecDeque::new();
    queue.push_back((local_dir.to_path_buf(), remote_dir.to_string()));

    while let Some((cur_local, cur_remote)) = queue.pop_front() {
        let mut rd = tokio::fs::read_dir(&cur_local).await?;
        while let Some(entry) = rd.next_entry().await? {
            let meta = entry.metadata().await?;
            let name = entry.file_name();
            let name = name.to_string_lossy().into_owned();
            if name == "." || name == ".." {
                continue;
            }
            let child_local = entry.path();
            let child_remote = format!("{cur_remote}/{name}");
            if meta.is_dir() {
                afc.mk_dir(&child_remote).await.ok();
                queue.push_back((child_local, child_remote));
            } else if meta.is_file() {
                afc_upload_file(afc, tokio::fs::read(&child_local).await?, &child_remote).await?;
            }
        }
    }
    Ok(())
}

/// Upload a file to `PublicStaging` and return its InstallationProxy path
async fn upload_file_to_public_staging<P: AsRef<[u8]>>(
    provider: &dyn IdeviceProvider,
    file: P,
) -> Result<InstallPackage, IdeviceError> {
    // Connect to AFC via the generic service connector
    let mut afc = AfcClient::connect(provider).await?;

    ensure_public_staging(&mut afc).await?;

    let file = file.as_ref();

    let package_type = determine_package_type(&file).await?;

    let remote_path = format!("{PUBLIC_STAGING}/{}", package_type.get_remote_file()?);

    afc_upload_file(&mut afc, file, &remote_path).await?;

    let options = match package_type {
        PackageType::Ipcc => plist!({"PackageType": "CarrierBundle"}),
        PackageType::Ipa(build_id) => plist!({"CFBundleIdentifier": build_id}),
        PackageType::Unknown => plist!({}),
    };

    Ok(InstallPackage {
        remote_package_path: remote_path,
        options,
    })
}

/// Upload a file to `PublicStaging` over RSD and return its InstallationProxy path
#[cfg(feature = "rsd")]
async fn upload_file_to_public_staging_rsd<P: AsRef<[u8]>>(
    provider: &mut impl RsdProvider,
    handshake: &mut rsd::RsdHandshake,
    file: P,
) -> Result<InstallPackage, IdeviceError> {
    let mut afc = AfcClient::connect_rsd(provider, handshake).await?;

    ensure_public_staging(&mut afc).await?;

    let file = file.as_ref();

    let package_type = determine_package_type(&file).await?;

    let remote_path = format!("{PUBLIC_STAGING}/{}", package_type.get_remote_file()?);

    afc_upload_file(&mut afc, file, &remote_path).await?;

    let options = match package_type {
        PackageType::Ipcc => plist!({"PackageType": "CarrierBundle"}),
        PackageType::Ipa(build_id) => plist!({"CFBundleIdentifier": build_id}),
        PackageType::Unknown => plist!({}),
    };

    Ok(InstallPackage {
        remote_package_path: remote_path,
        options,
    })
}

/// Recursively Upload a directory of file to `PublicStaging`
async fn upload_dir_to_public_staging<P: AsRef<Path>>(
    provider: &dyn IdeviceProvider,
    file: P,
) -> Result<InstallPackage, IdeviceError> {
    let mut afc = AfcClient::connect(provider).await?;

    ensure_public_staging(&mut afc).await?;

    let file = file.as_ref();
    let remote_folder_name = file
        .iter()
        .next_back()
        .map(|x| x.to_string_lossy().to_string())
        .unwrap_or(IPA_REMOTE_FILE.to_string());

    let remote_path = format!("{PUBLIC_STAGING}/{remote_folder_name}");

    afc_upload_dir(&mut afc, file, &remote_path).await?;

    Ok(InstallPackage {
        remote_package_path: remote_path,
        options: plist!({"PackageType": "Developer"}),
    })
}

/// Recursively upload a directory to `PublicStaging` over RSD.
#[cfg(feature = "rsd")]
async fn upload_dir_to_public_staging_rsd<P: AsRef<Path>>(
    provider: &mut impl RsdProvider,
    handshake: &mut rsd::RsdHandshake,
    file: P,
) -> Result<InstallPackage, IdeviceError> {
    let mut afc = AfcClient::connect_rsd(provider, handshake).await?;

    ensure_public_staging(&mut afc).await?;

    let file = file.as_ref();
    let remote_folder_name = file
        .iter()
        .next_back()
        .map(|x| x.to_string_lossy().to_string())
        .unwrap_or(IPA_REMOTE_FILE.to_string());

    let remote_path = format!("{PUBLIC_STAGING}/{remote_folder_name}");

    afc_upload_dir(&mut afc, file, &remote_path).await?;

    Ok(InstallPackage {
        remote_package_path: remote_path,
        options: plist!({"PackageType": "Developer"}),
    })
}

pub async fn prepare_file_upload(
    provider: &dyn IdeviceProvider,
    data: impl AsRef<[u8]>,
    caller_options: Option<plist::Value>,
) -> Result<InstallPackage, IdeviceError> {
    let InstallPackage {
        remote_package_path,
        options,
    } = upload_file_to_public_staging(provider, data).await?;
    let full_options = plist!({
        :<? caller_options,
        :< options,
    });

    Ok(InstallPackage {
        remote_package_path,
        options: full_options,
    })
}

#[cfg(feature = "rsd")]
pub async fn prepare_file_upload_rsd(
    provider: &mut impl RsdProvider,
    handshake: &mut rsd::RsdHandshake,
    data: impl AsRef<[u8]>,
    caller_options: Option<plist::Value>,
) -> Result<InstallPackage, IdeviceError> {
    let InstallPackage {
        remote_package_path,
        options,
    } = upload_file_to_public_staging_rsd(provider, handshake, data).await?;
    let full_options = plist!({
        :<? caller_options,
        :< options,
    });

    Ok(InstallPackage {
        remote_package_path,
        options: full_options,
    })
}

pub async fn prepare_dir_upload(
    provider: &dyn IdeviceProvider,
    local_path: impl AsRef<Path>,
    caller_options: Option<plist::Value>,
) -> Result<InstallPackage, IdeviceError> {
    let InstallPackage {
        remote_package_path,
        options,
    } = upload_dir_to_public_staging(provider, &local_path).await?;

    let full_options = plist!({
        :<? caller_options,
        :< options,
    });

    Ok(InstallPackage {
        remote_package_path,
        options: full_options,
    })
}

#[cfg(feature = "rsd")]
pub async fn prepare_dir_upload_rsd(
    provider: &mut impl RsdProvider,
    handshake: &mut rsd::RsdHandshake,
    local_path: impl AsRef<Path>,
    caller_options: Option<plist::Value>,
) -> Result<InstallPackage, IdeviceError> {
    let InstallPackage {
        remote_package_path,
        options,
    } = upload_dir_to_public_staging_rsd(provider, handshake, &local_path).await?;

    let full_options = plist!({
        :<? caller_options,
        :< options,
    });

    Ok(InstallPackage {
        remote_package_path,
        options: full_options,
    })
}