2
0

ReverseProxyMiddleware.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using FastGithub.Configuration;
  2. using FastGithub.Http;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Logging;
  5. using System;
  6. using System.Threading.Tasks;
  7. using Yarp.ReverseProxy.Forwarder;
  8. namespace FastGithub.ReverseProxy
  9. {
  10. /// <summary>
  11. /// 反向代理中间件
  12. /// </summary>
  13. sealed class ReverseProxyMiddleware
  14. {
  15. private readonly IHttpForwarder httpForwarder;
  16. private readonly IHttpClientFactory httpClientFactory;
  17. private readonly FastGithubConfig fastGithubConfig;
  18. private readonly ILogger<ReverseProxyMiddleware> logger;
  19. public ReverseProxyMiddleware(
  20. IHttpForwarder httpForwarder,
  21. IHttpClientFactory httpClientFactory,
  22. FastGithubConfig fastGithubConfig,
  23. ILogger<ReverseProxyMiddleware> logger)
  24. {
  25. this.httpForwarder = httpForwarder;
  26. this.httpClientFactory = httpClientFactory;
  27. this.fastGithubConfig = fastGithubConfig;
  28. this.logger = logger;
  29. }
  30. /// <summary>
  31. /// 处理请求
  32. /// </summary>
  33. /// <param name="context"></param>
  34. /// <param name="next"></param>
  35. /// <returns></returns>
  36. public async Task InvokeAsync(HttpContext context, RequestDelegate next)
  37. {
  38. var host = context.Request.Host.Host;
  39. if (this.fastGithubConfig.TryGetDomainConfig(host, out var domainConfig) == false)
  40. {
  41. await next(context);
  42. }
  43. else if (domainConfig.Response != null)
  44. {
  45. context.Response.StatusCode = domainConfig.Response.StatusCode;
  46. context.Response.ContentType = domainConfig.Response.ContentType;
  47. if (domainConfig.Response.ContentValue != null)
  48. {
  49. await context.Response.WriteAsync(domainConfig.Response.ContentValue);
  50. }
  51. }
  52. else
  53. {
  54. var destinationPrefix = GetDestinationPrefix(host, domainConfig.Destination);
  55. var httpClient = this.httpClientFactory.CreateHttpClient(domainConfig);
  56. var error = await httpForwarder.SendAsync(context, destinationPrefix, httpClient);
  57. await HandleErrorAsync(context, error);
  58. }
  59. }
  60. /// <summary>
  61. /// 获取目标前缀
  62. /// </summary>
  63. /// <param name="host"></param>
  64. /// <param name="destination"></param>
  65. /// <returns></returns>
  66. private string GetDestinationPrefix(string host, Uri? destination)
  67. {
  68. var defaultValue = $"https://{host}/";
  69. if (destination == null)
  70. {
  71. return defaultValue;
  72. }
  73. var baseUri = new Uri(defaultValue);
  74. var result = new Uri(baseUri, destination).ToString();
  75. this.logger.LogInformation($"[{defaultValue} <-> {result}]");
  76. return result;
  77. }
  78. /// <summary>
  79. /// 处理错误信息
  80. /// </summary>
  81. /// <param name="context"></param>
  82. /// <param name="error"></param>
  83. /// <returns></returns>
  84. private static async Task HandleErrorAsync(HttpContext context, ForwarderError error)
  85. {
  86. if (error == ForwarderError.None)
  87. {
  88. return;
  89. }
  90. var errorFeature = context.GetForwarderErrorFeature();
  91. if (errorFeature == null)
  92. {
  93. return;
  94. }
  95. await context.Response.WriteAsJsonAsync(new
  96. {
  97. error = error.ToString(),
  98. message = errorFeature.Exception?.Message
  99. });
  100. }
  101. }
  102. }