Răsfoiți Sursa

类型重命名&细节修改

老九 3 ani în urmă
părinte
comite
060928ab70

+ 2 - 3
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
         /// <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;

+ 2 - 2
FastGithub.Configuration/ValueBinder.cs → FastGithub.Configuration/TypeConverterBinder.cs

@@ -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();
 

+ 2 - 2
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<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

+ 2 - 1
FastGithub.DomainResolve/DomainResolveHostedService.cs

@@ -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)
                 {

+ 7 - 6
FastGithub.DomainResolve/DomainResolver.cs

@@ -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}]");
                 }
             }

+ 4 - 4
FastGithub.DomainResolve/DomainPersistence.cs → FastGithub.DomainResolve/PersistenceService.cs

@@ -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;

+ 1 - 1
FastGithub.DomainResolve/ServiceCollectionExtensions.cs

@@ -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>();

+ 9 - 15
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);
+                }
             }
         }
 

+ 1 - 1
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;
             }