diff --git a/src/listener_server.rs b/src/listener_server.rs index a0c60dd..24e82d9 100644 --- a/src/listener_server.rs +++ b/src/listener_server.rs @@ -1,15 +1,14 @@ -use std::io::{self, Read, Write}; +use std::io::{ErrorKind, Read, Write}; use std::net::TcpListener; use std::sync::mpsc; -use std::time::Duration; pub fn listen_to_client(tx: mpsc::Sender, rx: mpsc::Receiver, port: &str) { let listener = TcpListener::bind("127.0.0.1:".to_string() + port).unwrap(); let mut stream = listener.accept().unwrap().0; stream - .set_read_timeout(Some(Duration::from_millis(10))) - .expect("Couldn't set timeout!!"); + .set_nonblocking(true) + .expect("Couldn't set nonblocking"); loop { let mut buffer: Vec = Vec::new(); @@ -18,17 +17,17 @@ pub fn listen_to_client(tx: mpsc::Sender, rx: mpsc::Receiver, po while buf[0] != newline { match stream.read(&mut buf) { - Ok(_length) => { - buffer.push(buf[0]); - } + Ok(_length) => buffer.push(buf[0]), Err(_error) => match _error.kind() { - io::ErrorKind::TimedOut => {} - _ => {} + ErrorKind::WouldBlock => {}, + _ => { + dbg!(_error); + } }, } - match rx.try_recv() { Ok(value) => { + dbg!(&value); match stream.write_all(value.as_bytes()) { Ok(_) => {} Err(_e) => println!("Couldn't send {value}"), diff --git a/src/main.rs b/src/main.rs index 55cbe28..fa503f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,9 +10,7 @@ use std::sync::mpsc; use std::thread; use std::{env, fs}; -mod encryption; mod listener_server; -mod message_stream; mod writer_client; macro_rules! try_recv { @@ -127,7 +125,7 @@ fn main() -> Result<(), Box> { }); let mut keys: HashMap = HashMap::new(); - let mut userstring: Option = None; + let mut userstring = String::new(); loop { let recieved = try_recv!(listener_channel_rx); @@ -138,9 +136,12 @@ fn main() -> Result<(), Box> { if command.command == "PRIVMSG" && !command.params[0].starts_with("#") { let reciever = &command.params[0]; if !keys.contains_key(reciever) { - dbg!(recieved); - writer_channel_tx.send(format!("PRIVMSG {reciever} :START_KEY\r\n"))?; + userstring = ircparser::parse(&writer_channel_rx.recv()?).unwrap()[0] + .source + .clone() + .unwrap(); + writer_channel_tx .send(bytes_to_privmsg_base64(public_key.clone(), reciever))?; writer_channel_tx.send(format!("PRIVMSG {reciever} :END_KEY\r\n"))?; @@ -162,7 +163,7 @@ fn main() -> Result<(), Box> { continue; } - foreign_key.push(dbg!(recieved.params[1].clone())); + foreign_key.push(recieved.params[1].clone()); } foreign_key.pop(); println!("Got a foreign key from {reciever}"); @@ -170,8 +171,12 @@ fn main() -> Result<(), Box> { let foreign_key = general_purpose::STANDARD.decode(foreign_key)?; + dbg!("It's decoded"); + let key = SignedPublicKey::from_bytes(foreign_key.as_slice())?; + dbg!("And I now got it's SignedPublicKey format"); + keys.insert(reciever.to_string(), key); } @@ -184,14 +189,12 @@ fn main() -> Result<(), Box> { ))?; writer_channel_tx.send(format!("PRIVMSG {reciever} END_MESSAGE\r\n"))?; } else { - dbg!(&recieved); writer_channel_tx.send(recieved.replace("127.0.0.1", &server))?; } } let recieved = try_recv!(writer_channel_rx); if !recieved.is_empty() { - dbg!(&recieved); let recieved_parsed = &ircparser::parse(&recieved).unwrap()[0]; if recieved_parsed.command != "PRIVMSG" diff --git a/src/writer_client.rs b/src/writer_client.rs index 179b75a..aec3453 100644 --- a/src/writer_client.rs +++ b/src/writer_client.rs @@ -2,7 +2,6 @@ use openssl::ssl::{SslConnector, SslMethod}; use std::io::{ErrorKind, Write}; use std::net::TcpStream; use std::sync::mpsc; -use std::time::Duration; pub fn write_to_server( tcp_stream: TcpStream, @@ -10,15 +9,16 @@ pub fn write_to_server( rx: mpsc::Receiver, tx: mpsc::Sender, ) { - tcp_stream - .set_read_timeout(Some(Duration::from_millis(10))) - .expect("Couldn't set timeout!!"); - let connector = SslConnector::builder(SslMethod::tls()).unwrap().build(); let mut stream = connector - .connect(&server, &tcp_stream) + .connect(&server, tcp_stream) .expect("Couldn't start TLS"); + stream + .get_mut() + .set_nonblocking(true) + .expect("Failed to set nonblocking"); + loop { let mut buffer: Vec = Vec::new(); let mut buf: [u8; 1] = [0]; @@ -27,15 +27,15 @@ pub fn write_to_server( while buf[0] != newline { match stream.ssl_read(&mut buf) { Ok(_length) => { - buffer.push(buf[0]); + if _length > 0 { + buffer.push(buf[0]); + } } Err(_error) => match _error.io_error() { None => { dbg!(_error); - panic!("TLS Error") } Some(error) => match error.kind() { - ErrorKind::TimedOut => {} ErrorKind::WouldBlock => {} _ => { dbg!(error.kind()); @@ -44,18 +44,13 @@ pub fn write_to_server( }, }, } - - match rx.try_recv() { - Ok(value) => { - match stream.write_all(value.as_bytes()) { - Ok(_) => {} - Err(_e) => println!("Couldn't send {value}"), - }; - } - Err(_e) => {} - } + let value = rx.try_recv().unwrap_or("".to_string()); + match stream.write_all(value.as_bytes()) { + Ok(_) => {} + Err(_e) => println!("Couldn't send {value}"), + }; } - let _ = tx.send(String::from_utf8_lossy(&buffer).to_string()); + let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string())); } }