updated ipblc
Some checks reported errors
continuous-integration/drone/push Build encountered an error

This commit is contained in:
Paul 2023-01-15 15:32:41 +01:00
parent 6ffea4d0e8
commit ee5119c512
5 changed files with 131 additions and 83 deletions

View File

@ -169,7 +169,7 @@ impl Context {
res res
} }
pub async fn update_blocklist(&mut self, ipdata: &mut IpData) -> Option<IpData> { pub async fn update_blocklist(&mut self, ipdata: &IpData) -> Option<IpData> {
match self.cfg.sets.get(&ipdata.src) { match self.cfg.sets.get(&ipdata.src) {
Some(set) => { Some(set) => {
if self.blocklist.contains_key(&ipdata.ip) if self.blocklist.contains_key(&ipdata.ip)
@ -342,17 +342,50 @@ impl Config {
port: 9998, port: 9998,
subscription: String::new(), subscription: String::new(),
})]), })]),
api: String::from("127.0.0.1:8099") api: String::from("127.0.0.1:8060")
} }
} }
pub async fn load(&mut self, ctx: Context) -> Result<(), ReqError> { pub async fn load(&mut self, ctx: Context) -> Result<(), ReqError> {
self.get_global_config(&ctx).await?;
self.get_trustnets(&ctx).await?; self.get_trustnets(&ctx).await?;
self.get_sets(&ctx).await?; self.get_sets(&ctx).await?;
self.get_zmq_config(&ctx).await?; self.get_zmq_config(&ctx).await?;
Ok(()) Ok(())
} }
async fn get_global_config(&mut self, ctx: &Context) -> Result<(), ReqError> {
let resp: Result<Response, ReqError> = ctx
.client
.get(format!("{server}/config", server = ctx.flags.server))
.send()
.await;
let req = match resp {
Ok(re) => re,
Err(err) => return Err(err),
};
let data: HashMap<String, GlobalConfig> = match req.json::<Vec<GlobalConfig>>().await {
Ok(res) => {
let mut out: HashMap<String, GlobalConfig> = HashMap::new();
res.into_iter().map(|x| x).for_each(|x| {
out.insert(x.key.to_string(), x);
});
out
}
Err(err) => return Err(err),
};
let key = "".to_string();
self.api = data
.get(&key.to_string())
.unwrap_or(&GlobalConfig {
key: "api".to_string(),
value: "127.0.0.1:8060".to_string(),
})
.value
.clone();
Ok(())
}
async fn get_trustnets(&mut self, ctx: &Context) -> Result<(), ReqError> { async fn get_trustnets(&mut self, ctx: &Context) -> Result<(), ReqError> {
let resp: Result<Response, ReqError> = ctx let resp: Result<Response, ReqError> = ctx
.client .client
@ -432,6 +465,12 @@ impl Config {
} }
} }
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct GlobalConfig {
pub key: String,
pub value: String,
}
#[derive(Debug, Deserialize, Serialize, Clone)] #[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Set { pub struct Set {
pub src: String, pub src: String,

View File

@ -70,76 +70,6 @@ impl Display for IpData {
} }
} }
pub async fn push_ip(ctx: &Context, ip: &IpData, ret: &mut Vec<String>) -> Result<(), ReqError> {
let result: String;
let mut data: Vec<IpData> = vec![];
data.push(IpData {
ip: ip.ip.to_string(),
src: ip.src.to_string(),
date: ip.date.to_string(),
hostname: ip.hostname.to_string(),
mode: "file".to_string(),
});
let resp = ctx
.client
.post(format!("{server}/ips", server = ctx.flags.server))
.json(&data)
.send()
.await?;
ret.push(format!("status: {status}", status = resp.status()));
let res = resp.text().await.unwrap();
if res.trim().len() > 0 {
result = res.trim().to_string();
} else {
result = "".to_string();
}
ret.push(format!("response: {result}"));
Ok(())
}
pub async fn _push_ip_bulk(
ctx: &Context,
ips: &Vec<IpData>,
ret: &mut Vec<String>,
) -> Result<(), ReqError> {
let result: String;
let mut data: Vec<IpData> = vec![];
for ip in ips {
data.push(IpData {
ip: ip.ip.to_string(),
src: ip.src.to_string(),
date: ip.date.to_string(),
hostname: ip.hostname.to_string(),
mode: "file".to_string(),
})
}
let resp = ctx
.client
.post(format!("{server}/ips", server = ctx.flags.server))
.json(&data)
.send()
.await?;
ret.push(format!("status: {status}", status = resp.status()));
let res = resp.text().await.unwrap();
if res.trim().len() > 0 {
result = res.trim().to_string();
} else {
result = "".to_string();
}
ret.push(format!("response: {result}"));
Ok(())
}
pub fn filter( pub fn filter(
lines: Box<dyn Read>, lines: Box<dyn Read>,
list: &mut Vec<IpData>, list: &mut Vec<IpData>,
@ -222,8 +152,15 @@ fn parse_date(input: regex::Captures) -> DateTime<Local> {
} }
let date = Local let date = Local
.ymd(ymd[0] as i32, ymd[1] as u32, ymd[2] as u32) .with_ymd_and_hms(
.and_hms(hms[0] as u32, hms[1] as u32, hms[2] as u32); ymd[0] as i32,
ymd[1] as u32,
ymd[2] as u32,
hms[0] as u32,
hms[1] as u32,
hms[2] as u32,
)
.unwrap();
date date
} }

