增加请求日志中间件
This commit is contained in:
parent
0876c6b35d
commit
f220613178
@ -1,5 +1,6 @@
|
|||||||
using DNS.Client;
|
using DNS.Client;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@ -15,6 +16,7 @@ namespace FastGithub.ReverseProxy
|
|||||||
{
|
{
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
|
private readonly ILogger<DomainResolver> logger;
|
||||||
private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d);
|
private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -24,10 +26,12 @@ namespace FastGithub.ReverseProxy
|
|||||||
/// <param name="fastGithubConfig"></param>
|
/// <param name="fastGithubConfig"></param>
|
||||||
public DomainResolver(
|
public DomainResolver(
|
||||||
IMemoryCache memoryCache,
|
IMemoryCache memoryCache,
|
||||||
FastGithubConfig fastGithubConfig)
|
FastGithubConfig fastGithubConfig,
|
||||||
|
ILogger<DomainResolver> logger)
|
||||||
{
|
{
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.fastGithubConfig = fastGithubConfig;
|
this.fastGithubConfig = fastGithubConfig;
|
||||||
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -74,6 +78,8 @@ namespace FastGithub.ReverseProxy
|
|||||||
{
|
{
|
||||||
throw new FastGithubException($"dns({dns})被污染:解析{domain}为{address}");
|
throw new FastGithubException($"dns({dns})被污染:解析{domain}为{address}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger.LogInformation($"[{domain}->{address}]");
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
catch (FastGithubException)
|
catch (FastGithubException)
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Net.Security;
|
using System.Net.Security;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
@ -14,18 +13,14 @@ namespace FastGithub.ReverseProxy
|
|||||||
class HttpClientHanlder : DelegatingHandler
|
class HttpClientHanlder : DelegatingHandler
|
||||||
{
|
{
|
||||||
private readonly DomainResolver domainResolver;
|
private readonly DomainResolver domainResolver;
|
||||||
private readonly ILogger<HttpClientHanlder> logger;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// YARP的HttpClientHandler
|
/// YARP的HttpClientHandler
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="domainResolver"></param>
|
/// <param name="domainResolver"></param>
|
||||||
public HttpClientHanlder(
|
public HttpClientHanlder(DomainResolver domainResolver)
|
||||||
DomainResolver domainResolver,
|
|
||||||
ILogger<HttpClientHanlder> logger)
|
|
||||||
{
|
{
|
||||||
this.domainResolver = domainResolver;
|
this.domainResolver = domainResolver;
|
||||||
this.logger = logger;
|
|
||||||
this.InnerHandler = CreateSocketsHttpHandler();
|
this.InnerHandler = CreateSocketsHttpHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +57,6 @@ namespace FastGithub.ReverseProxy
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 替换域名为ip
|
/// 替换域名为ip
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -82,16 +76,6 @@ namespace FastGithub.ReverseProxy
|
|||||||
};
|
};
|
||||||
request.RequestUri = builder.Uri;
|
request.RequestUri = builder.Uri;
|
||||||
request.Headers.Host = uri.Host;
|
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);
|
return await base.SendAsync(request, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -92,7 +92,14 @@ namespace FastGithub
|
|||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
logger.LogWarning($"安装根证书{caPublicCerPath}失败:请手动安装到“将所有的证书都放入下载存储”\\“受信任的根证书颁发机构”");
|
if (OperatingSystem.IsWindows())
|
||||||
|
{
|
||||||
|
logger.LogWarning($"安装根证书{caPublicCerPath}失败:请手动安装到“将所有的证书都放入下载存储”\\“受信任的根证书颁发机构”");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
logger.LogWarning($"安装根证书{caPublicCerPath}失败:请根据你的系统平台要求安装和信任根证书");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
58
FastGithub.ReverseProxy/RequestLoggingMilldeware.cs
Normal file
58
FastGithub.ReverseProxy/RequestLoggingMilldeware.cs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FastGithub.ReverseProxy
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 请求日志中间件
|
||||||
|
/// </summary>
|
||||||
|
sealed class RequestLoggingMilldeware
|
||||||
|
{
|
||||||
|
private readonly ILogger<RequestLoggingMilldeware> logger;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 请求日志中间件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
public RequestLoggingMilldeware(ILogger<RequestLoggingMilldeware> logger)
|
||||||
|
{
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 执行请求
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <param name="next"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,6 +9,17 @@ namespace FastGithub
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class ReverseProxyApplicationBuilderExtensions
|
public static class ReverseProxyApplicationBuilderExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 使用请求日志中间件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="app"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static IApplicationBuilder UseRequestLogging(this IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
var middlware = app.ApplicationServices.GetRequiredService<RequestLoggingMilldeware>();
|
||||||
|
return app.Use(next => context => middlware.InvokeAsync(context, next));
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 使用https反向代理中间件
|
/// 使用https反向代理中间件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -20,6 +20,7 @@ namespace FastGithub
|
|||||||
.AddHttpForwarder()
|
.AddHttpForwarder()
|
||||||
.AddSingleton<DomainResolver>()
|
.AddSingleton<DomainResolver>()
|
||||||
.AddTransient<HttpClientHanlder>()
|
.AddTransient<HttpClientHanlder>()
|
||||||
|
.AddSingleton<RequestLoggingMilldeware>()
|
||||||
.AddSingleton<ReverseProxyMiddleware>();
|
.AddSingleton<ReverseProxyMiddleware>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,7 +41,11 @@ namespace FastGithub
|
|||||||
})
|
})
|
||||||
.ConfigureWebHostDefaults(web =>
|
.ConfigureWebHostDefaults(web =>
|
||||||
{
|
{
|
||||||
web.Configure(app => app.UseHttpsReverseProxy("README.html"));
|
web.Configure(app =>
|
||||||
|
{
|
||||||
|
app.UseRequestLogging();
|
||||||
|
app.UseHttpsReverseProxy("README.html");
|
||||||
|
});
|
||||||
web.UseKestrel(kestrel => kestrel.ListenHttpsReverseProxy());
|
web.UseKestrel(kestrel => kestrel.ListenHttpsReverseProxy());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user