优化结束逻辑
This commit is contained in:
parent
32cfe8e56f
commit
2882615bad
@ -12,9 +12,8 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
sealed class DnscryptProxyHostedService : IHostedService
|
sealed class DnscryptProxyHostedService : IHostedService
|
||||||
{
|
{
|
||||||
private const string dnscryptFile = "dnscrypt-proxy";
|
private const string dnscryptProxyFile = "dnscrypt-proxy";
|
||||||
private readonly ILogger<DnscryptProxyHostedService> logger;
|
private readonly ILogger<DnscryptProxyHostedService> logger;
|
||||||
private Process? dnscryptProcess;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// DnscryptProxy后台服务
|
/// DnscryptProxy后台服务
|
||||||
@ -34,20 +33,20 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (OperatingSystem.IsWindows() && Process.GetCurrentProcess().SessionId == 0)
|
if (OperatingSystem.IsWindows())
|
||||||
{
|
{
|
||||||
StartDnscrypt("-service install")?.WaitForExit();
|
StartDnscryptProxy("-service install", waitForExit: true);
|
||||||
StartDnscrypt("-service start")?.WaitForExit();
|
StartDnscryptProxy("-service start", waitForExit: true);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.dnscryptProcess = StartDnscrypt(string.Empty);
|
StartDnscryptProxy(string.Empty, waitForExit: false);
|
||||||
}
|
}
|
||||||
this.logger.LogInformation($"{dnscryptFile}启动成功");
|
this.logger.LogInformation($"{dnscryptProxyFile}启动成功");
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this.logger.LogWarning($"{dnscryptFile}启动失败:{ex.Message}");
|
this.logger.LogWarning($"{dnscryptProxyFile}启动失败:{ex.Message}");
|
||||||
}
|
}
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
@ -59,34 +58,48 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
if (this.dnscryptProcess != null)
|
try
|
||||||
{
|
{
|
||||||
this.dnscryptProcess.Kill();
|
if (OperatingSystem.IsWindows())
|
||||||
this.logger.LogInformation($"{dnscryptFile}已停止");
|
{
|
||||||
|
StartDnscryptProxy("-service stop", waitForExit: true);
|
||||||
|
StartDnscryptProxy("-service uninstall", waitForExit: true);
|
||||||
}
|
}
|
||||||
else if (OperatingSystem.IsWindows())
|
|
||||||
|
foreach (var process in Process.GetProcessesByName(dnscryptProxyFile))
|
||||||
{
|
{
|
||||||
StartDnscrypt("-service stop")?.WaitForExit();
|
process.Kill();
|
||||||
StartDnscrypt("-service uninstall")?.WaitForExit();
|
}
|
||||||
this.logger.LogInformation($"{dnscryptFile}已停止");
|
this.logger.LogInformation($"{dnscryptProxyFile}已停止");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning($"{dnscryptProxyFile}停止失败:{ex.Message}");
|
||||||
}
|
}
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 启动Dnscrypt
|
/// 启动DnscryptProxy进程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="arguments"></param>
|
/// <param name="arguments"></param>
|
||||||
private static Process? StartDnscrypt(string arguments)
|
/// <param name="waitForExit"></param>
|
||||||
|
private static void StartDnscryptProxy(string arguments, bool waitForExit)
|
||||||
{
|
{
|
||||||
return Process.Start(new ProcessStartInfo
|
var process = Process.Start(new ProcessStartInfo
|
||||||
{
|
{
|
||||||
FileName = OperatingSystem.IsWindows() ? $"{dnscryptFile}.exe" : dnscryptFile,
|
FileName = OperatingSystem.IsWindows() ? $"{dnscryptProxyFile}.exe" : dnscryptProxyFile,
|
||||||
Arguments = arguments,
|
Arguments = arguments,
|
||||||
UseShellExecute = true,
|
UseShellExecute = true,
|
||||||
CreateNoWindow = true,
|
CreateNoWindow = true,
|
||||||
WindowStyle = ProcessWindowStyle.Hidden
|
WindowStyle = ProcessWindowStyle.Hidden
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (waitForExit && process != null)
|
||||||
|
{
|
||||||
|
process.WaitForExit();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user