TcpInterceptor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using FastGithub.WinDiverts;
  2. using Microsoft.Extensions.Logging;
  3. using System;
  4. using System.ComponentModel;
  5. using System.Net;
  6. using System.Runtime.Versioning;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  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 = (ushort)oldServerPort;
  31. this.newServerPort = (ushort)newServerPort;
  32. this.logger = logger;
  33. }
  34. /// <summary>
  35. /// 拦截指定端口的数据包
  36. /// </summary>
  37. /// <param name="cancellationToken"></param>
  38. /// <exception cref="Win32Exception"></exception>
  39. public async Task InterceptAsync(CancellationToken cancellationToken)
  40. {
  41. if (this.oldServerPort == this.newServerPort)
  42. {
  43. return;
  44. }
  45. await Task.Yield();
  46. var handle = WinDivert.WinDivertOpen(this.filter, WinDivertLayer.Network, 0, WinDivertOpenFlags.None);
  47. if (handle == new IntPtr(unchecked((long)ulong.MaxValue)))
  48. {
  49. throw new Win32Exception();
  50. }
  51. this.logger.LogInformation($"tcp://{IPAddress.Loopback}:{this.oldServerPort} => tcp://{IPAddress.Loopback}:{this.newServerPort}");
  52. cancellationToken.Register(hwnd => WinDivert.WinDivertClose((IntPtr)hwnd!), handle);
  53. var packetLength = 0U;
  54. using var winDivertBuffer = new WinDivertBuffer();
  55. var winDivertAddress = new WinDivertAddress();
  56. while (cancellationToken.IsCancellationRequested == false)
  57. {
  58. if (WinDivert.WinDivertRecv(handle, winDivertBuffer, ref winDivertAddress, ref packetLength) == false)
  59. {
  60. throw new Win32Exception();
  61. }
  62. try
  63. {
  64. this.ModifyTcpPacket(winDivertBuffer, ref winDivertAddress, ref packetLength);
  65. }
  66. catch (Exception ex)
  67. {
  68. this.logger.LogWarning(ex.Message);
  69. }
  70. finally
  71. {
  72. WinDivert.WinDivertSend(handle, winDivertBuffer, packetLength, ref winDivertAddress);
  73. }
  74. }
  75. }
  76. /// <summary>
  77. /// 修改tcp数据端口的端口
  78. /// </summary>
  79. /// <param name="winDivertBuffer"></param>
  80. /// <param name="winDivertAddress"></param>
  81. /// <param name="packetLength"></param>
  82. unsafe private void ModifyTcpPacket(WinDivertBuffer winDivertBuffer, ref WinDivertAddress winDivertAddress, ref uint packetLength)
  83. {
  84. var packet = WinDivert.WinDivertHelperParsePacket(winDivertBuffer, packetLength);
  85. if (packet.TcpHeader->DstPort == oldServerPort)
  86. {
  87. packet.TcpHeader->DstPort = this.newServerPort;
  88. }
  89. else
  90. {
  91. packet.TcpHeader->SrcPort = oldServerPort;
  92. }
  93. winDivertAddress.Impostor = true;
  94. WinDivert.WinDivertHelperCalcChecksums(winDivertBuffer, packetLength, ref winDivertAddress, WinDivertChecksumHelperParam.All);
  95. }
  96. }
  97. }