fix: ASW-65 停止捕包时等待 tcpdump 资源释放,防止 pcap 不全

This commit is contained in:
shizhendong
2024-09-10 16:54:16 +08:00
parent 87e211987b
commit 7eb678813f
3 changed files with 18 additions and 1 deletions

View File

@@ -142,7 +142,7 @@ public class APIController {
}
@DeleteMapping("/pcap")
public void stopTcpdump(@RequestParam String id,
public synchronized void stopTcpdump(@RequestParam String id,
@RequestParam(required = false, defaultValue = "false") Boolean returnFile,
HttpServletResponse response) throws IOException {
AdbUtil.CommandResult result = adbUtil.stopTcpdump(id);

View File

@@ -641,6 +641,21 @@ public class AdbUtil {
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep %s | awk '{print $2}' | xargs kill -INT \"", id))
.build());
// 等待 tcpdump 资源释放避免出现错误The capture file appears to have been cut short in the middle of a packet.
for (int i = 0; i < 10; i++) {
T.ThreadUtil.sleep(500);
String str = CommandExec.exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep %s \"", id))
.build());
log.info("[stopTcpdump] [id: {}] [is running: {}]", id, str);
if (T.StrUtil.isEmpty(str)) {
break;
}
}
log.info("[stopTcpdump] [id: {}] [pcapFilePath: {}] [result: {}]", id, pcapFilePath, result);
if (T.StrUtil.isEmpty(result)) {
return new CommandResult(0, pcapFilePath);

View File

@@ -204,10 +204,12 @@ public class T {
}
public static class ResponseUtil {
static Log log = Log.get();
/**
* reponse 下载 byte数据
*/
public static void downloadFile(HttpServletResponse response, String filename, byte[] data) throws IORuntimeException, IOException {
log.info("[downloadFile] [fileName: {}] [size: {}]", filename, data.length);
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
String fileName = T.URLUtil.encode(filename, T.CharsetUtil.CHARSET_UTF_8);
cn.hutool.core.util.ReflectUtil.invoke(response, "addHeader", "Content-Disposition", "attachment; filename=" + fileName);