NetProcessTool.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using Azylee.Core.DataUtils.CollectionUtils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. namespace Azylee.Core.NetUtils
  10. {
  11. public class NetProcessTool
  12. {
  13. #region 枚举 读取连接表选项
  14. enum TCP_TABLE_CLASS
  15. {
  16. TCP_TABLE_BASIC_LISTENER,
  17. TCP_TABLE_BASIC_CONNECTIONS,
  18. TCP_TABLE_BASIC_ALL,
  19. TCP_TABLE_OWNER_PID_LISTENER,
  20. TCP_TABLE_OWNER_PID_CONNECTIONS,
  21. TCP_TABLE_OWNER_PID_ALL,
  22. TCP_TABLE_OWNER_MODULE_LISTENER,
  23. TCP_TABLE_OWNER_MODULE_CONNECTIONS,
  24. TCP_TABLE_OWNER_MODULE_ALL
  25. }
  26. enum UDP_TABLE_CLASS
  27. {
  28. UDP_TABLE_BASIC,
  29. UDP_TABLE_OWNER_PID,
  30. UDP_TABLE_OWNER_MODULE
  31. }
  32. #endregion
  33. #region UDP 和 TCP 列表 结构
  34. struct UdpTable
  35. {
  36. public uint dwNumEntries;
  37. private UdpRow table;
  38. }
  39. struct TcpTable
  40. {
  41. public uint dwNumEntries;
  42. private TcpRow table;
  43. }
  44. #endregion
  45. #region UDP 和 TCP 行记录 结构
  46. public struct UdpRow
  47. {
  48. private uint localAddr;
  49. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  50. private byte[] localPort;
  51. public IPAddress LocalIP
  52. {
  53. get
  54. {
  55. return new IPAddress((long)((ulong)this.localAddr));
  56. }
  57. }
  58. public ushort LocalPort
  59. {
  60. get
  61. {
  62. return BitConverter.ToUInt16(new byte[]
  63. {
  64. this.localPort[1],
  65. this.localPort[0]
  66. }, 0);
  67. }
  68. }
  69. public int ProcessId { get; }
  70. }
  71. public struct TcpRow
  72. {
  73. private ConnectionState state;
  74. private uint localAddr;
  75. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  76. private byte[] localPort;
  77. private uint remoteAddr;
  78. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
  79. private byte[] remotePort;
  80. public ConnectionState State
  81. {
  82. get { return state; }
  83. }
  84. public IPAddress LocalIP
  85. {
  86. get
  87. {
  88. return new IPAddress((long)((ulong)this.localAddr));
  89. }
  90. }
  91. public ushort LocalPort
  92. {
  93. get
  94. {
  95. return BitConverter.ToUInt16(new byte[]
  96. {
  97. this.localPort[1],
  98. this.localPort[0]
  99. }, 0);
  100. }
  101. }
  102. public IPAddress RemoteIP
  103. {
  104. get
  105. {
  106. return new IPAddress((long)((ulong)this.remoteAddr));
  107. }
  108. }
  109. public ushort RemotePort
  110. {
  111. get
  112. {
  113. return BitConverter.ToUInt16(new byte[]
  114. {
  115. this.remotePort[1],
  116. this.remotePort[0]
  117. }, 0);
  118. }
  119. }
  120. public int ProcessId { get; }
  121. }
  122. #endregion
  123. [DllImport("iphlpapi.dll", SetLastError = true)]
  124. static extern uint GetExtendedTcpTable(IntPtr pTcpTable, ref int dwOutBufLen, bool sort, int ipVersion, TCP_TABLE_CLASS tblClass, uint reserved = 0u);
  125. [DllImport("iphlpapi.dll", SetLastError = true)]
  126. static extern uint GetExtendedUdpTable(IntPtr pUdpTable, ref int dwOutBufLen, bool sort, int ipVersion, UDP_TABLE_CLASS tblClass, uint reserved = 0u);
  127. private static TcpRow[] GetTcpConnections()
  128. {
  129. TcpRow[] array = null;
  130. int ipVersion = 2;
  131. int cb = 0;
  132. uint extendedTcpTable = GetExtendedTcpTable(IntPtr.Zero, ref cb, true, ipVersion, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0u);
  133. IntPtr intPtr = Marshal.AllocHGlobal(cb);
  134. try
  135. {
  136. extendedTcpTable = GetExtendedTcpTable(intPtr, ref cb, true, ipVersion, TCP_TABLE_CLASS.TCP_TABLE_OWNER_PID_ALL, 0u);
  137. bool flag = extendedTcpTable > 0u;
  138. if (flag) return null;
  139. TcpTable tcpTable = (TcpTable)Marshal.PtrToStructure(intPtr, typeof(TcpTable));
  140. IntPtr intPtr2 = (IntPtr)((long)intPtr + Marshal.SizeOf(tcpTable.dwNumEntries));
  141. array = new TcpRow[tcpTable.dwNumEntries];
  142. int num = 0;
  143. while (num < (long)((ulong)tcpTable.dwNumEntries))
  144. {
  145. TcpRow tcpRow = (TcpRow)Marshal.PtrToStructure(intPtr2, typeof(TcpRow));
  146. array[num] = tcpRow;
  147. intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(tcpRow));
  148. num++;
  149. }
  150. }
  151. catch (Exception)
  152. { }
  153. finally
  154. {
  155. Marshal.FreeHGlobal(intPtr);
  156. }
  157. return array;
  158. }
  159. private static UdpRow[] GetUdpConnections()
  160. {
  161. UdpRow[] array = null;
  162. int ipVersion = 2;
  163. int cb = 0;
  164. uint extendedUdpTable = GetExtendedUdpTable(IntPtr.Zero, ref cb, true, ipVersion, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0u);
  165. IntPtr intPtr = Marshal.AllocHGlobal(cb);
  166. try
  167. {
  168. extendedUdpTable = GetExtendedUdpTable(intPtr, ref cb, true, ipVersion, UDP_TABLE_CLASS.UDP_TABLE_OWNER_PID, 0u);
  169. bool flag = extendedUdpTable > 0u;
  170. if (flag) return null;
  171. UdpTable udpTable = (UdpTable)Marshal.PtrToStructure(intPtr, typeof(UdpTable));
  172. IntPtr intPtr2 = (IntPtr)((long)intPtr + Marshal.SizeOf(udpTable.dwNumEntries));
  173. array = new UdpRow[udpTable.dwNumEntries];
  174. int num = 0;
  175. while (num < (long)((ulong)udpTable.dwNumEntries))
  176. {
  177. UdpRow udpRow = (UdpRow)Marshal.PtrToStructure(intPtr2, typeof(UdpRow));
  178. array[num] = udpRow;
  179. intPtr2 = (IntPtr)((long)intPtr2 + Marshal.SizeOf(udpRow));
  180. num++;
  181. }
  182. }
  183. catch (Exception)
  184. { }
  185. finally
  186. {
  187. Marshal.FreeHGlobal(intPtr);
  188. }
  189. return array;
  190. }
  191. /// <summary>
  192. /// 获取本机所有TCP连接
  193. /// </summary>
  194. /// <returns></returns>
  195. public static TcpRow[] GetTcps()
  196. {
  197. return GetTcpConnections();
  198. }
  199. /// <summary>
  200. /// 获取本机所有UDP连接
  201. /// </summary>
  202. /// <returns></returns>
  203. public static UdpRow[] GetUdps()
  204. {
  205. return GetUdpConnections();
  206. }
  207. #region 便捷方法
  208. /// <summary>
  209. /// 根据端口号获取进程ID
  210. /// </summary>
  211. /// <param name="port"></param>
  212. /// <returns></returns>
  213. public static int GetPidByPort(int port)
  214. {
  215. try
  216. {
  217. TcpRow[] list = GetTcps();
  218. if (Ls.Ok(list))
  219. foreach (var item in list)
  220. if (item.LocalPort == port) return item.ProcessId;
  221. }
  222. catch { }
  223. try
  224. {
  225. UdpRow[] list = GetUdps();
  226. if (Ls.Ok(list))
  227. foreach (var item in list)
  228. if (item.LocalPort == port) return item.ProcessId;
  229. }
  230. catch { }
  231. return -1;
  232. }
  233. #endregion
  234. }
  235. }