From 17f7959dbe879fa76282c21becf9f2c98ea53188 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:31:39 +0800 Subject: [PATCH] map fallback to github/fastgithub --- FastGithub.Configuration/LocalMachine.cs | 21 +------------------ FastGithub.Dns/DnsOverUdpHostedService.cs | 4 ++-- .../ReverseProxyMiddleware.cs | 14 ++----------- FastGithub/Startup.cs | 10 ++++++++- 4 files changed, 14 insertions(+), 35 deletions(-) diff --git a/FastGithub.Configuration/LocalMachine.cs b/FastGithub.Configuration/LocalMachine.cs index 4420e7f..eb63abf 100644 --- a/FastGithub.Configuration/LocalMachine.cs +++ b/FastGithub.Configuration/LocalMachine.cs @@ -58,26 +58,7 @@ namespace FastGithub.Configuration public static bool ContainsIPAddress(IPAddress address) { 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.Dns/DnsOverUdpHostedService.cs b/FastGithub.Dns/DnsOverUdpHostedService.cs index 45a543e..7d52c97 100644 --- a/FastGithub.Dns/DnsOverUdpHostedService.cs +++ b/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()); } diff --git a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs b/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs index 1abbb06..f8b9766 100644 --- a/FastGithub.ReverseProxy/ReverseProxyMiddleware.cs +++ b/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 logger; - private readonly HashSet 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); diff --git a/FastGithub/Startup.cs b/FastGithub/Startup.cs index 7198386..d57efe7 100644 --- a/FastGithub/Startup.cs +++ b/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(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; + })); } } }