From 83b10eb3d576e159fbc5333b626a13a68e62cb99 Mon Sep 17 00:00:00 2001 From: vanten-s Date: Thu, 19 Oct 2023 17:08:05 +0200 Subject: [PATCH] Changed from a .env to config file --- .gitignore | 3 -- Cargo.toml | 4 ++- src/client_handler.rs | 8 ++--- src/encryption.rs | 2 +- src/helpers.rs | 5 ++-- src/listener_server.rs | 4 +-- src/main.rs | 66 ++++++++++++++++++++++++++++++++---------- 7 files changed, 63 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 96f8ab2..96ef6c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ /target -secret.gpg -public.gpg -.env Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index 6dd6b99..b01759d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,10 +6,12 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +argparse = "0.2.2" base64 = "0.21.4" -dotenv = "0.15.0" +dirs = "5.0.1" eyre = "0.6.8" ircparser = "0.2.1" openssl = "0.10" pgp = "0.10.2" rand = "0.8.5" +toml = "0.8.2" diff --git a/src/client_handler.rs b/src/client_handler.rs index 1365f59..52932ee 100644 --- a/src/client_handler.rs +++ b/src/client_handler.rs @@ -17,7 +17,7 @@ pub fn handle_message_from_client( ) -> Result<()> { let command = &ircparser::parse(recieved).expect("Got an invalid IRC instruction")[0]; - if command.command == "PRIVMSG" && !command.params[0].starts_with("#") { + if command.command == "PRIVMSG" && !command.params[0].starts_with('#') { let other = &command.params[0]; if !keys.contains_key(other) { @@ -27,7 +27,7 @@ pub fn handle_message_from_client( listener_channel_tx, "127.0.0.1", server, - &other, + other, "END_KEY", )?; let key = SignedPublicKey::from_bytes(key.as_slice())?; @@ -38,12 +38,12 @@ pub fn handle_message_from_client( writer_channel_tx.send(format!("PRIVMSG {other} START_MESSAGE\r\n"))?; writer_channel_tx.send(bytes_to_privmsg_base64( - &encryption::encrypt(&foreign_key, &command.params[1])?, + &encryption::encrypt(foreign_key, &command.params[1])?, other, ))?; writer_channel_tx.send(format!("PRIVMSG {other} END_MESSAGE\r\n"))?; } else { - writer_channel_tx.send(recieved.replace("127.0.0.1", &server))?; + writer_channel_tx.send(recieved.replace("127.0.0.1", server))?; } Ok(()) } diff --git a/src/encryption.rs b/src/encryption.rs index fa643aa..c94993d 100644 --- a/src/encryption.rs +++ b/src/encryption.rs @@ -10,7 +10,7 @@ pub fn encrypt(key: &SignedPublicKey, message: &str) -> Result, pgp::err let message = message.encrypt_to_keys(&mut rng, SymmetricKeyAlgorithm::AES128, &[key])?; - Ok(message.to_bytes()?) + message.to_bytes() } pub fn decrypt<'a>( diff --git a/src/helpers.rs b/src/helpers.rs index b9976bd..412f820 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,6 +1,5 @@ use base64::{engine::general_purpose, Engine as _}; use eyre::Result; -use ircparser; use std::sync::mpsc::{self, Receiver, Sender}; #[derive(Debug)] @@ -45,7 +44,7 @@ fn forward( match ircparser::parse(&message) { Ok(val) => match val[0].command.as_str() { "PRIVMSG" => stream.send(message), - _ => stream.send(message.replace(&server_local, server_forward)), + _ => stream.send(message.replace(server_local, server_forward)), }, Err(_) => stream.send(message.replace(server_local, server_forward)), } @@ -113,7 +112,7 @@ pub fn recieve_message_base64( .clone() .unwrap_or("".to_string()) .starts_with(&begin_source_reciever) - || recieved.params[0].starts_with("#") + || recieved.params[0].starts_with('#') { forward(recieved_raw, forward_stream, server_local, server_forward)?; continue; diff --git a/src/listener_server.rs b/src/listener_server.rs index 40ceae1..0fbd68a 100644 --- a/src/listener_server.rs +++ b/src/listener_server.rs @@ -31,13 +31,13 @@ fn stream_handler(tx: &mpsc::Sender, rx: &mpsc::Receiver, mut st } }; } - Err(TryRecvError::Empty) => {}, + Err(TryRecvError::Empty) => {} Err(TryRecvError::Disconnected) => return, } thread::sleep(Duration::from_micros(100)); } - let _ = tx.send(String::from_utf8_lossy(&buffer).to_string()); + let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string())); } } diff --git a/src/main.rs b/src/main.rs index 4780cc4..06093fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ -use dotenv::{dotenv, vars}; +use argparse::{ArgumentParser, Store}; +use dirs::config_local_dir; use eyre::Result; use pgp::{Deserializable, SignedPublicKey, SignedSecretKey}; use std::collections::HashMap; @@ -7,6 +8,7 @@ use std::net::{Shutdown, TcpStream}; use std::sync::mpsc; use std::thread; use std::time::Duration; +use toml::Table; mod client_handler; mod encryption; @@ -16,29 +18,63 @@ mod server_handler; mod writer_client; fn main() -> Result<()> { - dotenv().expect("Couldn't load .env. It probably doesn't exist"); - let mut vars_hashmap = HashMap::new(); + let config_file = config_local_dir() + .expect("Couldn't get config directory") + .join("e2e-irc/config.toml"); - for var in vars() { - vars_hashmap.insert(var.0, var.1); + if !config_file.exists() { + panic!("Create a config file at {}", config_file.display()); } - let server = &vars_hashmap["SERVER"]; + let parsed_config = String::from_utf8_lossy(&fs::read(config_file)?).parse::()?; - let default_passwd = String::new(); + let public_key_location = parsed_config + .get("public_key") + .expect("Coudln't get public_key. Try creating it in the config") + .as_str() + .expect("Couldn't convert public_key to str"); + let secret_key_location = parsed_config + .get("secret_key") + .expect("Coudln't get secret_key. Try creating it in the config") + .as_str() + .expect("Couldn't convert secret_key to str"); - let port = match vars_hashmap.get("PORT") { - Some(val) => val, - None => "6666", + let default_password = toml::Value::String(String::new()); + + let passwd = parsed_config + .get("passwd") + .unwrap_or(&default_password) + .as_str() + .expect("Coudln't convert passwd to str"); + + let mut server = "irc.vanten-s.com".to_string(); + let mut port = "6666".to_string(); + let mut server_port = "6697".to_string(); + + { + let mut ap = ArgumentParser::new(); + ap.set_description("Encrypted IRC Bouncer"); + ap.refer(&mut server).add_option( + &["-s", "--server"], + Store, + "The Server The Bouncer Connects To", + ); + ap.refer(&mut port) + .add_option(&["-p", "--port"], Store, "The Port The Bouncer Binds To"); + ap.refer(&mut server_port).add_option( + &["--sp", "--server-port"], + Store, + "The TLS Enabled Port Of The Server", + ); + ap.parse_args_or_exit(); } - .to_string(); - let passwd = vars_hashmap.get("PASSWD").unwrap_or(&default_passwd); + let server = &server; - let stream = TcpStream::connect(format!("{server}:6697"))?; + let stream = TcpStream::connect(format!("{server}:{server_port}"))?; - let public_key = fs::read("public.gpg")?; - let secret_key = SignedSecretKey::from_bytes(fs::read("secret.gpg")?.as_slice())?; + let public_key = fs::read(public_key_location)?; + let secret_key = SignedSecretKey::from_bytes(fs::read(secret_key_location)?.as_slice())?; let reader_stream = match stream.try_clone() { Ok(stream) => stream,