From 12d9598a3e1b2b669f99a5cbf4e63d3b09690561 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 17:03:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=BF=9D=E5=AD=98=E5=9F=9F?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub.DomainResolve/DnscryptProxy.cs | 64 ++++++++----------- .../DomainResolveHostedService.cs | 13 +++- FastGithub.DomainResolve/DomainSpeedTester.cs | 62 ++++++++---------- 3 files changed, 68 insertions(+), 71 deletions(-) diff --git a/FastGithub.DomainResolve/DnscryptProxy.cs b/FastGithub.DomainResolve/DnscryptProxy.cs index d7784ba..e00dc0e 100644 --- a/FastGithub.DomainResolve/DnscryptProxy.cs +++ b/FastGithub.DomainResolve/DnscryptProxy.cs @@ -16,7 +16,7 @@ namespace FastGithub.DomainResolve /// /// DnscryptProxy服务 /// - sealed class DnscryptProxy : IDisposable + sealed class DnscryptProxy { private const string PATH = "dnscrypt-proxy"; private const string NAME = "dnscrypt-proxy"; @@ -99,6 +99,33 @@ namespace FastGithub.DomainResolve } } + /// + /// 停止服务 + /// + public void Stop() + { + try + { + if (OperatingSystem.IsWindows()) + { + StartDnscryptProxy("-service stop")?.WaitForExit(); + StartDnscryptProxy("-service uninstall")?.WaitForExit(); + } + if (this.process != null && this.process.HasExited == false) + { + this.process.Kill(); + } + } + catch (Exception ex) + { + this.logger.LogWarning($"{NAME}停止失败:{ex.Message }"); + } + finally + { + this.LocalEndPoint = null; + } + } + /// /// 获取可用的随机端口 /// @@ -158,41 +185,6 @@ namespace FastGithub.DomainResolve }); } - /// - /// 释放资源 - /// - public void Dispose() - { - try - { - this.Stop(); - } - catch (Exception ex) - { - this.logger.LogWarning($"{NAME}停止失败:{ex.Message }"); - } - finally - { - this.LocalEndPoint = null; - } - } - - /// - /// 停止服务 - /// - private void Stop() - { - if (OperatingSystem.IsWindows()) - { - StartDnscryptProxy("-service stop")?.WaitForExit(); - StartDnscryptProxy("-service uninstall")?.WaitForExit(); - } - if (this.process != null && this.process.HasExited == false) - { - this.process.Kill(); - } - } - /// /// 转换为字符串 /// diff --git a/FastGithub.DomainResolve/DomainResolveHostedService.cs b/FastGithub.DomainResolve/DomainResolveHostedService.cs index bed0acd..ba2a4df 100644 --- a/FastGithub.DomainResolve/DomainResolveHostedService.cs +++ b/FastGithub.DomainResolve/DomainResolveHostedService.cs @@ -27,7 +27,7 @@ namespace FastGithub.DomainResolve { this.dnscryptProxy = dnscryptProxy; this.speedTester = speedTester; - } + } /// /// 后台任务 @@ -45,5 +45,16 @@ namespace FastGithub.DomainResolve await Task.Delay(this.speedTestPeriod, stoppingToken); } } + + /// + /// 停止服务 + /// + /// + /// + public override Task StopAsync(CancellationToken cancellationToken) + { + this.dnscryptProxy.Stop(); + return base.StopAsync(cancellationToken); + } } } diff --git a/FastGithub.DomainResolve/DomainSpeedTester.cs b/FastGithub.DomainResolve/DomainSpeedTester.cs index a300530..b832bcd 100644 --- a/FastGithub.DomainResolve/DomainSpeedTester.cs +++ b/FastGithub.DomainResolve/DomainSpeedTester.cs @@ -14,7 +14,7 @@ namespace FastGithub.DomainResolve /// /// 域名的IP测速服务 /// - sealed class DomainSpeedTester : IDisposable + sealed class DomainSpeedTester { private const string DOMAINS_JSON_FILE = "domains.json"; @@ -73,15 +73,39 @@ namespace FastGithub.DomainResolve /// 添加要测速的域名 /// /// - /// - public bool Add(string domain) + public void Add(string domain) { lock (this.syncRoot) { - return this.domainIPAddressHashSet.TryAdd(domain, new IPAddressItemHashSet()); + if (this.domainIPAddressHashSet.TryAdd(domain, new IPAddressItemHashSet())) + { + try + { + this.SaveDomains(); + } + catch (Exception ex) + { + logger.LogWarning($"保存域名数据失败:{ex.Message}"); + } + } } } + /// + /// 保存域名 + /// + private void SaveDomains() + { + var domains = this.domainIPAddressHashSet.Keys + .Select(item => new DomainPattern(item)) + .OrderBy(item => item) + .Select(item => item.ToString()) + .ToArray(); + + var utf8Json = JsonSerializer.SerializeToUtf8Bytes(domains, new JsonSerializerOptions { WriteIndented = true }); + File.WriteAllBytes(DOMAINS_JSON_FILE, utf8Json); + } + /// /// 获取测试后排序的IP /// @@ -123,35 +147,5 @@ namespace FastGithub.DomainResolve await hashSet.PingAllAsync(); } } - - /// - /// 释放资源 - /// - public void Dispose() - { - try - { - this.SaveDomains(); - } - catch (Exception ex) - { - this.logger.LogWarning($"保存域名数据失败:{ex.Message}"); - } - } - - /// - /// 保存域名 - /// - private void SaveDomains() - { - var domains = this.domainIPAddressHashSet.Keys - .Select(item => new DomainPattern(item)) - .OrderBy(item => item) - .Select(item => item.ToString()) - .ToArray(); - - var utf8Json = JsonSerializer.SerializeToUtf8Bytes(domains, new JsonSerializerOptions { WriteIndented = true }); - File.WriteAllBytes(DOMAINS_JSON_FILE, utf8Json); - } } }