From 060928ab70a015cee6cd15bc98c1e1a0e78ed310 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com>
Date: Fri, 26 Nov 2021 23:08:27 +0800
Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=87=8D=E5=91=BD=E5=90=8D&?=
=?UTF-8?q?=E7=BB=86=E8=8A=82=E4=BF=AE=E6=94=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../ServiceCollectionExtensions.cs | 5 ++--
...{ValueBinder.cs => TypeConverterBinder.cs} | 4 ++--
FastGithub.DomainResolve/DnsClient.cs | 4 ++--
.../DomainResolveHostedService.cs | 3 ++-
FastGithub.DomainResolve/DomainResolver.cs | 13 +++++-----
...inPersistence.cs => PersistenceService.cs} | 8 +++----
.../ServiceCollectionExtensions.cs | 2 +-
FastGithub.HttpServer/HttpProxyMiddleware.cs | 24 +++++++------------
.../HttpReverseProxyMiddleware.cs | 2 +-
9 files changed, 30 insertions(+), 35 deletions(-)
rename FastGithub.Configuration/{ValueBinder.cs => TypeConverterBinder.cs} (97%)
rename FastGithub.DomainResolve/{DomainPersistence.cs => PersistenceService.cs} (95%)
diff --git a/FastGithub.Configuration/ServiceCollectionExtensions.cs b/FastGithub.Configuration/ServiceCollectionExtensions.cs
index f3d4832..bffca49 100644
--- a/FastGithub.Configuration/ServiceCollectionExtensions.cs
+++ b/FastGithub.Configuration/ServiceCollectionExtensions.cs
@@ -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
///
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();
return services;
diff --git a/FastGithub.Configuration/ValueBinder.cs b/FastGithub.Configuration/TypeConverterBinder.cs
similarity index 97%
rename from FastGithub.Configuration/ValueBinder.cs
rename to FastGithub.Configuration/TypeConverterBinder.cs
index 4e14778..cf37c7e 100644
--- a/FastGithub.Configuration/ValueBinder.cs
+++ b/FastGithub.Configuration/TypeConverterBinder.cs
@@ -6,9 +6,9 @@ using System.Globalization;
namespace FastGithub.Configuration
{
///
- /// 配置值绑定器
+ /// TypeConverter类型转换绑定器
///
- static class ValueBinder
+ static class TypeConverterBinder
{
private static readonly Dictionary binders = new();
diff --git a/FastGithub.DomainResolve/DnsClient.cs b/FastGithub.DomainResolve/DnsClient.cs
index 2301373..3b0a2bf 100644
--- a/FastGithub.DomainResolve/DnsClient.cs
+++ b/FastGithub.DomainResolve/DnsClient.cs
@@ -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(), this.minTimeToLive);
}
catch (Exception ex)
{
- this.logger.LogWarning($"{endPoint.Host}@{dns}:{ex.Message}");
+ this.logger.LogWarning($"{endPoint.Host}@{dns}->{ex.Message}");
return Array.Empty();
}
finally
diff --git a/FastGithub.DomainResolve/DomainResolveHostedService.cs b/FastGithub.DomainResolve/DomainResolveHostedService.cs
index c559d32..9054fbd 100644
--- a/FastGithub.DomainResolve/DomainResolveHostedService.cs
+++ b/FastGithub.DomainResolve/DomainResolveHostedService.cs
@@ -14,6 +14,7 @@ namespace FastGithub.DomainResolve
private readonly DnscryptProxy dnscryptProxy;
private readonly IDomainResolver domainResolver;
private readonly ILogger logger;
+ private readonly TimeSpan dnscryptProxyInitDelay = TimeSpan.FromSeconds(5d);
private readonly TimeSpan testPeriodTimeSpan = TimeSpan.FromSeconds(1d);
///
@@ -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)
{
diff --git a/FastGithub.DomainResolve/DomainResolver.cs b/FastGithub.DomainResolve/DomainResolver.cs
index b29c577..b64b550 100644
--- a/FastGithub.DomainResolve/DomainResolver.cs
+++ b/FastGithub.DomainResolve/DomainResolver.cs
@@ -15,8 +15,9 @@ namespace FastGithub.DomainResolve
///
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 logger;
private readonly ConcurrentDictionary dnsEndPointAddress = new();
@@ -30,7 +31,7 @@ namespace FastGithub.DomainResolve
///
public DomainResolver(
DnsClient dnsClient,
- DomainPersistence persistence,
+ PersistenceService persistence,
IPAddressService addressService,
ILogger 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}]");
}
}
diff --git a/FastGithub.DomainResolve/DomainPersistence.cs b/FastGithub.DomainResolve/PersistenceService.cs
similarity index 95%
rename from FastGithub.DomainResolve/DomainPersistence.cs
rename to FastGithub.DomainResolve/PersistenceService.cs
index 1b58b57..c4df224 100644
--- a/FastGithub.DomainResolve/DomainPersistence.cs
+++ b/FastGithub.DomainResolve/PersistenceService.cs
@@ -14,7 +14,7 @@ namespace FastGithub.DomainResolve
///
/// 域名持久化
///
- 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 logger;
+ private readonly ILogger logger;
private record EndPointItem(string Host, int Port);
@@ -35,9 +35,9 @@ namespace FastGithub.DomainResolve
///
///
///
- public DomainPersistence(
+ public PersistenceService(
FastGithubConfig fastGithubConfig,
- ILogger logger)
+ ILogger logger)
{
this.fastGithubConfig = fastGithubConfig;
this.logger = logger;
diff --git a/FastGithub.DomainResolve/ServiceCollectionExtensions.cs b/FastGithub.DomainResolve/ServiceCollectionExtensions.cs
index 5dc476f..525acfe 100644
--- a/FastGithub.DomainResolve/ServiceCollectionExtensions.cs
+++ b/FastGithub.DomainResolve/ServiceCollectionExtensions.cs
@@ -18,7 +18,7 @@ namespace FastGithub
{
services.TryAddSingleton();
services.TryAddSingleton();
- services.TryAddSingleton();
+ services.TryAddSingleton();
services.TryAddSingleton();
services.TryAddSingleton();
services.AddHostedService();
diff --git a/FastGithub.HttpServer/HttpProxyMiddleware.cs b/FastGithub.HttpServer/HttpProxyMiddleware.cs
index 230e7f3..86cb801 100644
--- a/FastGithub.HttpServer/HttpProxyMiddleware.cs
+++ b/FastGithub.HttpServer/HttpProxyMiddleware.cs
@@ -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);
+ }
}
}
diff --git a/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs b/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs
index e4ea8aa..c37eef0 100644
--- a/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs
+++ b/FastGithub.HttpServer/HttpReverseProxyMiddleware.cs
@@ -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;
}