From f220613178c9bf7645c09bbcb6fdaeec002a50ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Mon, 19 Jul 2021 14:57:58 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=B7=E6=B1=82=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.ReverseProxy/DomainResolver.cs | 8 ++- FastGithub.ReverseProxy/HttpClientHanlder.cs | 20 +------ .../KestrelServerOptionsExtensions.cs | 9 ++- .../RequestLoggingMilldeware.cs | 58 +++++++++++++++++++ ...everseProxyApplicationBuilderExtensions.cs | 11 ++++ ...ReverseProxyServiceCollectionExtensions.cs | 1 + FastGithub/Program.cs | 6 +- 7 files changed, 92 insertions(+), 21 deletions(-) create mode 100644 FastGithub.ReverseProxy/RequestLoggingMilldeware.cs diff --git a/FastGithub.ReverseProxy/DomainResolver.cs b/FastGithub.ReverseProxy/DomainResolver.cs index 49de55e..047d5ed 100644 --- a/FastGithub.ReverseProxy/DomainResolver.cs +++ b/FastGithub.ReverseProxy/DomainResolver.cs @@ -1,5 +1,6 @@ using DNS.Client; using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Net; @@ -15,6 +16,7 @@ namespace FastGithub.ReverseProxy { private readonly IMemoryCache memoryCache; private readonly FastGithubConfig fastGithubConfig; + private readonly ILogger logger; private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d); /// @@ -24,10 +26,12 @@ namespace FastGithub.ReverseProxy /// public DomainResolver( IMemoryCache memoryCache, - FastGithubConfig fastGithubConfig) + FastGithubConfig fastGithubConfig, + ILogger logger) { this.memoryCache = memoryCache; this.fastGithubConfig = fastGithubConfig; + this.logger = logger; } /// @@ -74,6 +78,8 @@ namespace FastGithub.ReverseProxy { throw new FastGithubException($"dns({dns})被污染:解析{domain}为{address}"); } + + this.logger.LogInformation($"[{domain}->{address}]"); return address; } catch (FastGithubException) diff --git a/FastGithub.ReverseProxy/HttpClientHanlder.cs b/FastGithub.ReverseProxy/HttpClientHanlder.cs index 1be2925..7063332 100644 --- a/FastGithub.ReverseProxy/HttpClientHanlder.cs +++ b/FastGithub.ReverseProxy/HttpClientHanlder.cs @@ -1,5 +1,4 @@ -using Microsoft.Extensions.Logging; -using System; +using System; using System.Net.Http; using System.Net.Security; using System.Net.Sockets; @@ -14,18 +13,14 @@ namespace FastGithub.ReverseProxy class HttpClientHanlder : DelegatingHandler { private readonly DomainResolver domainResolver; - private readonly ILogger logger; /// /// YARP的HttpClientHandler /// /// - public HttpClientHanlder( - DomainResolver domainResolver, - ILogger logger) + public HttpClientHanlder(DomainResolver domainResolver) { this.domainResolver = domainResolver; - this.logger = logger; this.InnerHandler = CreateSocketsHttpHandler(); } @@ -62,7 +57,6 @@ namespace FastGithub.ReverseProxy }; } - /// /// 替换域名为ip /// @@ -82,16 +76,6 @@ namespace FastGithub.ReverseProxy }; request.RequestUri = builder.Uri; request.Headers.Host = uri.Host; - - var context = request.GetSniContext(); - if (context.IsHttps && context.TlsSniValue.Length > 0) - { - this.logger.LogInformation($"[{address}--Sni->{uri.Host}]"); - } - else - { - this.logger.LogInformation($"[{address}--NoSni->{uri.Host}]"); - } } return await base.SendAsync(request, cancellationToken); } diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs index 0e5fb63..67a4c5e 100644 --- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs +++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs @@ -92,7 +92,14 @@ namespace FastGithub } catch (Exception) { - logger.LogWarning($"安装根证书{caPublicCerPath}失败:请手动安装到“将所有的证书都放入下载存储”\\“受信任的根证书颁发机构”"); + if (OperatingSystem.IsWindows()) + { + logger.LogWarning($"安装根证书{caPublicCerPath}失败:请手动安装到“将所有的证书都放入下载存储”\\“受信任的根证书颁发机构”"); + } + else + { + logger.LogWarning($"安装根证书{caPublicCerPath}失败:请根据你的系统平台要求安装和信任根证书"); + } } } diff --git a/FastGithub.ReverseProxy/RequestLoggingMilldeware.cs b/FastGithub.ReverseProxy/RequestLoggingMilldeware.cs new file mode 100644 index 0000000..3a454eb --- /dev/null +++ b/FastGithub.ReverseProxy/RequestLoggingMilldeware.cs @@ -0,0 +1,58 @@ +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Logging; +using System.Diagnostics; +using System.Threading.Tasks; + +namespace FastGithub.ReverseProxy +{ + /// + /// 请求日志中间件 + /// + sealed class RequestLoggingMilldeware + { + private readonly ILogger logger; + + /// + /// 请求日志中间件 + /// + /// + public RequestLoggingMilldeware(ILogger logger) + { + this.logger = logger; + } + + /// + /// 执行请求 + /// + /// + /// + /// + public async Task InvokeAsync(HttpContext context, RequestDelegate next) + { + var stopwatch = new Stopwatch(); + stopwatch.Start(); + + try + { + await next(context); + } + finally + { + stopwatch.Stop(); + } + + var request = context.Request; + var response = context.Response; + var message = $"{request.Method} {request.Scheme}://{request.Host}{request.Path} responded {response.StatusCode} in {stopwatch.Elapsed.TotalMilliseconds} ms"; + + if (500 <= response.StatusCode && response.StatusCode <= 599) + { + this.logger.LogError(message); + } + else + { + this.logger.LogInformation(message); + } + } + } +} diff --git a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs index 3ab048c..d785172 100644 --- a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs +++ b/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs @@ -9,6 +9,17 @@ namespace FastGithub /// public static class ReverseProxyApplicationBuilderExtensions { + /// + /// 使用请求日志中间件 + /// + /// + /// + public static IApplicationBuilder UseRequestLogging(this IApplicationBuilder app) + { + var middlware = app.ApplicationServices.GetRequiredService(); + return app.Use(next => context => middlware.InvokeAsync(context, next)); + } + /// /// 使用https反向代理中间件 /// diff --git a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs index 15d6d11..abac1bd 100644 --- a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs +++ b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs @@ -20,6 +20,7 @@ namespace FastGithub .AddHttpForwarder() .AddSingleton() .AddTransient() + .AddSingleton() .AddSingleton(); } } diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 45124fc..44ed162 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -41,7 +41,11 @@ namespace FastGithub }) .ConfigureWebHostDefaults(web => { - web.Configure(app => app.UseHttpsReverseProxy("README.html")); + web.Configure(app => + { + app.UseRequestLogging(); + app.UseHttpsReverseProxy("README.html"); + }); web.UseKestrel(kestrel => kestrel.ListenHttpsReverseProxy()); }); }