using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.Scanner
{
///
/// Github的dns解析的httpHandler
/// 使扫描索结果作为github的https请求的域名解析
///
[Service(ServiceLifetime.Transient)]
sealed class GithubDnsHttpHandler : DelegatingHandler
{
private readonly GithubContextCollection scanResults;
///
/// Github的dns解析的httpHandler
///
public GithubDnsHttpHandler(GithubContextCollection scanResults)
{
this.scanResults = scanResults;
}
///
/// 发送消息
///
///
///
///
protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var uri = request.RequestUri;
if (uri != null && uri.HostNameType == UriHostNameType.Dns)
{
var address = this.scanResults.FindBestAddress(uri.Host);
if (address != null)
{
var builder = new UriBuilder(uri)
{
Host = address.ToString()
};
request.RequestUri = builder.Uri;
request.Headers.Host = uri.Host;
}
}
return await base.SendAsync(request, cancellationToken);
}
}
}