feat: 请求 API 添加 token 认证;调整 tcpdump 执行逻辑;新增 execShellCmd 接口

This commit is contained in:
shizhendong
2024-08-27 15:46:51 +08:00
parent 6f6bb2ad90
commit 9814018149
8 changed files with 202 additions and 10 deletions

View File

@@ -127,6 +127,11 @@ public class APIController {
return R.ok();
}
@GetMapping("/pcap")
public R listTcpdump() {
return R.ok().putData("records", adbUtil.listTcpdump());
}
@PostMapping("/pcap")
public R startTcpdump(@RequestParam(required = false, defaultValue = "") String packageName) {
AdbUtil.CommandResult result = adbUtil.startTcpdump(packageName);
@@ -149,7 +154,10 @@ public class APIController {
// response pcap file
File tempFile = T.FileUtil.file(Constant.TEMP_PATH, id + ".pcap");
try {
String filePath = "/data/local/tmp/" + id + ".pcap";
String filePath = result.output();
if (T.StrUtil.isEmpty(filePath)) {
throw new APIException(RCode.NOT_EXISTS);
}
AdbUtil.CommandResult pulled = adbUtil.pull(filePath, tempFile.getAbsolutePath());
if (0 != pulled.exitCode()) {
throw new APIException(pulled.output());
@@ -163,4 +171,15 @@ public class APIController {
response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData("id", id)));
}
}
@PostMapping("/shell")
public R execShellCmd(@RequestBody Map<String, Object> requestBody) {
String cmd = T.MapUtil.getStr(requestBody, "cmd", "");
if (T.StrUtil.isEmpty(cmd)) {
return R.error(RCode.BAD_REQUEST);
}
Integer timeout = T.MapUtil.getInt(requestBody, "timeout", 10);
return R.ok().putData("result", adbUtil.execShellCommand(cmd, timeout));
}
}