From 249ff7a08168835e4b282e5d9ef041941a37c8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Tue, 28 Sep 2021 12:44:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=90=8E=E5=8F=B0=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.DomainResolve/DnscryptProxy.cs | 27 +++++-- .../DnscryptProxyHostedService.cs | 65 ---------------- .../DomainResolveHostedService.cs | 74 +++++++++++++++++++ FastGithub.DomainResolve/DomainResolver.cs | 12 +-- .../DomainSpeedTestHostedService.cs | 61 --------------- ...eedTestService.cs => DomainSpeedTester.cs} | 20 ++--- .../ServiceCollectionExtensions.cs | 5 +- 7 files changed, 111 insertions(+), 153 deletions(-) delete mode 100644 FastGithub.DomainResolve/DnscryptProxyHostedService.cs create mode 100644 FastGithub.DomainResolve/DomainResolveHostedService.cs delete mode 100644 FastGithub.DomainResolve/DomainSpeedTestHostedService.cs rename FastGithub.DomainResolve/{DomainSpeedTestService.cs => DomainSpeedTester.cs} (87%) diff --git a/FastGithub.DomainResolve/DnscryptProxy.cs b/FastGithub.DomainResolve/DnscryptProxy.cs index 7905e0d..e6cbea6 100644 --- a/FastGithub.DomainResolve/DnscryptProxy.cs +++ b/FastGithub.DomainResolve/DnscryptProxy.cs @@ -116,19 +116,30 @@ namespace FastGithub.DomainResolve /// /// 停止dnscrypt-proxy /// - public void Stop() + /// + public bool Stop() { - if (OperatingSystem.IsWindows()) + try { - StartDnscryptProxy("-service stop")?.WaitForExit(); - StartDnscryptProxy("-service uninstall")?.WaitForExit(); + if (OperatingSystem.IsWindows()) + { + StartDnscryptProxy("-service stop")?.WaitForExit(); + StartDnscryptProxy("-service uninstall")?.WaitForExit(); + } + if (this.process != null && this.process.HasExited == false) + { + this.process.Kill(); + } + return true; } - - if (this.process != null && this.process.HasExited == false) + catch (Exception) { - this.process.Kill(); + return false; + } + finally + { + this.LocalEndPoint = null; } - this.LocalEndPoint = null; } /// diff --git a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs b/FastGithub.DomainResolve/DnscryptProxyHostedService.cs deleted file mode 100644 index 219c068..0000000 --- a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs +++ /dev/null @@ -1,65 +0,0 @@ -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace FastGithub.DomainResolve -{ - /// - /// DnscryptProxy后台服务 - /// - sealed class DnscryptProxyHostedService : BackgroundService - { - private readonly ILogger logger; - private readonly DnscryptProxy dnscryptProxy; - - /// - /// DnscryptProxy后台服务 - /// - /// - /// - public DnscryptProxyHostedService( - DnscryptProxy dnscryptProxy, - ILogger logger) - { - this.dnscryptProxy = dnscryptProxy; - this.logger = logger; - } - - /// - /// 启动dnscrypt-proxy - /// - /// - /// - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - try - { - await this.dnscryptProxy.StartAsync(stoppingToken); - } - catch (Exception ex) - { - this.logger.LogWarning($"{this.dnscryptProxy}启动失败:{ex.Message}"); - } - } - - /// - /// 停止dnscrypt-proxy - /// - /// - /// - public override Task StopAsync(CancellationToken cancellationToken) - { - try - { - this.dnscryptProxy.Stop(); - } - catch (Exception ex) - { - this.logger.LogWarning($"{this.dnscryptProxy}停止失败:{ex.Message}"); - } - return base.StopAsync(cancellationToken); - } - } -} diff --git a/FastGithub.DomainResolve/DomainResolveHostedService.cs b/FastGithub.DomainResolve/DomainResolveHostedService.cs new file mode 100644 index 0000000..3e18641 --- /dev/null +++ b/FastGithub.DomainResolve/DomainResolveHostedService.cs @@ -0,0 +1,74 @@ +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace FastGithub.DomainResolve +{ + /// + /// 域名解析后台服务 + /// + sealed class DomainResolveHostedService : BackgroundService + { + private readonly DnscryptProxy dnscryptProxy; + private readonly DomainSpeedTester speedTester; + private readonly ILogger logger; + + private readonly TimeSpan speedTestDueTime = TimeSpan.FromSeconds(10d); + private readonly TimeSpan speedTestPeriod = TimeSpan.FromMinutes(2d); + + /// + /// 域名解析后台服务 + /// + /// + /// + /// + public DomainResolveHostedService( + DnscryptProxy dnscryptProxy, + DomainSpeedTester speedTester, + ILogger logger) + { + this.dnscryptProxy = dnscryptProxy; + this.speedTester = speedTester; + this.logger = logger; + } + + /// + /// 停止时 + /// + /// + /// + public override async Task StopAsync(CancellationToken cancellationToken) + { + await this.speedTester.SaveDomainsAsync(); + this.dnscryptProxy.Stop(); + await base.StopAsync(cancellationToken); + } + + /// + /// 后台任务 + /// + /// + /// + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + try + { + await this.dnscryptProxy.StartAsync(stoppingToken); + } + catch (Exception ex) + { + this.logger.LogWarning($"{this.dnscryptProxy}启动失败:{ex.Message}"); + } + + await Task.Delay(this.speedTestDueTime, stoppingToken); + await this.speedTester.LoadDomainsAsync(stoppingToken); + while (stoppingToken.IsCancellationRequested == false) + { + await this.speedTester.TestSpeedAsync(stoppingToken); + await Task.Delay(this.speedTestPeriod, stoppingToken); + } + } + } +} diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs index 236e1a4..c0374af 100644 --- a/FastGithub.DomainResolve/DomainResolver.cs +++ b/FastGithub.DomainResolve/DomainResolver.cs @@ -13,19 +13,19 @@ namespace FastGithub.DomainResolve sealed class DomainResolver : IDomainResolver { private readonly DnsClient dnsClient; - private readonly DomainSpeedTestService speedTestService; + private readonly DomainSpeedTester speedTester; /// /// 域名解析器 /// /// - /// + /// public DomainResolver( DnsClient dnsClient, - DomainSpeedTestService speedTestService) + DomainSpeedTester speedTester) { this.dnsClient = dnsClient; - this.speedTestService = speedTestService; + this.speedTester = speedTester; } /// @@ -51,7 +51,7 @@ namespace FastGithub.DomainResolve /// public async IAsyncEnumerable ResolveAllAsync(string domain, [EnumeratorCancellation] CancellationToken cancellationToken) { - var addresses = this.speedTestService.GetIPAddresses(domain); + var addresses = this.speedTester.GetIPAddresses(domain); if (addresses.Length > 0) { foreach (var address in addresses) @@ -61,7 +61,7 @@ namespace FastGithub.DomainResolve } else { - this.speedTestService.Add(domain); + this.speedTester.Add(domain); await foreach (var address in this.dnsClient.ResolveAsync(domain, cancellationToken)) { yield return address; diff --git a/FastGithub.DomainResolve/DomainSpeedTestHostedService.cs b/FastGithub.DomainResolve/DomainSpeedTestHostedService.cs deleted file mode 100644 index 6699073..0000000 --- a/FastGithub.DomainResolve/DomainSpeedTestHostedService.cs +++ /dev/null @@ -1,61 +0,0 @@ -using Microsoft.Extensions.Hosting; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace FastGithub.DomainResolve -{ - /// - /// 域名的IP测速后台服务 - /// - sealed class DomainSpeedTestHostedService : BackgroundService - { - private readonly DomainSpeedTestService speedTestService; - private readonly TimeSpan testDueTime = TimeSpan.FromMinutes(1d); - - /// - /// 域名的IP测速后台服务 - /// - /// - public DomainSpeedTestHostedService(DomainSpeedTestService speedTestService) - { - this.speedTestService = speedTestService; - } - - /// - /// 启动时 - /// - /// - /// - public override async Task StartAsync(CancellationToken cancellationToken) - { - await this.speedTestService.LoadDataAsync(cancellationToken); - await base.StartAsync(cancellationToken); - } - - /// - /// 停止时 - /// - /// - /// - public override async Task StopAsync(CancellationToken cancellationToken) - { - await this.speedTestService.SaveDataAsync(); - await base.StopAsync(cancellationToken); - } - - /// - /// 后台测速 - /// - /// - /// - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - while (stoppingToken.IsCancellationRequested == false) - { - await this.speedTestService.TestSpeedAsync(stoppingToken); - await Task.Delay(this.testDueTime, stoppingToken); - } - } - } -} diff --git a/FastGithub.DomainResolve/DomainSpeedTestService.cs b/FastGithub.DomainResolve/DomainSpeedTester.cs similarity index 87% rename from FastGithub.DomainResolve/DomainSpeedTestService.cs rename to FastGithub.DomainResolve/DomainSpeedTester.cs index c957ff2..743edf0 100644 --- a/FastGithub.DomainResolve/DomainSpeedTestService.cs +++ b/FastGithub.DomainResolve/DomainSpeedTester.cs @@ -13,9 +13,9 @@ namespace FastGithub.DomainResolve /// /// 域名的IP测速服务 /// - sealed class DomainSpeedTestService + sealed class DomainSpeedTester { - private const string DATA_FILE = "domains.json"; + private const string DOMAINS_JSON_FILE = "domains.json"; private readonly DnsClient dnsClient; private readonly object syncRoot = new(); @@ -25,7 +25,7 @@ namespace FastGithub.DomainResolve /// 域名的IP测速服务 /// /// - public DomainSpeedTestService(DnsClient dnsClient) + public DomainSpeedTester(DnsClient dnsClient) { this.dnsClient = dnsClient; } @@ -61,18 +61,18 @@ namespace FastGithub.DomainResolve } /// - /// 加载数据 + /// 加载域名数据 /// /// /// - public async Task LoadDataAsync(CancellationToken cancellationToken) + public async Task LoadDomainsAsync(CancellationToken cancellationToken) { - if (File.Exists(DATA_FILE) == false) + if (File.Exists(DOMAINS_JSON_FILE) == false) { return; } - using var fileStream = File.OpenRead(DATA_FILE); + using var fileStream = File.OpenRead(DOMAINS_JSON_FILE); var domains = await JsonSerializer.DeserializeAsync(fileStream, cancellationToken: cancellationToken); if (domains == null) { @@ -89,10 +89,10 @@ namespace FastGithub.DomainResolve } /// - /// 保存数据 + /// 保存域名数据 /// /// - public async Task SaveDataAsync() + public async Task SaveDomainsAsync() { var domains = this.domainIPAddressHashSet.Keys .Select(item => new DomainPattern(item)) @@ -102,7 +102,7 @@ namespace FastGithub.DomainResolve try { - using var fileStream = File.OpenWrite(DATA_FILE); + using var fileStream = File.OpenWrite(DOMAINS_JSON_FILE); await JsonSerializer.SerializeAsync(fileStream, domains, new JsonSerializerOptions { WriteIndented = true }); return true; } diff --git a/FastGithub.DomainResolve/ServiceCollectionExtensions.cs b/FastGithub.DomainResolve/ServiceCollectionExtensions.cs index cf646a5..35234a8 100644 --- a/FastGithub.DomainResolve/ServiceCollectionExtensions.cs +++ b/FastGithub.DomainResolve/ServiceCollectionExtensions.cs @@ -18,10 +18,9 @@ namespace FastGithub { services.TryAddSingleton(); services.TryAddSingleton(); - services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); - services.AddHostedService(); - services.AddHostedService(); + services.AddHostedService(); return services; } }