From 2b9586ff7e3478b5f49fd427873e4c2d14de2c34 Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sat, 24 Jul 2021 02:08:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B0=83=E7=94=A8=E4=BA=86?= =?UTF-8?q?=E5=B7=B2Dispose=E7=9A=84processExitTokenSource=E9=97=AE?= =?UTF-8?q?=E9=A2=98;=20=E5=B0=9D=E8=AF=95=E4=BD=BF=E7=94=A8=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E5=8F=B0=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=EF=BC=9B?= =?UTF-8?q?=20=E4=BF=AE=E6=94=B9dns=E9=99=8D=E7=BA=A7=E6=8F=90=E7=A4=BA?= =?UTF-8?q?=E8=AF=AD=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DnscryptProxyService.cs | 18 +++++- FastGithub.ReverseProxy/DomainResolver.cs | 58 ++++++++++++------- FastGithub/Program.cs | 1 + 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/FastGithub.DnscryptProxy/DnscryptProxyService.cs b/FastGithub.DnscryptProxy/DnscryptProxyService.cs index 47b93e6..70dd036 100644 --- a/FastGithub.DnscryptProxy/DnscryptProxyService.cs +++ b/FastGithub.DnscryptProxy/DnscryptProxyService.cs @@ -80,10 +80,22 @@ namespace FastGithub.DnscryptProxy using var processExitTokenSource = new CancellationTokenSource(); process.EnableRaisingEvents = true; - process.Exited += (s, e) => processExitTokenSource.Cancel(); + process.Exited += OnProcessExited; - using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token); - await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token); + try + { + using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token); + await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token); + } + finally + { + process.Exited -= OnProcessExited; + } + + void OnProcessExited(object? sender, EventArgs eventArgs) + { + processExitTokenSource.Cancel(); + } } /// diff --git a/FastGithub.ReverseProxy/DomainResolver.cs b/FastGithub.ReverseProxy/DomainResolver.cs index c1df3f1..2da0c6b 100644 --- a/FastGithub.ReverseProxy/DomainResolver.cs +++ b/FastGithub.ReverseProxy/DomainResolver.cs @@ -1,4 +1,5 @@ using DNS.Client; +using DNS.Protocol; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Logging; using System; @@ -61,31 +62,48 @@ namespace FastGithub.ReverseProxy /// private async Task LookupAsync(string domain, CancellationToken cancellationToken) { + var pureDns = this.fastGithubConfig.PureDns; + var fastDns = this.fastGithubConfig.FastDns; + 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) - { - throw new Exception($"解析不到{domain}的ip"); - } - - // 受干扰的dns,常常返回127.0.0.1来阻断请求 - // 虽然DnscryptProxy的抗干扰能力,但它仍然可能降级到不安全的普通dns上游 - if (address.Equals(IPAddress.Loopback)) - { - throw new Exception($"dns被污染,解析{domain}为{address}"); - } - - this.logger.LogInformation($"[{domain}->{address}]"); - return address; + return await LookupCoreAsync(pureDns, domain, cancellationToken); } - catch (Exception ex) + catch (Exception) { - var dns = this.fastGithubConfig.PureDns; - throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex); + this.logger.LogWarning($"由于{pureDns}解析{domain}失败,本次使用{fastDns}"); + return await LookupCoreAsync(fastDns, domain, cancellationToken); } } + + /// + /// 查找ip + /// + /// + /// + /// + /// + private async Task LookupCoreAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken) + { + var dnsClient = new DnsClient(dns); + using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1d)); + using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token); + + var addresses = await dnsClient.Lookup(domain, RecordType.A, linkedTokenSource.Token); + var address = addresses?.FirstOrDefault(); + if (address == null) + { + throw new FastGithubException($"dns{dns}解析不到{domain}的ip"); + } + + // 受干扰的dns,常常返回127.0.0.1来阻断请求 + if (address.Equals(IPAddress.Loopback)) + { + throw new FastGithubException($"dns{dns}被污染,解析{domain}为{address}"); + } + + this.logger.LogInformation($"[{domain}->{address}]"); + return address; + } } } diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index d001c4b..02c5330 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -26,6 +26,7 @@ namespace FastGithub { return Host .CreateDefaultBuilder(args) + .UseConsoleLifetime() .UseWindowsService() .UseBinaryPathContentRoot() .UseDefaultServiceProvider(c =>