diff --git a/src/config.rs b/src/config.rs index 8ebc0df..e46d82e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -169,7 +169,7 @@ impl Context { res } - pub async fn update_blocklist(&mut self, ipdata: &mut IpData) -> Option { + pub async fn update_blocklist(&mut self, ipdata: &IpData) -> Option { match self.cfg.sets.get(&ipdata.src) { Some(set) => { if self.blocklist.contains_key(&ipdata.ip) @@ -342,17 +342,50 @@ impl Config { port: 9998, subscription: String::new(), })]), - api: String::from("127.0.0.1:8099") + api: String::from("127.0.0.1:8060") } } pub async fn load(&mut self, ctx: Context) -> Result<(), ReqError> { + self.get_global_config(&ctx).await?; self.get_trustnets(&ctx).await?; self.get_sets(&ctx).await?; self.get_zmq_config(&ctx).await?; Ok(()) } + async fn get_global_config(&mut self, ctx: &Context) -> Result<(), ReqError> { + let resp: Result = ctx + .client + .get(format!("{server}/config", server = ctx.flags.server)) + .send() + .await; + let req = match resp { + Ok(re) => re, + Err(err) => return Err(err), + }; + let data: HashMap = match req.json::>().await { + Ok(res) => { + let mut out: HashMap = HashMap::new(); + res.into_iter().map(|x| x).for_each(|x| { + out.insert(x.key.to_string(), x); + }); + out + } + Err(err) => return Err(err), + }; + let key = "".to_string(); + self.api = data + .get(&key.to_string()) + .unwrap_or(&GlobalConfig { + key: "api".to_string(), + value: "127.0.0.1:8060".to_string(), + }) + .value + .clone(); + Ok(()) + } + async fn get_trustnets(&mut self, ctx: &Context) -> Result<(), ReqError> { let resp: Result = ctx .client @@ -432,6 +465,12 @@ impl Config { } } +#[derive(Debug, Deserialize, Serialize, Clone)] +pub struct GlobalConfig { + pub key: String, + pub value: String, +} + #[derive(Debug, Deserialize, Serialize, Clone)] pub struct Set { pub src: String, diff --git a/src/ip.rs b/src/ip.rs index f5f029f..dbadbdb 100644 --- a/src/ip.rs +++ b/src/ip.rs @@ -70,76 +70,6 @@ impl Display for IpData { } } -pub async fn push_ip(ctx: &Context, ip: &IpData, ret: &mut Vec) -> Result<(), ReqError> { - let result: String; - let mut data: Vec = vec![]; - - data.push(IpData { - ip: ip.ip.to_string(), - src: ip.src.to_string(), - date: ip.date.to_string(), - hostname: ip.hostname.to_string(), - mode: "file".to_string(), - }); - - let resp = ctx - .client - .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(()) -} - -pub 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 { - ip: ip.ip.to_string(), - src: ip.src.to_string(), - date: ip.date.to_string(), - hostname: ip.hostname.to_string(), - mode: "file".to_string(), - }) - } - - let resp = ctx - .client - .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(()) -} - pub fn filter( lines: Box, list: &mut Vec, @@ -222,8 +152,15 @@ fn parse_date(input: regex::Captures) -> DateTime { } let date = Local - .ymd(ymd[0] as i32, ymd[1] as u32, ymd[2] as u32) - .and_hms(hms[0] as u32, hms[1] as u32, hms[2] as u32); + .with_ymd_and_hms( + ymd[0] as i32, + ymd[1] as u32, + ymd[2] as u32, + hms[0] as u32, + hms[1] as u32, + hms[2] as u32, + ) + .unwrap(); date } diff --git a/src/ipblc.rs b/src/ipblc.rs index 9ba4279..1404904 100644 --- a/src/ipblc.rs +++ b/src/ipblc.rs @@ -70,7 +70,7 @@ pub async fn run() { tokio::select! { val = ipdata_wait => { - let mut received_ip = val.unwrap(); + let received_ip = val.unwrap(); let mut ctx = ctxclone.write().await; @@ -83,12 +83,13 @@ pub async fn run() { } // refresh context blocklist - let filtered_ip = ctx.update_blocklist(&mut received_ip).await; + let filtered_ip = ctx.update_blocklist(&received_ip).await; // send ip list to ws and zmq sockets - if let Some(mut ip) = filtered_ip { - send_to_ipbl_ws(&ctx, &mut ip, &mut ret).await; - send_to_ipbl_zmq(&zmqreqsocket, &mut ip).await; + if let Some(ip) = filtered_ip { + println!("sending {} to ws and zmq", ip.ip); + send_to_ipbl_ws(&ctx, &ip, &mut ret).await; + send_to_ipbl_zmq(&zmqreqsocket, &ip).await; } } _val = apimsg_wait => { @@ -122,7 +123,6 @@ pub async fn run() { match ctx.load().await { Ok(_) => { last_cfg_reload = Local::now().trunc_subsecs(0); - drop(ctx); } Err(err) => { println!("error loading config: {err}"); diff --git a/src/ws.rs b/src/ws.rs index fc51041..91dc829 100644 --- a/src/ws.rs +++ b/src/ws.rs @@ -1,8 +1,10 @@ use crate::config::Context; -use crate::ip::{push_ip, IpData}; +use crate::ip::IpData; use crate::utils::sleep_s; -pub async fn send_to_ipbl_ws(ctx: &Context, ip: &mut IpData, ret: &mut Vec) { +use reqwest::Error as ReqError; + +pub async fn send_to_ipbl_ws(ctx: &Context, ip: &IpData, ret: &mut Vec) { ret.push(format!("host: {hostname}", hostname = ctx.hostname)); loop { match push_ip(&ctx, &ip, ret).await { @@ -16,3 +18,73 @@ pub async fn send_to_ipbl_ws(ctx: &Context, ip: &mut IpData, ret: &mut Vec) -> Result<(), ReqError> { + let result: String; + let mut data: Vec = vec![]; + + data.push(IpData { + ip: ip.ip.to_string(), + src: ip.src.to_string(), + date: ip.date.to_string(), + hostname: ip.hostname.to_string(), + mode: "file".to_string(), + }); + + let resp = ctx + .client + .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(()) +} + +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 { + ip: ip.ip.to_string(), + src: ip.src.to_string(), + date: ip.date.to_string(), + hostname: ip.hostname.to_string(), + mode: "file".to_string(), + }) + } + + let resp = ctx + .client + .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(()) +} diff --git a/src/zmqcom.rs b/src/zmqcom.rs index 68898ed..de18769 100644 --- a/src/zmqcom.rs +++ b/src/zmqcom.rs @@ -82,7 +82,7 @@ async fn listenpubsub(ctx: &Arc>, txpubsub: Sender, sock }); } -pub async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &mut IpData) { +pub async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &IpData) { let msg = format!("{val}", val = serde_json::to_string(&ip).unwrap()); match reqsocket.send(&msg, 0) { Ok(_) => {}