From 5576c602083f58fcef493148eeebdf1dcd8123e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com> Date: Wed, 21 Jul 2021 12:49:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A5windows=E6=9C=8D=E5=8A=A1=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E4=B9=9F=E4=BB=A5=E6=9C=8D=E5=8A=A1=E8=BF=90=E8=A1=8C?= =?UTF-8?q?dnscrypt-proxy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DnscryptProxyHostedService.cs | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs index 8dfe005..b16591f 100644 --- a/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs +++ b/FastGithub.Dns.DnscryptProxy/DnscryptProxyHostedService.cs @@ -2,7 +2,6 @@ using Microsoft.Extensions.Logging; using System; using System.Diagnostics; -using System.IO; using System.Threading; using System.Threading.Tasks; @@ -35,23 +34,16 @@ namespace FastGithub.Dns.DnscryptProxy { try { - var fileName = dnscryptFile; - if (OperatingSystem.IsWindows()) + if (OperatingSystem.IsWindows() && Process.GetCurrentProcess().SessionId == 0) { - fileName = $"{dnscryptFile}.exe"; + StartDnscrypt("-service install")?.WaitForExit(); + StartDnscrypt("-service start")?.WaitForExit(); } - - if (File.Exists(fileName) == true) + else { - this.dnscryptProcess = Process.Start(new ProcessStartInfo - { - FileName = fileName, - UseShellExecute = true, - CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden - }); - this.logger.LogInformation($"{dnscryptFile}启动成功"); + this.dnscryptProcess = StartDnscrypt(string.Empty); } + this.logger.LogInformation($"{dnscryptFile}启动成功"); } catch (Exception ex) { @@ -72,7 +64,29 @@ namespace FastGithub.Dns.DnscryptProxy this.dnscryptProcess.Kill(); this.logger.LogInformation($"{dnscryptFile}已停止"); } + else if (OperatingSystem.IsWindows()) + { + StartDnscrypt("-service stop")?.WaitForExit(); + StartDnscrypt("-service uninstall")?.WaitForExit(); + this.logger.LogInformation($"{dnscryptFile}已停止"); + } return Task.CompletedTask; } + + /// + /// 启动Dnscrypt + /// + /// + private static Process? StartDnscrypt(string arguments) + { + return Process.Start(new ProcessStartInfo + { + FileName = OperatingSystem.IsWindows() ? $"{dnscryptFile}.exe" : dnscryptFile, + Arguments = arguments, + UseShellExecute = true, + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden + }); + } } }