From 8139fcfe74e6ac458d531719b8bd11a0181b617d Mon Sep 17 00:00:00 2001 From: Tommy Verrall Date: Wed, 25 Oct 2023 16:44:03 +0200 Subject: [PATCH] keep list of ips in memory before deciding what to do.. --- explorer-api/src/geo_ip/location.rs | 30 +++++++++++++++++++++++++---- explorer-api/src/helpers.rs | 8 -------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/explorer-api/src/geo_ip/location.rs b/explorer-api/src/geo_ip/location.rs index 4896cb3eee..272200f6d2 100644 --- a/explorer-api/src/geo_ip/location.rs +++ b/explorer-api/src/geo_ip/location.rs @@ -1,10 +1,12 @@ // Copyright 2022 - Nym Technologies SA // 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, @@ -27,6 +29,7 @@ pub enum GeoIpError { // and an error will be logged. pub(crate) struct GeoIp { pub(crate) db: Option>>, + failed_addresses: FailedIpAddresses, } #[derive(Clone)] @@ -43,6 +46,17 @@ pub(crate) struct Location { pub(crate) longitude: Option, } +pub(crate) struct FailedIpAddresses { + failed_ips: Arc>>, +} + +impl FailedIpAddresses { + pub fn new() -> Self { + FailedIpAddresses { + failed_ips: Arc::new(Mutex::new(HashSet::new())), + } + } +} impl From for nym_explorer_api_requests::Location { fn from(location: Location) -> Self { nym_explorer_api_requests::Location { @@ -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) -> Result, GeoIpError> { @@ -87,7 +107,8 @@ 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) @@ -95,7 +116,8 @@ impl GeoIp { } 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) diff --git a/explorer-api/src/helpers.rs b/explorer-api/src/helpers.rs index 93477369c1..dbe4a219e7 100644 --- a/explorer-api/src/helpers.rs +++ b/explorer-api/src/helpers.rs @@ -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; @@ -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)