sui_http/
connection_info.rsuse std::collections::HashMap;
use std::sync::{Arc, RwLock};
use tokio_rustls::rustls::pki_types::CertificateDer;
pub(crate) type ActiveConnections<A = std::net::SocketAddr> =
Arc<RwLock<HashMap<ConnectionId, ConnectionInfo<A>>>>;
pub type ConnectionId = usize;
#[derive(Debug)]
pub struct ConnectionInfo<A>(Arc<Inner<A>>);
#[derive(Clone, Debug)]
pub struct PeerCertificates(Arc<Vec<tokio_rustls::rustls::pki_types::CertificateDer<'static>>>);
impl PeerCertificates {
pub fn peer_certs(&self) -> &[tokio_rustls::rustls::pki_types::CertificateDer<'static>] {
self.0.as_ref()
}
}
impl<A> ConnectionInfo<A> {
pub(crate) fn new(
address: A,
peer_certificates: Option<Arc<Vec<CertificateDer<'static>>>>,
graceful_shutdown_token: tokio_util::sync::CancellationToken,
) -> Self {
Self(Arc::new(Inner {
address,
time_established: std::time::Instant::now(),
peer_certificates: peer_certificates.map(PeerCertificates),
graceful_shutdown_token,
}))
}
pub fn remote_address(&self) -> &A {
&self.0.address
}
pub fn time_established(&self) -> std::time::Instant {
self.0.time_established
}
pub fn peer_certificates(&self) -> Option<&PeerCertificates> {
self.0.peer_certificates.as_ref()
}
pub fn id(&self) -> ConnectionId {
&*self.0 as *const _ as usize
}
pub fn close(&self) {
self.0.graceful_shutdown_token.cancel()
}
}
#[derive(Debug)]
struct Inner<A = std::net::SocketAddr> {
address: A,
time_established: std::time::Instant,
peer_certificates: Option<PeerCertificates>,
graceful_shutdown_token: tokio_util::sync::CancellationToken,
}
#[derive(Debug, Clone)]
pub struct ConnectInfo<A = std::net::SocketAddr> {
pub local_addr: A,
pub remote_addr: A,
}
impl<A> ConnectInfo<A> {
pub fn local_addr(&self) -> &A {
&self.local_addr
}
pub fn remote_addr(&self) -> &A {
&self.remote_addr
}
}