Exemple #1
0
        //删除任务的实现方法
        public void taskDelete(Object id)
        {
            MySqlConnection conn   = null;
            string          taskId = (string)id;

            try
            {
                //获取本模拟器的编号并通过命令行删除
                conn = DatabaseOpt.getDBConnection();
                string emulatorId = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0];
                if (!"".Equals(emulatorId))
                {
                    //说明模拟器已创建,通过命令行删除
                    EmulatiorOpt.removeEmu(emulatorId);
                }

                //删除该条任务信息
                DatabaseOpt.deleteOne(conn, taskId);
            }
            catch
            {
                MessageBox.Show("删除任务出错");
            }
            finally
            {
                DatabaseOpt.close(conn);
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);
            }
        }
Exemple #2
0
        //激活任务
        private void taskActiveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MySqlConnection conn   = null;
            string          taskId = this.taskInfoDataGridView.CurrentRow.Cells["taskId"].Value.ToString();

            try
            {
                conn = DatabaseOpt.getDBConnection();
                List <string> taskIdList = DatabaseOpt.queryTaskTag(conn);
                if (taskIdList.Count > 0)
                {
                    //显示弹窗
                    ActiveConfirmForm activeConfirmForm = new ActiveConfirmForm(taskIdList);
                    activeConfirmForm.ShowDialog();
                }

                if (!taskIdList.Contains("cancel"))
                {
                    //激活任务代码
                    //通知web端本条任务激活
                    string phoneNumber = this.taskInfoDataGridView.CurrentRow.Cells["phoneNumber"].Value.ToString();
                    string IMSI        = this.taskInfoDataGridView.CurrentRow.Cells["IMSI"].Value.ToString();
                    string nationCode  = this.taskInfoDataGridView.CurrentRow.Cells["nationCode"].Value.ToString();

                    string url             = "http://192.168.17.232:8989/ghost/getVerificationCode?imsi=" + IMSI + "&phone=" + phoneNumber + "&phone_nation_code=" + nationCode;
                    string responseContent = WebServerCommunicate.httpGet(url);
                    if (!"ok".Equals(responseContent))
                    {
                        MessageBox.Show("激活失败");
                        return;
                    }

                    //修改任务标志
                    DatabaseOpt.updateOne(conn, taskId, "taskTag", "激活");
                }
            }catch (Exception)
            {
                MessageBox.Show("发生错误,激活失败");
            }
            finally
            {
                DatabaseOpt.close(conn);
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);
            }
        }
Exemple #3
0
        //确定按钮
        private void confirmBtn_Click(object sender, EventArgs e)
        {
            MySqlConnection conn   = null;
            string          taskId = taskDataGridView.CurrentRow.Cells["taskId"].Value.ToString();

            try
            {
                string newTaskName = this.taskNameModifyTextBox.Text;
                conn = DatabaseOpt.getDBConnection();
                DatabaseOpt.updateOne(conn, taskId, "taskName", newTaskName);
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskDataGridView);
            }
            finally
            {
                DatabaseOpt.close(conn);
                this.Dispose();
            }
        }
Exemple #4
0
        //停止任务的实现方法
        public void taskStop(Object id)
        {
            MySqlConnection conn   = null;
            string          taskId = (string)id;

            try
            {
                conn = DatabaseOpt.getDBConnection();

                //获取本模拟器的编号通过命令行停止,关闭appium和socket连接
                string emulatorId = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0];
                if (!"".Equals(emulatorId))
                {
                    //说明模拟器编号不为空,执行关闭模拟器操作
                    EmulatiorOpt.shutdownEmu(emulatorId);
                }
                string appiumPort = DatabaseOpt.queryOne(conn, taskId, "appiumPort")[0];
                if (!"".Equals(appiumPort))
                {
                    //说明appium端口不为空
                    AppiumOpt.endAppium(int.Parse(appiumPort));
                }
                string socketPort = DatabaseOpt.queryOne(conn, taskId, "socketPort")[0];
                if (!"".Equals(socketPort))
                {
                    //说明socket端口不为空
                    AppiumOpt.endAppium(int.Parse(socketPort));
                }

                //修改任务状态和任务标志
                DatabaseOpt.updateOne(conn, taskId, "taskStatus", "停止");
                DatabaseOpt.updateOne(conn, taskId, "taskTag", "普通");
            }
            catch (Exception)
            {
                MessageBox.Show("任务停止出错");
            }
            finally
            {
                DatabaseOpt.close(conn);
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);
            }
        }
