TcpInterceptor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using FastGithub.WinDiverts;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Runtime.Versioning;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace FastGithub.PacketIntercept.Tcp
  9. {
  10. /// <summary>
  11. /// tcp拦截器
  12. /// </summary>
  13. [SupportedOSPlatform("windows")]
  14. abstract class TcpInterceptor : ITcpInterceptor
  15. {
  16. private readonly string filter;
  17. private readonly ushort oldServerPort;
  18. private readonly ushort newServerPort;
  19. private readonly ILogger logger;
  20. /// <summary>
  21. /// tcp拦截器
  22. /// </summary>
  23. /// <param name="oldServerPort">修改前的服务器端口</param>
  24. /// <param name="newServerPort">修改后的服务器端口</param>
  25. /// <param name="logger"></param>
  26. public TcpInterceptor(int oldServerPort, int newServerPort, ILogger logger)
  27. {
  28. this.filter = $"loopback and (tcp.DstPort == {oldServerPort} or tcp.SrcPort == {newServerPort})";
  29. this.oldServerPort = (ushort)oldServerPort;
  30. this.newServerPort = (ushort)newServerPort;
  31. this.logger = logger;
  32. }
  33. /// <summary>
  34. /// 拦截指定端口的数据包
  35. /// </summary>
  36. /// <param name="cancellationToken"></param>
  37. /// <exception cref="Win32Exception"></exception>
  38. public async Task InterceptAsync(CancellationToken cancellationToken)
  39. {
  40. if (this.oldServerPort == this.newServerPort)
  41. {
  42. return;
  43. }
  44. await Task.Yield();
  45. var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
  46. if (handle == new IntPtr(unchecked((long)ulong.MaxValue)))
  47. {
  48. throw new Win32Exception();
  49. }
  50. this.logger.LogInformation($"tcp://localhost:{this.oldServerPort} => tcp://localhost:{this.newServerPort}");
  51. cancellationToken.Register(hwnd => WinDivert.WinDivertClose((IntPtr)hwnd!), handle);
  52. var packetLength = 0U;
  53. using var winDivertBuffer = new WinDivertBuffer();
  54. var winDivertAddress = new WinDivertAddress();
  55. while (cancellationToken.IsCancellationRequested == false)
  56. {
  57. if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
  58. {
  59. throw new Win32Exception();
  60. }
  61. try
  62. {
  63. this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
  64. }
  65. catch (Exception ex)
  66. {
  67. this.logger.LogWarning(ex.Message);
  68. }
  69. finally
  70. {
  71. WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
  72. }
  73. }
  74. }
  75. /// <summary>
  76. /// 修改tcp数据端口的端口
  77. /// </summary>
  78. /// <param name="winDivertBuffer"></param>
  79. /// <param name="winDivertAddress"></param>
  80. /// <param name="packetLength"></param>
  81. unsafe private void ModifyTcpPacket(WinDivertBuffer winDivertBuffer, ref WinDivertAddress winDivertAddress, ref uint packetLength)
  82. {
  83. var packet = WinDivert.WinDivertHelperParsePacket(winDivertBuffer, packetLength);
  84. if (packet.TcpHeader->DstPort == oldServerPort)
  85. {
  86. packet.TcpHeader->DstPort = this.newServerPort;
  87. }
  88. else
  89. {
  90. packet.TcpHeader->SrcPort = oldServerPort;
  91. }
  92. winDivertAddress.Impostor = true;
  93. WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
  94. }
  95. }
  96. }