增加IGithubResolver接口

This commit is contained in:
陈国伟 2021-07-15 12:50:58 +08:00
parent eb9bdfe906
commit acb18d7078
11 changed files with 86 additions and 89 deletions

View File

@ -23,5 +23,10 @@ namespace FastGithub.Dns
/// 是否设置本机使用此dns
/// </summary>
public bool SetToLocalMachine { get; set; } = true;
/// <summary>
/// 是否使用反向代理访问github
/// </summary>
public bool UseGithubReverseProxy { get; set; }
}
}

View File

@ -1,7 +1,6 @@
using DNS.Client.RequestResolver;
using DNS.Protocol;
using DNS.Protocol.ResourceRecords;
using FastGithub.ReverseProxy;
using FastGithub.Scanner;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
@ -21,26 +20,23 @@ namespace FastGithub.Dns
[Service(ServiceLifetime.Singleton)]
sealed class GithubRequestResolver : IRequestResolver
{
private readonly IGithubScanResults githubScanResults;
private readonly IGithubResolver githubResolver;
private readonly IOptionsMonitor<DnsOptions> options;
private readonly IOptionsMonitor<GithubReverseProxyOptions> reverseProxyOptions;
private readonly ILogger<GithubRequestResolver> logger;
/// <summary>
/// github相关域名解析器
/// </summary>
/// <param name="githubScanResults"></param>
/// <param name="githubResolver"></param>
/// <param name="options"></param>
/// <param name="logger"></param>
public GithubRequestResolver(
IGithubScanResults githubScanResults,
IGithubResolver githubResolver,
IOptionsMonitor<DnsOptions> options,
IOptionsMonitor<GithubReverseProxyOptions> reverseProxyOptions,
ILogger<GithubRequestResolver> logger)
{
this.githubScanResults = githubScanResults;
this.githubResolver = githubResolver;
this.options = options;
this.reverseProxyOptions = reverseProxyOptions;
this.logger = logger;
}
@ -61,14 +57,14 @@ namespace FastGithub.Dns
}
var domain = question.Name.ToString();
if (this.githubScanResults.Support(domain) == false)
if (this.githubResolver.IsSupported(domain) == false)
{
return response;
}
if (this.reverseProxyOptions.CurrentValue.Enable == false)
if (this.options.CurrentValue.UseGithubReverseProxy == false)
{
var address = this.githubScanResults.FindBestAddress(domain);
var address = this.githubResolver.Resolve(domain);
if (address != null)
{
var ttl = this.options.CurrentValue.GithubTTL;
@ -89,7 +85,6 @@ namespace FastGithub.Dns
{
this.logger.LogWarning($"无法获得{domain}的最快ip");
}
return response;
}

View File

@ -1,21 +0,0 @@
using Yarp.ReverseProxy.Forwarder;
namespace FastGithub.ReverseProxy
{
/// <summary>
/// 反向代理选项
/// </summary>
[Options("ReverseProxy")]
public class GithubReverseProxyOptions
{
/// <summary>
/// 是否启用
/// </summary>
public bool Enable { get; set; } = true;
/// <summary>
/// 请求配置
/// </summary>
public ForwarderRequestConfig ForwarderRequestConfig { get; set; } = new();
}
}

View File

@ -1,9 +1,7 @@
using FastGithub.ReverseProxy;
using FastGithub.Scanner;
using FastGithub.Scanner;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Net.Http;
using Yarp.ReverseProxy.Forwarder;
@ -23,13 +21,12 @@ namespace FastGithub
{
var httpForwarder = app.ApplicationServices.GetRequiredService<IHttpForwarder>();
var httpClientHanlder = app.ApplicationServices.GetRequiredService<GithubHttpClientHanlder>();
var scanResults = app.ApplicationServices.GetRequiredService<IGithubScanResults>();
var options = app.ApplicationServices.GetRequiredService<IOptionsMonitor<GithubReverseProxyOptions>>();
var githubResolver = app.ApplicationServices.GetRequiredService<IGithubResolver>();
app.Use(next => async context =>
{
var host = context.Request.Host.Host;
if (scanResults.Support(host) == false)
if (githubResolver.IsSupported(host) == false)
{
await context.Response.WriteAsJsonAsync(new { message = $"不支持以{host}访问" });
}
@ -38,8 +35,7 @@ namespace FastGithub
var port = context.Request.Host.Port ?? 443;
var destinationPrefix = $"http://{host}:{port}/";
var httpClient = new HttpMessageInvoker(httpClientHanlder, disposeHandler: false);
var requestConfig = options.CurrentValue.ForwarderRequestConfig;
await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig);
await httpForwarder.SendAsync(context, destinationPrefix, httpClient);
}
});

View File

