Files
ruvnet--RuView/v2/crates/wifi-densepose-geo/src/locate.rs
T
rUv 004a63e82d fix(security): audit — fix RUSTSEC vulns, clippy warnings, dead code (#769)
- Upgrade openssl to 0.10.78 (CVE-2026-41676), jsonwebtoken to 9.4
- Suppress unmaintained-only/no-CVE advisories in .cargo/audit.toml
  with per-entry rationale
- Fix all `cargo clippy --all-targets -- -D warnings` errors across
  35 crates: derivable_impls, needless_range_loop, map_or→is_some_and/
  is_none_or, await_holding_lock (drop MutexGuard before .await),
  ptr_arg (&mut Vec→&mut [T]), useless_conversion, approximate_constant
  (2.718→E, 3.14→PI), field_reassign_with_default, manual_inspect,
  useless_vec, lines_filter_map_ok, print_literal, dead_code
- Apply `cargo fmt --all`
- Pre-existing test failure in wifi-densepose-signal
  (test_estimate_occupancy_noise_only) is not introduced by this PR
2026-05-23 05:36:13 -04:00

43 lines
1.2 KiB
Rust

//! IP geolocation — determine location from public IP.
use crate::types::GeoPoint;
use anyhow::Result;
/// Locate by IP address (free, no API key).
pub async fn locate_by_ip() -> Result<GeoPoint> {
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(5))
.build()?;
// Primary: ip-api.com (free, 45 req/min)
let resp: serde_json::Value = client
.get("http://ip-api.com/json/?fields=lat,lon,city,regionName,country")
.send()
.await?
.json()
.await?;
let lat = resp.get("lat").and_then(|v| v.as_f64()).unwrap_or(0.0);
let lon = resp.get("lon").and_then(|v| v.as_f64()).unwrap_or(0.0);
if lat == 0.0 && lon == 0.0 {
anyhow::bail!("IP geolocation returned (0,0)");
}
Ok(GeoPoint { lat, lon, alt: 0.0 })
}
/// Get location with caching.
pub async fn get_location(cache_path: &str) -> Result<GeoPoint> {
// Check cache
if let Ok(data) = std::fs::read_to_string(cache_path) {
if let Ok(point) = serde_json::from_str::<GeoPoint>(&data) {
return Ok(point);
}
}
let point = locate_by_ip().await?;
let _ = std::fs::write(cache_path, serde_json::to_string(&point)?);
Ok(point)
}