dns监听所有ip

This commit is contained in:
陈国伟 2021-07-15 09:54:59 +08:00
parent 46606fed5f
commit 1638332e04
7 changed files with 38 additions and 57 deletions

View File

@ -50,7 +50,7 @@ namespace FastGithub.Dns
/// <returns></returns> /// <returns></returns>
public override Task StartAsync(CancellationToken cancellationToken) 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()) if (OperatingSystem.IsWindows())
{ {
this.socket.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]); this.socket.IOControl(SIO_UDP_CONNRESET, new byte[4], new byte[4]);

View File

@ -19,7 +19,6 @@ namespace FastGithub
var assembly = typeof(ReverseProxyServiceCollectionExtensions).Assembly; var assembly = typeof(ReverseProxyServiceCollectionExtensions).Assembly;
return services return services
.AddServiceAndOptions(assembly, configuration) .AddServiceAndOptions(assembly, configuration)
.AddMemoryCache()
.AddHttpForwarder(); .AddHttpForwarder();
} }
} }

View File

@ -1,26 +1,22 @@
using FastGithub.Scanner; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Net.Security; using System.Net.Security;
using System.Net.Sockets; using System.Net.Sockets;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace FastGithub.ReverseProxy namespace FastGithub.Scanner
{ {
/// <summary> /// <summary>
/// 使用于请求github的HttpClientHandler /// 用于请求github的HttpClientHandler
/// </summary> /// </summary>
[Service(ServiceLifetime.Transient)] [Service(ServiceLifetime.Transient)]
sealed class GithubHttpClientHanlder : DelegatingHandler public class GithubHttpClientHanlder : DelegatingHandler
{ {
private readonly IGithubScanResults githubScanResults; private readonly IGithubScanResults githubScanResults;
private readonly ILogger<GithubHttpClientHanlder> logger; private readonly ILogger<GithubHttpClientHanlder> logger;
private readonly IMemoryCache memoryCache;
/// <summary> /// <summary>
/// 请求github的HttpClientHandler /// 请求github的HttpClientHandler
@ -29,12 +25,10 @@ namespace FastGithub.ReverseProxy
/// <param name="logger"></param> /// <param name="logger"></param>
public GithubHttpClientHanlder( public GithubHttpClientHanlder(
IGithubScanResults githubScanResults, IGithubScanResults githubScanResults,
ILogger<GithubHttpClientHanlder> logger, ILogger<GithubHttpClientHanlder> logger)
IMemoryCache memoryCache)
{ {
this.githubScanResults = githubScanResults; this.githubScanResults = githubScanResults;
this.logger = logger; this.logger = logger;
this.memoryCache = memoryCache;
this.InnerHandler = CreateNoneSniHttpHandler(); this.InnerHandler = CreateNoneSniHttpHandler();
} }
@ -82,7 +76,7 @@ namespace FastGithub.ReverseProxy
var uri = request.RequestUri; var uri = request.RequestUri;
if (uri != null && uri.HostNameType == UriHostNameType.Dns) if (uri != null && uri.HostNameType == UriHostNameType.Dns)
{ {
var address = this.Resolve(uri.Host); var address = this.githubScanResults.FindBestAddress(uri.Host);
if (address != null) if (address != null)
{ {
this.logger.LogInformation($"使用{address} No SNI请求{uri.Host}"); this.logger.LogInformation($"使用{address} No SNI请求{uri.Host}");
@ -96,20 +90,5 @@ namespace FastGithub.ReverseProxy
} }
return await base.SendAsync(request, cancellationToken); return await base.SendAsync(request, cancellationToken);
} }
/// <summary>
/// 解析域名
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
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);
});
}
} }
} }

View File

@ -1,5 +1,7 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -14,10 +16,14 @@ namespace FastGithub.Scanner
{ {
private readonly object syncRoot = new(); private readonly object syncRoot = new();
private readonly List<GithubContext> contexts = new(); private readonly List<GithubContext> contexts = new();
private readonly IMemoryCache memoryCache;
private readonly IOptionsMonitor<GithubLookupFactoryOptions> options; private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
public GithubScanResults(IOptionsMonitor<GithubLookupFactoryOptions> options) public GithubScanResults(
IMemoryCache memoryCache,
IOptionsMonitor<GithubLookupFactoryOptions> options)
{ {
this.memoryCache = memoryCache;
this.options = options; this.options = options;
} }
@ -57,6 +63,22 @@ namespace FastGithub.Scanner
/// <param name="domain"></param> /// <param name="domain"></param>
/// <returns></returns> /// <returns></returns>
public IPAddress? FindBestAddress(string domain) public IPAddress? FindBestAddress(string domain)
{
var key = $"domain:{domain}";
return this.memoryCache.GetOrCreate(key, e =>
{
e.SetAbsoluteExpiration(TimeSpan.FromSeconds(1d));
return this.Resolve(domain);
});
}
/// <summary>
/// 解析域名
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
private IPAddress? Resolve(string domain)
{ {
if (this.options.CurrentValue.Domains.Contains(domain) == false) if (this.options.CurrentValue.Domains.Contains(domain) == false)
{ {

View File

@ -2,10 +2,7 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Net.Http;
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Net.Security;
using System.Net.Sockets;
namespace FastGithub namespace FastGithub
{ {
@ -34,29 +31,9 @@ namespace FastGithub
httpClient.DefaultRequestHeaders.Accept.TryParseAdd("*/*"); httpClient.DefaultRequestHeaders.Accept.TryParseAdd("*/*");
httpClient.DefaultRequestHeaders.UserAgent.Add(defaultUserAgent); httpClient.DefaultRequestHeaders.UserAgent.Add(defaultUserAgent);
}) })
.ConfigurePrimaryHttpMessageHandler(() => new SocketsHttpHandler .ConfigurePrimaryHttpMessageHandler((serviceProvider) =>
{ {
Proxy = null, return serviceProvider.GetRequiredService<GithubHttpClientHanlder>();
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 services return services

View File

@ -26,6 +26,10 @@ namespace FastGithub
.CreateDefaultBuilder(args) .CreateDefaultBuilder(args)
.UseWindowsService() .UseWindowsService()
.UseBinaryPathContentRoot() .UseBinaryPathContentRoot()
.UseDefaultServiceProvider(c =>
{
c.ValidateOnBuild = false;
})
.ConfigureAppConfiguration(c => .ConfigureAppConfiguration(c =>
{ {
c.AddJsonFile("appsettings.github.json", optional: true); c.AddJsonFile("appsettings.github.json", optional: true);

View File

@ -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-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 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 dotnet publish -c Release -f net6.0 /p:PublishSingleFile=true /p:PublishTrimmed=true -r osx-x64 -o ./bin/publish/osx-x64