use gossan_core::{Config, DiscoverySource, DomainTarget, Target};
use crate::sources::{SubdomainSource, SourceRate};
use async_trait::async_trait;
use governor::DefaultDirectRateLimiter;
pub struct Alienvault;
#[async_trait]
impl SubdomainSource for Alienvault {
fn name(&self) -> &'static str { "alienvault" }
fn requires_api_key(&self) -> bool { false }
fn api_key_name(&self) -> &'static str { "" }
fn rate_limit(&self) -> SourceRate { SourceRate::per_second(1) }
fn discovery_source(&self) -> DiscoverySource { DiscoverySource::PassiveDns }
async fn query(
&self,
domain: &str,
config: &Config,
client: &reqwest::Client,
limiter: &DefaultDirectRateLimiter,
) -> anyhow::Result<Vec<Target>> {
let url = format!("https://otx.alienvault.com/api/v1/indicators/domain/{}/passive_dns", domain);
limiter.until_ready().await;
let resp = client.get(&url).send().await?.error_for_status()?;
let max_size = config.max_response_size;
let bytes = gossan_core::read_response_limited(resp, max_size).await?;
let mut seen = std::collections::HashSet::new();
let domain_lower = domain.to_lowercase();
let json: serde_json::Value = serde_json::from_slice(&bytes).map_err(|e| {
anyhow::anyhow!("alienvault passive_dns JSON parse failed for {domain}: {e}")
})?;
if let Some(arr) = json.get("passive_dns").and_then(|v| v.as_array()) {
for item in arr {
if let Some(v) = item.get("hostname").and_then(|v| v.as_str()) {
let candidate = v.trim().trim_start_matches("*.").to_lowercase();
if !candidate.contains('*') && crate::is_subdomain_of(&candidate, &domain_lower) {
seen.insert(candidate);
}
}
}
}
Ok(seen.into_iter().map(|d| Target::Domain(DomainTarget {
domain: d,
source: DiscoverySource::PassiveDns,
})).collect())
}
}