Merge pull request 'fix: object reference not set by adding retry for GetDecryptionKeyOFDL' (#82) from TeenagerNeedsHelpQQ/OF-DL:master into master

Reviewed-on: #82
This commit is contained in:
sim0n00ps 2025-12-01 22:47:20 +00:00
commit 0bf8dfa8ab

View File

@ -31,6 +31,8 @@ public class APIHelper : IAPIHelper
private readonly Auth auth; private readonly Auth auth;
private static DateTime? cachedDynamicRulesExpiration; private static DateTime? cachedDynamicRulesExpiration;
private static DynamicRules? cachedDynamicRules; private static DynamicRules? cachedDynamicRules;
private const int MaxAttempts = 30;
private const int DelayBetweenAttempts = 3000;
static APIHelper() static APIHelper()
{ {
@ -2674,13 +2676,10 @@ public class APIHelper : IAPIHelper
return DateTime.Now; return DateTime.Now;
} }
public async Task<string> GetDecryptionKeyCDRMProject(Dictionary<string, string> drmHeaders, string licenceURL, string pssh) public async Task<string> GetDecryptionKeyCDRMProject(Dictionary<string, string> drmHeaders, string licenceURL, string pssh)
{ {
Log.Debug("Calling GetDecryptionKey"); Log.Debug("Calling GetDecryptionKey");
const int maxAttempts = 30;
const int delayBetweenAttempts = 3000;
int attempt = 0; int attempt = 0;
try try
@ -2701,7 +2700,7 @@ public class APIHelper : IAPIHelper
Log.Debug($"Posting to CDRM Project: {json}"); Log.Debug($"Posting to CDRM Project: {json}");
while (attempt < maxAttempts) while (attempt < MaxAttempts)
{ {
attempt++; attempt++;
@ -2727,19 +2726,19 @@ public class APIHelper : IAPIHelper
} }
else else
{ {
Log.Debug($"CDRM response status not successful. Retrying... Attempt {attempt} of {maxAttempts}"); Log.Debug($"CDRM response status not successful. Retrying... Attempt {attempt} of {MaxAttempts}");
if (attempt < maxAttempts) if (attempt < MaxAttempts)
{ {
await Task.Delay(delayBetweenAttempts); await Task.Delay(DelayBetweenAttempts);
} }
} }
} }
else else
{ {
Log.Debug($"Status not in CDRM response. Retrying... Attempt {attempt} of {maxAttempts}"); Log.Debug($"Status not in CDRM response. Retrying... Attempt {attempt} of {MaxAttempts}");
if (attempt < maxAttempts) if (attempt < MaxAttempts)
{ {
await Task.Delay(delayBetweenAttempts); await Task.Delay(DelayBetweenAttempts);
} }
} }
} }
@ -2764,11 +2763,10 @@ public class APIHelper : IAPIHelper
{ {
Log.Debug("Calling GetDecryptionOFDL"); Log.Debug("Calling GetDecryptionOFDL");
try try
{ {
string dcValue = string.Empty;
HttpClient client = new(); HttpClient client = new();
int attempt = 0;
OFDLRequest ofdlRequest = new OFDLRequest OFDLRequest ofdlRequest = new OFDLRequest
{ {
@ -2781,6 +2779,10 @@ public class APIHelper : IAPIHelper
Log.Debug($"Posting to ofdl.tools: {json}"); Log.Debug($"Posting to ofdl.tools: {json}");
while (attempt < MaxAttempts)
{
attempt++;
HttpRequestMessage request = new(HttpMethod.Post, "https://ofdl.tools/WV") HttpRequestMessage request = new(HttpMethod.Post, "https://ofdl.tools/WV")
{ {
Content = new StringContent(json, Encoding.UTF8, "application/json") Content = new StringContent(json, Encoding.UTF8, "application/json")
@ -2788,10 +2790,16 @@ public class APIHelper : IAPIHelper
using var response = await client.SendAsync(request); using var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
{ continue;
string body = await response.Content.ReadAsStringAsync(); string body = await response.Content.ReadAsStringAsync();
if (!body.TrimStart().StartsWith('{'))
return body; return body;
Log.Debug($"Received JSON object instead of string. Retrying... Attempt {attempt} of {MaxAttempts}");
await Task.Delay(DelayBetweenAttempts);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -2805,6 +2813,7 @@ public class APIHelper : IAPIHelper
Log.Error("Inner Exception: {0}\n\nStackTrace: {1}", ex.InnerException.Message, ex.InnerException.StackTrace); Log.Error("Inner Exception: {0}\n\nStackTrace: {1}", ex.InnerException.Message, ex.InnerException.StackTrace);
} }
} }
return null; return null;
} }