fixed file scanning bug

This commit is contained in:
Paul 2022-09-21 21:03:01 +02:00
parent f7b902aacf
commit 319d246235
3 changed files with 59 additions and 46 deletions

View File

@ -217,7 +217,7 @@ impl Context {
for (src, set) in self.cfg.sets.iter() {
let p = Path::new(set.path.as_str());
if p.is_dir() {
let res = match self.hashwd.get(&set.path.to_string()) {
let wd = match self.hashwd.get(&set.path.to_string()) {
Some(wd) => *wd,
None => {
let res = self
@ -238,6 +238,14 @@ impl Context {
)
}
};
match self.sas.get_mut(&src.clone()) {
Some(s) => {
s.filename = set.filename.clone();
s.fullpath = fullpath;
s.set = set.clone();
s.regex = Regex::new(set.regex.as_str()).unwrap();
}
None => {
self.sas.insert(
src.clone(),
SetMap {
@ -245,12 +253,14 @@ impl Context {
fullpath,
set: set.clone(),
regex: Regex::new(set.regex.as_str()).unwrap(),
wd: res,
wd,
watchedfiles: HashMap::new(),
},
);
}
}
}
}
Ok(())
}
}

View File

@ -60,22 +60,22 @@ pub async fn process(ctx: &Arc<Mutex<Context>>) {
let begin: DateTime<Local> = Local::now().trunc_subsecs(0);
// wait for logs parse and zmq channel receive
let mut received_ip = ipdatarx.recv().await.unwrap();
let mut recvip = ipdatarx.recv().await.unwrap();
// lock the context mutex
let ctxarc = Arc::clone(&ctx);
let mut ctx = ctxarc.lock().await;
if received_ip.ip == "".to_string() && received_ip.mode == "init".to_string() {
for ip_to_send in &mut ctx.get_blocklist_toblock().await {
ip_to_send.mode = "init".to_string();
send_to_ipbl_zmq(&reqsocket, ip_to_send).await;
if recvip.ip == "".to_string() && recvip.mode == "init".to_string() {
for sndip in &mut ctx.get_blocklist_toblock().await {
sndip.mode = "init".to_string();
send_to_ipbl_zmq(&reqsocket, sndip).await;
}
continue;
}
// refresh context blocklist
let filtered_ip = ctx.update_blocklist(&mut received_ip).await;
let filtered_ip = ctx.update_blocklist(&mut recvip).await;
ctx.gc_blocklist().await;
// send ip list to ws and zmq sockets
@ -122,21 +122,24 @@ async fn watchfiles(ctx: &Arc<Mutex<Context>>) -> Receiver<FileEvent> {
events = ctx.instance.read_events().unwrap();
}
for inotifyevent in events {
for inevent in events {
let date: DateTime<Local> = Local::now().trunc_subsecs(0);
bltx.send(FileEvent { inotifyevent, date }).await.unwrap();
bltx.send(FileEvent { inevent, date }).await.unwrap();
}
}
});
blrx
}
async fn get_last_file_size(watchedfiles: &mut HashMap<String, u64>, path: &str) -> u64 {
async fn get_last_file_size(w: &mut HashMap<String, u64>, path: &str) -> u64 {
let currentlen = match std::fs::metadata(&path.to_string()) {
Ok(u) => u.len().clone(),
Err(_) => 0u64,
Err(e) => {
println!("{e}");
0u64
}
};
let lastlen = match watchedfiles.insert(path.to_string(), currentlen) {
let lastlen = match w.insert(path.to_string(), currentlen) {
Some(u) => u,
None => 0,
};
@ -145,44 +148,44 @@ async fn get_last_file_size(watchedfiles: &mut HashMap<String, u64>, path: &str)
async fn compare_files_changes(
ctx: &Arc<Mutex<Context>>,
inotifyrx: &mut Receiver<FileEvent>,
inrx: &mut Receiver<FileEvent>,
ipdatatx: &Sender<IpData>,
) {
let mut trustnets;
let mut tnets;
loop {
let modifiedfiles = inotifyrx.recv().await.unwrap();
let mut list: Vec<IpData> = vec![];
let modfiles = inrx.recv().await.unwrap();
let mut iplist: Vec<IpData> = vec![];
let mut ctx = ctx.lock().await;
trustnets = build_trustnets(&ctx.cfg.trustnets);
tnets = build_trustnets(&ctx.cfg.trustnets);
match modifiedfiles.inotifyevent.name {
match modfiles.inevent.name {
Some(name) => {
let inotify_filename = name.to_str().unwrap();
let filename = name.to_str().unwrap();
for sak in &mut ctx.clone().sas.keys() {
let sa = &mut ctx.sas.get_mut(sak).unwrap();
if modifiedfiles.inotifyevent.wd == sa.wd {
let handle_filename: String;
if modfiles.inevent.wd == sa.wd {
let handle: String;
if sa.filename.as_str() == "" {
handle_filename = format!("{}/{}", &sa.fullpath, inotify_filename);
} else if inotify_filename.starts_with(sa.filename.as_str()) {
handle_filename = sa.fullpath.to_owned();
handle = format!("{}/{}", &sa.fullpath, filename);
} else if filename.starts_with(sa.filename.as_str()) {
handle = sa.fullpath.to_owned();
} else {
continue;
}
let filesize =
get_last_file_size(&mut sa.watchedfiles, &handle_filename).await;
let filesize = get_last_file_size(&mut sa.watchedfiles, &handle).await;
println!("{handle}, {filesize}");
match read_lines(&handle_filename, filesize) {
match read_lines(&handle, filesize) {
Some(lines) => {
filter(
lines,
&mut list,
&trustnets,
&mut iplist,
&tnets,
&sa.regex,
&sa.set.src,
&modifiedfiles.date,
&modfiles.date,
);
}
None => {}
@ -191,7 +194,7 @@ async fn compare_files_changes(
}
}
drop(ctx);
for ip in list {
for ip in iplist {
ipdatatx.send(ip).await.unwrap();
}
}

View File

@ -13,13 +13,13 @@ use tokio::sync::mpsc::Sender;
use tokio::sync::Mutex;
pub struct FileEvent {
pub inotifyevent: InotifyEvent,
pub inevent: InotifyEvent,
pub date: DateTime<Local>,
}
impl std::fmt::Debug for FileEvent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{ie:?}", ie = self.inotifyevent)
write!(f, "{ie:?}", ie = self.inevent)
}
}
@ -62,8 +62,8 @@ async fn send_to_ipbl_ws(ctx: &Context, ip: &mut IpData, ret: &mut Vec<String>)
async fn listenpubsub(ctx: &Arc<Mutex<Context>>, txpubsub: Sender<IpData>, socket: zmq::Socket) {
let ctx = ctx.lock().await;
let prefix = format!(
"{subscription} ",
subscription = ctx.cfg.zmq.get("pubsub").unwrap().subscription
"{sub} ",
sub = ctx.cfg.zmq.get("pubsub").unwrap().subscription
);
socket
.set_subscribe(ctx.cfg.zmq.get("pubsub").unwrap().subscription.as_bytes())