use reqwest::{self};
use serde_json::{Value};
use std::{error::Error, env};
use crate::string_utils;
pub async fn check_address_for_abuse(remote_address: &String, verbose: bool) -> Result<Option<i64>, Box<dyn Error>> {
let abuseipdb_api_key: String = match env::var("ABUSEIPDB_API_KEY") {
Ok(val) => val,
Err(_e) => {
if verbose {
string_utils::pretty_print_warning(
"Couldn't find AbuseIPDB API key. If you want to use this feature make sure to put the API key into the environment variable `ABUSEIPDB_API_KEY`.*"
);
}
return Ok(None);
},
};
let client = reqwest::Client::new();
let url = "https://api.abuseipdb.com/api/v2/check";
let params = [
("ipAddress", remote_address),
("maxAgeInDays", &("40".to_string())),
];
let response = client
.get(url)
.header("Key", abuseipdb_api_key)
.header("Accept", "application/json")
.query(¶ms)
.send()
.await?;
if response.status().is_success() {
let json_response: Value = response.json().await?;
let abuse_confidence_score: Option<i64> = json_response["data"]["abuseConfidenceScore"].as_i64();
Ok(abuse_confidence_score)
}
else {
if verbose {
string_utils::pretty_print_error(
&format!("AbuseIPDB Request failed with status code: {}", response.status())
);
}
Ok(None)
}
}
#[derive(Debug)]
pub enum IPType {
Localhost,
Unspecified,
Extern
}
pub fn check_address_type(remote_address: &str) -> IPType {
if remote_address == "127.0.0.1" || remote_address == "[::1]" {
return IPType::Localhost;
}
else if remote_address == "0.0.0.0" || remote_address == "[::]" {
return IPType::Unspecified;
}
IPType::Extern
}