use crate::config::{httpclient, Context}; use crate::ip::{IpData, IpEvent}; use crate::utils::sleep_s; use reqwest::Client; use reqwest::Error as ReqError; const MAX_FAILED_API_RATE: u64 = 10; pub async fn send_to_ipbl_api(server: &str, ip: &IpEvent) { let mut try_req = 0; let client = httpclient(); loop { match push_ip(&client, &server, &ip.ipdata.clone().unwrap()).await { Ok(_) => { break; } Err(e) => { println!("error: {e}"); sleep_s(1).await; if try_req == MAX_FAILED_API_RATE { break; } try_req += 1; } }; } } async fn push_ip(client: &Client, server: &str, ip: &IpData) -> Result<(), ReqError> { let mut data: Vec = vec![]; data.push(IpData { t: ip.t, ip: ip.ip.to_string(), src: ip.src.to_string(), date: ip.date.to_string(), hostname: ip.hostname.to_string(), }); client .post(format!("{server}/ips")) .json(&data) .send() .await?; Ok(()) } async fn _push_ip_bulk( ctx: &Context, ips: &Vec, ret: &mut Vec, ) -> Result<(), ReqError> { let result: String; let mut data: Vec = vec![]; for ip in ips { data.push(IpData { t: ip.t, ip: ip.ip.to_string(), src: ip.src.to_string(), date: ip.date.to_string(), hostname: ip.hostname.to_string(), }) } let resp = httpclient() .post(format!("{server}/ips", server = ctx.flags.server)) .json(&data) .send() .await?; ret.push(format!("status: {status}", status = resp.status())); let res = resp.text().await.unwrap(); if res.trim().len() > 0 { result = res.trim().to_string(); } else { result = "".to_string(); } ret.push(format!("response: {result}")); Ok(()) }