FastGithub/FastGithub.ReverseProxy/ReverseProxyApplicationBuilderExtensions.cs
2021-07-13 21:27:53 +08:00

37 lines
1.2 KiB
C#

using FastGithub.ReverseProxy;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Yarp.ReverseProxy.Forwarder;
namespace FastGithub
{
/// <summary>
/// gitub反向代理的中间件扩展
/// </summary>
public static class ReverseProxyApplicationBuilderExtensions
{
/// <summary>
/// 使用gitub反向代理中间件
/// </summary>
/// <param name="app"></param>
/// <returns></returns>
public static IApplicationBuilder UseGithubReverseProxy(this IApplicationBuilder app)
{
var httpForwarder = app.ApplicationServices.GetRequiredService<IHttpForwarder>();
var httpClientFactory = app.ApplicationServices.GetRequiredService<NoneSniHttpClientFactory>();
app.Use(next => async context =>
{
var hostString = context.Request.Host;
var port = hostString.Port ?? 443;
var destinationPrefix = $"http://{hostString.Host}:{port}/";
var httpClient = httpClientFactory.CreateHttpClient();
await httpForwarder.SendAsync(context, destinationPrefix, httpClient);
});
return app;
}
}
}