From 475048c621a6572717481d5cd5e7d9557d44ae2a Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Wed, 21 Jul 2021 20:30:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88Options=E7=9A=84=E7=9B=91?= =?UTF-8?q?=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.Dns/RequestResolver.cs | 17 ++--------------- FastGithub.ReverseProxy/DomainResolver.cs | 16 ++++++---------- 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/FastGithub.Dns/RequestResolver.cs b/FastGithub.Dns/RequestResolver.cs index 42f28af..e9c5a3b 100644 --- a/FastGithub.Dns/RequestResolver.cs +++ b/FastGithub.Dns/RequestResolver.cs @@ -2,7 +2,6 @@ using DNS.Protocol; using DNS.Protocol.ResourceRecords; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using System; using System.Linq; using System.Net; @@ -16,8 +15,6 @@ namespace FastGithub.Dns /// sealed class RequestResolver : IRequestResolver { - private IRequestResolver requestResolver; - private readonly TimeSpan ttl = TimeSpan.FromMinutes(1d); private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; @@ -26,24 +23,13 @@ namespace FastGithub.Dns /// dns解析者 /// /// - /// /// public RequestResolver( FastGithubConfig fastGithubConfig, - IOptionsMonitor options, ILogger logger) { this.fastGithubConfig = fastGithubConfig; this.logger = logger; - - this.requestResolver = new UdpRequestResolver(fastGithubConfig.FastDns); - options.OnChange(opt => OptionsChanged(opt)); - - void OptionsChanged(FastGithubOptions opt) - { - var dns = opt.FastDns.ToIPEndPoint(); - this.requestResolver = new UdpRequestResolver(dns); - } } /// @@ -78,7 +64,8 @@ namespace FastGithub.Dns return response; } - return await this.requestResolver.Resolve(request, cancellationToken); + var fastResolver = new UdpRequestResolver(fastGithubConfig.FastDns); + return await fastResolver.Resolve(request, cancellationToken); } } } diff --git a/FastGithub.ReverseProxy/DomainResolver.cs b/FastGithub.ReverseProxy/DomainResolver.cs index 3b89980..c1df3f1 100644 --- a/FastGithub.ReverseProxy/DomainResolver.cs +++ b/FastGithub.ReverseProxy/DomainResolver.cs @@ -1,7 +1,6 @@ using DNS.Client; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.Options; using System; using System.Linq; using System.Net; @@ -15,9 +14,8 @@ namespace FastGithub.ReverseProxy /// sealed class DomainResolver { - private DnsClient dnsClient; private readonly IMemoryCache memoryCache; - private readonly IOptionsMonitor options; + private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; private readonly TimeSpan cacheTimeSpan = TimeSpan.FromSeconds(10d); @@ -25,19 +23,16 @@ namespace FastGithub.ReverseProxy /// 域名解析器 /// /// - /// + /// /// public DomainResolver( IMemoryCache memoryCache, - IOptionsMonitor options, + FastGithubConfig fastGithubConfig, ILogger logger) { this.memoryCache = memoryCache; - this.options = options; + this.fastGithubConfig = fastGithubConfig; this.logger = logger; - - this.dnsClient = new DnsClient(options.CurrentValue.PureDns.ToIPEndPoint()); - options.OnChange(opt => this.dnsClient = new DnsClient(opt.PureDns.ToIPEndPoint())); } /// @@ -68,6 +63,7 @@ namespace FastGithub.ReverseProxy { try { + var dnsClient = new DnsClient(this.fastGithubConfig.PureDns); var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken); var address = addresses?.FirstOrDefault(); if (address == null) @@ -87,7 +83,7 @@ namespace FastGithub.ReverseProxy } catch (Exception ex) { - var dns = this.options.CurrentValue.PureDns; + var dns = this.fastGithubConfig.PureDns; throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex); } }