Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Accounts Apple keeps on its regional servers sign in there (#10)

Yashwanth Kamireddif48a5ab86d9a
diff --git a/crates/quince-auth/src/lib.rs b/crates/quince-auth/src/lib.rs
index 1524f08..bcfb2d5 100644
--- a/crates/quince-auth/src/lib.rs
+++ b/crates/quince-auth/src/lib.rs
@@ -42,7 +42,7 @@ use std::sync::Arc;
/// Base URLs for Apple's auth and setup services.
///
/// Injectable so the sign-in flow can be exercised against a mock server.
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Endpoints {
/// Origin for the SRP sign-in calls.
pub auth_root: String,
@@ -65,7 +65,41 @@ impl Default for Endpoints {
}
}
+/// The domain Apple names for the accounts it keeps on its own servers apart from iCloud.com
+/// (`domainToUse` in its answer to `accountLogin`), as it writes it.
+pub const REGIONAL_DOMAIN: &str = "icloud.com.cn";
+
+/// iCloud.com's own domain, as Apple names it in `domainToUse`.
+const HOME_DOMAIN: &str = "icloud.com";
+
+/// Where Apple's answer says this account is kept, when it says (`domainToUse`).
+fn domain_to_use(text: &str) -> Option<String> {
+ serde_json::from_str::<Value>(text)
+ .ok()?
+ .get("domainToUse")?
+ .as_str()
+ .map(|d| d.trim().to_ascii_lowercase())
+ .filter(|d| !d.is_empty())
+}
+
impl Endpoints {
+ /// Apple's servers for an account Apple keeps at `domain` (its `domainToUse`, kept in the
+ /// session): iCloud.com's for every account, unless Apple named its regional domain. The
+ /// same services and protocols, at Apple's own hosts there (icloudpd's `--domain cn`).
+ #[must_use]
+ pub fn for_domain(domain: Option<&str>) -> Self {
+ if domain.is_some_and(|d| d.eq_ignore_ascii_case(REGIONAL_DOMAIN)) {
+ Self {
+ auth_root: "https://idmsa.apple.com.cn".into(),
+ auth: "https://idmsa.apple.com.cn/appleauth/auth".into(),
+ setup: "https://setup.icloud.com.cn/setup/ws/1".into(),
+ home: "https://www.icloud.com.cn".into(),
+ }
+ } else {
+ Self::default()
+ }
+ }
+
/// Point every service at one base URL. Test seam.
pub fn at(base: &str) -> Self {
let base = base.trim_end_matches('/');
@@ -124,6 +158,13 @@ pub enum Error {
CoolingDown { seconds: u64 },
#[error("this needs a fresh sign-in — Apple won't accept a resumed session for it")]
ReauthRequired,
+ /// Apple keeps this account on its regional servers and this session has moved there, but
+ /// Apple wants a sign-in made there: whoever holds the password signs in again (issue #10).
+ #[error("Apple keeps this Apple Account on your region's iCloud servers. Sign in again to continue there.")]
+ Moved,
+ /// [`Error::Moved`] met after a code: signed in again there, and Apple sent a new code.
+ #[error("Apple keeps this Apple Account on your region's iCloud servers, and has sent a new code for them. Enter the new code.")]
+ CodeAgain,
// Apple's 429 and its 503 on sign-in both mean "not now", whether from too many tries or
// its own load: say what to do, not which of the two it was.
#[error(
@@ -498,6 +539,9 @@ pub struct Client {
store: Store,
session: Session,
endpoints: Endpoints,
+ /// iCloud.com's servers, and Apple's regional ones: where [`Client::switch_servers`] goes.
+ home_endpoints: Endpoints,
+ regional_endpoints: Endpoints,
/// A sign-in in flight: its password, kept only once the sign-in completes, and the code
/// Apple wants for it (see [`state::SignIn`]).
sign: state::SignIn,
@@ -624,6 +668,44 @@ fn hash_of(bytes: &[u8]) -> u64 {
h.finish()
}
+/// Apple's client for an account at `endpoints`: their web origin on every request (what
+/// setup.icloud.com and the service endpoints expect; the idmsa sign-in calls name their own),
+/// the cookie jar the session keeps, and the rest every connection to Apple gets.
+fn apple_client(endpoints: &Endpoints, jar: &Arc<CookieStoreMutex>) -> Result<HttpClient> {
+ // The account's web origin — what setup and the service endpoints expect. The idmsa SRP
+ // endpoints want their own origin and override it per request.
+ let mut defaults = HeaderMap::new();
+ if let Ok(v) = HeaderValue::from_str(&endpoints.home) {
+ defaults.insert("origin", v);
+ }
+ if let Ok(v) = HeaderValue::from_str(&format!("{}/", endpoints.home)) {
+ defaults.insert("referer", v);
+ }
+ defaults.insert(
+ "accept-language",
+ HeaderValue::from_static("en-US,en;q=0.9"),
+ );
+
+ // Never waits for ever. After a sleep or a change of network a pooled connection can
+ // be dead without saying so, and a request on it hung until the kernel gave up — a
+ // quarter of an hour or more — holding the sign-in lock, so every screen waited too.
+ // A read that brings nothing for a minute is given up (a long download keeps going as
+ // long as bytes arrive); idle connections are let go of before they can go stale; and
+ // keep-alives notice a dead one.
+ apple_only()
+ // The jar itself, telling diagnostics (when the person turned them on) which
+ // cookies go out and come back, by name.
+ .cookie_provider(Arc::new(diag::Told(Arc::clone(jar))))
+ .user_agent(UA)
+ .default_headers(defaults)
+ .connect_timeout(std::time::Duration::from_secs(15))
+ .read_timeout(std::time::Duration::from_secs(60))
+ .pool_idle_timeout(std::time::Duration::from_secs(50))
+ .tcp_keepalive(std::time::Duration::from_secs(30))
+ .build()
+ .map_err(Error::from)
+}
+
impl Client {
pub fn new(state_dir: &Path) -> Result<Self> {
Self::with(state_dir, Endpoints::default(), Store::keyring(state_dir)?)
@@ -640,44 +722,36 @@ impl Client {
let (jar, cookies) = read_jar(&store);
let jar = Arc::new(CookieStoreMutex::new(jar));
- // Default origin is icloud.com — what setup.icloud.com and the service endpoints
- // expect. The idmsa SRP endpoints want their own origin and override it per request.
- let mut defaults = HeaderMap::new();
- if let Ok(v) = HeaderValue::from_str(&endpoints.home) {
- defaults.insert("origin", v);
- }
- if let Ok(v) = HeaderValue::from_str(&format!("{}/", endpoints.home)) {
- defaults.insert("referer", v);
- }
- defaults.insert(
- "accept-language",
- HeaderValue::from_static("en-US,en;q=0.9"),
- );
-
- // Never waits for ever. After a sleep or a change of network a pooled connection can
- // be dead without saying so, and a request on it hung until the kernel gave up — a
- // quarter of an hour or more — holding the sign-in lock, so every screen waited too.
- // A read that brings nothing for a minute is given up (a long download keeps going as
- // long as bytes arrive); idle connections are let go of before they can go stale; and
- // keep-alives notice a dead one.
- let http = apple_only()
- // The jar itself, telling diagnostics (when the person turned them on) which
- // cookies go out and come back, by name.
- .cookie_provider(Arc::new(diag::Told(Arc::clone(&jar))))
- .user_agent(UA)
- .default_headers(defaults)
- .connect_timeout(std::time::Duration::from_secs(15))
- .read_timeout(std::time::Duration::from_secs(60))
- .pool_idle_timeout(std::time::Duration::from_secs(50))
- .tcp_keepalive(std::time::Duration::from_secs(30))
- .build()?;
+ // The account's own servers: iCloud.com's, or Apple's regional ones for an account
+ // Apple keeps there (issue #10), unless a test pointed the client somewhere itself.
+ let endpoints = if endpoints == Endpoints::default() {
+ Endpoints::for_domain(session.domain.as_deref())
+ } else {
+ endpoints
+ };
+ open_regional(endpoints == Endpoints::for_domain(Some(REGIONAL_DOMAIN)));
+ let http = apple_client(&endpoints, &jar)?;
+ // Where switch_servers goes: a test's mock for both, unless it names another for the
+ // regional servers; Apple's own otherwise.
+ let home_endpoints = if endpoints == Endpoints::for_domain(Some(REGIONAL_DOMAIN)) {
+ Endpoints::default()
+ } else {
+ endpoints.clone()
+ };
+ let regional_endpoints = if home_endpoints == Endpoints::default() {
+ Endpoints::for_domain(Some(REGIONAL_DOMAIN))
+ } else {
+ home_endpoints.clone()
+ };
Ok(Self {
http,
jar,
store,
session,
endpoints,
+ home_endpoints,
+ regional_endpoints,
sign: state::SignIn::Idle,
keyring_error,
unsaved: None,
@@ -836,6 +910,8 @@ impl Client {
/// Forget this account entirely.
pub fn sign_out(&mut self) -> Result<()> {
self.session = Session::default();
+ // The next account starts at iCloud.com, and the regional domains close again.
+ self.switch_servers(None)?;
self.data_access_forget();
if let Ok(mut jar) = self.jar.lock() {
jar.clear();
@@ -1062,7 +1138,13 @@ impl Client {
// Kept only once Apple has accepted it all the way: see `absorb`.
self.sign.start(apple_id, password);
- let outcome = self.sign_in_inner(apple_id, password, code).await;
+ let mut outcome = self.sign_in_inner(apple_id, password, code).await;
+ // Apple keeps the account on its regional servers and wants the sign-in made there:
+ // made again there, once — this session now starts at Apple's servers for it.
+ if matches!(outcome, Err(Error::Moved)) {
+ self.sign.start(apple_id, password);
+ outcome = self.sign_in_inner(apple_id, password, code).await;
+ }
if outcome.is_err() {
self.sign.fail();
}
@@ -1081,7 +1163,9 @@ impl Client {
| Error::Keyring(_)
| Error::Io(_)
| Error::Json(_)
- | Error::RepairNeeded,
+ | Error::RepairNeeded
+ | Error::Moved
+ | Error::CodeAgain,
) => {}
Err(e) => {
self.session.consecutive_failures =
@@ -1419,19 +1503,13 @@ impl Client {
return Err(err);
}
if self.trust().await.is_ok() {
- let data = self.account_login().await?;
- self.absorb(&data);
- self.keep();
- return Ok(());
+ return self.open_after_code().await;
}
return Err(err);
}
self.escrow_if_asked(escrow).await?;
self.trust().await?;
- let data = self.account_login().await?;
- self.absorb(&data);
- self.keep();
- Ok(())
+ self.open_after_code().await
}
/// Submit the six-digit code, then ask Apple to trust this install.
@@ -1468,20 +1546,14 @@ impl Client {
// accepted. The status alone is not evidence, so ask whether the session became
// usable before reporting a failure.
if self.trust().await.is_ok() {
- let data = self.account_login().await?;
- self.absorb(&data);
- self.keep();
- return Ok(());
+ return self.open_after_code().await;
}
return Err(err);
}
self.escrow_if_asked(escrow).await?;
self.trust().await?;
- let data = self.account_login().await?;
- self.absorb(&data);
- self.keep();
- Ok(())
+ self.open_after_code().await
}
/// Whether `password` is this account's Apple ID password: what the Hidden album asks for,
@@ -1730,6 +1802,32 @@ impl Client {
self.capture(&r);
let status = r.status();
let text = r.text().await.unwrap_or_default();
+ // An account Apple keeps on its regional servers: answered `302` with only
+ // `{"domainToUse": "iCloud.com.cn"}` (issue #10; rclone#8257). Asked again there, with
+ // the sign-in this session has; an answer naming where it already is goes on below.
+ if let Some(to) = domain_to_use(&text) {
+ let here = self.session.domain.as_deref().unwrap_or(HOME_DOMAIN);
+ if !to.eq_ignore_ascii_case(here) {
+ if !to.eq_ignore_ascii_case(REGIONAL_DOMAIN)
+ && !to.eq_ignore_ascii_case(HOME_DOMAIN)
+ {
+ return Err(Error::Protocol(format!(
+ "Apple keeps this Apple Account at {to}, which Skykeep can't open yet"
+ )));
+ }
+ quiet!("quince: Apple keeps this account at {to}; asking there");
+ self.switch_servers(Some(&to))?;
+ return match Box::pin(self.account_login()).await {
+ Ok(data) => Ok(data),
+ // Not there to ask: said as it is, and asked again later.
+ Err(e @ (Error::Network(_) | Error::Unavailable(_) | Error::RateLimited)) => {
+ Err(e)
+ }
+ // A sign-in made at iCloud.com that Apple won't take there: made again.
+ Err(_) => Err(Error::Moved),
+ };
+ }
+ }
if !status.is_success() {
return Err(transient(status).unwrap_or_else(|| {
Error::Protocol(format!("couldn't open the account ({status})"))
@@ -1745,6 +1843,57 @@ impl Client {
Ok(data)
}
+ /// This account's servers, from now on: Apple's at `domain` (its `domainToUse`) — iCloud.com's
+ /// for `None` — with a client of their own (their web origin on every request), the lock
+ /// opened to Apple's regional domains only for an account kept there, and the choice kept
+ /// in the session so every later launch starts there. The cookies stay: each belongs to
+ /// the host that set it.
+ fn switch_servers(&mut self, domain: Option<&str>) -> Result<()> {
+ let regional = domain.is_some_and(|d| d.eq_ignore_ascii_case(REGIONAL_DOMAIN));
+ let endpoints = if regional {
+ self.regional_endpoints.clone()
+ } else {
+ self.home_endpoints.clone()
+ };
+ if endpoints != self.endpoints {
+ self.http = apple_client(&endpoints, &self.jar)?;
+ self.endpoints = endpoints;
+ }
+ open_regional(regional);
+ self.session.domain = regional.then(|| REGIONAL_DOMAIN.to_string());
+ let _ = self.persist();
+ Ok(())
+ }
+
+ /// Where a test's regional servers are (its mock), instead of Apple's. Test seam.
+ pub fn regional_endpoints_at(&mut self, endpoints: Endpoints) {
+ self.regional_endpoints = endpoints;
+ }
+
+ /// The account opened after a code was taken. An account Apple then names as kept on its
+ /// regional servers, where this sign-in isn't taken, is signed in again there with the
+ /// password still in flight: Apple sends a new code for them, and the code screen, where
+ /// the person is, says to enter it (issue #10).
+ async fn open_after_code(&mut self) -> Result<()> {
+ match self.account_login().await {
+ Ok(data) => {
+ self.absorb(&data);
+ self.keep();
+ Ok(())
+ }
+ Err(Error::Moved) => {
+ let Some((apple_id, password)) = self.sign.credentials() else {
+ return Err(Error::Moved);
+ };
+ match Box::pin(self.sign_in_as(&apple_id, &password, Code::Send)).await? {
+ SignInOutcome::Complete => Ok(()),
+ SignInOutcome::NeedsTwoFactor => Err(Error::CodeAgain),
+ }
+ }
+ Err(e) => Err(e),
+ }
+ }
+
fn absorb(&mut self, data: &Value) {
// A completed sign-in — with or without a code — is when a password has proved
// itself, so that is when it is kept.
@@ -1853,6 +2002,42 @@ impl Client {
/// lists as what Skykeep talks to, read from here so the list is the lock itself.
pub const APPLE_DOMAINS: [&str; 3] = ["apple.com", "icloud.com", "icloud-content.com"];
+/// Apple's own domains for the accounts it keeps at [`REGIONAL_DOMAIN`]: its sign-in, iCloud's
+/// services and their files there, each served under Apple's certificate. Reachable only once
+/// this computer's account is one of them ([`open_regional`]), so every other account's
+/// client reaches exactly [`APPLE_DOMAINS`], and Settings lists exactly what can be reached.
+pub const REGIONAL_DOMAINS: [&str; 3] = ["apple.com.cn", "icloud.com.cn", "icloud-content.com.cn"];
+
+/// Whether [`REGIONAL_DOMAINS`] are open: set when the signed-in account is kept there.
+static REGIONAL: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
+/// Open (or close) Apple's regional domains, for an account Apple keeps there (or no longer).
+pub fn open_regional(open: bool) {
+ REGIONAL.store(open, std::sync::atomic::Ordering::SeqCst);
+}
+
+/// Every domain Apple's client can reach right now: [`APPLE_DOMAINS`], and [`REGIONAL_DOMAINS`]
+/// while the account is kept there. What Settings lists, read from the lock itself.
+#[must_use]
+pub fn reachable_domains() -> Vec<&'static str> {
+ let mut all = APPLE_DOMAINS.to_vec();
+ if REGIONAL.load(std::sync::atomic::Ordering::SeqCst) {
+ all.extend(REGIONAL_DOMAINS);
+ }
+ all
+}
+
+/// The web origin Apple's services expect from this account: iCloud.com's, or its regional
+/// site's while the account is kept there. For requests that name it themselves (push, Drive).
+#[must_use]
+pub fn web_origin() -> &'static str {
+ if REGIONAL.load(std::sync::atomic::Ordering::SeqCst) {
+ "https://www.icloud.com.cn"
+ } else {
+ "https://www.icloud.com"
+ }
+}
+
/// Whether this computer counts as Apple's: in this crate's tests, and in a build made with the
/// `loopback` feature (the other crates' tests, whose mock servers run here) — never in the
/// app. A shipped client that reached `localhost` could be pointed, by an address in Apple's
@@ -1863,15 +2048,19 @@ const LOOPBACK: bool = cfg!(any(test, feature = "loopback"));
/// reason to reach. Loopback too, which is this computer, only in tests (see `LOOPBACK`).
#[must_use]
pub fn is_apple_host(host: &str) -> bool {
- apple_host(host, LOOPBACK)
+ apple_host(
+ host,
+ LOOPBACK,
+ REGIONAL.load(std::sync::atomic::Ordering::SeqCst),
+ )
}
-fn apple_host(host: &str, loopback: bool) -> bool {
+fn apple_host(host: &str, loopback: bool, regional: bool) -> bool {
let host = host.trim_end_matches('.').to_ascii_lowercase();
+ let under = |d: &&str| host == *d || host.ends_with(&format!(".{d}"));
(loopback && host == "localhost")
- || APPLE_DOMAINS
- .iter()
- .any(|d| host == *d || host.ends_with(&format!(".{d}")))
+ || APPLE_DOMAINS.iter().any(under)
+ || (regional && REGIONAL_DOMAINS.iter().any(under))
}
/// Every HTTP client Skykeep makes for iCloud starts here: Apple's servers, and nothing else,
@@ -2055,7 +2244,12 @@ fn url_apples(url: &reqwest::Url, loopback: bool) -> bool {
// can read them (pyicloud#356, #401). This computer, in tests, is the one exception.
Err(_) => {
let secure = matches!(url.scheme(), "https" | "wss");
- (secure || (loopback && host == "localhost")) && apple_host(host, loopback)
+ (secure || (loopback && host == "localhost"))
+ && apple_host(
+ host,
+ loopback,
+ REGIONAL.load(std::sync::atomic::Ordering::SeqCst),
+ )
}
}
}
@@ -2090,7 +2284,8 @@ mod release_tests {
#[test]
fn a_release_client_never_reaches_this_computer() {
for host in ["localhost", "LOCALHOST.", "127.0.0.1", "[::1]"] {
- assert!(!apple_host(host, false), "{host}");
+ assert!(!apple_host(host, false, false), "{host}");
+ assert!(!apple_host(host, false, true), "{host}");
}
for url in [
"http://localhost:8080/",
diff --git a/crates/quince-auth/src/session.rs b/crates/quince-auth/src/session.rs
index 2ec36d5..e2c20e3 100644
--- a/crates/quince-auth/src/session.rs
+++ b/crates/quince-auth/src/session.rs
@@ -112,6 +112,11 @@ pub struct Session {
/// Advanced Data Protection, which Apple calls iCloud Data Protection (`iCDPEnabled`).
#[serde(default)]
pub data_protection: Option<bool>,
+ /// Where Apple keeps this account when it is not iCloud.com: the `domainToUse` it answered
+ /// `accountLogin` with (issue #10), so every later launch goes straight to Apple's servers
+ /// there. None for every other account.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub domain: Option<String>,
/// Under Advanced Data Protection, until when (unix seconds) each service's key — by its
/// `requestPCS` name — was released to this session (see `pcs`). Kept for the services
/// whose key cookie is not known by name; the rest are read from the cookies themselves.
diff --git a/crates/quince-auth/src/state.rs b/crates/quince-auth/src/state.rs
index a5b58a1..67f2627 100644
--- a/crates/quince-auth/src/state.rs
+++ b/crates/quince-auth/src/state.rs
@@ -115,6 +115,17 @@ impl SignIn {
matches!(self, SignIn::Code { sent: true, .. })
}
+ /// The Apple ID and password in flight, if any: for signing in again elsewhere.
+ pub(crate) fn credentials(&self) -> Option<(String, String)> {
+ match self {
+ SignIn::Idle => None,
+ SignIn::Proving { apple_id, password }
+ | SignIn::Code {
+ apple_id, password, ..
+ } => Some((apple_id.clone(), password.clone())),
+ }
+ }
+
/// The password in flight, if any.
pub(crate) fn password(&self) -> Option<&str> {
match self {
diff --git a/crates/quince-auth/tests/regional_servers.rs b/crates/quince-auth/tests/regional_servers.rs
new file mode 100644
index 0000000..2fc3d4e
--- /dev/null
+++ b/crates/quince-auth/tests/regional_servers.rs
@@ -0,0 +1,247 @@
+//! Accounts Apple keeps on its regional servers (issue #10): Apple answers `accountLogin` at
+//! iCloud.com with `302 {"domainToUse": "iCloud.com.cn"}`, and the account is reached only at
+//! Apple's servers there. Followed by itself — after a code, with a new code where Apple asks
+//! for one — remembered, and undone at sign-out; any other domain is refused. Its own file, run
+//! one test at a time: which domains the lock opens is the process's.
+
+use quince_auth::{is_apple_host, reachable_domains, web_origin, Client, Endpoints, Error, Store};
+use serde_json::json;
+use wiremock::matchers::{method, path};
+use wiremock::{Mock, MockServer, Request, ResponseTemplate};
+
+static ONE_AT_A_TIME: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
+
+const HOME_ORIGIN: &str = "https://web.test.invalid";
+const REGIONAL_ORIGIN: &str = "https://web-regional.test.invalid";
+
+fn endpoints_for(server: &MockServer, home: &str) -> Endpoints {
+ let base = server.uri();
+ Endpoints {
+ auth_root: "https://idmsa.test.invalid".into(),
+ home: home.into(),
+ auth: format!("{base}/appleauth/auth"),
+ setup: format!("{base}/setup/ws/1"),
+ }
+}
+
+fn b64(bytes: &[u8]) -> String {
+ use base64::{engine::general_purpose::STANDARD as E, Engine};
+ E.encode(bytes)
+}
+
+/// Apple's sign-in up to its code: a challenge, then 409 for the code, which is taken.
+async fn mount_sign_in(server: &MockServer) {
+ Mock::given(method("POST"))
+ .and(path("/appleauth/auth/signin/init"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "salt": b64(&[0x77; 16]),
+ "b": b64(&[0x5e; 256]),
+ "c": "challenge-c",
+ "iteration": 20173,
+ "protocol": "s2k",
+ })))
+ .mount(server)
+ .await;
+ Mock::given(method("POST"))
+ .and(path("/appleauth/auth/signin/complete"))
+ .respond_with(
+ ResponseTemplate::new(409)
+ .insert_header("X-Apple-Session-Token", "session-token")
+ .set_body_json(json!({})),
+ )
+ .mount(server)
+ .await;
+ for p in ["/appleauth/auth/verify/trusteddevice/securitycode"] {
+ Mock::given(method("POST"))
+ .and(path(p))
+ .respond_with(ResponseTemplate::new(204))
+ .mount(server)
+ .await;
+ }
+ // Asking for the code to be sent, as a sign-in someone watches does.
+ Mock::given(method("PUT"))
+ .and(path("/appleauth/auth/verify/trusteddevice/securitycode"))
+ .respond_with(ResponseTemplate::new(202))
+ .mount(server)
+ .await;
+ Mock::given(method("GET"))
+ .and(path("/appleauth/auth/2sv/trust"))
+ .respond_with(
+ ResponseTemplate::new(204)
+ .insert_header("X-Apple-TwoSV-Trust-Token", "trust-token")
+ .insert_header("X-Apple-Session-Token", "session-token-2"),
+ )
+ .mount(server)
+ .await;
+}
+
+/// iCloud.com, for an account kept elsewhere: everything as usual until `accountLogin`.
+async fn mount_moved(server: &MockServer, to: &str) {
+ mount_sign_in(server).await;
+ Mock::given(method("POST"))
+ .and(path("/setup/ws/1/accountLogin"))
+ .respond_with(ResponseTemplate::new(302).set_body_json(json!({ "domainToUse": to })))
+ .mount(server)
+ .await;
+}
+
+async fn mount_opened(server: &MockServer) {
+ Mock::given(method("POST"))
+ .and(path("/setup/ws/1/accountLogin"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(json!({
+ "dsInfo": { "dsid": "2026110700" },
+ "webservices": {
+ "ckdatabasews": { "url": "https://p01-ckdatabasews.icloud.com.cn:443", "status": "active" }
+ }
+ })))
+ .mount(server)
+ .await;
+}
+
+fn hits<'a>(reqs: &'a [Request], suffix: &str) -> Vec<&'a Request> {
+ reqs.iter()
+ .filter(|r| r.url.path().ends_with(suffix))
+ .collect()
+}
+
+fn origin(r: &Request) -> &str {
+ r.headers
+ .get("origin")
+ .and_then(|v| v.to_str().ok())
+ .unwrap_or("")
+}
+
+/// The issue's own path: the code is taken at iCloud.com, Apple then names its regional
+/// servers, and the sign-in made at iCloud.com isn't taken there. Signed in again there, the
+/// person is asked for the new code Apple sent, and is in.
+#[tokio::test]
+async fn an_account_kept_regionally_signs_in_there_after_a_new_code() {
+ let _one = ONE_AT_A_TIME.lock().await;
+ let home = MockServer::start().await;
+ let regional = MockServer::start().await;
+ mount_moved(&home, "iCloud.com.cn").await;
+ mount_sign_in(&regional).await;
+ // The iCloud.com sign-in, once, refused there; the one made there, taken.
+ Mock::given(method("POST"))
+ .and(path("/setup/ws/1/accountLogin"))
+ .respond_with(ResponseTemplate::new(421).set_body_json(json!({ "error": 1 })))
+ .up_to_n_times(1)
+ .with_priority(1)
+ .mount(&regional)
+ .await;
+ mount_opened(&regional).await;
+
+ let dir = tempfile::tempdir().unwrap();
+ let mut client = Client::with(
+ dir.path(),
+ endpoints_for(&home, HOME_ORIGIN),
+ Store::file(dir.path()).unwrap(),
+ )
+ .unwrap();
+ client.regional_endpoints_at(endpoints_for(&regional, REGIONAL_ORIGIN));
+ assert!(
+ !is_apple_host("setup.icloud.com.cn"),
+ "closed before any account needs it"
+ );
+
+ client
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap();
+ let again = client.submit_two_factor("111111").await.unwrap_err();
+ assert!(matches!(again, Error::CodeAgain), "{again}");
+ assert!(
+ !again.to_string().to_lowercase().contains("china"),
+ "{again}"
+ );
+ client.submit_two_factor("222222").await.unwrap();
+
+ let session = client.session();
+ assert_eq!(session.domain.as_deref(), Some("icloud.com.cn"));
+ assert_eq!(session.dsid.as_deref(), Some("2026110700"));
+ let seen = regional.received_requests().await.unwrap();
+ assert_eq!(
+ hits(&seen, "/signin/init").len(),
+ 1,
+ "signed in again there, once"
+ );
+ let logins = hits(&seen, "/accountLogin");
+ assert_eq!(logins.len(), 2);
+ assert!(
+ logins.iter().all(|r| origin(r) == REGIONAL_ORIGIN),
+ "its servers' origin"
+ );
+ // Its servers are reachable, and said to be, only now; the web origin follows.
+ assert!(is_apple_host("setup.icloud.com.cn"));
+ assert!(is_apple_host("cvws.icloud-content.com.cn"));
+ assert!(reachable_domains().contains(&"icloud.com.cn"));
+ assert_eq!(web_origin(), "https://www.icloud.com.cn");
+
+ // Kept: the next launch starts there.
+ let reloaded = Client::with(
+ dir.path(),
+ endpoints_for(&home, HOME_ORIGIN),
+ Store::file(dir.path()).unwrap(),
+ )
+ .unwrap();
+ assert_eq!(reloaded.session().domain.as_deref(), Some("icloud.com.cn"));
+
+ // Signing out: the next account starts at iCloud.com, and the regional domains close.
+ client.sign_out().unwrap();
+ assert!(!is_apple_host("setup.icloud.com.cn"));
+ assert!(!reachable_domains().contains(&"icloud.com.cn"));
+ assert_eq!(web_origin(), "https://www.icloud.com");
+}
+
+/// Where Apple's regional servers take the sign-in already made: in, with no second code.
+#[tokio::test]
+async fn an_account_kept_regionally_needs_no_second_code_when_apple_takes_the_first() {
+ let _one = ONE_AT_A_TIME.lock().await;
+ let home = MockServer::start().await;
+ let regional = MockServer::start().await;
+ mount_moved(&home, "iCloud.com.cn").await;
+ mount_opened(&regional).await;
+
+ let dir = tempfile::tempdir().unwrap();
+ let mut client = Client::with(
+ dir.path(),
+ endpoints_for(&home, HOME_ORIGIN),
+ Store::file(dir.path()).unwrap(),
+ )
+ .unwrap();
+ client.regional_endpoints_at(endpoints_for(&regional, REGIONAL_ORIGIN));
+ client
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap();
+ client.submit_two_factor("111111").await.unwrap();
+
+ assert_eq!(client.session().domain.as_deref(), Some("icloud.com.cn"));
+ let seen = regional.received_requests().await.unwrap();
+ assert!(hits(&seen, "/signin/init").is_empty(), "no second sign-in");
+ client.sign_out().unwrap();
+}
+
+/// A domain Apple names that isn't one Skykeep knows is said plainly, and opens nothing.
+#[tokio::test]
+async fn an_unknown_domain_opens_nothing() {
+ let _one = ONE_AT_A_TIME.lock().await;
+ let home = MockServer::start().await;
+ mount_moved(&home, "icloud.example").await;
+ let dir = tempfile::tempdir().unwrap();
+ let mut client = Client::with(
+ dir.path(),
+ endpoints_for(&home, HOME_ORIGIN),
+ Store::file(dir.path()).unwrap(),
+ )
+ .unwrap();
+ client
+ .sign_in("[email protected]", "hunter2")
+ .await
+ .unwrap();
+ let err = client.submit_two_factor("111111").await.unwrap_err();
+ assert!(err.to_string().contains("can't open"), "{err}");
+ assert_eq!(client.session().domain, None);
+ assert!(!is_apple_host("setup.icloud.com.cn"));
+ assert!(!is_apple_host("icloud.example"));
+}
diff --git a/crates/quince-icloud/src/courier.rs b/crates/quince-icloud/src/courier.rs
index 589ce26..aa88349 100644
--- a/crates/quince-icloud/src/courier.rs
+++ b/crates/quince-icloud/src/courier.rs
@@ -42,6 +42,11 @@ pub const SERVER: &str = "websocket.push.apple.com";
pub const REGISTRY: &str =
"https://gatewayws.icloud.com/pds/WebObjects/pushregistry.woa/webBatchRegister/batchRegisterToken";
+/// The same registry at Apple's regional servers, for an account Apple keeps there (issue #10).
+/// The socket is the one push server for every account.
+pub const REGIONAL_REGISTRY: &str =
+ "https://gatewayws.icloud.com.cn/pds/WebObjects/pushregistry.woa/webBatchRegister/batchRegisterToken";
+
/// How long a token asked for lives — the shell's own `86400`.
const TOKEN_SECONDS: u32 = 86_400;
@@ -410,10 +415,11 @@ fn push_base(server: &str, loopback: bool) -> Option<String> {
}
impl Courier {
- /// Connect to `server` (normally [`SERVER`]), get or keep a token, and filter the socket
- /// to the containers' topics. Not yet registered: see [`Courier::register`].
+ /// Connect to `server` (normally [`SERVER`]) as the account's web site `origin` would, get or
+ /// keep a token, and filter the socket to the containers' topics. Not yet registered: see [`Courier::register`].
pub async fn connect(
server: &str,
+ origin: &'static str,
identity: &mut Identity,
containers: &[&str],
) -> std::result::Result<Self, Failure> {
@@ -435,7 +441,7 @@ impl Courier {
.map_err(|e| Failure::Transient(format!("bad push address: {e}")))?;
request.headers_mut().insert(
"origin",
- tungstenite::http::HeaderValue::from_static("https://www.icloud.com"),
+ tungstenite::http::HeaderValue::from_static(origin),
);
let (mut ws, _) =
tokio::time::timeout(ANSWER, tokio_tungstenite::connect_async(request))
@@ -970,6 +976,7 @@ mod tests {
let mut id = Identity::new();
let mut courier = Courier::connect(
&format!("ws://{addr}"),
+ "https://www.icloud.com",
&mut id,
&["com.apple.notes", "com.apple.reminders"],
)
@@ -988,7 +995,13 @@ mod tests {
#[tokio::test]
async fn only_apple_is_ever_dialled() {
let mut id = Identity::new();
- let got = Courier::connect("example.com", &mut id, &["com.apple.notes"]).await;
+ let got = Courier::connect(
+ "example.com",
+ "https://www.icloud.com",
+ &mut id,
+ &["com.apple.notes"],
+ )
+ .await;
assert!(matches!(got, Err(Failure::Refused(_))));
}
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rs
index 5242741..c05dd11 100644
--- a/crates/quince-icloud/src/drive.rs
+++ b/crates/quince-icloud/src/drive.rs
@@ -3572,13 +3572,9 @@ async fn post_json(
let mut tries: u64 = 0;
let (status, text) = loop {
tries += 1;
- let response = http
- .post(url)
- .query(params)
- .header("Referer", "https://www.icloud.com/")
- .json(body)
- .send()
- .await?;
+ // The account's client sends its web origin and referer on every request: iCloud.com's,
+ // or Apple's regional site's for an account kept there (issue #10).
+ let response = http.post(url).query(params).json(body).send().await?;
let status = response.status();
let wait = response
.headers()
diff --git a/probe/examples/drive_courier_live.rs b/probe/examples/drive_courier_live.rs
index 6cdf3af..4683a07 100644
--- a/probe/examples/drive_courier_live.rs
+++ b/probe/examples/drive_courier_live.rs
@@ -28,6 +28,7 @@ async fn main() -> Result<()> {
let mut id = quince_icloud::courier::Identity::new();
let mut courier = quince_icloud::courier::Courier::connect(
quince_icloud::courier::SERVER,
+ "https://www.icloud.com",
&mut id,
&containers,
)
diff --git a/probe/examples/push_courier.rs b/probe/examples/push_courier.rs
index b11e110..7bb49f8 100644
--- a/probe/examples/push_courier.rs
+++ b/probe/examples/push_courier.rs
@@ -30,6 +30,7 @@ async fn main() -> Result<()> {
let t = Instant::now();
let mut courier = quince_icloud::courier::Courier::connect(
quince_icloud::courier::SERVER,
+ "https://www.icloud.com",
&mut id,
&containers,
)
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index 031e026..c2b26b1 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -2618,7 +2618,8 @@ fn save_subscriptions(
}
/// What Skykeep can reach, for Settings: Apple's domains as the network lock holds them
-/// (`quince_auth::APPLE_DOMAINS`, the list the resolver checks), the host of each calendar
+/// (`quince_auth::reachable_domains`, the list the resolver checks — Apple's regional ones too
+/// only for an account kept there), the host of each calendar
/// subscribed to, which only its own client can reach, and GitHub's release servers, which
/// only the updater's client can reach (`quince_auth::releases::RELEASE_HOSTS`) — listed only
/// for a copy that updates itself, since no other ever contacts them.
@@ -2641,7 +2642,7 @@ async fn network_reach(app: tauri::AppHandle, feeds: State<'_, Feeds>) -> UiResu
subscribed.sort();
subscribed.dedup();
Ok(Reach {
- apple: quince_auth::APPLE_DOMAINS.to_vec(),
+ apple: quince_auth::reachable_domains(),
subscribed,
updates: if updates::this_install(&app) == updates::Install::Here {
quince_auth::releases::RELEASE_HOSTS.to_vec()
diff --git a/src-tauri/src/push.rs b/src-tauri/src/push.rs
index 68822bc..23cab04 100644
--- a/src-tauri/src/push.rs
+++ b/src-tauri/src/push.rs
@@ -502,6 +502,16 @@ async fn listen(client: Arc<Mutex<Client>>, tx: mpsc::UnboundedSender<&'static s
}
}
+/// The push registry for this account: iCloud.com's, or Apple's regional one for an account
+/// Apple keeps there (issue #10). Where its tokens are registered follows where it is kept.
+fn registry() -> &'static str {
+ if quince_auth::web_origin() == "https://www.icloud.com" {
+ courier::REGISTRY
+ } else {
+ courier::REGIONAL_REGISTRY
+ }
+}
+
/// Connect, get a token and register it.
async fn open(
http: &reqwest::Client,
@@ -509,11 +519,14 @@ async fn open(
containers: &[&str],
dsid: &str,
) -> Result<Courier, Failure> {
- let mut channel = Courier::connect(courier::SERVER, identity, containers).await?;
- if let Err(f) = channel
- .register(http, courier::REGISTRY, identity, dsid)
- .await
- {
+ let mut channel = Courier::connect(
+ courier::SERVER,
+ quince_auth::web_origin(),
+ identity,
+ containers,
+ )
+ .await?;
+ if let Err(f) = channel.register(http, registry(), identity, dsid).await {
channel.close().await;
return Err(f);
}
@@ -536,10 +549,7 @@ async fn run(
return None;
}
if registered.elapsed() >= RENEW_REGISTRATION {
- if let Err(f) = channel
- .register(http, courier::REGISTRY, identity, dsid)
- .await
- {
+ if let Err(f) = channel.register(http, registry(), identity, dsid).await {
return Some(f);
}
registered = Instant::now();