Skip to main content

ntex_tls/openssl/
accept.rs

1//! Server handshake service with shared per-thread capacity and an I/O-configured timeout.
2use 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)]
13/// Upgrades existing I/O connections to server-side TLS using OpenSSL.
14///
15/// Reads [`TlsConfig`] from each connection's configuration. Readiness observes
16/// the shared per-thread soft limit set by [`crate::max_concurrent_ssl_accept`]
17/// without reserving a slot. Each call holds a slot until completion, error,
18/// timeout, or cancellation. Clones share both the OpenSSL context and counter.
19///
20/// Existing filters remain below the returned [`SslFilter`].
21pub struct SslAcceptor {
22    /// Certificate and TLS policy used to create each connection's session.
23    acceptor: ssl::SslAcceptor,
24    /// Thread-local handshake counter shared with other TLS acceptors.
25    conns: Counter,
26}
27
28impl SslAcceptor {
29    /// Creates a service from a prepared OpenSSL acceptor.
30    ///
31    /// Captures the current thread's handshake counter without changing the
32    /// supplied certificate, verification, or protocol settings.
33    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    /// Registers capacity-change notifications with the service readiness scope.
46    fn bind(&self, ctx: BindCtx<'_>) {
47        self.conns.bind(&ctx);
48    }
49
50    /// Waits for spare capacity without reserving it.
51    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    /// Counts the handshake, creates its session, and applies the I/O-configured timeout.
59    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}