From e8a3c5eb842cf090716f1799222d32760be99af5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Thu, 22 Jul 2021 09:36:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=88=B6=E4=BD=BF=E7=94=A8=E8=87=AA?= =?UTF-8?q?=E8=BA=AB=E5=8F=8D=E5=90=91=E4=BB=A3=E7=90=86=E8=BF=9B=E8=A1=8C?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Upgrade/FastGithub.Upgrade.csproj | 16 ++++--- FastGithub.Upgrade/ReverseProxyHttpHandler.cs | 43 +++++++++++++++++++ FastGithub.Upgrade/UpgradeHostedService.cs | 2 +- FastGithub.Upgrade/UpgradeService.cs | 5 ++- 4 files changed, 58 insertions(+), 8 deletions(-) create mode 100644 FastGithub.Upgrade/ReverseProxyHttpHandler.cs diff --git a/FastGithub.Upgrade/FastGithub.Upgrade.csproj b/FastGithub.Upgrade/FastGithub.Upgrade.csproj index ddaba9e..a870eac 100644 --- a/FastGithub.Upgrade/FastGithub.Upgrade.csproj +++ b/FastGithub.Upgrade/FastGithub.Upgrade.csproj @@ -1,11 +1,15 @@  - - net5.0 - + + net5.0 + - - - + + + + + + + diff --git a/FastGithub.Upgrade/ReverseProxyHttpHandler.cs b/FastGithub.Upgrade/ReverseProxyHttpHandler.cs new file mode 100644 index 0000000..48d43ce --- /dev/null +++ b/FastGithub.Upgrade/ReverseProxyHttpHandler.cs @@ -0,0 +1,43 @@ +using System; +using System.Net; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace FastGithub.Upgrade +{ + /// + /// 本机反向代理的httpHandler + /// + sealed class ReverseProxyHttpHandler : DelegatingHandler + { + /// + /// 本机反向代理的httpHandler + /// + public ReverseProxyHttpHandler() + { + this.InnerHandler = new HttpClientHandler(); + } + + /// + /// 替换为Loopback + /// + /// + /// + /// + protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) + { + var uri = request.RequestUri; + if (uri != null && uri.HostNameType == UriHostNameType.Dns) + { + var domain = uri.Host; + var builder = new UriBuilder(uri) { Host = IPAddress.Loopback.ToString() }; + + request.RequestUri = builder.Uri; + request.Headers.Host = domain; + } + + return await base.SendAsync(request, cancellationToken); + } + } +} diff --git a/FastGithub.Upgrade/UpgradeHostedService.cs b/FastGithub.Upgrade/UpgradeHostedService.cs index 8c9e1ce..ad97f32 100644 --- a/FastGithub.Upgrade/UpgradeHostedService.cs +++ b/FastGithub.Upgrade/UpgradeHostedService.cs @@ -33,7 +33,7 @@ namespace FastGithub.Upgrade /// protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - var maxTryCount = 3; + var maxTryCount = 5; for (var i = 1; i <= maxTryCount; i++) { try diff --git a/FastGithub.Upgrade/UpgradeService.cs b/FastGithub.Upgrade/UpgradeService.cs index 07a507d..94c54b3 100644 --- a/FastGithub.Upgrade/UpgradeService.cs +++ b/FastGithub.Upgrade/UpgradeService.cs @@ -59,7 +59,10 @@ namespace FastGithub.Upgrade /// public async Task GetLastedReleaseAsync(CancellationToken cancellationToken) { - using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(30d) }; + using var httpClient = new HttpClient(new ReverseProxyHttpHandler()) + { + Timeout = TimeSpan.FromSeconds(30d), + }; httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(nameof(FastGithub), "1.0"));