ipblc/old/full.rs

74 lines
2.0 KiB
Rust
Raw Normal View History

use crate::api;
use crate::config::*;
use crate::ip;
use crate::utils::*;
use chrono::prelude::*;
use regex::Regex;
use std::sync::Arc;
pub async fn process(ctx: Arc<Context>) {
let arcctx = Arc::clone(&ctx);
let hostname = arcctx.hostname.as_str().to_string();
loop {
let mut ret = String::new();
ret.push_str(format!("host: {hostname}, ", hostname = hostname).as_str());
let list = process_lines(arcctx.clone()).await;
loop {
match api::push_ip_bulk(&list, arcctx.flags.server.clone(), &mut ret).await {
Ok(_) => {
break println!("{ret}");
}
Err(err) => {
println!("{err}");
sleep(1);
}
};
}
sleep(ctx.flags.interval as u64);
println!("{ret}");
}
}
async fn process_lines(ctx: Arc<Context>) -> Vec<ip::IpData> {
let mut ret = String::new();
let mut list: Vec<ip::IpData> = vec![];
let trustnets = build_trustnets(ctx.cfg.trustnets.clone());
let lastprocess = Local::now();
for folder in &ctx.cfg.folders {
for set in &folder.sets {
let regex = Regex::new(set.regex.as_str()).unwrap();
let filenames = get_filenames(&set.filename);
let mut numip = 0;
for filename in filenames {
numip = numip
+ super::handle_file(
&mut list,
&filename,
set,
&regex,
&lastprocess,
&trustnets,
)
.await;
}
ret.push_str(format!("{t}: {numip}, ", t = set.t, numip = numip).as_str());
}
}
let oldlen = list.len();
let newlen = dedup(&mut list);
ret.push_str(
format!(
"dedup ratio: {percent:.2}%, ",
percent = ((oldlen - newlen) as f64 / oldlen as f64) * 100.
)
.as_str(),
);
list
}