don't print auth token on mpv logs

This commit is contained in:
Erica Z 2024-11-07 09:13:52 +01:00
parent 1f482ab512
commit e52adda2ed
2 changed files with 19 additions and 6 deletions

View file

@ -52,6 +52,7 @@ impl From<reqwest::Error> 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<T: serde::de::DeserializeOwned + Send + 'static>(
&self,
request: reqwest::RequestBuilder,

View file

@ -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,
),
};
}