HttpProxyMiddleware.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using FastGithub.Configuration;
  2. using FastGithub.DomainResolve;
  3. using Microsoft.AspNetCore.Connections.Features;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Http.Features;
  6. using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
  7. using System.IO.Pipelines;
  8. using System.Net;
  9. using System.Net.Http;
  10. using System.Net.Sockets;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Yarp.ReverseProxy.Forwarder;
  15. namespace FastGithub.HttpServer
  16. {
  17. /// <summary>
  18. /// http代理中间件
  19. /// </summary>
  20. sealed class HttpProxyMiddleware
  21. {
  22. private const string LOOPBACK = "127.0.0.1";
  23. private const string LOCALHOST = "localhost";
  24. private const int HTTP_PORT = 80;
  25. private const int HTTPS_PORT = 443;
  26. private readonly FastGithubConfig fastGithubConfig;
  27. private readonly IDomainResolver domainResolver;
  28. private readonly IHttpForwarder httpForwarder;
  29. private readonly HttpReverseProxyMiddleware httpReverseProxy;
  30. private readonly HttpMessageInvoker defaultHttpClient;
  31. static HttpProxyMiddleware()
  32. {
  33. // https://github.com/dotnet/aspnetcore/issues/37421
  34. var authority = typeof(HttpParser<>).Assembly
  35. .GetType("Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.HttpCharacters")?
  36. .GetField("_authority", BindingFlags.NonPublic | BindingFlags.Static)?
  37. .GetValue(null);
  38. if (authority is bool[] authorityArray)
  39. {
  40. authorityArray['-'] = true;
  41. }
  42. }
  43. /// <summary>
  44. /// http代理中间件
  45. /// </summary>
  46. /// <param name="fastGithubConfig"></param>
  47. /// <param name="domainResolver"></param>
  48. /// <param name="httpForwarder"></param>
  49. /// <param name="httpReverseProxy"></param>
  50. public HttpProxyMiddleware(
  51. FastGithubConfig fastGithubConfig,
  52. IDomainResolver domainResolver,
  53. IHttpForwarder httpForwarder,
  54. HttpReverseProxyMiddleware httpReverseProxy)
  55. {
  56. this.fastGithubConfig = fastGithubConfig;
  57. this.domainResolver = domainResolver;
  58. this.httpForwarder = httpForwarder;
  59. this.httpReverseProxy = httpReverseProxy;
  60. this.defaultHttpClient = new HttpMessageInvoker(CreateDefaultHttpHandler(), disposeHandler: false);
  61. }
  62. /// <summary>
  63. /// 处理请求
  64. /// </summary>
  65. /// <param name="context"></param>
  66. /// <returns></returns>
  67. public async Task InvokeAsync(HttpContext context)
  68. {
  69. var host = context.Request.Host;
  70. if (this.IsFastGithubServer(host) == true)
  71. {
  72. var proxyPac = this.CreateProxyPac(host);
  73. context.Response.ContentType = "application/x-ns-proxy-autoconfig";
  74. context.Response.Headers.Add("Content-Disposition", $"attachment;filename=proxy.pac");
  75. await context.Response.WriteAsync(proxyPac);
  76. }
  77. else if (context.Request.Method == HttpMethods.Connect)
  78. {
  79. var endpoint = await this.GetTargetEndPointAsync(host);
  80. using var targetSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
  81. await targetSocket.ConnectAsync(endpoint);
  82. context.Response.StatusCode = StatusCodes.Status200OK;
  83. context.Features.Get<IHttpResponseFeature>().ReasonPhrase = "Connection Established";
  84. await context.Response.CompleteAsync();
  85. var transport = context.Features.Get<IConnectionTransportFeature>()?.Transport;
  86. if (transport != null)
  87. {
  88. var targetStream = new NetworkStream(targetSocket, ownsSocket: false);
  89. await Task.WhenAny(targetStream.CopyToAsync(transport.Output), transport.Input.CopyToAsync(targetStream));
  90. }
  91. }
  92. else
  93. {
  94. await this.httpReverseProxy.InvokeAsync(context, async ctx =>
  95. {
  96. var destinationPrefix = $"{ctx.Request.Scheme}://{ctx.Request.Host}";
  97. await this.httpForwarder.SendAsync(ctx, destinationPrefix, this.defaultHttpClient);
  98. });
  99. }
  100. }
  101. /// <summary>
  102. /// 是否为fastgithub服务
  103. /// </summary>
  104. /// <param name="host"></param>
  105. /// <returns></returns>
  106. private bool IsFastGithubServer(HostString host)
  107. {
  108. return host.Port == this.fastGithubConfig.HttpProxyPort && (host.Host == LOOPBACK || host.Host == LOCALHOST);
  109. }
  110. /// <summary>
  111. /// 创建proxypac脚本
  112. /// </summary>
  113. /// <param name="proxyHost"></param>
  114. /// <returns></returns>
  115. private string CreateProxyPac(HostString proxyHost)
  116. {
  117. var buidler = new StringBuilder();
  118. buidler.AppendLine("function FindProxyForURL(url, host){");
  119. buidler.AppendLine($" var fastgithub = 'PROXY {proxyHost}';");
  120. foreach (var domain in this.fastGithubConfig.GetDomainPatterns())
  121. {
  122. buidler.AppendLine($" if (shExpMatch(host, '{domain}')) return fastgithub;");
  123. }
  124. buidler.AppendLine(" return 'DIRECT';");
  125. buidler.AppendLine("}");
  126. return buidler.ToString();
  127. }
  128. /// <summary>
  129. /// 获取目标终节点
  130. /// </summary>
  131. /// <param name="host"></param>
  132. /// <returns></returns>
  133. private async Task<EndPoint> GetTargetEndPointAsync(HostString host)
  134. {
  135. var targetHost = host.Host;
  136. var targetPort = host.Port ?? HTTPS_PORT;
  137. if (IPAddress.TryParse(targetHost, out var address) == true)
  138. {
  139. return new IPEndPoint(address, targetPort);
  140. }
  141. // 不关心的域名,直接使用系统dns
  142. if (this.fastGithubConfig.IsMatch(targetHost) == false)
  143. {
  144. return new DnsEndPoint(targetHost, targetPort);
  145. }
  146. if (targetPort == HTTP_PORT)
  147. {
  148. return new IPEndPoint(IPAddress.Loopback, ReverseProxyPort.Http);
  149. }
  150. if (targetPort == HTTPS_PORT)
  151. {
  152. return new IPEndPoint(IPAddress.Loopback, ReverseProxyPort.Https);
  153. }
  154. // 不使用系统dns
  155. address = await this.domainResolver.ResolveAnyAsync(new DnsEndPoint(targetHost, targetPort));
  156. return new IPEndPoint(address, targetPort);
  157. }
  158. /// <summary>
  159. /// 创建httpHandler
  160. /// </summary>
  161. /// <returns></returns>
  162. private static SocketsHttpHandler CreateDefaultHttpHandler()
  163. {
  164. return new()
  165. {
  166. Proxy = null,
  167. UseProxy = false,
  168. UseCookies = false,
  169. AllowAutoRedirect = false,
  170. AutomaticDecompression = DecompressionMethods.None
  171. };
  172. }
  173. }
  174. }