//! # Proxy Module
//!
//! This module has the actual proxy functionality, exposed through
-//! `ProxyServer`. The proxy consists of a local unencrypted TCP stream
+//! `Server`. The proxy consists of a local unencrypted TCP stream
//! and a remote TLS stream. Messages are passed between them via two
//! threads.
//!
//! - **Client to Server Thread**: Forwards data from client -> TLS server
//! - **Serveer to Client Thread**: Forwards data from TLS server -> client
//!
-//! Finally, the ProxyServer may be shutdown by calling `.shutdown()`,
+//! 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::ProxyConfiguration;
-//! use crate::proxy::ProxyServer;
+//! use crate::configuration::Proxy;
+//! use crate::proxy::Server;
//!
-//! let config = Arc::new(ProxyConfiguration {
+//! 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 = ProxyServer::new(config);
+//! let mut server = Server::new(config);
//! // The server runs in a background thread. To shut down gracefully:
//! server.shutdown();
//! ```
use std::thread::{sleep, spawn, JoinHandle};
use std::time::Duration;
-use crate::configuration::ProxyConfiguration;
+use crate::configuration::Proxy;
/// A proxy server that listens for plaintext connections and forwards them
/// via TLS.
///
-/// Creating a new `ProxyServer` spawns a dedicated thread that:
+/// 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 ProxyServer {
+pub struct Server {
running: Arc<AtomicBool>,
thread_handle: Option<JoinHandle<()>>,
}
-impl ProxyServer {
- /// Creates a new `ProxyServer` for the given `ProxyConfiguration` and
+impl Server {
+ /// Creates a new `Server` for the given `Proxy` configuration and
/// immediately starts it.
///
/// # Arguments
///
- /// * `configuration` - Shared (Arc) `ProxyConfiguration`
+ /// * `configuration` - Shared (Arc) `Proxy`
///
/// # Returns
///
- /// A `ProxyServer` instance that will keep running until its `.shutdown()`
+ /// A `Server` instance that will keep running until its `.shutdown()`
/// method is called, or an error occurs.
- pub fn new(configuration: Arc<ProxyConfiguration>) -> Self {
+ pub fn new(configuration: Arc<Proxy>) -> Self {
let running = Arc::new(AtomicBool::new(true));
let running_clone = Arc::clone(&running);
run_proxy(configuration, running_clone);
});
- ProxyServer {
+ Server {
running,
thread_handle: Some(thread_handle),
}
/// The main loop that listens for incoming (plaintext) connections on
/// `configuration.bind_address:configuration.local_port`.
-fn run_proxy(configuration: Arc<ProxyConfiguration>, running: Arc<AtomicBool>) {
+fn run_proxy(configuration: Arc<Proxy>, running: Arc<AtomicBool>) {
let listener = match TcpListener::bind(format!(
"{}:{}",
configuration.bind_address, configuration.local_port
}
/// Handles a single client connection by bridging it (plaintext) to a TLS connection.
-fn handle_client(client_stream: TcpStream, configuration: Arc<ProxyConfiguration>) {
+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;