diff --git a/src/main/java/net/geedge/api/controller/APIController.java b/src/main/java/net/geedge/api/controller/APIController.java index efa0045..a5efda8 100644 --- a/src/main/java/net/geedge/api/controller/APIController.java +++ b/src/main/java/net/geedge/api/controller/APIController.java @@ -1,6 +1,7 @@ package net.geedge.api.controller; import cn.hutool.core.codec.Base32Codec; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.log.Log; import jakarta.servlet.http.HttpServletResponse; @@ -253,50 +254,44 @@ public class APIController { @PostMapping("/playbook") public R execPlaybook(@RequestParam("files") MultipartFile[] files, @RequestParam("packageName") String packageName) throws IOException { // save zip and apk - File appFile = null; - File playbookFile = null; - try { - for (MultipartFile file : files) { - if (T.FileUtil.extName(file.getOriginalFilename()).equals("zip")) { - playbookFile = T.FileUtil.file(Constant.TEMP_PATH, file.getOriginalFilename()); - file.transferTo(playbookFile); - T.ZipUtil.unzip(playbookFile, Constant.PLAYBOOK_AIR_PATH); - } else { - appFile = T.FileUtil.file(Constant.TEMP_PATH, file.getOriginalFilename()); - file.transferTo(appFile); - } - } - } finally { - T.FileUtil.del(playbookFile); - } String tid = T.StrUtil.uuid(); - PlaybookRunnable playbookRunnable = new PlaybookRunnable(adbUtil, appFile, Constant.PLAYBOOK_AIR_PATH, tid, packageName); + File appFile = null; + File playbookAirDir = null; + for (MultipartFile file : files) { + if (T.FileUtil.extName(file.getOriginalFilename()).equals("zip")) { + File playbookFile = T.FileUtil.file(Constant.TEMP_PATH, tid, file.getOriginalFilename()); + T.FileUtil.writeBytes(file.getInputStream().readAllBytes(), playbookFile); + playbookAirDir = T.FileUtil.file(Constant.TEMP_PATH, tid, "main.air"); + T.ZipUtil.unzip(playbookFile, playbookAirDir); + } else { + appFile = T.FileUtil.file(Constant.TEMP_PATH, tid, file.getOriginalFilename()); + T.FileUtil.writeBytes(file.getInputStream().readAllBytes(), appFile); + } + } + PlaybookRunnable playbookRunnable = new PlaybookRunnable(adbUtil, appFile, playbookAirDir, tid, packageName); ThreadUtil.execAsync(playbookRunnable); return R.ok().putData("tid", tid); } @GetMapping("/playbook/{id}") - public void getExecPlaybookResult( @PathVariable("id") String id, HttpServletResponse response) throws IOException { + public void getExecPlaybookResult(@PathVariable("id") String id, HttpServletResponse response) throws IOException { if (T.StrUtil.isEmpty(id)) { throw new APIException(RCode.BAD_REQUEST); } - File tempFile = null; - try { - Map result = Constant.PLAYBOOK_RUN_RESULT.get(id); - if (result != null) { - if (T.MapUtil.getStr(result, "status").equals("done")) { - String artifact = T.MapUtil.getStr(result, "artifact"); - tempFile = T.FileUtil.file(artifact); - T.ResponseUtil.downloadFile(response, tempFile.getName(), T.FileUtil.readBytes(tempFile)); - } else { - response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData(result))); - } + Map result = Constant.PLAYBOOK_RUN_RESULT.get(id); + if (result != null) { + String status = T.MapUtil.getStr(result, "status"); + if (T.MapUtil.getStr(result, "status").equals("done")) { + String artifact = T.MapUtil.getStr(result, "artifact"); + File pcapFile = T.FileUtil.file(artifact); Constant.PLAYBOOK_RUN_RESULT.remove(id); + T.ResponseUtil.downloadFile(response, pcapFile.getName(), T.FileUtil.readBytes(pcapFile)); + } else if (status.equals("error")) { + Constant.PLAYBOOK_RUN_RESULT.remove(id); + response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData(result))); } else { - throw new APIException(RCode.BAD_REQUEST); + response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData(result))); } - }finally { - T.FileUtil.del(tempFile); } } @@ -338,6 +333,7 @@ public class APIController { // 3. exec playbook AdbUtil.CommandResult execResult = adbUtil.execPlaybook(playbookDir.getPath()); + T.FileUtil.writeString(execResult.output(), FileUtil.file(Constant.TEMP_PATH, tid, "log", "log.txt"), "UTF-8"); if (0 != execResult.exitCode()) { // exec playbook error, stop tcpdump and delete pcap AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output()); @@ -353,7 +349,7 @@ public class APIController { // 5. pull pcap file String filePath = stopTcpdump.output(); - File localPcapFile = T.FileUtil.file(Constant.TEMP_PATH, startTcpdump.output() + ".pcap"); + File localPcapFile = T.FileUtil.file(Constant.TEMP_PATH, tid, startTcpdump.output() + ".pcap"); if (T.StrUtil.isEmpty(filePath)) { throw new APIException(RCode.NOT_EXISTS); } @@ -377,8 +373,9 @@ public class APIController { .build(); Constant.PLAYBOOK_RUN_RESULT.put(tid, resultMap); } finally { - T.FileUtil.del(apkFile); - T.FileUtil.clean(playbookDir); + adbUtil.stopApp(packageName); + //T.FileUtil.del(apkFile); + //T.FileUtil.clean(playbookDir); } } } diff --git a/src/main/java/net/geedge/api/util/AdbCommandBuilder.java b/src/main/java/net/geedge/api/util/AdbCommandBuilder.java index cd6490c..575a809 100644 --- a/src/main/java/net/geedge/api/util/AdbCommandBuilder.java +++ b/src/main/java/net/geedge/api/util/AdbCommandBuilder.java @@ -211,4 +211,11 @@ public class AdbCommandBuilder { return this.command; } + public AdbCommandBuilder buildStopAppCommand(String packageName) { + this.command.add("shell"); + this.command.add("am"); + this.command.add("force-stop"); + this.command.add(packageName); + return this; + } } \ No newline at end of file diff --git a/src/main/java/net/geedge/api/util/AdbUtil.java b/src/main/java/net/geedge/api/util/AdbUtil.java index 7863486..21b7ed5 100644 --- a/src/main/java/net/geedge/api/util/AdbUtil.java +++ b/src/main/java/net/geedge/api/util/AdbUtil.java @@ -511,6 +511,20 @@ public class AdbUtil { return new CommandResult(T.StrUtil.containsAny(result, "Success") ? 0 : 1, result); } + /** + * stop app + * adb shell am force-stop package_name + */ + public CommandResult stopApp(String packageName) { + String result = CommandExec.exec(AdbCommandBuilder.builder() + .serial(this.getSerial()) + .buildStopAppCommand(packageName) + .build() + ); + log.info("[stopApp] [packageName: {}] [result: {}]", packageName, result); + return new CommandResult(T.StrUtil.isEmpty(result) ? 0 : 1, result); + } + /** * iptables -F * iptables -X