HttpProxyMiddleware.cs 6.5 KB

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