From 3f298af976a796e65e8e425037f8b9d7087159d5 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Thu, 2 Nov 2023 11:22:57 +0100 Subject: [PATCH] feat: misc updates in config and ip modules --- src/config.rs | 29 ++++++++++++++++++++++++----- src/ip.rs | 10 ++++++---- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index 8cbc299..3610071 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,7 +17,7 @@ use std::path::Path; pub const GIT_VERSION: &str = git_version!(args = ["--always", "--dirty="]); const MASTERSERVER: &str = "ipbl.paulbsd.com"; const WSSUBSCRIPTION: &str = "ipbl"; -const CONFIG_RETRY: u64 = 1; +const CONFIG_RETRY: u64 = 2; const WEB_CLIENT_TIMEOUT: i64 = 5; #[derive(Debug)] @@ -126,7 +126,7 @@ impl Context { break; } Err(err) => { - println!("error loading config: {err}, retrying in {CONFIG_RETRY} secs"); + println!("error loading config: {err}, retrying in {CONFIG_RETRY}s"); last_in_err = true; sleep_s(CONFIG_RETRY).await; } @@ -641,6 +641,19 @@ mod test { }, }) .await; + ctx.update_blocklist(&mut IpEvent { + msgtype: String::from("add"), + mode: String::from("ws"), + hostname: String::from("localhost"), + ipdata: IpData { + t: 6, + ip: "2a00:1450:4007:805::2003".to_string(), + hostname: "testgood".to_string(), + date: now.to_rfc3339().to_string(), + src: "http".to_string(), + }, + }) + .await; let ip1 = ctx.blocklist.get_mut(&"1.1.1.1".to_string()).unwrap(); ip1.starttime = DateTime::from(now) - Duration::minutes(61); @@ -655,8 +668,14 @@ mod test { let ctx = prepare_test_data().await; let pending = ctx.get_blocklist_pending().await; - assert_eq!(pending.len(), 4); - let ips = ["1.1.1.1", "1.1.1.2", "1.1.1.3", "1.1.1.4"]; + assert_eq!(pending.len(), 5); + let ips = [ + "1.1.1.1", + "1.1.1.2", + "1.1.1.3", + "1.1.1.4", + "2a00:1450:4007:805::2003", + ]; for i in ips { let ip = ctx .blocklist @@ -674,7 +693,7 @@ mod test { let mut ctx = prepare_test_data().await; ctx.gc_blocklist().await; let toblock = ctx.get_blocklist_toblock().await; - assert_eq!(toblock.len(), 2); + assert_eq!(toblock.len(), 3); } #[tokio::test] diff --git a/src/ip.rs b/src/ip.rs index b35c61a..93a48aa 100644 --- a/src/ip.rs +++ b/src/ip.rs @@ -162,12 +162,14 @@ pub fn filter( fn parse_date(input: regex::Captures) -> DateTime { let mut ymd: Vec = vec![]; let mut hms: Vec = vec![]; + let ymd_range = 2..5; + let hms_range = 5..8; - for capture in 2..5 { - ymd.push(input.get(capture).unwrap().as_str().parse::().unwrap()); + for cap in ymd_range { + ymd.push(input.get(cap).unwrap().as_str().parse::().unwrap()); } - for capture in 5..8 { - hms.push(input.get(capture).unwrap().as_str().parse::().unwrap()); + for cap in hms_range { + hms.push(input.get(cap).unwrap().as_str().parse::().unwrap()); } let date: DateTime =