optimized initialization

This commit is contained in:
Paul 2022-09-17 22:31:30 +02:00
parent b66c2ee677
commit da6ef34b0e
3 changed files with 15 additions and 8 deletions

View File

@ -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 => {}
}

View File

@ -22,6 +22,7 @@ pub fn block(
tablename: &String,
ips_add: &Vec<IpData>,
ret: &mut Vec<String>,
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(())
}

View File

@ -20,7 +20,8 @@ pub async fn process(ctx: &Arc<Mutex<Context>>) {
let (ipdatatx, mut ipdatarx): (Sender<IpData>, Receiver<IpData>) = 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<Mutex<Context>>) {
&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> = Local::now().trunc_subsecs(0);
if (end - begin) > Duration::seconds(5) {