mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
HTTPClients: avoid Exception User-Unhandled notices
This commit is contained in:
@@ -14,6 +14,7 @@ using Jackett.Common.Models.Config;
|
|||||||
using Jackett.Common.Services.Interfaces;
|
using Jackett.Common.Services.Interfaces;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Jackett.Common.Helpers;
|
using Jackett.Common.Helpers;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Jackett.Common.Utils.Clients
|
namespace Jackett.Common.Utils.Clients
|
||||||
{
|
{
|
||||||
@@ -23,6 +24,34 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
static protected string webProxyUrl;
|
static protected string webProxyUrl;
|
||||||
static protected IWebProxy webProxy;
|
static protected IWebProxy webProxy;
|
||||||
|
|
||||||
|
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
|
||||||
|
static public bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
|
{
|
||||||
|
if (sender.GetType() != typeof(HttpWebRequest))
|
||||||
|
return sslPolicyErrors == SslPolicyErrors.None;
|
||||||
|
|
||||||
|
var request = (HttpWebRequest)sender;
|
||||||
|
var hash = certificate.GetCertHashString();
|
||||||
|
|
||||||
|
ICollection<string> hosts;
|
||||||
|
|
||||||
|
trustedCertificates.TryGetValue(hash, out hosts);
|
||||||
|
if (hosts != null)
|
||||||
|
{
|
||||||
|
if (hosts.Contains(request.Host))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sslPolicyErrors != SslPolicyErrors.None)
|
||||||
|
{
|
||||||
|
// Throw exception with certificate details, this will cause a "Exception User-Unhandled" when running it in the Visual Studio debugger.
|
||||||
|
// The certificate is only available inside this function, so we can't catch it at the calling method.
|
||||||
|
throw new Exception("certificate validation failed: " + certificate.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return sslPolicyErrors == SslPolicyErrors.None;
|
||||||
|
}
|
||||||
|
|
||||||
static public void InitProxy(ServerConfig serverConfig)
|
static public void InitProxy(ServerConfig serverConfig)
|
||||||
{
|
{
|
||||||
// dispose old SocksWebProxy
|
// dispose old SocksWebProxy
|
||||||
@@ -100,30 +129,7 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
}
|
}
|
||||||
|
|
||||||
// custom handler for our own internal certificates
|
// custom handler for our own internal certificates
|
||||||
ServicePointManager.ServerCertificateValidationCallback += delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
ServicePointManager.ServerCertificateValidationCallback += ValidateCertificate;
|
||||||
{
|
|
||||||
if (sender.GetType() != typeof(HttpWebRequest))
|
|
||||||
return sslPolicyErrors == SslPolicyErrors.None;
|
|
||||||
|
|
||||||
var request = (HttpWebRequest)sender;
|
|
||||||
var hash = certificate.GetCertHashString();
|
|
||||||
|
|
||||||
ICollection<string> hosts;
|
|
||||||
|
|
||||||
trustedCertificates.TryGetValue(hash, out hosts);
|
|
||||||
if (hosts != null)
|
|
||||||
{
|
|
||||||
if (hosts.Contains(request.Host))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sslPolicyErrors != SslPolicyErrors.None)
|
|
||||||
{
|
|
||||||
throw new Exception("certificate validation failed: " + certificate.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return sslPolicyErrors == SslPolicyErrors.None;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override protected async Task<WebClientByteResult> Run(WebRequest webRequest)
|
override protected async Task<WebClientByteResult> Run(WebRequest webRequest)
|
||||||
|
@@ -14,6 +14,7 @@ using Jackett.Common.Models.Config;
|
|||||||
using Jackett.Common.Services.Interfaces;
|
using Jackett.Common.Services.Interfaces;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Jackett.Common.Helpers;
|
using Jackett.Common.Helpers;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Jackett.Common.Utils.Clients
|
namespace Jackett.Common.Utils.Clients
|
||||||
{
|
{
|
||||||
@@ -30,6 +31,34 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
static protected string webProxyUrl;
|
static protected string webProxyUrl;
|
||||||
static protected IWebProxy webProxy;
|
static protected IWebProxy webProxy;
|
||||||
|
|
||||||
|
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
|
||||||
|
static public bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
|
{
|
||||||
|
if (sender.GetType() != typeof(HttpWebRequest))
|
||||||
|
return sslPolicyErrors == SslPolicyErrors.None;
|
||||||
|
|
||||||
|
var request = (HttpWebRequest)sender;
|
||||||
|
var hash = certificate.GetCertHashString();
|
||||||
|
|
||||||
|
ICollection<string> hosts;
|
||||||
|
|
||||||
|
trustedCertificates.TryGetValue(hash, out hosts);
|
||||||
|
if (hosts != null)
|
||||||
|
{
|
||||||
|
if (hosts.Contains(request.Host))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sslPolicyErrors != SslPolicyErrors.None)
|
||||||
|
{
|
||||||
|
// Throw exception with certificate details, this will cause a "Exception User-Unhandled" when running it in the Visual Studio debugger.
|
||||||
|
// The certificate is only available inside this function, so we can't catch it at the calling method.
|
||||||
|
throw new Exception("certificate validation failed: " + certificate.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return sslPolicyErrors == SslPolicyErrors.None;
|
||||||
|
}
|
||||||
|
|
||||||
static public void InitProxy(ServerConfig serverConfig)
|
static public void InitProxy(ServerConfig serverConfig)
|
||||||
{
|
{
|
||||||
// dispose old SocksWebProxy
|
// dispose old SocksWebProxy
|
||||||
@@ -134,30 +163,7 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
|
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
|
||||||
|
|
||||||
// custom handler for our own internal certificates
|
// custom handler for our own internal certificates
|
||||||
ServicePointManager.ServerCertificateValidationCallback += delegate (object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
ServicePointManager.ServerCertificateValidationCallback += ValidateCertificate;
|
||||||
{
|
|
||||||
if (sender.GetType() != typeof(HttpWebRequest))
|
|
||||||
return sslPolicyErrors == SslPolicyErrors.None;
|
|
||||||
|
|
||||||
var request = (HttpWebRequest)sender;
|
|
||||||
var hash = certificate.GetCertHashString();
|
|
||||||
|
|
||||||
ICollection<string> hosts;
|
|
||||||
|
|
||||||
trustedCertificates.TryGetValue(hash, out hosts);
|
|
||||||
if (hosts != null)
|
|
||||||
{
|
|
||||||
if (hosts.Contains(request.Host))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sslPolicyErrors != SslPolicyErrors.None)
|
|
||||||
{
|
|
||||||
throw new Exception("certificate validation failed: " + certificate.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return sslPolicyErrors == SslPolicyErrors.None;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override protected async Task<WebClientByteResult> Run(WebRequest webRequest)
|
override protected async Task<WebClientByteResult> Run(WebRequest webRequest)
|
||||||
|
@@ -14,6 +14,7 @@ using Jackett.Common.Models.Config;
|
|||||||
using Jackett.Common.Services.Interfaces;
|
using Jackett.Common.Services.Interfaces;
|
||||||
using NLog;
|
using NLog;
|
||||||
using Jackett.Common.Helpers;
|
using Jackett.Common.Helpers;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
namespace Jackett.Common.Utils.Clients
|
namespace Jackett.Common.Utils.Clients
|
||||||
{
|
{
|
||||||
@@ -24,6 +25,32 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
static protected string webProxyUrl;
|
static protected string webProxyUrl;
|
||||||
static protected IWebProxy webProxy;
|
static protected IWebProxy webProxy;
|
||||||
|
|
||||||
|
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
|
||||||
|
static public bool ValidateCertificate(HttpRequestMessage request, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
|
{
|
||||||
|
{
|
||||||
|
var hash = certificate.GetCertHashString();
|
||||||
|
|
||||||
|
ICollection<string> hosts;
|
||||||
|
|
||||||
|
trustedCertificates.TryGetValue(hash, out hosts);
|
||||||
|
if (hosts != null)
|
||||||
|
{
|
||||||
|
if (hosts.Contains(request.RequestUri.Host))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sslPolicyErrors != SslPolicyErrors.None)
|
||||||
|
{
|
||||||
|
// Throw exception with certificate details, this will cause a "Exception User-Unhandled" when running it in the Visual Studio debugger.
|
||||||
|
// The certificate is only available inside this function, so we can't catch it at the calling method.
|
||||||
|
throw new Exception("certificate validation failed: " + certificate.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return sslPolicyErrors == SslPolicyErrors.None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static public void InitProxy(ServerConfig serverConfig)
|
static public void InitProxy(ServerConfig serverConfig)
|
||||||
{
|
{
|
||||||
// dispose old SocksWebProxy
|
// dispose old SocksWebProxy
|
||||||
@@ -137,26 +164,7 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
})
|
})
|
||||||
{
|
{
|
||||||
// custom certificate validation handler (netcore version)
|
// custom certificate validation handler (netcore version)
|
||||||
clientHandlr.ServerCertificateCustomValidationCallback = (request, certificate, chain, sslPolicyErrors) =>
|
clientHandlr.ServerCertificateCustomValidationCallback = ValidateCertificate;
|
||||||
{
|
|
||||||
var hash = certificate.GetCertHashString();
|
|
||||||
|
|
||||||
ICollection<string> hosts;
|
|
||||||
|
|
||||||
trustedCertificates.TryGetValue(hash, out hosts);
|
|
||||||
if (hosts != null)
|
|
||||||
{
|
|
||||||
if (hosts.Contains(request.RequestUri.Host))
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sslPolicyErrors != SslPolicyErrors.None)
|
|
||||||
{
|
|
||||||
throw new Exception("certificate validation failed: " + certificate.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
return sslPolicyErrors == SslPolicyErrors.None;
|
|
||||||
};
|
|
||||||
|
|
||||||
clearanceHandlr.InnerHandler = clientHandlr;
|
clearanceHandlr.InnerHandler = clientHandlr;
|
||||||
using (var client = new HttpClient(clearanceHandlr))
|
using (var client = new HttpClient(clearanceHandlr))
|
||||||
|
Reference in New Issue
Block a user