From 1e67d8c5b9b25a23ee43c5c9202e09b3953ae458 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, 13 Sep 2021 10:18:13 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=B3=E8=BF=87=E6=9C=AC=E6=9C=BA=E5=9F=9F?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Configuration/LocalMachine.cs | 19 +++++++++++++++++++ .../ApplicationBuilderExtensions.cs | 2 +- .../ReverseProxyMiddleware.cs | 11 ++++++++++- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs index f8db286..4420e7f 100644 --- a/FastGithub.Configuration/LocalMachine.cs +++ b/FastGithub.Configuration/LocalMachine.cs @@ -60,6 +60,25 @@ namespace FastGithub.Configuration return GetAllIPAddresses().Contains(address); } + /// + /// 获取所有域名和ip + /// + /// + public static HashSet GetAllHostNames() + { + var hashSet = new HashSet + { + Name, + "localhost", + }; + + foreach (var address in GetAllIPAddresses()) + { + hashSet.Add(address.ToString()); + } + return hashSet; + } + /// /// 获取与远程节点通讯的的本机IP地址 /// diff --git a/FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs b/FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs index cd8ff39..854b684 100644 --- a/FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs +++ b/FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs @@ -28,7 +28,7 @@ namespace FastGithub public static IApplicationBuilder UseReverseProxy(this IApplicationBuilder app) { var middleware = app.ApplicationServices.GetRequiredService(); - return app.Use(next => context => middleware.InvokeAsync(context)); + return app.Use(next => context => middleware.InvokeAsync(context, next)); } } } diff --git a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs index 6354098..1abbb06 100644 --- a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs +++ b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs @@ -3,6 +3,7 @@ using FastGithub.Http; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using System; +using System.Collections.Generic; using System.Threading.Tasks; using Yarp.ReverseProxy.Forwarder; @@ -17,6 +18,7 @@ namespace FastGithub.ReverseProxy private readonly IHttpClientFactory httpClientFactory; private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; + private readonly HashSet localHostNames = LocalMachine.GetAllHostNames(); private readonly DomainConfig defaultDomainConfig = new() { TlsSni = true }; public ReverseProxyMiddleware( @@ -35,10 +37,17 @@ namespace FastGithub.ReverseProxy /// 处理请求 /// /// + /// - public async Task InvokeAsync(HttpContext context) + public async Task InvokeAsync(HttpContext context, RequestDelegate next) { var host = context.Request.Host.Host; + if (this.localHostNames.Contains(host) == true) + { + await next(context); + return; + } + if (this.fastGithubConfig.TryGetDomainConfig(host, out var domainConfig) == false) { domainConfig = this.defaultDomainConfig;