NetFlowService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Oreo.PCMonitor.Commons;
  2. using Oreo.PCMonitor.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Net;
  8. using Y.Utils.AppUtils;
  9. using Y.Utils.DataUtils.Collections;
  10. using Y.Utils.NetUtils.NetInfoUtils;
  11. using Y.Utils.WindowsUtils.ProcessUtils;
  12. namespace Oreo.PCMonitor.Services
  13. {
  14. public class NetFlowService
  15. {
  16. public bool IsNetFlowRun { get { return _IsNetFlowRun; } }
  17. private bool _IsNetFlowRun = false;
  18. public bool IsNetPacketRun { get { return _IsNetPacketRun; } }
  19. private bool _IsNetPacketRun = false;
  20. public List<NetProcessInfo> NetProcessInfoList = new List<NetProcessInfo>();
  21. public List<NetConnectionInfo> NetConnectionInfoList = new List<NetConnectionInfo>();
  22. public NetFlowTool NetFlow = new NetFlowTool();
  23. List<NetPacketTool> NetPacketList = new List<NetPacketTool>();
  24. NetProcessTool.TcpRow[] TcpConnection;
  25. NetProcessTool.UdpRow[] UdpConnection;
  26. Process[] NowProcess;
  27. List<string> AllIPv4Address = new List<string>();
  28. public long LostPacketCount { get; set; }
  29. public void Start()
  30. {
  31. #region 启动系统性能计数器统计
  32. try
  33. {
  34. NetFlow.Start();
  35. NetFlow.DataMonitorEvent += DataMonitorEvent;
  36. _IsNetFlowRun = true;
  37. }
  38. catch { }
  39. #endregion
  40. #region 启动Socket包统计
  41. if (PermissionTool.IsAdmin())
  42. {
  43. List<IPAddress> hosts = NetCardInfoTool.GetIPv4Address();
  44. AllIPv4Address = NetCardInfoTool.GetAllIPv4Address();
  45. foreach (var host in hosts)
  46. {
  47. try
  48. {
  49. NetPacketTool p = new NetPacketTool(host);
  50. p.NewPacket += new NewPacketEventHandler(NewPacketEvent);
  51. p.Start();
  52. NetPacketList.Add(p);
  53. }
  54. catch { }
  55. }
  56. if (ListTool.HasElements(NetPacketList)) _IsNetPacketRun = true;
  57. }
  58. #endregion
  59. }
  60. public void Stop()
  61. {
  62. if (_IsNetFlowRun)
  63. {
  64. NetFlow.Stop();
  65. _IsNetFlowRun = false;
  66. }
  67. if (_IsNetPacketRun)
  68. {
  69. NetPacketList.ForEach(x => { x.Stop(); });
  70. _IsNetPacketRun = false;
  71. }
  72. }
  73. public void DataMonitorEvent(NetFlowTool n)
  74. {
  75. NowProcess = Process.GetProcesses();
  76. GetConnection();
  77. SetNetProcess();
  78. CalcNetProcessInfo();
  79. //#region 统计
  80. //p.Protocol == Protocol.Tcp
  81. //#endregion
  82. }
  83. private void NewPacketEvent(NetPacketTool tool, Packet packet)
  84. {
  85. bool isGather = false;
  86. #region 整理TCP包
  87. if (packet.Protocol == Protocol.Tcp && ListTool.HasElements(TcpConnection) && ListTool.HasElements(NowProcess))
  88. {
  89. lock (TcpConnection)
  90. {
  91. // tcp 下载
  92. if (TcpConnection.Any(x => x.RemoteIP.ToString() == packet.DestinationAddress.ToString() && x.RemotePort == packet.DestinationPort))
  93. {
  94. var tcpDownload = TcpConnection.FirstOrDefault(x => x.RemoteIP.ToString() == packet.DestinationAddress.ToString() && x.RemotePort == packet.DestinationPort);
  95. var process = NowProcess.FirstOrDefault(x => x.Id == tcpDownload.ProcessId);
  96. if (process != null)
  97. {
  98. var info = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
  99. if (info != null)
  100. {
  101. isGather = true;
  102. info.DownloadBag += packet.TotalLength;
  103. }
  104. }
  105. }
  106. // tcp 上传
  107. if (TcpConnection.Any(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort))
  108. {
  109. var tcUpload = TcpConnection.FirstOrDefault(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort);
  110. var process = NowProcess.FirstOrDefault(x => x.Id == tcUpload.ProcessId);
  111. if (process != null)
  112. {
  113. var info = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
  114. if (info != null)
  115. {
  116. isGather = true;
  117. info.UploadBag += packet.TotalLength;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. #endregion
  124. #region 整理UDP包
  125. if (packet.Protocol == Protocol.Udp && ListTool.HasElements(UdpConnection) && ListTool.HasElements(NowProcess))
  126. {
  127. lock (UdpConnection)
  128. {
  129. // tcp 下载
  130. if (UdpConnection.Any(x => x.LocalPort == packet.DestinationPort) && AllIPv4Address.Contains(packet.DestinationAddress.ToString()))
  131. {
  132. var udpDownload = UdpConnection.FirstOrDefault(x => AllIPv4Address.Contains(x.LocalIP.ToString()) && x.LocalPort == packet.DestinationPort);
  133. var process = NowProcess.FirstOrDefault(x => x.Id == udpDownload.ProcessId);
  134. if (process != null)
  135. {
  136. var info = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
  137. if (info != null)
  138. {
  139. isGather = true;
  140. info.DownloadBag += packet.TotalLength;
  141. if (info.ProcessName == "Idle")
  142. {
  143. }
  144. }
  145. }
  146. }
  147. // udp 上传
  148. if (UdpConnection.Any(x => x.LocalPort == packet.SourcePort) && AllIPv4Address.Contains(packet.SourceAddress.ToString()))
  149. {
  150. var udpIp = AllIPv4Address.FirstOrDefault(x => x == packet.SourceAddress.ToString());
  151. var ucUpload = UdpConnection.FirstOrDefault(x => AllIPv4Address.Contains(x.LocalIP.ToString()) && x.LocalPort == packet.SourcePort);
  152. var process = NowProcess.FirstOrDefault(x => x.Id == ucUpload.ProcessId);
  153. if (process != null)
  154. {
  155. var info = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
  156. if (info != null)
  157. {
  158. isGather = true;
  159. info.UploadBag += packet.TotalLength;
  160. if (info.ProcessName == "Idle")
  161. {
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. #endregion
  169. if (!isGather) LostPacketCount++;
  170. }
  171. #region 获取当前程序的所有连接
  172. void GetConnection()
  173. {
  174. TcpConnection = NetProcessTool.GetTcpConnection();
  175. UdpConnection = NetProcessTool.GetUdpConnection();
  176. }
  177. #endregion
  178. #region 设置程序流量及连接数统计列表
  179. void SetNetProcess()
  180. {
  181. // 清空已有连接数
  182. if (ListTool.HasElements(NetProcessInfoList))
  183. NetProcessInfoList.ForEach(x =>
  184. {
  185. x.ConnectCount = 0;
  186. });
  187. // 统计TCP连接数
  188. if (ListTool.HasElements(TcpConnection))
  189. {
  190. foreach (var t in TcpConnection)
  191. {
  192. SetNetProcessConnection(t.ProcessId);
  193. }
  194. }
  195. // 统计UDP连接数
  196. if (ListTool.HasElements(UdpConnection))
  197. {
  198. foreach (var u in UdpConnection)
  199. {
  200. SetNetProcessConnection(u.ProcessId);
  201. }
  202. }
  203. }
  204. void SetNetProcessConnection(int pid)
  205. {
  206. try
  207. {
  208. Process p = NowProcess.FirstOrDefault(x => x.Id == pid);
  209. if (p != null)
  210. {
  211. var ppl = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == p.ProcessName);
  212. if (ppl == null)
  213. {
  214. NetProcessInfoList.Add(
  215. new NetProcessInfo()
  216. {
  217. ProcessIcon = ProcessInfoTool.GetIcon(p, false),
  218. ProcessName = p.ProcessName,
  219. ConnectCount = 1,
  220. LastUpdateTime = DateTime.Now,
  221. });
  222. }
  223. else
  224. {
  225. ppl.ConnectCount++;
  226. ppl.LastUpdateTime = DateTime.Now;
  227. }
  228. }
  229. }
  230. catch (Exception e)
  231. {
  232. R.Log.e("对程序列表和网络连接列表整理时发生错误");
  233. R.Log.e(e.Message);
  234. }
  235. }
  236. #endregion
  237. #region 整理程序流量汇总信息
  238. void CalcNetProcessInfo()
  239. {
  240. if (ListTool.HasElements(NetProcessInfoList))
  241. {
  242. NetProcessInfoList.ForEach(p =>
  243. {
  244. p.UploadDataCount += p.UploadData;
  245. p.DownloadDataCount += p.DownloadData;
  246. });
  247. NetProcessInfoList.ForEach(p =>
  248. {
  249. p.LastUpdateTime = DateTime.Now;
  250. p.UploadData = (int)((float)p.UploadBag / (NetProcessInfoList.Sum(x => x.UploadBag) + 0.01) * NetFlow.UploadData);
  251. p.DownloadData = (int)((float)p.DownloadBag / (NetProcessInfoList.Sum(x => x.DownloadBag + 0.01)) * NetFlow.DownloadData);
  252. p.UploadBagCount += p.UploadBag;
  253. p.DownloadBagCount += p.DownloadBag;
  254. p.UploadBag = 0;
  255. p.DownloadBag = 0;
  256. });
  257. }
  258. }
  259. #endregion
  260. }
  261. }