diff --git a/FastGithub.Dns/DnsOptions.cs b/FastGithub.Dns/DnsOptions.cs index 6fcf7ed..d249c89 100644 --- a/FastGithub.Dns/DnsOptions.cs +++ b/FastGithub.Dns/DnsOptions.cs @@ -22,6 +22,11 @@ namespace FastGithub.Dns /// /// 是否设置本机使用此dns /// - public bool SetToLocalMachine { get; set; } = true; + public bool SetToLocalMachine { get; set; } = true; + + /// + /// 是否使用反向代理访问github + /// + public bool UseGithubReverseProxy { get; set; } } } diff --git a/FastGithub.Dns/GithubRequestResolver.cs b/FastGithub.Dns/GithubRequestResolver.cs index c871298..37c54c3 100644 --- a/FastGithub.Dns/GithubRequestResolver.cs +++ b/FastGithub.Dns/GithubRequestResolver.cs @@ -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 options; - private readonly IOptionsMonitor reverseProxyOptions; private readonly ILogger logger; /// /// github相关域名解析器 /// - /// + /// /// /// public GithubRequestResolver( - IGithubScanResults githubScanResults, + IGithubResolver githubResolver, IOptionsMonitor options, - IOptionsMonitor reverseProxyOptions, ILogger 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; } diff --git a/FastGithub.ReverseProxy/GithubReverseProxyOptions.cs b/FastGithub.ReverseProxy/GithubReverseProxyOptions.cs deleted file mode 100644 index 2c53567..0000000 --- a/FastGithub.ReverseProxy/GithubReverseProxyOptions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Yarp.ReverseProxy.Forwarder; - -namespace FastGithub.ReverseProxy -{ - /// - /// 反向代理选项 - /// - [Options("ReverseProxy")] - public class GithubReverseProxyOptions - { - /// - /// 是否启用 - /// - public bool Enable { get; set; } = true; - - /// - /// 请求配置 - /// - public ForwarderRequestConfig ForwarderRequestConfig { get; set; } = new(); - } -} diff --git a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs index 74150b2..1325192 100644 --- a/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs +++ b/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs @@ -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(); var httpClientHanlder = app.ApplicationServices.GetRequiredService(); - var scanResults = app.ApplicationServices.GetRequiredService(); - var options = app.ApplicationServices.GetRequiredService>(); + var githubResolver = app.ApplicationServices.GetRequiredService(); 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); } }); diff --git a/FastGithub.Scanner/GithubHttpClientHanlder.cs b/FastGithub.Scanner/GithubHttpClientHanlder.cs index 21b36aa..03c7855 100644 --- a/FastGithub.Scanner/GithubHttpClientHanlder.cs +++ b/FastGithub.Scanner/GithubHttpClientHanlder.cs @@ -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 logger; private readonly IMemoryCache memoryCache; /// /// 请求github的HttpClientHandler /// - /// + /// /// /// public GithubHttpClientHanlder( - IGithubScanResults githubScanResults, + IGithubResolver githubResolver, ILogger 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 /// 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) diff --git a/FastGithub.Scanner/GithubLookupFactoryOptions.cs b/FastGithub.Scanner/GithubLookupFactoryOptions.cs index 0530441..bf1a10d 100644 --- a/FastGithub.Scanner/GithubLookupFactoryOptions.cs +++ b/FastGithub.Scanner/GithubLookupFactoryOptions.cs @@ -6,7 +6,7 @@ namespace FastGithub.Scanner /// 域名 /// [Options("Lookup")] - class GithubLookupFactoryOptions + sealed class GithubLookupFactoryOptions { /// /// 反查的域名 diff --git a/FastGithub.Scanner/GithubResolver.cs b/FastGithub.Scanner/GithubResolver.cs new file mode 100644 index 0000000..b4adc02 --- /dev/null +++ b/FastGithub.Scanner/GithubResolver.cs @@ -0,0 +1,49 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; +using System.Net; + +namespace FastGithub.Scanner +{ + /// + /// github解析器 + /// + [Service(ServiceLifetime.Singleton, ServiceType = typeof(IGithubResolver))] + sealed class GithubResolver : IGithubResolver + { + private readonly GithubScanResults githubScanResults; + private readonly IOptionsMonitor options; + + /// + /// github解析器 + /// + /// + /// + public GithubResolver( + GithubScanResults githubScanResults, + IOptionsMonitor options) + { + this.githubScanResults = githubScanResults; + this.options = options; + } + + /// + /// 是否支持指定的域名 + /// + /// + /// + public bool IsSupported(string domain) + { + return this.options.CurrentValue.Domains.Contains(domain); + } + + /// + /// 解析指定的域名 + /// + /// + /// + public IPAddress? Resolve(string domain) + { + return this.IsSupported(domain) ? this.githubScanResults.FindBestAddress(domain) : default; + } + } +} diff --git a/FastGithub.Scanner/GithubScanResults.cs b/FastGithub.Scanner/GithubScanResults.cs index 4852252..b55e721 100644 --- a/FastGithub.Scanner/GithubScanResults.cs +++ b/FastGithub.Scanner/GithubScanResults.cs @@ -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集合 /// [Service(ServiceLifetime.Singleton)] - sealed class GithubScanResults : IGithubScanResults + sealed class GithubScanResults { private readonly object syncRoot = new(); private readonly List contexts = new(); - private readonly IOptionsMonitor options; - - public GithubScanResults(IOptionsMonitor options) - { - this.options = options; - } /// /// 添加GithubContext @@ -49,17 +42,7 @@ namespace FastGithub.Scanner { return this.contexts.ToArray(); } - } - - /// - /// 是否支持指定域名 - /// - /// - /// - public bool Support(string domain) - { - return this.options.CurrentValue.Domains.Contains(domain); - } + } /// /// 查找最优的ip @@ -67,12 +50,7 @@ namespace FastGithub.Scanner /// /// public IPAddress? FindBestAddress(string domain) - { - if (this.Support(domain) == false) - { - return default; - } - + { lock (this.syncRoot) { return this.contexts diff --git a/FastGithub.Scanner/IGithubScanResults.cs b/FastGithub.Scanner/IGithubResolver.cs similarity index 59% rename from FastGithub.Scanner/IGithubScanResults.cs rename to FastGithub.Scanner/IGithubResolver.cs index 2a259db..ca5942c 100644 --- a/FastGithub.Scanner/IGithubScanResults.cs +++ b/FastGithub.Scanner/IGithubResolver.cs @@ -3,22 +3,22 @@ namespace FastGithub.Scanner { /// - /// 定义扫描结果的接口 + /// github解析器 /// - public interface IGithubScanResults + public interface IGithubResolver { /// - /// 是否支持指定域名 + /// 是否支持指定的域名 /// /// /// - bool Support(string domain); + bool IsSupported(string domain); /// - /// 查找最优的ip + /// 解析指定的域名 /// /// /// - IPAddress? FindBestAddress(string domain); + IPAddress? Resolve(string domain); } } diff --git a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs index 1d34220..51b798e 100644 --- a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs +++ b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs @@ -40,8 +40,7 @@ namespace FastGithub .AddMemoryCache() .AddServiceAndOptions(assembly, configuration) .AddHostedService() - .AddHostedService() - .AddSingleton(appService => appService.GetRequiredService()); + .AddHostedService(); ; } } diff --git a/FastGithub/appsettings.json b/FastGithub/appsettings.json index 68e25a3..9ce2715 100644 --- a/FastGithub/appsettings.json +++ b/FastGithub/appsettings.json @@ -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": {