ntex_tls/openssl/
accept.rs1use std::{fmt, io};
3
4use ntex_io::{Filter, Io, Layer};
5use ntex_service::cfg::Cfg;
6use ntex_service::{BindCtx, Ctx, Service, cfg::Configuration};
7use ntex_util::services::Counter;
8use tls_openssl::ssl;
9
10use crate::{MAX_SSL_ACCEPT_COUNTER, TlsConfig, openssl::SslFilter};
11
12#[derive(Clone)]
13pub struct SslAcceptor {
22 acceptor: ssl::SslAcceptor,
24 conns: Counter,
26}
27
28impl SslAcceptor {
29 pub fn new(acceptor: ssl::SslAcceptor) -> Self {
34 MAX_SSL_ACCEPT_COUNTER.with(|conns| SslAcceptor {
35 acceptor,
36 conns: conns.clone(),
37 })
38 }
39}
40
41impl<F: Filter, St> Service<St, Io<F>> for SslAcceptor {
42 type Res = Io<Layer<SslFilter, F>>;
43 type Error = io::Error;
44
45 fn bind(&self, ctx: BindCtx<'_>) {
47 self.conns.bind(&ctx);
48 }
49
50 async fn ready(&self, _: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
52 if !self.conns.is_available() {
53 self.conns.available().await;
54 }
55 Ok(())
56 }
57
58 async fn call(&self, io: Io<F>, _: Ctx<'_, Self, St>) -> Result<Self::Res, Self::Error> {
60 let _guard = self.conns.get();
61 let ssl = ssl::Ssl::new(self.acceptor.context()).map_err(io::Error::other)?;
62 let cfg: Cfg<TlsConfig> = io.cfg().ctx().get();
63
64 log::trace!("{}: Accepting tls connection", io.tag());
65 crate::utils::with_timeout(cfg.handshake_timeout(), super::accept(io, ssl)).await
66 }
67}
68
69impl From<ssl::SslAcceptor> for SslAcceptor {
70 fn from(acceptor: ssl::SslAcceptor) -> Self {
71 Self::new(acceptor)
72 }
73}
74
75impl fmt::Debug for SslAcceptor {
76 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77 f.debug_struct("SslAcceptor").finish()
78 }
79}