Просмотр исходного кода

bin/ircd: implement encrypted DMs

ghassmo 3 лет назад
Родитель
Сommit
a392477a54

+ 1 - 1
bin/ircd/src/crypto.rs

@@ -6,7 +6,7 @@ use fxhash::FxHashMap;
 use rand::rngs::OsRng;
 
 use crate::{
-    privmsg::Privmsg,
+    privmsg::{Privmsg, MAXIMUM_LENGTH_OF_NICKNAME},
     settings::{ChannelInfo, ContactInfo},
     MAXIMUM_LENGTH_OF_NICKNAME,
     privmsg::{Privmsg, MAXIMUM_LENGTH_OF_NICKNAME},

+ 6 - 2
bin/ircd/src/irc_server/command.rs

@@ -232,8 +232,12 @@ impl<C: AsyncRead + AsyncWrite + Send + Unpin + 'static> IrcServerConnection<C>
                 info!("(Encrypted) PRIVMSG: {:?}", privmsg);
             }
         } else {
-            // If we have a configured secret for this nick, we encrypt the message.
-            if let Some(salt_box) = self.configured_contacts.get(target) {
+            if !self.configured_contacts.contains_key(target) {
+                return Ok(())
+            }
+
+            let contact_info = self.configured_contacts.get(target).unwrap();
+            if let Some(salt_box) = &contact_info.salt_box {
                 encrypt_privmsg(salt_box, &mut privmsg);
                 info!("(Encrypted) PRIVMSG: {:?}", privmsg);
             }

+ 22 - 11
bin/ircd/src/irc_server/mod.rs

@@ -7,10 +7,10 @@ use log::{debug, info, warn};
 use darkfi::{net::P2pPtr, system::SubscriberPtr, Error, Result};
 
 use crate::{
-    buffers::{ArcPrivmsgsBuffer, SeenMsgIds},
+    buffers::{ArcPrivmsgsBuffer, SeenIds},
     crypto::{decrypt_privmsg, decrypt_target},
     privmsg::MAXIMUM_LENGTH_OF_MESSAGE,
-    ChannelInfo, Privmsg,
+    ChannelInfo, ContactInfo, Privmsg,
 };
 
 mod command;
@@ -20,7 +20,7 @@ pub struct IrcServerConnection<C: AsyncRead + AsyncWrite + Send + Unpin + 'stati
     write_stream: WriteHalf<C>,
     pub peer_address: SocketAddr,
     // msg ids
-    seen_msg_ids: SeenMsgIds,
+    seen_msg_ids: SeenIds,
     privmsgs_buffer: ArcPrivmsgsBuffer,
     // user & channels
     is_nick_init: bool,
@@ -31,7 +31,7 @@ pub struct IrcServerConnection<C: AsyncRead + AsyncWrite + Send + Unpin + 'stati
     nickname: String,
     auto_channels: Vec<String>,
     pub configured_chans: FxHashMap<String, ChannelInfo>,
-    pub configured_contacts: FxHashMap<String, crypto_box::SalsaBox>,
+    pub configured_contacts: FxHashMap<String, ContactInfo>,
     capabilities: FxHashMap<String, bool>,
     // p2p
     p2p: P2pPtr,
@@ -45,12 +45,12 @@ impl<C: AsyncRead + AsyncWrite + Send + Unpin + 'static> IrcServerConnection<C>
     pub fn new(
         write_stream: WriteHalf<C>,
         peer_address: SocketAddr,
-        seen_msg_ids: SeenMsgIds,
+        seen_msg_ids: SeenIds,
         privmsgs_buffer: ArcPrivmsgsBuffer,
         auto_channels: Vec<String>,
         password: String,
         configured_chans: FxHashMap<String, ChannelInfo>,
-        configured_contacts: FxHashMap<String, crypto_box::SalsaBox>,
+        configured_contacts: FxHashMap<String, ContactInfo>,
         p2p: P2pPtr,
         senders: SubscriberPtr<Privmsg>,
         subscriber_id: u64,
@@ -83,8 +83,13 @@ impl<C: AsyncRead + AsyncWrite + Send + Unpin + 'static> IrcServerConnection<C>
         info!("Received msg from P2p network: {:?}", msg);
 
         let mut msg = msg.clone();
-        decrypt_target(&mut msg, self.configured_chans.clone(), self.configured_contacts.clone());
-
+        let mut contact = String::new();
+        decrypt_target(
+            &mut contact,
+            &mut msg,
+            self.configured_chans.clone(),
+            self.configured_contacts.clone(),
+        );
         if msg.target.starts_with('#') {
             // Try to potentially decrypt the incoming message.
             if !self.configured_chans.contains_key(&msg.target) {
@@ -108,10 +113,16 @@ impl<C: AsyncRead + AsyncWrite + Send + Unpin + 'static> IrcServerConnection<C>
 
             self.reply(&msg.to_string()).await?;
             return Ok(())
-        } else if self.is_cap_end && self.is_nick_init && self.nickname == msg.target {
-            if self.configured_contacts.contains_key(&msg.target) {
-                let salt_box = self.configured_contacts.get(&msg.target).unwrap();
+        } else if self.is_cap_end && self.is_nick_init {
+            if !self.configured_contacts.contains_key(&contact) {
+                return Ok(())
+            }
+
+            let contact_info = self.configured_contacts.get(&contact).unwrap();
+            if let Some(salt_box) = &contact_info.salt_box {
                 decrypt_privmsg(salt_box, &mut msg);
+                // This is for /query
+                msg.nickname = contact;
                 info!("Decrypted received message: {:?}", msg);
             }
 

+ 5 - 6
bin/ircd/src/main.rs

@@ -2,7 +2,6 @@ use async_std::{
     net::TcpListener,
     sync::{Arc, Mutex},
 };
-use settings::ContactInfo;
 use std::{fmt, fs::File, net::SocketAddr};
 
 use async_channel::Receiver;
@@ -40,14 +39,14 @@ pub mod rpc;
 pub mod settings;
 
 use crate::{
-    buffers::{ArcPrivmsgsBuffer, PrivmsgsBuffer, RingBuffer, SeenMsgIds, SIZE_OF_MSG_IDSS_BUFFER},
+    buffers::{ArcPrivmsgsBuffer, PrivmsgsBuffer, RingBuffer, SeenIds, SIZE_OF_MSG_IDSS_BUFFER},
     irc_server::IrcServerConnection,
     privmsg::Privmsg,
     protocol_privmsg::ProtocolPrivmsg,
     rpc::JsonRpcInterface,
     settings::{
-        parse_configured_channels, parse_configured_contacts, Args, ChannelInfo, CONFIG_FILE,
-        CONFIG_FILE_CONTENTS,
+        parse_configured_channels, parse_configured_contacts, Args, ChannelInfo, ContactInfo,
+        CONFIG_FILE, CONFIG_FILE_CONTENTS,
     },
 };
 
@@ -143,7 +142,7 @@ async fn start_listening(ircd: Ircd, executor: Arc<Executor<'_>>, settings: Args
 
 struct Ircd {
     // msgs
-    seen_msg_ids: SeenMsgIds,
+    seen_msg_ids: SeenIds,
     privmsgs_buffer: ArcPrivmsgsBuffer,
     // channels
     autojoin_chans: Vec<String>,
@@ -157,7 +156,7 @@ struct Ircd {
 
 impl Ircd {
     fn new(
-        seen_msg_ids: SeenMsgIds,
+        seen_msg_ids: SeenIds,
         privmsgs_buffer: ArcPrivmsgsBuffer,
         autojoin_chans: Vec<String>,
         password: String,

+ 3 - 3
bin/ircd/src/protocol_privmsg.rs

@@ -195,13 +195,13 @@ impl ProtocolPrivmsg {
         Ok(())
     }
 
-    async fn resend_loop(&self) -> Result<()> {
+    async fn resend_loop(self: Arc<Self>) -> Result<()> {
         sleep(SLEEP_TIME_FOR_RESEND).await;
 
-        self.update_unread_msgs();
+        self.update_unread_msgs().await?;
 
         for msg in self.unread_msgs.lock().await.values() {
-            self.channel.send(msg.clone()).await;
+            self.channel.send(msg.clone()).await?;
         }
         Ok(())
     }