公开Process属性
This commit is contained in:
parent
09ee688ba0
commit
03fc545c6c
@ -9,9 +9,8 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// DnscryptProxy后台服务
|
/// DnscryptProxy后台服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
sealed class DnscryptProxyHostedService : BackgroundService
|
sealed class DnscryptProxyHostedService : IHostedService
|
||||||
{
|
{
|
||||||
private bool isStopped = false;
|
|
||||||
private readonly DnscryptProxyService dnscryptProxyService;
|
private readonly DnscryptProxyService dnscryptProxyService;
|
||||||
private readonly ILogger<DnscryptProxyHostedService> logger;
|
private readonly ILogger<DnscryptProxyHostedService> logger;
|
||||||
|
|
||||||
@ -33,7 +32,7 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -44,36 +43,33 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
{
|
{
|
||||||
this.logger.LogWarning($"{this.dnscryptProxyService}启动失败:{ex.Message}");
|
this.logger.LogWarning($"{this.dnscryptProxyService}启动失败:{ex.Message}");
|
||||||
}
|
}
|
||||||
|
|
||||||
await base.StartAsync(cancellationToken);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
///// <summary>
|
||||||
/// 后台监控
|
///// 后台监控
|
||||||
/// </summary>
|
///// </summary>
|
||||||
/// <param name="stoppingToken"></param>
|
///// <param name="stoppingToken"></param>
|
||||||
/// <returns></returns>
|
///// <returns></returns>
|
||||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
//protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
{
|
//{
|
||||||
await Task.Yield();
|
// await Task.Yield();
|
||||||
|
|
||||||
this.dnscryptProxyService.WaitForExit();
|
// this.dnscryptProxyService.Process?.WaitForExit();
|
||||||
if (this.isStopped == false)
|
// if (this.isStopped == false)
|
||||||
{
|
// {
|
||||||
this.logger.LogCritical($"{this.dnscryptProxyService}已停止运行,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。");
|
// this.logger.LogCritical($"{this.dnscryptProxyService}已停止运行,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。");
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 停止dnscrypt-proxy
|
/// 停止dnscrypt-proxy
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="cancellationToken"></param>
|
/// <param name="cancellationToken"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this.isStopped = true;
|
|
||||||
this.dnscryptProxyService.Stop();
|
this.dnscryptProxyService.Stop();
|
||||||
this.logger.LogInformation($"{this.dnscryptProxyService}已停止");
|
this.logger.LogInformation($"{this.dnscryptProxyService}已停止");
|
||||||
}
|
}
|
||||||
@ -81,7 +77,8 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
{
|
{
|
||||||
this.logger.LogWarning($"{this.dnscryptProxyService}停止失败:{ex.Message}");
|
this.logger.LogWarning($"{this.dnscryptProxyService}停止失败:{ex.Message}");
|
||||||
}
|
}
|
||||||
return base.StopAsync(cancellationToken);
|
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,12 +11,13 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
sealed class DnscryptProxyService
|
sealed class DnscryptProxyService
|
||||||
{
|
{
|
||||||
|
private const string name = "dnscrypt-proxy";
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取文件名
|
/// 获取相关进程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Name => "dnscrypt-proxy";
|
public Process? Process { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// DnscryptProxy服务
|
/// DnscryptProxy服务
|
||||||
@ -34,22 +35,23 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var tomlPath = $"{this.Name}.toml";
|
var tomlPath = $"{name}.toml";
|
||||||
await TomlUtil.SetListensAsync(tomlPath, this.fastGithubConfig.PureDns, cancellationToken);
|
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();
|
process.Kill();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (OperatingSystem.IsWindows())
|
if (OperatingSystem.IsWindows())
|
||||||
{
|
{
|
||||||
this.StartDnscryptProxy("-service install", waitForExit: true);
|
StartDnscryptProxy("-service install")?.WaitForExit();
|
||||||
this.StartDnscryptProxy("-service start", waitForExit: true);
|
StartDnscryptProxy("-service start")?.WaitForExit();
|
||||||
|
this.Process = Process.GetProcessesByName(name).FirstOrDefault(item => item.SessionId == 0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
this.StartDnscryptProxy(string.Empty, waitForExit: false);
|
this.Process = StartDnscryptProxy(string.Empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,25 +62,13 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
{
|
{
|
||||||
if (OperatingSystem.IsWindows())
|
if (OperatingSystem.IsWindows())
|
||||||
{
|
{
|
||||||
this.StartDnscryptProxy("-service stop", waitForExit: true);
|
StartDnscryptProxy("-service stop")?.WaitForExit();
|
||||||
this.StartDnscryptProxy("-service uninstall", waitForExit: true);
|
StartDnscryptProxy("-service uninstall")?.WaitForExit();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var process in Process.GetProcessesByName(this.Name))
|
if (this.Process != null && this.Process.HasExited == false)
|
||||||
{
|
{
|
||||||
process.Kill();
|
this.Process.Kill();
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 等待退出
|
|
||||||
/// </summary>
|
|
||||||
public void WaitForExit()
|
|
||||||
{
|
|
||||||
var process = Process.GetProcessesByName(this.Name).FirstOrDefault();
|
|
||||||
if (process != null)
|
|
||||||
{
|
|
||||||
process.WaitForExit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,22 +76,16 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// 启动DnscryptProxy进程
|
/// 启动DnscryptProxy进程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="arguments"></param>
|
/// <param name="arguments"></param>
|
||||||
/// <param name="waitForExit"></param>
|
private static Process? StartDnscryptProxy(string arguments)
|
||||||
private void StartDnscryptProxy(string arguments, bool waitForExit)
|
|
||||||
{
|
{
|
||||||
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,
|
Arguments = arguments,
|
||||||
UseShellExecute = true,
|
UseShellExecute = true,
|
||||||
CreateNoWindow = true,
|
CreateNoWindow = true,
|
||||||
WindowStyle = ProcessWindowStyle.Hidden
|
WindowStyle = ProcessWindowStyle.Hidden
|
||||||
});
|
});
|
||||||
|
|
||||||
if (waitForExit && process != null)
|
|
||||||
{
|
|
||||||
process.WaitForExit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -110,7 +94,7 @@ namespace FastGithub.Dns.DnscryptProxy
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return this.Name;
|
return name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user