mas_listener/
lib.rs

1// Copyright 2024 New Vector Ltd.
2// Copyright 2022-2024 The Matrix.org Foundation C.I.C.
3//
4// SPDX-License-Identifier: AGPL-3.0-only
5// Please see LICENSE in the repository root for full details.
6
7#![deny(rustdoc::missing_crate_level_docs)]
8#![allow(clippy::module_name_repetitions)]
9
10//! An utility crate to build flexible [`hyper`] listeners, with optional TLS
11//! and proxy protocol support.
12
13use self::{maybe_tls::TlsStreamInfo, proxy_protocol::ProxyProtocolV1Info};
14
15pub mod maybe_tls;
16pub mod proxy_protocol;
17pub mod rewind;
18pub mod server;
19pub mod unix_or_tcp;
20
21#[derive(Debug, Clone)]
22pub struct ConnectionInfo {
23    tls: Option<TlsStreamInfo>,
24    proxy: Option<ProxyProtocolV1Info>,
25    net_peer_addr: Option<std::net::SocketAddr>,
26}
27
28impl ConnectionInfo {
29    /// Returns informations about the TLS connection. Returns [`None`] if the
30    /// connection was not TLS.
31    #[must_use]
32    pub fn get_tls_ref(&self) -> Option<&TlsStreamInfo> {
33        self.tls.as_ref()
34    }
35
36    /// Returns informations about the proxy protocol connection. Returns
37    /// [`None`] if the connection was not using the proxy protocol.
38    #[must_use]
39    pub fn get_proxy_ref(&self) -> Option<&ProxyProtocolV1Info> {
40        self.proxy.as_ref()
41    }
42
43    /// Returns the remote peer address. Returns [`None`] if the connection was
44    /// established via a UNIX domain socket.
45    #[must_use]
46    pub fn get_peer_addr(&self) -> Option<std::net::SocketAddr> {
47        self.net_peer_addr
48    }
49}