use std::time::Duration;
use crate::configuration::Proxy;
+use crate::middleware::{CLIENT_MIDDLEWARE, SERVER_MIDDLEWARE};
/// A proxy server that listens for plaintext connections and forwards them
/// via TLS.
let running_clone = Arc::clone(&running);
let thread_handle = spawn(move || {
- run_proxy(configuration, running_clone);
+ run_proxy(&configuration, &running_clone);
});
Server {
/// The main loop that listens for incoming (plaintext) connections on
/// `configuration.bind_address:configuration.local_port`.
-fn run_proxy(configuration: Arc<Proxy>, running: Arc<AtomicBool>) {
+fn run_proxy(configuration: &Arc<Proxy>, running: &Arc<AtomicBool>) {
let listener = match TcpListener::bind(format!(
"{}:{}",
configuration.bind_address, configuration.local_port
Ok((stream, addr)) => {
info!("New {} connection from {}", configuration.protocol, addr);
- let configuration_clone = Arc::clone(&configuration);
+ let configuration_clone = Arc::clone(configuration);
let handle = spawn(move || {
- handle_client(stream, configuration_clone);
+ handle_client(stream, &configuration_clone);
});
active_threads.push(handle);
}
}
/// Handles a single client connection by bridging it (plaintext) to a TLS connection.
-fn handle_client(client_stream: TcpStream, configuration: Arc<Proxy>) {
+fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
if let Err(e) = client_stream.set_nonblocking(true) {
error!("Failed to set client stream to nonblocking: {}", e);
return;
let mut buffer = [0u8; 8192];
let mut client_reader = client_stream;
loop {
- debug!(">");
let bytes_read = match client_reader.read(&mut buffer) {
Ok(0) => break,
Ok(n) => n,
}
};
- let debug_str = String::from_utf8_lossy(&buffer[..bytes_read])
+ let mut command = buffer[..bytes_read].to_vec();
+
+ for middleware in CLIENT_MIDDLEWARE {
+ command = middleware(&command);
+ }
+
+ let debug_str = String::from_utf8_lossy(&command)
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('\t', "\\t");
// Lock the TLS stream and write the data to server
match tls_stream_clone.lock() {
Ok(mut tls_guard) => {
- if let Err(error) = tls_guard.write_all(&buffer[..bytes_read]) {
+ if let Err(error) = tls_guard.write_all(&command) {
debug!(">>> Error writing to server: {error}");
break;
}
let mut buffer = [0u8; 8192];
let mut client_writer = client_stream_clone;
loop {
- debug!("<");
// Lock the TLS stream and read from the server
let bytes_read = match tls_stream_clone.lock() {
Ok(mut tls_guard) => match tls_guard.read(&mut buffer) {
}
};
- let debug_str = String::from_utf8_lossy(&buffer[..bytes_read])
+ let mut command = buffer[..bytes_read].to_vec();
+
+ for middleware in SERVER_MIDDLEWARE {
+ command = middleware(&command);
+ }
+
+ let debug_str = String::from_utf8_lossy(&command)
.replace('\n', "\\n")
.replace('\r', "\\r")
.replace('\t', "\\t");
debug!("<<< {}", debug_str);
// Write decrypted data to client
- if client_writer.write_all(&buffer[..bytes_read]).is_err() {
+ if client_writer.write_all(&command).is_err() {
debug!("<<< ERR");
break;
}