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
}
pub async fn update_blocklist(&mut self, ipdata: &IpData) -> Option<IpData> {
if self.blocklist.contains_key(&ipdata.ip) {
let mut block = self.blocklist.get_mut(&ipdata.ip).unwrap();
block.tryfail += 1;
let set = self.cfg.sets.get(&ipdata.src.to_string()).unwrap();
if block.tryfail >= set.tryfail {
return Some(block.ipdata.clone());
}
return None;
} else {
let mut tryfail = 0;
let mut send = false;
let starttime: DateTime<FixedOffset> =
DateTime::parse_from_rfc3339(ipdata.date.as_str()).unwrap();
match self.cfg.sets.get(&ipdata.src) {
Some(set) => {
if ipdata.mode == "zmq".to_string() {
tryfail = 100;
send = true;
pub async fn update_blocklist(&mut self, ipdata: &mut IpData) -> Option<IpData> {
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();
block.tryfail += 1;
block.blocktime = set.blocktime;
if block.tryfail >= set.tryfail {
return Some(block.ipdata.clone());
}
self.blocklist.insert(
ipdata.ip.to_string(),
BlockIpData {
} else {
let starttime: DateTime<FixedOffset> =
DateTime::parse_from_rfc3339(ipdata.date.as_str()).unwrap();
self.blocklist
.entry(ipdata.ip.to_string())
.or_insert(BlockIpData {
ipdata: ipdata.clone(),
tryfail,
tryfail: 100,
starttime,
blocktime: set.blocktime,
},
);
});
}
None => {}
}
match send {
true => return Some(ipdata.clone()),
false => return None,
}
None => {}
}
None
}
pub async fn gc_blocklist(&mut self) -> Vec<IpData> {
@ -460,7 +452,7 @@ mod test {
ctx.blocklist = HashMap::new();
for _i in 0..10 {
ctx.update_blocklist(&IpData {
ctx.update_blocklist(&mut IpData {
ip: "1.1.1.1".to_string(),
hostname: "test1".to_string(),
date: now.to_rfc3339().to_string(),
@ -471,7 +463,7 @@ mod test {
}
for _i in 0..10 {
ctx.update_blocklist(&IpData {
ctx.update_blocklist(&mut IpData {
ip: "1.1.1.2".to_string(),
hostname: "test2".to_string(),
date: now.to_rfc3339().to_string(),
@ -481,7 +473,7 @@ mod test {
.await;
}
ctx.update_blocklist(&IpData {
ctx.update_blocklist(&mut IpData {
ip: "1.1.1.3".to_string(),
hostname: "testgood".to_string(),
date: now.to_rfc3339().to_string(),
@ -490,7 +482,7 @@ mod test {
})
.await;
ctx.update_blocklist(&IpData {
ctx.update_blocklist(&mut IpData {
ip: "1.1.1.4".to_string(),
hostname: "testgood".to_string(),
date: now.to_rfc3339().to_string(),
@ -499,7 +491,7 @@ mod test {
})
.await;
ctx.update_blocklist(&IpData {
ctx.update_blocklist(&mut IpData {
ip: "1.1.1.4".to_string(),
hostname: "testgood".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 {
write!(
f,
"ip: {ip}, src: {src}, date: {date}, hostname: {hostname}",
"ip: {ip}, src: {src}, date: {date}, hostname: {hostname}, mode: {mode}",
ip = self.ip,
src = self.src,
date = self.date,
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);
// 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
let ctxarc = Arc::clone(&ctx);
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 {
ip_to_send.mode = "zmq".to_string();
ip_to_send.mode = "init".to_string();
send_to_ipbl_zmq(&reqsocket, ip_to_send).await;
}
continue;
}
// 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;
// send ip list to ws and zmq sockets
if let Some(ip) = filtered_ip {
send_to_ipbl_ws(&ctx, &ip, &mut ret).await;
send_to_ipbl_zmq(&reqsocket, &ip).await;
if let Some(mut ip) = filtered_ip {
send_to_ipbl_ws(&ctx, &mut ip, &mut ret).await;
send_to_ipbl_zmq(&reqsocket, &mut ip).await;
}
// 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());
match reqsocket.send(&msg, 0) {
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));
loop {
match push_ip(&ctx, &ip, ret).await {
@ -89,7 +89,7 @@ async fn listenpubsub(ctx: &Arc<Mutex<Context>>, txpubsub: Sender<IpData>, socke
Some(ss) => {
let msg = ss.strip_prefix(prefix.as_str()).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();
}
}