转发死循环检测(#27)

This commit is contained in:
陈国伟 2021-08-13 09:12:11 +08:00
parent 5c7145c056
commit 1b40882dc3
2 changed files with 24 additions and 6 deletions

View File

@ -97,12 +97,6 @@ namespace FastGithub.DomainResolve
throw new FastGithubException($"dns{dns}解析不到{domain}的ip"); throw new FastGithubException($"dns{dns}解析不到{domain}的ip");
} }
// 不允许域名解析指向FastGithub自身造成消息死循环
if (LocalMachine.ContainsIPAddress(address) == true)
{
throw new FastGithubException($"dns{dns}被污染,解析{domain}为{address}");
}
this.logger.LogInformation($"[{domain}->{address}]"); this.logger.LogInformation($"[{domain}->{address}]");
return address; return address;
} }

View File

@ -1,6 +1,9 @@
using FastGithub.Configuration; using FastGithub.Configuration;
using FastGithub.DomainResolve; using FastGithub.DomainResolve;
using System.Net.Http; using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
namespace FastGithub.Http namespace FastGithub.Http
{ {
@ -9,6 +12,11 @@ namespace FastGithub.Http
/// </summary> /// </summary>
public class HttpClient : HttpMessageInvoker public class HttpClient : HttpMessageInvoker
{ {
/// <summary>
/// 插入的UserAgent标记
/// </summary>
private readonly static ProductInfoHeaderValue userAgent = new(new ProductHeaderValue(nameof(FastGithub), "1.0"));
/// <summary> /// <summary>
/// http客户端 /// http客户端
/// </summary> /// </summary>
@ -28,5 +36,21 @@ namespace FastGithub.Http
: base(handler, disposeHandler) : base(handler, disposeHandler)
{ {
} }
/// <summary>
/// 发送请求
/// </summary>
/// <param name="request"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Headers.UserAgent.Contains(userAgent))
{
throw new FastGithubException($"由于{request.RequestUri}实际指向了{nameof(FastGithub)}自身,{nameof(FastGithub)}已中断本次转发");
}
request.Headers.UserAgent.Add(userAgent);
return base.SendAsync(request, cancellationToken);
}
} }
} }