Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / wps_harvest / src / bin / wps_extract.rs
wps_extract.rs130 lines · 4.6 KiBRaw
| 1 | //! `wps_extract V1.wpsdb --out seed-bssids.txt` |
| 2 | //! |
| 3 | //! Pull the BSSIDs out of a previous-generation `WPSDB1` store, to seed a crawl's frontier. |
| 4 | //! |
| 5 | //! **Identities only.** The v1 coordinates are not read and deliberately cannot be: v1 |
| 6 | //! quantized to a 20 m grid and recorded no accuracy, so importing a position would mean |
| 7 | //! inventing one. Worse, `quality::reduce` keeps whichever observation claims the better |
| 8 | //! accuracy — and since Apple typically reports 20-100 m for WiFi, a v1 position labelled 20 m |
| 9 | //! would beat most real crawled measurements and pin the beacon to its old grid cell. Every |
| 10 | //! coordinate in the new store comes from a source that also said how accurate it was. |
| 11 | //! |
| 12 | //! This is nonetheless the best seed available: there is no open bulk WiFi dump, and the |
| 13 | //! alternatives are a local scan (one metro) or guessing addresses for hours. |
| 14 | |
| 15 | use std::fs::File; |
| 16 | use std::io::{BufWriter, Write}; |
| 17 | use std::path::PathBuf; |
| 18 | use std::process::ExitCode; |
| 19 | |
| 20 | use wps_harvest::keys; |
| 21 | use wps_harvest::v1; |
| 22 | |
| 23 | const USAGE: &str = "\ |
| 24 | Usage: wps_extract V1.wpsdb --out FILE [--stride N] |
| 25 | |
| 26 | --out BSSIDs to write, one per line, for wps_crawl --seed |
| 27 | --stride keep every Nth BSSID (default 1 = all). The crawl reaches a beacon's neighbours |
| 28 | anyway, so a thinned list still covers the same ground with a smaller frontier."; |
| 29 | |
| 30 | struct Args { |
| 31 | input: PathBuf, |
| 32 | out: PathBuf, |
| 33 | stride: usize, |
| 34 | } |
| 35 | |
| 36 | fn parse_args(argv: &[String]) -> Result<Args, String> { |
| 37 | let mut input = None; |
| 38 | let mut out = None; |
| 39 | let mut stride = 1usize; |
| 40 | |
| 41 | let mut i = 0; |
| 42 | while i < argv.len() { |
| 43 | let a = argv[i].as_str(); |
| 44 | let value = || argv.get(i + 1).cloned().ok_or_else(|| format!("{a} needs a value")); |
| 45 | match a { |
| 46 | "--out" => { |
| 47 | out = Some(PathBuf::from(value()?)); |
| 48 | i += 2; |
| 49 | } |
| 50 | "--stride" => { |
| 51 | stride = value()?.parse().map_err(|_| "--stride must be a number")?; |
| 52 | i += 2; |
| 53 | } |
| 54 | "-h" | "--help" => return Err("help".to_string()), |
| 55 | other if other.starts_with('-') => return Err(format!("unknown option: {other}")), |
| 56 | other => { |
| 57 | if input.replace(PathBuf::from(other)).is_some() { |
| 58 | return Err("more than one input given".to_string()); |
| 59 | } |
| 60 | i += 1; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | Ok(Args { |
| 65 | input: input.ok_or("an input .wpsdb is required")?, |
| 66 | out: out.ok_or("--out is required")?, |
| 67 | stride: stride.max(1), |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | fn format_mac(mac: u64) -> String { |
| 72 | let b = mac.to_be_bytes(); |
| 73 | format!("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", b[2], b[3], b[4], b[5], b[6], b[7]) |
| 74 | } |
| 75 | |
| 76 | fn run(args: &Args) -> std::io::Result<()> { |
| 77 | println!("Reading {}", args.input.display()); |
| 78 | let buf = std::fs::read(&args.input)?; |
| 79 | println!(" {} on disk", wps_harvest::human_bytes(buf.len() as u64)); |
| 80 | |
| 81 | let started = std::time::Instant::now(); |
| 82 | let store = v1::parse(&buf)?; |
| 83 | println!( |
| 84 | " {} key(s), {}-bit universe, parsed in {:.1}s", |
| 85 | store.keys.len(), |
| 86 | store.universe_bits, |
| 87 | started.elapsed().as_secs_f64() |
| 88 | ); |
| 89 | |
| 90 | if store.universe_bits != keys::WIFI_UNIVERSE_BITS { |
| 91 | // v1 cell keys packed mcc|mnc|tac|cid with no radio type and a 28-bit cell id, so a 5G |
| 92 | // NCI was already truncated when they were written. There is no way to recover which |
| 93 | // tower a key meant, and a crawl seeded with them would query identities that never |
| 94 | // existed. |
| 95 | eprintln!( |
| 96 | "error: this is a {}-bit (cell) store. v1 cell keys carry no radio type and a \ |
| 97 | truncated cell id, so they do not identify a real tower. Seed cells from \ |
| 98 | OpenCelliD instead.", |
| 99 | store.universe_bits |
| 100 | ); |
| 101 | return Ok(()); |
| 102 | } |
| 103 | |
| 104 | let mut w = BufWriter::with_capacity(1 << 20, File::create(&args.out)?); |
| 105 | let (mut written, mut randomized) = (0u64, 0u64); |
| 106 | for (i, &mac) in store.keys.iter().enumerate() { |
| 107 | // v1 never filtered these. They are not stable identifiers, Apple will not know them, |
| 108 | // and the store builder drops them — so querying one is a wasted request. |
| 109 | if keys::is_randomized_mac(mac) { |
| 110 | randomized += 1; |
| 111 | continue; |
| 112 | } |
| 113 | if i % args.stride != 0 { |
| 114 | continue; |
| 115 | } |
| 116 | writeln!(w, "{}", format_mac(mac))?; |
| 117 | written += 1; |
| 118 | } |
| 119 | w.flush()?; |
| 120 | |
| 121 | println!( |
| 122 | "Wrote {} ({written} BSSID(s); {randomized} randomized or locally-administered skipped)", |
| 123 | args.out.display() |
| 124 | ); |
| 125 | Ok(()) |
| 126 | } |
| 127 | |
| 128 | fn main() -> ExitCode { |
| 129 | wps_harvest::run_main(parse_args, USAGE, run) |
| 130 | } |