Skip to content

Commit

Permalink
- validate entries don't spam the logs
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyv1987 committed Oct 25, 2023
1 parent 700cd16 commit 89746e7
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
37 changes: 23 additions & 14 deletions explorer-api/src/geo_ip/location.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 isocountry::CountryCode;
use log::warn;
use maxminddb::{geoip2::City, MaxMindDBError, Reader};
Expand Down Expand Up @@ -78,20 +79,28 @@ impl GeoIp {
&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(|| {
debug!("Fail to resolve IP address from {}:{}", &address, p);
GeoIpError::NoValidIP
})?;
let ip = socket_addr.ip();
debug!("Internal lookup succeed, resolved ip: {}", ip);
Ok(ip)
match (address, p).to_socket_addrs() {
Ok(mut addrs) => {
if let Some(socket_addr) = addrs.next() {
let ip = socket_addr.ip();
debug!("Internal lookup succeeded, resolved ip: {}", ip);
Ok(ip)
} else {
debug!("Fail to resolve IP address from {}:{}", &address, p);
if !ip_exists_in_file(address) {
append_ip_to_file(address);
}
Err(GeoIpError::NoValidIP)
}
}
Err(_) => {
debug!("Fail to resolve IP address from {}:{}.", &address, p);
if !ip_exists_in_file(address) {
append_ip_to_file(address);
}
Err(GeoIpError::NoValidIP)
}
}
})?;
let result = self
.db
Expand Down
26 changes: 26 additions & 0 deletions explorer-api/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@
// SPDX-License-Identifier: Apache-2.0

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

pub(crate) fn best_effort_small_dec_to_f64(dec: Decimal) -> f64 {
let num = dec.numerator().u128() as f64;
let den = dec.denominator().u128() as f64;
num / den
}

pub fn failed_ips_filepath() -> String {
let home_dir = env::var("HOME").unwrap_or_else(|_| "/tmp".to_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)
.create(true)
.open(failed_ips_filepath())
{
writeln!(file, "{}", address).expect("Failed to write to file");
}
}

0 comments on commit 89746e7

Please sign in to comment.