Skip to content

Commit

Permalink
keep list of ips in memory before deciding what to do..
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyv1987 committed Oct 25, 2023
1 parent 89746e7 commit 8139fcf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
30 changes: 26 additions & 4 deletions explorer-api/src/geo_ip/location.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright 2022 - Nym Technologies SA <[email protected]>
// SPDX-License-Identifier: Apache-2.0

use crate::helpers::{append_ip_to_file, ip_exists_in_file};
use crate::helpers::append_ip_to_file;
use isocountry::CountryCode;
use log::warn;
use maxminddb::{geoip2::City, MaxMindDBError, Reader};
use std::collections::HashSet;
use std::sync::Mutex;
use std::{
net::{IpAddr, ToSocketAddrs},
str::FromStr,
Expand All @@ -27,6 +29,7 @@ pub enum GeoIpError {
// and an error will be logged.
pub(crate) struct GeoIp {
pub(crate) db: Option<Reader<Vec<u8>>>,
failed_addresses: FailedIpAddresses,
}

#[derive(Clone)]
Expand All @@ -43,6 +46,17 @@ pub(crate) struct Location {
pub(crate) longitude: Option<f64>,
}

pub(crate) struct FailedIpAddresses {
failed_ips: Arc<Mutex<HashSet<String>>>,
}

impl FailedIpAddresses {
pub fn new() -> Self {
FailedIpAddresses {
failed_ips: Arc::new(Mutex::new(HashSet::new())),
}
}
}
impl From<Location> for nym_explorer_api_requests::Location {
fn from(location: Location) -> Self {
nym_explorer_api_requests::Location {
Expand All @@ -69,7 +83,13 @@ impl GeoIp {
error!("Fail to open GeoLite2 database file {}: {}", db_path, e);
})
.ok();
GeoIp { db: reader }

let failed_addresses = FailedIpAddresses::new();

GeoIp {
db: reader,
failed_addresses,
}
}

pub fn query(&self, address: &str, port: Option<u16>) -> Result<Option<Location>, GeoIpError> {
Expand All @@ -87,15 +107,17 @@ impl GeoIp {
Ok(ip)
} else {
debug!("Fail to resolve IP address from {}:{}", &address, p);
if !ip_exists_in_file(address) {
let mut failed_ips_guard = self.failed_addresses.failed_ips.lock().unwrap();
if failed_ips_guard.insert(address.to_string()) {
append_ip_to_file(address);
}
Err(GeoIpError::NoValidIP)
}
}
Err(_) => {
debug!("Fail to resolve IP address from {}:{}.", &address, p);
if !ip_exists_in_file(address) {
let mut failed_ips_guard = self.failed_addresses.failed_ips.lock().unwrap();
if failed_ips_guard.insert(address.to_string()) {
append_ip_to_file(address);
}
Err(GeoIpError::NoValidIP)
Expand Down
8 changes: 0 additions & 8 deletions explorer-api/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

use nym_mixnet_contract_common::{Decimal, Fraction};
use std::env;
use std::fs;
use std::fs::OpenOptions;
use std::io::Write;

Expand All @@ -18,13 +17,6 @@ pub fn failed_ips_filepath() -> String {
format!("{}/failed_ips.txt", home_dir)
}

pub fn ip_exists_in_file(address: &str) -> bool {
if let Ok(content) = fs::read_to_string(failed_ips_filepath()) {
return content.contains(address);
}
false
}

pub fn append_ip_to_file(address: &str) {
if let Ok(mut file) = OpenOptions::new()
.append(true)
Expand Down

0 comments on commit 8139fcf

Please sign in to comment.