From 2673d5a796d3d664dbb2dfde6eee50a24e8cd414 Mon Sep 17 00:00:00 2001 From: KZ Date: Sat, 25 Jul 2015 11:47:01 +0100 Subject: [PATCH] Fix SCC not saving and AlphaRatio --- src/Jackett/Controllers/APIController.cs | 6 + src/Jackett/Controllers/DownloadController.cs | 7 + src/Jackett/Indexers/AlphaRatio.cs | 122 +++++++----------- src/Jackett/Indexers/SceneAccess.cs | 5 +- src/Jackett/JackettModule.cs | 3 - 5 files changed, 66 insertions(+), 77 deletions(-) diff --git a/src/Jackett/Controllers/APIController.cs b/src/Jackett/Controllers/APIController.cs index e296706d5..0fd47e950 100644 --- a/src/Jackett/Controllers/APIController.cs +++ b/src/Jackett/Controllers/APIController.cs @@ -48,6 +48,12 @@ namespace Jackett.Controllers return Request.CreateResponse(HttpStatusCode.Forbidden, "Incorrect API key"); } + if (!indexer.IsConfigured) + { + logger.Warn(string.Format("Rejected a request to {0} which is unconfigured.", indexer.DisplayName)); + return Request.CreateResponse(HttpStatusCode.Forbidden, "This indexer is not configured."); + } + var releases = await indexer.PerformQuery(torznabQuery); if (string.IsNullOrWhiteSpace(torznabQuery.SanitizedSearchTerm)) diff --git a/src/Jackett/Controllers/DownloadController.cs b/src/Jackett/Controllers/DownloadController.cs index 1b7496b0c..e40fa1121 100644 --- a/src/Jackett/Controllers/DownloadController.cs +++ b/src/Jackett/Controllers/DownloadController.cs @@ -31,6 +31,13 @@ namespace Jackett.Controllers try { var indexer = indexerService.GetIndexer(indexerID); + + if (!indexer.IsConfigured) + { + logger.Warn(string.Format("Rejected a request to {0} which is unconfigured.", indexer.DisplayName)); + return Request.CreateResponse(HttpStatusCode.Forbidden, "This indexer is not configured."); + } + var remoteFile = Encoding.UTF8.GetString(HttpServerUtility.UrlTokenDecode(path)); var downloadBytes = await indexer.Download(new Uri(remoteFile)); diff --git a/src/Jackett/Indexers/AlphaRatio.cs b/src/Jackett/Indexers/AlphaRatio.cs index 0b12ebf8b..5a39584c5 100644 --- a/src/Jackett/Indexers/AlphaRatio.cs +++ b/src/Jackett/Indexers/AlphaRatio.cs @@ -13,6 +13,7 @@ using Jackett.Models; using Jackett.Utils; using NLog; using Jackett.Services; +using Jackett.Utils.Clients; namespace Jackett.Indexers { @@ -23,13 +24,10 @@ namespace Jackett.Indexers private readonly string DownloadUrl = ""; private readonly string GuidUrl = ""; - CookieContainer cookies; - HttpClientHandler handler; - HttpClient client; + private IWebClient webclient; + private string cookieHeader = ""; - string cookieHeader; - - public AlphaRatio(IIndexerManagerService i, Logger l) + public AlphaRatio(IIndexerManagerService i, IWebClient w, Logger l) : base(name: "AlphaRatio", description: "Legendary", link: new Uri("https://alpharatio.cc"), @@ -41,16 +39,7 @@ namespace Jackett.Indexers SearchUrl = SiteLink + "ajax.php?action=browse&searchstr="; DownloadUrl = SiteLink + "torrents.php?action=download&id="; GuidUrl = SiteLink + "torrents.php?torrentid="; - - cookies = new CookieContainer(); - handler = new HttpClientHandler - { - CookieContainer = cookies, - AllowAutoRedirect = true, - UseCookies = true, - - }; - client = new HttpClient(handler); + webclient = w; } public Task GetConfigurationForSetup() @@ -61,53 +50,48 @@ namespace Jackett.Indexers public async Task ApplyConfiguration(JToken configJson) { - var configSaveData = new JObject(); - SaveConfig(configSaveData); - - var config = new ConfigurationDataBasicLogin(); - config.LoadValuesFromJson(configJson); + var incomingConfig = new ConfigurationDataBasicLogin(); + incomingConfig.LoadValuesFromJson(configJson); var pairs = new Dictionary { - { "username", config.Username.Value }, - { "password", @config.Password.Value }, + { "username", incomingConfig.Username.Value }, + { "password", incomingConfig.Password.Value }, { "login", "Login" }, { "keeplogged", "1" } }; - var content = new FormUrlEncodedContent(pairs); - var message = CreateHttpRequest(new Uri(LoginUrl)); - message.Content = content; - - //message.Headers.Referrer = new Uri(LoginUrl); - string responseContent; - - configSaveData = new JObject(); - - if (Engine.IsWindows) + // Do the login + var response = await webclient.GetString(new Utils.Clients.WebRequest() { - // If Windows use .net http - var response = await client.SendAsync(message); - responseContent = await response.Content.ReadAsStringAsync(); - cookies.DumpToJson(SiteLink, configSaveData); - } - else + PostData = pairs, + Referer = LoginUrl, + Type = RequestType.POST, + Url = LoginUrl + }); + + cookieHeader = response.Cookies; + + if (response.Status == HttpStatusCode.Found || response.Status == HttpStatusCode.Redirect || response.Status == HttpStatusCode.RedirectMethod) { - // If UNIX system use curl, probably broken due to missing chromeUseragent record for CURL...cannot test - var response = await CurlHelper.PostAsync(LoginUrl, pairs); - responseContent = Encoding.UTF8.GetString(response.Content); - cookieHeader = response.CookieHeader; - configSaveData["cookie_header"] = cookieHeader; + response = await webclient.GetString(new Utils.Clients.WebRequest() + { + Url = SiteLink.ToString(), + Referer = LoginUrl.ToString(), + Cookies = cookieHeader + }); } - if (!responseContent.Contains("logout.php?")) + if (!response.Content.Contains("logout.php?")) { - CQ dom = responseContent; + CQ dom = response.Content; dom["#loginform > table"].Remove(); var errorMessage = dom["#loginform"].Text().Trim().Replace("\n\t", " "); - throw new ExceptionWithConfigData(errorMessage, (ConfigurationData)config); + throw new ExceptionWithConfigData(errorMessage, (ConfigurationData)incomingConfig); } else { + var configSaveData = new JObject(); + configSaveData["cookie_header"] = cookieHeader; SaveConfig(configSaveData); IsConfigured = true; } @@ -124,9 +108,11 @@ namespace Jackett.Indexers public void LoadFromSavedConfiguration(JToken jsonConfig) { - cookies.FillFromJson(SiteLink, jsonConfig, logger); - cookieHeader = cookies.GetCookieHeader(SiteLink); - IsConfigured = true; + cookieHeader = (string)jsonConfig["cookie_header"]; + if (!string.IsNullOrEmpty(cookieHeader)) + { + IsConfigured = true; + } } void FillReleaseInfoFromJson(ReleaseInfo release, JObject r) @@ -146,23 +132,18 @@ namespace Jackett.Indexers var searchString = query.SanitizedSearchTerm + " " + query.GetEpisodeSearchString(); var episodeSearchUrl = SearchUrl + HttpUtility.UrlEncode(searchString); - string results; - if (Engine.IsWindows) + var results = await webclient.GetString(new Utils.Clients.WebRequest() { - var request = CreateHttpRequest(new Uri(episodeSearchUrl)); - request.Method = HttpMethod.Get; - var response = await client.SendAsync(request); - results = await response.Content.ReadAsStringAsync(); - } - else - { - var response = await CurlHelper.GetAsync(episodeSearchUrl, cookieHeader); - results = Encoding.UTF8.GetString(response.Content); - } + Cookies = cookieHeader, + Type = RequestType.GET, + Url = episodeSearchUrl + }); + + try { - var json = JObject.Parse(results); + var json = JObject.Parse(results.Content); foreach (JObject r in json["response"]["results"]) { DateTime pubDate = DateTime.MinValue; @@ -198,7 +179,7 @@ namespace Jackett.Indexers } catch (Exception ex) { - OnParseError(results, ex); + OnParseError(results.Content, ex); } return releases.ToArray(); @@ -213,16 +194,13 @@ namespace Jackett.Indexers public async Task Download(Uri link) { - if (Engine.IsWindows) + var response = await webclient.GetBytes(new Utils.Clients.WebRequest() { - return await client.GetByteArrayAsync(link); - } - else - { - var response = await CurlHelper.GetAsync(link.ToString(), cookieHeader); - return response.Content; - } + Url = link.ToString(), + Cookies = cookieHeader + }); + return response.Content; } } diff --git a/src/Jackett/Indexers/SceneAccess.cs b/src/Jackett/Indexers/SceneAccess.cs index 743125971..d8056080d 100644 --- a/src/Jackett/Indexers/SceneAccess.cs +++ b/src/Jackett/Indexers/SceneAccess.cs @@ -86,6 +86,7 @@ namespace Jackett.Indexers else { var configSaveData = new JObject(); + configSaveData["cookie_header"] = cookieHeader; SaveConfig(configSaveData); IsConfigured = true; } @@ -96,8 +97,8 @@ namespace Jackett.Indexers cookieHeader = (string)jsonConfig["cookie_header"]; if (!string.IsNullOrEmpty(cookieHeader)) { - IsConfigured = true; - } + IsConfigured = true; + } } public async Task PerformQuery(TorznabQuery query) diff --git a/src/Jackett/JackettModule.cs b/src/Jackett/JackettModule.cs index 9db44e980..23cfae951 100644 --- a/src/Jackett/JackettModule.cs +++ b/src/Jackett/JackettModule.cs @@ -24,17 +24,14 @@ namespace Jackett if (Startup.CurlSafe) { builder.RegisterType().As(); - Console.WriteLine("Using UnixSafeCurlWebClient"); } else if(System.Environment.OSVersion.Platform == PlatformID.Unix) { builder.RegisterType().As(); - Console.WriteLine("Using UnixLibCurlWebClient"); } else { builder.RegisterType().As(); - Console.WriteLine("Using WindowsWebClient"); } // Register indexers