简化DnscryptProxy

This commit is contained in:
xljiulang 2021-07-30 01:26:57 +08:00
parent 359ac01033
commit 678bdea512
4 changed files with 40 additions and 84 deletions

View File

@ -1,23 +0,0 @@
namespace FastGithub.DomainResolve
{
/// <summary>
/// 服务控制状态
/// </summary>
enum ControllState
{
/// <summary>
/// 未控制
/// </summary>
None,
/// <summary>
/// 控制启动
/// </summary>
Started,
/// <summary>
/// 控制停止
/// </summary>
Stopped,
}
}

View File

@ -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
/// <summary>
/// DnscryptProxy服务
/// </summary>
sealed class DnscryptProxyService
sealed class DnscryptProxy
{
private const string name = "dnscrypt-proxy";
private readonly FastGithubConfig fastGithubConfig;
/// <summary>
/// 获取相关进程
/// 相关进程
/// </summary>
public Process? Process { get; private set; }
private Process? process;
/// <summary>
/// 获取服务控制状态
/// 获取监听的节点
/// </summary>
public ControllState ControllState { get; private set; } = ControllState.None;
public IPEndPoint EndPoint { get; }
/// <summary>
/// DnscryptProxy服务
/// </summary>
/// <param name="fastGithubConfig"></param>
public DnscryptProxyService(FastGithubConfig fastGithubConfig)
/// <param name="endPoint">监听的节点</param>
public DnscryptProxy(IPEndPoint endPoint)
{
this.fastGithubConfig = fastGithubConfig;
this.EndPoint = endPoint;
}
/// <summary>
@ -41,10 +40,8 @@ namespace FastGithub.DomainResolve
/// <returns></returns>
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
/// </summary>
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();
}
}

View File

@ -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
/// </summary>
sealed class DnscryptProxyHostedService : IHostedService
{
private readonly DnscryptProxyService dnscryptProxyService;
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DnscryptProxyHostedService> logger;
private DnscryptProxy? dnscryptProxy;
/// <summary>
/// DnscryptProxy后台服务
/// </summary>
/// <param name="dnscryptProxyService"></param>
/// <param name="fastGithubConfig"></param>
/// <param name="logger"></param>
public DnscryptProxyHostedService(
DnscryptProxyService dnscryptProxyService,
FastGithubConfig fastGithubConfig,
ILogger<DnscryptProxyHostedService> logger)
{
this.dnscryptProxyService = dnscryptProxyService;
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;
}
@ -34,40 +36,20 @@ namespace FastGithub.DomainResolve
/// <returns></returns>
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}");
}
}
/// <summary>
/// 进程退出时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnProcessExit(object? sender, EventArgs e)
{
if (this.dnscryptProxyService.ControllState != ControllState.Stopped)
{
this.logger.LogCritical($"{this.dnscryptProxyService}已意外停止,这将影响到{nameof(FastGithub)}解析域名的速度和准确性。");
}
}
/// <summary>
@ -77,14 +59,17 @@ namespace FastGithub.DomainResolve
/// <returns></returns>
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;

View File

@ -18,7 +18,6 @@ namespace FastGithub
{
services.AddMemoryCache();
services.TryAddSingleton<IDomainResolver, DomainResolver>();
services.TryAddSingleton<DnscryptProxyService>();
return services.AddHostedService<DnscryptProxyHostedService>();
}
}