You can now reconnect to the server
This commit is contained in:
parent
d38da05d05
commit
7c5bc94ade
|
@ -54,5 +54,6 @@ pub fn listen_to_client(tx: mpsc::Sender<String>, rx: mpsc::Receiver<String>, po
|
||||||
|
|
||||||
stream_handler(&tx, &rx, stream);
|
stream_handler(&tx, &rx, stream);
|
||||||
println!("Closed connection with {ip}");
|
println!("Closed connection with {ip}");
|
||||||
|
let _ = tx.send("DUMMY CLOSE_CONNECTION".to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
42
src/main.rs
42
src/main.rs
|
@ -4,7 +4,6 @@ use eyre::Result;
|
||||||
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
use pgp::{Deserializable, SignedPublicKey, SignedSecretKey};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::net::{Shutdown, TcpStream};
|
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
@ -55,7 +54,11 @@ fn main() -> Result<()> {
|
||||||
let mut ap = ArgumentParser::new();
|
let mut ap = ArgumentParser::new();
|
||||||
ap.set_description("Encrypted IRC Bouncer");
|
ap.set_description("Encrypted IRC Bouncer");
|
||||||
ap.refer(&mut server)
|
ap.refer(&mut server)
|
||||||
.add_argument("server", Store, "The Address Of The Server The Bouncer Connects To")
|
.add_argument(
|
||||||
|
"server",
|
||||||
|
Store,
|
||||||
|
"The Address Of The Server The Bouncer Connects To",
|
||||||
|
)
|
||||||
.required();
|
.required();
|
||||||
ap.refer(&mut port)
|
ap.refer(&mut port)
|
||||||
.add_option(&["-p", "--port"], Store, "The Port The Bouncer Binds To");
|
.add_option(&["-p", "--port"], Store, "The Port The Bouncer Binds To");
|
||||||
|
@ -67,44 +70,25 @@ fn main() -> Result<()> {
|
||||||
ap.parse_args_or_exit();
|
ap.parse_args_or_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
let server = &server;
|
|
||||||
|
|
||||||
let stream = TcpStream::connect(format!("{server}:{server_port}"))?;
|
|
||||||
|
|
||||||
let public_key = fs::read(public_key_location)?;
|
let public_key = fs::read(public_key_location)?;
|
||||||
let secret_key = SignedSecretKey::from_bytes(fs::read(secret_key_location)?.as_slice())?;
|
let secret_key = SignedSecretKey::from_bytes(fs::read(secret_key_location)?.as_slice())?;
|
||||||
|
|
||||||
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_send_tx, listener_channel_rx) = mpsc::channel();
|
||||||
let (listener_channel_tx, listener_channel_recv_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_tx, writer_channel_send_rx) = mpsc::channel();
|
||||||
let (writer_channel_recv_tx, writer_channel_rx) = mpsc::channel();
|
let (writer_channel_recv_tx, writer_channel_rx) = mpsc::channel();
|
||||||
|
|
||||||
|
let tmp_port = port.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
listener_server::listen_to_client(listener_channel_send_tx, listener_channel_recv_rx, port)
|
listener_server::listen_to_client(listener_channel_send_tx, listener_channel_recv_rx, tmp_port)
|
||||||
});
|
});
|
||||||
|
let tmp_port = server_port.clone();
|
||||||
let tmp_server = server.clone();
|
let tmp_server = server.clone();
|
||||||
thread::spawn(|| {
|
thread::spawn(move || {
|
||||||
writer_client::write_to_server(
|
writer_client::write_to_server(
|
||||||
writer_stream,
|
&tmp_server,
|
||||||
tmp_server,
|
&tmp_port,
|
||||||
writer_channel_send_rx,
|
writer_channel_send_rx,
|
||||||
writer_channel_recv_tx,
|
writer_channel_recv_tx,
|
||||||
)
|
)
|
||||||
|
@ -118,7 +102,7 @@ fn main() -> Result<()> {
|
||||||
let _ = client_handler::handle_message_from_client(
|
let _ = client_handler::handle_message_from_client(
|
||||||
&message,
|
&message,
|
||||||
&public_key,
|
&public_key,
|
||||||
server,
|
&server,
|
||||||
&mut keys,
|
&mut keys,
|
||||||
&writer_channel_tx,
|
&writer_channel_tx,
|
||||||
&writer_channel_rx,
|
&writer_channel_rx,
|
||||||
|
@ -138,7 +122,7 @@ fn main() -> Result<()> {
|
||||||
&message,
|
&message,
|
||||||
&public_key,
|
&public_key,
|
||||||
&secret_key,
|
&secret_key,
|
||||||
server,
|
&server,
|
||||||
passwd,
|
passwd,
|
||||||
&mut keys,
|
&mut keys,
|
||||||
&writer_channel_tx,
|
&writer_channel_tx,
|
||||||
|
|
|
@ -6,54 +6,68 @@ use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
pub fn write_to_server(
|
pub fn write_to_server(
|
||||||
tcp_stream: TcpStream,
|
server: &str,
|
||||||
server: String,
|
port: &str,
|
||||||
rx: mpsc::Receiver<String>,
|
rx: mpsc::Receiver<String>,
|
||||||
tx: mpsc::Sender<String>,
|
tx: mpsc::Sender<String>,
|
||||||
) {
|
) {
|
||||||
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
|
'big: loop {
|
||||||
let mut stream = connector
|
println!("Connecting to {server}:{port}");
|
||||||
.connect(&server, tcp_stream)
|
let tcp_stream =
|
||||||
.expect("Couldn't start TLS");
|
TcpStream::connect(format!("{server}:{port}")).expect("Couldn't connect to server");
|
||||||
|
|
||||||
stream
|
let connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
|
||||||
.get_mut()
|
let mut stream = connector
|
||||||
.set_nonblocking(true)
|
.connect(&server, &tcp_stream)
|
||||||
.expect("Failed to set nonblocking");
|
.expect("Couldn't start TLS");
|
||||||
|
|
||||||
loop {
|
stream
|
||||||
let mut buffer: Vec<u8> = Vec::new();
|
.get_mut()
|
||||||
let mut buf: [u8; 1] = [0];
|
.set_nonblocking(true)
|
||||||
let newline: u8 = b'\n';
|
.expect("Failed to set nonblocking");
|
||||||
|
|
||||||
while buf[0] != newline {
|
loop {
|
||||||
match stream.ssl_read(&mut buf) {
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
Ok(_length) => {
|
let mut buf: [u8; 1] = [0];
|
||||||
if _length > 0 {
|
let newline: u8 = b'\n';
|
||||||
buffer.push(buf[0]);
|
|
||||||
}
|
while buf[0] != newline {
|
||||||
}
|
match stream.ssl_read(&mut buf) {
|
||||||
Err(_error) => match _error.io_error() {
|
Ok(_length) => {
|
||||||
None => {
|
if _length > 0 {
|
||||||
dbg!(_error.ssl_error());
|
buffer.push(buf[0]);
|
||||||
}
|
|
||||||
Some(error) => match error.kind() {
|
|
||||||
ErrorKind::WouldBlock => {}
|
|
||||||
_ => {
|
|
||||||
dbg!(error.kind());
|
|
||||||
println!("Couldn't read the stream");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Err(_error) => match _error.io_error() {
|
||||||
|
None => {
|
||||||
|
dbg!(_error.ssl_error());
|
||||||
|
continue 'big;
|
||||||
|
}
|
||||||
|
Some(error) => match error.kind() {
|
||||||
|
ErrorKind::WouldBlock => {}
|
||||||
|
_ => {
|
||||||
|
dbg!(error.kind());
|
||||||
|
println!("Couldn't read the stream");
|
||||||
|
continue 'big;
|
||||||
|
}
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
}
|
||||||
|
let value = rx.try_recv().unwrap_or("".to_string());
|
||||||
|
match value.as_str() {
|
||||||
|
"DUMMY CLOSE_CONNECTION" => {
|
||||||
|
continue 'big;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
match stream.write_all(value.as_bytes()) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(_e) => println!("Couldn't send {value}"),
|
||||||
|
};
|
||||||
|
thread::sleep(Duration::from_micros(100));
|
||||||
}
|
}
|
||||||
let value = rx.try_recv().unwrap_or("".to_string());
|
|
||||||
match stream.write_all(value.as_bytes()) {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(_e) => println!("Couldn't send {value}"),
|
|
||||||
};
|
|
||||||
thread::sleep(Duration::from_micros(100));
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string()));
|
let _ = tx.send(dbg!(String::from_utf8_lossy(&buffer).to_string()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue