2
0

HttpProxyMiddleware.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. var responseFeature = context.Features.Get<IHttpResponseFeature>();
  83. if (responseFeature != null)
  84. {
  85. responseFeature.ReasonPhrase = "Connection Established";
  86. }
  87. context.Response.StatusCode = StatusCodes.Status200OK;
  88. await context.Response.CompleteAsync();
  89. var transport = context.Features.Get<IConnectionTransportFeature>()?.Transport;
  90. if (transport != null)
  91. {
  92. var targetStream = new NetworkStream(targetSocket, ownsSocket: false);
  93. await Task.WhenAny(targetStream.CopyToAsync(transport.Output), transport.Input.CopyToAsync(targetStream));
  94. }
  95. }
  96. else
  97. {
  98. await this.httpReverseProxy.InvokeAsync(context, async next =>
  99. {
  100. var destinationPrefix = $"{context.Request.Scheme}://{context.Request.Host}";
  101. await this.httpForwarder.SendAsync(context, destinationPrefix, this.defaultHttpClient);
  102. });
  103. }
  104. }
  105. /// <summary>
  106. /// 是否为fastgithub服务
  107. /// </summary>
  108. /// <param name="host"></param>
  109. /// <returns></returns>
  110. private bool IsFastGithubServer(HostString host)
  111. {
  112. return host.Port == this.fastGithubConfig.HttpProxyPort && (host.Host == LOOPBACK || host.Host == LOCALHOST);
  113. }
  114. /// <summary>
  115. /// 创建proxypac脚本
  116. /// </summary>
  117. /// <param name="proxyHost"></param>
  118. /// <returns></returns>
  119. private string CreateProxyPac(HostString proxyHost)
  120. {
  121. var buidler = new StringBuilder();
  122. buidler.AppendLine("function FindProxyForURL(url, host){");
  123. buidler.AppendLine($" var fastgithub = 'PROXY {proxyHost}';");
  124. foreach (var domain in this.fastGithubConfig.GetDomainPatterns())
  125. {
  126. buidler.AppendLine($" if (shExpMatch(host, '{domain}')) return fastgithub;");
  127. }
  128. buidler.AppendLine(" return 'DIRECT';");
  129. buidler.AppendLine("}");
  130. return buidler.ToString();
  131. }
  132. /// <summary>
  133. /// 获取目标终节点
  134. /// </summary>
  135. /// <param name="host"></param>
  136. /// <returns></returns>
  137. private async Task<EndPoint> GetTargetEndPointAsync(HostString host)
  138. {
  139. var targetHost = host.Host;
  140. var targetPort = host.Port ?? HTTPS_PORT;
  141. if (IPAddress.TryParse(targetHost, out var address) == true)
  142. {
  143. return new IPEndPoint(address, targetPort);
  144. }
  145. // 不关心的域名,直接使用系统dns
  146. if (this.fastGithubConfig.IsMatch(targetHost) == false)
  147. {
  148. return new DnsEndPoint(targetHost, targetPort);
  149. }
  150. if (targetPort == HTTP_PORT)
  151. {
  152. return new IPEndPoint(IPAddress.Loopback, ReverseProxyPort.Http);
  153. }
  154. if (targetPort == HTTPS_PORT)
  155. {
  156. return new IPEndPoint(IPAddress.Loopback, ReverseProxyPort.Https);
  157. }
  158. // 不使用系统dns
  159. address = await this.domainResolver.ResolveAnyAsync(new DnsEndPoint(targetHost, targetPort));
  160. return new IPEndPoint(address, targetPort);
  161. }
  162. /// <summary>
  163. /// 创建httpHandler
  164. /// </summary>
  165. /// <returns></returns>
  166. private static SocketsHttpHandler CreateDefaultHttpHandler()
  167. {
  168. return new()
  169. {
  170. Proxy = null,
  171. UseProxy = false,
  172. UseCookies = false,
  173. AllowAutoRedirect = false,
  174. AutomaticDecompression = DecompressionMethods.None
  175. };
  176. }
  177. }
  178. }