2
0

ReverseProxyApplicationBuilderExtensions.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using FastGithub.ReverseProxy;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Yarp.ReverseProxy.Forwarder;
  5. namespace FastGithub
  6. {
  7. /// <summary>
  8. /// gitub反向代理的中间件扩展
  9. /// </summary>
  10. public static class ReverseProxyApplicationBuilderExtensions
  11. {
  12. /// <summary>
  13. /// 使用gitub反向代理中间件
  14. /// </summary>
  15. /// <param name="app"></param>
  16. /// <returns></returns>
  17. public static IApplicationBuilder UseGithubReverseProxy(this IApplicationBuilder app)
  18. {
  19. var httpForwarder = app.ApplicationServices.GetRequiredService<IHttpForwarder>();
  20. var httpClient = app.ApplicationServices.GetRequiredService<NoneSniHttpClient>();
  21. app.Use(next => async context =>
  22. {
  23. var hostString = context.Request.Host;
  24. var port = hostString.Port ?? 443;
  25. var destinationPrefix = $"http://{hostString.Host}:{port}/";
  26. await httpForwarder.SendAsync(context, destinationPrefix, httpClient);
  27. });
  28. return app;
  29. }
  30. }
  31. }