Browse Source

跳过本机域名

陈国伟 3 years ago
parent
commit
1e67d8c5b9

+ 19 - 0
FastGithub.Configuration/LocalMachine.cs

@@ -60,6 +60,25 @@ namespace FastGithub.Configuration
             return GetAllIPAddresses().Contains(address);
         }
 
+        /// <summary>
+        /// 获取所有域名和ip
+        /// </summary>
+        /// <returns></returns>
+        public static HashSet<string> GetAllHostNames()
+        {
+            var hashSet = new HashSet<string>
+            {
+                Name,
+                "localhost",
+            };
+
+            foreach (var address in GetAllIPAddresses())
+            {
+                hashSet.Add(address.ToString());
+            }
+            return hashSet;
+        }
+
         /// <summary>
         /// 获取与远程节点通讯的的本机IP地址
         /// </summary>

+ 1 - 1
FastGithub.ReverseProxy/ApplicationBuilderExtensions.cs

@@ -28,7 +28,7 @@ namespace FastGithub
         public static IApplicationBuilder UseReverseProxy(this IApplicationBuilder app)
         {
             var middleware = app.ApplicationServices.GetRequiredService<ReverseProxyMiddleware>();
-            return app.Use(next => context => middleware.InvokeAsync(context));
+            return app.Use(next => context => middleware.InvokeAsync(context, next));
         }
     }
 }

+ 10 - 1
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<ReverseProxyMiddleware> logger;
+        private readonly HashSet<string> localHostNames = LocalMachine.GetAllHostNames();
         private readonly DomainConfig defaultDomainConfig = new() { TlsSni = true };
 
         public ReverseProxyMiddleware(
@@ -35,10 +37,17 @@ namespace FastGithub.ReverseProxy
         /// 处理请求
         /// </summary>
         /// <param name="context"></param>
+        /// <param name="next"?
         /// <returns></returns>
-        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;