NetFlowService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. info.DownloadBagCount += packet.TotalLength;
  104. }
  105. }
  106. }
  107. // tcp 上传
  108. if (TcpConnection.Any(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort))
  109. {
  110. var tcUpload = TcpConnection.FirstOrDefault(x => x.LocalIP.ToString() == packet.SourceAddress.ToString() && x.LocalPort == packet.SourcePort);
  111. var process = NowProcess.FirstOrDefault(x => x.Id == tcUpload.ProcessId);
  112. if (process != null)
  113. {
  114. var info = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
  115. if (info != null)
  116. {
  117. isGather = true;
  118. info.UploadBag += packet.TotalLength;
  119. info.UploadBagCount += packet.TotalLength;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. #endregion
  126. #region 整理UDP包
  127. if (packet.Protocol == Protocol.Udp && ListTool.HasElements(UdpConnection) && ListTool.HasElements(NowProcess))
  128. {
  129. lock (UdpConnection)
  130. {
  131. // tcp 下载
  132. if (UdpConnection.Any(x => x.LocalPort == packet.DestinationPort) && AllIPv4Address.Contains(packet.DestinationAddress.ToString()))
  133. {
  134. var udpDownload = UdpConnection.FirstOrDefault(x => AllIPv4Address.Contains(x.LocalIP.ToString()) && x.LocalPort == packet.DestinationPort);
  135. var process = NowProcess.FirstOrDefault(x => x.Id == udpDownload.ProcessId);
  136. if (process != null)
  137. {
  138. var info = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
  139. if (info != null)
  140. {
  141. isGather = true;
  142. info.DownloadBag += packet.TotalLength;
  143. info.DownloadBagCount += packet.TotalLength;
  144. if (info.ProcessName == "Idle")
  145. {
  146. }
  147. }
  148. }
  149. }
  150. // udp 上传
  151. if (UdpConnection.Any(x => x.LocalPort == packet.SourcePort) && AllIPv4Address.Contains(packet.SourceAddress.ToString()))
  152. {
  153. var udpIp = AllIPv4Address.FirstOrDefault(x => x == packet.SourceAddress.ToString());
  154. var ucUpload = UdpConnection.FirstOrDefault(x => AllIPv4Address.Contains(x.LocalIP.ToString()) && x.LocalPort == packet.SourcePort);
  155. var process = NowProcess.FirstOrDefault(x => x.Id == ucUpload.ProcessId);
  156. if (process != null)
  157. {
  158. var info = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == process.ProcessName);
  159. if (info != null)
  160. {
  161. isGather = true;
  162. info.UploadBag += packet.TotalLength;
  163. info.UploadBagCount += packet.TotalLength;
  164. if (info.ProcessName == "Idle")
  165. {
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. #endregion
  173. if (!isGather)
  174. {
  175. LostPacketCount++;
  176. }
  177. }
  178. #region 获取当前程序的所有连接
  179. void GetConnection()
  180. {
  181. TcpConnection = NetProcessTool.GetTcpConnection();
  182. UdpConnection = NetProcessTool.GetUdpConnection();
  183. }
  184. #endregion
  185. #region 设置程序流量及连接数统计列表
  186. void SetNetProcess()
  187. {
  188. // 清空已有连接数
  189. if (ListTool.HasElements(NetProcessInfoList))
  190. NetProcessInfoList.ForEach(x =>
  191. {
  192. x.ConnectCount = 0;
  193. });
  194. // 统计TCP连接数
  195. if (ListTool.HasElements(TcpConnection))
  196. {
  197. foreach (var t in TcpConnection)
  198. {
  199. SetNetProcessConnection(t.ProcessId);
  200. }
  201. }
  202. // 统计UDP连接数
  203. if (ListTool.HasElements(UdpConnection))
  204. {
  205. foreach (var u in UdpConnection)
  206. {
  207. SetNetProcessConnection(u.ProcessId);
  208. }
  209. }
  210. }
  211. void SetNetProcessConnection(int pid)
  212. {
  213. try
  214. {
  215. Process p = NowProcess.FirstOrDefault(x => x.Id == pid);
  216. if (p != null)
  217. {
  218. var ppl = NetProcessInfoList.FirstOrDefault(x => x.ProcessName == p.ProcessName);
  219. if (ppl == null)
  220. {
  221. NetProcessInfoList.Add(
  222. new NetProcessInfo()
  223. {
  224. ProcessIcon = ProcessInfoTool.GetIcon(p, false),
  225. ProcessName = p.ProcessName,
  226. ConnectCount = 1,
  227. LastUpdateTime = DateTime.Now,
  228. });
  229. }
  230. else
  231. {
  232. ppl.ConnectCount++;
  233. ppl.LastUpdateTime = DateTime.Now;
  234. }
  235. }
  236. }
  237. catch (Exception e)
  238. {
  239. R.Log.e("对程序列表和网络连接列表整理时发生错误");
  240. R.Log.e(e.Message);
  241. }
  242. }
  243. #endregion
  244. #region 整理程序流量汇总信息
  245. void CalcNetProcessInfo()
  246. {
  247. if (ListTool.HasElements(NetProcessInfoList))
  248. {
  249. NetProcessInfoList.ForEach(p =>
  250. {
  251. p.UploadDataCount += p.UploadData;
  252. p.DownloadDataCount += p.DownloadData;
  253. });
  254. int allupbag = NetProcessInfoList.Sum(x => x.UploadBag);
  255. int alldownbag = NetProcessInfoList.Sum(x => x.DownloadBag);
  256. NetProcessInfoList.ForEach(p =>
  257. {
  258. if (allupbag > 0 && NetFlow.UploadData > 0)
  259. {
  260. float uprate = (float)p.UploadBag / allupbag;
  261. p.UploadData = (int)(uprate * NetFlow.UploadData);
  262. }
  263. if (alldownbag > 0 && NetFlow.DownloadData > 0)
  264. {
  265. float downrate = (float)p.DownloadBag / alldownbag;
  266. p.DownloadData = (int)(downrate * NetFlow.DownloadData);
  267. }
  268. p.UploadBag = 0;
  269. p.DownloadBag = 0;
  270. p.LastUpdateTime = DateTime.Now;
  271. });
  272. }
  273. }
  274. #endregion
  275. }
  276. }