Explorar el Código

map fallback to github/fastgithub

陈国伟 hace 3 años
padre
commit
17f7959dbe

+ 1 - 20
FastGithub.Configuration/LocalMachine.cs

@@ -58,26 +58,7 @@ namespace FastGithub.Configuration
         public static bool ContainsIPAddress(IPAddress address)
         {
             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地址

+ 2 - 2
FastGithub.Dns/DnsOverUdpHostedService.cs

@@ -51,8 +51,8 @@ namespace FastGithub.Dns
             {
                 var builder = new StringBuilder().AppendLine($"DNS服务启动失败({ex.Message}),你可以选择如下的一种操作:");
                 builder.AppendLine($"1. 关闭占用udp53端口的进程然后重新打开本程序");
-                builder.AppendLine($"2. 向系统hosts文件添加要加速的域名的ip为127.0.0.1");
-                builder.AppendLine($"3. 配置系统或浏览器使用DNS over HTTPS:https://127.0.0.1/dns-query");
+                builder.AppendLine($"2. 配置系统或浏览器使用DNS over HTTPS:https://127.0.0.1/dns-query");
+                builder.AppendLine($"3. 向系统hosts文件添加github相关域名的ip为127.0.0.1");
                 builder.Append($"4. 在局域网其它设备上运行本程序,然后将本机DNS设置为局域网设备的IP");
                 this.logger.LogError(builder.ToString());
             }

+ 2 - 12
FastGithub.ReverseProxy/ReverseProxyMiddleware.cs

@@ -3,7 +3,6 @@ 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;
 
@@ -18,8 +17,6 @@ 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(
             IHttpForwarder httpForwarder,
@@ -42,18 +39,11 @@ namespace FastGithub.ReverseProxy
         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;
+                await next(context);
             }
-
-            if (domainConfig.Response == null)
+            else if (domainConfig.Response == null)
             {
                 var scheme = context.Request.Scheme;
                 var destinationPrefix = GetDestinationPrefix(scheme, host, domainConfig.Destination);

+ 9 - 1
FastGithub/Startup.cs

@@ -2,6 +2,7 @@ using FastGithub.Configuration;
 using Microsoft.AspNetCore.Builder;
 using Microsoft.Extensions.Configuration;
 using Microsoft.Extensions.DependencyInjection;
+using System.Threading.Tasks;
 
 namespace FastGithub
 {
@@ -29,7 +30,7 @@ namespace FastGithub
         {
             services.Configure<FastGithubOptions>(this.Configuration.GetSection(nameof(FastGithub)));
 
-            services.AddConfiguration();           
+            services.AddConfiguration();
             services.AddDomainResolve();
             services.AddDnsServer();
             services.AddHttpClient();
@@ -46,6 +47,13 @@ namespace FastGithub
             app.UseRequestLogging();
             app.UseDnsOverHttps();
             app.UseReverseProxy();
+
+            app.UseRouting();
+            app.UseEndpoints(endpoint => endpoint.MapFallback(context =>
+            {
+                context.Response.Redirect("https://github.com/dotnetcore/FastGithub");
+                return Task.CompletedTask;
+            }));
         }
     }
 }