for testing

This commit is contained in:
Paul 2022-09-18 10:48:32 +02:00
parent 424b1457a6
commit c480b322df
3 changed files with 28 additions and 40 deletions

View File

@ -167,44 +167,34 @@ impl Context {
res res
} }
pub async fn update_blocklist(&mut self, ipdata: &IpData) -> Option<IpData> { pub async fn update_blocklist(&mut self, ipdata: &mut IpData) -> Option<IpData> {
if self.blocklist.contains_key(&ipdata.ip) match self.cfg.sets.get(&ipdata.src) {
&& self.hostname == ipdata.hostname Some(set) => {
&& ipdata.mode == "file".to_string() if self.blocklist.contains_key(&ipdata.ip)
{ && self.hostname == ipdata.hostname
let mut block = self.blocklist.get_mut(&ipdata.ip).unwrap(); && ipdata.mode == "file".to_string()
block.tryfail += 1; {
match self.cfg.sets.get(&ipdata.src) { let mut block = self.blocklist.get_mut(&ipdata.ip).unwrap();
Some(set) => { block.tryfail += 1;
block.blocktime = set.blocktime;
if block.tryfail >= set.tryfail { if block.tryfail >= set.tryfail {
return Some(block.ipdata.clone()); return Some(block.ipdata.clone());
} }
} } else {
None => {}
}
}
if !self.blocklist.contains_key(&ipdata.ip) {
let mut tryfail = 0;
match self.cfg.sets.get(&ipdata.src) {
Some(set) => {
let starttime: DateTime<FixedOffset> = let starttime: DateTime<FixedOffset> =
DateTime::parse_from_rfc3339(ipdata.date.as_str()).unwrap(); DateTime::parse_from_rfc3339(ipdata.date.as_str()).unwrap();
if ipdata.mode == "zmq".to_string() { self.blocklist
tryfail = 100; .entry(ipdata.ip.to_string())
} .or_insert(BlockIpData {
self.blocklist.insert(
ipdata.ip.to_string(),
BlockIpData {
ipdata: ipdata.clone(), ipdata: ipdata.clone(),
tryfail, tryfail: 100,
starttime, starttime,
blocktime: set.blocktime, blocktime: set.blocktime,
}, });
);
} }
None => {}
} }
}; None => {}
}
None None
} }
@ -462,7 +452,7 @@ mod test {
ctx.blocklist = HashMap::new(); ctx.blocklist = HashMap::new();
for _i in 0..10 { for _i in 0..10 {
ctx.update_blocklist(&IpData { ctx.update_blocklist(&mut IpData {
ip: "1.1.1.1".to_string(), ip: "1.1.1.1".to_string(),
hostname: "test1".to_string(), hostname: "test1".to_string(),
date: now.to_rfc3339().to_string(), date: now.to_rfc3339().to_string(),
@ -473,7 +463,7 @@ mod test {
} }
for _i in 0..10 { for _i in 0..10 {
ctx.update_blocklist(&IpData { ctx.update_blocklist(&mut IpData {
ip: "1.1.1.2".to_string(), ip: "1.1.1.2".to_string(),
hostname: "test2".to_string(), hostname: "test2".to_string(),
date: now.to_rfc3339().to_string(), date: now.to_rfc3339().to_string(),
@ -483,7 +473,7 @@ mod test {
.await; .await;
} }
ctx.update_blocklist(&IpData { ctx.update_blocklist(&mut IpData {
ip: "1.1.1.3".to_string(), ip: "1.1.1.3".to_string(),
hostname: "testgood".to_string(), hostname: "testgood".to_string(),
date: now.to_rfc3339().to_string(), date: now.to_rfc3339().to_string(),
@ -492,7 +482,7 @@ mod test {
}) })
.await; .await;
ctx.update_blocklist(&IpData { ctx.update_blocklist(&mut IpData {
ip: "1.1.1.4".to_string(), ip: "1.1.1.4".to_string(),
hostname: "testgood".to_string(), hostname: "testgood".to_string(),
date: now.to_rfc3339().to_string(), date: now.to_rfc3339().to_string(),
@ -501,7 +491,7 @@ mod test {
}) })
.await; .await;
ctx.update_blocklist(&IpData { ctx.update_blocklist(&mut IpData {
ip: "1.1.1.4".to_string(), ip: "1.1.1.4".to_string(),
hostname: "testgood".to_string(), hostname: "testgood".to_string(),
date: now.to_rfc3339().to_string(), date: now.to_rfc3339().to_string(),

View File

@ -60,22 +60,22 @@ pub async fn process(ctx: &Arc<Mutex<Context>>) {
let begin: DateTime<Local> = Local::now().trunc_subsecs(0); let begin: DateTime<Local> = Local::now().trunc_subsecs(0);
// wait for logs parse and zmq channel receive // wait for logs parse and zmq channel receive
let received_ip = ipdatarx.recv().await.unwrap(); let mut received_ip = ipdatarx.recv().await.unwrap();
// lock the context mutex // lock the context mutex
let ctxarc = Arc::clone(&ctx); let ctxarc = Arc::clone(&ctx);
let mut ctx = ctxarc.lock().await; let mut ctx = ctxarc.lock().await;
if received_ip.mode == "init" { if received_ip.ip == "".to_string() && received_ip.mode == "init".to_string() {
for ip_to_send in &mut ctx.get_blocklist_toblock().await { for ip_to_send in &mut ctx.get_blocklist_toblock().await {
ip_to_send.mode = "zmq".to_string(); ip_to_send.mode = "init".to_string();
send_to_ipbl_zmq(&reqsocket, ip_to_send).await; send_to_ipbl_zmq(&reqsocket, ip_to_send).await;
} }
continue; continue;
} }
// refresh context blocklist // refresh context blocklist
let filtered_ip = ctx.update_blocklist(&received_ip).await; let filtered_ip = ctx.update_blocklist(&mut received_ip).await;
ctx.gc_blocklist().await; ctx.gc_blocklist().await;
// send ip list to ws and zmq sockets // send ip list to ws and zmq sockets

View File

@ -25,7 +25,6 @@ impl std::fmt::Debug for FileEvent {
async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &mut IpData) { async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &mut IpData) {
let msg = format!("{value}", value = serde_json::to_string(&ip).unwrap()); let msg = format!("{value}", value = serde_json::to_string(&ip).unwrap());
ip.mode = "zmq".to_string();
match reqsocket.send(&msg, 0) { match reqsocket.send(&msg, 0) {
Ok(_) => {} Ok(_) => {}
Err(e) => { Err(e) => {
@ -47,7 +46,6 @@ async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &mut IpData) {
async fn send_to_ipbl_ws(ctx: &Context, ip: &mut IpData, ret: &mut Vec<String>) { async fn send_to_ipbl_ws(ctx: &Context, ip: &mut IpData, ret: &mut Vec<String>) {
ret.push(format!("host: {hostname}", hostname = ctx.hostname)); ret.push(format!("host: {hostname}", hostname = ctx.hostname));
ip.mode = "zmq".to_string();
loop { loop {
match push_ip(&ctx, &ip, ret).await { match push_ip(&ctx, &ip, ret).await {
Ok(_) => { Ok(_) => {
@ -91,7 +89,7 @@ async fn listenpubsub(ctx: &Arc<Mutex<Context>>, txpubsub: Sender<IpData>, socke
Some(ss) => { Some(ss) => {
let msg = ss.strip_prefix(prefix.as_str()).unwrap(); let msg = ss.strip_prefix(prefix.as_str()).unwrap();
let tosend: IpData = serde_json::from_str(msg).unwrap(); let tosend: IpData = serde_json::from_str(msg).unwrap();
if tosend.hostname != gethostname(true) || tosend.mode == "zmq".to_string() { if tosend.hostname != gethostname(true) || tosend.mode == "init".to_string() {
txpubsub.send(tosend).await.unwrap(); txpubsub.send(tosend).await.unwrap();
} }
} }