Exemple #5
0
        //是按钮
        private void confirmBtn_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = null;

            try
            {
                conn = DatabaseOpt.getDBConnection();
                //将已激活任务置为普通
                for (int i = 0; i < taskIdList.Count; i++)
                {
                    string taskId = taskIdList[i];
                    //停止任务的代码
                    //查询并停止模拟器
                    string emualtorId = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0];
                    if (!"".Equals(emualtorId))
                    {
                        //说明模拟器已创建,通过命令行停止
                        EmulatiorOpt.shutdownEmu(emualtorId);
                    }
                    string appiumPort = DatabaseOpt.queryOne(conn, taskId, "appiumPort")[0];
                    if (!"".Equals(appiumPort))
                    {
                        //说明appium已经启动,通过命令行杀死
                        AppiumOpt.endAppium(int.Parse(appiumPort));
                    }
                    string socketPort = DatabaseOpt.queryOne(conn, taskId, "socketPort")[0];
                    if (!"".Equals(socketPort))
                    {
                        //说明socket已经开启,通过命令行杀死
                        AppiumOpt.endAppium(int.Parse(socketPort));
                    }

                    DatabaseOpt.updateOne(conn, taskId, "taskTag", "普通");
                    DatabaseOpt.updateOne(conn, taskId, "taskStatus", "停止");
                }
            }
            finally
            {
                DatabaseOpt.close(conn);
                this.Dispose();
            }
        }
Exemple #6
0
        public static void updateTaskInfoDataGridView(DataGridView taskInfoDataGridView)
        {
            MySqlConnection conn = null;

            try
            {
                DataTable dt = (DataTable)taskInfoDataGridView.DataSource;
                if (dt != null)
                {
                    dt.Rows.Clear();
                    taskInfoDataGridView.DataSource = dt;
                }
                conn = DatabaseOpt.getDBConnection();
                taskInfoDataGridView.DataSource = DatabaseOpt.queryAll(conn);
                taskInfoDataGridView.Refresh();
            }
            finally
            {
                DatabaseOpt.close(conn);
            }
        }
Exemple #7
0
        //创建任务按钮
        private void taskCreateBtn_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = null;

            try
            {
                if (appNameListView.SelectedItems.Count != 1)
                {
                    MessageBox.Show("请选择一个待执行的APP");
                    return;
                }

                if ("".Equals(apkPathTextBox.Text))
                {
                    MessageBox.Show("APK安装路径不能为空");
                    return;
                }

                if ("".Equals(taskNameText.Text))
                {
                    MessageBox.Show("任务名不能为空");
                    return;
                }
                if ("".Equals(IMSIText.Text))
                {
                    MessageBox.Show("IMSI不能为空");
                    return;
                }
                if ("".Equals(phoneNumberText.Text))
                {
                    MessageBox.Show("手机号不能为空");
                    return;
                }
                if ("".Equals(nationCodeTextBox.Text))
                {
                    MessageBox.Show("国家编码不能为空");
                    return;
                }

                string appName = appNameListView.SelectedItems[0].Text;
                string apkPath = apkPathTextBox.Text;
                apkPath = apkPath.Replace(@"\", @"\\");
                string taskName    = taskNameText.Text;
                string IMSI        = IMSIText.Text;
                string phoneNumber = phoneNumberText.Text;
                string nationCode  = nationCodeTextBox.Text;

                string taskCreateTime = DateTime.Now.ToString();
                string appPackageName = appNameListView.SelectedItems[0].Tag.ToString();

                TaskInfo taskInfo = new TaskInfo(taskName, phoneNumber, IMSI, nationCode, appName, taskCreateTime, "新建", "普通", apkPath, appPackageName, "无");

                conn = DatabaseOpt.getDBConnection();
                DatabaseOpt.insertOne(conn, taskInfo);

                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(taskInfoDataGridView);
            }
            finally
            {
                DatabaseOpt.close(conn);
                this.Dispose();
            }
        }
