dns监听所有ip
This commit is contained in:
parent
46606fed5f
commit
1638332e04
@ -50,7 +50,7 @@ namespace FastGithub.Dns
|
||||
/// <returns></returns>
|
||||
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]);
|
||||
|
||||
@ -19,7 +19,6 @@ namespace FastGithub
|
||||
var assembly = typeof(ReverseProxyServiceCollectionExtensions).Assembly;
|
||||
return services
|
||||
.AddServiceAndOptions(assembly, configuration)
|
||||
.AddMemoryCache()
|
||||
.AddHttpForwarder();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 使用于请求github的HttpClientHandler
|
||||
/// 适用于请求github的HttpClientHandler
|
||||
/// </summary>
|
||||
[Service(ServiceLifetime.Transient)]
|
||||
sealed class GithubHttpClientHanlder : DelegatingHandler
|
||||
public class GithubHttpClientHanlder : DelegatingHandler
|
||||
{
|
||||
private readonly IGithubScanResults githubScanResults;
|
||||
private readonly ILogger<GithubHttpClientHanlder> logger;
|
||||
private readonly IMemoryCache memoryCache;
|
||||
|
||||
/// <summary>
|
||||
/// 请求github的HttpClientHandler
|
||||
@ -29,12 +25,10 @@ namespace FastGithub.ReverseProxy
|
||||
/// <param name="logger"></param>
|
||||
public GithubHttpClientHanlder(
|
||||
IGithubScanResults githubScanResults,
|
||||
ILogger<GithubHttpClientHanlder> logger,
|
||||
IMemoryCache memoryCache)
|
||||
ILogger<GithubHttpClientHanlder> 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);
|
||||
}
|
||||
|
||||
/// <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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<GithubContext> contexts = new();
|
||||
private readonly IMemoryCache memoryCache;
|
||||
private readonly IOptionsMonitor<GithubLookupFactoryOptions> options;
|
||||
|
||||
public GithubScanResults(IOptionsMonitor<GithubLookupFactoryOptions> options)
|
||||
public GithubScanResults(
|
||||
IMemoryCache memoryCache,
|
||||
IOptionsMonitor<GithubLookupFactoryOptions> options)
|
||||
{
|
||||
this.memoryCache = memoryCache;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
@ -57,6 +63,22 @@ namespace FastGithub.Scanner
|
||||
/// <param name="domain"></param>
|
||||
/// <returns></returns>
|
||||
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)
|
||||
{
|
||||
|
||||
@ -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<GithubHttpClientHanlder>();
|
||||
});
|
||||
|
||||
return services
|
||||
|
||||
@ -26,6 +26,10 @@ namespace FastGithub
|
||||
.CreateDefaultBuilder(args)
|
||||
.UseWindowsService()
|
||||
.UseBinaryPathContentRoot()
|
||||
.UseDefaultServiceProvider(c =>
|
||||
{
|
||||
c.ValidateOnBuild = false;
|
||||
})
|
||||
.ConfigureAppConfiguration(c =>
|
||||
{
|
||||
c.AddJsonFile("appsettings.github.json", optional: true);
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user