1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-09-17 15:02:34 +02:00

Fixed: RestClient does not use global proxy settings

Co-Authored-By: Petr Shurgalin <pshurgal@users.noreply.github.com>
Co-Authored-By: taloth <taloth@users.noreply.github.com>
This commit is contained in:
Петр Шургалин 2020-01-16 13:38:26 +03:00 committed by Qstick
parent c45cff87fc
commit 1e4bdcc324
12 changed files with 95 additions and 34 deletions

View File

@ -28,6 +28,8 @@ HttpResponse<T> Post<T>(HttpRequest request)
public class HttpClient : IHttpClient public class HttpClient : IHttpClient
{ {
private const int MaxRedirects = 5;
private readonly Logger _logger; private readonly Logger _logger;
private readonly IRateLimitService _rateLimitService; private readonly IRateLimitService _rateLimitService;
private readonly ICached<CookieContainer> _cookieContainerCache; private readonly ICached<CookieContainer> _cookieContainerCache;
@ -70,7 +72,7 @@ public HttpResponse Execute(HttpRequest request)
_logger.Trace("Redirected to {0}", request.Url); _logger.Trace("Redirected to {0}", request.Url);
if (autoRedirectChain.Count > 3) if (autoRedirectChain.Count > MaxRedirects)
{ {
throw new WebException($"Too many automatic redirections were attempted for {autoRedirectChain.Join(" -> ")}", WebExceptionStatus.ProtocolError); throw new WebException($"Too many automatic redirections were attempted for {autoRedirectChain.Join(" -> ")}", WebExceptionStatus.ProtocolError);
} }

View File

@ -3,5 +3,6 @@ namespace NzbDrone.Common.Http.Proxy
public interface IHttpProxySettingsProvider public interface IHttpProxySettingsProvider
{ {
HttpProxySettings GetProxySettings(HttpRequest request); HttpProxySettings GetProxySettings(HttpRequest request);
HttpProxySettings GetProxySettings();
} }
} }

View File

