From e52adda2edb0776e899238c64bda852ca7246136 Mon Sep 17 00:00:00 2001 From: Erica Z Date: Thu, 7 Nov 2024 09:13:52 +0100 Subject: [PATCH] don't print auth token on mpv logs --- src/subsonic.rs | 6 ++++++ src/ui/window.rs | 19 +++++++++++++------ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/subsonic.rs b/src/subsonic.rs index ac2056c..5b0caf5 100644 --- a/src/subsonic.rs +++ b/src/subsonic.rs @@ -52,6 +52,7 @@ impl From for Error { pub struct Client { client: reqwest::Client, base_url: reqwest::Url, + token: String, } fn get_random_salt(length: usize) -> String { @@ -103,9 +104,14 @@ impl Client { .user_agent(crate::USER_AGENT) .build()?, base_url, + token: token.to_string(), }) } + pub fn strip_token(&self, string: &str) -> String { + string.replace(&self.token, "[authentication token]") + } + async fn send( &self, request: reqwest::RequestBuilder, diff --git a/src/ui/window.rs b/src/ui/window.rs index 2f3385c..d42900d 100644 --- a/src/ui/window.rs +++ b/src/ui/window.rs @@ -522,22 +522,29 @@ mod imp { fn on_log_message(&self, event: crate::mpv::event::LogMessageEvent) { let span = span!(Level::DEBUG, "mpv_log", prefix = event.prefix); let _guard = span.enter(); + + let event_text = event.text.trim_end_matches("\n"); // trim training newline + let event_text = match self.api.borrow().as_ref() { + None => event_text.to_string(), + Some(api) => api.strip_token(event_text), // hide auth token if printing url + }; + match event.log_level { // level has to be 'static so this sux l if l <= 20 => { - event!(target: "mpv_event", Level::ERROR, "{}", event.text.trim()) + event!(target: "mpv_event", Level::ERROR, "{}", event_text) } l if l <= 30 => { - event!(target: "mpv_event", Level::WARN, "{}", event.text.trim()) + event!(target: "mpv_event", Level::WARN, "{}", event_text) } l if l <= 40 => { - event!(target: "mpv_event", Level::INFO, "{}", event.text.trim()) + event!(target: "mpv_event", Level::INFO, "{}", event_text) } l if l <= 60 => { - event!(target: "mpv_event", Level::DEBUG, "{}", event.text.trim()) + event!(target: "mpv_event", Level::DEBUG, "{}", event_text) } l if l <= 70 => { - event!(target: "mpv_event", Level::TRACE, "{}", event.text.trim()) + event!(target: "mpv_event", Level::TRACE, "{}", event_text) } // should be unused _ => event!( @@ -545,7 +552,7 @@ mod imp { Level::DEBUG, log_level = event.log_level, "{}", - event.text.trim(), + event_text, ), }; }