使用扫描结果搜索替代dns解析

This commit is contained in:
xljiulang 2021-06-20 15:23:15 +08:00
parent 1520abbbbc
commit 87deb0c5cb
2 changed files with 13 additions and 29 deletions

View File

@ -1,8 +1,5 @@
using DNS.Client; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Linq;
using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -10,15 +7,21 @@ using System.Threading.Tasks;
namespace FastGithub.Scanner namespace FastGithub.Scanner
{ {
/// <summary> /// <summary>
/// 由本程序提值的dns的httpHandler /// Github的dns解析的httpHandler
/// 使扫描索结果作为github的https请求的域名解析
/// </summary> /// </summary>
[Service(ServiceLifetime.Transient)] [Service(ServiceLifetime.Transient)]
sealed class LoopbackDnsHttpHandler : DelegatingHandler sealed class GithubDnsHttpHandler : DelegatingHandler
{ {
private readonly GithubContextCollection scanResults;
/// <summary> /// <summary>
/// 本程序的dns /// Github的dns解析的httpHandler
/// </summary> /// </summary>
private static readonly DnsClient dnsClient = new(IPAddress.Loopback); public GithubDnsHttpHandler(GithubContextCollection scanResults)
{
this.scanResults = scanResults;
}
/// <summary> /// <summary>
/// 发送消息 /// 发送消息
@ -31,7 +34,7 @@ namespace FastGithub.Scanner
var uri = request.RequestUri; var uri = request.RequestUri;
if (uri != null && uri.HostNameType == UriHostNameType.Dns) if (uri != null && uri.HostNameType == UriHostNameType.Dns)
{ {
var address = await LookupAsync(uri.Host); var address = this.scanResults.FindBestAddress(uri.Host);
if (address != null) if (address != null)
{ {
var builder = new UriBuilder(uri) var builder = new UriBuilder(uri)
@ -45,24 +48,5 @@ namespace FastGithub.Scanner
return await base.SendAsync(request, cancellationToken); return await base.SendAsync(request, cancellationToken);
} }
/// <summary>
/// dns解析ip
/// </summary>
/// <param name="host"></param>
/// <returns></returns>
private static async Task<IPAddress?> LookupAsync(string host)
{
try
{
using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500d));
var addresses = await dnsClient.Lookup(host, cancellationToken: cancellationTokenSource.Token);
return addresses.FirstOrDefault();
}
catch (Exception)
{
return default;
}
}
} }
} }

View File

@ -37,7 +37,7 @@ namespace FastGithub
UseProxy = false, UseProxy = false,
AllowAutoRedirect = false AllowAutoRedirect = false
}) })
.AddHttpMessageHandler<LoopbackDnsHttpHandler>(); .AddHttpMessageHandler<GithubDnsHttpHandler>();
return services return services
.AddMemoryCache() .AddMemoryCache()