diff --git a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs index f5b34b0..8106f45 100644 --- a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs +++ b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs @@ -9,9 +9,8 @@ namespace FastGithub.Dns.DnscryptProxy /// /// DnscryptProxy后台服务 /// - sealed class DnscryptProxyHostedService : BackgroundService + sealed class DnscryptProxyHostedService : IHostedService { - private bool isStopped = false; private readonly DnscryptProxyService dnscryptProxyService; private readonly ILogger logger; @@ -33,7 +32,7 @@ namespace FastGithub.Dns.DnscryptProxy /// /// /// - public override async Task StartAsync(CancellationToken cancellationToken) + public async Task StartAsync(CancellationToken cancellationToken) { try { @@ -44,36 +43,33 @@ namespace FastGithub.Dns.DnscryptProxy { this.logger.LogWarning($"{this.dnscryptProxyService}启动失败:{ex.Message}"); } - - await base.StartAsync(cancellationToken); } - /// - /// 后台监控 - /// - /// - /// - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - await Task.Yield(); + ///// + ///// 后台监控 + ///// + ///// + ///// + //protected override async Task ExecuteAsync(CancellationToken stoppingToken) + //{ + // await Task.Yield(); - this.dnscryptProxyService.WaitForExit(); - if (this.isStopped == false) - { - this.logger.LogCritical($"{this.dnscryptProxyService}已停止运行,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。"); - } - } + // this.dnscryptProxyService.Process?.WaitForExit(); + // if (this.isStopped == false) + // { + // this.logger.LogCritical($"{this.dnscryptProxyService}已停止运行,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。"); + // } + //} /// /// 停止dnscrypt-proxy /// /// /// - public override Task StopAsync(CancellationToken cancellationToken) + public Task StopAsync(CancellationToken cancellationToken) { try { - this.isStopped = true; this.dnscryptProxyService.Stop(); this.logger.LogInformation($"{this.dnscryptProxyService}已停止"); } @@ -81,7 +77,8 @@ namespace FastGithub.Dns.DnscryptProxy { this.logger.LogWarning($"{this.dnscryptProxyService}停止失败:{ex.Message}"); } - return base.StopAsync(cancellationToken); + + return Task.CompletedTask; } } } diff --git a/FastGithub.Dns.DnscryptProxy/DnscryptProxyService.cs b/FastGithub.Dns.DnscryptProxy/DnscryptProxyService.cs index d6be7d6..ac20b6a 100644 --- a/FastGithub.Dns.DnscryptProxy/DnscryptProxyService.cs +++ b/FastGithub.Dns.DnscryptProxy/DnscryptProxyService.cs @@ -11,12 +11,13 @@ namespace FastGithub.Dns.DnscryptProxy /// sealed class DnscryptProxyService { + private const string name = "dnscrypt-proxy"; private readonly FastGithubConfig fastGithubConfig; /// - /// 获取文件名 + /// 获取相关进程 /// - public string Name => "dnscrypt-proxy"; + public Process? Process { get; private set; } /// /// DnscryptProxy服务 @@ -34,22 +35,23 @@ namespace FastGithub.Dns.DnscryptProxy /// public async Task StartAsync(CancellationToken cancellationToken) { - var tomlPath = $"{this.Name}.toml"; + var tomlPath = $"{name}.toml"; await TomlUtil.SetListensAsync(tomlPath, this.fastGithubConfig.PureDns, cancellationToken); - foreach (var process in Process.GetProcessesByName(this.Name)) + foreach (var process in Process.GetProcessesByName(name)) { process.Kill(); } if (OperatingSystem.IsWindows()) { - this.StartDnscryptProxy("-service install", waitForExit: true); - this.StartDnscryptProxy("-service start", waitForExit: true); + StartDnscryptProxy("-service install")?.WaitForExit(); + StartDnscryptProxy("-service start")?.WaitForExit(); + this.Process = Process.GetProcessesByName(name).FirstOrDefault(item => item.SessionId == 0); } else { - this.StartDnscryptProxy(string.Empty, waitForExit: false); + this.Process = StartDnscryptProxy(string.Empty); } } @@ -60,48 +62,30 @@ namespace FastGithub.Dns.DnscryptProxy { if (OperatingSystem.IsWindows()) { - this.StartDnscryptProxy("-service stop", waitForExit: true); - this.StartDnscryptProxy("-service uninstall", waitForExit: true); + StartDnscryptProxy("-service stop")?.WaitForExit(); + StartDnscryptProxy("-service uninstall")?.WaitForExit(); } - foreach (var process in Process.GetProcessesByName(this.Name)) + if (this.Process != null && this.Process.HasExited == false) { - process.Kill(); - } - } - - /// - /// 等待退出 - /// - public void WaitForExit() - { - var process = Process.GetProcessesByName(this.Name).FirstOrDefault(); - if (process != null) - { - process.WaitForExit(); + this.Process.Kill(); } } /// /// 启动DnscryptProxy进程 /// - /// - /// - private void StartDnscryptProxy(string arguments, bool waitForExit) + /// + private static Process? StartDnscryptProxy(string arguments) { - var process = Process.Start(new ProcessStartInfo + return Process.Start(new ProcessStartInfo { - FileName = OperatingSystem.IsWindows() ? $"{this.Name}.exe" : this.Name, + FileName = OperatingSystem.IsWindows() ? $"{name}.exe" : name, Arguments = arguments, UseShellExecute = true, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden }); - - if (waitForExit && process != null) - { - process.WaitForExit(); - } } /// @@ -110,7 +94,7 @@ namespace FastGithub.Dns.DnscryptProxy /// public override string ToString() { - return this.Name; + return name; } } }