@ -17,22 +17,22 @@ namespace FastGithub.Scanner
[Service(ServiceLifetime.Transient)]
public class GithubHttpClientHanlder : DelegatingHandler
{
private readonly IGithubScanResults githubScanResults;
private readonly IGithubResolver githubResolver;
private readonly ILogger<GithubHttpClientHanlder> logger;
private readonly IMemoryCache memoryCache;
/// <summary>
/// 请求github的HttpClientHandler
/// </summary>
/// <param name="githubScanResults"></param>
/// <param name="githubResolver"></param>
/// <param name="logger"></param>
/// <param name="memoryCache"></param>
public GithubHttpClientHanlder(
IGithubScanResults githubScanResults,
IGithubResolver githubResolver,
ILogger<GithubHttpClientHanlder> logger,
IMemoryCache memoryCache)
{
this.githubScanResults = githubScanResults;
this.githubResolver = githubResolver;
this.logger = logger;
this.memoryCache = memoryCache;
this.InnerHandler = CreateNoneSniHttpHandler();
@ -104,7 +104,8 @@ namespace FastGithub.Scanner
/// <returns></returns>
private IPAddress? Resolve(string domain)
{
if (this.githubScanResults.Support(domain) == false)
// 非github的域名返回null走上游dns
if (this.githubResolver.IsSupported(domain) == false)
{
return default;
}
@ -113,7 +114,7 @@ namespace FastGithub.Scanner
var address = this.memoryCache.GetOrCreate(key, e =>
{
e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
return this.githubScanResults.FindBestAddress(domain);
return this.githubResolver.Resolve(domain);
});
if (address == null)

View File

@ -6,7 +6,7 @@ namespace FastGithub.Scanner
/// 域名
/// </summary>
[Options("Lookup")]
class GithubLookupFactoryOptions
sealed class GithubLookupFactoryOptions
{
/// <summary>
/// 反查的域名

View File

@ -0,0 +1,49 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Net;
namespace FastGithub.Scanner
{
/// <summary>
/// github解析器
/// </summary>
[Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubResolver))]
sealed class GithubResolver : IGithubResolver
{
private readonly GithubScanResults githubScanResults;
private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
/// <summary>
/// github解析器
/// </summary>
/// <param name="githubScanResults"></param>
/// <param name="options"></param>
public GithubResolver(
GithubScanResults githubScanResults,
IOptionsMonitor<GithubLookupFactoryOptions> options)
{
this.githubScanResults = githubScanResults;
this.options = options;
}
/// <summary>
/// 是否支持指定的域名
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public bool IsSupported(string domain)
{
return this.options.CurrentValue.Domains.Contains(domain);
}
/// <summary>
/// 解析指定的域名
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public IPAddress? Resolve(string domain)
{
return this.IsSupported(domain) ? this.githubScanResults.FindBestAddress(domain) : default;
}
}
}

View File

@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Linq;
using System.Net;
@ -10,16 +9,10 @@ namespace FastGithub.Scanner
/// GithubContext集合
/// </summary>
[Service(ServiceLifetime.Singleton)]
sealed class GithubScanResults : IGithubScanResults
sealed class GithubScanResults
{
private readonly object syncRoot = new();
private readonly List<GithubContext> contexts = new();
private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
public GithubScanResults(IOptionsMonitor<GithubLookupFactoryOptions> options)
{
this.options = options;
}
/// <summary>
/// 添加GithubContext
@ -51,16 +44,6 @@ namespace FastGithub.Scanner
}
}
/// <summary>
/// 是否支持指定域名
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public bool Support(string domain)
{
return this.options.CurrentValue.Domains.Contains(domain);
}
/// <summary>
/// 查找最优的ip
/// </summary>
@ -68,11 +51,6 @@ namespace FastGithub.Scanner
/// <returns></returns>
public IPAddress? FindBestAddress(string domain)
{
if (this.Support(domain) == false)
{
return default;
}
lock (this.syncRoot)
{
return this.contexts

View File

@ -3,22 +3,22 @@
namespace FastGithub.Scanner
{
/// <summary>
/// 定义扫描结果的接口
/// github解析器
/// </summary>
public interface IGithubScanResults
public interface IGithubResolver
{
/// <summary>
/// 是否支持指定域名
/// 是否支持指定域名
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
bool Support(string domain);
bool IsSupported(string domain);
/// <summary>
/// 查找最优的ip
/// 解析指定的域名
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
IPAddress? FindBestAddress(string domain);
IPAddress? Resolve(string domain);
}
}

View File

@ -40,8 +40,7 @@ namespace FastGithub
.AddMemoryCache()
.AddServiceAndOptions(assembly, configuration)
.AddHostedService<GithubFullScanHostedService>()
.AddHostedService<GithubResultScanHostedService>()
.AddSingleton<IGithubScanResults>(appService => appService.GetRequiredService<GithubScanResults>());
.AddHostedService<GithubResultScanHostedService>();
;
}
}

View File

@ -2,13 +2,8 @@
"Dns": {
"UpStream": "114.114.114.114", // dns
"GithubTTL": "00:10:00", // github
"SetToLocalMachine": true // 使dns(windows)
},
"ReverseProxy": {
"Enable": true, // 使访github
"ForwarderRequestConfig": {
"Timeout": "00:02:00" //
}
"SetToLocalMachine": true, // 使dns(windows)
"UseGithubReverseProxy": true // 使访github
},
"Lookup": { // ip
"IPAddressComProvider": {