Merge pull request #1709 from nymtech/fix/explorerapi-geoip-lookup

fix(explorer-api): geoip lookup
This commit is contained in:
Tommy Verrall
2022-10-27 12:47:36 +02:00
committed by GitHub
3 changed files with 32 additions and 7 deletions
@@ -58,7 +58,10 @@ impl GeoLocateTask {
continue;
}
match geo_ip.query(&cache_item.mix_node().host) {
match geo_ip.query(
&cache_item.mix_node().host,
Some(cache_item.mix_node().mix_port),
) {
Ok(opt) => match opt {
Some(location) => {
let location = Location::new(location);
+27 -5
View File
@@ -4,9 +4,14 @@
use isocountry::CountryCode;
use log::warn;
use maxminddb::{geoip2::Country, MaxMindDBError, Reader};
use std::{net::IpAddr, str::FromStr, sync::Arc};
use std::{
net::{IpAddr, ToSocketAddrs},
str::FromStr,
sync::Arc,
};
const DEFAULT_DATABASE_PATH: &str = "./geo_ip/GeoLite2-Country.mmdb";
const FAKE_PORT: u16 = 1234;
#[derive(Debug)]
pub enum GeoIpError {
@@ -52,10 +57,27 @@ impl GeoIp {
GeoIp { db: reader }
}
pub fn query(&self, address: &str) -> Result<Option<Location>, GeoIpError> {
let ip: IpAddr = FromStr::from_str(address).map_err(|e| {
error!("Fail to create IpAddr from {}: {}", &address, e);
GeoIpError::NoValidIP
pub fn query(&self, address: &str, port: Option<u16>) -> Result<Option<Location>, GeoIpError> {
let ip: IpAddr = FromStr::from_str(address).or_else(|_| {
debug!(
"Fail to create IpAddr from {}. Trying using internal lookup...",
&address
);
let p = port.unwrap_or(FAKE_PORT);
let socket_addr = (address, p)
.to_socket_addrs()
.map_err(|e| {
error!("Fail to resolve IP address from {}:{}: {}", &address, p, e);
GeoIpError::NoValidIP
})?
.next()
.ok_or_else(|| {
error!("Fail to resolve IP address from {}:{}", &address, p);
GeoIpError::NoValidIP
})?;
let ip = socket_addr.ip();
debug!("Internal lookup succeed, resolved ip: {}", ip);
Ok(ip)
})?;
let result = self
.db
+1 -1
View File
@@ -39,7 +39,7 @@ fn find_location(request: &Request<'_>) -> Result<Location, (Status, LocationErr
let location = geo_ip
.0
.clone()
.query(&ip)
.query(&ip, None)
.map_err(|e| match e {
GeoIpError::NoValidIP => (Status::Forbidden, LocationError::NoIP),
GeoIpError::InternalError => {