use crate::domain::services::geo_location::{GeoLocation, GeoLocationService};
use crate::engines::client::reqwest::ReqwestEngine;
use crate::engines::engine_client::{EngineClient, ScrapeOptions, ScrapeRequest};
use crate::engines::router::{EngineRouter, EngineRouterTrait};
use crate::utils::http_client::DEFAULT_USER_AGENT;
use anyhow::Result;
use log::{debug, error, warn};
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::sync::Arc;
pub struct GeoLocationServiceImpl {
api_endpoint: String,
engine_client: Arc<EngineClient>,
}
impl GeoLocationServiceImpl {
pub fn new(client: Arc<reqwest::Client>) -> Self {
let engine_client = Self::create_engine_client(client);
Self {
api_endpoint: "https://ipapi.co".to_string(),
engine_client,
}
}
#[cfg(test)]
pub fn with_endpoint(api_endpoint: String, client: Arc<reqwest::Client>) -> Self {
let engine_client = Self::create_engine_client(client);
Self {
api_endpoint,
engine_client,
}
}
fn create_engine_client(http_client: Arc<reqwest::Client>) -> Arc<EngineClient> {
let reqwest_engine = ReqwestEngine::new(http_client);
let router: Arc<dyn EngineRouterTrait> =
Arc::new(EngineRouter::new(vec![Arc::new(reqwest_engine)]));
Arc::new(EngineClient::with_router(router))
}
}
#[async_trait::async_trait]
impl GeoLocationService for GeoLocationServiceImpl {
async fn get_location(&self, ip: &IpAddr) -> Result<GeoLocation> {
let ip_str = ip.to_string();
debug!("Getting geolocation for IP: {}", ip_str);
let url = format!("{}/{}/json/", self.api_endpoint, ip_str);
let mut headers = std::collections::HashMap::new();
headers.insert("User-Agent".to_string(), DEFAULT_USER_AGENT.to_string());
let request = ScrapeRequest::new(&url)
.with_options(ScrapeOptions::builder().headers(headers).build());
let response = self.engine_client.scrape(&request).await.map_err(|e| {
error!("Failed to fetch geolocation for IP {}: {}", ip_str, e);
anyhow::anyhow!("Failed to fetch geolocation: {}", e)
})?;
if !response.is_success() {
error!(
"Geolocation API returned error status: {} for IP {}",
response.status_code, ip_str
);
return Err(anyhow::anyhow!(
"Geolocation API error: {}",
response.status_code
));
}
let api_response: IpApiResponse = serde_json::from_str(&response.content).map_err(|e| {
error!(
"Failed to parse geolocation response for IP {}: {}",
ip_str, e
);
anyhow::anyhow!("Failed to parse geolocation response: {}", e)
})?;
let geo_location = GeoLocation {
ip: ip_str.clone(),
country_code: api_response.country_code.unwrap_or_else(|| {
warn!("No country code found for IP: {}", ip_str);
"UNKNOWN".to_string()
}),
country_name: api_response.country_name.unwrap_or_default(),
region: api_response.region,
city: api_response.city,
latitude: api_response.latitude,
longitude: api_response.longitude,
isp: api_response.org.clone(),
org: api_response.org,
};
debug!(
"Successfully retrieved geolocation for IP {}: {:?}",
ip_str, geo_location
);
Ok(geo_location)
}
}
#[derive(Debug, Serialize, Deserialize)]
struct IpApiResponse {
ip: Option<String>,
#[serde(rename = "country_code")]
country_code: Option<String>,
#[serde(rename = "country_name")]
country_name: Option<String>,
region: Option<String>,
city: Option<String>,
latitude: Option<f64>,
longitude: Option<f64>,
org: Option<String>,
error: Option<bool>,
reason: Option<String>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_geo_location_default_fields() {
let geo = GeoLocation::default();
assert_eq!(geo.ip, "");
assert_eq!(geo.country_code, "UNKNOWN");
assert_eq!(geo.country_name, "Unknown");
assert!(geo.region.is_none());
assert!(geo.city.is_none());
assert!(geo.latitude.is_none());
assert!(geo.longitude.is_none());
assert!(geo.isp.is_none());
assert!(geo.org.is_none());
}
#[test]
fn test_geo_location_service_impl_new_default_endpoint() {
let client = Arc::new(reqwest::Client::new());
let svc = GeoLocationServiceImpl::new(client);
assert_eq!(svc.api_endpoint, "https://ipapi.co");
}
#[test]
fn test_ip_api_response_deserialize_full() {
let json = r#"{
"ip": "8.8.8.8",
"country_code": "US",
"country_name": "United States",
"region": "California",
"city": "Mountain View",
"latitude": 37.4056,
"longitude": -122.0775,
"org": "Google LLC",
"error": false,
"reason": null
}"#;
let resp: IpApiResponse = serde_json::from_str(json).expect("deserialize failed");
assert_eq!(resp.ip.as_deref(), Some("8.8.8.8"));
assert_eq!(resp.country_code.as_deref(), Some("US"));
assert_eq!(resp.country_name.as_deref(), Some("United States"));
assert_eq!(resp.region.as_deref(), Some("California"));
assert_eq!(resp.city.as_deref(), Some("Mountain View"));
assert!((resp.latitude.unwrap() - 37.4056).abs() < 1e-4);
assert!((resp.longitude.unwrap() - (-122.0775)).abs() < 1e-4);
assert_eq!(resp.org.as_deref(), Some("Google LLC"));
assert_eq!(resp.error, Some(false));
assert!(resp.reason.is_none());
}
#[test]
fn test_ip_api_response_deserialize_partial() {
let json = r#"{"ip": "1.2.3.4"}"#;
let resp: IpApiResponse = serde_json::from_str(json).expect("deserialize failed");
assert_eq!(resp.ip.as_deref(), Some("1.2.3.4"));
assert!(resp.country_code.is_none());
assert!(resp.country_name.is_none());
assert!(resp.region.is_none());
assert!(resp.city.is_none());
assert!(resp.latitude.is_none());
assert!(resp.longitude.is_none());
assert!(resp.org.is_none());
assert!(resp.error.is_none());
assert!(resp.reason.is_none());
}
#[test]
fn test_ip_api_response_deserialize_error_response() {
let json = r#"{
"ip": "invalid",
"error": true,
"reason": "Invalid IP address"
}"#;
let resp: IpApiResponse = serde_json::from_str(json).expect("deserialize failed");
assert_eq!(resp.error, Some(true));
assert_eq!(resp.reason.as_deref(), Some("Invalid IP address"));
}
#[test]
fn test_ip_api_response_serialize_roundtrip() {
let original = IpApiResponse {
ip: Some("8.8.4.4".to_string()),
country_code: Some("US".to_string()),
country_name: Some("United States".to_string()),
region: None,
city: Some("Mountain View".to_string()),
latitude: Some(37.4),
longitude: Some(-122.1),
org: Some("Google".to_string()),
error: Some(false),
reason: None,
};
let json = serde_json::to_string(&original).expect("serialize failed");
let decoded: IpApiResponse = serde_json::from_str(&json).expect("deserialize failed");
assert_eq!(decoded.ip, original.ip);
assert_eq!(decoded.country_code, original.country_code);
assert_eq!(decoded.country_name, original.country_name);
assert_eq!(decoded.city, original.city);
assert!((decoded.latitude.unwrap() - 37.4).abs() < 1e-6);
assert_eq!(decoded.org, original.org);
assert_eq!(decoded.error, original.error);
}
#[test]
fn test_ip_api_response_deserialize_empty_json() {
let json = r#"{}"#;
let resp: IpApiResponse = serde_json::from_str(json).expect("deserialize failed");
assert!(resp.ip.is_none());
assert!(resp.country_code.is_none());
assert!(resp.country_name.is_none());
assert!(resp.region.is_none());
assert!(resp.city.is_none());
assert!(resp.latitude.is_none());
assert!(resp.longitude.is_none());
assert!(resp.org.is_none());
assert!(resp.error.is_none());
assert!(resp.reason.is_none());
}
#[test]
fn test_ip_api_response_deserialize_with_error_true() {
let json = r#"{
"error": true,
"reason": "Rate limited"
}"#;
let resp: IpApiResponse = serde_json::from_str(json).expect("deserialize failed");
assert_eq!(resp.error, Some(true));
assert_eq!(resp.reason.as_deref(), Some("Rate limited"));
}
#[test]
fn test_ip_api_response_serialize_empty_struct() {
let resp = IpApiResponse {
ip: None,
country_code: None,
country_name: None,
region: None,
city: None,
latitude: None,
longitude: None,
org: None,
error: None,
reason: None,
};
let json = serde_json::to_string(&resp).expect("serialize failed");
let decoded: IpApiResponse = serde_json::from_str(&json).expect("deserialize failed");
assert!(decoded.ip.is_none());
assert!(decoded.error.is_none());
}
#[test]
fn test_geo_location_service_impl_new_creates_engine_client() {
let client = Arc::new(reqwest::Client::new());
let svc = GeoLocationServiceImpl::new(client);
assert_eq!(svc.api_endpoint, "https://ipapi.co");
}
#[test]
fn test_geo_location_clone_preserves_fields() {
let geo = GeoLocation {
ip: "8.8.8.8".to_string(),
country_code: "US".to_string(),
country_name: "United States".to_string(),
region: Some("CA".to_string()),
city: Some("Mountain View".to_string()),
latitude: Some(37.4),
longitude: Some(-122.1),
isp: Some("Google".to_string()),
org: Some("Google LLC".to_string()),
};
let cloned = geo.clone();
assert_eq!(cloned.ip, geo.ip);
assert_eq!(cloned.country_code, geo.country_code);
assert_eq!(cloned.country_name, geo.country_name);
assert_eq!(cloned.region, geo.region);
assert_eq!(cloned.city, geo.city);
assert_eq!(cloned.latitude, geo.latitude);
assert_eq!(cloned.longitude, geo.longitude);
assert_eq!(cloned.isp, geo.isp);
assert_eq!(cloned.org, geo.org);
}
#[test]
fn test_geo_location_serialize_deserialize_roundtrip() {
let geo = GeoLocation {
ip: "1.2.3.4".to_string(),
country_code: "US".to_string(),
country_name: "United States".to_string(),
region: Some("CA".to_string()),
city: None,
latitude: Some(37.4),
longitude: None,
isp: Some("ISP".to_string()),
org: None,
};
let json = serde_json::to_string(&geo).expect("serialize failed");
let decoded: GeoLocation = serde_json::from_str(&json).expect("deserialize failed");
assert_eq!(decoded.ip, geo.ip);
assert_eq!(decoded.country_code, geo.country_code);
assert_eq!(decoded.region, geo.region);
assert_eq!(decoded.city, geo.city);
assert_eq!(decoded.latitude, geo.latitude);
assert_eq!(decoded.isp, geo.isp);
}
#[tokio::test]
async fn test_get_location_ssrf_blocked_returns_scrape_error() {
let client = Arc::new(reqwest::Client::new());
let svc = GeoLocationServiceImpl::with_endpoint("http://127.0.0.1:1".to_string(), client);
let ip: IpAddr = "8.8.8.8".parse().unwrap();
let result = svc.get_location(&ip).await;
assert!(result.is_err(), "should fail with SSRF error");
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("Failed to fetch geolocation"),
"error should be wrapped scrape failure, got: {}",
err_msg
);
}
#[tokio::test]
async fn test_get_location_unreachable_dns_returns_scrape_error() {
let client = Arc::new(reqwest::Client::new());
let svc =
GeoLocationServiceImpl::with_endpoint("http://example.invalid".to_string(), client);
let ip: IpAddr = "8.8.8.8".parse().unwrap();
let result = svc.get_location(&ip).await;
assert!(result.is_err(), "should fail with DNS error");
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("Failed to fetch geolocation"),
"error should be wrapped scrape failure, got: {}",
err_msg
);
}
}