-
-
Notifications
You must be signed in to change notification settings - Fork 577
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
68 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use maxminddb::{geoip2, MaxMindDBError}; | ||
|
||
use crate::countries::types::country::Country; | ||
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<geoip2::Country, 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(res1) = country_result { | ||
if let Some(res2) = res1.country { | ||
if let Some(res3) = res2.iso_code { | ||
return Country::from_str(res3); | ||
} | ||
} | ||
} | ||
Country::ZZ // unknown | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod asn; | ||
pub mod country; | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use maxminddb::Reader; | ||
|
||
pub enum MmdbReader { | ||
Default(Reader<&'static [u8]>), | ||
Custom(Reader<Vec<u8>>), | ||
} | ||
|
||
impl MmdbReader { | ||
pub fn from(mmdb_path: &String, default_mmdb: &'static [u8]) -> MmdbReader { | ||
let default_reader = maxminddb::Reader::from_source(default_mmdb).unwrap(); | ||
if mmdb_path.is_empty() { | ||
MmdbReader::Default(default_reader) | ||
} else { | ||
let custom_reader_result = maxminddb::Reader::open_readfile(mmdb_path); | ||
if let Ok(custom_reader) = custom_reader_result { | ||
return MmdbReader::Custom(custom_reader); | ||
} | ||
MmdbReader::Default(default_reader) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod mmdb_reader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
pub mod asn; | ||
pub mod formatted_strings; | ||
pub mod types; |