Exemple #8
0
        //重新吸附任务的实现方法
        public void reabsorption(Object id)
        {
            MySqlConnection conn   = null;
            string          taskId = (string)id;

            try
            {
                conn = DatabaseOpt.getDBConnection();

                //查询已激活的任务
                string activeTaskId = DatabaseOpt.queryTaskTag(conn)[0];
                if (!"".Equals(activeTaskId))
                {
                    //已有任务被激活
                    if (!taskId.Equals(activeTaskId))
                    {
                        //说明当前激活的任务与重新吸附的任务不同,需要停止其模拟器
                        string activeTaskEmulatorId = DatabaseOpt.queryOne(conn, activeTaskId, "emulatorId")[0];
                        //停止模拟器
                        EmulatiorOpt.shutdownEmu(activeTaskId);
                    }
                    //关闭当前激活任务的appium
                    string activeTaskAppiumPort = DatabaseOpt.queryOne(conn, activeTaskId, "appiumPort")[0];
                    if (!"".Equals(activeTaskAppiumPort))
                    {
                        //关闭appium
                        AppiumOpt.endAppium(int.Parse(activeTaskAppiumPort));
                        //将appium端口置空
                        DatabaseOpt.updateOne(conn, activeTaskId, "appiumPort", "");
                    }
                    //关闭socket
                    string activeTaskSocketPort = DatabaseOpt.queryOne(conn, activeTaskId, "socketPort")[0];
                    if (!"".Equals(activeTaskSocketPort))
                    {
                        //关闭socket
                        AppiumOpt.endAppium(int.Parse(activeTaskSocketPort));
                        //将socket端口置空
                        DatabaseOpt.updateOne(conn, activeTaskId, "socketPort", "");
                    }

                    //置之前激活的任务标志为普通
                    DatabaseOpt.updateOne(conn, activeTaskId, "taskTag", "普通");
                    TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);
                }

                //获取重新吸附任务的appium端口
                string appiumPort = DatabaseOpt.queryOne(conn, taskId, "appiumPort")[0];
                if (!"".Equals(appiumPort))
                {
                    //关闭之前的appium
                    AppiumOpt.endAppium(int.Parse(appiumPort));
                    //将appium端口置空
                    DatabaseOpt.updateOne(conn, taskId, "appiumPort", "");
                }
                string socketPort = DatabaseOpt.queryOne(conn, taskId, "socketPort")[0];
                if (!"".Equals(socketPort))
                {
                    //关闭之前的socket
                    AppiumOpt.endAppium(int.Parse(socketPort));
                    //将socket端口置空
                    DatabaseOpt.updateOne(conn, taskId, "socketPort", "");
                }

                //激活任务代码
                //通知web端本条任务激活
                string phoneNumber = DatabaseOpt.queryOne(conn, taskId, "phoneNumber")[0];
                string IMSI        = DatabaseOpt.queryOne(conn, taskId, "IMSI")[0];
                string nationCode  = DatabaseOpt.queryOne(conn, taskId, "nationCode")[0];

                string url             = "http://192.168.17.232:8989/ghost/getVerificationCode?imsi=" + IMSI + "&phone=" + phoneNumber + "&phone_nation_code=" + nationCode;
                string responseContent = WebServerCommunicate.httpGet(url);
                if (!"ok".Equals(responseContent))
                {
                    MessageBox.Show("激活失败");
                    return;
                }
                //修改任务标记为激活
                DatabaseOpt.updateOne(conn, taskId, "taskTag", "激活");
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);

                //启动任务函数
                taskStart(taskId);
            }
            finally
            {
                DatabaseOpt.close(conn);
            }
        }
