public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH backup 2/3] server/rest: also log user agent
Date: Fri, 16 Oct 2020 11:06:47 +0200	[thread overview]
Message-ID: <20201016090648.23887-2-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20201016090648.23887-1-t.lamprecht@proxmox.com>

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

Signed-off-by: Thomas Lamprecht <t.lamprecht@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





  reply	other threads:[~2020-10-16  9:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-16  9:06 [pbs-devel] [PATCH backup 1/3] server/rest: implement request access log Thomas Lamprecht
2020-10-16  9:06 ` Thomas Lamprecht [this message]
2020-10-16  9:06 ` [pbs-devel] [PATCH backup 3/3] api: access: log to separate file, reduce syslog to errors Thomas Lamprecht
2020-10-16  9:28 ` [pbs-devel] applied: [PATCH backup 1/3] server/rest: implement request access log Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201016090648.23887-2-t.lamprecht@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal