From bcc3024fc32df82012dea865a6ece0f65a7db88e Mon Sep 17 00:00:00 2001 From: trivernis Date: Tue, 3 Nov 2020 20:29:07 +0100 Subject: [PATCH] Add tls server implementation Signed-off-by: trivernis --- Cargo.toml | 4 +-- src/server/mod.rs | 1 + src/server/tls/mod.rs | 78 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/server/tls/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 64bc48e..c292d85 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,9 @@ edition = "2018" [dependencies] rmp = "0.8.9" rmp-serde = "0.14.4" -rmpv = "0.4.5" serde = { version = "1.0.117", features = ["serde_derive"] } byteorder = "1.3.4" parking_lot = "0.11.0" scheduled-thread-pool = "0.2.5" -log = "0.4.11" \ No newline at end of file +log = "0.4.11" +native-tls = "0.2.4" \ No newline at end of file diff --git a/src/server/mod.rs b/src/server/mod.rs index 1b7b589..d410af0 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -3,6 +3,7 @@ use crate::result::VentedResult; pub(crate) mod server_events; pub mod tcp; +pub mod tls; pub trait VentedServer { fn listen(&mut self, address: &str) -> VentedResult<()>; diff --git a/src/server/tls/mod.rs b/src/server/tls/mod.rs new file mode 100644 index 0000000..a371679 --- /dev/null +++ b/src/server/tls/mod.rs @@ -0,0 +1,78 @@ +use crate::event::Event; +use crate::event_handler::EventHandler; +use crate::result::VentedResult; +use crate::server::server_events::get_server_event_handler; +use crate::server::VentedServer; +use native_tls::{Identity, TlsAcceptor, TlsStream}; +use parking_lot::Mutex; +use scheduled_thread_pool::ScheduledThreadPool; +use std::borrow::BorrowMut; +use std::io::{Read, Write}; +use std::net::{TcpListener, TcpStream}; +use std::sync::Arc; + +pub struct VentedTlsServer { + event_handler: Arc>, + pool: ScheduledThreadPool, + identity: Identity, +} + +impl VentedServer for VentedTlsServer { + fn listen(&mut self, address: &str) -> VentedResult<()> { + let listener = TcpListener::bind(address)?; + let acceptor = TlsAcceptor::new(self.identity.clone())?; + let acceptor = Arc::new(acceptor); + + for stream in listener.incoming() { + log::trace!("Connection received."); + match stream { + Ok(stream) => self.handle_connection(stream, Arc::clone(&acceptor)), + Err(e) => log::error!("Failed to handle connection: {}", e), + } + } + + Ok(()) + } + + fn register_handler(&mut self, event_name: &str, handler: F) + where + F: Fn(Event) -> Option + Send + Sync, + { + self.event_handler + .lock() + .borrow_mut() + .on(event_name, handler); + } +} + +impl VentedTlsServer { + /// Creates a new server that runs on the specified number of threads + /// with the given tls identity + pub fn new(num_threads: usize, identity: Identity) -> Self { + let event_handler = get_server_event_handler(); + let pool = ScheduledThreadPool::new(num_threads); + + Self { + event_handler: Arc::new(Mutex::new(event_handler)), + pool, + identity, + } + } + + fn handle_connection(&self, mut stream: TcpStream, acceptor: Arc) { + let handler = Arc::clone(&self.event_handler); + self.pool.execute(move || { + acceptor.accept(stream); + + if let Ok(event) = Event::from_bytes(&mut stream) { + if let Some(mut event) = handler.lock().handle_event(event) { + if let Err(e) = stream.write(&event.as_bytes()) { + log::error!("Failed to respond to event: {}", e) + } + } + } else { + log::warn!("Failed to create an Event from received bytes.") + } + }); + } +}