跳过本机域名

This commit is contained in:
陈国伟 2021-09-13 10:18:13 +08:00
parent 5815f5ae94
commit 1e67d8c5b9
3 changed files with 30 additions and 2 deletions

View File

@ -60,6 +60,25 @@ namespace FastGithub.Configuration
return GetAllIPAddresses().Contains(address); return GetAllIPAddresses().Contains(address);
} }
/// <summary>
/// 获取所有域名和ip
/// </summary>
/// <returns></returns>
public static HashSet<string> GetAllHostNames()
{
var hashSet = new HashSet<string>
{
Name,
"localhost",
};
foreach (var address in GetAllIPAddresses())
{
hashSet.Add(address.ToString());
}
return hashSet;
}
/// <summary> /// <summary>
/// 获取与远程节点通讯的的本机IP地址 /// 获取与远程节点通讯的的本机IP地址
/// </summary> /// </summary>

View File

@ -28,7 +28,7 @@ namespace FastGithub
public static IApplicationBuilder UseReverseProxy(this IApplicationBuilder app) public static IApplicationBuilder UseReverseProxy(this IApplicationBuilder app)
{ {
var middleware = app.ApplicationServices.GetRequiredService<ReverseProxyMiddleware>(); var middleware = app.ApplicationServices.GetRequiredService<ReverseProxyMiddleware>();
return app.Use(next => context => middleware.InvokeAsync(context)); return app.Use(next => context => middleware.InvokeAsync(context, next));
} }
} }
} }

View File

@ -3,6 +3,7 @@ using FastGithub.Http;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Yarp.ReverseProxy.Forwarder; using Yarp.ReverseProxy.Forwarder;
@ -17,6 +18,7 @@ namespace FastGithub.ReverseProxy
private readonly IHttpClientFactory httpClientFactory; private readonly IHttpClientFactory httpClientFactory;
private readonly FastGithubConfig fastGithubConfig; private readonly FastGithubConfig fastGithubConfig;
private readonly ILogger<ReverseProxyMiddleware> logger; private readonly ILogger<ReverseProxyMiddleware> logger;
private readonly HashSet<string> localHostNames = LocalMachine.GetAllHostNames();
private readonly DomainConfig defaultDomainConfig = new() { TlsSni = true }; private readonly DomainConfig defaultDomainConfig = new() { TlsSni = true };
public ReverseProxyMiddleware( public ReverseProxyMiddleware(
@ -35,10 +37,17 @@ namespace FastGithub.ReverseProxy
/// 处理请求 /// 处理请求
/// </summary> /// </summary>
/// <param name="context"></param> /// <param name="context"></param>
/// <param name="next"?
/// <returns></returns> /// <returns></returns>
public async Task InvokeAsync(HttpContext context) public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{ {
var host = context.Request.Host.Host; var host = context.Request.Host.Host;
if (this.localHostNames.Contains(host) == true)
{
await next(context);
return;
}
if (this.fastGithubConfig.TryGetDomainConfig(host, out var domainConfig) == false) if (this.fastGithubConfig.TryGetDomainConfig(host, out var domainConfig) == false)
{ {
domainConfig = this.defaultDomainConfig; domainConfig = this.defaultDomainConfig;