Exemple #9
0
        //启动任务的实现方法
        public void taskStart(Object id)
        {
            MySqlConnection conn       = null;
            int             appiumPort = 0;

            //选中任务的id
            string taskId = (string)id;

            try
            {
                conn = DatabaseOpt.getDBConnection();

                //汇报任务进度
                DatabaseOpt.updateOne(conn, taskId, "taskProgress", "正在准备启动任务");
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);

                List <string> emulatorIdList = DatabaseOpt.queryOne(conn, taskId, "emulatorId");
                //说明该任务还没有创建模拟器
                if ("".Equals(emulatorIdList[0]))
                {
                    //创建模拟器
                    int emulatorId = EmulatiorOpt.createEmu();
                    //计算端口号
                    int emulatorPort = 21503 + emulatorId * 10;

                    //将新建信息插入数据库
                    DatabaseOpt.updateOne(conn, taskId, "emulatorId", emulatorId.ToString());
                    DatabaseOpt.updateOne(conn, taskId, "emulatorPort", emulatorPort.ToString());
                }

                //获取当前任务的模拟器号和端口
                string emulatorIdStr   = DatabaseOpt.queryOne(conn, taskId, "emulatorId")[0];
                string emulatorPortStr = DatabaseOpt.queryOne(conn, taskId, "emulatorPort")[0];

                //启动模拟器
                EmulatiorOpt.startEmu(emulatorIdStr);

                //设置任务状态为已启动未控
                DatabaseOpt.updateOne(conn, taskId, "taskStatus", "已启动未控");
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);

                //睡眠10秒等待模拟器启动完成
                Thread.Sleep(20000);

                //执行安装APP

                //汇报任务进度
                DatabaseOpt.updateOne(conn, taskId, "taskProgress", "正在安装APP");
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);

                string apkPath = DatabaseOpt.queryOne(conn, taskId, "apkPath")[0];

                //APP安装函数
                EmulatiorOpt.installApp(int.Parse(emulatorIdStr), apkPath);

                //执行启动appium,汇报任务进度
                DatabaseOpt.updateOne(conn, taskId, "taskProgress", "正在初始化appium");
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);

                appiumPort = AppiumOpt.getAvailablePort(4723, 65534);
                //说明appium无可用端口
                if (appiumPort == 0)
                {
                    //将该条任务在数据库中的appiumPort值置为""
                    DatabaseOpt.updateOne(conn, taskId, "appiumPort", "");
                    DatabaseOpt.updateOne(conn, taskId, "taskProgress", "appium无可用端口");
                    TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);
                    return;
                }
                else
                {
                    //开启appium服务
                    AppiumOpt.callCmd("appium -a 127.0.0.1 -p " + appiumPort);

                    //睡眠5秒等待appium开启
                    Thread.Sleep(5000);

                    //记录本次启动任务使用的appium端口号
                    DatabaseOpt.updateOne(conn, taskId, "appiumPort", appiumPort.ToString());
                }

                int socketPort = AppiumOpt.getAvailablePort(50000, 65534);
                //说明socket无可用端口
                if (socketPort == 0)
                {
                    //将该条任务在数据库中的socketPort值置为""
                    DatabaseOpt.updateOne(conn, taskId, "socketPort", "");
                    DatabaseOpt.updateOne(conn, taskId, "taskProgress", "socket通信无可用端口");
                    TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);
                    return;
                }

                //防止没有连上模拟器
                AppiumOpt.callCmd("adb connect 127.0.0.1:" + emulatorPortStr);
                Thread.Sleep(3000);

                //准备调用jar包所需要传入的参数
                string appName    = DatabaseOpt.queryOne(conn, taskId, "appName")[0];
                string methodName = MethodMapping(appName);
                string deviceName = "127.0.0.1:" + emulatorPortStr;

                string IMSI        = DatabaseOpt.queryOne(conn, taskId, "IMSI")[0];
                string phoneNumber = DatabaseOpt.queryOne(conn, taskId, "phoneNumber")[0];
                string nationCode  = DatabaseOpt.queryOne(conn, taskId, "nationCode")[0];

                //开启线程运行jar包
                Thread extrationThread = new Thread(new ParameterizedThreadStart(AppiumOpt.callCmd));
                extrationThread.Start("java -cp" + " " + @appiumProject_jarPath + " " + methodName + " " + "127.0.0.1 " + socketPort + " " + deviceName + " " + appiumPort + " " + IMSI + " " + phoneNumber + " " + nationCode);

                //与客户端通信
                AppiumOpt.communicate(socketPort, conn, taskId, this.taskInfoDataGridView);

                //查询任务执行到最后时的任务进度
                List <string> taskEndProgress = DatabaseOpt.queryOne(conn, taskId, "taskProgress");
                if (taskEndProgress[0].Contains("登录完成"))
                {
                    //说明正常登录,将任务状态置为已启动已控
                    DatabaseOpt.updateOne(conn, taskId, "taskStatus", "已启动已控");
                    TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(this.taskInfoDataGridView);
                }
            }
            finally
            {
                //关闭appium端口,清除该任务在数据库中存放的端口信息
                if (appiumPort != 0)
                {
                    AppiumOpt.endAppium(appiumPort);
                }
                if (conn != null)
                {
                    DatabaseOpt.updateOne(conn, taskId, "appiumPort", "");
                    DatabaseOpt.updateOne(conn, taskId, "socketPort", "");
                }

                DatabaseOpt.close(conn);
            }
        }
