Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.
Rust apple-notesapple-reminderscloudkitcrdtdesktop-appicloudicloud-calendaricloud-driveicloud-notesicloud-photosiphonelinuxlinux-desktoplocal-firstoffline-firstprivacyrusttauritauri-appwindows
Probes: how iCloud Drive's changes can be heard
Yashwanth Kamireddi9e479eec0d90
diff --git a/probe/examples/drive_courier_live.rs b/probe/examples/drive_courier_live.rsnew file mode 100644index 0000000..6cdf3af--- /dev/null+++ b/probe/examples/drive_courier_live.rs@@ -0,0 +1,82 @@+//! Live: does today's push socket (quince_icloud::courier) say when iCloud Drive changes? The+//! older channel this was first asked on is retired. One socket listens to the candidate Drive+//! containers; a throwaway folder ("Quince mount test …") is made in Drive's top level and+//! deleted, and every push is printed with its delay. The throwaway is cleared from Recently+//! Deleted at the end. Prints container names and timings only.+use anyhow::{anyhow, Result};+use std::time::{Duration, Instant};++#[tokio::main]+async fn main() -> Result<()> {+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");+ let mut client = quince_auth::Client::new(&dir)?;+ if !client.resume().await? {+ return Err(anyhow!("no session"));+ }+ let s = client.session();+ let dsid = s.dsid.clone().ok_or_else(|| anyhow!("no dsid"))?;+ let drivews = s+ .service_url("drivews")+ .ok_or_else(|| anyhow!("no drivews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ let containers = [+ "com.apple.clouddocs",+ "com.apple.CloudDocs",+ "com.apple.icloud.drive",+ "com.apple.notes",+ ];+ let mut id = quince_icloud::courier::Identity::new();+ let mut courier = quince_icloud::courier::Courier::connect(+ quince_icloud::courier::SERVER,+ &mut id,+ &containers,+ )+ .await+ .map_err(|e| anyhow!("connect: {e}"))?;+ courier+ .register(&http, quince_icloud::courier::REGISTRY, &id, &dsid)+ .await+ .map_err(|e| anyhow!("register: {e}"))?;+ println!("listening");+ let listen = |secs| Duration::from_secs(secs);+ async fn drain(c: &mut quince_icloud::courier::Courier, t: Instant, d: Duration) {+ let end = Instant::now() + d;+ while Instant::now() < end {+ match c.next(end - Instant::now()).await {+ Ok(Some(e)) => println!("{:>6.1}s push: {e:?}", t.elapsed().as_secs_f32()),+ Ok(None) => {}+ Err(e) => {+ println!("lost: {e}");+ return;+ }+ }+ }+ }+ let name = format!("Quince mount test {}", std::process::id());+ let t = Instant::now();+ let made = quince_icloud::drive::create_folder(+ &http,+ &drivews,+ p.clone(),+ "FOLDER::com.apple.CloudDocs::root",+ &name,+ )+ .await?;+ println!("folder made");+ drain(&mut courier, t, listen(30)).await;+ let t = Instant::now();+ quince_icloud::drive::delete(&http, &drivews, p.clone(), &made).await?;+ println!("folder deleted");+ drain(&mut courier, t, listen(30)).await;+ let trash = quince_icloud::drive::trash(&http, &drivews, p.clone()).await?;+ let mine: Vec<_> = trash+ .into_iter()+ .filter(|i| i.name.starts_with("Quince mount test"))+ .collect();+ println!("throwaways in Recently Deleted: {}", mine.len());+ if !mine.is_empty() {+ quince_icloud::drive::delete_forever_many(&http, &drivews, p, &mine).await?;+ }+ courier.close().await;+ Ok(())+}diff --git a/probe/examples/drive_token_live.rs b/probe/examples/drive_token_live.rsnew file mode 100644index 0000000..053c2ae--- /dev/null+++ b/probe/examples/drive_token_live.rs@@ -0,0 +1,95 @@+//! Live: does CloudKit's cheap "has anything changed" check (`changes/database`, read-only, a+//! few hundred bytes) move when iCloud Drive changes? Apple's push socket says nothing for+//! Drive (drive_courier_live). A token is seeded for each candidate container; a throwaway+//! folder ("Quince mount test …") is made in Drive's top level and deleted, and each token is+//! asked again after each step. The throwaway is cleared from Recently Deleted at the end.+use anyhow::{anyhow, Result};+use std::time::{Duration, Instant};++#[tokio::main]+async fn main() -> Result<()> {+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");+ let mut client = quince_auth::Client::new(&dir)?;+ if !client.resume().await? {+ return Err(anyhow!("no session"));+ }+ let s = client.session();+ let drivews = s+ .service_url("drivews")+ .ok_or_else(|| anyhow!("no drivews"))?;+ let ck = s+ .service_url("ckdatabasews")+ .ok_or_else(|| anyhow!("no ckdatabasews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ let containers = ["com.apple.clouddocs", "com.apple.CloudDocs"];+ let mut tokens: Vec<Option<String>> = Vec::new();+ for c in containers {+ match quince_icloud::push::database_changed(&http, &ck, &p, c, None).await {+ Ok((_, t)) => {+ println!("{c}: seeded {}", t.is_some());+ tokens.push(t);+ }+ Err(e) => {+ println!("{c}: {e}");+ tokens.push(None);+ }+ }+ }+ async fn ask(+ http: &reqwest::Client,+ ck: &str,+ p: &[(String, String)],+ containers: &[&str],+ tokens: &mut [Option<String>],+ step: &str,+ ) {+ let t = Instant::now();+ let mut seen = vec![false; containers.len()];+ while t.elapsed() < Duration::from_secs(25) && seen.iter().any(|s| !s) {+ for (i, c) in containers.iter().enumerate() {+ if seen[i] || tokens[i].is_none() {+ seen[i] = true;+ continue;+ }+ if let Ok((moved, next)) =+ quince_icloud::push::database_changed(http, ck, p, c, tokens[i].as_deref())+ .await+ {+ tokens[i] = next;+ if moved {+ seen[i] = true;+ println!("{c}: moved {:.1}s after {step}", t.elapsed().as_secs_f32());+ }+ }+ }+ tokio::time::sleep(Duration::from_millis(1500)).await;+ }+ for (i, c) in containers.iter().enumerate() {+ if !seen[i] {+ println!("{c}: no move in 25s after {step}");+ }+ }+ }+ let name = format!("Quince mount test {}", std::process::id());+ let made = quince_icloud::drive::create_folder(+ &http,+ &drivews,+ p.clone(),+ "FOLDER::com.apple.CloudDocs::root",+ &name,+ )+ .await?;+ ask(&http, &ck, &p, &containers, &mut tokens, "folder made").await;+ quince_icloud::drive::delete(&http, &drivews, p.clone(), &made).await?;+ ask(&http, &ck, &p, &containers, &mut tokens, "folder deleted").await;+ let trash = quince_icloud::drive::trash(&http, &drivews, p.clone()).await?;+ let mine: Vec<_> = trash+ .into_iter()+ .filter(|i| i.name.starts_with("Quince mount test"))+ .collect();+ println!("throwaways in Recently Deleted: {}", mine.len());+ if !mine.is_empty() {+ quince_icloud::drive::delete_forever_many(&http, &drivews, p, &mine).await?;+ }+ Ok(())+}diff --git a/probe/examples/drive_zone_delta.rs b/probe/examples/drive_zone_delta.rsnew file mode 100644index 0000000..aaaec58--- /dev/null+++ b/probe/examples/drive_zone_delta.rs@@ -0,0 +1,104 @@+//! Live: can iCloud Drive's changes be read item by item? Walks the main Drive zone's change+//! feed to its end (ids only), makes a throwaway folder ("Quince mount test …") in the top+//! level, reads the feed again from the end token, and prints the record names and parent+//! links that came — to be matched with the folder's own Drive id. Then deletes it, reads+//! again, and clears it from Recently Deleted. Never prints names or contents.+use anyhow::{anyhow, Result};+use serde_json::{json, Value};+use std::time::{Duration, Instant};++#[tokio::main]+async fn main() -> Result<()> {+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");+ let mut client = quince_auth::Client::new(&dir)?;+ if !client.resume().await? {+ return Err(anyhow!("no session"));+ }+ let s = client.session();+ let ck = s+ .service_url("ckdatabasews")+ .ok_or_else(|| anyhow!("no ck"))?;+ let drivews = s+ .service_url("drivews")+ .ok_or_else(|| anyhow!("no drivews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ let url = format!(+ "{}/database/1/com.apple.clouddocs/production/private/changes/zone",+ ck.trim_end_matches('/')+ );+ let zone_id = json!({"zoneName": "com.apple.CloudDocs", "zoneType": "REGULAR_CUSTOM_ZONE"});+ // One page from a token: (records, next token, more coming).+ let page = |token: Option<String>| {+ let (http, url, p, zone_id) = (http.clone(), url.clone(), p.clone(), zone_id.clone());+ async move {+ let mut z = json!({"zoneID": zone_id, "resultsLimit": 200});+ if let Some(t) = token {+ z["syncToken"] = json!(t);+ }+ let body: Value = http+ .post(&url)+ .query(&p)+ .json(&json!({"zones": [z]}))+ .send()+ .await?+ .json()+ .await?;+ let z = body["zones"][0].clone();+ anyhow::Ok((+ z["records"].as_array().cloned().unwrap_or_default(),+ z["syncToken"].as_str().map(str::to_string),+ z["moreComing"] == true,+ ))+ }+ };+ let t = Instant::now();+ let (mut token, mut total, mut pages) = (None, 0, 0);+ loop {+ let (recs, next, more) = page(token.clone()).await?;+ total += recs.len();+ pages += 1;+ token = next;+ if !more || pages > 2000 {+ break;+ }+ }+ println!("walked {total} records in {pages} pages, {:?}", t.elapsed());+ let show = |recs: &[Value]| {+ for r in recs {+ println!(+ " type={} name={} deleted={} parent={}",+ r["recordType"], r["recordName"], r["deleted"], r["fields"]["parent"]["value"]+ );+ }+ };+ let name = format!("Quince mount test {}", std::process::id());+ let made = quince_icloud::drive::create_folder(+ &http,+ &drivews,+ p.clone(),+ "FOLDER::com.apple.CloudDocs::root",+ &name,+ )+ .await?;+ println!("made: {made:?}");+ tokio::time::sleep(Duration::from_secs(2)).await;+ let (recs, next, _) = page(token.clone()).await?;+ println!("after make: {} records", recs.len());+ show(&recs);+ token = next.or(token);+ quince_icloud::drive::delete(&http, &drivews, p.clone(), &made).await?;+ tokio::time::sleep(Duration::from_secs(2)).await;+ let (recs, _, _) = page(token).await?;+ println!("after delete: {} records", recs.len());+ show(&recs);+ let trash = quince_icloud::drive::trash(&http, &drivews, p.clone()).await?;+ let mine: Vec<_> = trash+ .into_iter()+ .filter(|i| i.name.starts_with("Quince mount test"))+ .collect();+ println!("throwaways in Recently Deleted: {}", mine.len());+ if !mine.is_empty() {+ quince_icloud::drive::delete_forever_many(&http, &drivews, p, &mine).await?;+ }+ Ok(())+}diff --git a/probe/examples/drive_zone_shape.rs b/probe/examples/drive_zone_shape.rsnew file mode 100644index 0000000..204f9a9--- /dev/null+++ b/probe/examples/drive_zone_shape.rs@@ -0,0 +1,111 @@+//! Read-only: what iCloud Drive's CloudKit container holds — its zones, and for a few records+//! of each, the record type and field NAMES only (never values). Tells whether Drive's changes+//! can be read item by item from `changes/zone`.+use anyhow::{anyhow, Result};+use serde_json::{json, Value};++#[tokio::main]+async fn main() -> Result<()> {+ let dir = std::path::PathBuf::from(std::env::var("HOME")?).join(".local/share/quince-probe");+ let mut client = quince_auth::Client::new(&dir)?;+ if !client.resume().await? {+ return Err(anyhow!("no session"));+ }+ let s = client.session();+ let ck = s+ .service_url("ckdatabasews")+ .ok_or_else(|| anyhow!("no ckdatabasews"))?;+ let (http, p) = (client.http().clone(), client.service_params());+ let base = format!(+ "{}/database/1/com.apple.clouddocs/production/private",+ ck.trim_end_matches('/')+ );+ let post = |path: &str, body: Value| {+ http.post(format!("{base}/{path}"))+ .query(&p)+ .json(&body)+ .send()+ };+ let r = post("changes/database", json!({})).await?;+ println!("changes/database: {}", r.status());+ let db: Value = r.json().await?;+ let zones: Vec<Value> = db["zones"].as_array().cloned().unwrap_or_default();+ println!("zones: {}", zones.len());+ // Follow the main Drive zone until records show, and print their ids and parent links.+ let main = zones+ .iter()+ .find(|z| z["zoneID"]["zoneName"] == "com.apple.CloudDocs")+ .cloned()+ .unwrap_or(Value::Null);+ let mut token: Option<String> = None;+ let mut shown = 0;+ for page in 0..40 {+ let mut zone = json!({"zoneID": main["zoneID"], "resultsLimit": 50});+ if let Some(t) = &token {+ zone["syncToken"] = json!(t);+ }+ let body: Value = post("changes/zone", json!({"zones": [zone]}))+ .await?+ .json()+ .await?;+ let z = &body["zones"][0];+ token = z["syncToken"].as_str().map(str::to_string);+ for rec in z["records"].as_array().into_iter().flatten() {+ if shown < 6 {+ println!(+ "page {page} type={} name={} parent={} deleted={}",+ rec["recordType"], rec["recordName"], rec["fields"]["parent"], rec["deleted"]+ );+ shown += 1;+ }+ }+ if z["moreComing"] != true || shown >= 6 {+ break;+ }+ }+ let drivews = s+ .service_url("drivews")+ .ok_or_else(|| anyhow!("no drivews"))?;+ let root = quince_icloud::drive::fetch_folder(+ &http,+ &drivews,+ p.clone(),+ "FOLDER::com.apple.CloudDocs::root",+ )+ .await?;+ for i in root.iter().take(4) {+ println!("drivews id: {}", i.drivewsid);+ }+ return Ok(());+ #[allow(unreachable_code)]+ for z in zones.iter().take(6) {+ let zid = &z["zoneID"];+ println!("zone {}", zid["zoneName"]);+ let r = post(+ "changes/zone",+ json!({"zones": [{"zoneID": zid, "resultsLimit": 4}]}),+ )+ .await?;+ let status = r.status();+ let body: Value = r.json().await.unwrap_or(Value::Null);+ let zone = &body["zones"][0];+ println!(+ " changes/zone {status}: moreComing={} records={} err={}",+ zone["moreComing"],+ zone["records"].as_array().map_or(0, Vec::len),+ zone["serverErrorCode"]+ );+ for rec in zone["records"].as_array().into_iter().flatten() {+ let mut fields: Vec<_> = rec["fields"]+ .as_object()+ .map(|o| o.keys().cloned().collect())+ .unwrap_or_default();+ fields.sort();+ println!(+ " type={} deleted={} fields={fields:?}",+ rec["recordType"], rec["deleted"]+ );+ }+ }+ Ok(())+}