fix support for IPinfo's databases

This commit is contained in:
GyulyVGC
2025-08-13 23:10:35 +02:00
parent e50266e57f
commit 2a0103c05c
9 changed files with 43 additions and 44 deletions

View File

@@ -12,6 +12,7 @@ ## [UNRELEASED]
- Simplified Chinese ([#838](https://github.com/GyulyVGC/sniffnet/pull/838))
- Japanese ([#849](https://github.com/GyulyVGC/sniffnet/pull/849))
- French ([#864](https://github.com/GyulyVGC/sniffnet/pull/864))
- Fix support for IPinfo's databases (the most recent version renamed the `country` field to `country_code`)
## [1.4.0] - 2025-06-27
- Import PCAP files ([#795](https://github.com/GyulyVGC/sniffnet/pull/795) — fixes [#283](https://github.com/GyulyVGC/sniffnet/issues/283))

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -77,19 +77,19 @@ fn test_get_asn_with_custom_ipinfo_single_reader() {
assert!(matches!(reader, MmdbReader::Custom(_)));
// known IP
let res = get_asn(&IpAddr::from([61, 8, 0, 0]), &reader);
assert_eq!(res.code, "AS1221");
assert_eq!(res.name, "Telstra Limited");
let res = get_asn(&IpAddr::from([185, 72, 2, 28]), &reader);
assert_eq!(res.code, "AS202583");
assert_eq!(res.name, "AVATEL TELECOM, SA");
// another known IP
let res = get_asn(&IpAddr::from([206, 180, 34, 99]), &reader);
assert_eq!(res.code, "AS63344");
assert_eq!(res.name, "The Reynolds and Reynolds Company");
let res = get_asn(&IpAddr::from([89, 187, 198, 0]), &reader);
assert_eq!(res.code, "AS210367");
assert_eq!(res.name, "Krajska zdravotni, a.s.");
// known IPv6
let res = get_asn(&IpAddr::from_str("2806:230:2057::").unwrap(), &reader);
assert_eq!(res.code, "AS11888");
assert_eq!(res.name, "Television Internacional, S.A. de C.V.");
let res = get_asn(&IpAddr::from_str("2408:8957:6280::").unwrap(), &reader);
assert_eq!(res.code, "AS17622");
assert_eq!(res.name, "China Unicom Guangzhou network");
// unknown IP
let res = get_asn(&IpAddr::from([127, 0, 0, 1]), &reader);
@@ -106,31 +106,29 @@ fn test_get_asn_with_custom_ipinfo_single_reader() {
#[test]
fn test_get_asn_with_custom_ipinfo_combined_reader() {
let reader_1 = MmdbReader::from(
&String::from("resources/test/ipinfo_country_asn_sample.mmdb"),
&String::from("resources/test/ipinfo_lite_sample.mmdb"),
ASN_MMDB,
);
let reader_2 = MmdbReader::from(
&String::from("resources/test/ipinfo_country_asn_sample.mmdb"),
&[],
);
let reader_2 =
MmdbReader::from(&String::from("resources/test/ipinfo_lite_sample.mmdb"), &[]);
for reader in vec![reader_1, reader_2] {
assert!(matches!(reader, MmdbReader::Custom(_)));
// known IP
let res = get_asn(&IpAddr::from([31, 171, 144, 141]), &reader);
assert_eq!(res.code, "AS197742");
assert_eq!(res.name, "IBB Energie AG");
let res = get_asn(&IpAddr::from([1, 0, 65, 1]), &reader);
assert_eq!(res.code, "AS18144");
assert_eq!(res.name, "Enecom,Inc.");
// another known IP
let res = get_asn(&IpAddr::from([103, 112, 220, 111]), &reader);
assert_eq!(res.code, "AS134077");
assert_eq!(res.name, "Magik Pivot Company Limited");
let res = get_asn(&IpAddr::from([1, 6, 230, 0]), &reader);
assert_eq!(res.code, "AS4755");
assert_eq!(res.name, "TATA Communications formerly VSNL is Leading ISP");
// known IPv6
let res = get_asn(&IpAddr::from_str("2a02:6ea0:f001::").unwrap(), &reader);
assert_eq!(res.code, "AS60068");
assert_eq!(res.name, "Datacamp Limited");
// let res = get_asn(&IpAddr::from_str("2a02:6ea0:f001::").unwrap(), &reader);
// assert_eq!(res.code, "AS60068");
// assert_eq!(res.name, "Datacamp Limited");
// unknown IP
let res = get_asn(&IpAddr::from([127, 0, 0, 1]), &reader);

View File

@@ -63,11 +63,11 @@ fn test_get_country_with_default_reader() {
#[test]
fn test_get_country_with_custom_ipinfo_single_reader() {
let reader_1 = MmdbReader::from(
&String::from("resources/test/ipinfo_country_sample.mmdb"),
&String::from("resources/test/ipinfo_location_sample.mmdb"),
COUNTRY_MMDB,
);
let reader_2 = MmdbReader::from(
&String::from("resources/test/ipinfo_country_sample.mmdb"),
&String::from("resources/test/ipinfo_location_sample.mmdb"),
&[],
);
@@ -75,16 +75,16 @@ fn test_get_country_with_custom_ipinfo_single_reader() {
assert!(matches!(reader, MmdbReader::Custom(_)));
// known IP
let res = get_country(&IpAddr::from([2, 2, 146, 0]), &reader);
assert_eq!(res, Country::GB);
let res = get_country(&IpAddr::from([1, 0, 6, 99]), &reader);
assert_eq!(res, Country::AU);
// another known IP
let res = get_country(&IpAddr::from([23, 193, 112, 81]), &reader);
assert_eq!(res, Country::US);
let res = get_country(&IpAddr::from([1, 0, 8, 0]), &reader);
assert_eq!(res, Country::CN);
// known IPv6
let res = get_country(&IpAddr::from_str("2a0e:1d80::").unwrap(), &reader);
assert_eq!(res, Country::RO);
// let res = get_country(&IpAddr::from_str("2a0e:1d80::").unwrap(), &reader);
// assert_eq!(res, Country::RO);
// unknown IP
let res = get_country(&IpAddr::from([127, 0, 0, 1]), &reader);
@@ -99,28 +99,26 @@ fn test_get_country_with_custom_ipinfo_single_reader() {
#[test]
fn test_get_country_with_custom_ipinfo_combined_reader() {
let reader_1 = MmdbReader::from(
&String::from("resources/test/ipinfo_country_asn_sample.mmdb"),
&String::from("resources/test/ipinfo_lite_sample.mmdb"),
COUNTRY_MMDB,
);
let reader_2 = MmdbReader::from(
&String::from("resources/test/ipinfo_country_asn_sample.mmdb"),
&[],
);
let reader_2 =
MmdbReader::from(&String::from("resources/test/ipinfo_lite_sample.mmdb"), &[]);
for reader in vec![reader_1, reader_2] {
assert!(matches!(reader, MmdbReader::Custom(_)));
// known IP
let res = get_country(&IpAddr::from([31, 171, 144, 141]), &reader);
assert_eq!(res, Country::IT);
let res = get_country(&IpAddr::from([1, 0, 65, 1]), &reader);
assert_eq!(res, Country::JP);
// another known IP
let res = get_country(&IpAddr::from([103, 112, 220, 111]), &reader);
assert_eq!(res, Country::TH);
let res = get_country(&IpAddr::from([1, 6, 230, 0]), &reader);
assert_eq!(res, Country::IN);
// known IPv6
let res = get_country(&IpAddr::from_str("2a02:6ea0:f001::").unwrap(), &reader);
assert_eq!(res, Country::AR);
// let res = get_country(&IpAddr::from_str("2a02:6ea0:f001::").unwrap(), &reader);
// assert_eq!(res, Country::AR);
// unknown IP
let res = get_country(&IpAddr::from([127, 0, 0, 1]), &reader);

View File

@@ -30,7 +30,9 @@ fn get_country(&self) -> Country {
Self::Standard(StandardCountryEntry {
country: Some(StandardCountryEntryInner { iso_code: Some(c) }),
})
| Self::Ipinfo(IpinfoCountryEntry { country: Some(c) }) => Country::from_str(c),
| Self::Ipinfo(IpinfoCountryEntry {
country_code: Some(c),
}) => Country::from_str(c),
_ => Country::ZZ,
}
}
@@ -49,5 +51,5 @@ struct StandardCountryEntryInner<'a> {
#[derive(Deserialize)]
struct IpinfoCountryEntry<'a> {
country: Option<&'a str>,
country_code: Option<&'a str>,
}