Skip to content

Commit

Permalink
core: refactor http webclient part 5 #8529 (#7661)
Browse files Browse the repository at this point in the history
Update standard clients to X509Certificate2
  • Loading branch information
cadatoiva authored and ngosang committed Sep 21, 2020
1 parent 4dd6da4 commit 30965c0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
12 changes: 4 additions & 8 deletions src/Jackett.Common/Utils/Clients/HttpWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ public class HttpWebClient : WebClient
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;
}

Expand Down Expand Up @@ -125,8 +121,6 @@ public override void Init()
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)
Expand Down Expand Up @@ -159,6 +153,8 @@ protected override async Task<WebClientByteResult> Run(WebRequest webRequest)
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
})
{
// custom certificate validation handler (netcore version)
clientHandlr.ServerCertificateCustomValidationCallback = ValidateCertificate;
clearanceHandlr.InnerHandler = clientHandlr;
using (var client = new HttpClient(clearanceHandlr))
{
Expand Down
14 changes: 5 additions & 9 deletions src/Jackett.Common/Utils/Clients/HttpWebClient2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,15 @@ public class HttpWebClient2 : WebClient
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;
}

Expand Down Expand Up @@ -133,6 +129,9 @@ public void CreateClient()
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

// custom certificate validation handler (netcore version)
clientHandlr.ServerCertificateCustomValidationCallback = ValidateCertificate;

clearanceHandlr.InnerHandler = clientHandlr;
client = new HttpClient(clearanceHandlr);
}
Expand Down Expand Up @@ -160,9 +159,6 @@ public override void Init()
}

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)
Expand Down

0 comments on commit 30965c0

Please sign in to comment.