自动保存dns记录

This commit is contained in:
老九 2021-11-18 22:58:37 +08:00
parent 646e483c57
commit 34653a754f

View File

@ -3,9 +3,11 @@ 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;
@ -16,7 +18,18 @@ namespace FastGithub.DomainResolve
/// </summary>
sealed class DomainResolver : IDomainResolver
{
private record EndPointItem(string Host, int Port);
private static readonly string dnsEndpointFile = "dnsendpoints.json";
private static readonly SemaphoreSlim dnsEndpointLocker = new(1, 1);
private static readonly JsonSerializerOptions jsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
private readonly DnsClient dnsClient;
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DomainResolver> logger;
private readonly ConcurrentDictionary<DnsEndPoint, IPAddressElapsedCollection> dnsEndPointAddressElapseds = new();
@ -24,13 +37,94 @@ namespace FastGithub.DomainResolve
/// 域名解析器
/// </summary>
/// <param name="dnsClient"></param>
/// <param name="fastGithubConfig"></param>
/// <param name="logger"></param>
public DomainResolver(DnsClient dnsClient, ILogger<DomainResolver> logger)
public DomainResolver(
DnsClient dnsClient,
FastGithubConfig fastGithubConfig,
ILogger<DomainResolver> logger)
{
this.dnsClient = dnsClient;
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;
foreach (var endPoint in this.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))
{
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>
@ -64,7 +158,11 @@ namespace FastGithub.DomainResolve
}
else
{
this.dnsEndPointAddressElapseds.TryAdd(endPoint, IPAddressElapsedCollection.Empty);
if (this.dnsEndPointAddressElapseds.TryAdd(endPoint, IPAddressElapsedCollection.Empty))
{
await this.WriteDnsEndPointsAsync(this.dnsEndPointAddressElapseds.Keys, cancellationToken);
}
await foreach (var adddress in this.dnsClient.ResolveAsync(endPoint, fastSort: true, cancellationToken))
{
this.logger.LogInformation($"{endPoint.Host}->{adddress}");