Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IPinfo MMDB format #558

Merged
merged 6 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added method lookup to struct MmdbReader
  • Loading branch information
GyulyVGC committed Jul 13, 2024
commit 90cd18ee26fc2e90406e0db4c06aec2b4587dfad
10 changes: 2 additions & 8 deletions src/mmdb/asn.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use crate::mmdb::types::mmdb_asn_entry::MmdbAsnEntry;
use maxminddb::MaxMindDBError;

use crate::mmdb::types::mmdb_reader::MmdbReader;
use crate::networking::types::asn::Asn;

pub const ASN_MMDB: &[u8] = include_bytes!("../../resources/DB/GeoLite2-ASN.mmdb");

#[allow(clippy::module_name_repetitions)]
pub fn get_asn(address_to_lookup: &str, asn_db_reader: &MmdbReader) -> Asn {
let asn_result: Result<MmdbAsnEntry, MaxMindDBError> = match asn_db_reader {
MmdbReader::Default(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
MmdbReader::Custom(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
};
if let Ok(res) = asn_result {
pub fn get_asn(address: &str, asn_db_reader: &MmdbReader) -> Asn {
if let Ok(res) = asn_db_reader.lookup::<MmdbAsnEntry>(address.parse().unwrap()) {
return res.get_asn();
}
Asn::default()
Expand Down
10 changes: 2 additions & 8 deletions src/mmdb/country.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
use maxminddb::MaxMindDBError;

use crate::countries::types::country::Country;
use crate::mmdb::types::mmdb_country_entry::MmdbCountryEntry;
use crate::mmdb::types::mmdb_reader::MmdbReader;

pub const COUNTRY_MMDB: &[u8] = include_bytes!("../../resources/DB/GeoLite2-Country.mmdb");

#[allow(clippy::module_name_repetitions)]
pub fn get_country(address_to_lookup: &str, country_db_reader: &MmdbReader) -> Country {
let country_result: Result<MmdbCountryEntry, MaxMindDBError> = match country_db_reader {
MmdbReader::Default(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
MmdbReader::Custom(reader) => reader.lookup(address_to_lookup.parse().unwrap()),
};
if let Ok(res) = country_result {
pub fn get_country(address: &str, country_db_reader: &MmdbReader) -> Country {
if let Ok(res) = country_db_reader.lookup::<MmdbCountryEntry>(address.parse().unwrap()) {
return res.get_country();
}
Country::ZZ // unknown
Expand Down
11 changes: 10 additions & 1 deletion src/mmdb/types/mmdb_reader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use maxminddb::Reader;
use maxminddb::{MaxMindDBError, Reader};
use serde::Deserialize;
use std::net::IpAddr;

pub enum MmdbReader {
Default(Reader<&'static [u8]>),
Expand All @@ -18,4 +20,11 @@ impl MmdbReader {
MmdbReader::Default(default_reader)
}
}

pub fn lookup<'a, T: Deserialize<'a>>(&'a self, ip: IpAddr) -> Result<T, MaxMindDBError> {
match self {
MmdbReader::Default(reader) => reader.lookup(ip),
MmdbReader::Custom(reader) => reader.lookup(ip),
}
}
}