TcpInterceptor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Buffers.Binary;
  4. using System.Net;
  5. using System.Runtime.Versioning;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using WinDivertSharp;
  9. namespace FastGithub.PacketIntercept.Tcp
  10. {
  11. /// <summary>
  12. /// tcp拦截器
  13. /// </summary>
  14. [SupportedOSPlatform("windows")]
  15. abstract class TcpInterceptor : ITcpInterceptor
  16. {
  17. private readonly string filter;
  18. private readonly ushort oldServerPort;
  19. private readonly ushort newServerPort;
  20. private readonly ILogger logger;
  21. /// <summary>
  22. /// tcp拦截器
  23. /// </summary>
  24. /// <param name="oldServerPort">修改前的服务器端口</param>
  25. /// <param name="newServerPort">修改后的服务器端口</param>
  26. /// <param name="logger"></param>
  27. public TcpInterceptor(int oldServerPort, int newServerPort, ILogger logger)
  28. {
  29. this.filter = $"loopback and (tcp.DstPort == {oldServerPort} or tcp.SrcPort == {newServerPort})";
  30. this.oldServerPort = BinaryPrimitives.ReverseEndianness((ushort)oldServerPort);
  31. this.newServerPort = BinaryPrimitives.ReverseEndianness((ushort)newServerPort);
  32. this.logger = logger;
  33. }
  34. /// <summary>
  35. /// 拦截指定端口的数据包
  36. /// </summary>
  37. /// <param name="cancellationToken"></param>
  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 == IntPtr.Zero)
  47. {
  48. return;
  49. }
  50. this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(this.oldServerPort)} => tcp://{IPAddress.Loopback}:{BinaryPrimitives.ReverseEndianness(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))
  58. {
  59. try
  60. {
  61. this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
  62. }
  63. catch (Exception ex)
  64. {
  65. this.logger.LogWarning(ex.Message);
  66. }
  67. finally
  68. {
  69. WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
  70. }
  71. }
  72. }
  73. }
  74. /// <summary>
  75. /// 修改tcp数据端口的端口
  76. /// </summary>
  77. /// <param name="winDivertBuffer"></param>
  78. /// <param name="winDivertAddress"></param>
  79. /// <param name="packetLength"></param>
  80. unsafe private void ModifyTcpPacket(WinDivertBuffer winDivertBuffer, ref WinDivertAddress winDivertAddress, ref uint packetLength)
  81. {
  82. var packet = WinDivert.WinDivertHelperParsePacket(winDivertBuffer, packetLength);
  83. if (packet.TcpHeader->DstPort == oldServerPort)
  84. {
  85. packet.TcpHeader->DstPort = this.newServerPort;
  86. }
  87. else
  88. {
  89. packet.TcpHeader->SrcPort = oldServerPort;
  90. }
  91. winDivertAddress.Impostor = true;
  92. WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
  93. }
  94. }
  95. }