for testing

for testing

for testing

for testing

for testing
This commit is contained in:
Paul 2022-09-18 00:00:25 +02:00
parent c2eff44009
commit f7b902aacf
4 changed files with 39 additions and 46 deletions

View File

@ -167,43 +167,35 @@ 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) {
Some(set) => {
if self.blocklist.contains_key(&ipdata.ip)
&& self.hostname == ipdata.hostname
&& ipdata.mode == "file".to_string()
{
let mut block = self.blocklist.get_mut(&ipdata.ip).unwrap(); let mut block = self.blocklist.get_mut(&ipdata.ip).unwrap();
block.tryfail += 1; block.tryfail += 1;
let set = self.cfg.sets.get(&ipdata.src.to_string()).unwrap(); block.blocktime = set.blocktime;
if block.tryfail >= set.tryfail { if block.tryfail >= set.tryfail {
return Some(block.ipdata.clone()); return Some(block.ipdata.clone());
} }
return None;
} else { } else {
let mut tryfail = 0;
let mut send = false;
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();
match self.cfg.sets.get(&ipdata.src) { self.blocklist
Some(set) => { .entry(ipdata.ip.to_string())
if ipdata.mode == "zmq".to_string() { .or_insert(BlockIpData {
tryfail = 100;
send = true;
}
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 => {}
} }
match send { None
true => return Some(ipdata.clone()),
false => return None,
}
}
} }
pub async fn gc_blocklist(&mut self) -> Vec<IpData> { pub async fn gc_blocklist(&mut self) -> Vec<IpData> {
@ -460,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(),
@ -471,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(),
@ -481,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(),
@ -490,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(),
@ -499,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,11 +60,12 @@ impl Display for IpData {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!( write!(
f, f,
"ip: {ip}, src: {src}, date: {date}, hostname: {hostname}", "ip: {ip}, src: {src}, date: {date}, hostname: {hostname}, mode: {mode}",
ip = self.ip, ip = self.ip,
src = self.src, src = self.src,
date = self.date, date = self.date,
hostname = self.hostname, hostname = self.hostname,
mode = self.mode,
) )
} }
} }

View File

@ -60,28 +60,28 @@ 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
if let Some(ip) = filtered_ip { if let Some(mut ip) = filtered_ip {
send_to_ipbl_ws(&ctx, &ip, &mut ret).await; send_to_ipbl_ws(&ctx, &mut ip, &mut ret).await;
send_to_ipbl_zmq(&reqsocket, &ip).await; send_to_ipbl_zmq(&reqsocket, &mut ip).await;
} }
// apply firewall blocking // apply firewall blocking

View File

@ -23,7 +23,7 @@ impl std::fmt::Debug for FileEvent {
} }
} }
async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &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());
match reqsocket.send(&msg, 0) { match reqsocket.send(&msg, 0) {
Ok(_) => {} Ok(_) => {}
@ -44,7 +44,7 @@ async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &IpData) {
}; };
} }
async fn send_to_ipbl_ws(ctx: &Context, ip: &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));
loop { loop {
match push_ip(&ctx, &ip, ret).await { match push_ip(&ctx, &ip, ret).await {
@ -89,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();
} }
} }