diff --git a/src/client_handler.rs b/src/client_handler.rs index 04f6479..1365f59 100644 --- a/src/client_handler.rs +++ b/src/client_handler.rs @@ -1,9 +1,9 @@ use crate::helpers::bytes_to_privmsg_base64; use crate::{encryption, helpers}; +use eyre::Result; use pgp::{Deserializable, SignedPublicKey}; use std::collections::HashMap; use std::sync::mpsc::{Receiver, Sender}; -use eyre::Result; pub fn handle_message_from_client( recieved: &str, diff --git a/src/encryption.rs b/src/encryption.rs index 03b5882..fa643aa 100644 --- a/src/encryption.rs +++ b/src/encryption.rs @@ -1,8 +1,8 @@ +use eyre::Result; use pgp::crypto::sym::SymmetricKeyAlgorithm; use pgp::ser::Serialize; use pgp::{Deserializable, Message, SignedPublicKey, SignedSecretKey}; use rand::prelude::*; -use eyre::Result; pub fn encrypt(key: &SignedPublicKey, message: &str) -> Result, pgp::errors::Error> { let message = Message::new_literal("none", message); @@ -15,10 +15,10 @@ pub fn encrypt(key: &SignedPublicKey, message: &str) -> Result, pgp::err pub fn decrypt<'a>( key: &'a SignedSecretKey, - message: Vec, + message: &[u8], password: &'a str, ) -> Result { - let message = Message::from_bytes(message.as_slice())?; // Convert message bytes to message object + let message = Message::from_bytes(message)?; // Convert message bytes to message object let message = message.decrypt(|| password.to_string(), &[key])?.0; // Decrypt let message = message.map(|x| x.unwrap()).collect::>(); // Get all messages let message = &message[0]; // Get first message diff --git a/src/helpers.rs b/src/helpers.rs index 59efd00..1c3915e 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,7 +1,7 @@ use base64::{engine::general_purpose, Engine as _}; -use std::sync::mpsc::{self, Receiver, Sender}; use eyre::Result; use ircparser; +use std::sync::mpsc::{self, Receiver, Sender}; static MAX_LENGTH: usize = 300; @@ -12,7 +12,7 @@ fn forward( server_forward: &str, ) -> Result<(), mpsc::SendError> { if ircparser::parse(message).unwrap()[0].command == "PRIVMSG" { - return Ok(()) + return Ok(()); } stream.send(message.replace(&server_local, server_forward)) } @@ -37,11 +37,7 @@ pub fn bytes_to_privmsg_base64(message: &Vec, reciever: &str) -> String { command } -pub fn send_key( - sender: &Sender, - reciever: &str, - key: &Vec, -) -> Result<()> { +pub fn send_key(sender: &Sender, reciever: &str, key: &Vec) -> Result<()> { sender.send(format!("PRIVMSG {reciever} :START_KEY\r\n"))?; sender.send(bytes_to_privmsg_base64(key, reciever))?; sender.send(format!("PRIVMSG {reciever} :END_KEY\r\n"))?; @@ -76,7 +72,8 @@ pub fn recieve_message_base64( .source .clone() .unwrap_or("".to_string()) - .starts_with(&begin_source_reciever) || recieved.params[0].starts_with("#") + .starts_with(&begin_source_reciever) + || recieved.params[0].starts_with("#") { forward(&recieved_raw, forward_stream, server_local, server_forward)?; continue; diff --git a/src/main.rs b/src/main.rs index 2e45a1c..de80d92 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use eyre::Result; use pgp::{Deserializable, SignedPublicKey, SignedSecretKey}; use std::collections::HashMap; use std::net::{Shutdown, TcpStream}; @@ -5,7 +6,6 @@ use std::sync::mpsc; use std::thread; use std::time::Duration; use std::{env, fs}; -use eyre::Result; mod client_handler; mod encryption; diff --git a/src/server_handler.rs b/src/server_handler.rs index bc3138f..92aea38 100644 --- a/src/server_handler.rs +++ b/src/server_handler.rs @@ -1,8 +1,8 @@ use crate::{encryption, helpers}; +use eyre::Result; use pgp::{Deserializable, SignedPublicKey, SignedSecretKey}; use std::collections::HashMap; use std::sync::mpsc::{self, Receiver, Sender}; -use eyre::Result as Result; fn forward( message: &str, @@ -50,7 +50,7 @@ pub fn handle_message_from_server( &sender, "END_MESSAGE", )?; // Get - let message = encryption::decrypt(&secret_key, message, &passwd)?; // Decrypt + let message = encryption::decrypt(&secret_key, &message, &passwd)?; // Decrypt listener_channel_tx.send(recieved.replace("START_MESSAGE", &message))?; // Send } else if recieved_parsed.params[1] == "START_KEY" {