@ -1,4 +1,4 @@
using System; using System;
using System.Net; using System.Net;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Common.Http.Proxy; using NzbDrone.Common.Http.Proxy;
@ -17,19 +17,12 @@ public HttpProxySettingsProvider(IConfigService configService)
public HttpProxySettings GetProxySettings(HttpRequest request) public HttpProxySettings GetProxySettings(HttpRequest request)
{ {
if (!_configService.ProxyEnabled) var proxySettings = GetProxySettings();
if (proxySettings == null)
{ {
return null; return null;
} }
var proxySettings = new HttpProxySettings(_configService.ProxyType,
_configService.ProxyHostname,
_configService.ProxyPort,
_configService.ProxyBypassFilter,
_configService.ProxyBypassLocalAddresses,
_configService.ProxyUsername,
_configService.ProxyPassword);
if (ShouldProxyBeBypassed(proxySettings, request.Url)) if (ShouldProxyBeBypassed(proxySettings, request.Url))
{ {
return null; return null;
@ -38,6 +31,22 @@ public HttpProxySettings GetProxySettings(HttpRequest request)
return proxySettings; return proxySettings;
} }
public HttpProxySettings GetProxySettings()
{
if (!_configService.ProxyEnabled)
{
return null;
}
return new HttpProxySettings(_configService.ProxyType,
_configService.ProxyHostname,
_configService.ProxyPort,
_configService.ProxyBypassFilter,
_configService.ProxyBypassLocalAddresses,
_configService.ProxyUsername,
_configService.ProxyPassword);
}
public bool ShouldProxyBeBypassed(HttpProxySettings proxySettings, HttpUri url) public bool ShouldProxyBeBypassed(HttpProxySettings proxySettings, HttpUri url)
{ {
//We are utilising the WebProxy implementation here to save us having to reimplement it. This way we use Microsofts implementation //We are utilising the WebProxy implementation here to save us having to reimplement it. This way we use Microsofts implementation

View File

@ -17,10 +17,12 @@ public interface IBoxcarProxy
public class BoxcarProxy : IBoxcarProxy public class BoxcarProxy : IBoxcarProxy
{ {
private const string URL = "https://new.boxcar.io/api/notifications"; private const string URL = "https://new.boxcar.io/api/notifications";
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger; private readonly Logger _logger;
public BoxcarProxy(Logger logger) public BoxcarProxy(IRestClientFactory restClientFactory, Logger logger)
{ {
_restClientFactory = restClientFactory;
_logger = logger; _logger = logger;
} }
@ -71,7 +73,7 @@ private void SendNotification(string title, string message, RestRequest request,
{ {
try try
{ {
var client = RestClientFactory.BuildClient(URL); var client = _restClientFactory.BuildClient(URL);
request.AddParameter("user_credentials", settings.Token); request.AddParameter("user_credentials", settings.Token);
request.AddParameter("notification[title]", title); request.AddParameter("notification[title]", title);

View File

@ -10,9 +10,16 @@ public interface IGotifyProxy
public class GotifyProxy : IGotifyProxy public class GotifyProxy : IGotifyProxy
{ {
private readonly IRestClientFactory _restClientFactory;
public GotifyProxy(IRestClientFactory restClientFactory)
{
_restClientFactory = restClientFactory;
}
public void SendNotification(string title, string message, GotifySettings settings) public void SendNotification(string title, string message, GotifySettings settings)
{ {
var client = RestClientFactory.BuildClient(settings.Server); var client = _restClientFactory.BuildClient(settings.Server);
var request = new RestRequest("message", Method.POST); var request = new RestRequest("message", Method.POST);
request.AddQueryParameter("token", settings.AppToken); request.AddQueryParameter("token", settings.AppToken);

View File

@ -17,10 +17,12 @@ public interface IJoinProxy
public class JoinProxy : IJoinProxy public class JoinProxy : IJoinProxy
{ {
private const string URL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?"; private const string URL = "https://joinjoaomgcd.appspot.com/_ah/api/messaging/v1/sendPush?";
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger; private readonly Logger _logger;
public JoinProxy(Logger logger) public JoinProxy(IRestClientFactory restClientFactory, Logger logger)
{ {
_restClientFactory = restClientFactory;
_logger = logger; _logger = logger;
} }
@ -73,7 +75,7 @@ public ValidationFailure Test(JoinSettings settings)
private void SendNotification(string title, string message, RestRequest request, JoinSettings settings) private void SendNotification(string title, string message, RestRequest request, JoinSettings settings)
{ {
var client = RestClientFactory.BuildClient(URL); var client = _restClientFactory.BuildClient(URL);
if (settings.DeviceNames.IsNotNullOrWhiteSpace()) if (settings.DeviceNames.IsNotNullOrWhiteSpace())
{ {

View File

@ -23,10 +23,12 @@ public class PushBulletProxy : IPushBulletProxy
{ {
private const string PUSH_URL = "https://api.pushbullet.com/v2/pushes"; private const string PUSH_URL = "https://api.pushbullet.com/v2/pushes";
private const string DEVICE_URL = "https://api.pushbullet.com/v2/devices"; private const string DEVICE_URL = "https://api.pushbullet.com/v2/devices";
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger; private readonly Logger _logger;
public PushBulletProxy(Logger logger) public PushBulletProxy(IRestClientFactory restClientFactory, Logger logger)
{ {
_restClientFactory = restClientFactory;
_logger = logger; _logger = logger;
} }
@ -96,7 +98,7 @@ public List<PushBulletDevice> GetDevices(PushBulletSettings settings)
{ {
try try
{ {
var client = RestClientFactory.BuildClient(DEVICE_URL); var client = _restClientFactory.BuildClient(DEVICE_URL);
var request = new RestRequest(Method.GET); var request = new RestRequest(Method.GET);
client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty); client.Authenticator = new HttpBasicAuthenticator(settings.ApiKey, string.Empty);
@ -174,7 +176,7 @@ private void SendNotification(string title, string message, RestRequest request,
{ {
try try
{ {
var client = RestClientFactory.BuildClient(PUSH_URL); var client = _restClientFactory.BuildClient(PUSH_URL);
request.AddParameter("type", "note"); request.AddParameter("type", "note");
request.AddParameter("title", title); request.AddParameter("title", title);

View File

@ -1,4 +1,4 @@
using System; using System;
using FluentValidation.Results; using FluentValidation.Results;
using NLog; using NLog;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -16,19 +16,22 @@ public interface IPushoverProxy
public class PushoverProxy : IPushoverProxy public class PushoverProxy : IPushoverProxy
{ {
private const string URL = "https://api.pushover.net/1/messages.json"; private const string URL = "https://api.pushover.net/1/messages.json";
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger; private readonly Logger _logger;
public PushoverProxy(Logger logger) public PushoverProxy(IRestClientFactory restClientFactory, Logger logger)
{ {
_restClientFactory = restClientFactory;
_logger = logger; _logger = logger;
} }
public void SendNotification(string title, string message, PushoverSettings settings) public void SendNotification(string title, string message, PushoverSettings settings)
{ {
var client = RestClientFactory.BuildClient(URL); var client = _restClientFactory.BuildClient(URL);
var request = new RestRequest(Method.POST); var request = new RestRequest(Method.POST);
request.AddParameter("token", settings.ApiKey); request.AddParameter("token", settings.ApiKey);
request.AddParameter("user", settings.UserKey); request.AddParameter("user", settings.UserKey);
request.AddParameter("device", string.Join(",", settings.Devices));
request.AddParameter("title", title); request.AddParameter("title", title);
request.AddParameter("message", message); request.AddParameter("message", message);
request.AddParameter("priority", settings.Priority); request.AddParameter("priority", settings.Priority);
@ -52,13 +55,13 @@ public ValidationFailure Test(PushoverSettings settings)
try try
{ {
const string title = "Test Notification"; const string title = "Test Notification";
const string body = "This is a test message from Radarr"; const string body = "This is a test message from Sonarr";
SendNotification(title, body, settings); SendNotification(title, body, settings);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error(ex, "Unable to send test message: " + ex.Message); _logger.Error(ex, "Unable to send test message");
return new ValidationFailure("ApiKey", "Unable to send test message"); return new ValidationFailure("ApiKey", "Unable to send test message");
} }

View File

@ -1,9 +1,10 @@
using System; using System;
using System.Net; using System.Net;
using System.Web; 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.Proxy;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.Rest; using NzbDrone.Core.Rest;
using RestSharp; using RestSharp;
@ -19,10 +20,12 @@ public interface ITelegramProxy
public class TelegramProxy : ITelegramProxy public class TelegramProxy : ITelegramProxy
{ {
private const string URL = "https://api.telegram.org"; private const string URL = "https://api.telegram.org";
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger; private readonly Logger _logger;
public TelegramProxy(Logger logger) public TelegramProxy(IRestClientFactory restClientFactory, Logger logger)
{ {
_restClientFactory = restClientFactory;
_logger = logger; _logger = logger;
} }
@ -30,7 +33,8 @@ public void SendNotification(string title, string message, TelegramSettings sett
{ {
//Format text to add the title before and bold using markdown //Format text to add the title before and bold using markdown
var text = $"<b>{HttpUtility.HtmlEncode(title)}</b>\n{HttpUtility.HtmlEncode(message)}"; var text = $"<b>{HttpUtility.HtmlEncode(title)}</b>\n{HttpUtility.HtmlEncode(message)}";
var client = RestClientFactory.BuildClient(URL); var client = _restClientFactory.BuildClient(URL);
var request = new RestRequest("bot{token}/sendmessage", Method.POST); var request = new RestRequest("bot{token}/sendmessage", Method.POST);
request.AddUrlSegment("token", settings.BotToken); request.AddUrlSegment("token", settings.BotToken);
@ -54,9 +58,11 @@ public ValidationFailure Test(TelegramSettings settings)
{ {
_logger.Error(ex, "Unable to send test message"); _logger.Error(ex, "Unable to send test message");
var restException = ex as RestException; if (ex is WebException webException)
{
if (restException != null && restException.Response.StatusCode == HttpStatusCode.BadRequest) return new ValidationFailure("Connection", $"{webException.Status.ToString()}: {webException.Message}");
}
else if (ex is RestException restException && restException.Response.StatusCode == HttpStatusCode.BadRequest)
{ {
var error = Json.Deserialize<TelegramError>(restException.Response.Content); var error = Json.Deserialize<TelegramError>(restException.Response.Content);
var property = error.Description.ContainsIgnoreCase("chat not found") ? "ChatId" : "BotToken"; var property = error.Description.ContainsIgnoreCase("chat not found") ? "ChatId" : "BotToken";

View File

@ -21,10 +21,12 @@ public interface IXbmcJsonApiProxy
public class XbmcJsonApiProxy : IXbmcJsonApiProxy public class XbmcJsonApiProxy : IXbmcJsonApiProxy
{ {
private readonly IRestClientFactory _restClientFactory;
private readonly Logger _logger; private readonly Logger _logger;
public XbmcJsonApiProxy(Logger logger) public XbmcJsonApiProxy(IRestClientFactory restClientFactory, Logger logger)
{ {
_restClientFactory = restClientFactory;
_logger = logger; _logger = logger;
} }
@ -110,7 +112,7 @@ private string ProcessRequest(IRestRequest request, XbmcSettings settings, strin
private IRestClient BuildClient(XbmcSettings settings) private IRestClient BuildClient(XbmcSettings settings)
{ {
var url = string.Format(@"http://{0}/jsonrpc", settings.Address); var url = string.Format(@"http://{0}/jsonrpc", settings.Address);
var client = RestClientFactory.BuildClient(url); var client = _restClientFactory.BuildClient(url);
if (!settings.Username.IsNullOrWhiteSpace()) if (!settings.Username.IsNullOrWhiteSpace())
{ {

View File

@ -0,0 +1,9 @@
using RestSharp;
namespace NzbDrone.Core.Rest
{
public interface IRestClientFactory
{
RestClient BuildClient(string baseUrl);
}
}

View File

@ -1,17 +1,33 @@
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Http.Proxy;
using RestSharp; using RestSharp;
namespace NzbDrone.Core.Rest namespace NzbDrone.Core.Rest
{ {
public static class RestClientFactory public class RestClientFactory : IRestClientFactory
{ {
public static RestClient BuildClient(string baseUrl) private readonly IHttpProxySettingsProvider _httpProxySettingsProvider;
private readonly ICreateManagedWebProxy _createManagedWebProxy;
public RestClientFactory(IHttpProxySettingsProvider httpProxySettingsProvider, ICreateManagedWebProxy createManagedWebProxy)
{
_httpProxySettingsProvider = httpProxySettingsProvider;
_createManagedWebProxy = createManagedWebProxy;
}
public RestClient BuildClient(string baseUrl)
{ {
var restClient = new RestClient(baseUrl) var restClient = new RestClient(baseUrl)
{ {
UserAgent = $"{BuildInfo.AppName}/{BuildInfo.Version} ({OsInfo.Os})" UserAgent = $"{BuildInfo.AppName}/{BuildInfo.Version} ({OsInfo.Os})"
}; };
var proxySettings = _httpProxySettingsProvider.GetProxySettings();
if (proxySettings != null)
{
restClient.Proxy = _createManagedWebProxy.GetWebProxy(proxySettings);
}
return restClient; return restClient;
} }
} }