From 742be2f50607a9c0f371110a6ecd63caa5917797 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Fri, 23 Sep 2022 13:17:20 +0200 Subject: [PATCH] includes minimal changes --- src/config/mod.rs | 16 +++++++++++++++- src/ipblc/inc.rs | 22 +++++++++++----------- src/utils.rs | 14 -------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index d4080f9..6372910 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -4,6 +4,7 @@ use crate::utils::*; use chrono::prelude::*; use chrono::Duration; use clap::{Arg, ArgMatches, Command}; +use ipnet::IpNet; use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify, WatchDescriptor}; use regex::Regex; use reqwest::{Client, Error as ReqError, Response}; @@ -86,7 +87,7 @@ impl Context { } Err(err) => { println!("error loading config: {err}, retrying in {CONFIG_RETRY} secs"); - std::thread::sleep(std::time::Duration::from_secs(CONFIG_RETRY)); + sleep(CONFIG_RETRY); } } } @@ -405,6 +406,19 @@ impl Config { self.zmq = data; Ok(()) } + + pub fn build_trustnets(&self) -> Vec { + let mut trustnets: Vec = vec![]; + for trustnet in &self.trustnets { + match trustnet.parse() { + Ok(net) => trustnets.push(net), + Err(err) => { + println!("error parsing {trustnet}, error: {err}"); + } + }; + } + trustnets + } } #[derive(Debug, Deserialize, Serialize, Clone)] diff --git a/src/ipblc/inc.rs b/src/ipblc/inc.rs index 8482211..ffae386 100644 --- a/src/ipblc/inc.rs +++ b/src/ipblc/inc.rs @@ -131,19 +131,16 @@ async fn watchfiles(ctx: &Arc>) -> Receiver { blrx } -async fn get_last_file_size(w: &mut HashMap, path: &str) -> u64 { +async fn get_last_file_size(w: &mut HashMap, path: &str) -> (u64, bool) { let currentlen = match std::fs::metadata(&path.to_string()) { Ok(u) => u.len().clone(), - Err(e) => { - println!("{e}"); - 0u64 - } + Err(_) => 0u64, }; let lastlen = match w.insert(path.to_string(), currentlen) { Some(u) => u, - None => 0, + None => 0u64, }; - lastlen + (lastlen, lastlen != currentlen) } async fn compare_files_changes( @@ -157,7 +154,7 @@ async fn compare_files_changes( let mut iplist: Vec = vec![]; let mut ctx = ctx.lock().await; - tnets = build_trustnets(&ctx.cfg.trustnets); + tnets = ctx.cfg.build_trustnets(); match modfiles.inevent.name { Some(name) => { @@ -174,8 +171,12 @@ async fn compare_files_changes( continue; } - let filesize = get_last_file_size(&mut sa.watchedfiles, &handle).await; - println!("{handle}, {filesize}"); + let (filesize, sizechanged) = + get_last_file_size(&mut sa.watchedfiles, &handle).await; + + if !sizechanged { + continue; + } match read_lines(&handle, filesize) { Some(lines) => { @@ -193,7 +194,6 @@ async fn compare_files_changes( break; } } - drop(ctx); for ip in iplist { ipdatatx.send(ip).await.unwrap(); } diff --git a/src/utils.rs b/src/utils.rs index 6ad9521..92f8f6a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,3 @@ -use ipnet::IpNet; use lazy_static::lazy_static; use nix::unistd; use regex::Regex; @@ -34,19 +33,6 @@ pub fn _dedup(list: &mut Vec) -> usize { list.len() } -pub fn build_trustnets(cfgtrustnets: &Vec) -> Vec { - let mut trustnets: Vec = vec![]; - for trustnet in cfgtrustnets { - match trustnet.parse() { - Ok(net) => trustnets.push(net), - Err(err) => { - println!("error parsing {trustnet}, error: {err}"); - } - }; - } - trustnets -} - pub fn sleep(seconds: u64) { std::thread::sleep(Duration::from_secs(seconds)); }