类型重命名&细节修改

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -200,32 +200,26 @@ namespace FastGithub.HttpServer
if (IPAddress.TryParse(targetHost, out var address) == true) if (IPAddress.TryParse(targetHost, out var address) == true)
{ {
yield return new IPEndPoint(address, targetPort); yield return new IPEndPoint(address, targetPort);
yield break;
} }
else if (this.fastGithubConfig.IsMatch(targetHost) == false)
// 不关心的域名直接使用系统dns
if (this.fastGithubConfig.IsMatch(targetHost) == false)
{ {
yield return new DnsEndPoint(targetHost, targetPort); yield return new DnsEndPoint(targetHost, targetPort);
yield break;
} }
else if (targetPort == HTTP_PORT)
if (targetPort == HTTP_PORT)
{ {
yield return new IPEndPoint(IPAddress.Loopback, GlobalListener.HttpPort); yield return new IPEndPoint(IPAddress.Loopback, GlobalListener.HttpPort);
yield break;
} }
else if (targetPort == HTTPS_PORT)
if (targetPort == HTTPS_PORT)
{ {
yield return new IPEndPoint(IPAddress.Loopback, GlobalListener.HttpsPort); yield return new IPEndPoint(IPAddress.Loopback, GlobalListener.HttpsPort);
yield break;
} }
else
var dnsEndPoint = new DnsEndPoint(targetHost, targetPort);
await foreach (var item in this.domainResolver.ResolveAsync(dnsEndPoint, cancellationToken))
{ {
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的域名 // 未配置的域名但仍然被解析到本机ip的域名
if (OperatingSystem.IsWindows() && IsDomain(host.Host)) if (OperatingSystem.IsWindows() && IsDomain(host.Host))
{ {
this.logger.LogWarning($"域名{host.Host}可能已经被DNS污染"); this.logger.LogWarning($"域名{host.Host}可能已经被DNS污染如果域名为本机域名请解析为非回环IP");
domainConfig = defaultDomainConfig; domainConfig = defaultDomainConfig;
return true; return true;
} }