diff --git a/FastGithub.Dns/FastGithub.Dns.csproj b/FastGithub.Dns/FastGithub.Dns.csproj
index 65ae28c..2ebcc65 100644
--- a/FastGithub.Dns/FastGithub.Dns.csproj
+++ b/FastGithub.Dns/FastGithub.Dns.csproj
@@ -3,9 +3,14 @@
net5.0
true
-
-
+
+
-
+
+
+
+
+
+
diff --git a/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs b/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs
index 977425b..4661748 100644
--- a/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs
+++ b/FastGithub.DnscryptProxy/DnscryptProxyHostedService.cs
@@ -13,6 +13,7 @@ namespace FastGithub.DnscryptProxy
{
private readonly DnscryptProxyService dnscryptProxyService;
private readonly ILogger logger;
+ private readonly TimeSpan dnsOKTimeout = TimeSpan.FromSeconds(60d);
///
/// 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
}
}
+ ///
+ /// 等待dns服务初始化
+ ///
+ ///
+ ///
+ 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}秒内未能初始化完成");
+ }
+ }
+ }
+
///
/// 进程退出时
///
@@ -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以临时使用。");
}
}
diff --git a/FastGithub.DnscryptProxy/DnscryptProxyService.cs b/FastGithub.DnscryptProxy/DnscryptProxyService.cs
index b45229b..47b93e6 100644
--- a/FastGithub.DnscryptProxy/DnscryptProxyService.cs
+++ b/FastGithub.DnscryptProxy/DnscryptProxyService.cs
@@ -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;
///
@@ -62,6 +65,51 @@ namespace FastGithub.DnscryptProxy
}
}
+ ///
+ /// 等待dns服务OK
+ ///
+ ///
+ ///
+ 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);
+ }
+
+ ///
+ /// 等待dns服务OK
+ ///
+ ///
+ ///
+ 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();
+ }
+ }
+ }
+
///
/// 停止dnscrypt-proxy
///
diff --git a/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj b/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj
index c8b98b2..f4f3881 100644
--- a/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj
+++ b/FastGithub.DnscryptProxy/FastGithub.DnscryptProxy.csproj
@@ -5,11 +5,9 @@
+
-
-
-
-
+
diff --git a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
index 67a4c5e..aab7947 100644
--- a/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
+++ b/FastGithub.ReverseProxy/KestrelServerOptionsExtensions.cs
@@ -47,8 +47,6 @@ namespace FastGithub
listen.UseHttps(https =>
https.ServerCertificateSelector = (ctx, domain) =>
GetDomainCert(domain, caPublicCerPath, caPrivateKeyPath)));
-
- logger.LogInformation("https反向代理服务启动成功");
}
///
diff --git a/FastGithub.Upgrade/UpgradeHostedService.cs b/FastGithub.Upgrade/UpgradeHostedService.cs
index ad97f32..35fa681 100644
--- a/FastGithub.Upgrade/UpgradeHostedService.cs
+++ b/FastGithub.Upgrade/UpgradeHostedService.cs
@@ -33,22 +33,13 @@ namespace FastGithub.Upgrade
///
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}");
}
}
}