fix threading in invade

This commit is contained in:
legitnull 2023-02-18 14:01:55 -07:00
parent 6acbbf1413
commit 9f1a92d1b3
2 changed files with 15 additions and 21 deletions

View File

@ -74,7 +74,7 @@ fn main() {
// RESPOND TO PINGS // RESPOND TO PINGS
if message.starts_with("PING") { if message.starts_with("PING") {
println!("[%] PONG"); println!("[%] PONG {}", config.nick);
ssl_stream.write_all("PONG ircd.chat\r\n".as_bytes()).unwrap(); ssl_stream.write_all("PONG ircd.chat\r\n".as_bytes()).unwrap();
continue; // skip processing the PING message further continue; // skip processing the PING message further
} }

View File

@ -1,7 +1,7 @@
use crate::modules::Command; use crate::modules::Command;
use std::cell::RefCell; use std::cell::RefCell;
use std::io::{BufRead, BufReader, Write}; use std::io::{Write};
use std::net::TcpStream; use std::net::TcpStream;
use std::rc::Rc; use std::rc::Rc;
@ -9,8 +9,6 @@ use openssl::ssl::{SslConnector, SslMethod};
use serde::Deserialize; use serde::Deserialize;
use toml::Value; use toml::Value;
#[derive(Clone, Deserialize)] #[derive(Clone, Deserialize)]
struct Config { struct Config {
invaders: Vec<String>, invaders: Vec<String>,
@ -39,34 +37,30 @@ impl Command for InvadeCommand {
let thread_invader = invader.to_string(); let thread_invader = invader.to_string();
let config_clone = config.clone(); let config_clone = config.clone();
let screaming = scream.to_string(); let screaming = scream.to_string();
std::thread::spawn(move || { std::thread::spawn(move || {
let stream = TcpStream::connect(format!("{}:{}", config_clone.server, config_clone.port)).unwrap(); let stream = TcpStream::connect((config_clone.server.as_str(), config_clone.port)).unwrap();
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build(); let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
let ssl_stream = connector.connect(&config_clone.server, stream).unwrap(); let mut ssl_stream = connector.connect(config_clone.server.as_str(), stream).unwrap();
let ssl_stream = Rc::new(RefCell::new(ssl_stream));
let nick_command = format!("NICK {}\r\n", thread_invader); let nick_command = format!("NICK {}\r\n", thread_invader);
let user_command = format!("USER {} 0 * :{}\r\n", thread_invader, thread_invader); let user_command = format!("USER {} 0 * :{}\r\n", thread_invader, thread_invader);
ssl_stream.borrow_mut().write_all(nick_command.as_bytes()).unwrap(); ssl_stream.write_all(nick_command.as_bytes()).unwrap();
ssl_stream.borrow_mut().write_all(user_command.as_bytes()).unwrap(); ssl_stream.write_all(user_command.as_bytes()).unwrap();
let join_command = format!("JOIN {} \r\n", thread_channel); let join_command = format!("JOIN {} \r\n", thread_channel);
ssl_stream.borrow_mut().write_all(join_command.as_bytes()).unwrap(); ssl_stream.write_all(join_command.as_bytes()).unwrap();
let msg = format!("PRIVMSG {} :{}\r\n", thread_channel, screaming); let msg = format!("PRIVMSG {} :{}\r\n", thread_channel, screaming);
ssl_stream.borrow_mut().write_all(msg.as_bytes()).unwrap(); ssl_stream.write_all(msg.as_bytes()).unwrap();
loop { loop {
let mut ssl_stream_ref = ssl_stream.borrow_mut(); let mut buffer = [0; 512];
let mut reader = BufReader::new(&mut *ssl_stream_ref); match ssl_stream.ssl_read(&mut buffer) {
let mut message = String::new();
match reader.read_line(&mut message) {
Ok(0) => break, Ok(0) => break,
Ok(_) => { Ok(n) => {
let message = String::from_utf8_lossy(&buffer[..n]);
if message.starts_with("PING") { if message.starts_with("PING") {
let response = message.replace("PING", "PONG"); let response = message.replace("PING", "PONG");
ssl_stream.borrow_mut().write_all(response.as_bytes()).unwrap(); println!("[%] PONG {}", thread_invader);
ssl_stream.write_all(response.as_bytes()).unwrap();
} }
} }
Err(e) => { Err(e) => {
@ -83,4 +77,4 @@ impl Command for InvadeCommand {
response response
} }
} }