mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
Modern HTTP Client (#685)
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Globalization;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
using Moq;
|
using Moq;
|
||||||
@@ -15,8 +16,11 @@ using NzbDrone.Common.Http;
|
|||||||
using NzbDrone.Common.Http.Dispatchers;
|
using NzbDrone.Common.Http.Dispatchers;
|
||||||
using NzbDrone.Common.Http.Proxy;
|
using NzbDrone.Common.Http.Proxy;
|
||||||
using NzbDrone.Common.TPL;
|
using NzbDrone.Common.TPL;
|
||||||
|
using NzbDrone.Core.Configuration;
|
||||||
|
using NzbDrone.Core.Security;
|
||||||
using NzbDrone.Test.Common;
|
using NzbDrone.Test.Common;
|
||||||
using NzbDrone.Test.Common.Categories;
|
using NzbDrone.Test.Common.Categories;
|
||||||
|
using HttpClient = NzbDrone.Common.Http.HttpClient;
|
||||||
|
|
||||||
namespace NzbDrone.Common.Test.Http
|
namespace NzbDrone.Common.Test.Http
|
||||||
{
|
{
|
||||||
@@ -31,6 +35,8 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
private string _httpBinHost;
|
private string _httpBinHost;
|
||||||
private string _httpBinHost2;
|
private string _httpBinHost2;
|
||||||
|
|
||||||
|
private System.Net.Http.HttpClient _httpClient = new ();
|
||||||
|
|
||||||
[OneTimeSetUp]
|
[OneTimeSetUp]
|
||||||
public void FixtureSetUp()
|
public void FixtureSetUp()
|
||||||
{
|
{
|
||||||
@@ -38,7 +44,7 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
var mainHost = "httpbin.servarr.com";
|
var mainHost = "httpbin.servarr.com";
|
||||||
|
|
||||||
// Use mirrors for tests that use two hosts
|
// Use mirrors for tests that use two hosts
|
||||||
var candidates = new[] { "eu.httpbin.org", /* "httpbin.org", */ "www.httpbin.org" };
|
var candidates = new[] { "httpbin1.servarr.com" };
|
||||||
|
|
||||||
// httpbin.org is broken right now, occassionally redirecting to https if it's unavailable.
|
// httpbin.org is broken right now, occassionally redirecting to https if it's unavailable.
|
||||||
_httpBinHost = mainHost;
|
_httpBinHost = mainHost;
|
||||||
@@ -46,29 +52,20 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
|
|
||||||
TestLogger.Info($"{candidates.Length} TestSites available.");
|
TestLogger.Info($"{candidates.Length} TestSites available.");
|
||||||
|
|
||||||
_httpBinSleep = _httpBinHosts.Length < 2 ? 100 : 10;
|
_httpBinSleep = 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsTestSiteAvailable(string site)
|
private bool IsTestSiteAvailable(string site)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var req = WebRequest.Create($"https://{site}/get") as HttpWebRequest;
|
var res = _httpClient.GetAsync($"https://{site}/get").GetAwaiter().GetResult();
|
||||||
var res = req.GetResponse() as HttpWebResponse;
|
|
||||||
if (res.StatusCode != HttpStatusCode.OK)
|
if (res.StatusCode != HttpStatusCode.OK)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
res = _httpClient.GetAsync($"https://{site}/status/429").GetAwaiter().GetResult();
|
||||||
{
|
|
||||||
req = WebRequest.Create($"https://{site}/status/429") as HttpWebRequest;
|
|
||||||
res = req.GetResponse() as HttpWebResponse;
|
|
||||||
}
|
|
||||||
catch (WebException ex)
|
|
||||||
{
|
|
||||||
res = ex.Response as HttpWebResponse;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res == null || res.StatusCode != (HttpStatusCode)429)
|
if (res == null || res.StatusCode != (HttpStatusCode)429)
|
||||||
{
|
{
|
||||||
@@ -95,10 +92,13 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
Mocker.GetMock<IOsInfo>().Setup(c => c.Name).Returns("TestOS");
|
Mocker.GetMock<IOsInfo>().Setup(c => c.Name).Returns("TestOS");
|
||||||
Mocker.GetMock<IOsInfo>().Setup(c => c.Version).Returns("9.0.0");
|
Mocker.GetMock<IOsInfo>().Setup(c => c.Version).Returns("9.0.0");
|
||||||
|
|
||||||
|
Mocker.GetMock<IConfigService>().SetupGet(x => x.CertificateValidation).Returns(CertificateValidationType.Enabled);
|
||||||
|
|
||||||
Mocker.SetConstant<IUserAgentBuilder>(Mocker.Resolve<UserAgentBuilder>());
|
Mocker.SetConstant<IUserAgentBuilder>(Mocker.Resolve<UserAgentBuilder>());
|
||||||
|
|
||||||
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
|
Mocker.SetConstant<ICacheManager>(Mocker.Resolve<CacheManager>());
|
||||||
Mocker.SetConstant<ICreateManagedWebProxy>(Mocker.Resolve<ManagedWebProxyFactory>());
|
Mocker.SetConstant<ICreateManagedWebProxy>(Mocker.Resolve<ManagedWebProxyFactory>());
|
||||||
|
Mocker.SetConstant<ICertificateValidationService>(new X509CertificateValidationService(Mocker.GetMock<IConfigService>().Object, TestLogger));
|
||||||
Mocker.SetConstant<IRateLimitService>(Mocker.Resolve<RateLimitService>());
|
Mocker.SetConstant<IRateLimitService>(Mocker.Resolve<RateLimitService>());
|
||||||
Mocker.SetConstant<IEnumerable<IHttpRequestInterceptor>>(Array.Empty<IHttpRequestInterceptor>());
|
Mocker.SetConstant<IEnumerable<IHttpRequestInterceptor>>(Array.Empty<IHttpRequestInterceptor>());
|
||||||
Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>());
|
Mocker.SetConstant<IHttpDispatcher>(Mocker.Resolve<TDispatcher>());
|
||||||
@@ -138,6 +138,28 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
response.Content.Should().NotBeNullOrWhiteSpace();
|
response.Content.Should().NotBeNullOrWhiteSpace();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[TestCase(CertificateValidationType.Enabled)]
|
||||||
|
[TestCase(CertificateValidationType.DisabledForLocalAddresses)]
|
||||||
|
public void bad_ssl_should_fail_when_remote_validation_enabled(CertificateValidationType validationType)
|
||||||
|
{
|
||||||
|
Mocker.GetMock<IConfigService>().SetupGet(x => x.CertificateValidation).Returns(validationType);
|
||||||
|
var request = new HttpRequest($"https://expired.badssl.com");
|
||||||
|
|
||||||
|
Assert.Throws<HttpRequestException>(() => Subject.Execute(request));
|
||||||
|
ExceptionVerification.ExpectedErrors(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void bad_ssl_should_pass_if_remote_validation_disabled()
|
||||||
|
{
|
||||||
|
Mocker.GetMock<IConfigService>().SetupGet(x => x.CertificateValidation).Returns(CertificateValidationType.Disabled);
|
||||||
|
|
||||||
|
var request = new HttpRequest($"https://expired.badssl.com");
|
||||||
|
|
||||||
|
Subject.Execute(request);
|
||||||
|
ExceptionVerification.ExpectedErrors(0);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_execute_typed_get()
|
public void should_execute_typed_get()
|
||||||
{
|
{
|
||||||
@@ -162,15 +184,44 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
response.Resource.Data.Should().Be(message);
|
response.Resource.Data.Should().Be(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("gzip")]
|
[Test]
|
||||||
public void should_execute_get_using_gzip(string compression)
|
public void should_execute_post_with_content_type()
|
||||||
{
|
{
|
||||||
var request = new HttpRequest($"https://{_httpBinHost}/{compression}");
|
var message = "{ my: 1 }";
|
||||||
|
|
||||||
|
var request = new HttpRequest($"https://{_httpBinHost}/post");
|
||||||
|
request.SetContent(message);
|
||||||
|
request.Headers.ContentType = "application/json";
|
||||||
|
|
||||||
|
var response = Subject.Post<HttpBinResource>(request);
|
||||||
|
|
||||||
|
response.Resource.Data.Should().Be(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_execute_get_using_gzip()
|
||||||
|
{
|
||||||
|
var request = new HttpRequest($"https://{_httpBinHost}/gzip");
|
||||||
|
|
||||||
var response = Subject.Get<HttpBinResource>(request);
|
var response = Subject.Get<HttpBinResource>(request);
|
||||||
|
|
||||||
response.Resource.Headers["Accept-Encoding"].ToString().Should().Be(compression);
|
response.Resource.Headers["Accept-Encoding"].ToString().Should().Contain("gzip");
|
||||||
|
|
||||||
response.Resource.Gzipped.Should().BeTrue();
|
response.Resource.Gzipped.Should().BeTrue();
|
||||||
|
response.Resource.Brotli.Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_execute_get_using_brotli()
|
||||||
|
{
|
||||||
|
var request = new HttpRequest($"https://{_httpBinHost}/brotli");
|
||||||
|
|
||||||
|
var response = Subject.Get<HttpBinResource>(request);
|
||||||
|
|
||||||
|
response.Resource.Headers["Accept-Encoding"].ToString().Should().Contain("br");
|
||||||
|
|
||||||
|
response.Resource.Gzipped.Should().BeFalse();
|
||||||
|
response.Resource.Brotli.Should().BeTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(HttpStatusCode.Unauthorized)]
|
[TestCase(HttpStatusCode.Unauthorized)]
|
||||||
@@ -190,6 +241,28 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
ExceptionVerification.IgnoreWarns();
|
ExceptionVerification.IgnoreWarns();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_not_throw_on_suppressed_status_codes()
|
||||||
|
{
|
||||||
|
var request = new HttpRequest($"https://{_httpBinHost}/status/{HttpStatusCode.NotFound}");
|
||||||
|
request.SuppressHttpErrorStatusCodes = new[] { HttpStatusCode.NotFound };
|
||||||
|
|
||||||
|
Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
|
||||||
|
|
||||||
|
ExceptionVerification.IgnoreWarns();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_not_log_unsuccessful_status_codes()
|
||||||
|
{
|
||||||
|
var request = new HttpRequest($"https://{_httpBinHost}/status/{HttpStatusCode.NotFound}");
|
||||||
|
request.LogHttpError = false;
|
||||||
|
|
||||||
|
Assert.Throws<HttpException>(() => Subject.Get<HttpBinResource>(request));
|
||||||
|
|
||||||
|
ExceptionVerification.ExpectedWarns(0);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_not_follow_redirects_when_not_in_production()
|
public void should_not_follow_redirects_when_not_in_production()
|
||||||
{
|
{
|
||||||
@@ -315,13 +388,38 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
{
|
{
|
||||||
var file = GetTempFilePath();
|
var file = GetTempFilePath();
|
||||||
|
|
||||||
Assert.Throws<WebException>(() => Subject.DownloadFile("https://download.sonarr.tv/wrongpath", file));
|
Assert.Throws<HttpException>(() => Subject.DownloadFile("https://download.sonarr.tv/wrongpath", file));
|
||||||
|
|
||||||
File.Exists(file).Should().BeFalse();
|
File.Exists(file).Should().BeFalse();
|
||||||
|
|
||||||
ExceptionVerification.ExpectedWarns(1);
|
ExceptionVerification.ExpectedWarns(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void should_not_write_redirect_content_to_stream()
|
||||||
|
{
|
||||||
|
var file = GetTempFilePath();
|
||||||
|
|
||||||
|
using (var fileStream = new FileStream(file, FileMode.Create))
|
||||||
|
{
|
||||||
|
var request = new HttpRequest($"http://{_httpBinHost}/redirect/1");
|
||||||
|
request.AllowAutoRedirect = false;
|
||||||
|
request.ResponseStream = fileStream;
|
||||||
|
|
||||||
|
var response = Subject.Get(request);
|
||||||
|
|
||||||
|
response.StatusCode.Should().Be(HttpStatusCode.Moved);
|
||||||
|
}
|
||||||
|
|
||||||
|
ExceptionVerification.ExpectedErrors(1);
|
||||||
|
|
||||||
|
File.Exists(file).Should().BeTrue();
|
||||||
|
|
||||||
|
var fileInfo = new FileInfo(file);
|
||||||
|
|
||||||
|
fileInfo.Length.Should().Be(0);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void should_send_cookie()
|
public void should_send_cookie()
|
||||||
{
|
{
|
||||||
@@ -753,6 +851,7 @@ namespace NzbDrone.Common.Test.Http
|
|||||||
public string Url { get; set; }
|
public string Url { get; set; }
|
||||||
public string Data { get; set; }
|
public string Data { get; set; }
|
||||||
public bool Gzipped { get; set; }
|
public bool Gzipped { get; set; }
|
||||||
|
public bool Brotli { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class HttpCookieResource
|
public class HttpCookieResource
|
||||||
|
@@ -210,5 +210,26 @@ namespace NzbDrone.Common.Extensions
|
|||||||
|
|
||||||
return result.TrimStart(' ', '.').TrimEnd(' ');
|
return result.TrimStart(' ', '.').TrimEnd(' ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static string EncodeRFC3986(this string value)
|
||||||
|
{
|
||||||
|
// From Twitterizer http://www.twitterizer.net/
|
||||||
|
if (string.IsNullOrEmpty(value))
|
||||||
|
{
|
||||||
|
return string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
var encoded = Uri.EscapeDataString(value);
|
||||||
|
|
||||||
|
return Regex
|
||||||
|
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
|
||||||
|
.Replace("(", "%28")
|
||||||
|
.Replace(")", "%29")
|
||||||
|
.Replace("$", "%24")
|
||||||
|
.Replace("!", "%21")
|
||||||
|
.Replace("*", "%2A")
|
||||||
|
.Replace("'", "%27")
|
||||||
|
.Replace("%7E", "~");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/NzbDrone.Common/Http/BasicNetworkCredential.cs
Normal file
12
src/NzbDrone.Common/Http/BasicNetworkCredential.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System.Net;
|
||||||
|
|
||||||
|
namespace NzbDrone.Common.Http
|
||||||
|
{
|
||||||
|
public class BasicNetworkCredential : NetworkCredential
|
||||||
|
{
|
||||||
|
public BasicNetworkCredential(string user, string pass)
|
||||||
|
: base(user, pass)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -10,6 +10,7 @@ namespace NzbDrone.Common.Http
|
|||||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
|
||||||
// NOTE: we are not checking non-ascii characters and we should
|
// NOTE: we are not checking non-ascii characters and we should
|
||||||
private static readonly Regex _CookieRegex = new Regex(@"([^\(\)<>@,;:\\""/\[\]\?=\{\}\s]+)=([^,;\\""\s]+)");
|
private static readonly Regex _CookieRegex = new Regex(@"([^\(\)<>@,;:\\""/\[\]\?=\{\}\s]+)=([^,;\\""\s]+)");
|
||||||
|
private static readonly string[] FilterProps = { "COMMENT", "COMMENTURL", "DISCORD", "DOMAIN", "EXPIRES", "MAX-AGE", "PATH", "PORT", "SECURE", "VERSION", "HTTPONLY", "SAMESITE" };
|
||||||
private static readonly char[] InvalidKeyChars = { '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t', '\n' };
|
private static readonly char[] InvalidKeyChars = { '(', ')', '<', '>', '@', ',', ';', ':', '\\', '"', '/', '[', ']', '?', '=', '{', '}', ' ', '\t', '\n' };
|
||||||
private static readonly char[] InvalidValueChars = { '"', ',', ';', '\\', ' ', '\t', '\n' };
|
private static readonly char[] InvalidValueChars = { '"', ',', ';', '\\', ' ', '\t', '\n' };
|
||||||
|
|
||||||
@@ -24,7 +25,7 @@ namespace NzbDrone.Common.Http
|
|||||||
var matches = _CookieRegex.Match(cookieHeader);
|
var matches = _CookieRegex.Match(cookieHeader);
|
||||||
while (matches.Success)
|
while (matches.Success)
|
||||||
{
|
{
|
||||||
if (matches.Groups.Count > 2)
|
if (matches.Groups.Count > 2 && !FilterProps.Contains(matches.Groups[1].Value.ToUpperInvariant()))
|
||||||
{
|
{
|
||||||
cookieDictionary[matches.Groups[1].Value] = matches.Groups[2].Value;
|
cookieDictionary[matches.Groups[1].Value] = matches.Groups[2].Value;
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,11 @@
|
|||||||
|
using System.Net.Http;
|
||||||
|
using System.Net.Security;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
|
namespace NzbDrone.Common.Http.Dispatchers
|
||||||
|
{
|
||||||
|
public interface ICertificateValidationService
|
||||||
|
{
|
||||||
|
bool ShouldByPassValidationError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors);
|
||||||
|
}
|
||||||
|
}
|
@@ -6,6 +6,5 @@ namespace NzbDrone.Common.Http.Dispatchers
|
|||||||
public interface IHttpDispatcher
|
public interface IHttpDispatcher
|
||||||
{
|
{
|
||||||
Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieContainer cookies);
|
Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieContainer cookies);
|
||||||
Task DownloadFileAsync(string url, string fileName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,13 +1,16 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Reflection;
|
using System.Net.Http;
|
||||||
|
using System.Net.Security;
|
||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NLog.Fluent;
|
using NzbDrone.Common.Cache;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Common.Http.Proxy;
|
using NzbDrone.Common.Http.Proxy;
|
||||||
|
|
||||||
@@ -15,221 +18,223 @@ namespace NzbDrone.Common.Http.Dispatchers
|
|||||||
{
|
{
|
||||||
public class ManagedHttpDispatcher : IHttpDispatcher
|
public class ManagedHttpDispatcher : IHttpDispatcher
|
||||||
{
|
{
|
||||||
|
private const string NO_PROXY_KEY = "no-proxy";
|
||||||
|
|
||||||
|
private const int connection_establish_timeout = 2000;
|
||||||
|
private static bool useIPv6 = Socket.OSSupportsIPv6;
|
||||||
|
private static bool hasResolvedIPv6Availability;
|
||||||
|
|
||||||
private readonly IHttpProxySettingsProvider _proxySettingsProvider;
|
private readonly IHttpProxySettingsProvider _proxySettingsProvider;
|
||||||
private readonly ICreateManagedWebProxy _createManagedWebProxy;
|
private readonly ICreateManagedWebProxy _createManagedWebProxy;
|
||||||
|
private readonly ICertificateValidationService _certificateValidationService;
|
||||||
private readonly IUserAgentBuilder _userAgentBuilder;
|
private readonly IUserAgentBuilder _userAgentBuilder;
|
||||||
private readonly IPlatformInfo _platformInfo;
|
private readonly ICached<System.Net.Http.HttpClient> _httpClientCache;
|
||||||
private readonly Logger _logger;
|
private readonly ICached<CredentialCache> _credentialCache;
|
||||||
|
|
||||||
public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider, ICreateManagedWebProxy createManagedWebProxy, IUserAgentBuilder userAgentBuilder, IPlatformInfo platformInfo, Logger logger)
|
public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider,
|
||||||
|
ICreateManagedWebProxy createManagedWebProxy,
|
||||||
|
ICertificateValidationService certificateValidationService,
|
||||||
|
IUserAgentBuilder userAgentBuilder,
|
||||||
|
ICacheManager cacheManager)
|
||||||
{
|
{
|
||||||
_proxySettingsProvider = proxySettingsProvider;
|
_proxySettingsProvider = proxySettingsProvider;
|
||||||
_createManagedWebProxy = createManagedWebProxy;
|
_createManagedWebProxy = createManagedWebProxy;
|
||||||
|
_certificateValidationService = certificateValidationService;
|
||||||
_userAgentBuilder = userAgentBuilder;
|
_userAgentBuilder = userAgentBuilder;
|
||||||
_platformInfo = platformInfo;
|
|
||||||
_logger = logger;
|
_httpClientCache = cacheManager.GetCache<System.Net.Http.HttpClient>(typeof(ManagedHttpDispatcher));
|
||||||
|
_credentialCache = cacheManager.GetCache<CredentialCache>(typeof(ManagedHttpDispatcher), "credentialcache");
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieContainer cookies)
|
public async Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieContainer cookies)
|
||||||
{
|
{
|
||||||
var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url);
|
var requestMessage = new HttpRequestMessage(request.Method, (Uri)request.Url);
|
||||||
|
requestMessage.Headers.UserAgent.ParseAdd(_userAgentBuilder.GetUserAgent(request.UseSimplifiedUserAgent));
|
||||||
|
requestMessage.Headers.ConnectionClose = !request.ConnectionKeepAlive;
|
||||||
|
|
||||||
// Deflate is not a standard and could break depending on implementation.
|
var cookieHeader = cookies.GetCookieHeader((Uri)request.Url);
|
||||||
// we should just stick with the more compatible Gzip
|
if (cookieHeader.IsNotNullOrWhiteSpace())
|
||||||
//http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net
|
|
||||||
webRequest.AutomaticDecompression = DecompressionMethods.GZip;
|
|
||||||
|
|
||||||
webRequest.Method = request.Method.ToString();
|
|
||||||
webRequest.UserAgent = _userAgentBuilder.GetUserAgent(request.UseSimplifiedUserAgent);
|
|
||||||
webRequest.KeepAlive = request.ConnectionKeepAlive;
|
|
||||||
webRequest.AllowAutoRedirect = false;
|
|
||||||
webRequest.CookieContainer = cookies;
|
|
||||||
|
|
||||||
if (request.RequestTimeout != TimeSpan.Zero)
|
|
||||||
{
|
{
|
||||||
webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds);
|
requestMessage.Headers.Add("Cookie", cookieHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
webRequest.Proxy = request.Proxy ?? GetProxy(request.Url);
|
using var cts = new CancellationTokenSource();
|
||||||
|
if (request.RequestTimeout != TimeSpan.Zero)
|
||||||
|
{
|
||||||
|
cts.CancelAfter(request.RequestTimeout);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// The default for System.Net.Http.HttpClient
|
||||||
|
cts.CancelAfter(TimeSpan.FromSeconds(100));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.Credentials != null)
|
||||||
|
{
|
||||||
|
if (request.Credentials is BasicNetworkCredential bc)
|
||||||
|
{
|
||||||
|
// Manually set header to avoid initial challenge response
|
||||||
|
var authInfo = bc.UserName + ":" + bc.Password;
|
||||||
|
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
|
||||||
|
requestMessage.Headers.Add("Authorization", "Basic " + authInfo);
|
||||||
|
}
|
||||||
|
else if (request.Credentials is NetworkCredential nc)
|
||||||
|
{
|
||||||
|
var creds = GetCredentialCache();
|
||||||
|
foreach (var authtype in new[] { "Basic", "Digest" })
|
||||||
|
{
|
||||||
|
creds.Remove((Uri)request.Url, authtype);
|
||||||
|
creds.Add((Uri)request.Url, authtype, nc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.ContentData != null)
|
||||||
|
{
|
||||||
|
requestMessage.Content = new ByteArrayContent(request.ContentData);
|
||||||
|
}
|
||||||
|
|
||||||
if (request.Headers != null)
|
if (request.Headers != null)
|
||||||
{
|
{
|
||||||
AddRequestHeaders(webRequest, request.Headers);
|
AddRequestHeaders(requestMessage, request.Headers);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpWebResponse httpWebResponse;
|
var httpClient = GetClient(request.Url);
|
||||||
|
|
||||||
var sw = new Stopwatch();
|
var sw = new Stopwatch();
|
||||||
|
|
||||||
sw.Start();
|
sw.Start();
|
||||||
|
|
||||||
try
|
using var responseMessage = await httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cts.Token);
|
||||||
{
|
{
|
||||||
if (request.ContentData != null)
|
byte[] data = null;
|
||||||
{
|
|
||||||
webRequest.ContentLength = request.ContentData.Length;
|
|
||||||
using (var writeStream = webRequest.GetRequestStream())
|
|
||||||
{
|
|
||||||
writeStream.Write(request.ContentData, 0, request.ContentData.Length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
httpWebResponse = (HttpWebResponse)await webRequest.GetResponseAsync();
|
try
|
||||||
}
|
|
||||||
catch (WebException e)
|
|
||||||
{
|
|
||||||
httpWebResponse = (HttpWebResponse)e.Response;
|
|
||||||
|
|
||||||
if (httpWebResponse == null)
|
|
||||||
{
|
{
|
||||||
// The default messages for WebException on mono are pretty horrible.
|
if (request.ResponseStream != null && responseMessage.StatusCode == HttpStatusCode.OK)
|
||||||
if (e.Status == WebExceptionStatus.NameResolutionFailure)
|
|
||||||
{
|
{
|
||||||
throw new WebException($"DNS Name Resolution Failure: '{webRequest.RequestUri.Host}'", e.Status);
|
responseMessage.Content.CopyTo(request.ResponseStream, null, cts.Token);
|
||||||
}
|
|
||||||
else if (e.ToString().Contains("TLS Support not"))
|
|
||||||
{
|
|
||||||
throw new TlsFailureException(webRequest, e);
|
|
||||||
}
|
|
||||||
else if (e.ToString().Contains("The authentication or decryption has failed."))
|
|
||||||
{
|
|
||||||
throw new TlsFailureException(webRequest, e);
|
|
||||||
}
|
|
||||||
else if (OsInfo.IsNotWindows)
|
|
||||||
{
|
|
||||||
throw new WebException($"{e.Message}: '{webRequest.RequestUri}'", e, e.Status, e.Response);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw;
|
data = responseMessage.Content.ReadAsByteArrayAsync(cts.Token).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
|
|
||||||
byte[] data = null;
|
|
||||||
|
|
||||||
using (var responseStream = httpWebResponse.GetResponseStream())
|
|
||||||
{
|
|
||||||
if (responseStream != null && responseStream != Stream.Null)
|
|
||||||
{
|
{
|
||||||
try
|
throw new WebException("Failed to read complete http response", ex, WebExceptionStatus.ReceiveFailure, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
var headers = responseMessage.Headers.ToNameValueCollection();
|
||||||
|
|
||||||
|
headers.Add(responseMessage.Content.Headers.ToNameValueCollection());
|
||||||
|
|
||||||
|
CookieContainer responseCookies = new CookieContainer();
|
||||||
|
|
||||||
|
if (responseMessage.Headers.TryGetValues("Set-Cookie", out var cookieHeaders))
|
||||||
|
{
|
||||||
|
foreach (var responseCookieHeader in cookieHeaders)
|
||||||
{
|
{
|
||||||
data = await responseStream.ToBytes();
|
try
|
||||||
}
|
{
|
||||||
catch (Exception ex)
|
cookies.SetCookies(responseMessage.RequestMessage.RequestUri, responseCookieHeader);
|
||||||
{
|
}
|
||||||
throw new WebException("Failed to read complete http response", ex, WebExceptionStatus.ReceiveFailure, httpWebResponse);
|
catch
|
||||||
|
{
|
||||||
|
// Ignore invalid cookies
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
sw.Stop();
|
var cookieCollection = cookies.GetCookies(responseMessage.RequestMessage.RequestUri);
|
||||||
|
|
||||||
return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), httpWebResponse.Cookies, data, sw.ElapsedMilliseconds, httpWebResponse.StatusCode);
|
sw.Stop();
|
||||||
}
|
|
||||||
|
|
||||||
public async Task DownloadFileAsync(string url, string fileName)
|
return new HttpResponse(request, new HttpHeader(headers), cookieCollection, data, sw.ElapsedMilliseconds, responseMessage.StatusCode);
|
||||||
{
|
|
||||||
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);
|
|
||||||
await webClient.DownloadFileTaskAsync(url, 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);
|
|
||||||
|
|
||||||
if (File.Exists(fileName))
|
|
||||||
{
|
|
||||||
File.Delete(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_logger.Warn(e, "Failed to get response from: " + url);
|
|
||||||
|
|
||||||
if (File.Exists(fileName))
|
|
||||||
{
|
|
||||||
File.Delete(fileName);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual IWebProxy GetProxy(HttpUri uri)
|
protected virtual System.Net.Http.HttpClient GetClient(HttpUri uri)
|
||||||
{
|
{
|
||||||
IWebProxy proxy = null;
|
|
||||||
|
|
||||||
var proxySettings = _proxySettingsProvider.GetProxySettings(uri);
|
var proxySettings = _proxySettingsProvider.GetProxySettings(uri);
|
||||||
|
|
||||||
|
var key = proxySettings?.Key ?? NO_PROXY_KEY;
|
||||||
|
|
||||||
|
return _httpClientCache.Get(key, () => CreateHttpClient(proxySettings));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual System.Net.Http.HttpClient CreateHttpClient(HttpProxySettings proxySettings)
|
||||||
|
{
|
||||||
|
var handler = new SocketsHttpHandler()
|
||||||
|
{
|
||||||
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Brotli,
|
||||||
|
UseCookies = false, // sic - we don't want to use a shared cookie container
|
||||||
|
AllowAutoRedirect = false,
|
||||||
|
Credentials = GetCredentialCache(),
|
||||||
|
PreAuthenticate = true,
|
||||||
|
MaxConnectionsPerServer = 12,
|
||||||
|
ConnectCallback = onConnect,
|
||||||
|
SslOptions = new SslClientAuthenticationOptions
|
||||||
|
{
|
||||||
|
RemoteCertificateValidationCallback = _certificateValidationService.ShouldByPassValidationError
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (proxySettings != null)
|
if (proxySettings != null)
|
||||||
{
|
{
|
||||||
proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
|
handler.Proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
return proxy;
|
var client = new System.Net.Http.HttpClient(handler)
|
||||||
|
{
|
||||||
|
Timeout = Timeout.InfiniteTimeSpan
|
||||||
|
};
|
||||||
|
|
||||||
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual void AddRequestHeaders(HttpWebRequest webRequest, HttpHeader headers)
|
protected virtual void AddRequestHeaders(HttpRequestMessage webRequest, HttpHeader headers)
|
||||||
{
|
{
|
||||||
foreach (var header in headers)
|
foreach (var header in headers)
|
||||||
{
|
{
|
||||||
switch (header.Key)
|
switch (header.Key)
|
||||||
{
|
{
|
||||||
case "Accept":
|
case "Accept":
|
||||||
webRequest.Accept = header.Value;
|
webRequest.Headers.Accept.ParseAdd(header.Value);
|
||||||
break;
|
break;
|
||||||
case "Connection":
|
case "Connection":
|
||||||
webRequest.Connection = header.Value;
|
webRequest.Headers.Connection.Clear();
|
||||||
|
webRequest.Headers.Connection.Add(header.Value);
|
||||||
break;
|
break;
|
||||||
case "Content-Length":
|
case "Content-Length":
|
||||||
webRequest.ContentLength = Convert.ToInt64(header.Value);
|
AddContentHeader(webRequest, "Content-Length", header.Value);
|
||||||
break;
|
break;
|
||||||
case "Content-Type":
|
case "Content-Type":
|
||||||
webRequest.ContentType = header.Value;
|
AddContentHeader(webRequest, "Content-Type", header.Value);
|
||||||
break;
|
break;
|
||||||
case "Date":
|
case "Date":
|
||||||
webRequest.Date = HttpHeader.ParseDateTime(header.Value);
|
webRequest.Headers.Remove("Date");
|
||||||
|
webRequest.Headers.Date = HttpHeader.ParseDateTime(header.Value);
|
||||||
break;
|
break;
|
||||||
case "Expect":
|
case "Expect":
|
||||||
webRequest.Expect = header.Value;
|
webRequest.Headers.Expect.ParseAdd(header.Value);
|
||||||
break;
|
break;
|
||||||
case "Host":
|
case "Host":
|
||||||
webRequest.Host = header.Value;
|
webRequest.Headers.Host = header.Value;
|
||||||
break;
|
break;
|
||||||
case "If-Modified-Since":
|
case "If-Modified-Since":
|
||||||
webRequest.IfModifiedSince = HttpHeader.ParseDateTime(header.Value);
|
webRequest.Headers.IfModifiedSince = HttpHeader.ParseDateTime(header.Value);
|
||||||
break;
|
break;
|
||||||
case "Range":
|
case "Range":
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
case "Referer":
|
case "Referer":
|
||||||
webRequest.Referer = header.Value;
|
webRequest.Headers.Add("Referer", header.Value);
|
||||||
break;
|
break;
|
||||||
case "Transfer-Encoding":
|
case "Transfer-Encoding":
|
||||||
webRequest.TransferEncoding = header.Value;
|
webRequest.Headers.TransferEncoding.ParseAdd(header.Value);
|
||||||
break;
|
break;
|
||||||
case "User-Agent":
|
case "User-Agent":
|
||||||
webRequest.UserAgent = header.Value;
|
webRequest.Headers.UserAgent.ParseAdd(header.Value);
|
||||||
break;
|
break;
|
||||||
case "Proxy-Connection":
|
case "Proxy-Connection":
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
@@ -239,5 +244,84 @@ namespace NzbDrone.Common.Http.Dispatchers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void AddContentHeader(HttpRequestMessage request, string header, string value)
|
||||||
|
{
|
||||||
|
var headers = request.Content?.Headers;
|
||||||
|
if (headers == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
headers.Remove(header);
|
||||||
|
headers.Add(header, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CredentialCache GetCredentialCache()
|
||||||
|
{
|
||||||
|
return _credentialCache.Get("credentialCache", () => new CredentialCache());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async ValueTask<Stream> onConnect(SocketsHttpConnectionContext context, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// Until .NET supports an implementation of Happy Eyeballs (https://tools.ietf.org/html/rfc8305#section-2), let's make IPv4 fallback work in a simple way.
|
||||||
|
// This issue is being tracked at https://github.com/dotnet/runtime/issues/26177 and expected to be fixed in .NET 6.
|
||||||
|
if (useIPv6)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var localToken = cancellationToken;
|
||||||
|
|
||||||
|
if (!hasResolvedIPv6Availability)
|
||||||
|
{
|
||||||
|
// to make things move fast, use a very low timeout for the initial ipv6 attempt.
|
||||||
|
var quickFailCts = new CancellationTokenSource(connection_establish_timeout);
|
||||||
|
var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, quickFailCts.Token);
|
||||||
|
|
||||||
|
localToken = linkedTokenSource.Token;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await attemptConnection(AddressFamily.InterNetworkV6, context, localToken);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// very naively fallback to ipv4 permanently for this execution based on the response of the first connection attempt.
|
||||||
|
// note that this may cause users to eventually get switched to ipv4 (on a random failure when they are switching networks, for instance)
|
||||||
|
// but in the interest of keeping this implementation simple, this is acceptable.
|
||||||
|
useIPv6 = false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
hasResolvedIPv6Availability = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fallback to IPv4.
|
||||||
|
return await attemptConnection(AddressFamily.InterNetwork, context, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async ValueTask<Stream> attemptConnection(AddressFamily addressFamily, SocketsHttpConnectionContext context, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// The following socket constructor will create a dual-mode socket on systems where IPV6 is available.
|
||||||
|
var socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp)
|
||||||
|
{
|
||||||
|
// Turn off Nagle's algorithm since it degrades performance in most HttpClient scenarios.
|
||||||
|
NoDelay = true
|
||||||
|
};
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await socket.ConnectAsync(context.DnsEndPoint, cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
|
// The stream should take the ownership of the underlying socket,
|
||||||
|
// closing it when it's disposed.
|
||||||
|
return new NetworkStream(socket, ownsSocket: true);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
socket.Dispose();
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,15 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Net;
|
|
||||||
|
|
||||||
namespace NzbDrone.Common.Http
|
|
||||||
{
|
|
||||||
public class GZipWebClient : WebClient
|
|
||||||
{
|
|
||||||
protected override WebRequest GetWebRequest(Uri address)
|
|
||||||
{
|
|
||||||
var request = (HttpWebRequest)base.GetWebRequest(address);
|
|
||||||
request.AutomaticDecompression = DecompressionMethods.GZip;
|
|
||||||
return request;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@@ -1,6 +1,7 @@
|
|||||||
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 System.Net.Http;
|
using System.Net.Http;
|
||||||
@@ -86,13 +87,21 @@ namespace NzbDrone.Common.Http
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 302 or 303 should default to GET on redirect even if POST on original
|
// 302 or 303 should default to GET on redirect even if POST on original
|
||||||
if (response.StatusCode == HttpStatusCode.Redirect || response.StatusCode == HttpStatusCode.RedirectMethod)
|
if (RequestRequiresForceGet(response.StatusCode, response.Request.Method))
|
||||||
{
|
{
|
||||||
request.Method = HttpMethod.Get;
|
request.Method = HttpMethod.Get;
|
||||||
request.ContentData = null;
|
request.ContentData = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
response = await ExecuteRequestAsync(request, cookieContainer);
|
// Save to add to final response
|
||||||
|
var responseCookies = response.Cookies;
|
||||||
|
|
||||||
|
// Update cookiecontainer for next request with any cookies recieved on last request
|
||||||
|
var responseContainer = HandleRedirectCookies(request, response);
|
||||||
|
|
||||||
|
response = await ExecuteRequestAsync(request, responseContainer);
|
||||||
|
|
||||||
|
response.Cookies.Add(responseCookies);
|
||||||
}
|
}
|
||||||
while (response.HasHttpRedirect);
|
while (response.HasHttpRedirect);
|
||||||
}
|
}
|
||||||
@@ -102,9 +111,12 @@ namespace NzbDrone.Common.Http
|
|||||||
_logger.Error("Server requested a redirect to [{0}] while in developer mode. Update the request URL to avoid this redirect.", response.Headers["Location"]);
|
_logger.Error("Server requested a redirect to [{0}] while in developer mode. Update the request URL to avoid this redirect.", response.Headers["Location"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!request.SuppressHttpError && response.HasHttpError)
|
if (!request.SuppressHttpError && response.HasHttpError && (request.SuppressHttpErrorStatusCodes == null || !request.SuppressHttpErrorStatusCodes.Contains(response.StatusCode)))
|
||||||
{
|
{
|
||||||
_logger.Warn("HTTP Error - {0}", response);
|
if (request.LogHttpError)
|
||||||
|
{
|
||||||
|
_logger.Warn("HTTP Error - {0}", response);
|
||||||
|
}
|
||||||
|
|
||||||
if ((int)response.StatusCode == 429)
|
if ((int)response.StatusCode == 429)
|
||||||
{
|
{
|
||||||
@@ -124,6 +136,21 @@ namespace NzbDrone.Common.Http
|
|||||||
return ExecuteAsync(request).GetAwaiter().GetResult();
|
return ExecuteAsync(request).GetAwaiter().GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static bool RequestRequiresForceGet(HttpStatusCode statusCode, HttpMethod requestMethod)
|
||||||
|
{
|
||||||
|
switch (statusCode)
|
||||||
|
{
|
||||||
|
case HttpStatusCode.Moved:
|
||||||
|
case HttpStatusCode.Found:
|
||||||
|
case HttpStatusCode.MultipleChoices:
|
||||||
|
return requestMethod == HttpMethod.Post;
|
||||||
|
case HttpStatusCode.SeeOther:
|
||||||
|
return requestMethod != HttpMethod.Get && requestMethod != HttpMethod.Head;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private async Task<HttpResponse> ExecuteRequestAsync(HttpRequest request, CookieContainer cookieContainer)
|
private async Task<HttpResponse> ExecuteRequestAsync(HttpRequest request, CookieContainer cookieContainer)
|
||||||
{
|
{
|
||||||
foreach (var interceptor in _requestInterceptors)
|
foreach (var interceptor in _requestInterceptors)
|
||||||
@@ -140,8 +167,6 @@ namespace NzbDrone.Common.Http
|
|||||||
|
|
||||||
var stopWatch = Stopwatch.StartNew();
|
var stopWatch = Stopwatch.StartNew();
|
||||||
|
|
||||||
PrepareRequestCookies(request, cookieContainer);
|
|
||||||
|
|
||||||
var response = await _httpDispatcher.GetResponseAsync(request, cookieContainer);
|
var response = await _httpDispatcher.GetResponseAsync(request, cookieContainer);
|
||||||
|
|
||||||
HandleResponseCookies(response, cookieContainer);
|
HandleResponseCookies(response, cookieContainer);
|
||||||
@@ -208,52 +233,125 @@ namespace NzbDrone.Common.Http
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void PrepareRequestCookies(HttpRequest request, CookieContainer cookieContainer)
|
private CookieContainer HandleRedirectCookies(HttpRequest request, HttpResponse response)
|
||||||
{
|
{
|
||||||
// Don't collect persistnet cookies for intermediate/redirected urls.
|
var sourceContainer = new CookieContainer();
|
||||||
/*lock (_cookieContainerCache)
|
var responseCookies = response.GetCookies();
|
||||||
|
if (responseCookies.Count != 0)
|
||||||
{
|
{
|
||||||
var presistentContainer = _cookieContainerCache.Get("container", () => new CookieContainer());
|
foreach (var pair in responseCookies)
|
||||||
var persistentCookies = presistentContainer.GetCookies((Uri)request.Url);
|
{
|
||||||
var existingCookies = cookieContainer.GetCookies((Uri)request.Url);
|
Cookie cookie;
|
||||||
|
if (pair.Value == null)
|
||||||
|
{
|
||||||
|
cookie = new Cookie(pair.Key, "", "/")
|
||||||
|
{
|
||||||
|
Expires = DateTime.Now.AddDays(-1)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cookie = new Cookie(pair.Key, pair.Value, "/")
|
||||||
|
{
|
||||||
|
// Use Now rather than UtcNow to work around Mono cookie expiry bug.
|
||||||
|
// See https://gist.github.com/ta264/7822b1424f72e5b4c961
|
||||||
|
Expires = DateTime.Now.AddHours(1)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
cookieContainer.Add(persistentCookies);
|
sourceContainer.Add((Uri)request.Url, cookie);
|
||||||
cookieContainer.Add(existingCookies);
|
}
|
||||||
}*/
|
}
|
||||||
|
|
||||||
|
return sourceContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void HandleResponseCookies(HttpResponse response, CookieContainer cookieContainer)
|
private void HandleResponseCookies(HttpResponse response, CookieContainer container)
|
||||||
{
|
{
|
||||||
|
foreach (Cookie cookie in container.GetAllCookies())
|
||||||
|
{
|
||||||
|
cookie.Expired = true;
|
||||||
|
}
|
||||||
|
|
||||||
var cookieHeaders = response.GetCookieHeaders();
|
var cookieHeaders = response.GetCookieHeaders();
|
||||||
if (cookieHeaders.Empty())
|
if (cookieHeaders.Empty())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
AddCookiesToContainer(response.Request.Url, cookieHeaders, container);
|
||||||
|
|
||||||
if (response.Request.StoreResponseCookie)
|
if (response.Request.StoreResponseCookie)
|
||||||
{
|
{
|
||||||
lock (_cookieContainerCache)
|
lock (_cookieContainerCache)
|
||||||
{
|
{
|
||||||
var persistentCookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer());
|
var persistentCookieContainer = _cookieContainerCache.Get("container", () => new CookieContainer());
|
||||||
|
|
||||||
foreach (var cookieHeader in cookieHeaders)
|
AddCookiesToContainer(response.Request.Url, cookieHeaders, persistentCookieContainer);
|
||||||
{
|
}
|
||||||
try
|
}
|
||||||
{
|
}
|
||||||
persistentCookieContainer.SetCookies((Uri)response.Request.Url, cookieHeader);
|
|
||||||
}
|
private void AddCookiesToContainer(HttpUri url, string[] cookieHeaders, CookieContainer container)
|
||||||
catch (Exception ex)
|
{
|
||||||
{
|
foreach (var cookieHeader in cookieHeaders)
|
||||||
_logger.Debug(ex, "Invalid cookie in {0}", response.Request.Url);
|
{
|
||||||
}
|
try
|
||||||
}
|
{
|
||||||
|
container.SetCookies((Uri)url, cookieHeader);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.Debug(ex, "Invalid cookie in {0}", url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DownloadFileAsync(string url, string fileName)
|
public async Task DownloadFileAsync(string url, string fileName)
|
||||||
{
|
{
|
||||||
await _httpDispatcher.DownloadFileAsync(url, fileName);
|
var fileNamePart = fileName + ".part";
|
||||||
|
|
||||||
|
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);
|
||||||
|
request.AllowAutoRedirect = true;
|
||||||
|
request.ResponseStream = fileStream;
|
||||||
|
var response = await GetAsync(request);
|
||||||
|
|
||||||
|
if (response.Headers.ContentType != null && response.Headers.ContentType.Contains("text/html"))
|
||||||
|
{
|
||||||
|
throw new HttpException(request, response, "Site responded with html content.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 void DownloadFile(string url, string fileName)
|
public void DownloadFile(string url, string fileName)
|
||||||
|
@@ -4,11 +4,27 @@ using System.Collections.Generic;
|
|||||||
using System.Collections.Specialized;
|
using System.Collections.Specialized;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Common.Http
|
namespace NzbDrone.Common.Http
|
||||||
{
|
{
|
||||||
|
public static class WebHeaderCollectionExtensions
|
||||||
|
{
|
||||||
|
public static NameValueCollection ToNameValueCollection(this HttpHeaders headers)
|
||||||
|
{
|
||||||
|
var result = new NameValueCollection();
|
||||||
|
foreach (var header in headers)
|
||||||
|
{
|
||||||
|
result.Add(header.Key, header.Value.ConcatToString(";"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public class HttpHeader : NameValueCollection, IEnumerable<KeyValuePair<string, string>>, IEnumerable
|
public class HttpHeader : NameValueCollection, IEnumerable<KeyValuePair<string, string>>, IEnumerable
|
||||||
{
|
{
|
||||||
public HttpHeader(NameValueCollection headers)
|
public HttpHeader(NameValueCollection headers)
|
||||||
@@ -16,6 +32,11 @@ namespace NzbDrone.Common.Http
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HttpHeader(HttpHeaders headers)
|
||||||
|
: base(headers.ToNameValueCollection())
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
public HttpHeader()
|
public HttpHeader()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -12,11 +13,13 @@ namespace NzbDrone.Common.Http
|
|||||||
{
|
{
|
||||||
public HttpRequest(string url, HttpAccept httpAccept = null)
|
public HttpRequest(string url, HttpAccept httpAccept = null)
|
||||||
{
|
{
|
||||||
|
Method = HttpMethod.Get;
|
||||||
Url = new HttpUri(url);
|
Url = new HttpUri(url);
|
||||||
Headers = new HttpHeader();
|
Headers = new HttpHeader();
|
||||||
Method = HttpMethod.Get;
|
Method = HttpMethod.Get;
|
||||||
ConnectionKeepAlive = true;
|
ConnectionKeepAlive = true;
|
||||||
AllowAutoRedirect = true;
|
AllowAutoRedirect = true;
|
||||||
|
LogHttpError = true;
|
||||||
Cookies = new Dictionary<string, string>();
|
Cookies = new Dictionary<string, string>();
|
||||||
|
|
||||||
if (!RuntimeInfo.IsProduction)
|
if (!RuntimeInfo.IsProduction)
|
||||||
@@ -37,16 +40,20 @@ namespace NzbDrone.Common.Http
|
|||||||
public IWebProxy Proxy { get; set; }
|
public IWebProxy Proxy { get; set; }
|
||||||
public byte[] ContentData { get; set; }
|
public byte[] ContentData { get; set; }
|
||||||
public string ContentSummary { get; set; }
|
public string ContentSummary { get; set; }
|
||||||
|
public ICredentials Credentials { get; set; }
|
||||||
public bool SuppressHttpError { get; set; }
|
public bool SuppressHttpError { get; set; }
|
||||||
|
public IEnumerable<HttpStatusCode> SuppressHttpErrorStatusCodes { get; set; }
|
||||||
public bool UseSimplifiedUserAgent { get; set; }
|
public bool UseSimplifiedUserAgent { get; set; }
|
||||||
public bool AllowAutoRedirect { get; set; }
|
public bool AllowAutoRedirect { get; set; }
|
||||||
public bool ConnectionKeepAlive { get; set; }
|
public bool ConnectionKeepAlive { get; set; }
|
||||||
public bool LogResponseContent { get; set; }
|
public bool LogResponseContent { get; set; }
|
||||||
|
public bool LogHttpError { get; set; }
|
||||||
public Dictionary<string, string> Cookies { get; private set; }
|
public Dictionary<string, string> Cookies { get; private set; }
|
||||||
public bool StoreRequestCookie { get; set; }
|
public bool StoreRequestCookie { get; set; }
|
||||||
public bool StoreResponseCookie { get; set; }
|
public bool StoreResponseCookie { get; set; }
|
||||||
public TimeSpan RequestTimeout { get; set; }
|
public TimeSpan RequestTimeout { get; set; }
|
||||||
public TimeSpan RateLimit { get; set; }
|
public TimeSpan RateLimit { get; set; }
|
||||||
|
public Stream ResponseStream { get; set; }
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
@@ -103,12 +110,5 @@ namespace NzbDrone.Common.Http
|
|||||||
return encoding.GetString(ContentData);
|
return encoding.GetString(ContentData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddBasicAuthentication(string username, string password)
|
|
||||||
{
|
|
||||||
var authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes($"{username}:{password}"));
|
|
||||||
|
|
||||||
Headers.Set("Authorization", "Basic " + authInfo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -21,12 +21,13 @@ namespace NzbDrone.Common.Http
|
|||||||
public Dictionary<string, string> Segments { get; private set; }
|
public Dictionary<string, string> Segments { get; private set; }
|
||||||
public HttpHeader Headers { get; private set; }
|
public HttpHeader Headers { get; private set; }
|
||||||
public bool SuppressHttpError { get; set; }
|
public bool SuppressHttpError { get; set; }
|
||||||
|
public bool LogHttpError { get; set; }
|
||||||
public bool UseSimplifiedUserAgent { get; set; }
|
public bool UseSimplifiedUserAgent { get; set; }
|
||||||
public bool AllowAutoRedirect { get; set; }
|
public bool AllowAutoRedirect { get; set; }
|
||||||
public bool ConnectionKeepAlive { get; set; }
|
public bool ConnectionKeepAlive { get; set; }
|
||||||
public TimeSpan RateLimit { get; set; }
|
public TimeSpan RateLimit { get; set; }
|
||||||
public bool LogResponseContent { get; set; }
|
public bool LogResponseContent { get; set; }
|
||||||
public NetworkCredential NetworkCredential { get; set; }
|
public ICredentials NetworkCredential { get; set; }
|
||||||
public Dictionary<string, string> Cookies { get; private set; }
|
public Dictionary<string, string> Cookies { get; private set; }
|
||||||
public bool StoreRequestCookie { get; set; }
|
public bool StoreRequestCookie { get; set; }
|
||||||
public bool StoreResponseCookie { get; set; }
|
public bool StoreResponseCookie { get; set; }
|
||||||
@@ -46,6 +47,7 @@ namespace NzbDrone.Common.Http
|
|||||||
Headers = new HttpHeader();
|
Headers = new HttpHeader();
|
||||||
Cookies = new Dictionary<string, string>();
|
Cookies = new Dictionary<string, string>();
|
||||||
FormData = new List<HttpFormData>();
|
FormData = new List<HttpFormData>();
|
||||||
|
LogHttpError = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpRequestBuilder(bool useHttps, string host, int port, string urlBase = null)
|
public HttpRequestBuilder(bool useHttps, string host, int port, string urlBase = null)
|
||||||
@@ -106,6 +108,7 @@ namespace NzbDrone.Common.Http
|
|||||||
request.Method = Method;
|
request.Method = Method;
|
||||||
request.Encoding = Encoding;
|
request.Encoding = Encoding;
|
||||||
request.SuppressHttpError = SuppressHttpError;
|
request.SuppressHttpError = SuppressHttpError;
|
||||||
|
request.LogHttpError = LogHttpError;
|
||||||
request.UseSimplifiedUserAgent = UseSimplifiedUserAgent;
|
request.UseSimplifiedUserAgent = UseSimplifiedUserAgent;
|
||||||
request.AllowAutoRedirect = AllowAutoRedirect;
|
request.AllowAutoRedirect = AllowAutoRedirect;
|
||||||
request.StoreRequestCookie = StoreRequestCookie;
|
request.StoreRequestCookie = StoreRequestCookie;
|
||||||
@@ -113,13 +116,7 @@ namespace NzbDrone.Common.Http
|
|||||||
request.ConnectionKeepAlive = ConnectionKeepAlive;
|
request.ConnectionKeepAlive = ConnectionKeepAlive;
|
||||||
request.RateLimit = RateLimit;
|
request.RateLimit = RateLimit;
|
||||||
request.LogResponseContent = LogResponseContent;
|
request.LogResponseContent = LogResponseContent;
|
||||||
|
request.Credentials = NetworkCredential;
|
||||||
if (NetworkCredential != null)
|
|
||||||
{
|
|
||||||
var authInfo = NetworkCredential.UserName + ":" + NetworkCredential.Password;
|
|
||||||
authInfo = Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(authInfo));
|
|
||||||
request.Headers.Set("Authorization", "Basic " + authInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var header in Headers)
|
foreach (var header in Headers)
|
||||||
{
|
{
|
||||||
|
@@ -64,10 +64,11 @@ namespace NzbDrone.Common.Http
|
|||||||
public bool HasHttpError => (int)StatusCode >= 400;
|
public bool HasHttpError => (int)StatusCode >= 400;
|
||||||
|
|
||||||
public bool HasHttpRedirect => StatusCode == HttpStatusCode.Moved ||
|
public bool HasHttpRedirect => StatusCode == HttpStatusCode.Moved ||
|
||||||
StatusCode == HttpStatusCode.MovedPermanently ||
|
|
||||||
StatusCode == HttpStatusCode.RedirectMethod ||
|
|
||||||
StatusCode == HttpStatusCode.TemporaryRedirect ||
|
|
||||||
StatusCode == HttpStatusCode.Found ||
|
StatusCode == HttpStatusCode.Found ||
|
||||||
|
StatusCode == HttpStatusCode.SeeOther ||
|
||||||
|
StatusCode == HttpStatusCode.TemporaryRedirect ||
|
||||||
|
StatusCode == HttpStatusCode.MultipleChoices ||
|
||||||
|
StatusCode == HttpStatusCode.PermanentRedirect ||
|
||||||
Headers.ContainsKey("Refresh");
|
Headers.ContainsKey("Refresh");
|
||||||
|
|
||||||
public string RedirectUrl
|
public string RedirectUrl
|
||||||
@@ -117,7 +118,7 @@ namespace NzbDrone.Common.Http
|
|||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
var result = string.Format("Res: [{0}] {1}: {2}.{3}", Request.Method, Request.Url, (int)StatusCode, StatusCode);
|
var result = string.Format("Res: [{0}] {1}: {2}.{3} ({4} bytes)", Request.Method, Request.Url, (int)StatusCode, StatusCode, ResponseData?.Length ?? 0);
|
||||||
|
|
||||||
if (HasHttpError && Headers.ContentType.IsNotNullOrWhiteSpace() && !Headers.ContentType.Equals("text/html", StringComparison.InvariantCultureIgnoreCase))
|
if (HasHttpError && Headers.ContentType.IsNotNullOrWhiteSpace() && !Headers.ContentType.Equals("text/html", StringComparison.InvariantCultureIgnoreCase))
|
||||||
{
|
{
|
||||||
|
103
src/NzbDrone.Common/Http/XmlRpcRequestBuilder.cs
Normal file
103
src/NzbDrone.Common/Http/XmlRpcRequestBuilder.cs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using NLog;
|
||||||
|
using NzbDrone.Common.Instrumentation;
|
||||||
|
|
||||||
|
namespace NzbDrone.Common.Http
|
||||||
|
{
|
||||||
|
public class XmlRpcRequestBuilder : HttpRequestBuilder
|
||||||
|
{
|
||||||
|
public static string XmlRpcContentType = "text/xml";
|
||||||
|
|
||||||
|
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(XmlRpcRequestBuilder));
|
||||||
|
|
||||||
|
public string XmlMethod { get; private set; }
|
||||||
|
public List<object> XmlParameters { get; private set; }
|
||||||
|
|
||||||
|
public XmlRpcRequestBuilder(string baseUrl)
|
||||||
|
: base(baseUrl)
|
||||||
|
{
|
||||||
|
Method = HttpMethod.Post;
|
||||||
|
XmlParameters = new List<object>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public XmlRpcRequestBuilder(bool useHttps, string host, int port, string urlBase = null)
|
||||||
|
: this(BuildBaseUrl(useHttps, host, port, urlBase))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public override HttpRequestBuilder Clone()
|
||||||
|
{
|
||||||
|
var clone = base.Clone() as XmlRpcRequestBuilder;
|
||||||
|
clone.XmlParameters = new List<object>(XmlParameters);
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
public XmlRpcRequestBuilder Call(string method, params object[] parameters)
|
||||||
|
{
|
||||||
|
var clone = Clone() as XmlRpcRequestBuilder;
|
||||||
|
clone.XmlMethod = method;
|
||||||
|
clone.XmlParameters = parameters.ToList();
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Apply(HttpRequest request)
|
||||||
|
{
|
||||||
|
base.Apply(request);
|
||||||
|
|
||||||
|
request.Headers.ContentType = XmlRpcContentType;
|
||||||
|
|
||||||
|
var methodCallElements = new List<XElement> { new XElement("methodName", XmlMethod) };
|
||||||
|
|
||||||
|
if (XmlParameters.Any())
|
||||||
|
{
|
||||||
|
var argElements = XmlParameters.Select(x => new XElement("param", ConvertParameter(x))).ToList();
|
||||||
|
var paramsElement = new XElement("params", argElements);
|
||||||
|
methodCallElements.Add(paramsElement);
|
||||||
|
}
|
||||||
|
|
||||||
|
var message = new XDocument(
|
||||||
|
new XDeclaration("1.0", "utf-8", "yes"),
|
||||||
|
new XElement("methodCall", methodCallElements));
|
||||||
|
|
||||||
|
var body = message.ToString();
|
||||||
|
|
||||||
|
Logger.Debug($"Executing remote method: {XmlMethod}");
|
||||||
|
|
||||||
|
Logger.Trace($"methodCall {XmlMethod} body:\n{body}");
|
||||||
|
|
||||||
|
request.SetContent(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static XElement ConvertParameter(object value)
|
||||||
|
{
|
||||||
|
XElement data;
|
||||||
|
|
||||||
|
if (value is string s)
|
||||||
|
{
|
||||||
|
data = new XElement("string", s);
|
||||||
|
}
|
||||||
|
else if (value is List<string> l)
|
||||||
|
{
|
||||||
|
data = new XElement("array", new XElement("data", l.Select(x => new XElement("value", new XElement("string", x)))));
|
||||||
|
}
|
||||||
|
else if (value is int i)
|
||||||
|
{
|
||||||
|
data = new XElement("int", i);
|
||||||
|
}
|
||||||
|
else if (value is byte[] bytes)
|
||||||
|
{
|
||||||
|
data = new XElement("base64", Convert.ToBase64String(bytes));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Unhandled argument type {value.GetType().Name}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new XElement("value", data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -11,6 +11,7 @@ using NzbDrone.Common.TPL;
|
|||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
using NzbDrone.Core.Http;
|
using NzbDrone.Core.Http;
|
||||||
using NzbDrone.Core.Parser;
|
using NzbDrone.Core.Parser;
|
||||||
|
using NzbDrone.Core.Security;
|
||||||
using NzbDrone.Test.Common;
|
using NzbDrone.Test.Common;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Test.Framework
|
namespace NzbDrone.Core.Test.Framework
|
||||||
@@ -25,7 +26,8 @@ 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<ICertificateValidationService>(new X509CertificateValidationService(Mocker.Resolve<ConfigService>(), TestLogger));
|
||||||
|
Mocker.SetConstant<IHttpDispatcher>(new ManagedHttpDispatcher(Mocker.Resolve<IHttpProxySettingsProvider>(), Mocker.Resolve<ICreateManagedWebProxy>(), Mocker.Resolve<ICertificateValidationService>(), Mocker.Resolve<UserAgentBuilder>(), Mocker.Resolve<CacheManager>()));
|
||||||
Mocker.SetConstant<IHttpClient>(new HttpClient(Array.Empty<IHttpRequestInterceptor>(), Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), TestLogger));
|
Mocker.SetConstant<IHttpClient>(new HttpClient(Array.Empty<IHttpRequestInterceptor>(), Mocker.Resolve<CacheManager>(), Mocker.Resolve<RateLimitService>(), Mocker.Resolve<IHttpDispatcher>(), TestLogger));
|
||||||
Mocker.SetConstant<IProwlarrCloudRequestBuilder>(new ProwlarrCloudRequestBuilder());
|
Mocker.SetConstant<IProwlarrCloudRequestBuilder>(new ProwlarrCloudRequestBuilder());
|
||||||
}
|
}
|
||||||
|
@@ -1,111 +1,161 @@
|
|||||||
using CookComputing.XmlRpc;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using System.Xml.XPath;
|
||||||
|
using NzbDrone.Core.Download.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Download.Clients.Aria2
|
namespace NzbDrone.Core.Download.Clients.Aria2
|
||||||
{
|
{
|
||||||
public class Aria2Version
|
public class Aria2Fault
|
||||||
{
|
{
|
||||||
[XmlRpcMember("version")]
|
public Aria2Fault(XElement element)
|
||||||
public string Version;
|
{
|
||||||
|
foreach (var e in element.XPathSelectElements("./value/struct/member"))
|
||||||
|
{
|
||||||
|
var name = e.ElementAsString("name");
|
||||||
|
if (name == "faultCode")
|
||||||
|
{
|
||||||
|
FaultCode = e.Element("value").ElementAsInt("int");
|
||||||
|
}
|
||||||
|
else if (name == "faultString")
|
||||||
|
{
|
||||||
|
FaultString = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[XmlRpcMember("enabledFeatures")]
|
public int FaultCode { get; set; }
|
||||||
public string[] EnabledFeatures;
|
public string FaultString { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Aria2Uri
|
public class Aria2Version
|
||||||
{
|
{
|
||||||
[XmlRpcMember("status")]
|
public Aria2Version(XElement element)
|
||||||
public string Status;
|
{
|
||||||
|
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||||
|
{
|
||||||
|
if (e.ElementAsString("name") == "version")
|
||||||
|
{
|
||||||
|
Version = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[XmlRpcMember("uri")]
|
public string Version { get; set; }
|
||||||
public string Uri;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Aria2File
|
public class Aria2File
|
||||||
{
|
{
|
||||||
[XmlRpcMember("index")]
|
public Aria2File(XElement element)
|
||||||
public string Index;
|
{
|
||||||
|
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||||
|
{
|
||||||
|
var name = e.ElementAsString("name");
|
||||||
|
|
||||||
[XmlRpcMember("length")]
|
if (name == "path")
|
||||||
public string Length;
|
{
|
||||||
|
Path = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[XmlRpcMember("completedLength")]
|
public string Path { get; set; }
|
||||||
public string CompletedLength;
|
}
|
||||||
|
|
||||||
[XmlRpcMember("path")]
|
public class Aria2Dict
|
||||||
public string Path;
|
{
|
||||||
|
public Aria2Dict(XElement element)
|
||||||
|
{
|
||||||
|
Dict = new Dictionary<string, string>();
|
||||||
|
|
||||||
[XmlRpcMember("selected")]
|
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
{
|
||||||
public string Selected;
|
Dict.Add(e.ElementAsString("name"), e.Element("value").GetStringValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[XmlRpcMember("uris")]
|
public Dictionary<string, string> Dict { get; set; }
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
}
|
||||||
public Aria2Uri[] Uris;
|
|
||||||
|
public class Aria2Bittorrent
|
||||||
|
{
|
||||||
|
public Aria2Bittorrent(XElement element)
|
||||||
|
{
|
||||||
|
foreach (var e in element.Descendants("member"))
|
||||||
|
{
|
||||||
|
if (e.ElementAsString("name") == "name")
|
||||||
|
{
|
||||||
|
Name = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Aria2Status
|
public class Aria2Status
|
||||||
{
|
{
|
||||||
[XmlRpcMember("bittorrent")]
|
public Aria2Status(XElement element)
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
{
|
||||||
public XmlRpcStruct Bittorrent;
|
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||||
|
{
|
||||||
|
var name = e.ElementAsString("name");
|
||||||
|
|
||||||
[XmlRpcMember("bitfield")]
|
if (name == "bittorrent")
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
{
|
||||||
public string Bitfield;
|
Bittorrent = new Aria2Bittorrent(e.Element("value"));
|
||||||
|
}
|
||||||
|
else if (name == "infoHash")
|
||||||
|
{
|
||||||
|
InfoHash = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
else if (name == "completedLength")
|
||||||
|
{
|
||||||
|
CompletedLength = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
else if (name == "downloadSpeed")
|
||||||
|
{
|
||||||
|
DownloadSpeed = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
else if (name == "files")
|
||||||
|
{
|
||||||
|
Files = e.XPathSelectElement("./value/array/data")
|
||||||
|
.Elements()
|
||||||
|
.Select(x => new Aria2File(x))
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
else if (name == "gid")
|
||||||
|
{
|
||||||
|
Gid = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
else if (name == "status")
|
||||||
|
{
|
||||||
|
Status = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
else if (name == "totalLength")
|
||||||
|
{
|
||||||
|
TotalLength = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
else if (name == "uploadLength")
|
||||||
|
{
|
||||||
|
UploadLength = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
else if (name == "errorMessage")
|
||||||
|
{
|
||||||
|
ErrorMessage = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[XmlRpcMember("infoHash")]
|
public Aria2Bittorrent Bittorrent { get; set; }
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
public string InfoHash { get; set; }
|
||||||
public string InfoHash;
|
public string CompletedLength { get; set; }
|
||||||
|
public string DownloadSpeed { get; set; }
|
||||||
[XmlRpcMember("completedLength")]
|
public Aria2File[] Files { get; set; }
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
public string Gid { get; set; }
|
||||||
public string CompletedLength;
|
public string Status { get; set; }
|
||||||
|
public string TotalLength { get; set; }
|
||||||
[XmlRpcMember("connections")]
|
public string UploadLength { get; set; }
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
public string ErrorMessage { get; set; }
|
||||||
public string Connections;
|
|
||||||
|
|
||||||
[XmlRpcMember("dir")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string Dir;
|
|
||||||
|
|
||||||
[XmlRpcMember("downloadSpeed")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string DownloadSpeed;
|
|
||||||
|
|
||||||
[XmlRpcMember("files")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public Aria2File[] Files;
|
|
||||||
|
|
||||||
[XmlRpcMember("gid")]
|
|
||||||
public string Gid;
|
|
||||||
|
|
||||||
[XmlRpcMember("numPieces")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string NumPieces;
|
|
||||||
|
|
||||||
[XmlRpcMember("pieceLength")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string PieceLength;
|
|
||||||
|
|
||||||
[XmlRpcMember("status")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string Status;
|
|
||||||
|
|
||||||
[XmlRpcMember("totalLength")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string TotalLength;
|
|
||||||
|
|
||||||
[XmlRpcMember("uploadLength")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string UploadLength;
|
|
||||||
|
|
||||||
[XmlRpcMember("uploadSpeed")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string UploadSpeed;
|
|
||||||
|
|
||||||
[XmlRpcMember("errorMessage")]
|
|
||||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
|
||||||
public string ErrorMessage;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
using System.Linq;
|
||||||
using CookComputing.XmlRpc;
|
using System.Xml.Linq;
|
||||||
using NLog;
|
using System.Xml.XPath;
|
||||||
|
using NzbDrone.Common.Http;
|
||||||
|
using NzbDrone.Core.Download.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Download.Clients.Aria2
|
namespace NzbDrone.Core.Download.Clients.Aria2
|
||||||
{
|
{
|
||||||
@@ -12,46 +12,103 @@ namespace NzbDrone.Core.Download.Clients.Aria2
|
|||||||
string GetVersion(Aria2Settings settings);
|
string GetVersion(Aria2Settings settings);
|
||||||
string AddUri(Aria2Settings settings, string magnet);
|
string AddUri(Aria2Settings settings, string magnet);
|
||||||
string AddTorrent(Aria2Settings settings, byte[] torrent);
|
string AddTorrent(Aria2Settings settings, byte[] torrent);
|
||||||
|
Dictionary<string, string> GetGlobals(Aria2Settings settings);
|
||||||
|
List<Aria2Status> GetTorrents(Aria2Settings settings);
|
||||||
Aria2Status GetFromGID(Aria2Settings settings, string gid);
|
Aria2Status GetFromGID(Aria2Settings settings, string gid);
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IAria2 : IXmlRpcProxy
|
|
||||||
{
|
|
||||||
[XmlRpcMethod("aria2.getVersion")]
|
|
||||||
Aria2Version GetVersion(string token);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.addUri")]
|
|
||||||
string AddUri(string token, string[] uri);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.addTorrent")]
|
|
||||||
string AddTorrent(string token, byte[] torrent);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.forceRemove")]
|
|
||||||
string Remove(string token, string gid);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellStatus")]
|
|
||||||
Aria2Status GetFromGid(string token, string gid);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.getGlobalOption")]
|
|
||||||
XmlRpcStruct GetGlobalOption(string token);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellActive")]
|
|
||||||
Aria2Status[] GetActives(string token);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellWaiting")]
|
|
||||||
Aria2Status[] GetWaitings(string token, int offset, int num);
|
|
||||||
|
|
||||||
[XmlRpcMethod("aria2.tellStopped")]
|
|
||||||
Aria2Status[] GetStoppeds(string token, int offset, int num);
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Aria2Proxy : IAria2Proxy
|
public class Aria2Proxy : IAria2Proxy
|
||||||
{
|
{
|
||||||
private readonly Logger _logger;
|
private readonly IHttpClient _httpClient;
|
||||||
|
|
||||||
public Aria2Proxy(Logger logger)
|
public Aria2Proxy(IHttpClient httpClient)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetVersion(Aria2Settings settings)
|
||||||
|
{
|
||||||
|
var response = ExecuteRequest(settings, "aria2.getVersion", GetToken(settings));
|
||||||
|
|
||||||
|
var element = response.XPathSelectElement("./methodResponse/params/param/value");
|
||||||
|
|
||||||
|
var version = new Aria2Version(element);
|
||||||
|
|
||||||
|
return version.Version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Aria2Status GetFromGID(Aria2Settings settings, string gid)
|
||||||
|
{
|
||||||
|
var response = ExecuteRequest(settings, "aria2.tellStatus", GetToken(settings), gid);
|
||||||
|
|
||||||
|
var element = response.XPathSelectElement("./methodResponse/params/param/value");
|
||||||
|
|
||||||
|
return new Aria2Status(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Aria2Status> GetTorrentsMethod(Aria2Settings settings, string method, params object[] args)
|
||||||
|
{
|
||||||
|
var allArgs = new List<object> { GetToken(settings) };
|
||||||
|
if (args.Any())
|
||||||
|
{
|
||||||
|
allArgs.AddRange(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
var response = ExecuteRequest(settings, method, allArgs.ToArray());
|
||||||
|
|
||||||
|
var element = response.XPathSelectElement("./methodResponse/params/param/value/array/data");
|
||||||
|
|
||||||
|
var torrents = element?.Elements()
|
||||||
|
.Select(x => new Aria2Status(x))
|
||||||
|
.ToList()
|
||||||
|
?? new List<Aria2Status>();
|
||||||
|
return torrents;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Aria2Status> GetTorrents(Aria2Settings settings)
|
||||||
|
{
|
||||||
|
var active = GetTorrentsMethod(settings, "aria2.tellActive");
|
||||||
|
|
||||||
|
var waiting = GetTorrentsMethod(settings, "aria2.tellWaiting", 0, 10 * 1024);
|
||||||
|
|
||||||
|
var stopped = GetTorrentsMethod(settings, "aria2.tellStopped", 0, 10 * 1024);
|
||||||
|
|
||||||
|
var items = new List<Aria2Status>();
|
||||||
|
|
||||||
|
items.AddRange(active);
|
||||||
|
items.AddRange(waiting);
|
||||||
|
items.AddRange(stopped);
|
||||||
|
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Dictionary<string, string> GetGlobals(Aria2Settings settings)
|
||||||
|
{
|
||||||
|
var response = ExecuteRequest(settings, "aria2.getGlobalOption", GetToken(settings));
|
||||||
|
|
||||||
|
var element = response.XPathSelectElement("./methodResponse/params/param/value");
|
||||||
|
|
||||||
|
var result = new Aria2Dict(element);
|
||||||
|
|
||||||
|
return result.Dict;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string AddUri(Aria2Settings settings, string magnet)
|
||||||
|
{
|
||||||
|
var response = ExecuteRequest(settings, "aria2.addUri", GetToken(settings), new List<string> { magnet });
|
||||||
|
|
||||||
|
var gid = response.GetStringResponse();
|
||||||
|
|
||||||
|
return gid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string AddTorrent(Aria2Settings settings, byte[] torrent)
|
||||||
|
{
|
||||||
|
var response = ExecuteRequest(settings, "aria2.addTorrent", GetToken(settings), torrent);
|
||||||
|
|
||||||
|
var gid = response.GetStringResponse();
|
||||||
|
|
||||||
|
return gid;
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetToken(Aria2Settings settings)
|
private string GetToken(Aria2Settings settings)
|
||||||
@@ -59,86 +116,29 @@ namespace NzbDrone.Core.Download.Clients.Aria2
|
|||||||
return $"token:{settings?.SecretToken}";
|
return $"token:{settings?.SecretToken}";
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetURL(Aria2Settings settings)
|
private XDocument ExecuteRequest(Aria2Settings settings, string methodName, params object[] args)
|
||||||
{
|
{
|
||||||
return $"http{(settings.UseSsl ? "s" : "")}://{settings.Host}:{settings.Port}{settings.RpcPath}";
|
var requestBuilder = new XmlRpcRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.RpcPath)
|
||||||
}
|
|
||||||
|
|
||||||
public string GetVersion(Aria2Settings settings)
|
|
||||||
{
|
|
||||||
_logger.Debug("> aria2.getVersion");
|
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
|
||||||
var version = ExecuteRequest(() => client.GetVersion(GetToken(settings)));
|
|
||||||
|
|
||||||
_logger.Debug("< aria2.getVersion");
|
|
||||||
|
|
||||||
return version.Version;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Aria2Status GetFromGID(Aria2Settings settings, string gid)
|
|
||||||
{
|
|
||||||
_logger.Debug("> aria2.tellStatus");
|
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
|
||||||
var found = ExecuteRequest(() => client.GetFromGid(GetToken(settings), gid));
|
|
||||||
|
|
||||||
_logger.Debug("< aria2.tellStatus");
|
|
||||||
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string AddUri(Aria2Settings settings, string magnet)
|
|
||||||
{
|
|
||||||
_logger.Debug("> aria2.addUri");
|
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
|
||||||
var gid = ExecuteRequest(() => client.AddUri(GetToken(settings), new[] { magnet }));
|
|
||||||
|
|
||||||
_logger.Debug("< aria2.addUri");
|
|
||||||
|
|
||||||
return gid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string AddTorrent(Aria2Settings settings, byte[] torrent)
|
|
||||||
{
|
|
||||||
_logger.Debug("> aria2.addTorrent");
|
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
|
||||||
var gid = ExecuteRequest(() => client.AddTorrent(GetToken(settings), torrent));
|
|
||||||
|
|
||||||
_logger.Debug("< aria2.addTorrent");
|
|
||||||
|
|
||||||
return gid;
|
|
||||||
}
|
|
||||||
|
|
||||||
private IAria2 BuildClient(Aria2Settings settings)
|
|
||||||
{
|
|
||||||
var client = XmlRpcProxyGen.Create<IAria2>();
|
|
||||||
client.Url = GetURL(settings);
|
|
||||||
|
|
||||||
return client;
|
|
||||||
}
|
|
||||||
|
|
||||||
private T ExecuteRequest<T>(Func<T> task)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
return task();
|
LogResponseContent = true,
|
||||||
}
|
};
|
||||||
catch (XmlRpcServerException ex)
|
|
||||||
{
|
|
||||||
throw new DownloadClientException("Unable to connect to aria2, please check your settings", ex);
|
|
||||||
}
|
|
||||||
catch (WebException ex)
|
|
||||||
{
|
|
||||||
if (ex.Status == WebExceptionStatus.TrustFailure)
|
|
||||||
{
|
|
||||||
throw new DownloadClientUnavailableException("Unable to connect to aria2, certificate validation failed.", ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new DownloadClientUnavailableException("Unable to connect to aria2, please check your settings", ex);
|
var request = requestBuilder.Call(methodName, args).Build();
|
||||||
|
|
||||||
|
var response = _httpClient.Execute(request);
|
||||||
|
|
||||||
|
var doc = XDocument.Parse(response.Content);
|
||||||
|
|
||||||
|
var faultElement = doc.XPathSelectElement("./methodResponse/fault");
|
||||||
|
|
||||||
|
if (faultElement != null)
|
||||||
|
{
|
||||||
|
var fault = new Aria2Fault(faultElement);
|
||||||
|
|
||||||
|
throw new DownloadClientException($"Aria2 returned error code {fault.FaultCode}: {fault.FaultString}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return doc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
28
src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentFault.cs
Normal file
28
src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentFault.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using System.Xml.Linq;
|
||||||
|
using System.Xml.XPath;
|
||||||
|
using NzbDrone.Core.Download.Extensions;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Download.Clients.RTorrent
|
||||||
|
{
|
||||||
|
public class RTorrentFault
|
||||||
|
{
|
||||||
|
public RTorrentFault(XElement element)
|
||||||
|
{
|
||||||
|
foreach (var e in element.XPathSelectElements("./value/struct/member"))
|
||||||
|
{
|
||||||
|
var name = e.ElementAsString("name");
|
||||||
|
if (name == "faultCode")
|
||||||
|
{
|
||||||
|
FaultCode = e.Element("value").GetIntValue();
|
||||||
|
}
|
||||||
|
else if (name == "faultString")
|
||||||
|
{
|
||||||
|
FaultString = e.Element("value").GetStringValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int FaultCode { get; set; }
|
||||||
|
public string FaultString { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@@ -1,10 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using CookComputing.XmlRpc;
|
using System.Xml.Linq;
|
||||||
using NLog;
|
using System.Xml.XPath;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Common.Serializer;
|
using NzbDrone.Common.Http;
|
||||||
|
using NzbDrone.Core.Download.Extensions;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Download.Clients.RTorrent
|
namespace NzbDrone.Core.Download.Clients.RTorrent
|
||||||
{
|
{
|
||||||
@@ -18,122 +20,70 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
|
|||||||
void RemoveTorrent(string hash, RTorrentSettings settings);
|
void RemoveTorrent(string hash, RTorrentSettings settings);
|
||||||
void SetTorrentLabel(string hash, string label, RTorrentSettings settings);
|
void SetTorrentLabel(string hash, string label, RTorrentSettings settings);
|
||||||
bool HasHashTorrent(string hash, RTorrentSettings settings);
|
bool HasHashTorrent(string hash, RTorrentSettings settings);
|
||||||
}
|
void PushTorrentUniqueView(string hash, string view, RTorrentSettings settings);
|
||||||
|
|
||||||
public interface IRTorrent : IXmlRpcProxy
|
|
||||||
{
|
|
||||||
[XmlRpcMethod("d.multicall2")]
|
|
||||||
object[] TorrentMulticall(params string[] parameters);
|
|
||||||
|
|
||||||
[XmlRpcMethod("load.normal")]
|
|
||||||
int LoadNormal(string target, string data, params string[] commands);
|
|
||||||
|
|
||||||
[XmlRpcMethod("load.start")]
|
|
||||||
int LoadStart(string target, string data, params string[] commands);
|
|
||||||
|
|
||||||
[XmlRpcMethod("load.raw")]
|
|
||||||
int LoadRaw(string target, byte[] data, params string[] commands);
|
|
||||||
|
|
||||||
[XmlRpcMethod("load.raw_start")]
|
|
||||||
int LoadRawStart(string target, byte[] data, params string[] commands);
|
|
||||||
|
|
||||||
[XmlRpcMethod("d.erase")]
|
|
||||||
int Remove(string hash);
|
|
||||||
|
|
||||||
[XmlRpcMethod("d.name")]
|
|
||||||
string GetName(string hash);
|
|
||||||
|
|
||||||
[XmlRpcMethod("d.custom1.set")]
|
|
||||||
string SetLabel(string hash, string label);
|
|
||||||
|
|
||||||
[XmlRpcMethod("system.client_version")]
|
|
||||||
string GetVersion();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RTorrentProxy : IRTorrentProxy
|
public class RTorrentProxy : IRTorrentProxy
|
||||||
{
|
{
|
||||||
private readonly Logger _logger;
|
private readonly IHttpClient _httpClient;
|
||||||
|
|
||||||
public RTorrentProxy(Logger logger)
|
public RTorrentProxy(IHttpClient httpClient)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_httpClient = httpClient;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GetVersion(RTorrentSettings settings)
|
public string GetVersion(RTorrentSettings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("Executing remote method: system.client_version");
|
var document = ExecuteRequest(settings, "system.client_version");
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
return document.Descendants("string").FirstOrDefault()?.Value ?? "0.0.0";
|
||||||
var version = ExecuteRequest(() => client.GetVersion());
|
|
||||||
|
|
||||||
return version;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<RTorrentTorrent> GetTorrents(RTorrentSettings settings)
|
public List<RTorrentTorrent> GetTorrents(RTorrentSettings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("Executing remote method: d.multicall2");
|
var document = ExecuteRequest(settings,
|
||||||
|
"d.multicall2",
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"d.name=", // string
|
||||||
|
"d.hash=", // string
|
||||||
|
"d.base_path=", // string
|
||||||
|
"d.custom1=", // string (label)
|
||||||
|
"d.size_bytes=", // long
|
||||||
|
"d.left_bytes=", // long
|
||||||
|
"d.down.rate=", // long (in bytes / s)
|
||||||
|
"d.ratio=", // long
|
||||||
|
"d.is_open=", // long
|
||||||
|
"d.is_active=", // long
|
||||||
|
"d.complete=", //long
|
||||||
|
"d.timestamp.finished="); // long (unix timestamp)
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
var torrents = document.XPathSelectElement("./methodResponse/params/param/value/array/data")
|
||||||
var ret = ExecuteRequest(() => client.TorrentMulticall(
|
?.Elements()
|
||||||
"",
|
.Select(x => new RTorrentTorrent(x))
|
||||||
"",
|
.ToList()
|
||||||
"d.name=", // string
|
?? new List<RTorrentTorrent>();
|
||||||
"d.hash=", // string
|
|
||||||
"d.base_path=", // string
|
|
||||||
"d.custom1=", // string (label)
|
|
||||||
"d.size_bytes=", // long
|
|
||||||
"d.left_bytes=", // long
|
|
||||||
"d.down.rate=", // long (in bytes / s)
|
|
||||||
"d.ratio=", // long
|
|
||||||
"d.is_open=", // long
|
|
||||||
"d.is_active=", // long
|
|
||||||
"d.complete=")); //long
|
|
||||||
|
|
||||||
_logger.Trace(ret.ToJson());
|
return torrents;
|
||||||
|
|
||||||
var items = new List<RTorrentTorrent>();
|
|
||||||
|
|
||||||
foreach (object[] torrent in ret)
|
|
||||||
{
|
|
||||||
var labelDecoded = System.Web.HttpUtility.UrlDecode((string)torrent[3]);
|
|
||||||
|
|
||||||
var item = new RTorrentTorrent();
|
|
||||||
item.Name = (string)torrent[0];
|
|
||||||
item.Hash = (string)torrent[1];
|
|
||||||
item.Path = (string)torrent[2];
|
|
||||||
item.Category = labelDecoded;
|
|
||||||
item.TotalSize = (long)torrent[4];
|
|
||||||
item.RemainingSize = (long)torrent[5];
|
|
||||||
item.DownRate = (long)torrent[6];
|
|
||||||
item.Ratio = (long)torrent[7];
|
|
||||||
item.IsOpen = Convert.ToBoolean((long)torrent[8]);
|
|
||||||
item.IsActive = Convert.ToBoolean((long)torrent[9]);
|
|
||||||
item.IsFinished = Convert.ToBoolean((long)torrent[10]);
|
|
||||||
|
|
||||||
items.Add(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
return items;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddTorrentFromUrl(string torrentUrl, string label, RTorrentPriority priority, string directory, RTorrentSettings settings)
|
public void AddTorrentFromUrl(string torrentUrl, string label, RTorrentPriority priority, string directory, RTorrentSettings settings)
|
||||||
{
|
{
|
||||||
var client = BuildClient(settings);
|
var args = new List<object> { "", torrentUrl };
|
||||||
var response = ExecuteRequest(() =>
|
args.AddRange(GetCommands(label, priority, directory));
|
||||||
{
|
|
||||||
if (settings.AddStopped)
|
|
||||||
{
|
|
||||||
_logger.Debug("Executing remote method: load.normal");
|
|
||||||
return client.LoadNormal("", torrentUrl, GetCommands(label, priority, directory));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.Debug("Executing remote method: load.start");
|
|
||||||
return client.LoadStart("", torrentUrl, GetCommands(label, priority, directory));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response != 0)
|
XDocument response;
|
||||||
|
|
||||||
|
if (settings.AddStopped)
|
||||||
|
{
|
||||||
|
response = ExecuteRequest(settings, "load.normal", args.ToArray());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response = ExecuteRequest(settings, "load.start", args.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.GetIntResponse() != 0)
|
||||||
{
|
{
|
||||||
throw new DownloadClientException("Could not add torrent: {0}.", torrentUrl);
|
throw new DownloadClientException("Could not add torrent: {0}.", torrentUrl);
|
||||||
}
|
}
|
||||||
@@ -141,22 +91,21 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
|
|||||||
|
|
||||||
public void AddTorrentFromFile(string fileName, byte[] fileContent, string label, RTorrentPriority priority, string directory, RTorrentSettings settings)
|
public void AddTorrentFromFile(string fileName, byte[] fileContent, string label, RTorrentPriority priority, string directory, RTorrentSettings settings)
|
||||||
{
|
{
|
||||||
var client = BuildClient(settings);
|
var args = new List<object> { "", fileContent };
|
||||||
var response = ExecuteRequest(() =>
|
args.AddRange(GetCommands(label, priority, directory));
|
||||||
{
|
|
||||||
if (settings.AddStopped)
|
|
||||||
{
|
|
||||||
_logger.Debug("Executing remote method: load.raw");
|
|
||||||
return client.LoadRaw("", fileContent, GetCommands(label, priority, directory));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_logger.Debug("Executing remote method: load.raw_start");
|
|
||||||
return client.LoadRawStart("", fileContent, GetCommands(label, priority, directory));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response != 0)
|
XDocument response;
|
||||||
|
|
||||||
|
if (settings.AddStopped)
|
||||||
|
{
|
||||||
|
response = ExecuteRequest(settings, "load.raw", args.ToArray());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
response = ExecuteRequest(settings, "load.raw_start", args.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.GetIntResponse() != 0)
|
||||||
{
|
{
|
||||||
throw new DownloadClientException("Could not add torrent: {0}.", fileName);
|
throw new DownloadClientException("Could not add torrent: {0}.", fileName);
|
||||||
}
|
}
|
||||||
@@ -164,25 +113,29 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
|
|||||||
|
|
||||||
public void SetTorrentLabel(string hash, string label, RTorrentSettings settings)
|
public void SetTorrentLabel(string hash, string label, RTorrentSettings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("Executing remote method: d.custom1.set");
|
var response = ExecuteRequest(settings, "d.custom1.set", hash, label);
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
if (response.GetStringResponse() != label)
|
||||||
var response = ExecuteRequest(() => client.SetLabel(hash, label));
|
|
||||||
|
|
||||||
if (response != label)
|
|
||||||
{
|
{
|
||||||
throw new DownloadClientException("Could not set label to {1} for torrent: {0}.", hash, label);
|
throw new DownloadClientException("Could not set label to {1} for torrent: {0}.", hash, label);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void PushTorrentUniqueView(string hash, string view, RTorrentSettings settings)
|
||||||
|
{
|
||||||
|
var response = ExecuteRequest(settings, "d.views.push_back_unique", hash, view);
|
||||||
|
|
||||||
|
if (response.GetIntResponse() != 0)
|
||||||
|
{
|
||||||
|
throw new DownloadClientException("Could not push unique view {0} for torrent: {1}.", view, hash);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void RemoveTorrent(string hash, RTorrentSettings settings)
|
public void RemoveTorrent(string hash, RTorrentSettings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("Executing remote method: d.erase");
|
var response = ExecuteRequest(settings, "d.erase", hash);
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
if (response.GetIntResponse() != 0)
|
||||||
var response = ExecuteRequest(() => client.Remove(hash));
|
|
||||||
|
|
||||||
if (response != 0)
|
|
||||||
{
|
{
|
||||||
throw new DownloadClientException("Could not remove torrent: {0}.", hash);
|
throw new DownloadClientException("Could not remove torrent: {0}.", hash);
|
||||||
}
|
}
|
||||||
@@ -190,13 +143,10 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
|
|||||||
|
|
||||||
public bool HasHashTorrent(string hash, RTorrentSettings settings)
|
public bool HasHashTorrent(string hash, RTorrentSettings settings)
|
||||||
{
|
{
|
||||||
_logger.Debug("Executing remote method: d.name");
|
|
||||||
|
|
||||||
var client = BuildClient(settings);
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var name = ExecuteRequest(() => client.GetName(hash));
|
var response = ExecuteRequest(settings, "d.name", hash);
|
||||||
|
var name = response.GetStringResponse();
|
||||||
|
|
||||||
if (name.IsNullOrWhiteSpace())
|
if (name.IsNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
@@ -235,45 +185,34 @@ namespace NzbDrone.Core.Download.Clients.RTorrent
|
|||||||
return result.ToArray();
|
return result.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
private IRTorrent BuildClient(RTorrentSettings settings)
|
private XDocument ExecuteRequest(RTorrentSettings settings, string methodName, params object[] args)
|
||||||
{
|
{
|
||||||
var client = XmlRpcProxyGen.Create<IRTorrent>();
|
var requestBuilder = new XmlRpcRequestBuilder(settings.UseSsl, settings.Host, settings.Port, settings.UrlBase)
|
||||||
|
{
|
||||||
client.Url = string.Format(@"{0}://{1}:{2}/{3}",
|
LogResponseContent = true,
|
||||||
settings.UseSsl ? "https" : "http",
|
};
|
||||||
settings.Host,
|
|
||||||
settings.Port,
|
|
||||||
settings.UrlBase);
|
|
||||||
|
|
||||||
client.EnableCompression = true;
|
|
||||||
|
|
||||||
if (!settings.Username.IsNullOrWhiteSpace())
|
if (!settings.Username.IsNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
client.Credentials = new NetworkCredential(settings.Username, settings.Password);
|
requestBuilder.NetworkCredential = new NetworkCredential(settings.Username, settings.Password);
|
||||||
}
|
}
|
||||||
|
|
||||||
return client;
|
var request = requestBuilder.Call(methodName, args).Build();
|
||||||
}
|
|
||||||
|
|
||||||
private T ExecuteRequest<T>(Func<T> task)
|
var response = _httpClient.Execute(request);
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
return task();
|
|
||||||
}
|
|
||||||
catch (XmlRpcServerException ex)
|
|
||||||
{
|
|
||||||
throw new DownloadClientException("Unable to connect to rTorrent, please check your settings", ex);
|
|
||||||
}
|
|
||||||
catch (WebException ex)
|
|
||||||
{
|
|
||||||
if (ex.Status == WebExceptionStatus.TrustFailure)
|
|
||||||
{
|
|
||||||
throw new DownloadClientUnavailableException("Unable to connect to rTorrent, certificate validation failed.", ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new DownloadClientUnavailableException("Unable to connect to rTorrent, please check your settings", ex);
|
var doc = XDocument.Parse(response.Content);
|
||||||
|
|
||||||
|
var faultElement = doc.XPathSelectElement("./methodResponse/fault");
|
||||||
|
|
||||||
|
if (faultElement != null)
|
||||||
|
{
|
||||||
|
var fault = new RTorrentFault(faultElement);
|
||||||
|
|
||||||
|
throw new DownloadClientException($"rTorrent returned error code {fault.FaultCode}: {fault.FaultString}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return doc;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,35 @@
|
|||||||
namespace NzbDrone.Core.Download.Clients.RTorrent
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
using NzbDrone.Core.Download.Extensions;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Download.Clients.RTorrent
|
||||||
{
|
{
|
||||||
public class RTorrentTorrent
|
public class RTorrentTorrent
|
||||||
{
|
{
|
||||||
|
public RTorrentTorrent()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public RTorrentTorrent(XElement element)
|
||||||
|
{
|
||||||
|
var data = element.Descendants("value").ToList();
|
||||||
|
|
||||||
|
Name = data[0].GetStringValue();
|
||||||
|
Hash = data[1].GetStringValue();
|
||||||
|
Path = data[2].GetStringValue();
|
||||||
|
Category = HttpUtility.UrlDecode(data[3].GetStringValue());
|
||||||
|
TotalSize = data[4].GetLongValue();
|
||||||
|
RemainingSize = data[5].GetLongValue();
|
||||||
|
DownRate = data[6].GetLongValue();
|
||||||
|
Ratio = data[7].GetLongValue();
|
||||||
|
IsOpen = Convert.ToBoolean(data[8].GetLongValue());
|
||||||
|
IsActive = Convert.ToBoolean(data[9].GetLongValue());
|
||||||
|
IsFinished = Convert.ToBoolean(data[10].GetLongValue());
|
||||||
|
FinishedTime = data[11].GetLongValue();
|
||||||
|
}
|
||||||
|
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Hash { get; set; }
|
public string Hash { get; set; }
|
||||||
public string Path { get; set; }
|
public string Path { get; set; }
|
||||||
@@ -10,6 +38,7 @@
|
|||||||
public long RemainingSize { get; set; }
|
public long RemainingSize { get; set; }
|
||||||
public long DownRate { get; set; }
|
public long DownRate { get; set; }
|
||||||
public long Ratio { get; set; }
|
public long Ratio { get; set; }
|
||||||
|
public long FinishedTime { get; set; }
|
||||||
public bool IsFinished { get; set; }
|
public bool IsFinished { get; set; }
|
||||||
public bool IsOpen { get; set; }
|
public bool IsOpen { get; set; }
|
||||||
public bool IsActive { get; set; }
|
public bool IsActive { get; set; }
|
||||||
|
54
src/NzbDrone.Core/Download/Extensions/XmlExtensions.cs
Normal file
54
src/NzbDrone.Core/Download/Extensions/XmlExtensions.cs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
using System.Xml.Linq;
|
||||||
|
using System.Xml.XPath;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Download.Extensions
|
||||||
|
{
|
||||||
|
internal static class XmlExtensions
|
||||||
|
{
|
||||||
|
public static string GetStringValue(this XElement element)
|
||||||
|
{
|
||||||
|
return element.ElementAsString("string");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long GetLongValue(this XElement element)
|
||||||
|
{
|
||||||
|
return element.ElementAsLong("i8");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int GetIntValue(this XElement element)
|
||||||
|
{
|
||||||
|
return element.ElementAsInt("i4");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ElementAsString(this XElement element, XName name, bool trim = false)
|
||||||
|
{
|
||||||
|
var el = element.Element(name);
|
||||||
|
|
||||||
|
return string.IsNullOrWhiteSpace(el?.Value)
|
||||||
|
? null
|
||||||
|
: (trim ? el.Value.Trim() : el.Value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long ElementAsLong(this XElement element, XName name)
|
||||||
|
{
|
||||||
|
var el = element.Element(name);
|
||||||
|
return long.TryParse(el?.Value, out long value) ? value : default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int ElementAsInt(this XElement element, XName name)
|
||||||
|
{
|
||||||
|
var el = element.Element(name);
|
||||||
|
return int.TryParse(el?.Value, out int value) ? value : default(int);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int GetIntResponse(this XDocument document)
|
||||||
|
{
|
||||||
|
return document.XPathSelectElement("./methodResponse/params/param/value").GetIntValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GetStringResponse(this XDocument document)
|
||||||
|
{
|
||||||
|
return document.XPathSelectElement("./methodResponse/params/param/value").GetStringValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -59,7 +59,7 @@ namespace NzbDrone.Core.Indexers.Headphones
|
|||||||
|
|
||||||
var request = requestBuilder.Build();
|
var request = requestBuilder.Build();
|
||||||
|
|
||||||
request.AddBasicAuthentication(Settings.Username, Settings.Password);
|
request.Credentials = new BasicNetworkCredential(Settings.Username, Settings.Password);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@@ -131,7 +131,7 @@ namespace NzbDrone.Core.Indexers.Headphones
|
|||||||
}
|
}
|
||||||
|
|
||||||
var request = new IndexerRequest(string.Format("{0}&{1}", baseUrl, parameters.GetQueryString()), HttpAccept.Rss);
|
var request = new IndexerRequest(string.Format("{0}&{1}", baseUrl, parameters.GetQueryString()), HttpAccept.Rss);
|
||||||
request.HttpRequest.AddBasicAuthentication(Settings.Username, Settings.Password);
|
request.HttpRequest.Credentials = new BasicNetworkCredential(Settings.Username, Settings.Password);
|
||||||
|
|
||||||
yield return request;
|
yield return request;
|
||||||
}
|
}
|
||||||
|
@@ -7,17 +7,20 @@ using MailKit.Security;
|
|||||||
using MimeKit;
|
using MimeKit;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
using NzbDrone.Common.Http.Dispatchers;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Notifications.Email
|
namespace NzbDrone.Core.Notifications.Email
|
||||||
{
|
{
|
||||||
public class Email : NotificationBase<EmailSettings>
|
public class Email : NotificationBase<EmailSettings>
|
||||||
{
|
{
|
||||||
|
private readonly ICertificateValidationService _certificateValidationService;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
|
|
||||||
public override string Name => "Email";
|
public override string Name => "Email";
|
||||||
|
|
||||||
public Email(Logger logger)
|
public Email(ICertificateValidationService certificateValidationService, Logger logger)
|
||||||
{
|
{
|
||||||
|
_certificateValidationService = certificateValidationService;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,6 +116,8 @@ namespace NzbDrone.Core.Notifications.Email
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
client.ServerCertificateValidationCallback = _certificateValidationService.ShouldByPassValidationError;
|
||||||
|
|
||||||
_logger.Debug("Connecting to mail server");
|
_logger.Debug("Connecting to mail server");
|
||||||
|
|
||||||
client.Connect(settings.Server, settings.Port, serverOption);
|
client.Connect(settings.Server, settings.Port, serverOption);
|
||||||
|
@@ -102,7 +102,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
|||||||
var request = requestBuilder.Build();
|
var request = requestBuilder.Build();
|
||||||
|
|
||||||
request.Method = HttpMethod.Get;
|
request.Method = HttpMethod.Get;
|
||||||
request.AddBasicAuthentication(settings.ApiKey, string.Empty);
|
request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
|
||||||
|
|
||||||
var response = _httpClient.Execute(request);
|
var response = _httpClient.Execute(request);
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ namespace NzbDrone.Core.Notifications.PushBullet
|
|||||||
|
|
||||||
var request = requestBuilder.Build();
|
var request = requestBuilder.Build();
|
||||||
|
|
||||||
request.AddBasicAuthentication(settings.ApiKey, string.Empty);
|
request.Credentials = new BasicNetworkCredential(settings.ApiKey, string.Empty);
|
||||||
|
|
||||||
_httpClient.Execute(request);
|
_httpClient.Execute(request);
|
||||||
}
|
}
|
||||||
|
110
src/NzbDrone.Core/Notifications/Twitter/TwitterProxy.cs
Normal file
110
src/NzbDrone.Core/Notifications/Twitter/TwitterProxy.cs
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.Specialized;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
|
using System.Text;
|
||||||
|
using System.Web;
|
||||||
|
using NzbDrone.Common.Extensions;
|
||||||
|
using NzbDrone.Common.Http;
|
||||||
|
using NzbDrone.Common.OAuth;
|
||||||
|
|
||||||
|
namespace NzbDrone.Core.Notifications.Twitter
|
||||||
|
{
|
||||||
|
public interface ITwitterProxy
|
||||||
|
{
|
||||||
|
NameValueCollection GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier);
|
||||||
|
string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl);
|
||||||
|
void UpdateStatus(string message, TwitterSettings settings);
|
||||||
|
void DirectMessage(string message, TwitterSettings settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class TwitterProxy : ITwitterProxy
|
||||||
|
{
|
||||||
|
private readonly IHttpClient _httpClient;
|
||||||
|
|
||||||
|
public TwitterProxy(IHttpClient httpClient)
|
||||||
|
{
|
||||||
|
_httpClient = httpClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl)
|
||||||
|
{
|
||||||
|
// Creating a new instance with a helper method
|
||||||
|
var oAuthRequest = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callbackUrl);
|
||||||
|
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token";
|
||||||
|
var qscoll = HttpUtility.ParseQueryString(ExecuteRequest(GetRequest(oAuthRequest, new Dictionary<string, string>())).Content);
|
||||||
|
|
||||||
|
return string.Format("https://api.twitter.com/oauth/authorize?oauth_token={0}", qscoll["oauth_token"]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NameValueCollection GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
|
||||||
|
{
|
||||||
|
// Creating a new instance with a helper method
|
||||||
|
var oAuthRequest = OAuthRequest.ForAccessToken(consumerKey, consumerSecret, oauthToken, "", oauthVerifier);
|
||||||
|
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
|
||||||
|
|
||||||
|
return HttpUtility.ParseQueryString(ExecuteRequest(GetRequest(oAuthRequest, new Dictionary<string, string>())).Content);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void UpdateStatus(string message, TwitterSettings settings)
|
||||||
|
{
|
||||||
|
var oAuthRequest = OAuthRequest.ForProtectedResource("POST", settings.ConsumerKey, settings.ConsumerSecret, settings.AccessToken, settings.AccessTokenSecret);
|
||||||
|
|
||||||
|
oAuthRequest.RequestUrl = "https://api.twitter.com/1.1/statuses/update.json";
|
||||||
|
|
||||||
|
var customParams = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "status", message.EncodeRFC3986() }
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = GetRequest(oAuthRequest, customParams);
|
||||||
|
|
||||||
|
request.Headers.ContentType = "application/x-www-form-urlencoded";
|
||||||
|
request.SetContent(Encoding.ASCII.GetBytes(GetCustomParametersString(customParams)));
|
||||||
|
|
||||||
|
ExecuteRequest(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DirectMessage(string message, TwitterSettings settings)
|
||||||
|
{
|
||||||
|
var oAuthRequest = OAuthRequest.ForProtectedResource("POST", settings.ConsumerKey, settings.ConsumerSecret, settings.AccessToken, settings.AccessTokenSecret);
|
||||||
|
|
||||||
|
oAuthRequest.RequestUrl = "https://api.twitter.com/1.1/direct_messages/new.json";
|
||||||
|
|
||||||
|
var customParams = new Dictionary<string, string>
|
||||||
|
{
|
||||||
|
{ "text", message.EncodeRFC3986() },
|
||||||
|
{ "screenname", settings.Mention.EncodeRFC3986() }
|
||||||
|
};
|
||||||
|
|
||||||
|
var request = GetRequest(oAuthRequest, customParams);
|
||||||
|
|
||||||
|
request.Headers.ContentType = "application/x-www-form-urlencoded";
|
||||||
|
request.SetContent(Encoding.ASCII.GetBytes(GetCustomParametersString(customParams)));
|
||||||
|
|
||||||
|
ExecuteRequest(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
private string GetCustomParametersString(Dictionary<string, string> customParams)
|
||||||
|
{
|
||||||
|
return customParams.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpRequest GetRequest(OAuthRequest oAuthRequest, Dictionary<string, string> customParams)
|
||||||
|
{
|
||||||
|
var auth = oAuthRequest.GetAuthorizationHeader(customParams);
|
||||||
|
var request = new HttpRequest(oAuthRequest.RequestUrl);
|
||||||
|
|
||||||
|
request.Headers.Add("Authorization", auth);
|
||||||
|
|
||||||
|
request.Method = oAuthRequest.Method == "POST" ? HttpMethod.Post : HttpMethod.Get;
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
private HttpResponse ExecuteRequest(HttpRequest request)
|
||||||
|
{
|
||||||
|
return _httpClient.Execute(request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -1,13 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Specialized;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Web;
|
|
||||||
using FluentValidation.Results;
|
using FluentValidation.Results;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Common.Http;
|
|
||||||
using NzbDrone.Common.OAuth;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Notifications.Twitter
|
namespace NzbDrone.Core.Notifications.Twitter
|
||||||
{
|
{
|
||||||
@@ -21,31 +17,18 @@ namespace NzbDrone.Core.Notifications.Twitter
|
|||||||
|
|
||||||
public class TwitterService : ITwitterService
|
public class TwitterService : ITwitterService
|
||||||
{
|
{
|
||||||
private readonly IHttpClient _httpClient;
|
private readonly ITwitterProxy _twitterProxy;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
|
|
||||||
public TwitterService(IHttpClient httpClient, Logger logger)
|
public TwitterService(ITwitterProxy twitterProxy, Logger logger)
|
||||||
{
|
{
|
||||||
_httpClient = httpClient;
|
_twitterProxy = twitterProxy;
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
private NameValueCollection OAuthQuery(OAuthRequest oAuthRequest)
|
|
||||||
{
|
|
||||||
var auth = oAuthRequest.GetAuthorizationHeader();
|
|
||||||
var request = new Common.Http.HttpRequest(oAuthRequest.RequestUrl);
|
|
||||||
request.Headers.Add("Authorization", auth);
|
|
||||||
var response = _httpClient.Get(request);
|
|
||||||
|
|
||||||
return HttpUtility.ParseQueryString(response.Content);
|
|
||||||
}
|
|
||||||
|
|
||||||
public OAuthToken GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
|
public OAuthToken GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
|
||||||
{
|
{
|
||||||
// Creating a new instance with a helper method
|
var qscoll = _twitterProxy.GetOAuthToken(consumerKey, consumerSecret, oauthToken, oauthVerifier);
|
||||||
var oAuthRequest = OAuthRequest.ForAccessToken(consumerKey, consumerSecret, oauthToken, "", oauthVerifier);
|
|
||||||
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
|
|
||||||
var qscoll = OAuthQuery(oAuthRequest);
|
|
||||||
|
|
||||||
return new OAuthToken
|
return new OAuthToken
|
||||||
{
|
{
|
||||||
@@ -56,31 +39,16 @@ namespace NzbDrone.Core.Notifications.Twitter
|
|||||||
|
|
||||||
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl)
|
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl)
|
||||||
{
|
{
|
||||||
// Creating a new instance with a helper method
|
return _twitterProxy.GetOAuthRedirect(consumerKey, consumerSecret, callbackUrl);
|
||||||
var oAuthRequest = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callbackUrl);
|
|
||||||
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token";
|
|
||||||
var qscoll = OAuthQuery(oAuthRequest);
|
|
||||||
|
|
||||||
return string.Format("https://api.twitter.com/oauth/authorize?oauth_token={0}", qscoll["oauth_token"]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SendNotification(string message, TwitterSettings settings)
|
public void SendNotification(string message, TwitterSettings settings)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var oAuth = new TinyTwitter.OAuthInfo
|
|
||||||
{
|
|
||||||
ConsumerKey = settings.ConsumerKey,
|
|
||||||
ConsumerSecret = settings.ConsumerSecret,
|
|
||||||
AccessToken = settings.AccessToken,
|
|
||||||
AccessSecret = settings.AccessTokenSecret
|
|
||||||
};
|
|
||||||
|
|
||||||
var twitter = new TinyTwitter.TinyTwitter(oAuth);
|
|
||||||
|
|
||||||
if (settings.DirectMessage)
|
if (settings.DirectMessage)
|
||||||
{
|
{
|
||||||
twitter.DirectMessage(message, settings.Mention);
|
_twitterProxy.DirectMessage(message, settings);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -89,7 +57,7 @@ namespace NzbDrone.Core.Notifications.Twitter
|
|||||||
message += string.Format(" @{0}", settings.Mention);
|
message += string.Format(" @{0}", settings.Mention);
|
||||||
}
|
}
|
||||||
|
|
||||||
twitter.UpdateStatus(message);
|
_twitterProxy.UpdateStatus(message, settings);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (WebException ex)
|
catch (WebException ex)
|
||||||
|
@@ -40,7 +40,7 @@ namespace NzbDrone.Core.Notifications.Webhook
|
|||||||
|
|
||||||
if (settings.Username.IsNotNullOrWhiteSpace() || settings.Password.IsNotNullOrWhiteSpace())
|
if (settings.Username.IsNotNullOrWhiteSpace() || settings.Password.IsNotNullOrWhiteSpace())
|
||||||
{
|
{
|
||||||
request.AddBasicAuthentication(settings.Username, settings.Password);
|
request.Credentials = new BasicNetworkCredential(settings.Username, settings.Password);
|
||||||
}
|
}
|
||||||
|
|
||||||
_httpClient.Execute(request);
|
_httpClient.Execute(request);
|
||||||
|
@@ -4,13 +4,12 @@ using System.Net.Security;
|
|||||||
using System.Security.Cryptography.X509Certificates;
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using NLog;
|
using NLog;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
using NzbDrone.Common.Http.Dispatchers;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
using NzbDrone.Core.Lifecycle;
|
|
||||||
using NzbDrone.Core.Messaging.Events;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Security
|
namespace NzbDrone.Core.Security
|
||||||
{
|
{
|
||||||
public class X509CertificateValidationService : IHandle<ApplicationStartedEvent>
|
public class X509CertificateValidationService : ICertificateValidationService
|
||||||
{
|
{
|
||||||
private readonly IConfigService _configService;
|
private readonly IConfigService _configService;
|
||||||
private readonly Logger _logger;
|
private readonly Logger _logger;
|
||||||
@@ -21,19 +20,29 @@ namespace NzbDrone.Core.Security
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool ShouldByPassValidationError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
public bool ShouldByPassValidationError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
{
|
{
|
||||||
var request = sender as HttpWebRequest;
|
var targetHostName = string.Empty;
|
||||||
|
|
||||||
if (request == null)
|
if (sender is not SslStream && sender is not string)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var cert2 = certificate as X509Certificate2;
|
if (sender is SslStream request)
|
||||||
if (cert2 != null && request != null && cert2.SignatureAlgorithm.FriendlyName == "md5RSA")
|
|
||||||
{
|
{
|
||||||
_logger.Error("https://{0} uses the obsolete md5 hash in it's https certificate, if that is your certificate, please (re)create certificate with better algorithm as soon as possible.", request.RequestUri.Authority);
|
targetHostName = request.TargetHostName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mailkit passes host in sender as string
|
||||||
|
if (sender is string stringHost)
|
||||||
|
{
|
||||||
|
targetHostName = stringHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (certificate is X509Certificate2 cert2 && cert2.SignatureAlgorithm.FriendlyName == "md5RSA")
|
||||||
|
{
|
||||||
|
_logger.Error("https://{0} uses the obsolete md5 hash in it's https certificate, if that is your certificate, please (re)create certificate with better algorithm as soon as possible.", targetHostName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sslPolicyErrors == SslPolicyErrors.None)
|
if (sslPolicyErrors == SslPolicyErrors.None)
|
||||||
@@ -41,12 +50,12 @@ namespace NzbDrone.Core.Security
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.RequestUri.Host == "localhost" || request.RequestUri.Host == "127.0.0.1")
|
if (targetHostName == "localhost" || targetHostName == "127.0.0.1")
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var ipAddresses = GetIPAddresses(request.RequestUri.Host);
|
var ipAddresses = GetIPAddresses(targetHostName);
|
||||||
var certificateValidation = _configService.CertificateValidation;
|
var certificateValidation = _configService.CertificateValidation;
|
||||||
|
|
||||||
if (certificateValidation == CertificateValidationType.Disabled)
|
if (certificateValidation == CertificateValidationType.Disabled)
|
||||||
@@ -60,7 +69,7 @@ namespace NzbDrone.Core.Security
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
_logger.Error("Certificate validation for {0} failed. {1}", request.Address, sslPolicyErrors);
|
_logger.Error("Certificate validation for {0} failed. {1}", targetHostName, sslPolicyErrors);
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -74,10 +83,5 @@ namespace NzbDrone.Core.Security
|
|||||||
|
|
||||||
return Dns.GetHostEntry(host).AddressList;
|
return Dns.GetHostEntry(host).AddressList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Handle(ApplicationStartedEvent message)
|
|
||||||
{
|
|
||||||
ServicePointManager.ServerCertificateValidationCallback = ShouldByPassValidationError;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,235 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Net;
|
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
|
|
||||||
namespace TinyTwitter
|
|
||||||
{
|
|
||||||
public class OAuthInfo
|
|
||||||
{
|
|
||||||
public string ConsumerKey { get; set; }
|
|
||||||
public string ConsumerSecret { get; set; }
|
|
||||||
public string AccessToken { get; set; }
|
|
||||||
public string AccessSecret { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Tweet
|
|
||||||
{
|
|
||||||
public long Id { get; set; }
|
|
||||||
public DateTime CreatedAt { get; set; }
|
|
||||||
public string UserName { get; set; }
|
|
||||||
public string ScreenName { get; set; }
|
|
||||||
public string Text { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class TinyTwitter
|
|
||||||
{
|
|
||||||
private readonly OAuthInfo _oauth;
|
|
||||||
|
|
||||||
public TinyTwitter(OAuthInfo oauth)
|
|
||||||
{
|
|
||||||
_oauth = oauth;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void UpdateStatus(string message)
|
|
||||||
{
|
|
||||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
|
||||||
.AddParameter("status", message)
|
|
||||||
.Execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* As of June 26th 2015 Direct Messaging is not part of TinyTwitter.
|
|
||||||
* I have added it to Sonarr's copy to make our implementation easier
|
|
||||||
* and added this banner so it's not blindly updated.
|
|
||||||
*
|
|
||||||
**/
|
|
||||||
public void DirectMessage(string message, string screenName)
|
|
||||||
{
|
|
||||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
|
|
||||||
.AddParameter("text", message)
|
|
||||||
.AddParameter("screen_name", screenName)
|
|
||||||
.Execute();
|
|
||||||
}
|
|
||||||
|
|
||||||
public class RequestBuilder
|
|
||||||
{
|
|
||||||
private const string VERSION = "1.0";
|
|
||||||
private const string SIGNATURE_METHOD = "HMAC-SHA1";
|
|
||||||
|
|
||||||
private readonly OAuthInfo _oauth;
|
|
||||||
private readonly string _method;
|
|
||||||
private readonly IDictionary<string, string> _customParameters;
|
|
||||||
private readonly string _url;
|
|
||||||
|
|
||||||
public RequestBuilder(OAuthInfo oauth, string method, string url)
|
|
||||||
{
|
|
||||||
_oauth = oauth;
|
|
||||||
_method = method;
|
|
||||||
_url = url;
|
|
||||||
_customParameters = new Dictionary<string, string>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public RequestBuilder AddParameter(string name, string value)
|
|
||||||
{
|
|
||||||
_customParameters.Add(name, value.EncodeRFC3986());
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Execute()
|
|
||||||
{
|
|
||||||
var timespan = GetTimestamp();
|
|
||||||
var nonce = CreateNonce();
|
|
||||||
|
|
||||||
var parameters = new Dictionary<string, string>(_customParameters);
|
|
||||||
AddOAuthParameters(parameters, timespan, nonce);
|
|
||||||
|
|
||||||
var signature = GenerateSignature(parameters);
|
|
||||||
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature);
|
|
||||||
|
|
||||||
var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl());
|
|
||||||
request.Method = _method;
|
|
||||||
request.ContentType = "application/x-www-form-urlencoded";
|
|
||||||
|
|
||||||
request.Headers.Add("Authorization", headerValue);
|
|
||||||
|
|
||||||
WriteRequestBody(request);
|
|
||||||
|
|
||||||
// It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions
|
|
||||||
// after some requests. Abort the request seems to work. More info:
|
|
||||||
// http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly
|
|
||||||
var response = request.GetResponse();
|
|
||||||
|
|
||||||
string content;
|
|
||||||
|
|
||||||
using (var stream = response.GetResponseStream())
|
|
||||||
{
|
|
||||||
using (var reader = new StreamReader(stream))
|
|
||||||
{
|
|
||||||
content = reader.ReadToEnd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
request.Abort();
|
|
||||||
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void WriteRequestBody(HttpWebRequest request)
|
|
||||||
{
|
|
||||||
if (_method == "GET")
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString());
|
|
||||||
using (var stream = request.GetRequestStream())
|
|
||||||
{
|
|
||||||
stream.Write(requestBody, 0, requestBody.Length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetRequestUrl()
|
|
||||||
{
|
|
||||||
if (_method != "GET" || _customParameters.Count == 0)
|
|
||||||
{
|
|
||||||
return _url;
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Format("{0}?{1}", _url, GetCustomParametersString());
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GetCustomParametersString()
|
|
||||||
{
|
|
||||||
return _customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
|
|
||||||
{
|
|
||||||
return new StringBuilder("OAuth ")
|
|
||||||
.Append(parameters.Concat(new KeyValuePair<string, string>("oauth_signature", signature))
|
|
||||||
.Where(x => x.Key.StartsWith("oauth_"))
|
|
||||||
.Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986()))
|
|
||||||
.Join(","))
|
|
||||||
.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters)
|
|
||||||
{
|
|
||||||
var dataToSign = new StringBuilder()
|
|
||||||
.Append(_method).Append('&')
|
|
||||||
.Append(_url.EncodeRFC3986()).Append('&')
|
|
||||||
.Append(parameters
|
|
||||||
.OrderBy(x => x.Key)
|
|
||||||
.Select(x => string.Format("{0}={1}", x.Key, x.Value))
|
|
||||||
.Join("&")
|
|
||||||
.EncodeRFC3986());
|
|
||||||
|
|
||||||
var signatureKey = string.Format("{0}&{1}", _oauth.ConsumerSecret.EncodeRFC3986(), _oauth.AccessSecret.EncodeRFC3986());
|
|
||||||
var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey));
|
|
||||||
|
|
||||||
var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString()));
|
|
||||||
return Convert.ToBase64String(signatureBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void AddOAuthParameters(IDictionary<string, string> parameters, string timestamp, string nonce)
|
|
||||||
{
|
|
||||||
parameters.Add("oauth_version", VERSION);
|
|
||||||
parameters.Add("oauth_consumer_key", _oauth.ConsumerKey);
|
|
||||||
parameters.Add("oauth_nonce", nonce);
|
|
||||||
parameters.Add("oauth_signature_method", SIGNATURE_METHOD);
|
|
||||||
parameters.Add("oauth_timestamp", timestamp);
|
|
||||||
parameters.Add("oauth_token", _oauth.AccessToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetTimestamp()
|
|
||||||
{
|
|
||||||
return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string CreateNonce()
|
|
||||||
{
|
|
||||||
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class TinyTwitterHelperExtensions
|
|
||||||
{
|
|
||||||
public static string Join<T>(this IEnumerable<T> items, string separator)
|
|
||||||
{
|
|
||||||
return string.Join(separator, items.ToArray());
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T value)
|
|
||||||
{
|
|
||||||
return items.Concat(new[] { value });
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string EncodeRFC3986(this string value)
|
|
||||||
{
|
|
||||||
// From Twitterizer http://www.twitterizer.net/
|
|
||||||
if (string.IsNullOrEmpty(value))
|
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
var encoded = Uri.EscapeDataString(value);
|
|
||||||
|
|
||||||
return Regex
|
|
||||||
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
|
|
||||||
.Replace("(", "%28")
|
|
||||||
.Replace(")", "%29")
|
|
||||||
.Replace("$", "%24")
|
|
||||||
.Replace("!", "%21")
|
|
||||||
.Replace("*", "%2A")
|
|
||||||
.Replace("'", "%27")
|
|
||||||
.Replace("%7E", "~");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user