use std::borrow::Cow;
use std::net::IpAddr;
use std::str::FromStr;
use std::sync::LazyLock;
use ipnet::IpNet;
use crate::bogons::{BOGON_V4, BOGON_V6};
use crate::lookup::Lookup;
use crate::models::{AbuseContact, Asn, Company, Location, LookupResponse};
pub fn is_bogon(ip: &str) -> bool {
let Ok(addr) = IpAddr::from_str(ip) else {
return false;
};
let addr = addr.to_canonical();
let table: &[IpNet] = if addr.is_ipv4() { &PREFIXES_V4 } else { &PREFIXES_V6 };
table.iter().any(|net| net.contains(&addr))
}
pub(crate) fn unmapped(ip: &str) -> Cow<'_, str> {
match IpAddr::from_str(ip) {
Ok(IpAddr::V6(v6)) => match v6.to_ipv4_mapped() {
Some(v4) => Cow::Owned(v4.to_string()),
None => Cow::Borrowed(ip),
},
_ => Cow::Borrowed(ip),
}
}
pub(crate) fn bogon_lookup(ip: &str) -> Lookup {
let empty = || Some(String::new());
Lookup {
answer: LookupResponse {
ip: ip.to_owned(),
is_bogon: true,
is_vpn: Some(false),
is_tor: false,
is_relay: false,
is_resproxy: Some(false),
is_dcproxy: Some(false),
is_mobproxy: Some(false),
is_pubproxy: Some(false),
is_hosting: Some(false),
is_cdn: Some(false),
is_anycast: false,
is_carrier: false,
is_satellite: false,
is_ssh: false,
is_smtp: false,
is_web: false,
is_dns_resolver: false,
is_bittorrent: false,
vpn: Some(Box::default()),
tor: Box::default(),
relay: Box::default(),
resproxy: Some(Box::default()),
dcproxy: Some(Box::default()),
mobproxy: Some(Box::default()),
pubproxy: Some(Box::default()),
hosting: Some(Box::default()),
cdn: Some(Box::default()),
carrier: Some(Box::default()),
satellite: Some(Box::default()),
ssh: Some(Box::default()),
smtp: Some(Box::default()),
web: Some(Vec::new()),
dns_resolver: Some(Box::default()),
bittorrent: Some(Box::default()),
location: Box::new(Location {
region: empty(),
region_code: empty(),
city: empty(),
postal_code: empty(),
latitude: Some(0.0),
longitude: Some(0.0),
accuracy_radius: Some(0),
geoname_id: empty(),
dma_code: empty(),
last_changed: empty(),
..Location::default()
}),
asn: Box::new(Asn { country: empty(), rpki_status: empty(), ..Asn::default() }),
company: Box::new(Company { r#type: empty(), country: empty(), ..Company::default() }),
hostname: String::new(),
abuse: Box::new(AbuseContact {
name: empty(),
phone: empty(),
address: empty(),
country: empty(),
network: empty(),
validated: Some(false),
unvalidated_since: empty(),
..AbuseContact::default()
}),
whois: Some(Box::default()),
rwhois: Some(Box::default()),
hosted_domains: Some(Box::default()),
trial_expired: None,
},
}
}
static PREFIXES_V4: LazyLock<Vec<IpNet>> = LazyLock::new(|| parse_prefixes(BOGON_V4));
static PREFIXES_V6: LazyLock<Vec<IpNet>> = LazyLock::new(|| parse_prefixes(BOGON_V6));
fn parse_prefixes(cidrs: &[&str]) -> Vec<IpNet> {
cidrs
.iter()
.map(|cidr| {
IpNet::from_str(cidr)
.unwrap_or_else(|e| panic!("internetdata: unparseable bogon range {cidr:?}: {e}"))
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_table_matches_the_corpus() {
#[derive(serde::Deserialize)]
struct Tables {
v4: Vec<String>,
v6: Vec<String>,
}
let corpus: Tables =
serde_json::from_str(include_str!("../testdata/bogons.json")).expect("bogons.json");
assert_eq!(BOGON_V4, corpus.v4);
assert_eq!(BOGON_V6, corpus.v6);
}
#[test]
fn every_generated_range_parses() {
assert_eq!(PREFIXES_V4.len(), BOGON_V4.len());
assert_eq!(PREFIXES_V6.len(), BOGON_V6.len());
}
}