From 1638332e0449b63302f00920edfbd8f35b0a72ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=99=88=E5=9B=BD=E4=BC=9F?= <366193849@qq.com>
Date: Thu, 15 Jul 2021 09:54:59 +0800
Subject: [PATCH] =?UTF-8?q?dns=E7=9B=91=E5=90=AC=E6=89=80=E6=9C=89ip?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub.Dns/DnsHostedService.cs | 2 +-
...ReverseProxyServiceCollectionExtensions.cs | 1 -
.../GithubHttpClientHanlder.cs | 33 ++++---------------
FastGithub.Scanner/GithubScanResults.cs | 26 +++++++++++++--
.../ScannerServiceCollectionExtensions.cs | 27 ++-------------
FastGithub/Program.cs | 4 +++
FastGithub/publish.cmd | 2 +-
7 files changed, 38 insertions(+), 57 deletions(-)
rename {FastGithub.ReverseProxy => FastGithub.Scanner}/GithubHttpClientHanlder.cs (75%)
diff --git a/FastGithub.Dns/DnsHostedService.cs b/FastGithub.Dns/DnsHostedService.cs
index 33eda8a..7bd1c14 100644
--- a/FastGithub.Dns/DnsHostedService.cs
+++ b/FastGithub.Dns/DnsHostedService.cs
@@ -50,7 +50,7 @@ namespace FastGithub.Dns
///
public override Task StartAsync(CancellationToken cancellationToken)
{
- this.socket.Bind(new IPEndPoint(IPAddress.Loopback, 53));
+ this.socket.Bind(new IPEndPoint(IPAddress.Any, 53));
if (OperatingSystem.IsWindows())
{
this.socket.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]);
diff --git a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs
index b7245f7..df95e9e 100644
--- a/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs
+++ b/FastGithub.ReverseProxy/ReverseProxyServiceCollectionExtensions.cs
@@ -19,7 +19,6 @@ namespace FastGithub
var assembly = typeof(ReverseProxyServiceCollectionExtensions).Assembly;
return services
.AddServiceAndOptions(assembly, configuration)
- .AddMemoryCache()
.AddHttpForwarder();
}
}
diff --git a/FastGithub.ReverseProxy/GithubHttpClientHanlder.cs b/FastGithub.Scanner/GithubHttpClientHanlder.cs
similarity index 75%
rename from FastGithub.ReverseProxy/GithubHttpClientHanlder.cs
rename to FastGithub.Scanner/GithubHttpClientHanlder.cs
index 7e66aa0..907215a 100644
--- a/FastGithub.ReverseProxy/GithubHttpClientHanlder.cs
+++ b/FastGithub.Scanner/GithubHttpClientHanlder.cs
@@ -1,26 +1,22 @@
-using FastGithub.Scanner;
-using Microsoft.Extensions.Caching.Memory;
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
-using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
-namespace FastGithub.ReverseProxy
+namespace FastGithub.Scanner
{
///
- /// 使用于请求github的HttpClientHandler
+ /// 适用于请求github的HttpClientHandler
///
[Service(ServiceLifetime.Transient)]
- sealed class GithubHttpClientHanlder : DelegatingHandler
+ public class GithubHttpClientHanlder : DelegatingHandler
{
private readonly IGithubScanResults githubScanResults;
private readonly ILogger logger;
- private readonly IMemoryCache memoryCache;
///
/// 请求github的HttpClientHandler
@@ -29,12 +25,10 @@ namespace FastGithub.ReverseProxy
///
public GithubHttpClientHanlder(
IGithubScanResults githubScanResults,
- ILogger logger,
- IMemoryCache memoryCache)
+ ILogger logger)
{
this.githubScanResults = githubScanResults;
this.logger = logger;
- this.memoryCache = memoryCache;
this.InnerHandler = CreateNoneSniHttpHandler();
}
@@ -82,7 +76,7 @@ namespace FastGithub.ReverseProxy
var uri = request.RequestUri;
if (uri != null && uri.HostNameType == UriHostNameType.Dns)
{
- var address = this.Resolve(uri.Host);
+ var address = this.githubScanResults.FindBestAddress(uri.Host);
if (address != null)
{
this.logger.LogInformation($"使用{address} No SNI请求{uri.Host}");
@@ -96,20 +90,5 @@ namespace FastGithub.ReverseProxy
}
return await base.SendAsync(request, cancellationToken);
}
-
- ///
- /// 解析域名
- ///
- ///
- ///
- private IPAddress? Resolve(string domain)
- {
- var key = $"domain:{domain}";
- return this.memoryCache.GetOrCreate(key, e =>
- {
- e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
- return this.githubScanResults.FindBestAddress(domain);
- });
- }
}
}
diff --git a/FastGithub.Scanner/GithubScanResults.cs b/FastGithub.Scanner/GithubScanResults.cs
index 32f7771..abb1d02 100644
--- a/FastGithub.Scanner/GithubScanResults.cs
+++ b/FastGithub.Scanner/GithubScanResults.cs
@@ -1,5 +1,7 @@
-using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Caching.Memory;
+using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
@@ -14,10 +16,14 @@ namespace FastGithub.Scanner
{
private readonly object syncRoot = new();
private readonly List contexts = new();
+ private readonly IMemoryCache memoryCache;
private readonly IOptionsMonitor options;
- public GithubScanResults(IOptionsMonitor options)
+ public GithubScanResults(
+ IMemoryCache memoryCache,
+ IOptionsMonitor options)
{
+ this.memoryCache = memoryCache;
this.options = options;
}
@@ -57,6 +63,22 @@ namespace FastGithub.Scanner
///
///
public IPAddress? FindBestAddress(string domain)
+ {
+ var key = $"domain:{domain}";
+ return this.memoryCache.GetOrCreate(key, e =>
+ {
+ e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
+ return this.Resolve(domain);
+ });
+ }
+
+
+ ///
+ /// 解析域名
+ ///
+ ///
+ ///
+ private IPAddress? Resolve(string domain)
{
if (this.options.CurrentValue.Domains.Contains(domain) == false)
{
diff --git a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
index 11f3c38..1d34220 100644
--- a/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
+++ b/FastGithub.Scanner/ScannerServiceCollectionExtensions.cs
@@ -2,10 +2,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
-using System.Net.Http;
using System.Net.Http.Headers;
-using System.Net.Security;
-using System.Net.Sockets;
namespace FastGithub
{
@@ -34,29 +31,9 @@ namespace FastGithub
httpClient.DefaultRequestHeaders.Accept.TryParseAdd("*/*");
httpClient.DefaultRequestHeaders.UserAgent.Add(defaultUserAgent);
})
- .ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler
+ .ConfigurePrimaryHttpMessageHandler((serviceProvider) =>
{
- Proxy = null,
- UseProxy = false,
- AllowAutoRedirect = false,
- ConnectCallback = async (ctx, ct) =>
- {
- var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
- await socket.ConnectAsync(ctx.DnsEndPoint, ct);
- var stream = new NetworkStream(socket, ownsSocket: true);
- if (ctx.InitialRequestMessage.Headers.Host == null)
- {
- return stream;
- }
-
- var sslStream = new SslStream(stream, leaveInnerStreamOpen: false);
- await sslStream.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
- {
- TargetHost = string.Empty,
- RemoteCertificateValidationCallback = delegate { return true; }
- }, ct);
- return sslStream;
- }
+ return serviceProvider.GetRequiredService();
});
return services
diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs
index d705577..fa4ece1 100644
--- a/FastGithub/Program.cs
+++ b/FastGithub/Program.cs
@@ -26,6 +26,10 @@ namespace FastGithub
.CreateDefaultBuilder(args)
.UseWindowsService()
.UseBinaryPathContentRoot()
+ .UseDefaultServiceProvider(c =>
+ {
+ c.ValidateOnBuild = false;
+ })
.ConfigureAppConfiguration(c =>
{
c.AddJsonFile("appsettings.github.json", optional: true);
diff --git a/FastGithub/publish.cmd b/FastGithub/publish.cmd
index 22db123..d294743 100644
--- a/FastGithub/publish.cmd
+++ b/FastGithub/publish.cmd
@@ -1,4 +1,4 @@
+dotnet publish -c Release -f net6.0 /p:PublishSingleFile=true /p:PublishTrimmed=true -r linux-x64 -o ./bin/publish/linux-x64
dotnet publish -c Release -f net6.0 /p:PublishSingleFile=true /p:PublishTrimmed=true -r win-x86 -o ./bin/publish/win-x86
dotnet publish -c Release -f net6.0 /p:PublishSingleFile=true /p:PublishTrimmed=true -r win-x64 -o ./bin/publish/win-x64
-dotnet publish -c Release -f net6.0 /p:PublishSingleFile=true /p:PublishTrimmed=true -r linux-x64 -o ./bin/publish/linux-x64
dotnet publish -c Release -f net6.0 /p:PublishSingleFile=true /p:PublishTrimmed=true -r osx-x64 -o ./bin/publish/osx-x64