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;
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
String tid = T.StrUtil.uuid();
File appFile = null;
File playbookFile = null;
try {
File playbookAirDir = null;
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);
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, file.getOriginalFilename());
file.transferTo(appFile);
appFile = T.FileUtil.file(Constant.TEMP_PATH, tid, file.getOriginalFilename());
T.FileUtil.writeBytes(file.getInputStream().readAllBytes(), appFile);
}
}
} finally {
T.FileUtil.del(playbookFile);
}
String tid = T.StrUtil.uuid();
PlaybookRunnable playbookRunnable = new PlaybookRunnable(adbUtil, appFile, Constant.PLAYBOOK_AIR_PATH, tid, packageName);
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) {
String status = T.MapUtil.getStr(result, "status");
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));
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 {
response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData(result)));
}
Constant.PLAYBOOK_RUN_RESULT.remove(id);
} else {
throw new APIException(RCode.BAD_REQUEST);
}
}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);
}
}
}

View File

@@ -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;
}
}

View File

@@ -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