From 8026f34b8b4dbe22477132deabd2bfdb500a253a Mon Sep 17 00:00:00 2001 From: ghassmo Date: Sat, 18 Jun 2022 08:48:37 +0300 Subject: [PATCH] bin/ircd: create util module and general clean up --- bin/ircd/src/main.rs | 118 ++++++++++++++++++++----------------------- bin/ircd/src/util.rs | 24 +++++++++ 2 files changed, 80 insertions(+), 62 deletions(-) create mode 100644 bin/ircd/src/util.rs diff --git a/bin/ircd/src/main.rs b/bin/ircd/src/main.rs index d33592622..f33ffc098 100644 --- a/bin/ircd/src/main.rs +++ b/bin/ircd/src/main.rs @@ -2,7 +2,7 @@ use async_std::{ net::{TcpListener, TcpStream}, sync::{Arc, Mutex}, }; -use std::{net::SocketAddr, sync::atomic::Ordering}; +use std::net::SocketAddr; use async_channel::Receiver; use async_executor::Executor; @@ -30,6 +30,7 @@ pub mod protocol_privmsg; pub mod rpc; pub mod server; pub mod settings; +pub mod util; use crate::{ crypto::try_decrypt_message, @@ -38,29 +39,11 @@ use crate::{ rpc::JsonRpcInterface, server::IrcServerConnection, settings::{parse_configured_channels, Args, ChannelInfo, CONFIG_FILE, CONFIG_FILE_CONTENTS}, + util::clean_input, }; const SIZE_OF_MSGS_BUFFER: usize = 4096; -fn clean_input(mut line: String, peer_addr: &SocketAddr) -> Result { - if line.is_empty() { - warn!("Received empty line from {}. ", peer_addr); - warn!("Closing connection."); - return Err(Error::ChannelStopped) - } - - if &line[(line.len() - 2)..] != "\r\n" { - warn!("Closing connection."); - return Err(Error::ChannelStopped) - } - - // Remove CRLF - line.pop(); - line.pop(); - - Ok(line) -} - struct Ircd { // msgs seen_msg_ids: SeenMsgIds, @@ -105,52 +88,63 @@ impl Ircd { let p2p_receiver = self.p2p_receiver.clone(); - executor.spawn(async move { - loop { - let mut line = String::new(); - futures::select! { - privmsg = p2p_receiver.recv().fuse() => { - let mut msg = privmsg?; - info!("Received msg from P2p network: {:?}", msg); + executor + .spawn(async move { + loop { + let mut line = String::new(); + futures::select! { + privmsg = p2p_receiver.recv().fuse() => { + let mut msg = privmsg?; + info!("Received msg from P2p network: {:?}", msg); - // Try to potentially decrypt the incoming message. - if conn.configured_chans.contains_key(&msg.channel) { - let chan_info = conn.configured_chans.get(&msg.channel).unwrap(); - if !chan_info.joined { - continue - } - if let Some(salt_box) = &chan_info.salt_box { - if let Some(decrypted_msg) = try_decrypt_message(salt_box, &msg.message) { - msg.message = decrypted_msg; - info!("Decrypted received message: {:?}", msg); + // Try to potentially decrypt the incoming message. + if conn.configured_chans.contains_key(&msg.channel) { + let chan_info = conn.configured_chans.get(&msg.channel).unwrap(); + if !chan_info.joined { + continue } + + let salt_box = chan_info.salt_box.clone(); + if salt_box.is_none() { + continue + } + + let decrypted_msg = + try_decrypt_message(&salt_box.unwrap(), &msg.message); + + if decrypted_msg.is_none() { + continue + } + + msg.message = decrypted_msg.unwrap(); + info!("Decrypted received message: {:?}", msg); + } + + conn.reply(&msg.to_irc_msg()).await?; + } + err = reader.read_line(&mut line).fuse() => { + if let Err(e) = err { + warn!("Read line error. Closing stream for {}: {}", peer_addr, e); + return Ok(()) + } + + info!("Received msg from IRC client: {:?}", line); + let irc_msg = match clean_input(line, &peer_addr) { + Ok(m) => m, + Err(e) => return Err(e) + }; + + info!("Send msg to IRC client '{}' from {}", irc_msg, peer_addr); + + if let Err(e) = conn.update(irc_msg).await { + warn!("Connection error: {} for {}", e, peer_addr); + return Err(Error::ChannelStopped) } } - - conn.reply(&msg.to_irc_msg()).await?; - } - err = reader.read_line(&mut line).fuse() => { - if let Err(e) = err { - warn!("Read line error. Closing stream for {}: {}", peer_addr, e); - return Ok(()) - } - - info!("Received msg from IRC client: {:?}", line); - let irc_msg = match clean_input(line, &peer_addr) { - Ok(m) => m, - Err(e) => return Err(e) - }; - - info!("Send msg to IRC client '{}' from {}", irc_msg, peer_addr); - - if let Err(e) = conn.update(irc_msg).await { - warn!("Connection error: {} for {}", e, peer_addr); - return Err(Error::ChannelStopped) - } - } - }; - } - }).detach(); + }; + } + }) + .detach(); Ok(()) } diff --git a/bin/ircd/src/util.rs b/bin/ircd/src/util.rs new file mode 100644 index 000000000..c16a94653 --- /dev/null +++ b/bin/ircd/src/util.rs @@ -0,0 +1,24 @@ +use std::net::SocketAddr; + +use log::warn; + +use darkfi::{Error, Result}; + +pub fn clean_input(mut line: String, peer_addr: &SocketAddr) -> Result { + if line.is_empty() { + warn!("Received empty line from {}. ", peer_addr); + warn!("Closing connection."); + return Err(Error::ChannelStopped) + } + + if &line[(line.len() - 2)..] != "\r\n" { + warn!("Closing connection."); + return Err(Error::ChannelStopped) + } + + // Remove CRLF + line.pop(); + line.pop(); + + Ok(line) +}