mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
core: refactor http webclient part 15 #8529
Fix Mono 5.x and Automapper bugs
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Security;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CloudflareSolverRe;
|
using CloudflareSolverRe;
|
||||||
@@ -23,11 +26,41 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
|
||||||
|
public static 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();
|
||||||
|
|
||||||
|
|
||||||
|
trustedCertificates.TryGetValue(hash, out var 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;
|
||||||
|
}
|
||||||
|
|
||||||
public override void Init()
|
public override void Init()
|
||||||
{
|
{
|
||||||
ServicePointManager.DefaultConnectionLimit = 1000;
|
ServicePointManager.DefaultConnectionLimit = 1000;
|
||||||
|
|
||||||
base.Init();
|
base.Init();
|
||||||
|
|
||||||
|
// custom handler for our own internal certificates
|
||||||
|
ServicePointManager.ServerCertificateValidationCallback += ValidateCertificate;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override async Task<WebResult> Run(WebRequest webRequest)
|
protected override async Task<WebResult> Run(WebRequest webRequest)
|
||||||
@@ -60,8 +93,6 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
// custom certificate validation handler (netcore version)
|
|
||||||
clientHandlr.ServerCertificateCustomValidationCallback = ValidateCertificate;
|
|
||||||
clearanceHandlr.InnerHandler = clientHandlr;
|
clearanceHandlr.InnerHandler = clientHandlr;
|
||||||
using (var client = new HttpClient(clearanceHandlr))
|
using (var client = new HttpClient(clearanceHandlr))
|
||||||
{
|
{
|
||||||
|
@@ -1,8 +1,11 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Security;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CloudflareSolverRe;
|
using CloudflareSolverRe;
|
||||||
@@ -32,6 +35,29 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
CreateClient();
|
CreateClient();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
|
||||||
|
public static bool ValidateCertificate(HttpRequestMessage request, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||||
|
{
|
||||||
|
var hash = certificate.GetCertHashString();
|
||||||
|
|
||||||
|
|
||||||
|
trustedCertificates.TryGetValue(hash, out var 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;
|
||||||
|
}
|
||||||
|
|
||||||
public void CreateClient()
|
public void CreateClient()
|
||||||
{
|
{
|
||||||
clearanceHandlr = new ClearanceHandler(BrowserUtil.ChromeUserAgent)
|
clearanceHandlr = new ClearanceHandler(BrowserUtil.ChromeUserAgent)
|
||||||
|
@@ -1,16 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
using System.Net.Security;
|
|
||||||
using System.Security.Cryptography.X509Certificates;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Web;
|
using System.Web;
|
||||||
using AutoMapper;
|
|
||||||
using com.LandonKey.SocksWebProxy;
|
using com.LandonKey.SocksWebProxy;
|
||||||
using com.LandonKey.SocksWebProxy.Proxy;
|
using com.LandonKey.SocksWebProxy.Proxy;
|
||||||
using Jackett.Common.Models.Config;
|
using Jackett.Common.Models.Config;
|
||||||
@@ -35,31 +31,6 @@ namespace Jackett.Common.Utils.Clients
|
|||||||
protected static string webProxyUrl;
|
protected static string webProxyUrl;
|
||||||
protected static IWebProxy webProxy;
|
protected static IWebProxy webProxy;
|
||||||
|
|
||||||
|
|
||||||
[DebuggerNonUserCode] // avoid "Exception User-Unhandled" Visual Studio messages
|
|
||||||
public static bool ValidateCertificate(HttpRequestMessage request, X509Certificate2 certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
|
||||||
{
|
|
||||||
{
|
|
||||||
var hash = certificate.GetCertHashString();
|
|
||||||
|
|
||||||
|
|
||||||
trustedCertificates.TryGetValue(hash, out var 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static void InitProxy(ServerConfig serverConfig)
|
public static void InitProxy(ServerConfig serverConfig)
|
||||||
{
|
{
|
||||||
// dispose old SocksWebProxy
|
// dispose old SocksWebProxy
|
||||||
|
@@ -73,6 +73,8 @@ namespace Jackett.Server
|
|||||||
// TODO: fix deprecation warning (remove #pragma to see the build warning)
|
// TODO: fix deprecation warning (remove #pragma to see the build warning)
|
||||||
Mapper.Initialize(cfg =>
|
Mapper.Initialize(cfg =>
|
||||||
{
|
{
|
||||||
|
cfg.CreateMap<WebResult, WebResult>();
|
||||||
|
|
||||||
cfg.CreateMap<ReleaseInfo, ReleaseInfo>();
|
cfg.CreateMap<ReleaseInfo, ReleaseInfo>();
|
||||||
|
|
||||||
cfg.CreateMap<ReleaseInfo, TrackerCacheResult>().AfterMap((r, t) =>
|
cfg.CreateMap<ReleaseInfo, TrackerCacheResult>().AfterMap((r, t) =>
|
||||||
|
Reference in New Issue
Block a user