Fixed: Use renewed mam_id from response to avoid invalid credentials after original one expires

This commit is contained in:
Bogdan
2024-08-27 05:46:54 +03:00
parent 7a5fa452f0
commit 123535b9a5
2 changed files with 36 additions and 18 deletions

View File

@@ -7,6 +7,7 @@ using System.Net;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using FluentValidation; using FluentValidation;
using FluentValidation.Results;
using Newtonsoft.Json; using Newtonsoft.Json;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
@@ -96,7 +97,23 @@ namespace NzbDrone.Core.Indexers.Definitions
protected override IDictionary<string, string> GetCookies() protected override IDictionary<string, string> GetCookies()
{ {
return CookieUtil.CookieHeaderToDictionary("mam_id=" + Settings.MamId); var cookies = base.GetCookies();
if (cookies is { Count: > 0 } && cookies.TryGetValue("mam_id", out var mamId) && mamId.IsNotNullOrWhiteSpace())
{
return cookies;
}
return CookieUtil.CookieHeaderToDictionary($"mam_id={Settings.MamId}");
}
protected override async Task<ValidationFailure> TestConnection()
{
UpdateCookies(null, null);
_logger.Debug("Cookies cleared.");
return await base.TestConnection().ConfigureAwait(false);
} }
private IndexerCapabilities SetCapabilities() private IndexerCapabilities SetCapabilities()
@@ -359,11 +376,12 @@ namespace NzbDrone.Core.Indexers.Definitions
private readonly IndexerCapabilitiesCategories _categories; private readonly IndexerCapabilitiesCategories _categories;
private readonly IIndexerHttpClient _httpClient; private readonly IIndexerHttpClient _httpClient;
private readonly Logger _logger; private readonly Logger _logger;
private readonly ICached<string> _userClassCache; private readonly ICached<string> _userClassCache;
private readonly HashSet<string> _vipFreeleechUserClasses = new (StringComparer.OrdinalIgnoreCase) private readonly HashSet<string> _vipFreeleechUserClasses = new (StringComparer.OrdinalIgnoreCase)
{ {
"VIP", "VIP",
"Elite VIP", "Elite VIP"
}; };
public MyAnonamouseParser(MyAnonamouseSettings settings, public MyAnonamouseParser(MyAnonamouseSettings settings,
@@ -376,32 +394,35 @@ namespace NzbDrone.Core.Indexers.Definitions
_categories = categories; _categories = categories;
_httpClient = httpClient; _httpClient = httpClient;
_logger = logger; _logger = logger;
_userClassCache = cacheManager.GetCache<string>(GetType()); _userClassCache = cacheManager.GetCache<string>(GetType());
} }
public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse) public IList<ReleaseInfo> ParseResponse(IndexerResponse indexerResponse)
{ {
var httpResponse = indexerResponse.HttpResponse;
// Throw auth errors here before we try to parse // Throw auth errors here before we try to parse
if (indexerResponse.HttpResponse.StatusCode == HttpStatusCode.Forbidden) if (httpResponse.StatusCode == HttpStatusCode.Forbidden)
{ {
throw new IndexerAuthException("[403 Forbidden] - mam_id expired or invalid"); throw new IndexerAuthException("[403 Forbidden] - mam_id expired or invalid");
} }
// Throw common http errors here before we try to parse // Throw common http errors here before we try to parse
if (indexerResponse.HttpResponse.StatusCode != HttpStatusCode.OK) if (httpResponse.StatusCode != HttpStatusCode.OK)
{ {
// Remove cookie cache // Remove cookie cache
CookiesUpdater(null, null); CookiesUpdater(null, null);
throw new IndexerException(indexerResponse, $"Unexpected response status {indexerResponse.HttpResponse.StatusCode} code from indexer request"); throw new IndexerException(indexerResponse, $"Unexpected response status {httpResponse.StatusCode} code from indexer request");
} }
if (!indexerResponse.HttpResponse.Headers.ContentType.Contains(HttpAccept.Json.Value)) if (!httpResponse.Headers.ContentType.Contains(HttpAccept.Json.Value))
{ {
// Remove cookie cache // Remove cookie cache
CookiesUpdater(null, null); CookiesUpdater(null, null);
throw new IndexerException(indexerResponse, $"Unexpected response header {indexerResponse.HttpResponse.Headers.ContentType} from indexer request, expected {HttpAccept.Json.Value}"); throw new IndexerException(indexerResponse, $"Unexpected response header {httpResponse.Headers.ContentType} from indexer request, expected {HttpAccept.Json.Value}");
} }
var releaseInfos = new List<ReleaseInfo>(); var releaseInfos = new List<ReleaseInfo>();
@@ -414,7 +435,7 @@ namespace NzbDrone.Core.Indexers.Definitions
return releaseInfos.ToArray(); return releaseInfos.ToArray();
} }
var hasUserVip = HasUserVip(); var hasUserVip = HasUserVip(httpResponse.GetCookies());
foreach (var item in jsonResponse.Data) foreach (var item in jsonResponse.Data)
{ {
@@ -482,10 +503,13 @@ namespace NzbDrone.Core.Indexers.Definitions
releaseInfos.Add(release); releaseInfos.Add(release);
} }
// Update cookies with the updated mam_id value received in the response
CookiesUpdater(httpResponse.GetCookies(), DateTime.Now.AddDays(30));
return releaseInfos.ToArray(); return releaseInfos.ToArray();
} }
private bool HasUserVip() private bool HasUserVip(Dictionary<string, string> cookies)
{ {
var cacheKey = "myanonamouse_user_class_" + _settings.ToJson().SHA256Hash(); var cacheKey = "myanonamouse_user_class_" + _settings.ToJson().SHA256Hash();
@@ -496,11 +520,10 @@ namespace NzbDrone.Core.Indexers.Definitions
var request = new HttpRequestBuilder(_settings.BaseUrl.Trim('/')) var request = new HttpRequestBuilder(_settings.BaseUrl.Trim('/'))
.Resource("/jsonLoad.php") .Resource("/jsonLoad.php")
.Accept(HttpAccept.Json) .Accept(HttpAccept.Json)
.SetCookies(cookies)
.Build(); .Build();
_logger.Debug("Fetching user data: " + request.Url.FullUri); _logger.Debug("Fetching user data: {0}", request.Url.FullUri);
request.Cookies.Add("mam_id", _settings.MamId);
var response = _httpClient.Get(request); var response = _httpClient.Get(request);
var jsonResponse = JsonConvert.DeserializeObject<MyAnonamouseUserDataResponse>(response.Content); var jsonResponse = JsonConvert.DeserializeObject<MyAnonamouseUserDataResponse>(response.Content);

View File

@@ -607,12 +607,7 @@ namespace NzbDrone.Core.Indexers
protected virtual bool CheckIfLoginNeeded(HttpResponse httpResponse) protected virtual bool CheckIfLoginNeeded(HttpResponse httpResponse)
{ {
if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) return httpResponse.StatusCode == HttpStatusCode.Unauthorized;
{
return true;
}
return false;
} }
protected virtual Task DoLogin() protected virtual Task DoLogin()