1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
//! # Proxy Module
//!
//! This module has the actual proxy functionality, exposed through
//! `Server`. The proxy consists of a local unencrypted TCP stream
//! and a remote TLS stream. Messages are passed between them via two
//! threads.
//!
//! Each new client connection spawns two threads:
//! - **Client to Server Thread**: Forwards data from client -> TLS server
//! - **Serveer to Client Thread**: Forwards data from TLS server -> client
//!
//! Finally, the `Server` may be shutdown by calling `.shutdown()`,
//! this will stop new connections and wait for it to finish.
//!
//! # Example
//!
//! ```
//! use std::sync::Arc;
//! use crate::configuration::Proxy;
//! use crate::proxy::Server;
//!
//! let config = Arc::new(Proxy {
//! protocol: "IMAP".to_string(),
//! local_port: 143,
//! remote_domain: "imap.example.com".to_string(),
//! remote_port: 993,
//! });
//!
//! let mut server = Server::new(config);
//! // The server runs in a background thread. To shut down gracefully:
//! server.shutdown();
//! ```
use log::{debug, error, info};
use native_tls::TlsConnector;
use std::io::{ErrorKind, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc, Mutex,
};
use std::thread::{sleep, spawn, JoinHandle};
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.
///
/// Creating a new `Server` spawns a dedicated thread that:
/// - Binds to a local port (non-blocking mode).
/// - Spawns additional threads for each incoming client connection.
/// - Manages connection-lifetime until it receives a shutdown signal.
pub struct Server {
running: Arc<AtomicBool>,
thread_handle: Option<JoinHandle<()>>,
}
impl Server {
/// Creates a new `Server` for the given `Proxy` configuration and
/// immediately starts it.
///
/// # Arguments
///
/// * `configuration` - Shared (Arc) `Proxy`
///
/// # Returns
///
/// A `Server` instance that will keep running until its `.shutdown()`
/// method is called, or an error occurs.
pub fn new(configuration: Arc<Proxy>) -> Self {
let running = Arc::new(AtomicBool::new(true));
let running_clone = Arc::clone(&running);
let thread_handle = spawn(move || {
run_proxy(&configuration, &running_clone);
});
Server {
running,
thread_handle: Some(thread_handle),
}
}
/// Signals this proxy to stop accepting new connections and waits
/// for all active connection threads to complete.
pub fn shutdown(&mut self) {
self.running.store(false, Ordering::SeqCst);
if let Some(handle) = self.thread_handle.take() {
let _ = handle.join();
}
}
}
/// 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>) {
let listener = match TcpListener::bind(format!(
"{}:{}",
configuration.bind_address, configuration.local_port
)) {
Ok(l) => l,
Err(e) => {
error!("Failed to bind to port {}: {}", configuration.local_port, e);
return;
}
};
listener.set_nonblocking(true).unwrap();
info!(
"{} proxy listening on port {}:{}",
configuration.protocol, configuration.bind_address, configuration.local_port
);
// Keep track of active connections so we can join them on shutdown
let mut active_threads = vec![];
while running.load(Ordering::SeqCst) {
match listener.accept() {
Ok((stream, address)) => {
info!("New {} connection from {}", configuration.protocol, address);
let configuration_clone = Arc::clone(configuration);
let handle = spawn(move || {
handle_client(stream, &configuration_clone);
});
active_threads.push(handle);
}
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
// No pending connection; sleep briefly then loop again
sleep(Duration::from_millis(100));
continue;
}
Err(e) => {
error!("Error accepting connection: {}", e);
break;
}
}
// Clean up any finished threads
active_threads.retain(|thread| !thread.is_finished());
// Potential Improvement: Configure thread limit.
if active_threads.len() >= 50 {
sleep(Duration::from_millis(100));
}
}
// On shutdown, wait for all threads to finish
for thread in active_threads {
let _ = thread.join();
}
}
/// Handles a single client connection by bridging it (plaintext) to a TLS connection.
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 available_middleware = get_middleware();
let available_middleware_clone = Arc::clone(&available_middleware);
let connector = match TlsConnector::new() {
Ok(c) => c,
Err(e) => {
error!("Failed to create TLS connector: {}", e);
return;
}
};
let remote_address = format!(
"{}:{}",
configuration.remote_host, configuration.remote_port
);
let tcp_stream = match TcpStream::connect(&remote_address) {
Ok(stream) => stream,
Err(e) => {
error!("Failed to connect to {}: {}", remote_address, e);
return;
}
};
let tls_stream = match connector.connect(&configuration.remote_host, tcp_stream) {
Ok(tls_stream) => tls_stream,
Err(e) => {
error!(
"TLS handshake to {} failed: {}",
configuration.remote_host, e
);
return;
}
};
// The nonblocking needs to be set AFTER the TLS handshake is completed.
// Otherwise the TLS handshake is interrupted.
if let Err(e) = tls_stream.get_ref().set_nonblocking(true) {
error!("Failed to set remote stream to nonblocking: {}", e);
return;
}
let tls_stream = Arc::new(Mutex::new(tls_stream));
let client_stream_clone = match client_stream.try_clone() {
Ok(s) => s,
Err(e) => {
error!("Failed to clone client stream: {}", e);
return;
}
};
// Client to Server Thread
let tls_stream_clone = Arc::clone(&tls_stream);
let client_to_server = spawn(move || {
debug!(">>> BEGIN");
let mut buffer = [0u8; 8192];
let mut client_reader = client_stream;
loop {
let bytes_read = match client_reader.read(&mut buffer) {
Ok(0) => break,
Ok(n) => n,
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
sleep(Duration::from_millis(10));
continue;
}
Err(error) => {
debug!(">>> Error reading buffer {error}");
break;
}
};
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");
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) => {
if let Err(error) = tls_guard.write_all(&command) {
debug!(">>> Error writing to server: {error}");
break;
}
if let Err(error) = tls_guard.flush() {
debug!(">>> Error flushing server connection: {error}");
break;
}
}
Err(error) => {
debug!(">>> Error acquiring TLS stream lock: {error}");
break;
}
}
}
});
// Server to Client Thread
let tls_stream_clone = Arc::clone(&tls_stream);
let server_to_client = spawn(move || {
debug!("<<< BEGIN");
let mut buffer = [0u8; 8192];
let mut client_writer = client_stream_clone;
loop {
// 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) {
Ok(0) => break, // TLS server closed
Ok(n) => n,
Err(ref e) if e.kind() == ErrorKind::WouldBlock => {
sleep(Duration::from_millis(10));
continue;
}
Err(error) => {
debug!("<<< Error reading buffer {error}");
break;
}
},
Err(error) => {
debug!("<<< Error Cloning TLS {error}");
break;
}
};
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");
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
if client_writer.write_all(&command).is_err() {
debug!("<<< ERR");
break;
}
if client_writer.flush().is_err() {
debug!("<<< ERR");
break;
}
}
});
// Wait for both directions to finish
let _ = client_to_server.join();
let _ = server_to_client.join();
}
|