Fixed: (Caridgann) Custom headers in login and download blocks

This commit is contained in:
Bogdan
2023-02-20 13:54:38 +02:00
parent 35f85fc986
commit 0ec54906c6
3 changed files with 39 additions and 31 deletions

View File

@@ -763,8 +763,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
return data; return data;
} }
protected Dictionary<string, string> ParseCustomHeaders(Dictionary<string, List<string>> customHeaders, protected Dictionary<string, string> ParseCustomHeaders(Dictionary<string, List<string>> customHeaders, Dictionary<string, object> variables)
Dictionary<string, object> variables)
{ {
if (customHeaders == null) if (customHeaders == null)
{ {

View File

@@ -102,6 +102,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
public List<ErrorBlock> Error { get; set; } public List<ErrorBlock> Error { get; set; }
public PageTestBlock Test { get; set; } public PageTestBlock Test { get; set; }
public CaptchaBlock Captcha { get; set; } public CaptchaBlock Captcha { get; set; }
public Dictionary<string, List<string>> Headers { get; set; }
} }
public class ErrorBlock public class ErrorBlock
@@ -182,6 +183,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
public string Method { get; set; } public string Method { get; set; }
public BeforeBlock Before { get; set; } public BeforeBlock Before { get; set; }
public InfohashBlock Infohash { get; set; } public InfohashBlock Infohash { get; set; }
public Dictionary<string, List<string>> Headers { get; set; }
} }
public class InfohashBlock public class InfohashBlock

View File

