//! Client for Apple's location service at `gs-loc.apple.com/clls/wloc`. //! //! Ported from the Kotlin client this app used to carry, recovered in //! `reference/ApplePositioningService.kt.txt`; the wire schema is `reference/apple_wps.proto`. //! //! Apple does not return a device position. Given beacon identifiers it returns each beacon's //! own coordinates and horizontal accuracy — which is precisely a beacon database lookup, and //! why this makes a store builder rather than a positioning client. //! //! It also returns **more beacons than were asked for**: a query for one BSSID comes back with //! a few hundred others in the same area. That is what makes a crawl possible at all, since //! every response both answers the query and extends the frontier. //! //! ## This talks to somebody else's service //! //! Requests are serialized through one client and rate-limited by [`Client::min_interval`], //! defaulting to something a person could plausibly generate. Errors back off rather than //! retrying hard. Doing otherwise would be both rude and self-defeating. use std::time::{Duration, Instant}; use crate::proto::{self, Field}; const ENDPOINT: &str = "https://gs-loc.apple.com/clls/wloc"; /// Apple prefixes the response protobuf with a fixed-size header. const RESPONSE_HEADER_BYTES: usize = 10; /// OS version string in the request envelope. Must be 10 bytes to match the hardcoded length. const OS_VERSION: &str = "17.5.21F79"; /// Coordinates arrive as integer degrees x 1e8, which is exactly what the store keeps. pub const COORD_SCALE: i64 = 100_000_000; /// What Apple returns for a beacon it does not know: -180 degrees, in both axes. const NOT_FOUND_E8: i64 = -18_000_000_000; // Field numbers within `AppleWLoc`, from wtfps's `src/apple.proto`. const F_WIFI_DEVICES: u32 = 2; const F_CELL_RESPONSE: u32 = 22; const F_CELL_REQUEST: u32 = 25; /// Altitude (Apple's `location.5`) meaning "not reported": Apple sends -1. pub const ALT_NOT_REPORTED: i64 = -1; /// Clamp a source-reported altitude into the store's `i16` metres, mapping Apple's -1 /// "not reported" to [`crate::record::ALT_UNKNOWN`]. Saturates rather than wrapping: nothing /// real sits outside ±32 km, so saturation can only trigger on garbage, which then reads as /// extreme rather than plausible. pub fn clamp_altitude(metres: i64) -> i16 { if metres == ALT_NOT_REPORTED { i16::MIN } else { metres.clamp(i16::MIN as i64 + 1, i16::MAX as i64) as i16 } } /// A beacon Apple knows the location of. #[derive(Debug, Clone, PartialEq, Eq)] pub struct WifiFix { /// Lowercase `aa:bb:cc:dd:ee:ff`, as echoed back. pub bssid: String, /// Latitude in degrees x 1e8. pub lat_e8: i64, /// Longitude in degrees x 1e8. pub lon_e8: i64, /// Horizontal accuracy in metres. pub accuracy_m: i32, /// Altitude in metres, or [`crate::record::ALT_UNKNOWN`]. Present on essentially every /// WiFi fix (Apple's `location.5`); cell responses have no altitude field at all. pub altitude_m: i16, } /// A cell tower Apple knows the location of. #[derive(Debug, Clone, PartialEq, Eq)] pub struct CellFix { /// Mobile country code. pub mcc: i32, /// Mobile network code. pub mnc: i32, /// Cell id as Apple reports it. pub cell_id: i64, /// TAC or LAC. pub area: i32, /// Latitude in degrees x 1e8. pub lat_e8: i64, /// Longitude in degrees x 1e8. pub lon_e8: i64, /// Horizontal accuracy in metres. pub accuracy_m: i32, } /// What went wrong with a request. Callers distinguish these because they back off /// differently: a transport hiccup is worth retrying, a refusal is not. #[derive(Debug)] pub enum Error { /// The request never completed. Transport(String), /// A non-200 response. Status(u16), /// A 200 that did not contain a parseable body. Malformed(&'static str), } impl std::fmt::Display for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Error::Transport(e) => write!(f, "transport: {e}"), Error::Status(c) => write!(f, "HTTP {c}"), Error::Malformed(m) => write!(f, "malformed response: {m}"), } } } impl std::error::Error for Error {} /// A rate-limited gs-loc client. pub struct Client { agent: ureq::Agent, /// Minimum wall time between two requests from this client. pub min_interval: Duration, last: Option, } impl Client { /// Build a client. /// /// A global timeout is not optional: without one a stalled connection hangs the crawl /// indefinitely, and the service does sometimes accept a connection and then never answer. pub fn new(min_interval: Duration) -> Client { let config = ureq::Agent::config_builder() .timeout_global(Some(Duration::from_secs(20))) .timeout_connect(Some(Duration::from_secs(10))) .build(); Client { agent: config.into(), min_interval, last: None } } fn throttle(&mut self) { if let Some(last) = self.last { let elapsed = last.elapsed(); if elapsed < self.min_interval { std::thread::sleep(self.min_interval - elapsed); } } self.last = Some(Instant::now()); } /// Ask for the locations of `bssids`. /// /// The response carries far more devices than were sent — Apple volunteers the neighbours /// of anything it recognises, which is the whole basis of the crawl. There is no field /// asking for that; it is simply what the service does. pub fn query_wifi(&mut self, bssids: &[String]) -> Result, Error> { if bssids.is_empty() { return Ok(Vec::new()); } let mut body = Vec::new(); for b in bssids { let mut dev = Vec::new(); proto::put_bytes(&mut dev, 1, b.as_bytes()); proto::put_bytes(&mut body, F_WIFI_DEVICES, &dev); } let resp = self.post(&body)?; Ok(parse_wifi(&resp)) } /// Ask for the location of one cell tower. /// /// `AppleWLoc` carries a single `cell_tower_request` (field 25) and answers in a repeated /// `cell_tower_response` (field 22), so unlike WiFi this is one tower per request. pub fn query_cell( &mut self, mcc: u32, mnc: u32, cell_id: u32, tac: u32, ) -> Result, Error> { let mut tower = Vec::new(); proto::put_int(&mut tower, 1, mcc as i64); proto::put_int(&mut tower, 2, mnc as i64); proto::put_int(&mut tower, 3, cell_id as i64); proto::put_int(&mut tower, 4, tac as i64); let mut body = Vec::new(); proto::put_bytes(&mut body, F_CELL_REQUEST, &tower); let resp = self.post(&body)?; Ok(parse_cells(&resp)) } fn post(&mut self, payload: &[u8]) -> Result, Error> { self.throttle(); let framed = frame(payload); let mut resp = self .agent .post(ENDPOINT) .header("Content-Type", "application/x-www-form-urlencoded") .header("Accept", "*/*") .header("Accept-Charset", "utf-8") .header("Accept-Language", "en-us") .header("User-Agent", "locationd/1753.17 CFNetwork/711.1.12 Darwin/14.0.0") .send(&framed[..]) .map_err(|e| match e { ureq::Error::StatusCode(code) => Error::Status(code), other => Error::Transport(other.to_string()), })?; let raw = resp .body_mut() .with_config() .limit(64 * 1024 * 1024) .read_to_vec() .map_err(|e| Error::Transport(e.to_string()))?; if raw.len() <= RESPONSE_HEADER_BYTES { return Err(Error::Malformed("shorter than the response header")); } Ok(raw[RESPONSE_HEADER_BYTES..].to_vec()) } } /// Wrap `payload` in Apple's request envelope. /// /// Byte-for-byte from `joelkoen/wtfps` (`src/fetch.rs`), which is a working client. The /// envelope recovered from this app's old Kotlin implementation was **wrong** in three ways /// and produced an empty response every time: /// /// * its trailer was eight bytes (`00 00 00 01` then `00 00 00 00`); the real one is **seven**; /// * it length-prefixed the body with a big-endian `u16`; the real one uses a protobuf /// **varint**, which for a short body is a single byte — so the two together put two spurious /// zero bytes in front of every request; /// * it sent the body once. The real one sends it **twice**, length-delimited and then again. /// /// The duplicate looks like a mistake and may well be one, but it is what the working client /// does and this is not the place to find out whether Apple tolerates its absence. pub fn frame(payload: &[u8]) -> Vec { let mut out = Vec::with_capacity(payload.len() * 2 + 64); out.extend_from_slice(&[0x00, 0x01, 0x00, 0x05]); out.extend_from_slice(b"en_US"); out.extend_from_slice(&[0x00, 0x13]); out.extend_from_slice(b"com.apple.locationd"); out.extend_from_slice(&[0x00, 0x0a]); out.extend_from_slice(OS_VERSION.as_bytes()); out.extend_from_slice(&[0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]); proto::put_varint(&mut out, payload.len() as u64); out.extend_from_slice(payload); out.extend_from_slice(payload); out } /// A location submessage. `None` when the beacon is unknown, which Apple signals by returning /// -180 degrees in both axes rather than by omitting the field. /// /// Returns `(lat, lon, accuracy_m, altitude_m)`. Only WiFi carries fields 5/6 (altitude and /// vertical accuracy); cell locations top out at field 4, so their altitude reads as /// "not reported" without any special-casing. fn parse_location(body: &[u8]) -> Option<(i64, i64, i32, i16)> { let mut lat = None; let mut lon = None; let mut acc: i64 = -1; let mut alt: i64 = ALT_NOT_REPORTED; let r = proto::Reader::new(body); for (field, v) in r { if let Field::Varint(raw) = v { match field { 1 => lat = Some(raw as i64), 2 => lon = Some(raw as i64), 3 => acc = raw as i64, 5 => alt = raw as i64, _ => {} } } } let (lat, lon) = (lat?, lon?); if lat == NOT_FOUND_E8 || lon == NOT_FOUND_E8 { return None; } // Accuracy is int64 on the wire but only ever a plausible radius in metres; clamp rather // than truncate so a hostile or garbled value cannot wrap into something small and // confident-looking. Some((lat, lon, acc.clamp(-1, i32::MAX as i64) as i32, clamp_altitude(alt))) } fn parse_wifi(body: &[u8]) -> Vec { let mut out = Vec::new(); let r = proto::Reader::new(body); for (field, v) in r { let Field::Bytes(dev) = v else { continue }; if field != F_WIFI_DEVICES { continue; } let mut bssid = None; let mut loc = None; let dr = proto::Reader::new(dev); for (df, dv) in dr { match (df, dv) { (1, Field::Bytes(s)) => bssid = std::str::from_utf8(s).ok(), (2, Field::Bytes(b)) => loc = parse_location(b), _ => {} } } if let (Some(bssid), Some((lat_e8, lon_e8, accuracy_m, altitude_m))) = (bssid, loc) { out.push(WifiFix { bssid: bssid.to_ascii_lowercase(), lat_e8, lon_e8, accuracy_m, altitude_m, }); } } out } fn parse_cells(body: &[u8]) -> Vec { let mut out = Vec::new(); let r = proto::Reader::new(body); for (field, v) in r { let Field::Bytes(tower) = v else { continue }; if field != F_CELL_RESPONSE { continue; } let (mut mcc, mut mnc, mut cell_id, mut area) = (None, None, None, None); let mut loc = None; let tr = proto::Reader::new(tower); for (tf, tv) in tr { match (tf, tv) { (1, Field::Varint(x)) => mcc = Some(x as i64 as i32), (2, Field::Varint(x)) => mnc = Some(x as i64 as i32), (3, Field::Varint(x)) => cell_id = Some(x as i64), (4, Field::Varint(x)) => area = Some(x as i64 as i32), (5, Field::Bytes(b)) => loc = parse_location(b), _ => {} } } if let (Some(mcc), Some(mnc), Some(cell_id), Some(area), Some((lat_e8, lon_e8, acc, _))) = (mcc, mnc, cell_id, area, loc) { out.push(CellFix { mcc, mnc, cell_id, area, lat_e8, lon_e8, accuracy_m: acc }); } } out } include!("gsloc_s1.rs");