Compare commits

..

No commits in common. "6bf609c1624d1657c9c379bd8e3494da502002f4" and "512a406f43d671f9ccdc9d740382a32c0ad40c81" have entirely different histories.

2 changed files with 31 additions and 53 deletions

View File

@ -10,7 +10,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use colored::*; use colored::*;
use tokio_socks::tcp::Socks5Stream; use tokio_socks::tcp::Socks5Stream;
#[derive(Deserialize, Clone)] #[derive(Deserialize)]
struct Config { struct Config {
server: String, server: String,
port: u16, port: u16,
@ -22,9 +22,6 @@ struct Config {
sasl_password: Option<String>, sasl_password: Option<String>,
capabilities: Option<Vec<String>>, capabilities: Option<Vec<String>>,
reconnect_delay: u64,
reconnect_attempts: u64,
// Proxy // Proxy
use_proxy: bool, use_proxy: bool,
proxy_type: Option<String>, proxy_type: Option<String>,
@ -48,62 +45,43 @@ use mods::ascii::handle_ascii_command;
#[tokio::main(flavor = "multi_thread", worker_threads = 12)] #[tokio::main(flavor = "multi_thread", worker_threads = 12)]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
//tokio::spawn(async move { tokio::spawn(async move {
println!("Loading Config..."); println!("Loading Config...");
let config = loaded_config().expect("Error parsing config.toml"); let config = loaded_config().expect("Error parsing config.toml");
println!("Config loaded!"); println!("Config loaded!");
let mut reconnect_attempts = 0;
while reconnect_attempts < config.reconnect_attempts { let server = format!("{}:{}", config.server, config.port);
let configc = config.clone();
let server = format!("{}:{}", configc.server, configc.port); if config.use_ssl {
let connection_result = tokio::spawn(async move { if config.use_proxy {
let config = configc.clone(); let tcp_stream = proxy_exec(&config).await;
if config.use_ssl { match tcp_stream {
if config.use_proxy { Ok(tcp_stream) => {
let tcp_stream = proxy_exec(&config).await; let tls_stream = tls_exec(&config, tcp_stream).await;
match tcp_stream { match tls_stream {
Ok(tcp_stream) => { Ok(tls_stream) => {
let tls_stream = tls_exec(&config, tcp_stream).await; if let Err(e) = handler(tls_stream, config).await {
match tls_stream { println!("Error handling TLS connection: {}", e);
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) => { Err(e) => {
println!("Error connecting to proxy: {}", e); println!("Error establishing TLS connection: {}", e);
} }
} }
} else { },
let tcp_stream = TcpStream::connect(server).await.expect("Error connecting to server"); Err(e) => {
let tls_stream = tls_exec(&config, tcp_stream).await.expect("Error establishing TLS connection"); println!("Error connecting to proxy: {}", e);
handler(tls_stream, config).await.unwrap();
} }
} else {
println!("Non-SSL connection not implemented.");
}
Ok::<(), Box<dyn std::error::Error + Send>>(())
}).await.unwrap();
match connection_result {
Ok(_) => {
println!("Connection established successfully!");
reconnect_attempts = 0;
},
Err(e) => {
println!("Error handling connection: {}", e);
reconnect_attempts += 1;
tokio::time::sleep(tokio::time::Duration::from_secs(config.reconnect_delay)).await;
} }
} 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.");
} }
println!("Reconnect attempts exceeded. Exiting..."); }).await.unwrap();
Ok(()) Ok(())
} }
@ -239,9 +217,9 @@ async fn writemsg(mut writer: tokio::io::WriteHalf<tokio_native_tls::TlsStream<T
} }
} }
if *cmd == "KICK" { if *cmd == "KICK" {
let channel = parts.get(2).unwrap_or(&""); let channel = parts[2];
let userme = parts.get(3).unwrap_or(&""); let userme = parts[3];
if *userme == nickname { if userme == nickname {
writer.write_all(format!("JOIN {}\r\n", channel).as_bytes()).await.unwrap(); writer.write_all(format!("JOIN {}\r\n", channel).as_bytes()).await.unwrap();
writer.flush().await.unwrap(); writer.flush().await.unwrap();
} }

View File

@ -27,6 +27,7 @@ async fn send_ansi_art<W: AsyncWriteExt + Unpin>(writer: &mut W, file_path: &str
let reader = BufReader::new(file); let reader = BufReader::new(file);
let mut lines = reader.lines(); let mut lines = reader.lines();
while let Some(line) = lines.next_line().await? { while let Some(line) = lines.next_line().await? {
if line.len() > CHUNK_SIZE { if line.len() > CHUNK_SIZE {
@ -76,7 +77,7 @@ pub async fn handle_ascii_command<W: AsyncWriteExt + Unpin>(
if *command_type == "random" && parts.len() == 2 { if *command_type == "random" && parts.len() == 2 {
handle_random(writer, config, channel).await?; handle_random(writer, config, channel).await?;
} else if *command_type == "list"{ } else if *command_type == "list"{
handle_list(writer, config, channel, Some(parts.get(2).unwrap_or(&""))).await?; handle_list(writer, config, channel, Some(parts.get(3).unwrap_or(&""))).await?;
} else { } else {
handle_specific_file(writer, config, channel, &parts).await?; handle_specific_file(writer, config, channel, &parts).await?;
} }
@ -103,11 +104,10 @@ async fn handle_list<W: AsyncWriteExt + Unpin>(
writer: &mut W, writer: &mut W,
config: &Config, config: &Config,
channel: &str, channel: &str,
parts: Option<&str> subdirectory: Option<&str>,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let base_dir = config.ascii_art.clone().unwrap_or_else(|| "ascii_art".to_string()); let base_dir = config.ascii_art.clone().unwrap_or_else(|| "ascii_art".to_string());
let dir = if let Some(subdir) = subdirectory {
let dir = if let Some(subdir) = parts {
format!("{}/{}", base_dir, subdir) format!("{}/{}", base_dir, subdir)
} else { } else {
base_dir base_dir