]> git.r.bdr.sh - rbdr/olden-mail/blobdiff - src/proxy.rs
Split LINE and not COMMAND
[rbdr/olden-mail] / src / proxy.rs
index 255101531ad92be0a52e73d23d55a7bdc1998ea6..9c6bbad314d475c8727e72d440fea50720c18e60 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::get as get_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.
@@ -116,8 +117,8 @@ fn run_proxy(configuration: &Arc<Proxy>, running: &Arc<AtomicBool>) {
 
     while running.load(Ordering::SeqCst) {
         match listener.accept() {
 
     while running.load(Ordering::SeqCst) {
         match listener.accept() {
-            Ok((stream, addr)) => {
-                info!("New {} connection from {}", configuration.protocol, addr);
+            Ok((stream, address)) => {
+                info!("New {} connection from {}", configuration.protocol, address);
 
                 let configuration_clone = Arc::clone(configuration);
                 let handle = spawn(move || {
 
                 let configuration_clone = Arc::clone(configuration);
                 let handle = spawn(move || {
@@ -158,6 +159,9 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
         return;
     }
 
         return;
     }
 
+    let available_middleware = get_middleware();
+    let available_middleware_clone = Arc::clone(&available_middleware);
+
     let connector = match TlsConnector::new() {
         Ok(c) => c,
         Err(e) => {
     let connector = match TlsConnector::new() {
         Ok(c) => c,
         Err(e) => {
@@ -166,14 +170,14 @@ fn handle_client(client_stream: TcpStream, configuration: &Arc<Proxy>) {
         }
     };
 
         }
     };
 
-    let remote_addr = format!(
+    let remote_address = format!(
         "{}:{}",
         configuration.remote_host, configuration.remote_port
     );
         "{}:{}",
         configuration.remote_host, configuration.remote_port
     );
-    let tcp_stream = match TcpStream::connect(&remote_addr) {
+    let tcp_stream = match TcpStream::connect(&remote_address) {
         Ok(stream) => stream,
         Err(e) => {
         Ok(stream) => stream,
         Err(e) => {
-            error!("Failed to connect to {}: {}", remote_addr, e);
+            error!("Failed to connect to {}: {}", remote_address, e);
             return;
         }
     };
             return;
         }
     };
@@ -226,16 +230,33 @@ 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();
+
+            if let Ok(mut guard) = available_middleware.lock() {
+                for middleware in guard.iter_mut() {
+                    command = middleware.client_message(&command);
+                }
+            }
+
+            let debug_original = String::from_utf8_lossy(&buffer[..bytes_read])
                 .replace('\n', "\\n")
                 .replace('\r', "\\r")
                 .replace('\t', "\\t");
                 .replace('\n', "\\n")
                 .replace('\r', "\\r")
                 .replace('\t', "\\t");
-            debug!(">>> {}", debug_str);
+
+            let debug_final = String::from_utf8_lossy(&command)
+                .replace('\n', "\\n")
+                .replace('\r', "\\r")
+                .replace('\t', "\\t");
+
+            debug!(">>> {debug_original}");
+            if debug_original != debug_final {
+                debug!("### {debug_final}");
+            }
 
             // 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;
                     }
@@ -279,14 +300,30 @@ 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();
+
+            if let Ok(mut guard) = available_middleware_clone.lock() {
+                for middleware in guard.iter_mut() {
+                    command = middleware.server_message(&command);
+                }
+            }
+
+            let debug_original = String::from_utf8_lossy(&buffer[..bytes_read])
                 .replace('\n', "\\n")
                 .replace('\r', "\\r")
                 .replace('\t', "\\t");
                 .replace('\n', "\\n")
                 .replace('\r', "\\r")
                 .replace('\t', "\\t");
-            debug!("<<< {}", debug_str);
+
+            let debug_final = String::from_utf8_lossy(&command)
+                .replace('\n', "\\n")
+                .replace('\r', "\\r")
+                .replace('\t', "\\t");
+            debug!("<<< {debug_original}");
+            if debug_original != debug_final {
+                debug!("### {debug_final}");
+            }
 
             // Write decrypted data to client
 
             // 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;
             }