diff --git a/FastGithub.DomainResolve/ControllState.cs b/FastGithub.DomainResolve/ControllState.cs deleted file mode 100644 index 877c70e..0000000 --- a/FastGithub.DomainResolve/ControllState.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace FastGithub.DomainResolve -{ - /// - /// 服务控制状态 - /// - enum ControllState - { - /// - /// 未控制 - /// - None, - - /// - /// 控制启动 - /// - Started, - - /// - /// 控制停止 - /// - Stopped, - } -} diff --git a/FastGithub.DomainResolve/DnscryptProxyService.cs b/FastGithub.DomainResolve/DnscryptProxy.cs similarity index 71% rename from FastGithub.DomainResolve/DnscryptProxyService.cs rename to FastGithub.DomainResolve/DnscryptProxy.cs index c6a3cab..311e9cd 100644 --- a/FastGithub.DomainResolve/DnscryptProxyService.cs +++ b/FastGithub.DomainResolve/DnscryptProxy.cs @@ -1,7 +1,7 @@ -using FastGithub.Configuration; -using System; +using System; using System.Diagnostics; using System.Linq; +using System.Net; using System.Threading; using System.Threading.Tasks; @@ -10,28 +10,27 @@ namespace FastGithub.DomainResolve /// /// DnscryptProxy服务 /// - sealed class DnscryptProxyService + sealed class DnscryptProxy { private const string name = "dnscrypt-proxy"; - private readonly FastGithubConfig fastGithubConfig; /// - /// 获取相关进程 + /// 相关进程 /// - public Process? Process { get; private set; } + private Process? process; /// - /// 获取服务控制状态 + /// 获取监听的节点 /// - public ControllState ControllState { get; private set; } = ControllState.None; + public IPEndPoint EndPoint { get; } /// /// DnscryptProxy服务 /// - /// - public DnscryptProxyService(FastGithubConfig fastGithubConfig) + /// 监听的节点 + public DnscryptProxy(IPEndPoint endPoint) { - this.fastGithubConfig = fastGithubConfig; + this.EndPoint = endPoint; } /// @@ -41,10 +40,8 @@ namespace FastGithub.DomainResolve /// public async Task StartAsync(CancellationToken cancellationToken) { - this.ControllState = ControllState.Started; - var tomlPath = $"{name}.toml"; - await TomlUtil.SetListensAsync(tomlPath, this.fastGithubConfig.PureDns, cancellationToken); + await TomlUtil.SetListensAsync(tomlPath, this.EndPoint, cancellationToken); foreach (var process in Process.GetProcessesByName(name)) { @@ -57,11 +54,11 @@ namespace FastGithub.DomainResolve StartDnscryptProxy("-service uninstall")?.WaitForExit(); StartDnscryptProxy("-service install")?.WaitForExit(); StartDnscryptProxy("-service start")?.WaitForExit(); - this.Process = Process.GetProcessesByName(name).FirstOrDefault(item => item.SessionId == 0); + this.process = Process.GetProcessesByName(name).FirstOrDefault(item => item.SessionId == 0); } else { - this.Process = StartDnscryptProxy(string.Empty); + this.process = StartDnscryptProxy(string.Empty); } } @@ -71,17 +68,15 @@ namespace FastGithub.DomainResolve /// public void Stop() { - this.ControllState = ControllState.Stopped; - if (OperatingSystem.IsWindows()) { StartDnscryptProxy("-service stop")?.WaitForExit(); StartDnscryptProxy("-service uninstall")?.WaitForExit(); } - if (this.Process != null && this.Process.HasExited == false) + if (this.process != null && this.process.HasExited == false) { - this.Process.Kill(); + this.process.Kill(); } } diff --git a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs b/FastGithub.DomainResolve/DnscryptProxyHostedService.cs index b1e1630..135164b 100644 --- a/FastGithub.DomainResolve/DnscryptProxyHostedService.cs +++ b/FastGithub.DomainResolve/DnscryptProxyHostedService.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Hosting; +using FastGithub.Configuration; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Threading; @@ -11,19 +12,20 @@ namespace FastGithub.DomainResolve /// sealed class DnscryptProxyHostedService : IHostedService { - private readonly DnscryptProxyService dnscryptProxyService; + private readonly FastGithubConfig fastGithubConfig; private readonly ILogger logger; + private DnscryptProxy? dnscryptProxy; /// /// DnscryptProxy后台服务 /// - /// + /// /// public DnscryptProxyHostedService( - DnscryptProxyService dnscryptProxyService, + FastGithubConfig fastGithubConfig, ILogger logger) { - this.dnscryptProxyService = dnscryptProxyService; + this.fastGithubConfig = fastGithubConfig; this.logger = logger; } @@ -34,40 +36,20 @@ namespace FastGithub.DomainResolve /// public async Task StartAsync(CancellationToken cancellationToken) { - try + var pureDns = this.fastGithubConfig.PureDns; + if (LocalMachine.ContainsIPAddress(pureDns.Address) == true) { - await this.dnscryptProxyService.StartAsync(cancellationToken); - this.logger.LogInformation($"{this.dnscryptProxyService}启动成功"); - - // 监听意外退出 - var process = this.dnscryptProxyService.Process; - if (process == null) + this.dnscryptProxy = new DnscryptProxy(pureDns); + try { - this.OnProcessExit(null, new EventArgs()); + await this.dnscryptProxy.StartAsync(cancellationToken); + this.logger.LogInformation($"{this.dnscryptProxy}启动成功"); } - else + catch (Exception ex) { - process.EnableRaisingEvents = true; - process.Exited += this.OnProcessExit; + this.logger.LogWarning($"{this.dnscryptProxy}启动失败:{ex.Message}"); } } - catch (Exception ex) - { - this.logger.LogWarning($"{this.dnscryptProxyService}启动失败:{ex.Message}"); - } - } - - /// - /// 进程退出时 - /// - /// - /// - private void OnProcessExit(object? sender, EventArgs e) - { - if (this.dnscryptProxyService.ControllState != ControllState.Stopped) - { - this.logger.LogCritical($"{this.dnscryptProxyService}已意外停止,这将影响到{nameof(FastGithub)}解析域名的速度和准确性。"); - } } /// @@ -77,14 +59,17 @@ namespace FastGithub.DomainResolve /// public Task StopAsync(CancellationToken cancellationToken) { - try + if (this.dnscryptProxy != null) { - this.dnscryptProxyService.Stop(); - this.logger.LogInformation($"{this.dnscryptProxyService}已停止"); - } - catch (Exception ex) - { - this.logger.LogWarning($"{this.dnscryptProxyService}停止失败:{ex.Message}"); + try + { + this.dnscryptProxy.Stop(); + this.logger.LogInformation($"{this.dnscryptProxy}已停止"); + } + catch (Exception ex) + { + this.logger.LogWarning($"{this.dnscryptProxy}停止失败:{ex.Message}"); + } } return Task.CompletedTask; diff --git a/FastGithub.DomainResolve/ServiceCollectionExtensions.cs b/FastGithub.DomainResolve/ServiceCollectionExtensions.cs index 2f49da7..1244eb4 100644 --- a/FastGithub.DomainResolve/ServiceCollectionExtensions.cs +++ b/FastGithub.DomainResolve/ServiceCollectionExtensions.cs @@ -18,7 +18,6 @@ namespace FastGithub { services.AddMemoryCache(); services.TryAddSingleton(); - services.TryAddSingleton(); return services.AddHostedService(); } }