This commit is contained in:
parent
479520416d
commit
742be2f506
@ -4,6 +4,7 @@ use crate::utils::*;
|
|||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use chrono::Duration;
|
use chrono::Duration;
|
||||||
use clap::{Arg, ArgMatches, Command};
|
use clap::{Arg, ArgMatches, Command};
|
||||||
|
use ipnet::IpNet;
|
||||||
use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify, WatchDescriptor};
|
use nix::sys::inotify::{AddWatchFlags, InitFlags, Inotify, WatchDescriptor};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::{Client, Error as ReqError, Response};
|
use reqwest::{Client, Error as ReqError, Response};
|
||||||
@ -86,7 +87,7 @@ impl Context {
|
|||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
println!("error loading config: {err}, retrying in {CONFIG_RETRY} secs");
|
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;
|
self.zmq = data;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn build_trustnets(&self) -> Vec<IpNet> {
|
||||||
|
let mut trustnets: Vec<IpNet> = 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)]
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||||
|
@ -131,19 +131,16 @@ async fn watchfiles(ctx: &Arc<Mutex<Context>>) -> Receiver<FileEvent> {
|
|||||||
blrx
|
blrx
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_last_file_size(w: &mut HashMap<String, u64>, path: &str) -> u64 {
|
async fn get_last_file_size(w: &mut HashMap<String, u64>, path: &str) -> (u64, bool) {
|
||||||
let currentlen = match std::fs::metadata(&path.to_string()) {
|
let currentlen = match std::fs::metadata(&path.to_string()) {
|
||||||
Ok(u) => u.len().clone(),
|
Ok(u) => u.len().clone(),
|
||||||
Err(e) => {
|
Err(_) => 0u64,
|
||||||
println!("{e}");
|
|
||||||
0u64
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let lastlen = match w.insert(path.to_string(), currentlen) {
|
let lastlen = match w.insert(path.to_string(), currentlen) {
|
||||||
Some(u) => u,
|
Some(u) => u,
|
||||||
None => 0,
|
None => 0u64,
|
||||||
};
|
};
|
||||||
lastlen
|
(lastlen, lastlen != currentlen)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn compare_files_changes(
|
async fn compare_files_changes(
|
||||||
@ -157,7 +154,7 @@ async fn compare_files_changes(
|
|||||||
let mut iplist: Vec<IpData> = vec![];
|
let mut iplist: Vec<IpData> = vec![];
|
||||||
|
|
||||||
let mut ctx = ctx.lock().await;
|
let mut ctx = ctx.lock().await;
|
||||||
tnets = build_trustnets(&ctx.cfg.trustnets);
|
tnets = ctx.cfg.build_trustnets();
|
||||||
|
|
||||||
match modfiles.inevent.name {
|
match modfiles.inevent.name {
|
||||||
Some(name) => {
|
Some(name) => {
|
||||||
@ -174,8 +171,12 @@ async fn compare_files_changes(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let filesize = get_last_file_size(&mut sa.watchedfiles, &handle).await;
|
let (filesize, sizechanged) =
|
||||||
println!("{handle}, {filesize}");
|
get_last_file_size(&mut sa.watchedfiles, &handle).await;
|
||||||
|
|
||||||
|
if !sizechanged {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
match read_lines(&handle, filesize) {
|
match read_lines(&handle, filesize) {
|
||||||
Some(lines) => {
|
Some(lines) => {
|
||||||
@ -193,7 +194,6 @@ async fn compare_files_changes(
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
drop(ctx);
|
|
||||||
for ip in iplist {
|
for ip in iplist {
|
||||||
ipdatatx.send(ip).await.unwrap();
|
ipdatatx.send(ip).await.unwrap();
|
||||||
}
|
}
|
||||||
|
14
src/utils.rs
14
src/utils.rs
@ -1,4 +1,3 @@
|
|||||||
use ipnet::IpNet;
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use nix::unistd;
|
use nix::unistd;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
@ -34,19 +33,6 @@ pub fn _dedup<T: Ord + PartialOrd>(list: &mut Vec<T>) -> usize {
|
|||||||
list.len()
|
list.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_trustnets(cfgtrustnets: &Vec<String>) -> Vec<IpNet> {
|
|
||||||
let mut trustnets: Vec<IpNet> = 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) {
|
pub fn sleep(seconds: u64) {
|
||||||
std::thread::sleep(Duration::from_secs(seconds));
|
std::thread::sleep(Duration::from_secs(seconds));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user