1- use std:: { fmt, sync:: Arc } ;
1+ use std:: { fmt, sync:: Arc , time :: Duration } ;
22
33use tokio:: io:: { AsyncRead , AsyncWrite } ;
4+ use tokio:: time;
45use tokio_rustls:: {
56 rustls:: { server:: WebPkiClientVerifier , RootCertStore , ServerConfig } ,
67 server:: TlsStream ,
78 TlsAcceptor as RustlsAcceptor ,
89} ;
910
1011use crate :: transport:: {
11- service:: tls:: { convert_certificate_to_pki_types, convert_identity_to_pki_types, ALPN_H2 } ,
12+ service:: tls:: {
13+ convert_certificate_to_pki_types, convert_identity_to_pki_types, TlsError , ALPN_H2 ,
14+ } ,
1215 Certificate , Identity ,
1316} ;
1417
1518#[ derive( Clone ) ]
1619pub ( crate ) struct TlsAcceptor {
1720 inner : Arc < ServerConfig > ,
21+ timeout : Option < Duration > ,
1822}
1923
2024impl TlsAcceptor {
@@ -23,6 +27,7 @@ impl TlsAcceptor {
2327 client_ca_root : Option < & Certificate > ,
2428 client_auth_optional : bool ,
2529 ignore_client_order : bool ,
30+ timeout : Option < Duration > ,
2631 ) -> Result < Self , crate :: BoxError > {
2732 let builder = ServerConfig :: builder ( ) ;
2833
@@ -48,6 +53,7 @@ impl TlsAcceptor {
4853 config. alpn_protocols . push ( ALPN_H2 . into ( ) ) ;
4954 Ok ( Self {
5055 inner : Arc :: new ( config) ,
56+ timeout,
5157 } )
5258 }
5359
@@ -56,7 +62,14 @@ impl TlsAcceptor {
5662 IO : AsyncRead + AsyncWrite + Unpin ,
5763 {
5864 let acceptor = RustlsAcceptor :: from ( self . inner . clone ( ) ) ;
59- acceptor. accept ( io) . await . map_err ( Into :: into)
65+ let accept_fut = acceptor. accept ( io) ;
66+ match self . timeout {
67+ Some ( timeout) => time:: timeout ( timeout, accept_fut)
68+ . await
69+ . map_err ( |_| TlsError :: HandshakeTimeout ) ?,
70+ None => accept_fut. await ,
71+ }
72+ . map_err ( Into :: into)
6073 }
6174}
6275
0 commit comments