增加IGithubResolver接口
This commit is contained in:
parent
eb9bdfe906
commit
acb18d7078
@ -22,6 +22,11 @@ namespace FastGithub.Dns
|
||||
/// <summary>
|
||||
/// 是否设置本机使用此dns
|
||||
/// </summary>
|
||||
public bool SetToLocalMachine { get; set; } = true;
|
||||
public bool SetToLocalMachine { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否使用反向代理访问github
|
||||
/// </summary>
|
||||
public bool UseGithubReverseProxy { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -6,7 +6,7 @@ namespace FastGithub.Scanner
|
||||
/// 域名
|
||||
/// </summary>
|
||||
[Options("Lookup")]
|
||||
class GithubLookupFactoryOptions
|
||||
sealed class GithubLookupFactoryOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 反查的域名
|
||||
|
||||
49
FastGithub.Scanner/GithubResolver.cs
Normal file
49
FastGithub.Scanner/GithubResolver.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
@ -49,17 +42,7 @@ namespace FastGithub.Scanner
|
||||
{
|
||||
return this.contexts.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否支持指定域名
|
||||
/// </summary>
|
||||
/// <param name="domain"></param>
|
||||
/// <returns></returns>
|
||||
public bool Support(string domain)
|
||||
{
|
||||
return this.options.CurrentValue.Domains.Contains(domain);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找最优的ip
|
||||
@ -67,12 +50,7 @@ namespace FastGithub.Scanner
|
||||
/// <param name="domain"></param>
|
||||
/// <returns></returns>
|
||||
public IPAddress? FindBestAddress(string domain)
|
||||
{
|
||||
if (this.Support(domain) == false)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
{
|
||||
lock (this.syncRoot)
|
||||
{
|
||||
return this.contexts
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -40,8 +40,7 @@ namespace FastGithub
|
||||
.AddMemoryCache()
|
||||
.AddServiceAndOptions(assembly, configuration)
|
||||
.AddHostedService<GithubFullScanHostedService>()
|
||||
.AddHostedService<GithubResultScanHostedService>()
|
||||
.AddSingleton<IGithubScanResults>(appService => appService.GetRequiredService<GithubScanResults>());
|
||||
.AddHostedService<GithubResultScanHostedService>();
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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": {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user