Exemple #10
0
        public static void communicate(int socketPort, MySqlConnection conn, string taskId, DataGridView taskInfoDataGridView)
        {
            Socket serverSocket = null;
            Socket socket       = null;

            try
            {
                //初始化socket
                serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress  ip    = IPAddress.Parse("127.0.0.1");   //本机IP
                IPEndPoint ipend = new IPEndPoint(ip, socketPort); //网络终结点表示为IP地址和端口号
                serverSocket.Bind(ipend);
                serverSocket.Listen(10);

                //等待用户连接
                socket = serverSocket.Accept();

                //记录本次启动任务使用的socket端口号
                DatabaseOpt.updateOne(conn, taskId, "socketPort", socketPort.ToString());

                //附件名列表(未来将取证部分引入时会用到)
                List <String> attachmentsList = new List <string>();

                //通知客户端服务端已准备好
                byte[] frameHeader = new byte[] { (byte)0x00 };
                byte[] frameLength = intToBytes(4);
                byte[] frameCmd    = new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
                byte[] frameEnd    = new byte[] { (byte)0xff };

                byte[] frame1 = arrayJoin(frameHeader, frameLength);
                byte[] frame2 = arrayJoin(frame1, frameCmd);
                byte[] frame  = arrayJoin(frame2, frameEnd);
                socket.Send(frame);

                //接收并解析客户端回传的进度信息
                while (true)
                {
                    byte[] receiveFrameHeader = new byte[1];
                    socket.Receive(receiveFrameHeader);
                    byte[] receiveFrameLength = new byte[4];
                    socket.Receive(receiveFrameLength);
                    int length = byteToInt(receiveFrameLength); //命令+内容长度
                    length -= 4;                                //内容长度
                    byte[] receiveCommand = new byte[4];
                    socket.Receive(receiveCommand);
                    byte[] attachmentCommand    = new byte[] { 0x00, 0x00, 0x00, 0x02 }; //附件名报文命令
                    byte[] progressCommand      = new byte[] { 0x00, 0x00, 0x00, 0x01 }; //进度报文命令
                    byte[] attachmentEndCommand = new byte[] { 0x00, 0x00, 0x00, 0x03 }; //附件名传送结束命令
                    if (byteArrayEquals(receiveCommand, attachmentCommand))
                    {
                        if (length > 0)//有附件内容
                        {
                            byte[] attachmentContent = new byte[length];
                            socket.Receive(attachmentContent);
                            string content = Encoding.UTF8.GetString(attachmentContent, 0, length);
                            attachmentsList.Add(content);
                        }
                    }
                    if (byteArrayEquals(receiveCommand, progressCommand))
                    {
                        if (length > 0)//有内容
                        {
                            byte[] receiveContent = new byte[length];
                            socket.Receive(receiveContent);
                            //string content = receiveContent.ToString();
                            string       content        = Encoding.UTF8.GetString(receiveContent, 0, length);
                            TaskProgress taskProgress   = new TaskProgress();
                            JObject      contentJObject = JObject.Parse(content);
                            taskProgress.TASK_ID        = (string)contentJObject["TASK_ID"];
                            taskProgress.SUB_TASK_ID    = (string)contentJObject["SUB_TASK_ID"];
                            taskProgress.PROGRESS_CUR   = (int)contentJObject["PROGRESS_CUR"];
                            taskProgress.PROGRESS_TOTAL = (int)contentJObject["PROGRESS_TOTAL"];
                            taskProgress.STATUS_CODE    = (string)contentJObject["STATUS_CODE"];
                            taskProgress.STATUS_TEXT    = (string)contentJObject["STATUS_TEXT"];

                            //更新任务进度
                            DatabaseOpt.updateOne(conn, taskId, "taskProgress", taskProgress.STATUS_TEXT);
                            TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(taskInfoDataGridView);
                        }
                    }
                    byte[] receiveFrameEnd = new byte[1];
                    socket.Receive(receiveFrameEnd);
                    byte[] endCommand = new byte[] { 0x00, 0x00, 0xff, 0xff };
                    if (byteArrayEquals(endCommand, receiveCommand))
                    {
                        break;
                    }
                    if (byteArrayEquals(attachmentEndCommand, receiveCommand))
                    {
                        break;
                    }
                }
            }
            catch (Exception)
            {
                //更新出错时的任务进度
                DatabaseOpt.updateOne(conn, taskId, "taskStatus", "登录任务执行出错");
                TaskInfoDataGridViewOpt.updateTaskInfoDataGridView(taskInfoDataGridView);
            }
            finally
            {
                closeSocket(serverSocket, socket);
            }
        }