增加dnscrypt-proxy初始化等待
This commit is contained in:
parent
bc3f49b076
commit
908d81be11
@ -6,6 +6,11 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\FastGithub.ReverseProxy\FastGithub.ReverseProxy.csproj" />
|
<PackageReference Include="DNS" Version="6.1.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\FastGithub.Core\FastGithub.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@ -13,6 +13,7 @@ namespace FastGithub.DnscryptProxy
|
|||||||
{
|
{
|
||||||
private readonly DnscryptProxyService dnscryptProxyService;
|
private readonly DnscryptProxyService dnscryptProxyService;
|
||||||
private readonly ILogger<DnscryptProxyHostedService> logger;
|
private readonly ILogger<DnscryptProxyHostedService> logger;
|
||||||
|
private readonly TimeSpan dnsOKTimeout = TimeSpan.FromSeconds(60d);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// DnscryptProxy后台服务
|
/// DnscryptProxy后台服务
|
||||||
@ -40,15 +41,16 @@ namespace FastGithub.DnscryptProxy
|
|||||||
this.logger.LogInformation($"{this.dnscryptProxyService}启动成功");
|
this.logger.LogInformation($"{this.dnscryptProxyService}启动成功");
|
||||||
|
|
||||||
// 监听意外退出
|
// 监听意外退出
|
||||||
var service = this.dnscryptProxyService.Process;
|
var process = this.dnscryptProxyService.Process;
|
||||||
if (service == null)
|
if (process == null)
|
||||||
{
|
{
|
||||||
this.OnProcessExit(null, new EventArgs());
|
this.OnProcessExit(null, new EventArgs());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
service.EnableRaisingEvents = true;
|
process.EnableRaisingEvents = true;
|
||||||
service.Exited += this.OnProcessExit;
|
process.Exited += this.OnProcessExit;
|
||||||
|
await this.WaitForDnsOKAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
@ -57,6 +59,31 @@ namespace FastGithub.DnscryptProxy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 等待dns服务初始化
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task WaitForDnsOKAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
this.logger.LogInformation($"{this.dnscryptProxyService}正在初始化");
|
||||||
|
using var timeoutTokenSource = new CancellationTokenSource(this.dnsOKTimeout);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var linkeTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
|
||||||
|
await this.dnscryptProxyService.WaitForDnsOKAsync(linkeTokenSource.Token);
|
||||||
|
this.logger.LogInformation($"{this.dnscryptProxyService}初始化完成");
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
if (timeoutTokenSource.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning($"{this.dnscryptProxyService}在{this.dnsOKTimeout.TotalSeconds}秒内未能初始化完成");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 进程退出时
|
/// 进程退出时
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -66,7 +93,7 @@ namespace FastGithub.DnscryptProxy
|
|||||||
{
|
{
|
||||||
if (this.dnscryptProxyService.ControllState != ControllState.Stopped)
|
if (this.dnscryptProxyService.ControllState != ControllState.Stopped)
|
||||||
{
|
{
|
||||||
this.logger.LogCritical($"{this.dnscryptProxyService}已停止运行,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。");
|
this.logger.LogCritical($"{this.dnscryptProxyService}已意外停止,{nameof(FastGithub)}将无法解析域名。你可以把配置文件的{nameof(FastGithubOptions.PureDns)}修改为其它可用的DNS以临时使用。");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
using System;
|
using DNS.Client;
|
||||||
|
using DNS.Protocol;
|
||||||
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -12,6 +14,7 @@ namespace FastGithub.DnscryptProxy
|
|||||||
sealed class DnscryptProxyService
|
sealed class DnscryptProxyService
|
||||||
{
|
{
|
||||||
private const string name = "dnscrypt-proxy";
|
private const string name = "dnscrypt-proxy";
|
||||||
|
private const string testDomain = "api.github.com";
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -62,6 +65,51 @@ namespace FastGithub.DnscryptProxy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 等待dns服务OK
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task WaitForDnsOKAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var process = this.Process;
|
||||||
|
if (process == null || process.HasExited || this.ControllState != ControllState.Started)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
using var processExitTokenSource = new CancellationTokenSource();
|
||||||
|
process.EnableRaisingEvents = true;
|
||||||
|
process.Exited += (s, e) => processExitTokenSource.Cancel();
|
||||||
|
|
||||||
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token);
|
||||||
|
await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 等待dns服务OK
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private async Task WaitForDnsOKCoreAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1d));
|
||||||
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
|
||||||
|
var dnsClient = new DnsClient(this.fastGithubConfig.PureDns);
|
||||||
|
await dnsClient.Lookup(testDomain, RecordType.A, linkedTokenSource.Token);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
cancellationToken.ThrowIfCancellationRequested();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 停止dnscrypt-proxy
|
/// 停止dnscrypt-proxy
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -5,10 +5,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="DNS" Version="6.1.0" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\FastGithub.Core\FastGithub.Core.csproj" />
|
<ProjectReference Include="..\FastGithub.Core\FastGithub.Core.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
@ -47,8 +47,6 @@ namespace FastGithub
|
|||||||
listen.UseHttps(https =>
|
listen.UseHttps(https =>
|
||||||
https.ServerCertificateSelector = (ctx, domain) =>
|
https.ServerCertificateSelector = (ctx, domain) =>
|
||||||
GetDomainCert(domain, caPublicCerPath, caPrivateKeyPath)));
|
GetDomainCert(domain, caPublicCerPath, caPrivateKeyPath)));
|
||||||
|
|
||||||
logger.LogInformation("https反向代理服务启动成功");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@ -32,24 +32,15 @@ namespace FastGithub.Upgrade
|
|||||||
/// <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)
|
||||||
{
|
|
||||||
var maxTryCount = 5;
|
|
||||||
for (var i = 1; i <= maxTryCount; i++)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromSeconds(2d), stoppingToken);
|
|
||||||
await this.upgradeService.UpgradeAsync(stoppingToken);
|
await this.upgradeService.UpgradeAsync(stoppingToken);
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
|
||||||
if (i == maxTryCount)
|
|
||||||
{
|
{
|
||||||
this.logger.LogWarning($"升级失败:{ex.Message}");
|
this.logger.LogWarning($"升级失败:{ex.Message}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user