[pbs-devel] [PATCH backup 2/3] server/rest: also log user agent

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Oct 16 11:06:47 CEST 2020


allows easily to see if a request is from a browser or a proxmox-backup-client
CLI

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 src/server/rest.rs | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/server/rest.rs b/src/server/rest.rs
index 0b3bc067..7235a291 100644
--- a/src/server/rest.rs
+++ b/src/server/rest.rs
@@ -116,6 +116,7 @@ fn log_response(
     method: hyper::Method,
     path_query: &str,
     resp: &Response<Body>,
+    user_agent: Option<String>,
 ) {
 
     if resp.extensions().get::<NoLogExtension>().is_some() { return; };
@@ -150,7 +151,7 @@ fn log_response(
             .lock()
             .unwrap()
             .log(format!(
-                "{} - {} [{}] \"{} {}\" {} {}",
+                "{} - {} [{}] \"{} {}\" {} {} {}",
                 peer.ip(),
                 user,
                 datetime,
@@ -158,6 +159,7 @@ fn log_response(
                 path,
                 status.as_str(),
                 resp.body().size_hint().lower(),
+                user_agent.unwrap_or("-".into()),
             ));
     }
 }
@@ -173,6 +175,15 @@ fn get_proxied_peer(headers: &HeaderMap) -> Option<std::net::SocketAddr> {
     rhost.parse().ok()
 }
 
+fn get_user_agent(headers: &HeaderMap) -> Option<String> {
+    let agent = headers.get(header::USER_AGENT)?.to_str();
+    agent.map(|s| {
+        let mut s = s.to_owned();
+        s.truncate(128);
+        s
+    }).ok()
+}
+
 impl tower_service::Service<Request<Body>> for ApiService {
     type Response = Response<Body>;
     type Error = Error;
@@ -185,6 +196,7 @@ impl tower_service::Service<Request<Body>> for ApiService {
     fn call(&mut self, req: Request<Body>) -> Self::Future {
         let path = req.uri().path_and_query().unwrap().as_str().to_owned();
         let method = req.method().clone();
+        let user_agent = get_user_agent(req.headers());
 
         let config = Arc::clone(&self.api_config);
         let peer = match get_proxied_peer(req.headers()) {
@@ -203,7 +215,7 @@ impl tower_service::Service<Request<Body>> for ApiService {
                 }
             };
             let logger = config.get_file_log();
-            log_response(logger, &peer, method, &path, &response);
+            log_response(logger, &peer, method, &path, &response, user_agent);
             Ok(response)
         }
         .boxed()
-- 
2.27.0






More information about the pbs-devel mailing list