Merge pull request 'dev-laptop' (#1) from dev-laptop into main
Reviewed-on: #1
This commit is contained in:
commit
87f2b142c9
|
@ -1,15 +1,14 @@
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{ErrorKind, Read, Write};
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, port: &str) {
|
pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, port: &str) {
|
||||||
let listener = TcpListener::bind("127.0.0.1:".to_string() + port).unwrap();
|
let listener = TcpListener::bind("127.0.0.1:".to_string() + port).unwrap();
|
||||||
let mut stream = listener.accept().unwrap().0;
|
let mut stream = listener.accept().unwrap().0;
|
||||||
|
|
||||||
stream
|
stream
|
||||||
.set_read_timeout(Some(Duration::from_millis(10)))
|
.set_nonblocking(true)
|
||||||
.expect("Couldn't set timeout!!");
|
.expect("Couldn't set nonblocking");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
|
@ -18,15 +17,14 @@ pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, po
|
||||||
|
|
||||||
while buf[0] != newline {
|
while buf[0] != newline {
|
||||||
match stream.read(&mut buf) {
|
match stream.read(&mut buf) {
|
||||||
Ok(_length) => {
|
Ok(_length) => buffer.push(buf[0]),
|
||||||
buffer.push(buf[0]);
|
|
||||||
}
|
|
||||||
Err(_error) => match _error.kind() {
|
Err(_error) => match _error.kind() {
|
||||||
io::ErrorKind::TimedOut => {}
|
ErrorKind::WouldBlock => {},
|
||||||
_ => {}
|
_ => {
|
||||||
|
dbg!(_error);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
match rx.try_recv() {
|
match rx.try_recv() {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
match stream.write_all(value.as_bytes()) {
|
match stream.write_all(value.as_bytes()) {
|
||||||
|
|
43
src/main.rs
43
src/main.rs
|
@ -8,11 +8,10 @@ use std::collections::HashMap;
|
||||||
use std::net::{Shutdown, TcpStream};
|
use std::net::{Shutdown, TcpStream};
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
|
use std::time::Duration;
|
||||||
use std::{env, fs};
|
use std::{env, fs};
|
||||||
|
|
||||||
mod encryption;
|
|
||||||
mod listener_server;
|
mod listener_server;
|
||||||
mod message_stream;
|
|
||||||
mod writer_client;
|
mod writer_client;
|
||||||
|
|
||||||
macro_rules! try_recv {
|
macro_rules! try_recv {
|
||||||
|
@ -35,7 +34,7 @@ fn bytes_to_privmsg_base64(message: Vec<u8>, reciever: &str) -> String {
|
||||||
.encode(message)
|
.encode(message)
|
||||||
.chars()
|
.chars()
|
||||||
.collect::<Vec<char>>()
|
.collect::<Vec<char>>()
|
||||||
.chunks(50)
|
.chunks(500)
|
||||||
.map(|c| c.iter().collect::<String>())
|
.map(|c| c.iter().collect::<String>())
|
||||||
{
|
{
|
||||||
command.push_str(&format!("PRIVMSG {reciever} :{line}\r\n"));
|
command.push_str(&format!("PRIVMSG {reciever} :{line}\r\n"));
|
||||||
|
@ -127,7 +126,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut keys: HashMap<String, SignedPublicKey> = HashMap::new();
|
let mut keys: HashMap<String, SignedPublicKey> = HashMap::new();
|
||||||
let mut userstring: Option<String> = None;
|
let mut userstring = String::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let recieved = try_recv!(listener_channel_rx);
|
let recieved = try_recv!(listener_channel_rx);
|
||||||
|
@ -138,9 +137,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
if command.command == "PRIVMSG" && !command.params[0].starts_with("#") {
|
if command.command == "PRIVMSG" && !command.params[0].starts_with("#") {
|
||||||
let reciever = &command.params[0];
|
let reciever = &command.params[0];
|
||||||
if !keys.contains_key(reciever) {
|
if !keys.contains_key(reciever) {
|
||||||
dbg!(recieved);
|
|
||||||
|
|
||||||
writer_channel_tx.send(format!("PRIVMSG {reciever} :START_KEY\r\n"))?;
|
writer_channel_tx.send(format!("PRIVMSG {reciever} :START_KEY\r\n"))?;
|
||||||
|
|
||||||
writer_channel_tx
|
writer_channel_tx
|
||||||
.send(bytes_to_privmsg_base64(public_key.clone(), reciever))?;
|
.send(bytes_to_privmsg_base64(public_key.clone(), reciever))?;
|
||||||
writer_channel_tx.send(format!("PRIVMSG {reciever} :END_KEY\r\n"))?;
|
writer_channel_tx.send(format!("PRIVMSG {reciever} :END_KEY\r\n"))?;
|
||||||
|
@ -162,16 +160,22 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreign_key.push(dbg!(recieved.params[1].clone()));
|
foreign_key.push(recieved.params[1].clone());
|
||||||
}
|
}
|
||||||
foreign_key.pop();
|
foreign_key.pop();
|
||||||
|
|
||||||
println!("Got a foreign key from {reciever}");
|
println!("Got a foreign key from {reciever}");
|
||||||
let foreign_key = foreign_key.concat();
|
let foreign_key = foreign_key.concat();
|
||||||
|
dbg!(&foreign_key);
|
||||||
|
|
||||||
let foreign_key = general_purpose::STANDARD.decode(foreign_key)?;
|
let foreign_key = general_purpose::STANDARD.decode(foreign_key)?;
|
||||||
|
|
||||||
|
dbg!("It's decoded");
|
||||||
|
|
||||||
let key = SignedPublicKey::from_bytes(foreign_key.as_slice())?;
|
let key = SignedPublicKey::from_bytes(foreign_key.as_slice())?;
|
||||||
|
|
||||||
|
dbg!("And I now got it's SignedPublicKey format");
|
||||||
|
|
||||||
keys.insert(reciever.to_string(), key);
|
keys.insert(reciever.to_string(), key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,14 +188,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
))?;
|
))?;
|
||||||
writer_channel_tx.send(format!("PRIVMSG {reciever} END_MESSAGE\r\n"))?;
|
writer_channel_tx.send(format!("PRIVMSG {reciever} END_MESSAGE\r\n"))?;
|
||||||
} else {
|
} else {
|
||||||
dbg!(&recieved);
|
|
||||||
writer_channel_tx.send(recieved.replace("127.0.0.1", &server))?;
|
writer_channel_tx.send(recieved.replace("127.0.0.1", &server))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let recieved = try_recv!(writer_channel_rx);
|
let recieved = try_recv!(writer_channel_rx);
|
||||||
if !recieved.is_empty() {
|
if !recieved.is_empty() {
|
||||||
dbg!(&recieved);
|
|
||||||
let recieved_parsed = &ircparser::parse(&recieved).unwrap()[0];
|
let recieved_parsed = &ircparser::parse(&recieved).unwrap()[0];
|
||||||
|
|
||||||
if recieved_parsed.command != "PRIVMSG"
|
if recieved_parsed.command != "PRIVMSG"
|
||||||
|
@ -205,6 +207,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dbg!(&recieved_parsed);
|
||||||
|
|
||||||
if recieved_parsed.params[1] == "START_MESSAGE" {
|
if recieved_parsed.params[1] == "START_MESSAGE" {
|
||||||
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
||||||
let sample_privmsg = recieved.clone();
|
let sample_privmsg = recieved.clone();
|
||||||
|
@ -222,7 +226,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
.unwrap_or("".to_string())
|
.unwrap_or("".to_string())
|
||||||
.starts_with(&begin_source_reciever)
|
.starts_with(&begin_source_reciever)
|
||||||
{
|
{
|
||||||
listener_channel_tx.send(recieved_raw)?;
|
listener_channel_tx.send(recieved_raw.replace(&server, "127.0.0.1"))?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,6 +241,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
listener_channel_tx
|
listener_channel_tx
|
||||||
.send(sample_privmsg.replace("START_MESSAGE", &decrypted_message))?;
|
.send(sample_privmsg.replace("START_MESSAGE", &decrypted_message))?;
|
||||||
} else if recieved_parsed.params[1] == "START_KEY" {
|
} else if recieved_parsed.params[1] == "START_KEY" {
|
||||||
|
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
||||||
|
let to_send = bytes_to_privmsg_base64(public_key.clone(), &reciever);
|
||||||
|
writer_channel_tx.send(to_send)?;
|
||||||
|
writer_channel_tx.send(format!("PRIVMSG {reciever} END_KEY\r\n"))?;
|
||||||
|
|
||||||
|
dbg!("Started Getting a key");
|
||||||
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
||||||
let mut foreign_key: Vec<String> = Vec::new();
|
let mut foreign_key: Vec<String> = Vec::new();
|
||||||
while !foreign_key.contains(&"END_KEY".to_string()) {
|
while !foreign_key.contains(&"END_KEY".to_string()) {
|
||||||
|
@ -251,26 +261,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
.unwrap_or("".to_string())
|
.unwrap_or("".to_string())
|
||||||
.starts_with(&begin_source_reciever)
|
.starts_with(&begin_source_reciever)
|
||||||
{
|
{
|
||||||
listener_channel_tx.send(recieved_raw)?;
|
listener_channel_tx.send(recieved_raw.replace(&server, "127.0.0.1"))?;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreign_key.push(recieved.params[1].clone());
|
foreign_key.push(recieved.params[1].clone());
|
||||||
}
|
}
|
||||||
foreign_key.pop();
|
foreign_key.pop();
|
||||||
println!("Got a foreign key from {reciever}");
|
dbg!("Got a foreign key from");
|
||||||
|
dbg!(&reciever);
|
||||||
|
|
||||||
let foreign_key = foreign_key.concat();
|
let foreign_key = foreign_key.concat();
|
||||||
|
|
||||||
let foreign_key = general_purpose::STANDARD.decode(foreign_key)?;
|
let foreign_key = general_purpose::STANDARD.decode(foreign_key)?;
|
||||||
|
dbg!("Decoded the key");
|
||||||
|
|
||||||
let key = SignedPublicKey::from_bytes(foreign_key.as_slice())?;
|
let key = SignedPublicKey::from_bytes(foreign_key.as_slice())?;
|
||||||
|
dbg!("Deserialized the key");
|
||||||
|
|
||||||
keys.insert(reciever.to_string(), key);
|
keys.insert(reciever.to_string(), key);
|
||||||
|
|
||||||
let reciever = get_nick(&recieved_parsed.source.clone().unwrap());
|
|
||||||
let to_send = bytes_to_privmsg_base64(public_key.clone(), &reciever);
|
|
||||||
writer_channel_tx.send(to_send)?;
|
|
||||||
writer_channel_tx.send(format!("PRIVMSG {reciever} END_KEY\r\n"))?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ use openssl::ssl::{SslConnector, SslMethod};
|
||||||
use std::io::{ErrorKind, Write};
|
use std::io::{ErrorKind, Write};
|
||||||
use std::net::TcpStream;
|
use std::net::TcpStream;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
pub fn write_to_server(
|
pub fn write_to_server(
|
||||||
tcp_stream: TcpStream,
|
tcp_stream: TcpStream,
|
||||||
|
@ -10,15 +9,16 @@ pub fn write_to_server(
|
||||||
rx: mpsc::Receiver<String>,
|
rx: mpsc::Receiver<String>,
|
||||||
tx: mpsc::Sender<String>,
|
tx: mpsc::Sender<String>,
|
||||||
) {
|
) {
|
||||||
tcp_stream
|
|
||||||
.set_read_timeout(Some(Duration::from_millis(10)))
|
|
||||||
.expect("Couldn't set timeout!!");
|
|
||||||
|
|
||||||
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
|
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
|
||||||
let mut stream = connector
|
let mut stream = connector
|
||||||
.connect(&server, &tcp_stream)
|
.connect(&server, tcp_stream)
|
||||||
.expect("Couldn't start TLS");
|
.expect("Couldn't start TLS");
|
||||||
|
|
||||||
|
stream
|
||||||
|
.get_mut()
|
||||||
|
.set_nonblocking(true)
|
||||||
|
.expect("Failed to set nonblocking");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
let mut buf: [u8; 1] = [0];
|
let mut buf: [u8; 1] = [0];
|
||||||
|
@ -27,15 +27,15 @@ pub fn write_to_server(
|
||||||
while buf[0] != newline {
|
while buf[0] != newline {
|
||||||
match stream.ssl_read(&mut buf) {
|
match stream.ssl_read(&mut buf) {
|
||||||
Ok(_length) => {
|
Ok(_length) => {
|
||||||
buffer.push(buf[0]);
|
if _length > 0 {
|
||||||
|
buffer.push(buf[0]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(_error) => match _error.io_error() {
|
Err(_error) => match _error.io_error() {
|
||||||
None => {
|
None => {
|
||||||
dbg!(_error);
|
dbg!(_error);
|
||||||
panic!("TLS Error")
|
|
||||||
}
|
}
|
||||||
Some(error) => match error.kind() {
|
Some(error) => match error.kind() {
|
||||||
ErrorKind::TimedOut => {}
|
|
||||||
ErrorKind::WouldBlock => {}
|
ErrorKind::WouldBlock => {}
|
||||||
_ => {
|
_ => {
|
||||||
dbg!(error.kind());
|
dbg!(error.kind());
|
||||||
|
@ -44,18 +44,13 @@ pub fn write_to_server(
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
let value = rx.try_recv().unwrap_or("".to_string());
|
||||||
match rx.try_recv() {
|
match stream.write_all(value.as_bytes()) {
|
||||||
Ok(value) => {
|
Ok(_) => {}
|
||||||
match stream.write_all(value.as_bytes()) {
|
Err(_e) => println!("Couldn't send {value}"),
|
||||||
Ok(_) => {}
|
};
|
||||||
Err(_e) => println!("Couldn't send {value}"),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Err(_e) => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let _ = tx.send(String::from_utf8_lossy(&buffer).to_string());
|
let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue