From 82b809e23945205ee89638ba7113683bfd12ca24 Mon Sep 17 00:00:00 2001 From: sad Date: Thu, 16 May 2024 00:46:01 -0600 Subject: [PATCH] added non-ssl support --- src/main.rs | 72 ++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4a48b0e..1b9ed36 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,7 +53,6 @@ use mods::vomit::{handle_vomit_command}; #[tokio::main(flavor = "multi_thread", worker_threads = 12)] async fn main() -> Result<(), Box> { - //tokio::spawn(async move { println!("Loading Config..."); let config = loaded_config().expect("Error parsing config.toml"); println!("Config loaded!"); @@ -65,37 +64,40 @@ async fn main() -> Result<(), Box> { let server = format!("{}:{}", configc.server, configc.port); let connection_result = tokio::spawn(async move { let config = configc.clone(); - if config.use_ssl { - if config.use_proxy { - let tcp_stream = proxy_exec(&config).await; - match tcp_stream { - Ok(tcp_stream) => { - let tls_stream = tls_exec(&config, tcp_stream).await; - match tls_stream { - Ok(tls_stream) => { - if let Err(e) = handler(tls_stream, config).await { - println!("Error handling TLS connection: {}", e); - } - }, - Err(e) => { - println!("Error establishing TLS connection: {}", e); - } - } - }, - Err(e) => { - println!("Error connecting to proxy: {}", e); - } + let tcp_stream = if config.use_proxy { + match proxy_exec(&config).await { + Ok(stream) => stream, + Err(e) => { + println!("Error connecting to proxy: {}", e); + return Ok::<(), Box>(()); } - } else { - let tcp_stream = TcpStream::connect(server).await.expect("Error connecting to server"); - let tls_stream = tls_exec(&config, tcp_stream).await.expect("Error establishing TLS connection"); - handler(tls_stream, config).await.unwrap(); } } else { - println!("Non-SSL connection not implemented."); + match TcpStream::connect(server).await { + Ok(stream) => stream, + Err(e) => { + println!("Error connecting to server: {}", e); + return Ok::<(), Box>(()); + } + } + }; + + if config.use_ssl { + println!("Connecting to SSL server..."); + match tls_exec(&config, tcp_stream).await { + Ok(tls_stream) => handler(tls_stream, config).await.unwrap(), + Err(e) => { + println!("Error establishing TLS connection: {}", e); + return Ok::<(), Box>(()); + } + } + } else { + println!("Connecting to Non-SSL server..."); + handler(tcp_stream, config).await.unwrap(); } Ok::<(), Box>(()) }).await.unwrap(); + match connection_result { Ok(_) => { println!("Connection established successfully!"); @@ -139,8 +141,8 @@ async fn proxy_exec(config: &Config) -> Result Result, config: Config) -> Result<(), Box> { - let (reader, writer) = split(tls_stream); +async fn handler(stream: S, config: Config) -> Result<(), Box> where S: AsyncRead + AsyncWrite + Unpin + Send + 'static { + let (reader, writer) = split(stream); let (tx, rx) = mpsc::channel(1000); let read_task = tokio::spawn(async move { @@ -166,13 +168,13 @@ async fn handler(tls_stream: tokio_native_tls::TlsStream, config: Con writemsg(writer, rx, &config, message_buffer).await; }); - let _ = tokio::try_join!(read_task, write_task); - + //let _ = tokio::try_join!(read_task, write_task); + tokio::try_join!(read_task, write_task).map_err(|e| Box::new(e) as Box)?; Ok(()) } /// Read messages from the server -async fn readmsg(mut reader: tokio::io::ReadHalf>, tx: tokio::sync::mpsc::Sender) { +async fn readmsg(mut reader: tokio::io::ReadHalf, tx: tokio::sync::mpsc::Sender) where S: AsyncRead + Unpin { let mut buf = vec![0; 4096]; while let Ok (n) = reader.read(&mut buf).await { if n == 0 { break; } @@ -191,8 +193,7 @@ async fn readmsg(mut reader: tokio::io::ReadHalf>, mut rx: tokio::sync::mpsc::Receiver, config: &Config, mut message_buffer: MessageBuffer) { - +async fn writemsg(mut writer: tokio::io::WriteHalf, mut rx: tokio::sync::mpsc::Receiver, config: &Config, mut message_buffer: MessageBuffer) where S: AsyncWrite + Unpin { let username = config.sasl_username.clone().unwrap(); let password = config.sasl_password.clone().unwrap(); let nickname = config.nickname.clone(); @@ -283,7 +284,6 @@ async fn writemsg(mut writer: tokio::io::WriteHalf