2023-03-05 23:05:50 +01:00
|
|
|
use crate::config::{Context, WebSocketCfg};
|
|
|
|
use crate::ip::IpEvent;
|
2023-04-09 15:05:09 +02:00
|
|
|
use crate::utils::gethostname;
|
2023-03-05 23:05:50 +01:00
|
|
|
|
2023-04-09 01:42:17 +02:00
|
|
|
use serde_json::json;
|
2023-03-12 14:27:05 +01:00
|
|
|
use std::net::TcpStream;
|
2023-03-05 23:05:50 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::mpsc::Sender;
|
|
|
|
use tokio::sync::RwLock;
|
2023-03-12 14:27:05 +01:00
|
|
|
use tungstenite::stream::*;
|
|
|
|
use tungstenite::*;
|
2023-03-05 23:05:50 +01:00
|
|
|
|
|
|
|
pub async fn websocketconnect<'a>(
|
|
|
|
wscfg: &WebSocketCfg,
|
2023-04-09 01:42:17 +02:00
|
|
|
hostname: String,
|
2023-03-05 23:05:50 +01:00
|
|
|
) -> Result<WebSocket<MaybeTlsStream<TcpStream>>, Error> {
|
2023-04-09 01:42:17 +02:00
|
|
|
let (mut socket, _response) = connect(&wscfg.endpoint).expect("Can't connect");
|
|
|
|
let msg = json!({ "hostname": hostname });
|
|
|
|
socket
|
|
|
|
.write_message(Message::Text(msg.to_string()))
|
|
|
|
.unwrap();
|
2023-03-05 23:05:50 +01:00
|
|
|
Ok(socket)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn websocketinit(
|
2023-04-09 01:42:17 +02:00
|
|
|
ctxarc: &Arc<RwLock<Context>>,
|
2023-04-10 11:31:16 +02:00
|
|
|
ipeventtx: Arc<RwLock<Sender<IpEvent>>>,
|
2023-04-09 15:05:09 +02:00
|
|
|
) -> WebSocket<MaybeTlsStream<TcpStream>> {
|
2023-04-10 11:31:16 +02:00
|
|
|
let (wssocketps, mut wssocketrr, bootstrap_event);
|
2023-03-05 23:05:50 +01:00
|
|
|
{
|
|
|
|
let ctx = ctxarc.read().await;
|
2023-04-10 11:31:16 +02:00
|
|
|
bootstrap_event = ctxarc.read().await.cfg.bootstrap_event().clone();
|
2023-04-09 01:42:17 +02:00
|
|
|
wssocketps = Arc::new(RwLock::new(
|
|
|
|
websocketconnect(&ctx.cfg.ws.get("pubsub").unwrap(), ctx.hostname.clone())
|
|
|
|
.await
|
|
|
|
.unwrap(),
|
|
|
|
));
|
2023-04-09 15:05:09 +02:00
|
|
|
wssocketrr = websocketconnect(&ctx.cfg.ws.get("reqrep").unwrap(), ctx.hostname.clone())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
2023-04-10 11:31:16 +02:00
|
|
|
send_to_ipbl_websocket(&mut wssocketrr, &bootstrap_event).await;
|
2023-03-05 23:05:50 +01:00
|
|
|
}
|
|
|
|
|
2023-04-10 11:31:16 +02:00
|
|
|
wslistenpubsub(wssocketps, ipeventtx).await;
|
2023-04-09 15:05:09 +02:00
|
|
|
return wssocketrr;
|
2023-03-05 23:05:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn wslistenpubsub(
|
2023-04-09 01:42:17 +02:00
|
|
|
websocket: Arc<RwLock<WebSocket<MaybeTlsStream<TcpStream>>>>,
|
2023-04-10 11:31:16 +02:00
|
|
|
txpubsub: Arc<RwLock<Sender<IpEvent>>>,
|
2023-03-05 23:05:50 +01:00
|
|
|
) {
|
|
|
|
tokio::spawn(async move {
|
|
|
|
loop {
|
|
|
|
{
|
2023-04-09 01:42:17 +02:00
|
|
|
let mut ws = websocket.write().await;
|
2023-04-09 15:05:09 +02:00
|
|
|
match ws.read_message() {
|
|
|
|
Ok(msg) => {
|
|
|
|
let tosend: IpEvent =
|
|
|
|
serde_json::from_str(msg.to_string().as_str()).unwrap();
|
2023-04-09 01:42:17 +02:00
|
|
|
if tosend.ipdata.hostname != gethostname(true)
|
|
|
|
|| tosend.msgtype == "init".to_string()
|
|
|
|
{
|
2023-04-10 11:31:16 +02:00
|
|
|
let txps = txpubsub.write().await;
|
|
|
|
txps.send(tosend).await.unwrap();
|
2023-04-09 01:42:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
println!("error: {e:?}");
|
|
|
|
ws.close(None).unwrap();
|
|
|
|
return;
|
2023-03-05 23:05:50 +01:00
|
|
|
}
|
2023-04-09 01:42:17 +02:00
|
|
|
};
|
|
|
|
}
|
2023-03-05 23:05:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn send_to_ipbl_websocket(
|
2023-04-09 15:05:09 +02:00
|
|
|
ws: &mut WebSocket<MaybeTlsStream<TcpStream>>,
|
2023-03-05 23:05:50 +01:00
|
|
|
ip: &IpEvent,
|
2023-04-10 11:31:16 +02:00
|
|
|
) -> bool {
|
2023-03-05 23:05:50 +01:00
|
|
|
let msg = format!("{val}", val = serde_json::to_string(&ip).unwrap());
|
2023-03-12 14:27:05 +01:00
|
|
|
|
|
|
|
match ws.write_message(Message::Text(msg)) {
|
2023-04-09 15:05:09 +02:00
|
|
|
Ok(_) => {}
|
2023-03-05 23:05:50 +01:00
|
|
|
Err(e) => {
|
2023-04-10 11:31:16 +02:00
|
|
|
println!("err 1: {e:?}");
|
|
|
|
return false;
|
2023-03-05 23:05:50 +01:00
|
|
|
}
|
|
|
|
};
|
2023-04-09 01:42:17 +02:00
|
|
|
|
2023-03-12 14:27:05 +01:00
|
|
|
match ws.read_message() {
|
2023-04-09 15:05:09 +02:00
|
|
|
Ok(_) => {}
|
2023-03-05 23:05:50 +01:00
|
|
|
Err(e) => {
|
2023-04-10 11:31:16 +02:00
|
|
|
println!("err 2: {e:?}");
|
|
|
|
return false;
|
2023-03-05 23:05:50 +01:00
|
|
|
}
|
|
|
|
};
|
2023-04-10 11:31:16 +02:00
|
|
|
true
|
2023-03-05 23:05:50 +01:00
|
|
|
}
|