增加IGithubResolver接口
This commit is contained in:
parent
eb9bdfe906
commit
acb18d7078
@ -22,6 +22,11 @@ namespace FastGithub.Dns
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否设置本机使用此dns
|
/// 是否设置本机使用此dns
|
||||||
/// </summary>
|
/// </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.Client.RequestResolver;
|
||||||
using DNS.Protocol;
|
using DNS.Protocol;
|
||||||
using DNS.Protocol.ResourceRecords;
|
using DNS.Protocol.ResourceRecords;
|
||||||
using FastGithub.ReverseProxy;
|
|
||||||
using FastGithub.Scanner;
|
using FastGithub.Scanner;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@ -21,26 +20,23 @@ namespace FastGithub.Dns
|
|||||||
[Service(ServiceLifetime.Singleton)]
|
[Service(ServiceLifetime.Singleton)]
|
||||||
sealed class GithubRequestResolver : IRequestResolver
|
sealed class GithubRequestResolver : IRequestResolver
|
||||||
{
|
{
|
||||||
private readonly IGithubScanResults githubScanResults;
|
private readonly IGithubResolver githubResolver;
|
||||||
private readonly IOptionsMonitor<DnsOptions> options;
|
private readonly IOptionsMonitor<DnsOptions> options;
|
||||||
private readonly IOptionsMonitor<GithubReverseProxyOptions> reverseProxyOptions;
|
|
||||||
private readonly ILogger<GithubRequestResolver> logger;
|
private readonly ILogger<GithubRequestResolver> logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// github相关域名解析器
|
/// github相关域名解析器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="githubScanResults"></param>
|
/// <param name="githubResolver"></param>
|
||||||
/// <param name="options"></param>
|
/// <param name="options"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public GithubRequestResolver(
|
public GithubRequestResolver(
|
||||||
IGithubScanResults githubScanResults,
|
IGithubResolver githubResolver,
|
||||||
IOptionsMonitor<DnsOptions> options,
|
IOptionsMonitor<DnsOptions> options,
|
||||||
IOptionsMonitor<GithubReverseProxyOptions> reverseProxyOptions,
|
|
||||||
ILogger<GithubRequestResolver> logger)
|
ILogger<GithubRequestResolver> logger)
|
||||||
{
|
{
|
||||||
this.githubScanResults = githubScanResults;
|
this.githubResolver = githubResolver;
|
||||||
this.options = options;
|
this.options = options;
|
||||||
this.reverseProxyOptions = reverseProxyOptions;
|
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,14 +57,14 @@ namespace FastGithub.Dns
|
|||||||
}
|
}
|
||||||
|
|
||||||
var domain = question.Name.ToString();
|
var domain = question.Name.ToString();
|
||||||
if (this.githubScanResults.Support(domain) == false)
|
if (this.githubResolver.IsSupported(domain) == false)
|
||||||
{
|
{
|
||||||
return response;
|
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)
|
if (address != null)
|
||||||
{
|
{
|
||||||
var ttl = this.options.CurrentValue.GithubTTL;
|
var ttl = this.options.CurrentValue.GithubTTL;
|
||||||
@ -89,7 +85,6 @@ namespace FastGithub.Dns
|
|||||||
{
|
{
|
||||||
this.logger.LogWarning($"无法获得{domain}的最快ip");
|
this.logger.LogWarning($"无法获得{domain}的最快ip");
|
||||||
}
|
}
|
||||||
|
|
||||||
return response;
|
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.Builder;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using Yarp.ReverseProxy.Forwarder;
|
using Yarp.ReverseProxy.Forwarder;
|
||||||
|
|
||||||
@ -23,13 +21,12 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
var httpForwarder = app.ApplicationServices.GetRequiredService<IHttpForwarder>();
|
var httpForwarder = app.ApplicationServices.GetRequiredService<IHttpForwarder>();
|
||||||
var httpClientHanlder = app.ApplicationServices.GetRequiredService<GithubHttpClientHanlder>();
|
var httpClientHanlder = app.ApplicationServices.GetRequiredService<GithubHttpClientHanlder>();
|
||||||
var scanResults = app.ApplicationServices.GetRequiredService<IGithubScanResults>();
|
var githubResolver = app.ApplicationServices.GetRequiredService<IGithubResolver>();
|
||||||
var options = app.ApplicationServices.GetRequiredService<IOptionsMonitor<GithubReverseProxyOptions>>();
|
|
||||||
|
|
||||||
app.Use(next => async context =>
|
app.Use(next => async context =>
|
||||||
{
|
{
|
||||||
var host = context.Request.Host.Host;
|
var host = context.Request.Host.Host;
|
||||||
if (scanResults.Support(host) == false)
|
if (githubResolver.IsSupported(host) == false)
|
||||||
{
|
{
|
||||||
await context.Response.WriteAsJsonAsync(new { message = $"不支持以{host}访问" });
|
await context.Response.WriteAsJsonAsync(new { message = $"不支持以{host}访问" });
|
||||||
}
|
}
|
||||||
@ -38,8 +35,7 @@ namespace FastGithub
|
|||||||
var port = context.Request.Host.Port ?? 443;
|
var port = context.Request.Host.Port ?? 443;
|
||||||
var destinationPrefix = $"http://{host}:{port}/";
|
var destinationPrefix = $"http://{host}:{port}/";
|
||||||
var httpClient = new HttpMessageInvoker(httpClientHanlder, disposeHandler: false);
|
var httpClient = new HttpMessageInvoker(httpClientHanlder, disposeHandler: false);
|
||||||
var requestConfig = options.CurrentValue.ForwarderRequestConfig;
|
await httpForwarder.SendAsync(context, destinationPrefix, httpClient);
|
||||||
await httpForwarder.SendAsync(context, destinationPrefix, httpClient, requestConfig);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -17,22 +17,22 @@ namespace FastGithub.Scanner
|
|||||||
[Service(ServiceLifetime.Transient)]
|
[Service(ServiceLifetime.Transient)]
|
||||||
public class GithubHttpClientHanlder : DelegatingHandler
|
public class GithubHttpClientHanlder : DelegatingHandler
|
||||||
{
|
{
|
||||||
private readonly IGithubScanResults githubScanResults;
|
private readonly IGithubResolver githubResolver;
|
||||||
private readonly ILogger<GithubHttpClientHanlder> logger;
|
private readonly ILogger<GithubHttpClientHanlder> logger;
|
||||||
private readonly IMemoryCache memoryCache;
|
private readonly IMemoryCache memoryCache;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 请求github的HttpClientHandler
|
/// 请求github的HttpClientHandler
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="githubScanResults"></param>
|
/// <param name="githubResolver"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
/// <param name="memoryCache"></param>
|
/// <param name="memoryCache"></param>
|
||||||
public GithubHttpClientHanlder(
|
public GithubHttpClientHanlder(
|
||||||
IGithubScanResults githubScanResults,
|
IGithubResolver githubResolver,
|
||||||
ILogger<GithubHttpClientHanlder> logger,
|
ILogger<GithubHttpClientHanlder> logger,
|
||||||
IMemoryCache memoryCache)
|
IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
this.githubScanResults = githubScanResults;
|
this.githubResolver = githubResolver;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.memoryCache = memoryCache;
|
this.memoryCache = memoryCache;
|
||||||
this.InnerHandler = CreateNoneSniHttpHandler();
|
this.InnerHandler = CreateNoneSniHttpHandler();
|
||||||
@ -104,7 +104,8 @@ namespace FastGithub.Scanner
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private IPAddress? Resolve(string domain)
|
private IPAddress? Resolve(string domain)
|
||||||
{
|
{
|
||||||
if (this.githubScanResults.Support(domain) == false)
|
// 非github的域名,返回null走上游dns
|
||||||
|
if (this.githubResolver.IsSupported(domain) == false)
|
||||||
{
|
{
|
||||||
return default;
|
return default;
|
||||||
}
|
}
|
||||||
@ -113,7 +114,7 @@ namespace FastGithub.Scanner
|
|||||||
var address = this.memoryCache.GetOrCreate(key, e =>
|
var address = this.memoryCache.GetOrCreate(key, e =>
|
||||||
{
|
{
|
||||||
e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
|
e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
|
||||||
return this.githubScanResults.FindBestAddress(domain);
|
return this.githubResolver.Resolve(domain);
|
||||||
});
|
});
|
||||||
|
|
||||||
if (address == null)
|
if (address == null)
|
||||||
|
|||||||
@ -6,7 +6,7 @@ namespace FastGithub.Scanner
|
|||||||
/// 域名
|
/// 域名
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Options("Lookup")]
|
[Options("Lookup")]
|
||||||
class GithubLookupFactoryOptions
|
sealed class GithubLookupFactoryOptions
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <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.DependencyInjection;
|
||||||
using Microsoft.Extensions.Options;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
@ -10,16 +9,10 @@ namespace FastGithub.Scanner
|
|||||||
/// GithubContext集合
|
/// GithubContext集合
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Service(ServiceLifetime.Singleton)]
|
[Service(ServiceLifetime.Singleton)]
|
||||||
sealed class GithubScanResults : IGithubScanResults
|
sealed class GithubScanResults
|
||||||
{
|
{
|
||||||
private readonly object syncRoot = new();
|
private readonly object syncRoot = new();
|
||||||
private readonly List<GithubContext> contexts = new();
|
private readonly List<GithubContext> contexts = new();
|
||||||
private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
|
|
||||||
|
|
||||||
public GithubScanResults(IOptionsMonitor<GithubLookupFactoryOptions> options)
|
|
||||||
{
|
|
||||||
this.options = options;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 添加GithubContext
|
/// 添加GithubContext
|
||||||
@ -49,17 +42,7 @@ namespace FastGithub.Scanner
|
|||||||
{
|
{
|
||||||
return this.contexts.ToArray();
|
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>
|
/// <summary>
|
||||||
/// 查找最优的ip
|
/// 查找最优的ip
|
||||||
@ -67,12 +50,7 @@ namespace FastGithub.Scanner
|
|||||||
/// <param name="domain"></param>
|
/// <param name="domain"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public IPAddress? FindBestAddress(string domain)
|
public IPAddress? FindBestAddress(string domain)
|
||||||
{
|
{
|
||||||
if (this.Support(domain) == false)
|
|
||||||
{
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
|
|
||||||
lock (this.syncRoot)
|
lock (this.syncRoot)
|
||||||
{
|
{
|
||||||
return this.contexts
|
return this.contexts
|
||||||
|
|||||||
@ -3,22 +3,22 @@
|
|||||||
namespace FastGithub.Scanner
|
namespace FastGithub.Scanner
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 定义扫描结果的接口
|
/// github解析器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IGithubScanResults
|
public interface IGithubResolver
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 是否支持指定域名
|
/// 是否支持指定的域名
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="domain"></param>
|
/// <param name="domain"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
bool Support(string domain);
|
bool IsSupported(string domain);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 查找最优的ip
|
/// 解析指定的域名
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="domain"></param>
|
/// <param name="domain"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
IPAddress? FindBestAddress(string domain);
|
IPAddress? Resolve(string domain);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -40,8 +40,7 @@ namespace FastGithub
|
|||||||
.AddMemoryCache()
|
.AddMemoryCache()
|
||||||
.AddServiceAndOptions(assembly, configuration)
|
.AddServiceAndOptions(assembly, configuration)
|
||||||
.AddHostedService<GithubFullScanHostedService>()
|
.AddHostedService<GithubFullScanHostedService>()
|
||||||
.AddHostedService<GithubResultScanHostedService>()
|
.AddHostedService<GithubResultScanHostedService>();
|
||||||
.AddSingleton<IGithubScanResults>(appService => appService.GetRequiredService<GithubScanResults>());
|
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,13 +2,8 @@
|
|||||||
"Dns": {
|
"Dns": {
|
||||||
"UpStream": "114.114.114.114", // 上游dns
|
"UpStream": "114.114.114.114", // 上游dns
|
||||||
"GithubTTL": "00:10:00", // github相关域名解析结果的存活时长
|
"GithubTTL": "00:10:00", // github相关域名解析结果的存活时长
|
||||||
"SetToLocalMachine": true // 是否设置本机使用此dns(仅支持windows)
|
"SetToLocalMachine": true, // 是否设置本机使用此dns(仅支持windows)
|
||||||
},
|
"UseGithubReverseProxy": true // 是否使用反向代理访问github以解决连接被重复的问题
|
||||||
"ReverseProxy": {
|
|
||||||
"Enable": true, // 是否使用反向代理访问github以解决连接被重复的问题
|
|
||||||
"ForwarderRequestConfig": {
|
|
||||||
"Timeout": "00:02:00" // 代理超时时间
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"Lookup": { // ip查找
|
"Lookup": { // ip查找
|
||||||
"IPAddressComProvider": {
|
"IPAddressComProvider": {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user