2
0

HttpProxyMiddleware.cs 6.6 KB

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