修复调用了已Dispose的processExitTokenSource问题;
尝试使用控制台生命周期; 修改dns降级提示语;
This commit is contained in:
parent
908d81be11
commit
2b9586ff7e
@ -80,11 +80,23 @@ 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;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token);
|
using var linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, processExitTokenSource.Token);
|
||||||
await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token);
|
await this.WaitForDnsOKCoreAsync(linkedTokenSource.Token);
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
process.Exited -= OnProcessExited;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnProcessExited(object? sender, EventArgs eventArgs)
|
||||||
|
{
|
||||||
|
processExitTokenSource.Cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 等待dns服务OK
|
/// 等待dns服务OK
|
||||||
|
|||||||
@ -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);
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning($"由于{pureDns}解析{domain}失败,本次使用{fastDns}");
|
||||||
|
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();
|
var address = addresses?.FirstOrDefault();
|
||||||
if (address == null)
|
if (address == null)
|
||||||
{
|
{
|
||||||
throw new Exception($"解析不到{domain}的ip");
|
throw new FastGithubException($"dns{dns}解析不到{domain}的ip");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 受干扰的dns,常常返回127.0.0.1来阻断请求
|
// 受干扰的dns,常常返回127.0.0.1来阻断请求
|
||||||
// 虽然DnscryptProxy的抗干扰能力,但它仍然可能降级到不安全的普通dns上游
|
|
||||||
if (address.Equals(IPAddress.Loopback))
|
if (address.Equals(IPAddress.Loopback))
|
||||||
{
|
{
|
||||||
throw new Exception($"dns被污染,解析{domain}为{address}");
|
throw new FastGithubException($"dns{dns}被污染,解析{domain}为{address}");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.LogInformation($"[{domain}->{address}]");
|
this.logger.LogInformation($"[{domain}->{address}]");
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
var dns = this.fastGithubConfig.PureDns;
|
|
||||||
throw new FastGithubException($"dns({dns})服务器异常:{ex.Message}", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,7 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
return Host
|
return Host
|
||||||
.CreateDefaultBuilder(args)
|
.CreateDefaultBuilder(args)
|
||||||
|
.UseConsoleLifetime()
|
||||||
.UseWindowsService()
|
.UseWindowsService()
|
||||||
.UseBinaryPathContentRoot()
|
.UseBinaryPathContentRoot()
|
||||||
.UseDefaultServiceProvider(c =>
|
.UseDefaultServiceProvider(c =>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user