- validate entries don't spam the logs
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Copyright 2022 - Nym Technologies SA <contact@nymtech.net>
|
||||
// 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};
|
||||
@@ -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
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user