TcpInterceptor.cs 4.2 KB

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