feat(transport): add unix socket support in server (#861)

This commit is contained in:
Anthony Green
2022-02-14 15:00:58 -05:00
committed by GitHub
parent d6c0fc112b
commit dee2ab52ff
5 changed files with 120 additions and 79 deletions
+2 -2
View File
@@ -202,8 +202,8 @@ impl<T> Request<T> {
/// Get the remote address of this connection.
///
/// This will return `None` if the `IO` type used
/// does not implement `Connected`. This currently,
/// only works on the server side.
/// does not implement `Connected` or when using a unix domain socket.
/// This currently only works on the server side.
pub fn remote_addr(&self) -> Option<SocketAddr> {
#[cfg(feature = "transport")]
{
+5
View File
@@ -6,6 +6,8 @@ mod recover_error;
#[cfg(feature = "tls")]
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
mod tls;
#[cfg(unix)]
mod unix;
pub use conn::{Connected, TcpConnectInfo};
#[cfg(feature = "tls")]
@@ -17,6 +19,9 @@ pub use conn::TlsConnectInfo;
#[cfg(feature = "tls")]
use super::service::TlsAcceptor;
#[cfg(unix)]
pub use unix::UdsConnectInfo;
use incoming::TcpIncoming;
#[cfg(feature = "tls")]
+31
View File
@@ -0,0 +1,31 @@
use super::Connected;
use std::sync::Arc;
/// Connection info for Unix domain socket streams.
///
/// This type will be accessible through [request extensions][ext] if you're using
/// a unix stream.
///
/// See [Connected] for more details.
///
/// [ext]: crate::Request::extensions
/// [Connected]: crate::transport::server::Connected
#[cfg_attr(docsrs, doc(cfg(unix)))]
#[derive(Clone, Debug)]
pub struct UdsConnectInfo {
/// Peer address. This will be "unnamed" for client unix sockets.
pub peer_addr: Option<Arc<tokio::net::unix::SocketAddr>>,
/// Process credentials for the unix socket.
pub peer_cred: Option<tokio::net::unix::UCred>,
}
impl Connected for tokio::net::UnixStream {
type ConnectInfo = UdsConnectInfo;
fn connect_info(&self) -> Self::ConnectInfo {
UdsConnectInfo {
peer_addr: self.peer_addr().ok().map(Arc::new),
peer_cred: self.peer_cred().ok(),
}
}
}