简化DnscryptProxy
This commit is contained in:
parent
359ac01033
commit
678bdea512
@ -1,23 +0,0 @@
|
|||||||
namespace FastGithub.DomainResolve
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 服务控制状态
|
|
||||||
/// </summary>
|
|
||||||
enum ControllState
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// 未控制
|
|
||||||
/// </summary>
|
|
||||||
None,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 控制启动
|
|
||||||
/// </summary>
|
|
||||||
Started,
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 控制停止
|
|
||||||
/// </summary>
|
|
||||||
Stopped,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
using FastGithub.Configuration;
|
using System;
|
||||||
using System;
|
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -10,28 +10,27 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// DnscryptProxy服务
|
/// DnscryptProxy服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
sealed class DnscryptProxyService
|
sealed class DnscryptProxy
|
||||||
{
|
{
|
||||||
private const string name = "dnscrypt-proxy";
|
private const string name = "dnscrypt-proxy";
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取相关进程
|
/// 相关进程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Process? Process { get; private set; }
|
private Process? process;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取服务控制状态
|
/// 获取监听的节点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ControllState ControllState { get; private set; } = ControllState.None;
|
public IPEndPoint EndPoint { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// DnscryptProxy服务
|
/// DnscryptProxy服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="fastGithubConfig"></param>
|
/// <param name="endPoint">监听的节点</param>
|
||||||
public DnscryptProxyService(FastGithubConfig fastGithubConfig)
|
public DnscryptProxy(IPEndPoint endPoint)
|
||||||
{
|
{
|
||||||
this.fastGithubConfig = fastGithubConfig;
|
this.EndPoint = endPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -41,10 +40,8 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
this.ControllState = ControllState.Started;
|
|
||||||
|
|
||||||
var tomlPath = $"{name}.toml";
|
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))
|
foreach (var process in Process.GetProcessesByName(name))
|
||||||
{
|
{
|
||||||
@ -57,11 +54,11 @@ namespace FastGithub.DomainResolve
|
|||||||
StartDnscryptProxy("-service uninstall")?.WaitForExit();
|
StartDnscryptProxy("-service uninstall")?.WaitForExit();
|
||||||
StartDnscryptProxy("-service install")?.WaitForExit();
|
StartDnscryptProxy("-service install")?.WaitForExit();
|
||||||
StartDnscryptProxy("-service start")?.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
|
else
|
||||||
{
|
{
|
||||||
this.Process = StartDnscryptProxy(string.Empty);
|
this.process = StartDnscryptProxy(string.Empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,17 +68,15 @@ namespace FastGithub.DomainResolve
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Stop()
|
public void Stop()
|
||||||
{
|
{
|
||||||
this.ControllState = ControllState.Stopped;
|
|
||||||
|
|
||||||
if (OperatingSystem.IsWindows())
|
if (OperatingSystem.IsWindows())
|
||||||
{
|
{
|
||||||
StartDnscryptProxy("-service stop")?.WaitForExit();
|
StartDnscryptProxy("-service stop")?.WaitForExit();
|
||||||
StartDnscryptProxy("-service uninstall")?.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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.Extensions.Hosting;
|
using FastGithub.Configuration;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -11,19 +12,20 @@ namespace FastGithub.DomainResolve
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
sealed class DnscryptProxyHostedService : IHostedService
|
sealed class DnscryptProxyHostedService : IHostedService
|
||||||
{
|
{
|
||||||
private readonly DnscryptProxyService dnscryptProxyService;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
private readonly ILogger<DnscryptProxyHostedService> logger;
|
private readonly ILogger<DnscryptProxyHostedService> logger;
|
||||||
|
private DnscryptProxy? dnscryptProxy;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// DnscryptProxy后台服务
|
/// DnscryptProxy后台服务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dnscryptProxyService"></param>
|
/// <param name="fastGithubConfig"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public DnscryptProxyHostedService(
|
public DnscryptProxyHostedService(
|
||||||
DnscryptProxyService dnscryptProxyService,
|
FastGithubConfig fastGithubConfig,
|
||||||
ILogger<DnscryptProxyHostedService> logger)
|
ILogger<DnscryptProxyHostedService> logger)
|
||||||
{
|
{
|
||||||
this.dnscryptProxyService = dnscryptProxyService;
|
this.fastGithubConfig = fastGithubConfig;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -34,40 +36,20 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
var pureDns = this.fastGithubConfig.PureDns;
|
||||||
|
if (LocalMachine.ContainsIPAddress(pureDns.Address) == true)
|
||||||
{
|
{
|
||||||
await this.dnscryptProxyService.StartAsync(cancellationToken);
|
this.dnscryptProxy = new DnscryptProxy(pureDns);
|
||||||
this.logger.LogInformation($"{this.dnscryptProxyService}启动成功");
|
try
|
||||||
|
|
||||||
// 监听意外退出
|
|
||||||
var process = this.dnscryptProxyService.Process;
|
|
||||||
if (process == null)
|
|
||||||
{
|
{
|
||||||
this.OnProcessExit(null, new EventArgs());
|
await this.dnscryptProxy.StartAsync(cancellationToken);
|
||||||
|
this.logger.LogInformation($"{this.dnscryptProxy}启动成功");
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
process.EnableRaisingEvents = true;
|
this.logger.LogWarning($"{this.dnscryptProxy}启动失败:{ex.Message}");
|
||||||
process.Exited += this.OnProcessExit;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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>
|
/// <summary>
|
||||||
@ -77,14 +59,17 @@ namespace FastGithub.DomainResolve
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public Task StopAsync(CancellationToken cancellationToken)
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
if (this.dnscryptProxy != null)
|
||||||
{
|
{
|
||||||
this.dnscryptProxyService.Stop();
|
try
|
||||||
this.logger.LogInformation($"{this.dnscryptProxyService}已停止");
|
{
|
||||||
}
|
this.dnscryptProxy.Stop();
|
||||||
catch (Exception ex)
|
this.logger.LogInformation($"{this.dnscryptProxy}已停止");
|
||||||
{
|
}
|
||||||
this.logger.LogWarning($"{this.dnscryptProxyService}停止失败:{ex.Message}");
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning($"{this.dnscryptProxy}停止失败:{ex.Message}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
|
|||||||
@ -18,7 +18,6 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
services.AddMemoryCache();
|
services.AddMemoryCache();
|
||||||
services.TryAddSingleton<IDomainResolver, DomainResolver>();
|
services.TryAddSingleton<IDomainResolver, DomainResolver>();
|
||||||
services.TryAddSingleton<DnscryptProxyService>();
|
|
||||||
return services.AddHostedService<DnscryptProxyHostedService>();
|
return services.AddHostedService<DnscryptProxyHostedService>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user