Commit 733f531c authored by liuyang's avatar liuyang

APK抓包>从频道1开始依次抓取, 修复抓包等待时间停止任务,重新开始后之前的任务仍在执行问题

#BYLSERVER-1693
parent 8caefa37
...@@ -21,19 +21,13 @@ import java.util.* ...@@ -21,19 +21,13 @@ import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class Task : IAppBaseCallback { class Task : Thread, IAppBaseCallback {
private val TAG = "RobotTask" enum class State {
IDLE, RUNNING, INTERRUPT, SCREEN_SHOT_FAILED, UPLOADING, REPORT_FAILED, COMPLETE, MANUAL_STOP
}
val STATUS_MAP = mapOf( private val TAG = "RobotTask"
"0" to "未处理",
"1" to "进行中",
"2" to "中断",
"3" to "截图失败",
"4" to "图片上传中",
"5" to "上报失败",
"6" to "处理完成",
"7" to "手动停止")
lateinit var apkInfo: ApkInfo lateinit var apkInfo: ApkInfo
// 截图存放路径 // 截图存放路径
...@@ -45,11 +39,10 @@ class Task : IAppBaseCallback { ...@@ -45,11 +39,10 @@ class Task : IAppBaseCallback {
private var uploadId = "" private var uploadId = ""
private var channelIndex = 0 private var channelIndex = 0
private var status = "0" var status: State = State.IDLE
private var reportProtocol: ApkReportProtocol private var reportProtocol: ApkReportProtocol
private var dataHandler:AppBaseHandler private var dataHandler: AppBaseHandler
private lateinit var reportJson: JSONObject private lateinit var reportJson: JSONObject
private var context: Context private var context: Context
...@@ -62,6 +55,12 @@ class Task : IAppBaseCallback { ...@@ -62,6 +55,12 @@ class Task : IAppBaseCallback {
this.dataHandler = AppBaseHandler(this) this.dataHandler = AppBaseHandler(this)
} }
fun isRunning(): Boolean {
if (status == State.COMPLETE)
return false
return true
}
fun from(apkInfo: ApkInfo) { fun from(apkInfo: ApkInfo) {
this.apkInfo = apkInfo this.apkInfo = apkInfo
// this.imagePath = this.context.cacheDir.absolutePath + "/" + apkInfo.packageName // this.imagePath = this.context.cacheDir.absolutePath + "/" + apkInfo.packageName
...@@ -81,14 +80,18 @@ class Task : IAppBaseCallback { ...@@ -81,14 +80,18 @@ class Task : IAppBaseCallback {
reportJson.putOpt("channels", JSONArray()) reportJson.putOpt("channels", JSONArray())
} }
fun start() { override fun run() {
super.run()
execute()
}
private fun execute() {
//如果之前的上传任务还在执行,进行取消 //如果之前的上传任务还在执行,进行取消
if (!uploadId.isNullOrEmpty()) { if (!uploadId.isNullOrEmpty()) {
UploadService.stopUpload(uploadId) UploadService.stopUpload(uploadId)
} }
this.status = "1" this.status = State.RUNNING
this.prepareReport() this.prepareReport()
// 启动应用 // 启动应用
this.launchApp(true) this.launchApp(true)
...@@ -97,8 +100,10 @@ class Task : IAppBaseCallback { ...@@ -97,8 +100,10 @@ class Task : IAppBaseCallback {
var success = this.processChannels(0) var success = this.processChannels(0)
// 再次尝试 // 再次尝试
if (!success) { if (!success) {
if (this.status == "7") if (this.status == State.MANUAL_STOP) {
Log.i(TAG, "manual exit")
return return
}
this.launchApp(true) this.launchApp(true)
// 重新切换到上次崩溃的频道,接着抓取 // 重新切换到上次崩溃的频道,接着抓取
...@@ -107,27 +112,26 @@ class Task : IAppBaseCallback { ...@@ -107,27 +112,26 @@ class Task : IAppBaseCallback {
} }
success = this.processChannels(this.channelIndex) success = this.processChannels(this.channelIndex)
if (!success && this.status == "7") if (!success && this.status == State.MANUAL_STOP)
return return
} }
// 上传图片 // 上传图片
this.uploadImage() this.uploadImage()
this.status = "4" this.status = State.UPLOADING
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
this.status = "2" this.status = State.INTERRUPT
e.printStackTrace() e.printStackTrace()
} }
// 退出应用
AdbUtil.stopApp(this.apkInfo.packageName)
// 上报数据,上传图片在后台进行 // 上报数据,上传图片在后台进行
report() report()
// 退出应用
AdbUtil.stopApp(this.apkInfo.packageName)
} }
fun reset() { fun reset() {
this.status = "0" this.status = State.IDLE
this.channelIndex = 0 this.channelIndex = 0
this.uploadImages.clear() this.uploadImages.clear()
...@@ -138,9 +142,8 @@ class Task : IAppBaseCallback { ...@@ -138,9 +142,8 @@ class Task : IAppBaseCallback {
} }
} }
fun destroy() { fun release() {
// 杀掉tcpdump进程 // 杀掉tcpdump进程
this.status = "7"
AdbUtil.killTcpdump() AdbUtil.killTcpdump()
// 退出应用 // 退出应用
...@@ -151,6 +154,11 @@ class Task : IAppBaseCallback { ...@@ -151,6 +154,11 @@ class Task : IAppBaseCallback {
// UploadService.stopUpload(uploadId) // UploadService.stopUpload(uploadId)
} }
fun exit() {
this.status = State.MANUAL_STOP
release()
}
private fun finish(result: Boolean) { private fun finish(result: Boolean) {
this.taskListener?.run { this.taskListener?.run {
onComplete(result) onComplete(result)
...@@ -178,13 +186,13 @@ class Task : IAppBaseCallback { ...@@ -178,13 +186,13 @@ class Task : IAppBaseCallback {
} }
} }
private fun processChannels(index:Int): Boolean { private fun processChannels(index: Int): Boolean {
Log.i(TAG, "processChannels") Log.i(TAG, "processChannels")
step() step()
quitCapture() quitCapture()
for (i in index + 1 until this.apkInfo.channelCount) { for (i in index + 1 until this.apkInfo.channelCount) {
if (this.status == "7") if (this.status == State.MANUAL_STOP)
return false return false
// 模拟按键事件. 切换频道进行抓取 // 模拟按键事件. 切换频道进行抓取
this.channelIndex = i this.channelIndex = i
...@@ -340,7 +348,7 @@ class Task : IAppBaseCallback { ...@@ -340,7 +348,7 @@ class Task : IAppBaseCallback {
} }
// 截图处理,转成jpg // 截图处理,转成jpg
private fun screenShot(absName : String) { private fun screenShot(absName: String) {
val pngPath = "$absName.png" val pngPath = "$absName.png"
val jpgPath = pngPath.replace(".png", ".jpg") val jpgPath = pngPath.replace(".png", ".jpg")
AdbUtil.screenShot(pngPath) AdbUtil.screenShot(pngPath)
...@@ -361,7 +369,7 @@ class Task : IAppBaseCallback { ...@@ -361,7 +369,7 @@ class Task : IAppBaseCallback {
val totalUpload = this.uploadImages.size val totalUpload = this.uploadImages.size
Log.i(TAG, "total upload count : $totalUpload") Log.i(TAG, "total upload count : $totalUpload")
return object: UploadStatusDelegate { return object : UploadStatusDelegate {
override fun onCancelled(context: Context?, uploadInfo: UploadInfo?) { override fun onCancelled(context: Context?, uploadInfo: UploadInfo?) {
} }
...@@ -416,17 +424,17 @@ class Task : IAppBaseCallback { ...@@ -416,17 +424,17 @@ class Task : IAppBaseCallback {
} }
override fun onProtocolFailed(p0: IProtocol?) { override fun onProtocolFailed(p0: IProtocol?) {
this.status = "5" this.status = State.REPORT_FAILED
finish(false) finish(false)
} }
override fun onHttpFailed(p0: IProtocol?) { override fun onHttpFailed(p0: IProtocol?) {
this.status = "5" this.status = State.REPORT_FAILED
finish(false) finish(false)
} }
override fun onProtocolSucceed(p0: IProtocol?) { override fun onProtocolSucceed(p0: IProtocol?) {
this.status = "6" this.status = State.COMPLETE
finish(true) finish(true)
} }
......
...@@ -2,52 +2,36 @@ package com.duolebo.blyrobot.tools ...@@ -2,52 +2,36 @@ package com.duolebo.blyrobot.tools
import android.util.Log import android.util.Log
import com.duolebo.blyrobot.data.Task import com.duolebo.blyrobot.data.Task
import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class TaskManager { class TaskManager {
private val TAG = "TaskManager"
private val RUN_TASK_PERIOD = 6*60*60*1000
val tasks = ArrayList<Task>()
private val overTasks = ArrayList<Task>() companion object {
val instance = TaskManager()
val TAG = "RobotTaskManager"
val RUN_TASK_PERIOD: Long = 6 * 60 * 60 * 1000
}
val tasks = ArrayList<Task>()
var isRunning = false var isRunning = false
private var status = IDLE private val overTasks = ArrayList<Task>()
private var currentTask: Task ?= null private var currentTask: Task? = null
// 处理时间 private var scheduleTime: Timer? = null
private var updateTime = 0L
private val runnable = Runnable {
while (isRunning) {
val interval = System.currentTimeMillis() - this.updateTime
if (this.status == IDLE && interval > RUN_TASK_PERIOD) {
this.updateTime = System.currentTimeMillis()
this.status = RUNNING
for (task in tasks) {
task.reset()
currentTask = task
currentTask!!.start()
}
}
else {
Log.i(TAG, "no task running")
}
Thread.sleep(3 * 1000)
}
}
fun add(task: Task) { fun add(task: Task) {
for (existTask in tasks) {
Log.i(TAG, "exist task app: ${existTask.apkInfo.packageName}")
}
task.taskListener = object : Task.OnTaskListener { task.taskListener = object : Task.OnTaskListener {
override fun onComplete(result: Boolean) { override fun onComplete(result: Boolean) {
Log.i(TAG, "task ${task.apkInfo.name} complete with result: $result") Log.i(TAG, "task ${task.apkInfo.name} complete with result: $result")
tasks.remove(task)
overTasks.add(task) overTasks.add(task)
if (overTasks.size == tasks.size) { if (tasks.size == 0) {
status = IDLE isRunning = false
} }
} }
} }
...@@ -62,8 +46,9 @@ class TaskManager { ...@@ -62,8 +46,9 @@ class TaskManager {
} }
item?.run { item?.run {
Log.i(TAG, "remove task ${this.apkInfo.packageName}")
tasks.remove(this) tasks.remove(this)
destroy() this.release()
} }
} }
...@@ -76,35 +61,60 @@ class TaskManager { ...@@ -76,35 +61,60 @@ class TaskManager {
} }
fun start() { fun start() {
Log.i(TAG, "start...")
if (this.isRunning) { if (this.isRunning) {
Log.i(TAG, "is running") Log.i(TAG, "is running")
return return
} }
if (this.tasks.size <= 0) { if (this.tasks.size == 0) {
Log.i(TAG, "no tasks") Log.i(TAG, "no tasks")
return return
} }
this.isRunning = true this.isRunning = true
this.status = IDLE scheduleTime?.run {
val t = Thread(runnable) cancel()
t.start()
} }
scheduleTime = Timer()
scheduleTime!!.schedule(object : TimerTask() {
override fun run() {
while (isRunning) {
fun stop() { if (currentTask != null && currentTask!!.isRunning()) {
this.tasks.forEach { Log.i(TAG, "task ${currentTask!!.apkInfo.packageName} is running")
it.destroy() }
else {
if (tasks.size == 0) {
for (task in overTasks) {
task.reset()
tasks.add(task)
}
} }
this.overTasks.clear() if (tasks.size > 0) {
currentTask = tasks[0]
currentTask!!.start()
Log.i(TAG, "task ${currentTask!!.apkInfo.packageName} start")
}
}
Thread.sleep(3 * 1000)
}
}
}, 0, RUN_TASK_PERIOD)
}
fun stop() {
this.isRunning = false this.isRunning = false
this.updateTime = 0L scheduleTime?.run {
cancel()
}
this.tasks.forEach {
it.exit()
} }
companion object { this.tasks.clear()
val instance = TaskManager() this.overTasks.clear()
const val IDLE = 0
const val RUNNING = 1
} }
} }
\ No newline at end of file
...@@ -369,7 +369,11 @@ object AdbUtil { ...@@ -369,7 +369,11 @@ object AdbUtil {
} }
fun stopApp(packageName: String) { fun stopApp(packageName: String) {
try {
exeCmdEcho("am force-stop $packageName", true) exeCmdEcho("am force-stop $packageName", true)
} catch (e:Exception) {
e.printStackTrace()
}
} }
class CmdResult { class CmdResult {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment