修复调用了已Dispose的processExitTokenSource问题;

尝试使用控制台生命周期;
修改dns降级提示语;
This commit is contained in:
xljiulang 2021-07-24 02:08:33 +08:00
parent 908d81be11
commit 2b9586ff7e
3 changed files with 54 additions and 23 deletions

View File

@ -80,10 +80,22 @@ namespace FastGithub.DnscryptProxy
using var processExitTokenSource = new CancellationTokenSource(); using var processExitTokenSource = new CancellationTokenSource();
process.EnableRaisingEvents = true; process.EnableRaisingEvents = true;
process.Exited += (s, e) => processExitTokenSource.Cancel(); process.Exited += OnProcessExited;
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token); try
await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token); {
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token);
await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token);
}
finally
{
process.Exited -= OnProcessExited;
}
void OnProcessExited(object? sender, EventArgs eventArgs)
{
processExitTokenSource.Cancel();
}
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,5 @@
using DNS.Client; using DNS.Client;
using DNS.Protocol;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
@ -61,31 +62,48 @@ namespace FastGithub.ReverseProxy
/// <exception cref="FastGithubException"> /// <exception cref="FastGithubException">
private async Task<IPAddress> LookupAsync(string domain, CancellationToken cancellationToken) private async Task<IPAddress> LookupAsync(string domain, CancellationToken cancellationToken)
{ {
var pureDns = this.fastGithubConfig.PureDns;
var fastDns = this.fastGithubConfig.FastDns;
try try
{ {
var dnsClient = new DnsClient(this.fastGithubConfig.PureDns); return await LookupCoreAsync(pureDns, domain, cancellationToken);
var addresses = await dnsClient.Lookup(domain, DNS.Protocol.RecordType.A, cancellationToken);
var address = addresses?.FirstOrDefault();
if (address == null)
{
throw new Exception($"解析不到{domain}的ip");
}
// 受干扰的dns常常返回127.0.0.1来阻断请求
// 虽然DnscryptProxy的抗干扰能力但它仍然可能降级到不安全的普通dns上游
if (address.Equals(IPAddress.Loopback))
{
throw new Exception($"dns被污染解析{domain}为{address}");
}
this.logger.LogInformation($"[{domain}->{address}]");
return address;
} }
catch (Exception ex) catch (Exception)
{ {
var dns = this.fastGithubConfig.PureDns; this.logger.LogWarning($"由于{pureDns}解析{domain}失败,本次使用{fastDns}");
throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex); return await LookupCoreAsync(fastDns, domain, cancellationToken);
} }
} }
/// <summary>
/// 查找ip
/// </summary>
/// <param name="dns"></param>
/// <param name="domain"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task<IPAddress> LookupCoreAsync(IPEndPoint dns, string domain, CancellationToken cancellationToken)
{
var dnsClient = new DnsClient(dns);
using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1d));
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
var addresses = await dnsClient.Lookup(domain, RecordType.A, linkedTokenSource.Token);
var address = addresses?.FirstOrDefault();
if (address == null)
{
throw new FastGithubException($"dns{dns}解析不到{domain}的ip");
}
// 受干扰的dns常常返回127.0.0.1来阻断请求
if (address.Equals(IPAddress.Loopback))
{
throw new FastGithubException($"dns{dns}被污染,解析{domain}为{address}");
}
this.logger.LogInformation($"[{domain}->{address}]");
return address;
}
} }
} }

View File

@ -26,6 +26,7 @@ namespace FastGithub
{ {
return Host return Host
.CreateDefaultBuilder(args) .CreateDefaultBuilder(args)
.UseConsoleLifetime()
.UseWindowsService() .UseWindowsService()
.UseBinaryPathContentRoot() .UseBinaryPathContentRoot()
.UseDefaultServiceProvider(c => .UseDefaultServiceProvider(c =>