cardigann: follow login redirect if domain doesn't change (#14544)

This commit is contained in:
Bogdan
2023-07-08 01:37:22 +03:00
committed by GitHub
parent 2c22fd4c0b
commit 5fce510adf
3 changed files with 47 additions and 20 deletions

View File

@@ -42,13 +42,12 @@ caps:
book-search: [q]
settings:
- name: cookie
- name: username
type: text
label: Cookie
- name: info
type: info
label: How to get the Cookie
default: "<ol><li>Login to this tracker with your browser</li><li>Open the <b>DevTools</b> panel by pressing <b>F12</b></li><li>Select the <b>Network</b> tab</li><li>Click on the <b>Doc</b> button (Chrome Browser) or <b>HTML</b> button (FireFox)</li><li>Refresh the page by pressing <b>F5</b></li><li>Click on the first row entry</li><li>Select the <b>Headers</b> tab on the Right panel</li><li>Find <b>'cookie:'</b> in the <b>Request Headers</b> section</li><li><b>Select</b> and <b>Copy</b> the whole cookie string <i>(everything after 'cookie: ')</i> and <b>Paste</b> here.</li></ol>"
label: Username
- name: password
type: password
label: Password
- name: freeleech
type: checkbox
label: Search freeleech only
@@ -75,12 +74,25 @@ settings:
default: For best results, change the <b>Torrents per page:</b> setting to <b>100</b> on your account profile.
login:
method: cookie
path: login.php
method: form
form: form[action="takelogin.php"]
inputs:
cookie: "{{ .Config.cookie }}"
username: "{{ .Config.username }}"
password: "{{ .Config.password }}"
captcha:
type: image
selector: img.cimage
input: captcha
error:
- selector: td.embedded:has(h2:contains("failed"))
- selector: td.embedded:has(h2:contains("Error"))
- selector: form[action="takelogin.php"]
message:
text: "Login page detected at {{ .Config.sitelink }}."
test:
path: my.php
selector: a[href="logout.php"]
path: index.php
selector: a[href="/my.php"]
download:
selectors:

View File

@@ -580,24 +580,29 @@ namespace Jackett.Common.Indexers
}
}
protected async Task FollowIfRedirect(WebResult response, string referrer = null, string overrideRedirectUrl = null, string overrideCookies = null, bool accumulateCookies = false)
protected async Task FollowIfRedirect(WebResult response, string referrer = null, string overrideRedirectUrl = null, string overrideCookies = null, bool accumulateCookies = false, int maxRedirects = 5)
{
// Follow up to 5 redirects
for (var i = 0; i < 5; i++)
for (var i = 0; i < maxRedirects; i++)
{
if (!response.IsRedirect)
{
break;
}
var redirectingTo = new Uri(response.RedirectingTo);
if (redirectingTo.Scheme == "magnet")
{
break;
}
await DoFollowIfRedirect(response, referrer, overrideRedirectUrl, overrideCookies, accumulateCookies);
if (accumulateCookies)
{
CookieHeader = ResolveCookies((CookieHeader != null && CookieHeader != "" ? CookieHeader + " " : "") + (overrideCookies != null && overrideCookies != "" ? overrideCookies + " " : "") + response.Cookies);
overrideCookies = response.Cookies = CookieHeader;
}
if (overrideCookies != null && response.Cookies == null)
{
response.Cookies = overrideCookies;

View File

@@ -849,16 +849,23 @@ namespace Jackett.Common.Indexers
var headers = ParseCustomHeaders(Definition.Login?.Headers ?? Definition.Search?.Headers, GetBaseTemplateVariables());
var testResult = await RequestWithCookiesAsync(LoginTestUrl, headers: headers);
// Follow the redirect on login if the domain doesn't change
if (testResult.IsRedirect && GetRedirectDomainHint(testResult) == null)
{
await FollowIfRedirect(testResult, LoginTestUrl, overrideCookies: testResult.Cookies, accumulateCookies: true, maxRedirects: 1);
}
if (testResult.IsRedirect)
{
var errormessage = $"Login Failed, got redirected to: {testResult.RedirectingTo}";
var DomainHint = GetRedirectDomainHint(testResult);
if (DomainHint != null)
var domainHint = GetRedirectDomainHint(testResult);
if (domainHint != null)
{
errormessage += " Try changing the indexer URL to " + DomainHint + ".";
errormessage += " Try changing the indexer URL to " + domainHint + ".";
if (Definition.Followredirect)
{
configData.SiteLink.Value = DomainHint;
configData.SiteLink.Value = domainHint;
SiteLink = configData.SiteLink.Value;
SaveConfig();
errormessage += " Updated site link, please try again.";
@@ -1925,12 +1932,15 @@ namespace Jackett.Common.Indexers
protected async Task<WebResult> HandleRedirectableRequestAsync(string url, Dictionary<string, string> headers = null, int maxRedirects = 5)
{
var response = await RequestWithCookiesAsync(url, headers: headers);
for (var i = 0; i < maxRedirects; i++)
{
if (response.IsRedirect)
response = await RequestWithCookiesAsync(response.RedirectingTo, headers: headers);
else
if (!response.IsRedirect)
{
break;
}
response = await RequestWithCookiesAsync(response.RedirectingTo, headers: headers);
}
return response;
}