类型重命名&细节修改

This commit is contained in:
老九 2021-11-26 23:08:27 +08:00
parent 59fde288b5
commit 060928ab70
9 changed files with 30 additions and 35 deletions

View File

@ -1,7 +1,6 @@
using FastGithub.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using System.Net;
namespace FastGithub
@ -18,8 +17,8 @@ namespace FastGithub
/// <returns></returns>
public static IServiceCollection AddConfiguration(this IServiceCollection services)
{
ValueBinder.Bind(val => IPAddress.Parse(val), val => val?.ToString());
ValueBinder.Bind(val => IPEndPoint.Parse(val), val => val?.ToString());
TypeConverterBinder.Bind(val => IPAddress.Parse(val), val => val?.ToString());
TypeConverterBinder.Bind(val => IPEndPoint.Parse(val), val => val?.ToString());
services.TryAddSingleton<FastGithubConfig>();
return services;

View File

@ -6,9 +6,9 @@ using System.Globalization;
namespace FastGithub.Configuration
{
/// <summary>
/// 配置值绑定器
/// TypeConverter类型转换绑定器
/// </summary>
static class ValueBinder
static class TypeConverterBinder
{
private static readonly Dictionary<Type, Binder> binders = new();

View File

@ -132,12 +132,12 @@ namespace FastGithub.DomainResolve
}
catch (SocketException ex)
{
this.logger.LogWarning($"{endPoint.Host}@{dns}{ex.Message}");
this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}");
return this.dnsLookupCache.Set(key, Array.Empty<IPAddress>(), this.minTimeToLive);
}
catch (Exception ex)
{
this.logger.LogWarning($"{endPoint.Host}@{dns}{ex.Message}");
this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}");
return Array.Empty<IPAddress>();
}
finally

View File

@ -14,6 +14,7 @@ namespace FastGithub.DomainResolve
private readonly DnscryptProxy dnscryptProxy;
private readonly IDomainResolver domainResolver;
private readonly ILogger<DomainResolveHostedService> logger;
private readonly TimeSpan dnscryptProxyInitDelay = TimeSpan.FromSeconds(5d);
private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds(1d);
/// <summary>
@ -41,7 +42,7 @@ namespace FastGithub.DomainResolve
try
{
await this.dnscryptProxy.StartAsync(stoppingToken);
await Task.Delay(TimeSpan.FromSeconds(5d), stoppingToken);
await Task.Delay(dnscryptProxyInitDelay, stoppingToken);
while (stoppingToken.IsCancellationRequested == false)
{

View File

@ -15,8 +15,9 @@ namespace FastGithub.DomainResolve
/// </summary>
sealed class DomainResolver : IDomainResolver
{
private const int MAX_IP_COUNT = 3;
private readonly DnsClient dnsClient;
private readonly DomainPersistence persistence;
private readonly PersistenceService persistence;
private readonly IPAddressService addressService;
private readonly ILogger<DomainResolver> logger;
private readonly ConcurrentDictionary<DnsEndPoint, IPAddress[]> dnsEndPointAddress = new();
@ -30,7 +31,7 @@ namespace FastGithub.DomainResolve
/// <param name="logger"></param>
public DomainResolver(
DnsClient dnsClient,
DomainPersistence persistence,
PersistenceService persistence,
IPAddressService addressService,
ILogger<DomainResolver> logger)
{
@ -89,11 +90,11 @@ namespace FastGithub.DomainResolve
var newAddresses = await this.addressService.GetAddressesAsync(dnsEndPoint, oldAddresses, cancellationToken);
this.dnsEndPointAddress[dnsEndPoint] = newAddresses;
var oldSegmentum = oldAddresses.Take(5);
var newSegmentum = newAddresses.Take(5);
if (oldSegmentum.SequenceEqual(newSegmentum) == false)
var oldSegmentums = oldAddresses.Take(MAX_IP_COUNT);
var newSegmentums = newAddresses.Take(MAX_IP_COUNT);
if (oldSegmentums.SequenceEqual(newSegmentums) == false)
{
var addressArray = string.Join(", ", newSegmentum.Select(item => item.ToString()));
var addressArray = string.Join(", ", newSegmentums.Select(item => item.ToString()));
this.logger.LogInformation($"{dnsEndPoint.Host}:{dnsEndPoint.Port}->[{addressArray}]");
}
}

View File

@ -14,7 +14,7 @@ namespace FastGithub.DomainResolve
/// <summary>
/// 域名持久化
/// </summary>
sealed class DomainPersistence
sealed class PersistenceService
{
private static readonly string dataFile = "dnsendpoints.json";
private static readonly SemaphoreSlim dataLocker = new(1, 1);
@ -26,7 +26,7 @@ namespace FastGithub.DomainResolve
};
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<DomainPersistence> logger;
private readonly ILogger<PersistenceService> logger;
private record EndPointItem(string Host, int Port);
@ -35,9 +35,9 @@ namespace FastGithub.DomainResolve
/// </summary>
/// <param name="fastGithubConfig"></param>
/// <param name="logger"></param>
public DomainPersistence(
public PersistenceService(
FastGithubConfig fastGithubConfig,
ILogger<DomainPersistence> logger)
ILogger<PersistenceService> logger)
{
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;

View File

@ -18,7 +18,7 @@ namespace FastGithub
{
services.TryAddSingleton<DnsClient>();
services.TryAddSingleton<DnscryptProxy>();
services.TryAddSingleton<DomainPersistence>();
services.TryAddSingleton<PersistenceService>();
services.TryAddSingleton<IPAddressService>();
services.TryAddSingleton<IDomainResolver, DomainResolver>();
services.AddHostedService<DomainResolveHostedService>();

View File

@ -200,32 +200,26 @@ namespace FastGithub.HttpServer
if (IPAddress.TryParse(targetHost, out var address) == true)
{
yield return new IPEndPoint(address, targetPort);
yield break;
}
// 不关心的域名直接使用系统dns
if (this.fastGithubConfig.IsMatch(targetHost) == false)
else if (this.fastGithubConfig.IsMatch(targetHost) == false)
{
yield return new DnsEndPoint(targetHost, targetPort);
yield break;
}
if (targetPort == HTTP_PORT)
else if (targetPort == HTTP_PORT)
{
yield return new IPEndPoint(IPAddress.Loopback, GlobalListener.HttpPort);
yield break;
}
if (targetPort == HTTPS_PORT)
else if (targetPort == HTTPS_PORT)
{
yield return new IPEndPoint(IPAddress.Loopback, GlobalListener.HttpsPort);
yield break;
}
var dnsEndPoint = new DnsEndPoint(targetHost, targetPort);
await foreach (var item in this.domainResolver.ResolveAsync(dnsEndPoint, cancellationToken))
else
{
yield return new IPEndPoint(item, targetPort);
var dnsEndPoint = new DnsEndPoint(targetHost, targetPort);
await foreach (var item in this.domainResolver.ResolveAsync(dnsEndPoint, cancellationToken))
{
yield return new IPEndPoint(item, targetPort);
}
}
}

View File

@ -82,7 +82,7 @@ namespace FastGithub.HttpServer
// 未配置的域名但仍然被解析到本机ip的域名
if (OperatingSystem.IsWindows() && IsDomain(host.Host))
{
this.logger.LogWarning($"域名{host.Host}可能已经被DNS污染");
this.logger.LogWarning($"域名{host.Host}可能已经被DNS污染如果域名为本机域名请解析为非回环IP");
domainConfig = defaultDomainConfig;
return true;
}