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 {
@@ -24,6 +28,7 @@ impl TlsAcceptor {
2428 client_auth_optional : bool ,
2529 ignore_client_order : bool ,
2630 use_key_log : bool ,
31+ timeout : Option < Duration > ,
2732 ) -> Result < Self , crate :: BoxError > {
2833 let builder = ServerConfig :: builder ( ) ;
2934
@@ -53,6 +58,7 @@ impl TlsAcceptor {
5358 config. alpn_protocols . push ( ALPN_H2 . into ( ) ) ;
5459 Ok ( Self {
5560 inner : Arc :: new ( config) ,
61+ timeout,
5662 } )
5763 }
5864
@@ -61,7 +67,14 @@ impl TlsAcceptor {
6167 IO : AsyncRead + AsyncWrite + Unpin ,
6268 {
6369 let acceptor = RustlsAcceptor :: from ( self . inner . clone ( ) ) ;
64- acceptor. accept ( io) . await . map_err ( Into :: into)
70+ let accept_fut = acceptor. accept ( io) ;
71+ match self . timeout {
72+ Some ( timeout) => time:: timeout ( timeout, accept_fut)
73+ . await
74+ . map_err ( |_| TlsError :: HandshakeTimeout ) ?,
75+ None => accept_fut. await ,
76+ }
77+ . map_err ( Into :: into)
6578 }
6679}
6780
0 commit comments