From 4e50c5c1d4feb0a8e393c48bf93fe64070c0da8d Mon Sep 17 00:00:00 2001 From: nab138 Date: Thu, 22 Jan 2026 12:34:34 -0500 Subject: [PATCH] Keep working on anisette --- Cargo.lock | 297 +++++++++++++++++++++++++++- README.md | 100 +--------- examples/minimal/Cargo.toml | 4 +- examples/minimal/src/main.rs | 24 ++- isideload/Cargo.toml | 3 + isideload/src/anisette/mod.rs | 7 +- isideload/src/anisette/remote_v3.rs | 92 +++++++++ isideload/src/auth/apple_account.rs | 50 ++++- isideload/src/auth/grandslam.rs | 64 ++++++ isideload/src/auth/mod.rs | 1 + isideload/src/lib.rs | 15 +- 11 files changed, 534 insertions(+), 123 deletions(-) create mode 100644 isideload/src/anisette/remote_v3.rs create mode 100644 isideload/src/auth/grandslam.rs diff --git a/Cargo.lock b/Cargo.lock index a8b294f..b83065f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,74 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aho-corasick" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "async-compression" version = "0.4.36" @@ -27,6 +95,12 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + [[package]] name = "aws-lc-rs" version = "1.15.2" @@ -103,6 +177,19 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chrono" +version = "0.4.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fac4744fb15ae8337dc853fee7fb3f4e48c0fbaa23d0afe49c447b4fab126118" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + [[package]] name = "cmake" version = "0.1.56" @@ -112,6 +199,12 @@ dependencies = [ "cc", ] +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "combine" version = "4.6.7" @@ -209,6 +302,29 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_filter" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bf3c259d255ca70051b30e2e95b5446cdb8949ac4cd22c0d7fd634d89f568e2" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c863f0904021b108aa8b2f55046443e6b1ebde8fd4a15c399893aae4fa069f" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "jiff", + "log", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -446,6 +562,30 @@ dependencies = [ "windows-registry", ] +[[package]] +name = "iana-time-zone" +version = "0.1.64" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33e57f83510bb73707521ebaffa789ec8caf86f9657cad665b092b581d40e9fb" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "icu_collections" version = "2.1.1" @@ -590,15 +730,24 @@ dependencies = [ "serde", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "isideload" version = "0.2.0" dependencies = [ + "chrono", "idevice", + "log", "plist", "plist-macro", "reqwest", "thiserror 2.0.17", + "thiserror-context", ] [[package]] @@ -607,6 +756,30 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" +[[package]] +name = "jiff" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e67e8da4c49d6d9909fe03361f9b620f58898859f5c7aded68351e85e71ecf50" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", +] + +[[package]] +name = "jiff-static" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0c84ee7f197eca9a86c6fd6cb771e55eb991632f15f2bc3ca6ec838929e6e78" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "jni" version = "0.21.1" @@ -689,7 +862,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" name = "minimal" version = "0.1.0" dependencies = [ + "env_logger", "isideload", + "tokio", ] [[package]] @@ -719,12 +894,27 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "openssl-probe" version = "0.2.0" @@ -771,6 +961,21 @@ dependencies = [ "plist", ] +[[package]] +name = "portable-atomic" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f89776e4d69bb58bc6993e99ffa1d11f228b839984854c7daeb5d37f87cbe950" + +[[package]] +name = "portable-atomic-util" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8a2f0d8d040d7848a709caf78912debcc3f33ee4b3cac47d73d1e1069e83507" +dependencies = [ + "portable-atomic", +] + [[package]] name = "potential_utf" version = "0.1.4" @@ -913,6 +1118,35 @@ dependencies = [ "getrandom 0.3.4", ] +[[package]] +name = "regex" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "843bc0191f75f3e22651ae5f1e72939ab2f72a4bc30fa80a066bd66edefc24d4" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" + [[package]] name = "reqwest" version = "0.13.1" @@ -1260,6 +1494,12 @@ dependencies = [ "thiserror-impl 2.0.17", ] +[[package]] +name = "thiserror-context" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d4cf778b0694c0adf030668380054b3a382f1e5e6a2e9a0b54bc5e677045bca" + [[package]] name = "thiserror-impl" version = "1.0.69" @@ -1340,18 +1580,30 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.48.0" +version = "1.49.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" +checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86" dependencies = [ "bytes", "libc", "mio", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.61.2", ] +[[package]] +name = "tokio-macros" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af407857209536a95c8e56f8231ef2c2e2aff839b22e07a1ffcbc617e9db9fa5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-rustls" version = "0.26.4" @@ -1492,6 +1744,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "walkdir" version = "2.5.0" @@ -1622,6 +1880,41 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "windows-link" version = "0.2.1" diff --git a/README.md b/README.md index 5055363..e5dd7cf 100644 --- a/README.md +++ b/README.md @@ -4,106 +4,8 @@ A Rust library for sideloading iOS applications using an Apple ID. Used in [CrossCode](https://github.com/nab138/CrossCode) and [iloader](https://github.com/nab138/iloader). -This also serves as a rust library for accessing Apple's private developer APIs. See [`developer_session.rs`](isideload/src/developer_session.rs) for details. - -## Disclaimer - -This package uses private Apple Developer APIs. Use at your own risk. - -## Usage - -To use isideload, add the following to your `Cargo.toml`: - -```toml -[dependencies] -# Make sure to use the latest version -isideload = { version = "0.1.21", features = ["vendored-openssl"] }# Optionally, the vendored feature can be enabled to avoid needing OpenSSL installed on your system. -idevice = { version = "0.1.46", features = ["usbmuxd", "ring"], default-features = false} # Reccomended to disable default features and enable ring to reduce the number of ssl stacks used -``` - -Then, you can use it like so: - -```rs -use std::{env, path::PathBuf, sync::Arc}; - -use idevice::usbmuxd::{UsbmuxdAddr, UsbmuxdConnection}; -use isideload::{ - AnisetteConfiguration, AppleAccount, SideloadConfiguration, - developer_session::DeveloperSession, sideload::sideload_app, -}; - -#[tokio::main] -async fn main() { - let args: Vec = env::args().collect(); - let app_path = PathBuf::from( - args.get(1) - .expect("Please provide the path to the app to install"), - ); - let apple_id = args - .get(2) - .expect("Please provide the Apple ID to use for installation"); - let apple_password = args.get(3).expect("Please provide the Apple ID password"); - - // You don't have to use usbmuxd, you can use any IdeviceProvider - let usbmuxd = UsbmuxdConnection::default().await; - if usbmuxd.is_err() { - panic!("Failed to connect to usbmuxd: {:?}", usbmuxd.err()); - } - let mut usbmuxd = usbmuxd.unwrap(); - - let devs = usbmuxd.get_devices().await.unwrap(); - if devs.is_empty() { - panic!("No devices found"); - } - - let provider = devs - .iter() - .next() - .unwrap() - .to_provider(UsbmuxdAddr::from_env_var().unwrap(), "isideload-demo"); - - // Change the anisette url and such here - // Note that right now only remote anisette servers are supported - let anisette_config = AnisetteConfiguration::default(); - - let get_2fa_code = || { - let mut code = String::new(); - println!("Enter 2FA code:"); - std::io::stdin().read_line(&mut code).unwrap(); - Ok(code.trim().to_string()) - }; - - let account = AppleAccount::login( - || Ok((apple_id.to_string(), apple_password.to_string())), - get_2fa_code, - anisette_config, - ) - .await - .unwrap(); - - let dev_session = DeveloperSession::new(Arc::new(account)); - - // You can change the machine name, store directory (for certs, anisette data, & provision files), and logger - let config = SideloadConfiguration::default().set_machine_name("isideload-demo".to_string()); - - sideload_app(&provider, &dev_session, app_path, config) - .await - .unwrap() -} -``` - -See [examples/minimal/src/main.rs](examples/minimal/src/main.rs). +This branch is home to isideload-next, the next major version of isideload. It features a redesigned API, improved error handling, better entitlement handling, and more. It is not ready! ## Licensing This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. - -## Credits - -- The amazing [idevice](https://github.com/jkcoxson/idevice) crate is used to communicate with the device - -- Packages from [`apple-private-apis`](https://github.com/SideStore/apple-private-apis), which is licensed under MPL-2.0, were used for authentication, but the original project was left unfinished. To support isideload, `apple-private-apis` was [forked](https://github.com/nab138/apple-private-apis) and modified to add missing features. With permission from the original developers, the fork was published to crates.io until the official project is published. - -- [apple-codesign](https://crates.io/crates/apple-codesign) was used for code signing, which is licensed under MPL-2.0. - -- [Sideloader](https://github.com/Dadoum/Sideloader) was used as a reference for how the private API endpoints work diff --git a/examples/minimal/Cargo.toml b/examples/minimal/Cargo.toml index 45d4403..4cf593c 100644 --- a/examples/minimal/Cargo.toml +++ b/examples/minimal/Cargo.toml @@ -4,4 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] -isideload = { path = "../../isideload" } \ No newline at end of file +isideload = { path = "../../isideload" } +env_logger = "0.11.8" +tokio = { version = "1.49.0", features = ["rt-multi-thread", "macros"] } diff --git a/examples/minimal/src/main.rs b/examples/minimal/src/main.rs index b9dadba..ae679b2 100644 --- a/examples/minimal/src/main.rs +++ b/examples/minimal/src/main.rs @@ -1,10 +1,15 @@ use std::{env, path::PathBuf}; -use isideload::auth::apple_account::AppleAccountBuilder; +use isideload::{ + anisette::remote_v3::RemoteV3AnisetteProvider, auth::apple_account::AppleAccountBuilder, +}; + +#[tokio::main] +async fn main() { + env_logger::init(); -fn main() { let args: Vec = env::args().collect(); - let app_path = PathBuf::from( + let _app_path = PathBuf::from( args.get(1) .expect("Please provide the path to the app to install"), ); @@ -13,8 +18,17 @@ fn main() { .expect("Please provide the Apple ID to use for installation"); let apple_password = args.get(3).expect("Please provide the Apple ID password"); - let account = AppleAccountBuilder::new(apple_id) + let get_2fa_code = || { + let mut code = String::new(); + println!("Enter 2FA code:"); + std::io::stdin().read_line(&mut code).unwrap(); + Ok(code.trim().to_string()) + }; + + let _account = AppleAccountBuilder::new(apple_id) .danger_debug(true) - .build() + .anisette(RemoteV3AnisetteProvider::default().set_serial_number("2".to_string())) + .login(apple_password, get_2fa_code) + .await .unwrap(); } diff --git a/isideload/Cargo.toml b/isideload/Cargo.toml index 2ed76ae..5dd6ad1 100644 --- a/isideload/Cargo.toml +++ b/isideload/Cargo.toml @@ -18,5 +18,8 @@ install = ["dep:idevice"] idevice = { version = "0.1.51", optional = true } plist = "1.8.0" plist-macro = "0.1.0" +log = "0.4" reqwest = { version = "0.13.1", features = ["json", "gzip"] } thiserror = "2.0.17" +thiserror-context = "0.1.2" +chrono = "0.4.43" diff --git a/isideload/src/anisette/mod.rs b/isideload/src/anisette/mod.rs index d3d07a6..9f21bb5 100644 --- a/isideload/src/anisette/mod.rs +++ b/isideload/src/anisette/mod.rs @@ -1,6 +1,3 @@ +pub mod remote_v3; + pub trait AnisetteProvider {} - -// tmp -pub struct DefaultAnisetteProvider {} - -impl AnisetteProvider for DefaultAnisetteProvider {} diff --git a/isideload/src/anisette/remote_v3.rs b/isideload/src/anisette/remote_v3.rs new file mode 100644 index 0000000..17d7fb5 --- /dev/null +++ b/isideload/src/anisette/remote_v3.rs @@ -0,0 +1,92 @@ +use std::path::PathBuf; + +use chrono::{DateTime, SubsecRound, Utc}; +use reqwest::header::{HeaderMap, HeaderValue}; + +use crate::SideloadResult as Result; +use crate::anisette::AnisetteProvider; + +pub const DEFAULT_ANISETTE_V3_URL: &str = "https://ani.sidestore.io"; + +pub struct RemoteV3AnisetteProvider { + url: String, + config_path: PathBuf, + serial_number: String, +} + +impl RemoteV3AnisetteProvider { + /// Create a new RemoteV3AnisetteProvider with the given URL and config path + /// + /// # Arguments + /// - `url`: The URL of the remote anisette service + /// - `config_path`: The path to the config file + /// - `serial_number`: The serial number of the device + pub fn new(url: &str, config_path: PathBuf, serial_number: String) -> Self { + Self { + url: url.to_string(), + config_path, + serial_number, + } + } + + pub fn set_url(mut self, url: &str) -> RemoteV3AnisetteProvider { + self.url = url.to_string(); + self + } + + pub fn set_config_path(mut self, config_path: PathBuf) -> RemoteV3AnisetteProvider { + self.config_path = config_path; + self + } + + pub fn set_serial_number(mut self, serial_number: String) -> RemoteV3AnisetteProvider { + self.serial_number = serial_number; + self + } +} + +impl Default for RemoteV3AnisetteProvider { + fn default() -> Self { + Self::new(DEFAULT_ANISETTE_V3_URL, PathBuf::new(), "0".to_string()) + } +} + +#[derive(Debug)] +pub struct AnisetteData { + machine_id: String, + one_time_password: String, + routing_info: String, + device_description: String, + device_unique_identifier: String, + local_user_id: String, +} + +impl AnisetteData { + pub fn get_headers(&self, serial: String) -> Result { + let dt: DateTime = Utc::now().round_subsecs(0); + + let mut headers = HeaderMap::new(); + + for (key, value) in vec![ + ( + "X-Apple-I-Client-Time", + dt.format("%+").to_string().replace("+00:00", "Z"), + ), + ("X-Apple-I-SRL-NO", serial), + ("X-Apple-I-TimeZone", "UTC".to_string()), + ("X-Apple-Locale", "en_US".to_string()), + ("X-Apple-I-MD-RINFO", self.routing_info.clone()), + ("X-Apple-I-MD-LU", self.local_user_id.clone()), + ("X-Mme-Device-Id", self.device_unique_identifier.clone()), + ("X-Apple-I-MD", self.one_time_password.clone()), + ("X-Apple-I-MD-M", self.machine_id.clone()), + ("X-Mme-Client-Info", self.device_description.clone()), + ] { + headers.insert(key, HeaderValue::from_str(&value)?); + } + + Ok(headers) + } +} + +impl AnisetteProvider for RemoteV3AnisetteProvider {} diff --git a/isideload/src/auth/apple_account.rs b/isideload/src/auth/apple_account.rs index af88953..208ca3b 100644 --- a/isideload/src/auth/apple_account.rs +++ b/isideload/src/auth/apple_account.rs @@ -1,5 +1,11 @@ -use crate::Result; +use crate::{ + SideloadResult as Result, + anisette::{AnisetteProvider, remote_v3::RemoteV3AnisetteProvider}, + auth::grandslam::GrandSlam, +}; +use log::{info, warn}; use reqwest::{Certificate, ClientBuilder}; +use thiserror_context::Context; const APPLE_ROOT: &[u8] = include_bytes!("./apple_root.der"); @@ -7,13 +13,13 @@ pub struct AppleAccount { pub email: String, pub spd: Option, pub client: reqwest::Client, - pub anisette: Box, + pub anisette: Box, } -#[derive(Debug)] pub struct AppleAccountBuilder { email: String, debug: Option, + anisette: Option>, } impl AppleAccountBuilder { @@ -25,6 +31,7 @@ impl AppleAccountBuilder { Self { email: email.to_string(), debug: None, + anisette: None, } } @@ -37,14 +44,28 @@ impl AppleAccountBuilder { self } - /// Build the AppleAccount + pub fn anisette(mut self, anisette: impl AnisetteProvider + 'static) -> Self { + self.anisette = Some(Box::new(anisette)); + self + } + + /// Build the AppleAccount and log in /// + /// # Arguments + /// - `password`: The Apple ID password + /// - `two_factor_callback`: A callback function that returns the two-factor authentication code /// # Errors /// Returns an error if the reqwest client cannot be built - pub fn login(self) -> Result { + pub async fn login(self, _password: &str, _two_factor_callback: F) -> Result + where + F: Fn() -> Result, + { let debug = self.debug.unwrap_or(false); + let anisette = self + .anisette + .unwrap_or_else(|| Box::new(RemoteV3AnisetteProvider::default())); - AppleAccount::login(&self.email, debug) + AppleAccount::login(&self.email, debug, anisette).await } } @@ -60,14 +81,27 @@ impl AppleAccount { /// Log in to an Apple account with the given email /// /// Reccomended to use the AppleAccountBuilder instead - pub fn login(email: &str, debug: bool) -> Result { + pub async fn login( + email: &str, + debug: bool, + anisette: Box, + ) -> Result { + info!("Logging in to apple ID: {}", email); + if debug { + warn!("Debug mode enabled: this is a security risk!"); + } let client = Self::build_client(debug)?; + let mut gs = GrandSlam::new(&client); + gs.get_url_bag() + .await + .context("Failed to get URL bag from GrandSlam")?; + Ok(AppleAccount { email: email.to_string(), spd: None, client, - anisette: Box::new(crate::anisette::DefaultAnisetteProvider {}), + anisette, }) } diff --git a/isideload/src/auth/grandslam.rs b/isideload/src/auth/grandslam.rs new file mode 100644 index 0000000..593b5f2 --- /dev/null +++ b/isideload/src/auth/grandslam.rs @@ -0,0 +1,64 @@ +use crate::SideloadResult as Result; +use log::debug; +use plist::Dictionary; +use plist_macro::pretty_print_dictionary; +use reqwest::header::HeaderValue; + +const URL_BAG: &str = "https://gsa.apple.com/grandslam/GsService2/lookup"; + +pub struct GrandSlam<'a> { + client: &'a reqwest::Client, + url_bag: Option, +} + +impl<'a> GrandSlam<'a> { + /// Create a new GrandSlam instance + /// + /// # Arguments + /// - `client`: The reqwest client to use for requests + pub fn new(client: &'a reqwest::Client) -> Self { + Self { + client, + url_bag: None, + } + } + + /// Get the URL bag from GrandSlam + pub async fn get_url_bag(&mut self) -> Result<&Dictionary> { + if self.url_bag.is_none() { + debug!("Fetching URL bag from GrandSlam"); + let resp = self + .client + .get(URL_BAG) + .headers(Self::base_headers()) + .send() + .await? + .text() + .await?; + let dict: Dictionary = plist::from_bytes(resp.as_bytes())?; + debug!("{}", pretty_print_dictionary(&dict)); + self.url_bag = Some(dict); + } + Ok(self.url_bag.as_ref().unwrap()) + } + + fn base_headers() -> reqwest::header::HeaderMap { + let mut headers = reqwest::header::HeaderMap::new(); + headers.insert("Context-Type", HeaderValue::from_static("text/x-xml-plist")); + headers.insert("Accept", HeaderValue::from_static("text/x-xml-plist")); + headers.insert( + "X-Mme-Client-Info", + HeaderValue::from_static( + " ", + ), + ); + headers.insert("User-Agent", HeaderValue::from_static("Xcode")); + headers.insert("X-Xcode-Version", HeaderValue::from_static("14.2 (14C18)")); + headers.insert( + "X-Apple-App-Info", + HeaderValue::from_static("com.apple.gs.xcode.auth"), + ); + + headers + } +} diff --git a/isideload/src/auth/mod.rs b/isideload/src/auth/mod.rs index 7af46c6..9aa7ce1 100644 --- a/isideload/src/auth/mod.rs +++ b/isideload/src/auth/mod.rs @@ -1 +1,2 @@ pub mod apple_account; +pub mod grandslam; diff --git a/isideload/src/lib.rs b/isideload/src/lib.rs index c5b0051..00ca26b 100644 --- a/isideload/src/lib.rs +++ b/isideload/src/lib.rs @@ -1,12 +1,21 @@ use thiserror::Error as ThisError; +use thiserror_context::{Context, impl_context}; pub mod anisette; pub mod auth; #[derive(Debug, ThisError)] -pub enum Error { - #[error("Reqwest error: {0}")] +pub enum ErrorInner { + #[error("Failed sending request: {0}")] Reqwest(#[from] reqwest::Error), + + #[error("Failed parsing plist: {0}")] + Plist(#[from] plist::Error), + + #[error("Invalid Header: {0}")] + InvalidHeader(#[from] reqwest::header::InvalidHeaderValue), } -pub type Result = std::result::Result; +impl_context!(Error(ErrorInner)); + +pub type SideloadResult = std::result::Result;