From ec06b11d7e910b6cad6319af2b212b996230842c Mon Sep 17 00:00:00 2001 From: xljiulang <366193849@qq.com> Date: Sat, 24 Jul 2021 02:45:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=90=AF=E5=8A=A8=E7=AD=89?= =?UTF-8?q?=E5=BE=85DnscryptProxy=EF=BC=8C=E5=9B=A0=E4=B8=BA=E5=BD=B1?= =?UTF-8?q?=E5=93=8D=E5=88=B0windows=E6=9C=8D=E5=8A=A1=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DnscryptProxyHostedService.cs | 27 -------- .../DnscryptProxyService.cs | 61 +------------------ .../FastGithub.DnscryptProxy.csproj | 1 - FastGithub.ReverseProxy/DomainResolver.cs | 2 +- FastGithub/Program.cs | 1 - 5 files changed, 2 insertions(+), 90 deletions(-) diff --git a/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs b/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs index c29cccc..94db659 100644 --- a/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs +++ b/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs @@ -13,7 +13,6 @@ namespace FastGithub.DnscryptProxy { private readonly DnscryptProxyService dnscryptProxyService; private readonly ILogger logger; - private readonly TimeSpan dnsOKTimeout = TimeSpan.FromSeconds(60d); /// /// DnscryptProxy后台服务 @@ -50,7 +49,6 @@ namespace FastGithub.DnscryptProxy { process.EnableRaisingEvents = true; process.Exited += this.OnProcessExit; - await this.WaitForDnsOKAsync(cancellationToken); } } catch (Exception ex) @@ -59,31 +57,6 @@ namespace FastGithub.DnscryptProxy } } - /// - /// 等待dns服务初始化 - /// - /// - /// - private async Task WaitForDnsOKAsync(CancellationToken cancellationToken) - { - this.logger.LogInformation($"{this.dnscryptProxyService}正在初始化"); - using var timeoutTokenSource = new CancellationTokenSource(this.dnsOKTimeout); - - try - { - using var linkeTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token); - await this.dnscryptProxyService.WaitForDnsOKAsync(linkeTokenSource.Token); - this.logger.LogInformation($"{this.dnscryptProxyService}初始化完成"); - } - catch (Exception) - { - if (timeoutTokenSource.IsCancellationRequested) - { - this.logger.LogWarning($"{this.dnscryptProxyService}在{this.dnsOKTimeout.TotalSeconds}秒内未能初始化完成"); - } - } - } - /// /// 进程退出时 /// diff --git a/FastGithub.DnscryptProxy/DnscryptProxyService.cs b/FastGithub.DnscryptProxy/DnscryptProxyService.cs index 70dd036..320933a 100644 --- a/FastGithub.DnscryptProxy/DnscryptProxyService.cs +++ b/FastGithub.DnscryptProxy/DnscryptProxyService.cs @@ -1,6 +1,4 @@ -using DNS.Client; -using DNS.Protocol; -using System; +using System; using System.Diagnostics; using System.Linq; using System.Threading; @@ -14,7 +12,6 @@ namespace FastGithub.DnscryptProxy sealed class DnscryptProxyService { private const string name = "dnscrypt-proxy"; - private const string testDomain = "api.github.com"; private readonly FastGithubConfig fastGithubConfig; /// @@ -65,62 +62,6 @@ namespace FastGithub.DnscryptProxy } } - /// - /// 等待dns服务OK - /// - /// - /// - public async Task WaitForDnsOKAsync(CancellationToken cancellationToken) - { - var process = this.Process; - if (process == null || process.HasExited || this.ControllState != ControllState.Started) - { - return; - } - - using var processExitTokenSource = new CancellationTokenSource(); - process.EnableRaisingEvents = true; - process.Exited += OnProcessExited; - - 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(); - } - } - - /// - /// 等待dns服务OK - /// - /// - /// - private async Task WaitForDnsOKCoreAsync(CancellationToken cancellationToken) - { - while (true) - { - try - { - using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1d)); - using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token); - var dnsClient = new DnsClient(this.fastGithubConfig.PureDns); - await dnsClient.Lookup(testDomain, RecordType.A, linkedTokenSource.Token); - break; - } - catch (Exception) - { - cancellationToken.ThrowIfCancellationRequested(); - } - } - } /// /// 停止dnscrypt-proxy diff --git a/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj b/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj index f4f3881..d8cfc6c 100644 --- a/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj +++ b/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj @@ -5,7 +5,6 @@ - diff --git a/FastGithub.ReverseProxy/DomainResolver.cs b/FastGithub.ReverseProxy/DomainResolver.cs index 3ab07aa..2b40391 100644 --- a/FastGithub.ReverseProxy/DomainResolver.cs +++ b/FastGithub.ReverseProxy/DomainResolver.cs @@ -71,7 +71,7 @@ namespace FastGithub.ReverseProxy } catch (Exception) { - this.logger.LogWarning($"由于{pureDns}解析{domain}失败,本次使用{fastDns}"); + this.logger.LogTrace($"由于{pureDns}解析{domain}失败,本次使用{fastDns}"); return await LookupCoreAsync(fastDns, domain, cancellationToken); } } diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index 02c5330..d001c4b 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -26,7 +26,6 @@ namespace FastGithub { return Host .CreateDefaultBuilder(args) - .UseConsoleLifetime() .UseWindowsService() .UseBinaryPathContentRoot() .UseDefaultServiceProvider(c =>