From de38ad6e8038ff469a6f04603a6c9e4cf1e64ef9 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Wed, 28 Jul 2021 00:16:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=90=86=E9=AA=8C?= =?UTF-8?q?=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/DnsHostedService.cs | 15 +++-- ...ostsFileValidator.cs => HostsValidator.cs} | 8 +-- FastGithub.Dns/IDnsValidator.cs | 16 +++++ FastGithub.Dns/ProxyValidtor.cs | 64 +++++++++++++++++++ FastGithub.Dns/ServiceCollectionExtensions.cs | 3 +- 5 files changed, 96 insertions(+), 10 deletions(-) rename FastGithub.Dns/{HostsFileValidator.cs => HostsValidator.cs} (94%) create mode 100644 FastGithub.Dns/IDnsValidator.cs create mode 100644 FastGithub.Dns/ProxyValidtor.cs diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs index 6831b5f..db2426a 100644 --- a/FastGithub.Dns/DnsHostedService.cs +++ b/FastGithub.Dns/DnsHostedService.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; +using System.Collections.Generic; using System.Net; using System.Threading; using System.Threading.Tasks; @@ -15,24 +16,24 @@ namespace FastGithub.Dns sealed class DnsHostedService : BackgroundService { private readonly DnsServer dnsServer; - private readonly HostsFileValidator hostsValidator; + private readonly IEnumerable dnsValidators; private readonly ILogger logger; /// /// dns后台服务 /// /// - /// + /// /// /// public DnsHostedService( DnsServer dnsServer, - HostsFileValidator hostsValidator, + IEnumerable dnsValidators, IOptionsMonitor options, ILogger logger) { this.dnsServer = dnsServer; - this.hostsValidator = hostsValidator; + this.dnsValidators = dnsValidators; this.logger = logger; options.OnChange(opt => @@ -72,7 +73,11 @@ namespace FastGithub.Dns this.logger.LogWarning($"不支持自动设置DNS,请根据你的系统平台情况修改主DNS为{IPAddress.Loopback}"); } - await this.hostsValidator.ValidateAsync(); + foreach (var item in this.dnsValidators) + { + await item.ValidateAsync(); + } + await base.StartAsync(cancellationToken); } diff --git a/FastGithub.Dns/HostsFileValidator.cs b/FastGithub.Dns/HostsValidator.cs similarity index 94% rename from FastGithub.Dns/HostsFileValidator.cs rename to FastGithub.Dns/HostsValidator.cs index de71c46..2fb76b2 100644 --- a/FastGithub.Dns/HostsFileValidator.cs +++ b/FastGithub.Dns/HostsValidator.cs @@ -13,19 +13,19 @@ namespace FastGithub.Dns /// /// host文件配置验证器 /// - sealed class HostsFileValidator + sealed class HostsValidator : IDnsValidator { private readonly FastGithubConfig fastGithubConfig; - private readonly ILogger logger; + private readonly ILogger logger; /// /// host文件配置验证器 /// /// /// - public HostsFileValidator( + public HostsValidator( FastGithubConfig fastGithubConfig, - ILogger logger) + ILogger logger) { this.fastGithubConfig = fastGithubConfig; this.logger = logger; diff --git a/FastGithub.Dns/IDnsValidator.cs b/FastGithub.Dns/IDnsValidator.cs new file mode 100644 index 0000000..a252428 --- /dev/null +++ b/FastGithub.Dns/IDnsValidator.cs @@ -0,0 +1,16 @@ +using System.Threading.Tasks; + +namespace FastGithub.Dns +{ + /// + /// Dns验证器 + /// + interface IDnsValidator + { + /// + /// 验证 + /// + /// + Task ValidateAsync(); + } +} \ No newline at end of file diff --git a/FastGithub.Dns/ProxyValidtor.cs b/FastGithub.Dns/ProxyValidtor.cs new file mode 100644 index 0000000..8d75723 --- /dev/null +++ b/FastGithub.Dns/ProxyValidtor.cs @@ -0,0 +1,64 @@ +using FastGithub.Configuration; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System; +using System.Net.Http; +using System.Threading.Tasks; + +namespace FastGithub.Dns +{ + /// + /// 代理验证 + /// + sealed class ProxyValidtor : IDnsValidator + { + private readonly IOptions options; + private readonly ILogger logger; + + public ProxyValidtor( + IOptions options, + ILogger logger) + { + this.options = options; + this.logger = logger; + } + + /// + /// 验证是否使用了代理 + /// + /// + public Task ValidateAsync() + { + try + { + this.ValidateSystemProxy(); + } + catch (Exception) + { + } + return Task.CompletedTask; + } + + /// + /// 验证代理 + /// + private void ValidateSystemProxy() + { + var systemProxy = HttpClient.DefaultProxy; + if (systemProxy == null) + { + return; + } + + foreach (var domain in this.options.Value.DomainConfigs.Keys) + { + var destination = new Uri($"https://{domain.Replace('*', 'a')}"); + var proxyServer = systemProxy.GetProxy(destination); + if (proxyServer != null) + { + this.logger.LogError($"由于系统配置了{proxyServer}代理{domain},{nameof(FastGithub)}无法加速相关域名"); + } + } + } + } +} diff --git a/FastGithub.Dns/ServiceCollectionExtensions.cs b/FastGithub.Dns/ServiceCollectionExtensions.cs index 454734f..3f74afc 100644 --- a/FastGithub.Dns/ServiceCollectionExtensions.cs +++ b/FastGithub.Dns/ServiceCollectionExtensions.cs @@ -18,7 +18,8 @@ namespace FastGithub { services.TryAddSingleton(); services.TryAddSingleton(); - services.TryAddSingleton(); + services.AddSingleton(); + services.AddSingleton(); return services.AddHostedService(); } }