2023-10-12 21:26:52 +02:00
|
|
|
use eyre::Result;
|
2023-10-12 13:59:07 +02:00
|
|
|
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
2023-10-05 14:13:19 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::net::{Shutdown, TcpStream};
|
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::thread;
|
2023-10-07 14:19:58 +02:00
|
|
|
use std::time::Duration;
|
2023-10-05 14:13:19 +02:00
|
|
|
use std::{env, fs};
|
|
|
|
|
2023-10-12 13:59:07 +02:00
|
|
|
mod client_handler;
|
|
|
|
mod encryption;
|
|
|
|
mod helpers;
|
2023-10-05 14:13:19 +02:00
|
|
|
mod listener_server;
|
2023-10-12 13:59:07 +02:00
|
|
|
mod server_handler;
|
2023-10-05 14:13:19 +02:00
|
|
|
mod writer_client;
|
|
|
|
|
2023-10-12 13:59:07 +02:00
|
|
|
fn main() -> Result<()> {
|
2023-10-05 22:57:26 +02:00
|
|
|
let mut args = env::args().collect::<Vec<String>>();
|
2023-10-05 14:13:19 +02:00
|
|
|
|
2023-10-05 21:16:34 +02:00
|
|
|
let server = args[1].clone();
|
2023-10-05 22:57:26 +02:00
|
|
|
let port = args[2].clone();
|
|
|
|
|
2023-10-05 21:16:34 +02:00
|
|
|
let default_password = String::new();
|
2023-10-05 22:57:26 +02:00
|
|
|
let passwd = args.pop().unwrap_or(default_password);
|
2023-10-05 14:13:19 +02:00
|
|
|
|
|
|
|
let stream = TcpStream::connect(format!("{server}:6697"))?;
|
|
|
|
|
2023-10-05 21:16:34 +02:00
|
|
|
let public_key = fs::read("public.gpg")?;
|
|
|
|
let secret_key = SignedSecretKey::from_bytes(fs::read("secret.gpg")?.as_slice())?;
|
2023-10-05 14:13:19 +02:00
|
|
|
|
|
|
|
let reader_stream = match stream.try_clone() {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(_error) => {
|
|
|
|
let _ = stream.shutdown(Shutdown::Both);
|
|
|
|
panic!("Failed to create the reader stream")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let writer_stream = match stream.try_clone() {
|
|
|
|
Ok(stream) => stream,
|
|
|
|
Err(_error) => {
|
|
|
|
let _ = stream.shutdown(Shutdown::Both);
|
|
|
|
let _ = reader_stream.shutdown(Shutdown::Both);
|
|
|
|
panic!("Failed to create the writer stream")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let (listener_channel_send_tx, listener_channel_rx) = mpsc::channel();
|
|
|
|
let (listener_channel_tx, listener_channel_recv_rx) = mpsc::channel();
|
|
|
|
|
|
|
|
let (writer_channel_tx, writer_channel_send_rx) = mpsc::channel();
|
|
|
|
let (writer_channel_recv_tx, writer_channel_rx) = mpsc::channel();
|
|
|
|
|
2023-10-05 22:57:26 +02:00
|
|
|
thread::spawn(move || {
|
|
|
|
listener_server::listen_to_client(listener_channel_send_tx, listener_channel_recv_rx, &port)
|
2023-10-05 14:13:19 +02:00
|
|
|
});
|
2023-10-05 22:57:26 +02:00
|
|
|
let tmp_server = server.clone();
|
2023-10-05 14:13:19 +02:00
|
|
|
thread::spawn(|| {
|
|
|
|
writer_client::write_to_server(
|
|
|
|
writer_stream,
|
2023-10-05 22:57:26 +02:00
|
|
|
tmp_server,
|
2023-10-05 14:13:19 +02:00
|
|
|
writer_channel_send_rx,
|
|
|
|
writer_channel_recv_tx,
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2023-10-05 21:16:34 +02:00
|
|
|
let mut keys: HashMap<String, SignedPublicKey> = HashMap::new();
|
2023-10-05 14:13:19 +02:00
|
|
|
|
|
|
|
loop {
|
2023-10-12 13:59:07 +02:00
|
|
|
match listener_channel_rx.try_recv() {
|
|
|
|
Ok(message) => client_handler::handle_message_from_client(
|
|
|
|
&message,
|
|
|
|
&public_key,
|
|
|
|
&server,
|
|
|
|
&mut keys,
|
|
|
|
&writer_channel_tx,
|
|
|
|
&writer_channel_rx,
|
|
|
|
&listener_channel_tx,
|
|
|
|
&listener_channel_rx,
|
|
|
|
)?,
|
|
|
|
Err(error) => match error {
|
|
|
|
mpsc::TryRecvError::Empty => {}
|
|
|
|
mpsc::TryRecvError::Disconnected => panic!("listener_channel_rx disconnected"),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
match writer_channel_rx.try_recv() {
|
|
|
|
Ok(message) => server_handler::handle_message_from_server(
|
|
|
|
&message,
|
|
|
|
&public_key,
|
|
|
|
&secret_key,
|
|
|
|
&server,
|
|
|
|
&passwd,
|
|
|
|
&mut keys,
|
|
|
|
&writer_channel_tx,
|
|
|
|
&writer_channel_rx,
|
|
|
|
&listener_channel_tx,
|
|
|
|
&listener_channel_rx,
|
|
|
|
)?,
|
|
|
|
Err(error) => match error {
|
|
|
|
mpsc::TryRecvError::Empty => {}
|
|
|
|
mpsc::TryRecvError::Disconnected => panic!("writer_channel_rx disconnected"),
|
|
|
|
},
|
|
|
|
};
|
2023-10-05 22:57:26 +02:00
|
|
|
|
2023-10-12 13:59:07 +02:00
|
|
|
thread::sleep(Duration::from_millis(100));
|
2023-10-05 14:13:19 +02:00
|
|
|
}
|
|
|
|
}
|