简化域名判断

This commit is contained in:
陈国伟 2021-11-06 12:00:10 +08:00
parent bec5c732ab
commit 3676caf118
2 changed files with 8 additions and 20 deletions

View File

@ -105,10 +105,10 @@ namespace FastGithub.HttpServer
}
else
{
await this.httpReverseProxy.InvokeAsync(context, async ctx =>
await this.httpReverseProxy.InvokeAsync(context, async next =>
{
var destinationPrefix = $"{ctx.Request.Scheme}://{ctx.Request.Host}";
await this.httpForwarder.SendAsync(ctx, destinationPrefix, this.defaultHttpClient);
var destinationPrefix = $"{context.Request.Scheme}://{context.Request.Host}";
await this.httpForwarder.SendAsync(context, destinationPrefix, this.defaultHttpClient);
});
}
}

View File

@ -14,11 +14,6 @@ namespace FastGithub.HttpServer
/// </summary>
sealed class HttpReverseProxyMiddleware
{
private const string LOOPBACK = "127.0.0.1";
private const string LOCALHOST = "localhost";
private const int HTTP_PORT = 80;
private const int HTTPS_PORT = 443;
private static readonly DomainConfig defaultDomainConfig = new() { TlsSni = true };
private readonly IHttpForwarder httpForwarder;
@ -26,7 +21,6 @@ namespace FastGithub.HttpServer
private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<HttpReverseProxyMiddleware> logger;
public HttpReverseProxyMiddleware(
IHttpForwarder httpForwarder,
IHttpClientFactory httpClientFactory,
@ -84,22 +78,16 @@ namespace FastGithub.HttpServer
return true;
}
// http(s)://127.0.0.1
// http(s)://localhost
if (host.Host == LOOPBACK || host.Host == LOCALHOST)
// 未配置的域名但仍然被解析到本机ip的域名
if (host.Host.Contains('.') == true)
{
if (host.Port == null || host.Port == HTTPS_PORT || host.Port == HTTP_PORT)
{
return false;
}
}
// 未配置的域名但dns污染解析为127.0.0.1的域名
this.logger.LogWarning($"检测到{host.Host}可能遭遇了dns污染");
domainConfig = defaultDomainConfig;
return true;
}
return false;
}
/// <summary>
/// 获取目标前缀
/// </summary>