ntex_tls/rustls/
accept.rs1use std::{io, sync::Arc};
3
4use ntex_io::{Filter, Io, Layer};
5use ntex_service::{BindCtx, Ctx, Service, cfg::Cfg, cfg::Configuration};
6use ntex_util::services::Counter;
7use tls_rustls::ServerConfig;
8
9use crate::{MAX_SSL_ACCEPT_COUNTER, TlsConfig, rustls::TlsServerFilter};
10
11#[derive(Debug)]
12pub struct TlsAcceptor {
21 cfg: Arc<ServerConfig>,
23 conns: Counter,
25}
26
27impl TlsAcceptor {
28 pub fn new(cfg: Arc<ServerConfig>) -> Self {
33 MAX_SSL_ACCEPT_COUNTER.with(|conns| TlsAcceptor {
34 cfg,
35 conns: conns.clone(),
36 })
37 }
38}
39
40impl From<ServerConfig> for TlsAcceptor {
41 fn from(cfg: ServerConfig) -> Self {
42 Self::new(Arc::new(cfg))
43 }
44}
45
46impl<F: Filter, St> Service<St, Io<F>> for TlsAcceptor {
47 type Res = Io<Layer<TlsServerFilter, F>>;
48 type Error = io::Error;
49
50 fn bind(&self, ctx: BindCtx<'_>) {
52 self.conns.bind(&ctx);
53 }
54
55 async fn ready(&self, _: Ctx<'_, Self, St>) -> Result<(), Self::Error> {
57 if !self.conns.is_available() {
58 self.conns.available().await;
59 }
60 Ok(())
61 }
62
63 async fn call(&self, io: Io<F>, _: Ctx<'_, Self, St>) -> Result<Self::Res, Self::Error> {
65 let _guard = self.conns.get();
66 let cfg: Cfg<TlsConfig> = io.cfg().ctx().get();
67 super::TlsServerFilter::create(io, self.cfg.clone(), cfg.handshake_timeout()).await
68 }
69}