fix: retry updating deobf data after a RustyPipe update

This commit is contained in:
ThetaDev 2025-02-05 11:46:13 +01:00
parent e04d1240d8
commit 5b4e12e794
2 changed files with 22 additions and 12 deletions

View file

@ -519,7 +519,6 @@ struct CacheHolder {
#[serde(default)] #[serde(default)]
struct CacheData { struct CacheData {
clients: HashMap<ClientType, CacheEntry<ClientData>>, clients: HashMap<ClientType, CacheEntry<ClientData>>,
#[serde(skip_serializing_if = "CacheEntry::is_none")]
deobf: CacheEntry<DeobfData>, deobf: CacheEntry<DeobfData>,
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
oauth_token: Option<OauthToken>, oauth_token: Option<OauthToken>,
@ -541,6 +540,9 @@ struct CacheEntry<T> {
skip_serializing_if = "Option::is_none" skip_serializing_if = "Option::is_none"
)] )]
retry_at: Option<OffsetDateTime>, retry_at: Option<OffsetDateTime>,
/// RustyPipe version that failed to updated the entry
#[serde(skip_serializing_if = "Option::is_none")]
failed_version: Option<String>,
data: Option<T>, data: Option<T>,
} }
@ -575,10 +577,21 @@ impl<T> CacheEntry<T> {
self.data.is_none() self.data.is_none()
} }
/// Retry updating a cache entry only after a delay or a RustyPipe update
fn should_retry(&self) -> bool { fn should_retry(&self) -> bool {
self.retry_at self.retry_at
.map(|d| OffsetDateTime::now_utc() > d) .map(|d| OffsetDateTime::now_utc() > d)
.unwrap_or(true) .unwrap_or(true)
|| self
.failed_version
.as_deref()
.map(|v| crate::VERSION != v)
.unwrap_or(true)
}
fn retry_later(&mut self, delay_h: i64) {
self.retry_at = Some(util::now_sec() + time::Duration::hours(delay_h));
self.failed_version = Some(crate::VERSION.to_owned());
} }
} }
@ -587,6 +600,7 @@ impl<T> From<T> for CacheEntry<T> {
Self { Self {
last_update: Some(util::now_sec()), last_update: Some(util::now_sec()),
retry_at: None, retry_at: None,
failed_version: None,
data: Some(f), data: Some(f),
} }
} }
@ -1071,7 +1085,7 @@ impl RustyPipe {
return version.into(); return version.into();
} }
Err(e) => { Err(e) => {
client.retry_at = Some(util::now_sec() + time::Duration::hours(1)); client.retry_later(1);
drop(client); drop(client);
self.store_cache().await; self.store_cache().await;
tracing::warn!( tracing::warn!(
@ -1108,11 +1122,7 @@ impl RustyPipe {
if deobf_data.should_retry() { if deobf_data.should_retry() {
tracing::debug!("getting deobf data"); tracing::debug!("getting deobf data");
match DeobfData::extract( match DeobfData::extract(&self.inner.http, self.inner.reporter.as_deref()).await
self.inner.http.clone(),
self.inner.reporter.as_deref(),
)
.await
{ {
Ok(new_data) => { Ok(new_data) => {
// Write new data to the cache // Write new data to the cache
@ -1123,7 +1133,7 @@ impl RustyPipe {
} }
Err(e) => { Err(e) => {
// Try to fall back to expired cache data if available, otherwise return error // Try to fall back to expired cache data if available, otherwise return error
deobf_data.retry_at = Some(util::now_sec() + time::Duration::days(1)); deobf_data.retry_later(24);
let res = match deobf_data.get_expired() { let res = match deobf_data.get_expired() {
Some(d) => { Some(d) => {
tracing::warn!("could not get new deobf data ({e}), falling back to expired cache"); tracing::warn!("could not get new deobf data ({e}), falling back to expired cache");

View file

@ -29,9 +29,9 @@ impl DeobfData {
/// Download and extract the latest deobfuscation data from YouTube /// Download and extract the latest deobfuscation data from YouTube
/// ///
/// Creates a report if the data could not be extracted /// Creates a report if the data could not be extracted
pub async fn extract(http: Client, reporter: Option<&dyn Reporter>) -> Result<Self, Error> { pub async fn extract(http: &Client, reporter: Option<&dyn Reporter>) -> Result<Self, Error> {
let js_url = get_player_js_url(&http).await?; let js_url = get_player_js_url(http).await?;
let player_js = get_response(&http, &js_url).await?; let player_js = get_response(http, &js_url).await?;
tracing::debug!("downloaded player.js from {}", js_url); tracing::debug!("downloaded player.js from {}", js_url);
let res = Self::extract_fns(&js_url, &player_js); let res = Self::extract_fns(&js_url, &player_js);
@ -690,7 +690,7 @@ c[36](c[8],c[32]),c[20](c[25],c[10]),c[2](c[22],c[8]),c[32](c[20],c[16]),c[32](c
#[traced_test] #[traced_test]
async fn t_update() { async fn t_update() {
let client = Client::new(); let client = Client::new();
let deobf_data = DeobfData::extract(client, None).await.unwrap(); let deobf_data = DeobfData::extract(&client, None).await.unwrap();
let deobf = Deobfuscator::new(&deobf_data).unwrap(); let deobf = Deobfuscator::new(&deobf_data).unwrap();
let deobf_sig = deobf.deobfuscate_sig("GOqGOqGOq0QJ8wRAIgaryQHfplJ9xJSKFywyaSMHuuwZYsoMTAvRvfm51qIGECIA5061zWeyfMPX9hEl_U6f9J0tr7GTJMKyPf5XNrJb5fb5i").unwrap(); let deobf_sig = deobf.deobfuscate_sig("GOqGOqGOq0QJ8wRAIgaryQHfplJ9xJSKFywyaSMHuuwZYsoMTAvRvfm51qIGECIA5061zWeyfMPX9hEl_U6f9J0tr7GTJMKyPf5XNrJb5fb5i").unwrap();