Пример #1
0
        public static List <NetStat> GetSendIpInfo()
        {
            Process p = new Process();

            p.StartInfo.FileName               = "cmd.exe"; //设置启动的应用程序
            p.StartInfo.UseShellExecute        = false;     //禁止使用操作系统外壳程序启动进程
            p.StartInfo.RedirectStandardInput  = true;      //应用程序的输入从流中读取
            p.StartInfo.RedirectStandardOutput = true;      //应用程序的输出写入流中
            p.StartInfo.RedirectStandardError  = true;      //将错误信息写入流
            p.StartInfo.CreateNoWindow         = true;      //是否在新窗口中启动进程
            p.Start();
            p.StandardInput.WriteLine(@"netstat -aon|findstr ""ESTABLISHED""|findstr " + ServerIp);
            p.StandardInput.WriteLine("exit");
            Regex          reg  = new Regex("\\s+", RegexOptions.Compiled);
            string         line = "";
            List <NetStat> list = new List <NetStat>();

            while ((line = p.StandardOutput.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase))
                {
                    line = reg.Replace(line, ",");
                    string[] arr = line.Split(',');
                    if (arr.Length == 5)
                    {
                        if (arr[2].StartsWith(ServerIp))
                        {
                            string[] ss      = arr[1].Split(':');
                            string[] dd      = arr[2].Split(':');
                            NetStat  netstat = new NetStat()
                            {
                                LocalAddress   = ss[0],
                                SrcPort        = ss[1],
                                ForeignAddress = dd[0],
                                DstPort        = dd[1],
                                Pid            = arr[4]
                            };
                            list.Add(netstat);
                        }
                    }
                }
            }
            return(list);
        }
Пример #2
0
        public static void UpdateSimulator()
        {
            if (DmSystem == null)
            {
                return;;
            }
            DmPlugin dm = DmSystem.Dm;

            string hwnds = dm.EnumWindow(0, "QWidgetClassWindow", "Qt5QWindowIcon", 3);

            if (hwnds == "")
            {
                Debug.WriteLine("获取句柄失败!");
                return;
            }
            else
            {
                Debug.WriteLine(hwnds);
            }

            Process p = new Process();

            p.StartInfo.FileName               = "cmd.exe"; //设置启动的应用程序
            p.StartInfo.UseShellExecute        = false;     //禁止使用操作系统外壳程序启动进程
            p.StartInfo.RedirectStandardInput  = true;      //应用程序的输入从流中读取
            p.StartInfo.RedirectStandardOutput = true;      //应用程序的输出写入流中
            p.StartInfo.RedirectStandardError  = true;      //将错误信息写入流
            p.StartInfo.CreateNoWindow         = true;      //是否在新窗口中启动进程
            p.Start();
            p.StandardInput.WriteLine(@"netstat -aon|findstr ""ESTABLISHED""");
            p.StandardInput.WriteLine("exit");
            Regex          reg  = new Regex("\\s+", RegexOptions.Compiled);
            string         line = "";
            List <NetStat> list = new List <NetStat>();

            while ((line = p.StandardOutput.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.StartsWith("TCP", StringComparison.OrdinalIgnoreCase))
                {
                    line = reg.Replace(line, ",");
                    string[] arr = line.Split(',');
                    if (arr.Length == 5)
                    {
                        if (arr[1].StartsWith("127.0.0.1") && arr[2].StartsWith("127.0.0.1") || arr[2].StartsWith(ServerIp))
                        {
                            NetStat netstat = new NetStat()
                            {
                                Proto          = arr[0],
                                LocalAddress   = arr[1],
                                ForeignAddress = arr[2],
                                State          = arr[3],
                                Pid            = arr[4]
                            };
                            list.Add(netstat);
                        }
                    }
                }
            }
            //移除所有不存在的窗口
            YeShenSimulatorList.RemoveAll(x => !dm.GetWindowState(x.NoxHwnd, 0));
            foreach (var hwnd in hwnds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToList())
            {
                YeShenSimulator yss = new YeShenSimulator();
                var             s   = YeShenSimulatorList.FirstOrDefault(x => x.NoxHwnd == hwnd);
                if (s != null)
                {
                    if (s.NoxVMHandlePid == 0)
                    {
                        yss.NoxHwnd = s.NoxHwnd;
                        yss.NoxPid  = s.NoxPid;
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    yss.NoxHwnd = hwnd;
                    yss.NoxPid  = dm.GetWindowProcessId(hwnd);
                }
                try
                {
                    //找到模拟器Nox所对应连接NoxVMHandle,可能有多个连接
                    List <NetStat> ln1 = list.FindAll(a => a.Pid == yss.NoxPid.ToString());
                    //本地远程相反的连接就是NoxVMHandle,获得NoxVMHandle进程Id,n2为NoxVMHandle进程与Nox一个链接
                    NetStat n2 = list.First(b => b.ForeignAddress == ln1.FirstOrDefault().LocalAddress&& b.LocalAddress == ln1.FirstOrDefault().ForeignAddress);
                    //找到该进程id的另一个连接就是adb连接
                    NetStat n3 = list.FirstOrDefault(c => c.Pid == n2.Pid && (ln1.FirstOrDefault(x => x.ForeignAddress == c.LocalAddress)) == null);
                    if (n3 != null)
                    {
                        yss.NoxVMHandlePid = int.Parse(n3.Pid);
                        yss.AdbDevicesId   = n3.LocalAddress; //对应的本地地址就是adb设备地址
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }

                YeShenSimulatorList.Add(yss);
            }
            p.Close();
            Debug.WriteLine("当前打开的模拟器:" + YeShenSimulatorList.Count);
        }