From da6ef34b0e51cb5a6ab01a6ededddc44aadd2b3b Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sat, 17 Sep 2022 22:31:30 +0200 Subject: [PATCH] optimized initialization --- src/config/mod.rs | 9 ++++----- src/firewall/mod.rs | 6 +++++- src/ipblc/inc.rs | 8 ++++++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index ff76b92..47fe5cc 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -181,15 +181,14 @@ impl Context { DateTime::parse_from_rfc3339(ipdata.date.as_str()).unwrap(); match self.cfg.sets.get(&ipdata.src) { Some(set) => { - self.blocklist.insert( - ipdata.ip.to_string(), - BlockIpData { + self.blocklist + .entry(ipdata.ip.to_string()) + .or_insert(BlockIpData { ipdata: ipdata.clone(), tryfail, starttime, blocktime: set.blocktime, - }, - ); + }); } None => {} } diff --git a/src/firewall/mod.rs b/src/firewall/mod.rs index 0a69e33..e026bf7 100644 --- a/src/firewall/mod.rs +++ b/src/firewall/mod.rs @@ -22,6 +22,7 @@ pub fn block( tablename: &String, ips_add: &Vec, ret: &mut Vec, + fwlen: &mut usize, ) -> std::result::Result<(), Error> { // convert chain let ips_add = convert(ips_add); @@ -59,7 +60,10 @@ pub fn block( // validate and send batch let finalized_batch = batch.finalize(); send_and_process(&finalized_batch)?; - ret.push(format!("{length} ip in memory", length = ips_add.len())); + if fwlen < &mut ips_add.len() { + ret.push(format!("{length} ip in firewall", length = ips_add.len())); + } + *fwlen = ips_add.len(); Ok(()) } diff --git a/src/ipblc/inc.rs b/src/ipblc/inc.rs index c124bb7..eb6b3fc 100644 --- a/src/ipblc/inc.rs +++ b/src/ipblc/inc.rs @@ -20,7 +20,8 @@ pub async fn process(ctx: &Arc>) { let (ipdatatx, mut ipdatarx): (Sender, Receiver) = channel(ZMQ_CHAN_SIZE); // initialize the firewall table - //firewall::init(&env!("CARGO_PKG_NAME").to_string()); + firewall::init(&env!("CARGO_PKG_NAME").to_string()); + let mut fwlen: usize = 0; // initialize zeromq sockets let reqsocket; @@ -88,11 +89,14 @@ pub async fn process(ctx: &Arc>) { &env!("CARGO_PKG_NAME").to_string(), &ctx.get_blocklist_toblock().await, &mut ret, + &mut fwlen, ) .unwrap(); // log lines - println!("{ret}", ret = ret.join(", ")); + if ret.len() > 0 { + println!("{ret}", ret = ret.join(", ")); + } let end: DateTime = Local::now().trunc_subsecs(0); if (end - begin) > Duration::seconds(5) {