TcpTable.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Buffers.Binary;
  3. using System.Diagnostics;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Runtime.InteropServices;
  7. using System.Runtime.Versioning;
  8. namespace FastGithub.ReverseProxy
  9. {
  10. /// <summary>
  11. /// windows iphlpapi
  12. /// </summary>
  13. [SupportedOSPlatform("windows")]
  14. unsafe static class TcpTable
  15. {
  16. private const int ERROR_INSUFFICIENT_BUFFER = 122;
  17. [DllImport("iphlpapi.dll", SetLastError = true)]
  18. private static extern uint GetExtendedTcpTable(void* pTcpTable, ref int pdwSize, bool bOrder, AddressFamily ulAf, TCP_TABLE_CLASS tableClass, uint reserved = 0);
  19. /// <summary>
  20. /// 杀死占用进程
  21. /// </summary>
  22. /// <param name="port"></param>
  23. /// <returns></returns>
  24. public static bool KillPortOwner(int port)
  25. {
  26. if (TryGetOwnerProcessId(port, out var pid) == false)
  27. {
  28. return true;
  29. }
  30. try
  31. {
  32. var proess = Process.GetProcessById(pid);
  33. proess.Kill();
  34. proess.WaitForExit();
  35. return true;
  36. }
  37. catch (ArgumentException)
  38. {
  39. return true;
  40. }
  41. catch (Exception)
  42. {
  43. return false;
  44. }
  45. }
  46. /// <summary>
  47. /// 获取tcp端口的占用进程id
  48. /// </summary>
  49. /// <param name="port"></param>
  50. /// <param name="processId"></param>
  51. /// <returns></returns>
  52. public static bool TryGetOwnerProcessId(int port, out int processId)
  53. {
  54. processId = 0;
  55. var pdwSize = 0;
  56. var result = GetExtendedTcpTable(null, ref pdwSize, false, AddressFamily.InterNetwork, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_LISTENER);
  57. if (result != ERROR_INSUFFICIENT_BUFFER)
  58. {
  59. return false;
  60. }
  61. var buffer = new byte[pdwSize];
  62. fixed (byte* pTcpTable = &buffer[0])
  63. {
  64. result = GetExtendedTcpTable(pTcpTable, ref pdwSize, false, AddressFamily.InterNetwork, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_LISTENER);
  65. if (result != 0)
  66. {
  67. return false;
  68. }
  69. var prt = new IntPtr(pTcpTable);
  70. var table = Marshal.PtrToStructure<MIB_TCPTABLE_OWNER_PID>(prt);
  71. prt += sizeof(int);
  72. for (var i = 0; i < table.dwNumEntries; i++)
  73. {
  74. var row = Marshal.PtrToStructure<MIB_TCPROW_OWNER_PID>(prt);
  75. if (row.LocalPort == port)
  76. {
  77. processId = row.ProcessId;
  78. return true;
  79. }
  80. prt += Marshal.SizeOf<MIB_TCPROW_OWNER_PID>();
  81. }
  82. }
  83. return false;
  84. }
  85. private enum TCP_TABLE_CLASS
  86. {
  87. TCP_TABLE_BASIC_LISTENER,
  88. TCP_TABLE_BASIC_CONNECTIONS,
  89. TCP_TABLE_BASIC_ALL,
  90. TCP_TABLE_OWNER_PID_LISTENER,
  91. TCP_TABLE_OWNER_PID_CONNECTIONS,
  92. TCP_TABLE_OWNER_PID_ALL,
  93. TCP_TABLE_OWNER_MODULE_LISTENER,
  94. TCP_TABLE_OWNER_MODULE_CONNECTIONS,
  95. TCP_TABLE_OWNER_MODULE_ALL
  96. }
  97. [StructLayout(LayoutKind.Sequential)]
  98. private struct MIB_TCPTABLE_OWNER_PID
  99. {
  100. public uint dwNumEntries;
  101. }
  102. [StructLayout(LayoutKind.Sequential)]
  103. private struct MIB_TCPROW_OWNER_PID
  104. {
  105. public uint state;
  106. public uint localAddr;
  107. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  108. public byte[] localPort;
  109. public uint remoteAddr;
  110. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  111. public byte[] remotePort;
  112. public int owningPid;
  113. public int ProcessId => owningPid;
  114. public IPAddress LocalAddress => new(localAddr);
  115. public ushort LocalPort => BinaryPrimitives.ReadUInt16BigEndian(this.localPort);
  116. }
  117. }
  118. }