]> git.r.bdr.sh - rbdr/olden-mail/blobdiff - src/proxy.rs
Don't use a prefix
[rbdr/olden-mail] / src / proxy.rs
index 8348325274185b7e67927fb56181585c8b7da309..33de44039615959fdc360b56767d601bbbba58d7 100644 (file)
@@ -42,6 +42,7 @@ use std::thread::{sleep, spawn, JoinHandle};
 use std::time::Duration;
 
 use crate::configuration::Proxy;
 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.
 
 /// A proxy server that listens for plaintext connections and forwards them
 /// via TLS.
@@ -119,7 +120,7 @@ fn run_proxy(configuration: &Arc<Proxy>, running: &Arc<AtomicBool>) {
             Ok((stream, addr)) => {
                 info!("New {} connection from {}", configuration.protocol, addr);
 
             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);
                 });
                 let handle = spawn(move || {
                     handle_client(stream, &configuration_clone);
                 });
@@ -213,7 +214,6 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
         let mut buffer = [0u8; 8192];
         let mut client_reader = client_stream;
         loop {
         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 bytes_read = match client_reader.read(&mut buffer) {
                 Ok(0) => break,
                 Ok(n) => n,
@@ -227,7 +227,13 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
                 }
             };
 
                 }
             };
 
-            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");
                 .replace('\n', "\\n")
                 .replace('\r', "\\r")
                 .replace('\t', "\\t");
@@ -236,7 +242,7 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
             // Lock the TLS stream and write the data to server
             match tls_stream_clone.lock() {
                 Ok(mut tls_guard) => {
             // 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;
                     }
                         debug!(">>> Error writing to server: {error}");
                         break;
                     }
@@ -260,7 +266,6 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
         let mut buffer = [0u8; 8192];
         let mut client_writer = client_stream_clone;
         loop {
         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) {
             // 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) {
@@ -281,14 +286,20 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
                 }
             };
 
                 }
             };
 
-            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
                 .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;
             }
                 debug!("<<< ERR");
                 break;
             }