core: refactor http webclient part 5 #8529 (#7661)

Update standard clients to X509Certificate2
This commit is contained in:
Cory 2020-03-15 12:38:03 -05:00 committed by ngosang
parent c605c9a7ae
commit 4a11a770d7
2 changed files with 9 additions and 17 deletions

View File

@ -26,19 +26,15 @@ namespace Jackett.Common.Utils.Clients
protected static IWebProxy webProxy;
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
public static bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
public static bool ValidateCertificate(HttpRequestMessage request, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sender.GetType() != typeof(HttpWebRequest))
return sslPolicyErrors == SslPolicyErrors.None;
var request = (HttpWebRequest)sender;
var hash = certificate.GetCertHashString();
trustedCertificates.TryGetValue(hash, out var hosts);
if (hosts != null)
{
if (hosts.Contains(request.Host))
if (hosts.Contains(request.RequestUri.Host))
return true;
}
@ -125,8 +121,6 @@ namespace Jackett.Common.Utils.Clients
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };
}
// custom handler for our own internal certificates
ServicePointManager.ServerCertificateValidationCallback += ValidateCertificate;
}
protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
@ -159,6 +153,8 @@ namespace Jackett.Common.Utils.Clients
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
})
{
// custom certificate validation handler (netcore version)
clientHandlr.ServerCertificateCustomValidationCallback = ValidateCertificate;
clearanceHandlr.InnerHandler = clientHandlr;
using (var client = new HttpClient(clearanceHandlr))
{

View File

@ -33,19 +33,15 @@ namespace Jackett.Common.Utils.Clients
protected static IWebProxy webProxy;
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
public static bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
public static bool ValidateCertificate(HttpRequestMessage request, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sender.GetType() != typeof(HttpWebRequest))
return sslPolicyErrors == SslPolicyErrors.None;
var request = (HttpWebRequest)sender;
var hash = certificate.GetCertHashString();
trustedCertificates.TryGetValue(hash, out var hosts);
if (hosts != null)
{
if (hosts.Contains(request.Host))
if (hosts.Contains(request.RequestUri.Host))
return true;
}
@ -133,6 +129,9 @@ namespace Jackett.Common.Utils.Clients
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
// custom certificate validation handler (netcore version)
clientHandlr.ServerCertificateCustomValidationCallback = ValidateCertificate;
clearanceHandlr.InnerHandler = clientHandlr;
client = new HttpClient(clearanceHandlr);
}
@ -160,9 +159,6 @@ namespace Jackett.Common.Utils.Clients
}
ServicePointManager.SecurityProtocol = (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
// custom handler for our own internal certificates
ServicePointManager.ServerCertificateValidationCallback += ValidateCertificate;
}
protected override async Task<WebClientByteResult> Run(WebRequest webRequest)