View File

@ -70,7 +70,7 @@ pub async fn run() {
tokio::select! { tokio::select! {
val = ipdata_wait => { val = ipdata_wait => {
let mut received_ip = val.unwrap(); let received_ip = val.unwrap();
let mut ctx = ctxclone.write().await; let mut ctx = ctxclone.write().await;
@ -83,12 +83,13 @@ pub async fn run() {
} }
// refresh context blocklist // refresh context blocklist
let filtered_ip = ctx.update_blocklist(&mut received_ip).await; let filtered_ip = ctx.update_blocklist(&received_ip).await;
// send ip list to ws and zmq sockets // send ip list to ws and zmq sockets
if let Some(mut ip) = filtered_ip { if let Some(ip) = filtered_ip {
send_to_ipbl_ws(&ctx, &mut ip, &mut ret).await; println!("sending {} to ws and zmq", ip.ip);
send_to_ipbl_zmq(&zmqreqsocket, &mut ip).await; send_to_ipbl_ws(&ctx, &ip, &mut ret).await;
send_to_ipbl_zmq(&zmqreqsocket, &ip).await;
} }
} }
_val = apimsg_wait => { _val = apimsg_wait => {
@ -122,7 +123,6 @@ pub async fn run() {
match ctx.load().await { match ctx.load().await {
Ok(_) => { Ok(_) => {
last_cfg_reload = Local::now().trunc_subsecs(0); last_cfg_reload = Local::now().trunc_subsecs(0);
drop(ctx);
} }
Err(err) => { Err(err) => {
println!("error loading config: {err}"); println!("error loading config: {err}");

View File

@ -1,8 +1,10 @@
use crate::config::Context; use crate::config::Context;
use crate::ip::{push_ip, IpData}; use crate::ip::IpData;
use crate::utils::sleep_s; use crate::utils::sleep_s;
pub async fn send_to_ipbl_ws(ctx: &Context, ip: &mut IpData, ret: &mut Vec<String>) { use reqwest::Error as ReqError;
pub async fn send_to_ipbl_ws(ctx: &Context, ip: &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 {
@ -16,3 +18,73 @@ pub async fn send_to_ipbl_ws(ctx: &Context, ip: &mut IpData, ret: &mut Vec<Strin
}; };
} }
} }
async fn push_ip(ctx: &Context, ip: &IpData, ret: &mut Vec<String>) -> Result<(), ReqError> {
let result: String;
let mut data: Vec<IpData> = vec![];
data.push(IpData {
ip: ip.ip.to_string(),
src: ip.src.to_string(),
date: ip.date.to_string(),
hostname: ip.hostname.to_string(),
mode: "file".to_string(),
});
let resp = ctx
.client
.post(format!("{server}/ips", server = ctx.flags.server))
.json(&data)
.send()
.await?;
ret.push(format!("status: {status}", status = resp.status()));
let res = resp.text().await.unwrap();
if res.trim().len() > 0 {
result = res.trim().to_string();
} else {
result = "".to_string();
}
ret.push(format!("response: {result}"));
Ok(())
}
async fn _push_ip_bulk(
ctx: &Context,
ips: &Vec<IpData>,
ret: &mut Vec<String>,
) -> Result<(), ReqError> {
let result: String;
let mut data: Vec<IpData> = vec![];
for ip in ips {
data.push(IpData {
ip: ip.ip.to_string(),
src: ip.src.to_string(),
date: ip.date.to_string(),
hostname: ip.hostname.to_string(),
mode: "file".to_string(),
})
}
let resp = ctx
.client
.post(format!("{server}/ips", server = ctx.flags.server))
.json(&data)
.send()
.await?;
ret.push(format!("status: {status}", status = resp.status()));
let res = resp.text().await.unwrap();
if res.trim().len() > 0 {
result = res.trim().to_string();
} else {
result = "".to_string();
}
ret.push(format!("response: {result}"));
Ok(())
}

View File

@ -82,7 +82,7 @@ async fn listenpubsub(ctx: &Arc<RwLock<Context>>, txpubsub: Sender<IpData>, sock
}); });
} }
pub async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &mut IpData) { pub async fn send_to_ipbl_zmq(reqsocket: &zmq::Socket, ip: &IpData) {
let msg = format!("{val}", val = serde_json::to_string(&ip).unwrap()); let msg = format!("{val}", val = serde_json::to_string(&ip).unwrap());
match reqsocket.send(&msg, 0) { match reqsocket.send(&msg, 0) {
Ok(_) => {} Ok(_) => {}