增加dnscrypt-proxy初始化等待
This commit is contained in:
parent
bc3f49b076
commit
908d81be11
@ -6,6 +6,11 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<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>
|
||||
</Project>
|
||||
|
||||
@ -13,6 +13,7 @@ namespace FastGithub.DnscryptProxy
|
||||
{
|
||||
private readonly DnscryptProxyService dnscryptProxyService;
|
||||
private readonly ILogger<DnscryptProxyHostedService> logger;
|
||||
private readonly TimeSpan dnsOKTimeout = TimeSpan.FromSeconds(60d);
|
||||
|
||||
/// <summary>
|
||||
/// DnscryptProxy后台服务
|
||||
@ -40,15 +41,16 @@ namespace FastGithub.DnscryptProxy
|
||||
this.logger.LogInformation($"{this.dnscryptProxyService}启动成功");
|
||||
|
||||
// 监听意外退出
|
||||
var service = this.dnscryptProxyService.Process;
|
||||
if (service == null)
|
||||
var process = this.dnscryptProxyService.Process;
|
||||
if (process == null)
|
||||
{
|
||||
this.OnProcessExit(null, new EventArgs());
|
||||
}
|
||||
else
|
||||
{
|
||||
service.EnableRaisingEvents = true;
|
||||
service.Exited += this.OnProcessExit;
|
||||
process.EnableRaisingEvents = true;
|
||||
process.Exited += this.OnProcessExit;
|
||||
await this.WaitForDnsOKAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
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>
|
||||
@ -66,7 +93,7 @@ namespace FastGithub.DnscryptProxy
|
||||
{
|
||||
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.Linq;
|
||||
using System.Threading;
|
||||
@ -12,6 +14,7 @@ namespace FastGithub.DnscryptProxy
|
||||
sealed class DnscryptProxyService
|
||||
{
|
||||
private const string name = "dnscrypt-proxy";
|
||||
private const string testDomain = "api.github.com";
|
||||
private readonly FastGithubConfig fastGithubConfig;
|
||||
|
||||
/// <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>
|
||||
/// 停止dnscrypt-proxy
|
||||
/// </summary>
|
||||
|
||||
@ -5,11 +5,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<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" />
|
||||
<ProjectReference Include="..\FastGithub.Core\FastGithub.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -47,8 +47,6 @@ namespace FastGithub
|
||||
listen.UseHttps(https =>
|
||||
https.ServerCertificateSelector = (ctx, domain) =>
|
||||
GetDomainCert(domain, caPublicCerPath, caPrivateKeyPath)));
|
||||
|
||||
logger.LogInformation("https反向代理服务启动成功");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -33,22 +33,13 @@ namespace FastGithub.Upgrade
|
||||
/// <returns></returns>
|
||||
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);
|
||||
break;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (i == maxTryCount)
|
||||
{
|
||||
this.logger.LogWarning($"升级失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
await this.upgradeService.UpgradeAsync(stoppingToken);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogWarning($"升级失败:{ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user