From 14aa80605857eb14180ed1a00c311529b583713e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com> Date: Sun, 29 Aug 2021 23:17:34 +0800 Subject: [PATCH] =?UTF-8?q?dns=E8=A7=A3=E6=9E=90=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.DomainResolve/DomainResolver.cs | 51 +++++++++++++--------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs index ea7be8a..0357b47 100644 --- a/FastGithub.DomainResolve/DomainResolver.cs +++ b/FastGithub.DomainResolve/DomainResolver.cs @@ -81,7 +81,7 @@ namespace FastGithub.DomainResolve try { await semaphore.WaitAsync(); - return await this.LookupAsync(domain, cancellationToken); + return await this.ResolveNoCacheAsync(domain, cancellationToken); } finally { @@ -90,12 +90,13 @@ namespace FastGithub.DomainResolve } /// - /// 查找ip + /// 解析域名 + /// 不使用缓存 /// /// /// /// - private async Task LookupAsync(DnsEndPoint domain, CancellationToken cancellationToken) + private async Task ResolveNoCacheAsync(DnsEndPoint domain, CancellationToken cancellationToken) { if (this.domainResolveCache.TryGetValue(domain, out var address)) { @@ -105,7 +106,7 @@ namespace FastGithub.DomainResolve var expiration = this.dnscryptExpiration; if (this.dnscryptProxy.LocalEndPoint != null) { - address = await this.LookupCoreAsync(this.dnscryptProxy.LocalEndPoint, domain, cancellationToken); + address = await this.LookupAsync(this.dnscryptProxy.LocalEndPoint, domain, cancellationToken); } if (address == null) @@ -140,7 +141,7 @@ namespace FastGithub.DomainResolve { foreach (var dns in this.fastGithubConfig.FallbackDns) { - var address = await this.LookupCoreAsync(dns, domain, cancellationToken); + var address = await this.LookupAsync(dns, domain, cancellationToken); if (address != null) { return address; @@ -149,7 +150,6 @@ namespace FastGithub.DomainResolve return default; } - /// /// 查找ip /// @@ -157,22 +157,33 @@ namespace FastGithub.DomainResolve /// /// /// - private async Task LookupCoreAsync(IPEndPoint dns, DnsEndPoint domain, CancellationToken cancellationToken) + private async Task LookupAsync(IPEndPoint dns, DnsEndPoint domain, CancellationToken cancellationToken) { - try + const int MAX_TRY_COUNT = 2; + for (var i = 0; i < MAX_TRY_COUNT; i++) { - var dnsClient = new DnsClient(dns); - using var timeoutTokenSource = new CancellationTokenSource(this.lookupTimeout); - using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token); - var addresses = await dnsClient.Lookup(domain.Host, RecordType.A, linkedTokenSource.Token); - addresses = addresses.Where(address => this.disableIPAddressCache.TryGetValue(address, out _) == false).ToList(); - return await this.FindFastValueAsync(addresses, domain.Port, cancellationToken); - } - catch (Exception ex) - { - this.logger.LogWarning($"dns({dns})无法解析{domain.Host}:{ex.Message}"); - return default; + try + { + var dnsClient = new DnsClient(dns); + using var timeoutTokenSource = new CancellationTokenSource(this.lookupTimeout); + using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token); + var addresses = await dnsClient.Lookup(domain.Host, RecordType.A, linkedTokenSource.Token); + addresses = addresses.Where(address => this.disableIPAddressCache.TryGetValue(address, out _) == false).ToList(); + return await this.FindFastValueAsync(addresses, domain.Port, cancellationToken); + } + catch (OperationCanceledException) + { + continue; + } + catch (Exception ex) + { + this.logger.LogWarning($"dns({dns})无法解析{domain.Host}:{ex.Message}"); + return default; + } } + + this.logger.LogWarning($"dns({dns})无法解析{domain.Host}"); + return default; } @@ -220,7 +231,7 @@ namespace FastGithub.DomainResolve } catch (OperationCanceledException) { - this.SetDisabled(address, TimeSpan.FromMilliseconds(1d)); + this.SetDisabled(address, TimeSpan.FromMilliseconds(2d)); return default; } catch (Exception)