From e377909beee2d8213569a37961e93128392ebf52 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 24 Jan 2023 11:15:22 +0100 Subject: [PATCH] rest-server: PeerAddr trait, drop proxmox-http dep We pulled in proxmox-http with the client feature solely to implement the `Service` trait on `SslStream>`. All those `Service` impls are the same: provide a peer address and return an `ApiService`. Let's put the `peer_addr()` call into a trait and build from there. Signed-off-by: Wolfgang Bumiller --- proxmox-rest-server/Cargo.toml | 1 - proxmox-rest-server/src/rest.rs | 89 +++++++++------------------------ 2 files changed, 24 insertions(+), 66 deletions(-) diff --git a/proxmox-rest-server/Cargo.toml b/proxmox-rest-server/Cargo.toml index 8238c1d2..24473927 100644 --- a/proxmox-rest-server/Cargo.toml +++ b/proxmox-rest-server/Cargo.toml @@ -38,7 +38,6 @@ proxmox-async.workspace = true proxmox-compression.workspace = true proxmox-io.workspace = true proxmox-lang.workspace = true -proxmox-http = { workspace = true, features = [ "client" ] } proxmox-router.workspace = true proxmox-schema = { workspace = true, features = [ "api-macro", "upid-api-impl" ] } proxmox-time.workspace = true diff --git a/proxmox-rest-server/src/rest.rs b/proxmox-rest-server/src/rest.rs index 96c35f09..57e97623 100644 --- a/proxmox-rest-server/src/rest.rs +++ b/proxmox-rest-server/src/rest.rs @@ -28,8 +28,6 @@ use proxmox_router::{ }; use proxmox_schema::{ObjectSchemaType, ParameterSchema}; -use proxmox_http::client::RateLimitedStream; - use proxmox_async::stream::AsyncReaderStream; use proxmox_compression::{DeflateEncoder, Level}; @@ -78,9 +76,7 @@ impl RestServer { } } -impl Service<&Pin>>>> - for RestServer -{ +impl Service<&T> for RestServer { type Response = ApiService; type Error = Error; type Future = Pin> + Send>>; @@ -89,82 +85,45 @@ impl Service<&Pin>>>, - ) -> Self::Future { - match ctx.get_ref().peer_addr() { - Err(err) => future::err(format_err!("unable to get peer address - {}", err)).boxed(), - Ok(peer) => future::ok(ApiService { + fn call(&mut self, ctx: &T) -> Self::Future { + let result = match ctx.peer_addr() { + Err(err) => Err(format_err!("unable to get peer address - {}", err)), + Ok(peer) => Ok(ApiService { peer, api_config: self.api_config.clone(), - }) - .boxed(), - } + }), + }; + Box::pin(async move { result }) } } -impl Service<&Pin>>> for RestServer { - type Response = ApiService; - type Error = Error; - type Future = Pin> + Send>>; +pub trait PeerAddress { + fn peer_addr(&self) -> Result; +} - fn poll_ready(&mut self, _cx: &mut Context) -> Poll> { - Poll::Ready(Ok(())) - } - - fn call( - &mut self, - ctx: &Pin>>, - ) -> Self::Future { - match ctx.get_ref().peer_addr() { - Err(err) => future::err(format_err!("unable to get peer address - {}", err)).boxed(), - Ok(peer) => future::ok(ApiService { - peer, - api_config: self.api_config.clone(), - }) - .boxed(), - } +impl PeerAddress for tokio_openssl::SslStream { + fn peer_addr(&self) -> Result { + self.get_ref().peer_addr() } } -impl Service<&hyper::server::conn::AddrStream> for RestServer { - type Response = ApiService; - type Error = Error; - type Future = Pin> + Send>>; - - fn poll_ready(&mut self, _cx: &mut Context) -> Poll> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, ctx: &hyper::server::conn::AddrStream) -> Self::Future { - let peer = ctx.remote_addr(); - future::ok(ApiService { - peer, - api_config: self.api_config.clone(), - }) - .boxed() +impl PeerAddress for tokio::net::TcpStream { + fn peer_addr(&self) -> Result { + Ok(self.peer_addr()?) } } -impl Service<&tokio::net::UnixStream> for RestServer { - type Response = ApiService; - type Error = Error; - type Future = Pin> + Send>>; - - fn poll_ready(&mut self, _cx: &mut Context) -> Poll> { - Poll::Ready(Ok(())) +impl PeerAddress for hyper::server::conn::AddrStream { + fn peer_addr(&self) -> Result { + Ok(self.remote_addr()) } +} - fn call(&mut self, _ctx: &tokio::net::UnixStream) -> Self::Future { +impl PeerAddress for tokio::net::UnixStream { + fn peer_addr(&self) -> Result { // TODO: Find a way to actually represent the vsock peer in the ApiService struct - for now // it doesn't really matter, so just use a fake IP address - let fake_peer = "0.0.0.0:807".parse().unwrap(); - future::ok(ApiService { - peer: fake_peer, - api_config: self.api_config.clone(), - }) - .boxed() + Ok(([0, 0, 0, 0], 807).into()) } }