@@ -15,10 +15,11 @@ use retry::{retry_function, RetryError};
1515use tokio:: time:: { timeout, Instant } ;
1616use types:: batch_state:: BatchState ;
1717use types:: user_state:: UserState ;
18-
18+ use boring :: ssl :: { SslMethod , SslAcceptor , SslStream , SslFiletype } ;
1919use std:: collections:: HashMap ;
2020use std:: env;
2121use std:: net:: SocketAddr ;
22+ use std:: path:: PathBuf ;
2223use std:: sync:: Arc ;
2324use std:: time:: Duration ;
2425
@@ -261,7 +262,14 @@ impl Batcher {
261262 }
262263 }
263264
264- pub async fn listen_connections ( self : Arc < Self > , address : & str ) -> Result < ( ) , BatcherError > {
265+ pub async fn listen_connections ( self : Arc < Self > , address : & str , cert : PathBuf , key : PathBuf ) -> Result < ( ) , BatcherError > {
266+ let mut acceptor;
267+ let mut acceptor_builder = SslAcceptor :: mozilla_intermediate_v5 ( SslMethod :: tls ( ) ) . unwrap ( ) ;
268+ acceptor_builder. set_private_key_file ( key, SslFiletype :: PEM ) . unwrap ( ) ;
269+ acceptor_builder. set_certificate_chain_file ( cert) . unwrap ( ) ;
270+ acceptor_builder. check_private_key ( ) . unwrap ( ) ;
271+ acceptor = Arc :: new ( acceptor_builder. build ( ) ) ;
272+
265273 // Create the event loop and TCP listener we'll accept connections on.
266274 let listener = TcpListener :: bind ( address)
267275 . await
@@ -273,7 +281,7 @@ impl Batcher {
273281 Ok ( ( stream, addr) ) => {
274282 let batcher = self . clone ( ) ;
275283 // Let's spawn the handling of each connection in a separate task.
276- tokio:: spawn ( batcher. handle_connection ( stream, addr) ) ;
284+ tokio:: spawn ( batcher. handle_connection ( stream, addr, acceptor . clone ( ) ) ) ;
277285 }
278286 Err ( e) => {
279287 self . metrics . user_error ( & [ "connection_accept_error" , "" ] ) ;
@@ -367,11 +375,14 @@ impl Batcher {
367375 self : Arc < Self > ,
368376 raw_stream : TcpStream ,
369377 addr : SocketAddr ,
378+ acceptor : Arc < SslAcceptor > ,
370379 ) -> Result < ( ) , BatcherError > {
371380 info ! ( "Incoming TCP connection from: {}" , addr) ;
372381 self . metrics . open_connections . inc ( ) ;
373-
374- let ws_stream_future = tokio_tungstenite:: accept_async ( raw_stream) ;
382+ let tls_stream = tokio_boring:: accept ( & acceptor, raw_stream)
383+ . await
384+ . map_err ( |e | BatcherError :: TlsError ( e. to_string ( ) ) ) ?;
385+ let ws_stream_future = tokio_tungstenite:: accept_async ( tls_stream) ;
375386 let ws_stream =
376387 match timeout ( Duration :: from_secs ( CONNECTION_TIMEOUT ) , ws_stream_future) . await {
377388 Ok ( Ok ( stream) ) => stream,
0 commit comments