]> git.proxmox.com Git - proxmox-backup.git/commitdiff
rest: implement tower service for UnixStream
authorStefan Reiter <s.reiter@proxmox.com>
Tue, 16 Feb 2021 17:07:01 +0000 (18:07 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 17 Feb 2021 06:50:35 +0000 (07:50 +0100)
This allows anything that can be represented as a UnixStream to be used
as transport for an API server (e.g. virtio sockets).

A tower service expects an IP address as it's peer, which we can't
reliably provide for unix socket based transports, so just fake one.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
src/server/rest.rs

index fc59be9ab6e12c4c39f7039e3d6683b3539cf66c..9bf494fd68791bddc161e8588439bd52fc5bb85e 100644 (file)
@@ -107,6 +107,26 @@ impl tower_service::Service<&tokio::net::TcpStream> for RestServer {
     }
 }
 
+impl tower_service::Service<&tokio::net::UnixStream> for RestServer {
+    type Response = ApiService;
+    type Error = Error;
+    type Future = Pin<Box<dyn Future<Output = Result<ApiService, Error>> + Send>>;
+
+    fn poll_ready(&mut self, _cx: &mut Context) -> Poll<Result<(), Self::Error>> {
+        Poll::Ready(Ok(()))
+    }
+
+    fn call(&mut self, _ctx: &tokio::net::UnixStream) -> Self::Future {
+        // 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()
+    }
+}
+
 pub struct ApiService {
     pub peer: std::net::SocketAddr,
     pub api_config: Arc<ApiConfig>,