fix: object reference not set by adding retry for GetDecryptionKeyOFDL

This commit is contained in:
Spam.China 2025-11-29 15:01:21 +01:00
parent c147a19a0a
commit 2bddedb644

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,17 +2779,27 @@ public class APIHelper : IAPIHelper
Log.Debug($"Posting to ofdl.tools: {json}"); Log.Debug($"Posting to ofdl.tools: {json}");
HttpRequestMessage request = new(HttpMethod.Post, "https://ofdl.tools/WV") while (attempt < MaxAttempts)
{ {
Content = new StringContent(json, Encoding.UTF8, "application/json") attempt++;
};
using var response = await client.SendAsync(request); HttpRequestMessage request = new(HttpMethod.Post, "https://ofdl.tools/WV")
{
Content = new StringContent(json, Encoding.UTF8, "application/json")
};
using var response = await client.SendAsync(request);
if (!response.IsSuccessStatusCode)
continue;
if (response.IsSuccessStatusCode)
{
string body = await response.Content.ReadAsStringAsync(); string body = await response.Content.ReadAsStringAsync();
return body;
if (!body.TrimStart().StartsWith('{'))
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;
} }