fix: 以 tid 为目录保留 playbook 文件,apk ,pcap 及playbook 运行日志

This commit is contained in:
zhangshuai
2024-10-18 17:17:53 +08:00
parent 05bacb1bf3
commit 562ac17fd4
3 changed files with 53 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
package net.geedge.api.controller; package net.geedge.api.controller;
import cn.hutool.core.codec.Base32Codec; import cn.hutool.core.codec.Base32Codec;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.log.Log; import cn.hutool.log.Log;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@@ -253,50 +254,44 @@ public class APIController {
@PostMapping("/playbook") @PostMapping("/playbook")
public R execPlaybook(@RequestParam("files") MultipartFile[] files, @RequestParam("packageName") String packageName) throws IOException { public R execPlaybook(@RequestParam("files") MultipartFile[] files, @RequestParam("packageName") String packageName) throws IOException {
// save zip and apk // 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(); 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); ThreadUtil.execAsync(playbookRunnable);
return R.ok().putData("tid", tid); return R.ok().putData("tid", tid);
} }
@GetMapping("/playbook/{id}") @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)) { if (T.StrUtil.isEmpty(id)) {
throw new APIException(RCode.BAD_REQUEST); throw new APIException(RCode.BAD_REQUEST);
} }
File tempFile = null; Map result = Constant.PLAYBOOK_RUN_RESULT.get(id);
try { if (result != null) {
Map result = Constant.PLAYBOOK_RUN_RESULT.get(id); String status = T.MapUtil.getStr(result, "status");
if (result != null) { if (T.MapUtil.getStr(result, "status").equals("done")) {
if (T.MapUtil.getStr(result, "status").equals("done")) { String artifact = T.MapUtil.getStr(result, "artifact");
String artifact = T.MapUtil.getStr(result, "artifact"); File pcapFile = T.FileUtil.file(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)));
}
Constant.PLAYBOOK_RUN_RESULT.remove(id); 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 { } 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 // 3. exec playbook
AdbUtil.CommandResult execResult = adbUtil.execPlaybook(playbookDir.getPath()); 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()) { if (0 != execResult.exitCode()) {
// exec playbook error, stop tcpdump and delete pcap // exec playbook error, stop tcpdump and delete pcap
AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output()); AdbUtil.CommandResult stopTcpdump = adbUtil.stopTcpdump(startTcpdump.output());
@@ -353,7 +349,7 @@ public class APIController {
// 5. pull pcap file // 5. pull pcap file
String filePath = stopTcpdump.output(); 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)) { if (T.StrUtil.isEmpty(filePath)) {
throw new APIException(RCode.NOT_EXISTS); throw new APIException(RCode.NOT_EXISTS);
} }
@@ -377,8 +373,9 @@ public class APIController {
.build(); .build();
Constant.PLAYBOOK_RUN_RESULT.put(tid, resultMap); Constant.PLAYBOOK_RUN_RESULT.put(tid, resultMap);
} finally { } finally {
T.FileUtil.del(apkFile); adbUtil.stopApp(packageName);
T.FileUtil.clean(playbookDir); //T.FileUtil.del(apkFile);
//T.FileUtil.clean(playbookDir);
} }
} }
} }

View File

@@ -211,4 +211,11 @@ public class AdbCommandBuilder {
return this.command; 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;
}
} }

View File

@@ -511,6 +511,20 @@ public class AdbUtil {
return new CommandResult(T.StrUtil.containsAny(result, "Success") ? 0 : 1, result); 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 -F
* iptables -X * iptables -X