sui_http/
connection_info.rs1use std::collections::HashMap;
5use std::sync::Arc;
6use std::sync::RwLock;
7use tokio_rustls::rustls::pki_types::CertificateDer;
8
9pub(crate) type ActiveConnections<A = std::net::SocketAddr> =
10 Arc<RwLock<HashMap<ConnectionId, ConnectionInfo<A>>>>;
11
12pub type ConnectionId = usize;
13
14#[derive(Debug)]
15pub struct ConnectionInfo<A>(Arc<Inner<A>>);
16
17#[derive(Clone, Debug)]
18pub struct PeerCertificates(Arc<Vec<tokio_rustls::rustls::pki_types::CertificateDer<'static>>>);
19
20impl PeerCertificates {
21 pub fn peer_certs(&self) -> &[tokio_rustls::rustls::pki_types::CertificateDer<'static>] {
22 self.0.as_ref()
23 }
24}
25
26impl<A> ConnectionInfo<A> {
27 pub(crate) fn new(
28 address: A,
29 peer_certificates: Option<Arc<Vec<CertificateDer<'static>>>>,
30 graceful_shutdown_token: tokio_util::sync::CancellationToken,
31 ) -> Self {
32 Self(Arc::new(Inner {
33 address,
34 time_established: std::time::Instant::now(),
35 peer_certificates: peer_certificates.map(PeerCertificates),
36 graceful_shutdown_token,
37 }))
38 }
39
40 pub fn remote_address(&self) -> &A {
42 &self.0.address
43 }
44
45 pub fn time_established(&self) -> std::time::Instant {
47 self.0.time_established
48 }
49
50 pub fn peer_certificates(&self) -> Option<&PeerCertificates> {
51 self.0.peer_certificates.as_ref()
52 }
53
54 pub fn id(&self) -> ConnectionId {
56 &*self.0 as *const _ as usize
57 }
58
59 pub fn close(&self) {
67 self.0.graceful_shutdown_token.cancel()
68 }
69}
70
71#[derive(Debug)]
72struct Inner<A = std::net::SocketAddr> {
73 address: A,
74
75 time_established: std::time::Instant,
77
78 peer_certificates: Option<PeerCertificates>,
79 graceful_shutdown_token: tokio_util::sync::CancellationToken,
80}
81
82#[derive(Debug, Clone)]
83pub struct ConnectInfo<A = std::net::SocketAddr> {
84 pub local_addr: A,
86 pub remote_addr: A,
88}
89
90impl<A> ConnectInfo<A> {
91 pub fn local_addr(&self) -> &A {
93 &self.local_addr
94 }
95
96 pub fn remote_addr(&self) -> &A {
98 &self.remote_addr
99 }
100}