@@ -193,6 +193,9 @@ namespace NzbDrone.Core.Indexers.Cardigann
{ {
var login = _definition.Login; var login = _definition.Login;
var variables = GetBaseTemplateVariables();
var headers = ParseCustomHeaders(_definition.Login?.Headers ?? _definition.Search?.Headers, variables);
if (login.Method == "post") if (login.Method == "post")
{ {
var pairs = new Dictionary<string, string>(); var pairs = new Dictionary<string, string>();
@@ -221,7 +224,15 @@ namespace NzbDrone.Core.Indexers.Cardigann
requestBuilder.AddFormParameter(pair.Key, pair.Value); requestBuilder.AddFormParameter(pair.Key, pair.Value);
} }
Cookies = null;
if (login.Cookies != null)
{
Cookies = CookieUtil.CookieHeaderToDictionary(string.Join("; ", login.Cookies));
}
var request = requestBuilder var request = requestBuilder
.SetCookies(Cookies ?? new Dictionary<string, string>())
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", SiteLink) .SetHeader("Referer", SiteLink)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -241,13 +252,9 @@ namespace NzbDrone.Core.Indexers.Cardigann
var queryCollection = new NameValueCollection(); var queryCollection = new NameValueCollection();
var pairs = new Dictionary<string, string>(); var pairs = new Dictionary<string, string>();
var formSelector = login.Form; var formSelector = login.Form ?? "form";
if (formSelector == null)
{
formSelector = "form";
}
// landingResultDocument might not be initiated if the login is caused by a relogin during a query // landingResultDocument might not be initiated if the login is caused by a re-login during a query
if (landingResultDocument == null) if (landingResultDocument == null)
{ {
await GetConfigurationForSetup(true); await GetConfigurationForSetup(true);
@@ -279,11 +286,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
continue; continue;
} }
var value = input.GetAttribute("value"); var value = input.GetAttribute("value") ?? "";
if (value == null)
{
value = "";
}
pairs[name] = value; pairs[name] = value;
} }
@@ -364,6 +367,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
var request = requestBuilder var request = requestBuilder
.SetCookies(Cookies) .SetCookies(Cookies)
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", loginUrl) .SetHeader("Referer", loginUrl)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -406,7 +410,6 @@ namespace NzbDrone.Core.Indexers.Cardigann
var enctype = form.GetAttribute("enctype"); var enctype = form.GetAttribute("enctype");
if (enctype == "multipart/form-data") if (enctype == "multipart/form-data")
{ {
var headers = new Dictionary<string, string>();
var boundary = "---------------------------" + DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString().Replace(".", ""); var boundary = "---------------------------" + DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds.ToString().Replace(".", "");
var bodyParts = new List<string>(); var bodyParts = new List<string>();
@@ -437,13 +440,9 @@ namespace NzbDrone.Core.Indexers.Cardigann
requestBuilder.AddFormParameter(pair.Key, pair.Value); requestBuilder.AddFormParameter(pair.Key, pair.Value);
} }
foreach (var header in headers)
{
requestBuilder.SetHeader(header.Key, header.Value);
}
var request = requestBuilder var request = requestBuilder
.SetCookies(Cookies) .SetCookies(Cookies)
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", SiteLink) .SetHeader("Referer", SiteLink)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -470,6 +469,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
var request = requestBuilder var request = requestBuilder
.SetCookies(Cookies) .SetCookies(Cookies)
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", loginUrl) .SetHeader("Referer", loginUrl)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -509,6 +509,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
}; };
var request = requestBuilder var request = requestBuilder
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", SiteLink) .SetHeader("Referer", SiteLink)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -537,6 +538,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
}; };
var request = requestBuilder var request = requestBuilder
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", SiteLink) .SetHeader("Referer", SiteLink)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -551,7 +553,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
} }
else else
{ {
throw new NotImplementedException("Login method " + login.Method + " not implemented"); throw new NotImplementedException($"Login method {login.Method} not implemented");
} }
} }
@@ -596,15 +598,11 @@ namespace NzbDrone.Core.Indexers.Cardigann
return null; return null;
} }
var variables = GetBaseTemplateVariables();
var headers = ParseCustomHeaders(_definition.Login?.Headers ?? _definition.Search?.Headers, variables);
var loginUrl = ResolvePath(login.Path); var loginUrl = ResolvePath(login.Path);
Cookies = null;
if (login.Cookies != null)
{
Cookies = CookieUtil.CookieHeaderToDictionary(string.Join("; ", login.Cookies));
}
var requestBuilder = new HttpRequestBuilder(loginUrl.AbsoluteUri) var requestBuilder = new HttpRequestBuilder(loginUrl.AbsoluteUri)
{ {
LogResponseContent = true, LogResponseContent = true,
@@ -612,12 +610,15 @@ namespace NzbDrone.Core.Indexers.Cardigann
Encoding = _encoding Encoding = _encoding
}; };
if (Cookies != null) Cookies = null;
if (login.Cookies != null)
{ {
requestBuilder.SetCookies(Cookies); Cookies = CookieUtil.CookieHeaderToDictionary(string.Join("; ", login.Cookies));
} }
var request = requestBuilder var request = requestBuilder
.SetCookies(Cookies ?? new Dictionary<string, string>())
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", SiteLink) .SetHeader("Referer", SiteLink)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -653,6 +654,9 @@ namespace NzbDrone.Core.Indexers.Cardigann
{ {
var captcha = login.Captcha; var captcha = login.Captcha;
var variables = GetBaseTemplateVariables();
var headers = ParseCustomHeaders(_definition.Login?.Headers ?? _definition.Search?.Headers, variables);
if (captcha.Type == "image") if (captcha.Type == "image")
{ {
var captchaElement = landingResultDocument.QuerySelector(captcha.Selector); var captchaElement = landingResultDocument.QuerySelector(captcha.Selector);
@@ -663,6 +667,7 @@ namespace NzbDrone.Core.Indexers.Cardigann
var request = new HttpRequestBuilder(captchaUrl.ToString()) var request = new HttpRequestBuilder(captchaUrl.ToString())
.SetCookies(landingResult.GetCookies()) .SetCookies(landingResult.GetCookies())
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", loginUrl.AbsoluteUri) .SetHeader("Referer", loginUrl.AbsoluteUri)
.SetEncoding(_encoding) .SetEncoding(_encoding)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
@@ -757,8 +762,11 @@ namespace NzbDrone.Core.Indexers.Cardigann
} }
} }
var headers = ParseCustomHeaders(_definition.Download?.Headers ?? _definition.Search?.Headers, variables);
var httpRequest = httpRequestBuilder var httpRequest = httpRequestBuilder
.SetCookies(Cookies ?? new Dictionary<string, string>()) .SetCookies(Cookies ?? new Dictionary<string, string>())
.SetHeaders(headers ?? new Dictionary<string, string>())
.SetHeader("Referer", referer) .SetHeader("Referer", referer)
.WithRateLimit(_rateLimit.TotalSeconds) .WithRateLimit(_rateLimit.TotalSeconds)
.Build(); .Build();
@@ -773,11 +781,10 @@ namespace NzbDrone.Core.Indexers.Cardigann
{ {
Cookies = GetCookies(); Cookies = GetCookies();
var method = HttpMethod.Get; var method = HttpMethod.Get;
var headers = new Dictionary<string, string>();
var variables = GetBaseTemplateVariables(); var variables = GetBaseTemplateVariables();
AddTemplateVariablesFromUri(variables, link, ".DownloadUri"); AddTemplateVariablesFromUri(variables, link, ".DownloadUri");
headers = ParseCustomHeaders(_definition.Search?.Headers, variables); var headers = ParseCustomHeaders(_definition.Download?.Headers ?? _definition.Search?.Headers, variables);
if (_definition.Download != null) if (_definition.Download != null)
{ {