map fallback to github/fastgithub

This commit is contained in:
陈国伟 2021-09-13 10:31:39 +08:00
parent 1e67d8c5b9
commit 17f7959dbe
4 changed files with 14 additions and 35 deletions

View File

@ -58,26 +58,7 @@ namespace FastGithub.Configuration
public static bool ContainsIPAddress(IPAddress address) public static bool ContainsIPAddress(IPAddress address)
{ {
return GetAllIPAddresses().Contains(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> /// <summary>
/// 获取与远程节点通讯的的本机IP地址 /// 获取与远程节点通讯的的本机IP地址

View File

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

View File

@ -3,7 +3,6 @@ using FastGithub.Http;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yarp.ReverseProxy.Forwarder; using Yarp.ReverseProxy.Forwarder;
@ -18,8 +17,6 @@ namespace FastGithub.ReverseProxy
private readonly IHttpClientFactory httpClientFactory; private readonly IHttpClientFactory httpClientFactory;
private readonly FastGithubConfig fastGithubConfig; private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<ReverseProxyMiddleware> logger; private readonly ILogger<ReverseProxyMiddleware> logger;
private readonly HashSet<string> localHostNames = LocalMachine.GetAllHostNames();
private readonly DomainConfig defaultDomainConfig = new() { TlsSni = true };
public ReverseProxyMiddleware( public ReverseProxyMiddleware(
IHttpForwarder httpForwarder, IHttpForwarder httpForwarder,
@ -42,18 +39,11 @@ namespace FastGithub.ReverseProxy
public async Task InvokeAsync(HttpContext context, RequestDelegate next) public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{ {
var host = context.Request.Host.Host; 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) if (this.fastGithubConfig.TryGetDomainConfig(host, out var domainConfig) == false)
{ {
domainConfig = this.defaultDomainConfig; await next(context);
} }
else if (domainConfig.Response == null)
if (domainConfig.Response == null)
{ {
var scheme = context.Request.Scheme; var scheme = context.Request.Scheme;
var destinationPrefix = GetDestinationPrefix(scheme, host, domainConfig.Destination); var destinationPrefix = GetDestinationPrefix(scheme, host, domainConfig.Destination);

View File

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