mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-16 22:13:30 +02:00
Working geoip lookup
This commit is contained in:
50
src/geoip.rs
50
src/geoip.rs
@ -7,20 +7,11 @@ use maxminddb;
|
||||
use maxminddb::geoip2;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use std::net::IpAddr;
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
/* Datatypes */
|
||||
|
||||
// TODO
|
||||
pub enum IsTheNameOf {
|
||||
Continent,
|
||||
Country,
|
||||
Subdivision,
|
||||
City,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Default)]
|
||||
pub struct NamedLocation {
|
||||
iso_code: Option<String>,
|
||||
@ -53,15 +44,16 @@ pub struct AsnResult {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
struct MMDBCarrier {
|
||||
mmdb: Option<maxminddb::Reader<Vec<u8>>>,
|
||||
pub struct MMDBCarrier {
|
||||
pub mmdb: Option<maxminddb::Reader<Vec<u8>>>,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
trait QueryLocation {
|
||||
pub trait QueryLocation {
|
||||
fn query_location_for_ip(&self, address: IpAddr, laguages: &Vec<String>) -> Option<LocationResult>;
|
||||
}
|
||||
|
||||
trait QueryAsn {
|
||||
pub trait QueryAsn {
|
||||
fn query_asn_for_ip(&self, address: IpAddr) -> Option<AsnResult>;
|
||||
}
|
||||
|
||||
@ -128,7 +120,7 @@ pub fn geoip2_subdivision_to_named_location(item: geoip2::model::Subdivision, la
|
||||
|
||||
impl QueryAsn for MMDBCarrier {
|
||||
fn query_asn_for_ip(&self, address: IpAddr) -> Option<AsnResult> {
|
||||
match self.mmdb {
|
||||
match &self.mmdb {
|
||||
Some(mmdb) => {
|
||||
match mmdb.lookup::<geoip2::Asn>(address) {
|
||||
Ok(res) => {
|
||||
@ -150,7 +142,7 @@ impl QueryAsn for MMDBCarrier {
|
||||
|
||||
impl QueryLocation for MMDBCarrier {
|
||||
fn query_location_for_ip(&self, address: IpAddr, languages: &Vec<String>) -> Option<LocationResult> {
|
||||
match self.mmdb {
|
||||
match &self.mmdb {
|
||||
Some(mmdb) => {
|
||||
match mmdb.lookup::<geoip2::City>(address) {
|
||||
Ok(res) => {
|
||||
@ -168,7 +160,7 @@ impl QueryLocation for MMDBCarrier {
|
||||
|
||||
subdivisions: match res.subdivisions {
|
||||
Some(sds) => {
|
||||
let subdivisions = Vec::new();
|
||||
let mut subdivisions = Vec::new();
|
||||
subdivisions.reserve_exact(sds.len());
|
||||
for sd in sds {
|
||||
subdivisions.push(geoip2_subdivision_to_named_location(sd, languages));
|
||||
@ -198,3 +190,27 @@ impl QueryLocation for MMDBCarrier {
|
||||
}
|
||||
}
|
||||
|
||||
impl MMDBCarrier {
|
||||
pub fn load_database_from_path(&mut self, path: &Path) -> Result<(),maxminddb::MaxMindDBError> {
|
||||
println!("Loading {} from '{}' ...", &self.name, path.display());
|
||||
match maxminddb::Reader::open_readfile(path) {
|
||||
Ok(reader) => {
|
||||
let wording = if self.mmdb.is_some() {
|
||||
"Replaced old"
|
||||
} else {
|
||||
"Loaded new"
|
||||
};
|
||||
self.mmdb = Some(reader);
|
||||
println!("{} {} with new one.", wording, &self.name);
|
||||
Ok(())
|
||||
},
|
||||
Err(e) => {
|
||||
println!("Error while reading {}: {}", &self.name, &e);
|
||||
if self.mmdb.is_some() {
|
||||
println!("Not replacing old database.");
|
||||
}
|
||||
Err(e)
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user