mirror of
https://github.com/Readarr/Readarr.git
synced 2026-04-27 22:56:45 -04:00
Fixed: Use Proxy for MediaCovers and Metadata
Signed-off-by: Robin Dadswell <robin@dadswell.email>
This commit is contained in:
@@ -99,9 +99,9 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>());
|
Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>());
|
||||||
|
|
||||||
// Used for manual testing of socks proxies.
|
// Used for manual testing of socks proxies.
|
||||||
//Mocker.GetMock<IHttpProxySettingsProvider>()
|
// Mocker.GetMock<IHttpProxySettingsProvider>()
|
||||||
// .Setup(v => v.GetProxySettings(It.IsAny<HttpRequest>()))
|
// .Setup(v => v.GetProxySettings(It.IsAny<HttpUri>()))
|
||||||
// .Returns(new HttpProxySettings(ProxyType.Socks5, "127.0.0.1", 5476, "", false));
|
// .Returns(new HttpProxySettings(ProxyType.Socks5, "127.0.0.1", 5476, "", false));
|
||||||
|
|
||||||
// Roundrobin over the two servers, to reduce the chance of hitting the ratelimiter.
|
// Roundrobin over the two servers, to reduce the chance of hitting the ratelimiter.
|
||||||
_httpBinHost2 = _httpBinHosts[_httpBinRandom++ % _httpBinHosts.Length];
|
_httpBinHost2 = _httpBinHosts[_httpBinRandom++ % _httpBinHosts.Length];
|
||||||
@@ -283,16 +283,31 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
{
|
{
|
||||||
var file = GetTempFilePath();
|
var file = GetTempFilePath();
|
||||||
|
|
||||||
var url = "https://sonarr.tv/img/slider/seriesdetails.png";
|
var url = "https://lidarr.audio/img/slider/artistdetails.png";
|
||||||
|
|
||||||
Subject.DownloadFile(url, file);
|
Subject.DownloadFile(url, file);
|
||||||
|
|
||||||
File.Exists(file).Should().BeTrue();
|
var fileInfo = new FileInfo(file);
|
||||||
File.Exists(file + ".part").Should().BeFalse();
|
fileInfo.Exists.Should().BeTrue();
|
||||||
|
fileInfo.Length.Should().Be(146122);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_download_file_with_redirect()
|
||||||
|
{
|
||||||
|
var file = GetTempFilePath();
|
||||||
|
|
||||||
|
var request = new HttpRequestBuilder($"https://{_httpBinHost}/redirect-to")
|
||||||
|
.AddQueryParam("url", $"https://lidarr.audio/img/slider/artistdetails.png")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
Subject.DownloadFile(request.Url.FullUri, file);
|
||||||
|
|
||||||
|
ExceptionVerification.ExpectedErrors(0);
|
||||||
|
|
||||||
var fileInfo = new FileInfo(file);
|
var fileInfo = new FileInfo(file);
|
||||||
|
fileInfo.Exists.Should().BeTrue();
|
||||||
fileInfo.Length.Should().Be(307054);
|
fileInfo.Length.Should().Be(146122);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -303,9 +318,8 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
Assert.Throws<WebException>(() => Subject.DownloadFile("https://download.readarr.com/wrongpath", file));
|
Assert.Throws<WebException>(() => Subject.DownloadFile("https://download.readarr.com/wrongpath", file));
|
||||||
|
|
||||||
File.Exists(file).Should().BeFalse();
|
File.Exists(file).Should().BeFalse();
|
||||||
File.Exists(file + ".part").Should().BeFalse();
|
|
||||||
|
|
||||||
ExceptionVerification.ExpectedWarns(0);
|
ExceptionVerification.ExpectedWarns(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -333,7 +347,7 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
var oldRequest = new HttpRequest($"https://{_httpBinHost2}/get");
|
var oldRequest = new HttpRequest($"https://{_httpBinHost2}/get");
|
||||||
oldRequest.Cookies["my"] = "cookie";
|
oldRequest.Cookies["my"] = "cookie";
|
||||||
|
|
||||||
var oldClient = new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<ICacheManager>(), Mocker.Resolve<IRateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), Mocker.GetMock<IUserAgentBuilder>().Object, Mocker.Resolve<Logger>());
|
var oldClient = new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<ICacheManager>(), Mocker.Resolve<IRateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), Mocker.Resolve<Logger>());
|
||||||
|
|
||||||
oldClient.Should().NotBeSameAs(Subject);
|
oldClient.Should().NotBeSameAs(Subject);
|
||||||
|
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ namespace NzbDrone.Common.Http.Dispatchers
|
|||||||
public interface IHttpDispatcher
|
public interface IHttpDispatcher
|
||||||
{
|
{
|
||||||
HttpResponse GetResponse(HttpRequest request, CookieContainer cookies);
|
HttpResponse GetResponse(HttpRequest request, CookieContainer cookies);
|
||||||
|
void DownloadFile(string url, string fileName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.IO.Compression;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -74,7 +76,7 @@ namespace NzbDrone.Common.Http.Dispatchers
|
|||||||
webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds);
|
webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
AddProxy(webRequest, request);
|
webRequest.Proxy = GetProxy(request.Url);
|
||||||
|
|
||||||
if (request.Headers != null)
|
if (request.Headers != null)
|
||||||
{
|
{
|
||||||
@@ -163,13 +165,54 @@ namespace NzbDrone.Common.Http.Dispatchers
|
|||||||
return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode);
|
return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void AddProxy(HttpWebRequest webRequest, HttpRequest request)
|
public void DownloadFile(string url, string fileName)
|
||||||
{
|
{
|
||||||
var proxySettings = _proxySettingsProvider.GetProxySettings(request);
|
try
|
||||||
|
{
|
||||||
|
var fileInfo = new FileInfo(fileName);
|
||||||
|
if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
|
||||||
|
{
|
||||||
|
fileInfo.Directory.Create();
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.Debug("Downloading [{0}] to [{1}]", url, fileName);
|
||||||
|
|
||||||
|
var stopWatch = Stopwatch.StartNew();
|
||||||
|
var uri = new HttpUri(url);
|
||||||
|
|
||||||
|
using (var webClient = new GZipWebClient())
|
||||||
|
{
|
||||||
|
webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent());
|
||||||
|
webClient.Proxy = GetProxy(uri);
|
||||||
|
webClient.DownloadFile(uri.FullUri, fileName);
|
||||||
|
stopWatch.Stop();
|
||||||
|
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (WebException e)
|
||||||
|
{
|
||||||
|
_logger.Warn("Failed to get response from: {0} {1}", url, e.Message);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.Warn(e, "Failed to get response from: " + url);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual IWebProxy GetProxy(HttpUri uri)
|
||||||
|
{
|
||||||
|
IWebProxy proxy = null;
|
||||||
|
|
||||||
|
var proxySettings = _proxySettingsProvider.GetProxySettings(uri);
|
||||||
|
|
||||||
if (proxySettings != null)
|
if (proxySettings != null)
|
||||||
{
|
{
|
||||||
webRequest.Proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
|
proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void AddRequestHeaders(HttpWebRequest webRequest, HttpHeader headers)
|
protected virtual void AddRequestHeaders(HttpWebRequest webRequest, HttpHeader headers)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using NLog;
|
using NLog;
|
||||||
@@ -33,19 +32,16 @@ namespace NzbDrone.Common.Http
|
|||||||
private readonly ICached<CookieContainer> _cookieContainerCache;
|
private readonly ICached<CookieContainer> _cookieContainerCache;
|
||||||
private readonly List<IHttpRequestInterceptor> _requestInterceptors;
|
private readonly List<IHttpRequestInterceptor> _requestInterceptors;
|
||||||
private readonly IHttpDispatcher _httpDispatcher;
|
private readonly IHttpDispatcher _httpDispatcher;
|
||||||
private readonly IUserAgentBuilder _userAgentBuilder;
|
|
||||||
|
|
||||||
public HttpClient(IEnumerable<IHttpRequestInterceptor> requestInterceptors,
|
public HttpClient(IEnumerable<IHttpRequestInterceptor> requestInterceptors,
|
||||||
ICacheManager cacheManager,
|
ICacheManager cacheManager,
|
||||||
IRateLimitService rateLimitService,
|
IRateLimitService rateLimitService,
|
||||||
IHttpDispatcher httpDispatcher,
|
IHttpDispatcher httpDispatcher,
|
||||||
IUserAgentBuilder userAgentBuilder,
|
|
||||||
Logger logger)
|
Logger logger)
|
||||||
{
|
{
|
||||||
_requestInterceptors = requestInterceptors.ToList();
|
_requestInterceptors = requestInterceptors.ToList();
|
||||||
_rateLimitService = rateLimitService;
|
_rateLimitService = rateLimitService;
|
||||||
_httpDispatcher = httpDispatcher;
|
_httpDispatcher = httpDispatcher;
|
||||||
_userAgentBuilder = userAgentBuilder;
|
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
ServicePointManager.DefaultConnectionLimit = 12;
|
ServicePointManager.DefaultConnectionLimit = 12;
|
||||||
@@ -231,48 +227,7 @@ namespace NzbDrone.Common.Http
|
|||||||
|
|
||||||
public void DownloadFile(string url, string fileName, string userAgent = null)
|
public void DownloadFile(string url, string fileName, string userAgent = null)
|
||||||
{
|
{
|
||||||
var fileNamePart = fileName + ".part";
|
_httpDispatcher.DownloadFile(url, fileName);
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var fileInfo = new FileInfo(fileName);
|
|
||||||
if (fileInfo.Directory != null && !fileInfo.Directory.Exists)
|
|
||||||
{
|
|
||||||
fileInfo.Directory.Create();
|
|
||||||
}
|
|
||||||
|
|
||||||
_logger.Debug("Downloading [{0}] to [{1}]", url, fileName);
|
|
||||||
|
|
||||||
var stopWatch = Stopwatch.StartNew();
|
|
||||||
using (var fileStream = new FileStream(fileNamePart, FileMode.Create, FileAccess.ReadWrite))
|
|
||||||
{
|
|
||||||
var request = new HttpRequest(url);
|
|
||||||
|
|
||||||
if (userAgent.IsNotNullOrWhiteSpace())
|
|
||||||
{
|
|
||||||
request.Headers.Set("User-Agent", userAgent);
|
|
||||||
}
|
|
||||||
|
|
||||||
request.ResponseStream = fileStream;
|
|
||||||
var response = Get(request);
|
|
||||||
}
|
|
||||||
|
|
||||||
stopWatch.Stop();
|
|
||||||
if (File.Exists(fileName))
|
|
||||||
{
|
|
||||||
File.Delete(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
File.Move(fileNamePart, fileName);
|
|
||||||
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (File.Exists(fileNamePart))
|
|
||||||
{
|
|
||||||
File.Delete(fileNamePart);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpResponse Get(HttpRequest request)
|
public HttpResponse Get(HttpRequest request)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ namespace NzbDrone.Common.Http.Proxy
|
|||||||
{
|
{
|
||||||
public interface IHttpProxySettingsProvider
|
public interface IHttpProxySettingsProvider
|
||||||
{
|
{
|
||||||
HttpProxySettings GetProxySettings(HttpRequest request);
|
HttpProxySettings GetProxySettings(HttpUri uri);
|
||||||
|
HttpProxySettings GetProxySettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ namespace NzbDrone.Core.Test.Framework
|
|||||||
Mocker.SetConstant<IHttpProxySettingsProvider>(new HttpProxySettingsProvider(Mocker.Resolve<ConfigService>()));
|
Mocker.SetConstant<IHttpProxySettingsProvider>(new HttpProxySettingsProvider(Mocker.Resolve<ConfigService>()));
|
||||||
Mocker.SetConstant<ICreateManagedWebProxy>(new ManagedWebProxyFactory(Mocker.Resolve<CacheManager>()));
|
Mocker.SetConstant<ICreateManagedWebProxy>(new ManagedWebProxyFactory(Mocker.Resolve<CacheManager>()));
|
||||||
Mocker.SetConstant<IHttpDispatcher>(new ManagedHttpDispatcher(Mocker.Resolve<IHttpProxySettingsProvider>(), Mocker.Resolve<ICreateManagedWebProxy>(), Mocker.Resolve<UserAgentBuilder>(), Mocker.Resolve<IPlatformInfo>(), TestLogger));
|
Mocker.SetConstant<IHttpDispatcher>(new ManagedHttpDispatcher(Mocker.Resolve<IHttpProxySettingsProvider>(), Mocker.Resolve<ICreateManagedWebProxy>(), Mocker.Resolve<UserAgentBuilder>(), Mocker.Resolve<IPlatformInfo>(), TestLogger));
|
||||||
Mocker.SetConstant<IHttpClient>(new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), Mocker.Resolve<UserAgentBuilder>(), TestLogger));
|
Mocker.SetConstant<IHttpClient>(new HttpClient(new IHttpRequestInterceptor[0], Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), TestLogger));
|
||||||
Mocker.SetConstant<IReadarrCloudRequestBuilder>(new ReadarrCloudRequestBuilder());
|
Mocker.SetConstant<IReadarrCloudRequestBuilder>(new ReadarrCloudRequestBuilder());
|
||||||
Mocker.SetConstant<IMetadataRequestBuilder>(Mocker.Resolve<MetadataRequestBuilder>());
|
Mocker.SetConstant<IMetadataRequestBuilder>(Mocker.Resolve<MetadataRequestBuilder>());
|
||||||
|
|
||||||
|
|||||||
@@ -15,27 +15,36 @@ namespace NzbDrone.Core.Http
|
|||||||
_configService = configService;
|
_configService = configService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpProxySettings GetProxySettings(HttpRequest request)
|
public HttpProxySettings GetProxySettings(HttpUri uri)
|
||||||
|
{
|
||||||
|
var proxySettings = GetProxySettings();
|
||||||
|
if (proxySettings == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ShouldProxyBeBypassed(proxySettings, uri))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return proxySettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpProxySettings GetProxySettings()
|
||||||
{
|
{
|
||||||
if (!_configService.ProxyEnabled)
|
if (!_configService.ProxyEnabled)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
var proxySettings = new HttpProxySettings(_configService.ProxyType,
|
return new HttpProxySettings(_configService.ProxyType,
|
||||||
_configService.ProxyHostname,
|
_configService.ProxyHostname,
|
||||||
_configService.ProxyPort,
|
_configService.ProxyPort,
|
||||||
_configService.ProxyBypassFilter,
|
_configService.ProxyBypassFilter,
|
||||||
_configService.ProxyBypassLocalAddresses,
|
_configService.ProxyBypassLocalAddresses,
|
||||||
_configService.ProxyUsername,
|
_configService.ProxyUsername,
|
||||||
_configService.ProxyPassword);
|
_configService.ProxyPassword);
|
||||||
|
|
||||||
if (ShouldProxyBeBypassed(proxySettings, request.Url))
|
|
||||||
{
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return proxySettings;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool ShouldProxyBeBypassed(HttpProxySettings proxySettings, HttpUri url)
|
public bool ShouldProxyBeBypassed(HttpProxySettings proxySettings, HttpUri url)
|
||||||
|
|||||||
Reference in New Issue
Block a user