From b6b449644b8c985361ef4e40f7d08c9b3ac6b4b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Wed, 21 Jul 2021 09:56:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8HttpClientFactory=E5=8F=96?= =?UTF-8?q?=E4=BB=A3HttpClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.ReverseProxy/HttpClient.cs | 50 ------------- FastGithub.ReverseProxy/HttpClientFactory.cs | 73 +++++++++++++++++++ .../ReverseProxyMiddleware.cs | 10 +-- ...ReverseProxyServiceCollectionExtensions.cs | 2 +- FastGithub/appsettings.json | 3 +- 5 files changed, 80 insertions(+), 58 deletions(-) delete mode 100644 FastGithub.ReverseProxy/HttpClient.cs create mode 100644 FastGithub.ReverseProxy/HttpClientFactory.cs diff --git a/FastGithub.ReverseProxy/HttpClient.cs b/FastGithub.ReverseProxy/HttpClient.cs deleted file mode 100644 index e2d141f..0000000 --- a/FastGithub.ReverseProxy/HttpClient.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; - -namespace FastGithub.ReverseProxy -{ - /// - /// YARP的HttpClient - /// - class HttpClient : HttpMessageInvoker - { - private readonly TlsSniPattern tlsSniPattern; - private readonly bool tlsIgnoreNameMismatch; - - /// - /// YARP的HttpClient - /// - /// - /// - /// - public HttpClient( - HttpMessageHandler handler, - TlsSniPattern tlsSniPattern, - bool tlsIgnoreNameMismatch, - bool disposeHandler = false) : base(handler, disposeHandler) - { - this.tlsSniPattern = tlsSniPattern; - this.tlsIgnoreNameMismatch = tlsIgnoreNameMismatch; - } - - /// - /// 发送数据 - /// - /// - /// - /// - public override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) - { - request.SetRequestContext(new RequestContext - { - Host = request.RequestUri?.Host, - IsHttps = request.RequestUri?.Scheme == Uri.UriSchemeHttps, - TlsSniPattern = this.tlsSniPattern, - TlsIgnoreNameMismatch = this.tlsIgnoreNameMismatch - }); - return base.SendAsync(request, cancellationToken); - } - } -} diff --git a/FastGithub.ReverseProxy/HttpClientFactory.cs b/FastGithub.ReverseProxy/HttpClientFactory.cs new file mode 100644 index 0000000..57b66d7 --- /dev/null +++ b/FastGithub.ReverseProxy/HttpClientFactory.cs @@ -0,0 +1,73 @@ +using Microsoft.Extensions.Options; +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace FastGithub.ReverseProxy +{ + /// + /// HttpClient工厂 + /// + sealed class HttpClientFactory + { + private HttpClientHanlder httpClientHanlder; + + /// + /// HttpClient工厂 + /// + /// + /// + public HttpClientFactory( + DomainResolver domainResolver, + IOptionsMonitor options) + { + this.httpClientHanlder = new HttpClientHanlder(domainResolver); + options.OnChange(opt => this.httpClientHanlder = new HttpClientHanlder(domainResolver)); + } + + /// + /// 创建httpClient + /// + /// + /// + public HttpMessageInvoker CreateHttpClient(DomainConfig domainConfig) + { + return new HttpClient(this.httpClientHanlder, domainConfig, disposeHandler: false); + } + + /// + /// http客户端 + /// + private class HttpClient : HttpMessageInvoker + { + private readonly DomainConfig domainConfig; + + public HttpClient( + HttpMessageHandler handler, + DomainConfig domainConfig, + bool disposeHandler = false) : base(handler, disposeHandler) + { + this.domainConfig = domainConfig; + } + + /// + /// 发送数据 + /// + /// + /// + /// + public override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + request.SetRequestContext(new RequestContext + { + Host = request.RequestUri?.Host, + IsHttps = request.RequestUri?.Scheme == Uri.UriSchemeHttps, + TlsSniPattern = this.domainConfig.GetTlsSniPattern(), + TlsIgnoreNameMismatch = this.domainConfig.TlsIgnoreNameMismatch + }); + return base.SendAsync(request, cancellationToken); + } + } + } +} diff --git a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs index 934a995..7b28119 100644 --- a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs +++ b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs @@ -12,18 +12,18 @@ namespace FastGithub.ReverseProxy sealed class ReverseProxyMiddleware { private readonly IHttpForwarder httpForwarder; - private readonly HttpClientHanlder httpClientHanlder; + private readonly HttpClientFactory httpClientFactory; private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; public ReverseProxyMiddleware( IHttpForwarder httpForwarder, - HttpClientHanlder httpClientHanlder, + HttpClientFactory httpClientFactory, FastGithubConfig fastGithubConfig, ILogger logger) { this.httpForwarder = httpForwarder; - this.httpClientHanlder = httpClientHanlder; + this.httpClientFactory = httpClientFactory; this.fastGithubConfig = fastGithubConfig; this.logger = logger; } @@ -54,11 +54,9 @@ namespace FastGithub.ReverseProxy else { var destinationPrefix = GetDestinationPrefix(host, domainConfig.Destination); + var httpClient = this.httpClientFactory.CreateHttpClient(domainConfig); var requestConfig = new ForwarderRequestConfig { Timeout = domainConfig.Timeout }; - var tlsSniPattern = domainConfig.GetTlsSniPattern(); - using var httpClient = new HttpClient(this.httpClientHanlder, tlsSniPattern, domainConfig.TlsIgnoreNameMismatch); - var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig); await HandleErrorAsync(context, error); } diff --git a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs index abac1bd..3ec02e4 100644 --- a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs +++ b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs @@ -19,7 +19,7 @@ namespace FastGithub .AddMemoryCache() .AddHttpForwarder() .AddSingleton() - .AddTransient() + .AddTransient() .AddSingleton() .AddSingleton(); } diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index fda2e06..9ae34f7 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -40,7 +40,8 @@ "TlsSni": false }, "*github*.s3.amazonaws.com": { - "TlsSni": false + "TlsSni": false, + "TlsIgnoreNameMismatch": true }, "ajax.googleapis.com": { "TlsSni": true,