修改日志
This commit is contained in:
parent
8df865b6e7
commit
4b807991e5
@ -128,13 +128,13 @@ namespace FastGithub.DomainResolve
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
this.logger.LogWarning($"dns://{dns}无法解析{endPoint.Host}:请求超时");
|
||||
this.logger.LogWarning($"{endPoint.Host}@{dns}:请求超时。");
|
||||
return Array.Empty<IPAddress>();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this.logger.LogWarning($"dns://{dns}无法解析{endPoint.Host}:{ex.Message}");
|
||||
return this.dnsCache.Set(key, Array.Empty<IPAddress>(), this.maxTimeToLive);
|
||||
this.logger.LogWarning($"{endPoint.Host}@{dns}:{ex.Message}");
|
||||
return Array.Empty<IPAddress>();
|
||||
}
|
||||
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.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@ -19,112 +17,32 @@ namespace FastGithub.DomainResolve
|
||||
sealed class DomainResolver : IDomainResolver
|
||||
{
|
||||
private readonly DnsClient dnsClient;
|
||||
private readonly FastGithubConfig fastGithubConfig;
|
||||
private readonly DomainPersistence persistence;
|
||||
private readonly ILogger<DomainResolver> logger;
|
||||
private record EndPointItem(string Host, int Port);
|
||||
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>
|
||||
/// <param name="dnsClient"></param>
|
||||
/// <param name="fastGithubConfig"></param>
|
||||
/// <param name="persistence"></param>
|
||||
/// <param name="logger"></param>
|
||||
public DomainResolver(
|
||||
DnsClient dnsClient,
|
||||
FastGithubConfig fastGithubConfig,
|
||||
DomainPersistence persistence,
|
||||
ILogger<DomainResolver> logger)
|
||||
{
|
||||
this.dnsClient = dnsClient;
|
||||
this.fastGithubConfig = fastGithubConfig;
|
||||
this.persistence = persistence;
|
||||
this.logger = logger;
|
||||
|
||||
foreach (var endPoint in this.ReadDnsEndPoints())
|
||||
foreach (var endPoint in persistence.ReadDnsEndPoints())
|
||||
{
|
||||
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>
|
||||
/// 解析ip
|
||||
/// </summary>
|
||||
@ -150,7 +68,6 @@ namespace FastGithub.DomainResolve
|
||||
{
|
||||
if (this.dnsEndPointAddressElapseds.TryGetValue(endPoint, out var addressElapseds) && addressElapseds.IsEmpty == false)
|
||||
{
|
||||
this.logger.LogInformation($"{endPoint.Host}->{addressElapseds}");
|
||||
foreach (var addressElapsed in addressElapseds)
|
||||
{
|
||||
yield return addressElapsed.Adddress;
|
||||
@ -160,12 +77,11 @@ namespace FastGithub.DomainResolve
|
||||
{
|
||||
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))
|
||||
{
|
||||
this.logger.LogInformation($"{endPoint.Host}->{adddress}");
|
||||
yield return adddress;
|
||||
}
|
||||
}
|
||||
@ -201,6 +117,7 @@ namespace FastGithub.DomainResolve
|
||||
addressElapseds = new IPAddressElapsedCollection(await Task.WhenAll(tasks));
|
||||
}
|
||||
this.dnsEndPointAddressElapseds[dnsEndPoint] = addressElapseds;
|
||||
this.logger.LogInformation($"{dnsEndPoint.Host}->{addressElapseds}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -17,7 +17,8 @@ namespace FastGithub
|
||||
public static IServiceCollection AddDomainResolve(this IServiceCollection services)
|
||||
{
|
||||
services.TryAddSingleton<DnsClient>();
|
||||
services.TryAddSingleton<DnscryptProxy>();
|
||||
services.TryAddSingleton<DnscryptProxy>();
|
||||
services.TryAddSingleton<DomainPersistence>();
|
||||
services.TryAddSingleton<IDomainResolver, DomainResolver>();
|
||||
services.AddHostedService<DomainResolveHostedService>();
|
||||
return services;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user