修改日志
This commit is contained in:
parent
8df865b6e7
commit
4b807991e5
@ -128,13 +128,13 @@ namespace FastGithub.DomainResolve
|
|||||||
}
|
}
|
||||||
catch (OperationCanceledException)
|
catch (OperationCanceledException)
|
||||||
{
|
{
|
||||||
this.logger.LogWarning($"dns://{dns}无法解析{endPoint.Host}:请求超时");
|
this.logger.LogWarning($"{endPoint.Host}@{dns}:请求超时。");
|
||||||
return Array.Empty<IPAddress>();
|
return Array.Empty<IPAddress>();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this.logger.LogWarning($"dns://{dns}无法解析{endPoint.Host}:{ex.Message}");
|
this.logger.LogWarning($"{endPoint.Host}@{dns}:{ex.Message}");
|
||||||
return this.dnsCache.Set(key, Array.Empty<IPAddress>(), this.maxTimeToLive);
|
return Array.Empty<IPAddress>();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
|
|||||||
116
FastGithub.DomainResolve/DomainPersistence.cs
Normal file
116
FastGithub.DomainResolve/DomainPersistence.cs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
using FastGithub.Configuration;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace FastGithub.DomainResolve
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 域名持久化
|
||||||
|
/// </summary>
|
||||||
|
sealed class DomainPersistence
|
||||||
|
{
|
||||||
|
private static readonly string dataFile = "dnsendpoints.json";
|
||||||
|
private static readonly SemaphoreSlim dataLocker = new(1, 1);
|
||||||
|
private static readonly JsonSerializerOptions jsonOptions = new()
|
||||||
|
{
|
||||||
|
WriteIndented = true,
|
||||||
|
PropertyNameCaseInsensitive = true,
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||||
|
};
|
||||||
|
|
||||||
|
private readonly FastGithubConfig fastGithubConfig;
|
||||||
|
private readonly ILogger<DomainPersistence> logger;
|
||||||
|
private record EndPointItem(string Host, int Port);
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 域名持久化
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fastGithubConfig"></param>
|
||||||
|
/// <param name="logger"></param>
|
||||||
|
public DomainPersistence(
|
||||||
|
FastGithubConfig fastGithubConfig,
|
||||||
|
ILogger<DomainPersistence> logger)
|
||||||
|
{
|
||||||
|
this.fastGithubConfig = fastGithubConfig;
|
||||||
|
this.logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 读取保存的节点
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public IList<DnsEndPoint> ReadDnsEndPoints()
|
||||||
|
{
|
||||||
|
if (File.Exists(dataFile) == false)
|
||||||
|
{
|
||||||
|
return Array.Empty<DnsEndPoint>();
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
dataLocker.Wait();
|
||||||
|
|
||||||
|
var utf8Json = File.ReadAllBytes(dataFile);
|
||||||
|
var endPointItems = JsonSerializer.Deserialize<EndPointItem[]>(utf8Json, jsonOptions);
|
||||||
|
if (endPointItems == null)
|
||||||
|
{
|
||||||
|
return Array.Empty<DnsEndPoint>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var dnsEndPoints = new List<DnsEndPoint>();
|
||||||
|
foreach (var item in endPointItems)
|
||||||
|
{
|
||||||
|
if (this.fastGithubConfig.IsMatch(item.Host) == true)
|
||||||
|
{
|
||||||
|
dnsEndPoints.Add(new DnsEndPoint(item.Host, item.Port));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dnsEndPoints;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning(ex.Message, "读取dns记录异常");
|
||||||
|
return Array.Empty<DnsEndPoint>();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
dataLocker.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 保存节点到文件
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dnsEndPoints"></param>
|
||||||
|
/// <param name="cancellationToken"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task WriteDnsEndPointsAsync(IEnumerable<DnsEndPoint> dnsEndPoints, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await dataLocker.WaitAsync(CancellationToken.None);
|
||||||
|
|
||||||
|
var endPointItems = dnsEndPoints.Select(item => new EndPointItem(item.Host, item.Port)).ToArray();
|
||||||
|
var utf8Json = JsonSerializer.SerializeToUtf8Bytes(endPointItems, jsonOptions);
|
||||||
|
await File.WriteAllBytesAsync(dataFile, utf8Json, cancellationToken);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
this.logger.LogWarning(ex.Message, "保存dns记录异常");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
dataLocker.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,11 +3,9 @@ using Microsoft.Extensions.Logging;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
using System.Text.Json;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -19,112 +17,32 @@ namespace FastGithub.DomainResolve
|
|||||||
sealed class DomainResolver : IDomainResolver
|
sealed class DomainResolver : IDomainResolver
|
||||||
{
|
{
|
||||||
private readonly DnsClient dnsClient;
|
private readonly DnsClient dnsClient;
|
||||||
private readonly FastGithubConfig fastGithubConfig;
|
private readonly DomainPersistence persistence;
|
||||||
private readonly ILogger<DomainResolver> logger;
|
private readonly ILogger<DomainResolver> logger;
|
||||||
private record EndPointItem(string Host, int Port);
|
|
||||||
private readonly ConcurrentDictionary<DnsEndPoint, IPAddressElapsedCollection> dnsEndPointAddressElapseds = new();
|
private readonly ConcurrentDictionary<DnsEndPoint, IPAddressElapsedCollection> dnsEndPointAddressElapseds = new();
|
||||||
|
|
||||||
private static readonly string dnsEndpointFile = "dnsendpoints.json";
|
|
||||||
private static readonly SemaphoreSlim dnsEndpointLocker = new(1, 1);
|
|
||||||
private static readonly JsonSerializerOptions jsonOptions = new()
|
|
||||||
{
|
|
||||||
WriteIndented = true,
|
|
||||||
PropertyNameCaseInsensitive = true,
|
|
||||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
||||||
};
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 域名解析器
|
/// 域名解析器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dnsClient"></param>
|
/// <param name="dnsClient"></param>
|
||||||
/// <param name="fastGithubConfig"></param>
|
/// <param name="persistence"></param>
|
||||||
/// <param name="logger"></param>
|
/// <param name="logger"></param>
|
||||||
public DomainResolver(
|
public DomainResolver(
|
||||||
DnsClient dnsClient,
|
DnsClient dnsClient,
|
||||||
FastGithubConfig fastGithubConfig,
|
DomainPersistence persistence,
|
||||||
ILogger<DomainResolver> logger)
|
ILogger<DomainResolver> logger)
|
||||||
{
|
{
|
||||||
this.dnsClient = dnsClient;
|
this.dnsClient = dnsClient;
|
||||||
this.fastGithubConfig = fastGithubConfig;
|
this.persistence = persistence;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
|
|
||||||
foreach (var endPoint in this.ReadDnsEndPoints())
|
foreach (var endPoint in persistence.ReadDnsEndPoints())
|
||||||
{
|
{
|
||||||
this.dnsEndPointAddressElapseds.TryAdd(endPoint, IPAddressElapsedCollection.Empty);
|
this.dnsEndPointAddressElapseds.TryAdd(endPoint, IPAddressElapsedCollection.Empty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 读取保存的节点
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
private IList<DnsEndPoint> ReadDnsEndPoints()
|
|
||||||
{
|
|
||||||
if (File.Exists(dnsEndpointFile) == false)
|
|
||||||
{
|
|
||||||
return Array.Empty<DnsEndPoint>();
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
dnsEndpointLocker.Wait();
|
|
||||||
|
|
||||||
var utf8Json = File.ReadAllBytes(dnsEndpointFile);
|
|
||||||
var endPointItems = JsonSerializer.Deserialize<EndPointItem[]>(utf8Json, jsonOptions);
|
|
||||||
if (endPointItems == null)
|
|
||||||
{
|
|
||||||
return Array.Empty<DnsEndPoint>();
|
|
||||||
}
|
|
||||||
|
|
||||||
var dnsEndPoints = new List<DnsEndPoint>();
|
|
||||||
foreach (var item in endPointItems)
|
|
||||||
{
|
|
||||||
if (this.fastGithubConfig.IsMatch(item.Host) == true)
|
|
||||||
{
|
|
||||||
dnsEndPoints.Add(new DnsEndPoint(item.Host, item.Port));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return dnsEndPoints;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
this.logger.LogWarning(ex.Message, "读取dns记录异常");
|
|
||||||
return Array.Empty<DnsEndPoint>();
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
dnsEndpointLocker.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 保存节点到文件
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="dnsEndPoints"></param>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private async Task WriteDnsEndPointsAsync(IEnumerable<DnsEndPoint> dnsEndPoints, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await dnsEndpointLocker.WaitAsync(CancellationToken.None);
|
|
||||||
|
|
||||||
var endPointItems = dnsEndPoints.Select(item => new EndPointItem(item.Host, item.Port)).ToArray();
|
|
||||||
var utf8Json = JsonSerializer.SerializeToUtf8Bytes(endPointItems, jsonOptions);
|
|
||||||
await File.WriteAllBytesAsync(dnsEndpointFile, utf8Json, cancellationToken);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
this.logger.LogWarning(ex.Message, "保存dns记录异常");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
dnsEndpointLocker.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 解析ip
|
/// 解析ip
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -150,7 +68,6 @@ namespace FastGithub.DomainResolve
|
|||||||
{
|
{
|
||||||
if (this.dnsEndPointAddressElapseds.TryGetValue(endPoint, out var addressElapseds) && addressElapseds.IsEmpty == false)
|
if (this.dnsEndPointAddressElapseds.TryGetValue(endPoint, out var addressElapseds) && addressElapseds.IsEmpty == false)
|
||||||
{
|
{
|
||||||
this.logger.LogInformation($"{endPoint.Host}->{addressElapseds}");
|
|
||||||
foreach (var addressElapsed in addressElapseds)
|
foreach (var addressElapsed in addressElapseds)
|
||||||
{
|
{
|
||||||
yield return addressElapsed.Adddress;
|
yield return addressElapsed.Adddress;
|
||||||
@ -160,12 +77,11 @@ namespace FastGithub.DomainResolve
|
|||||||
{
|
{
|
||||||
if (this.dnsEndPointAddressElapseds.TryAdd(endPoint, IPAddressElapsedCollection.Empty))
|
if (this.dnsEndPointAddressElapseds.TryAdd(endPoint, IPAddressElapsedCollection.Empty))
|
||||||
{
|
{
|
||||||
await this.WriteDnsEndPointsAsync(this.dnsEndPointAddressElapseds.Keys, cancellationToken);
|
await this.persistence.WriteDnsEndPointsAsync(this.dnsEndPointAddressElapseds.Keys, cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
await foreach (var adddress in this.dnsClient.ResolveAsync(endPoint, fastSort: true, cancellationToken))
|
await foreach (var adddress in this.dnsClient.ResolveAsync(endPoint, fastSort: true, cancellationToken))
|
||||||
{
|
{
|
||||||
this.logger.LogInformation($"{endPoint.Host}->{adddress}");
|
|
||||||
yield return adddress;
|
yield return adddress;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -201,6 +117,7 @@ namespace FastGithub.DomainResolve
|
|||||||
addressElapseds = new IPAddressElapsedCollection(await Task.WhenAll(tasks));
|
addressElapseds = new IPAddressElapsedCollection(await Task.WhenAll(tasks));
|
||||||
}
|
}
|
||||||
this.dnsEndPointAddressElapseds[dnsEndPoint] = addressElapseds;
|
this.dnsEndPointAddressElapseds[dnsEndPoint] = addressElapseds;
|
||||||
|
this.logger.LogInformation($"{dnsEndPoint.Host}->{addressElapseds}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,7 @@ namespace FastGithub
|
|||||||
{
|
{
|
||||||
services.TryAddSingleton<DnsClient>();
|
services.TryAddSingleton<DnsClient>();
|
||||||
services.TryAddSingleton<DnscryptProxy>();
|
services.TryAddSingleton<DnscryptProxy>();
|
||||||
|
services.TryAddSingleton<DomainPersistence>();
|
||||||
services.TryAddSingleton<IDomainResolver, DomainResolver>();
|
services.TryAddSingleton<IDomainResolver, DomainResolver>();
|
||||||
services.AddHostedService<DomainResolveHostedService>();
|
services.AddHostedService<DomainResolveHostedService>();
|
||||||
return services;
|
return services;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user