feat: 请求 API 添加 token 认证;调整 tcpdump 执行逻辑;新增 execShellCmd 接口
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -482,6 +482,7 @@ public class AdbUtil {
|
||||
* iptables -F
|
||||
* iptables -X
|
||||
*/
|
||||
@Deprecated
|
||||
private void cleanIptables() {
|
||||
// Delete all rules in chain or all chains
|
||||
CommandExec.exec(AdbCommandBuilder.builder()
|
||||
@@ -497,17 +498,47 @@ public class AdbUtil {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* list tcpdump
|
||||
*/
|
||||
public List<Map> listTcpdump() {
|
||||
String result = CommandExec.exec(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep capture_ | awk '{print $NF}' \""))
|
||||
.build());
|
||||
|
||||
List<Map> list = T.ListUtil.list(true);
|
||||
|
||||
String[] lines = result.split("\\n");
|
||||
for (String line : lines) {
|
||||
try {
|
||||
String fileName = T.FileUtil.mainName(line);
|
||||
String taskId = "", packageName = "";
|
||||
if (fileName.contains("capture_all_")) {
|
||||
taskId = fileName.replaceAll("capture_all_", "");
|
||||
} else {
|
||||
String[] split = fileName.split("_");
|
||||
packageName = split[2];
|
||||
taskId = split[split.length - 1];
|
||||
}
|
||||
Map<Object, Object> m = T.MapUtil.builder()
|
||||
.put("id", taskId)
|
||||
.put("packageName", packageName)
|
||||
.build();
|
||||
list.add(m);
|
||||
} catch (Exception e) {
|
||||
log.warn(e, "[listTcpdump] [get task info error] [line: {}]", line);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* start Tcpdump
|
||||
* iptables option
|
||||
* tcpdump pcap
|
||||
*/
|
||||
public CommandResult startTcpdump(String packageName) {
|
||||
// clean iptables conf
|
||||
this.cleanIptables();
|
||||
|
||||
String taskId = T.IdUtil.fastSimpleUUID();
|
||||
String pcapFilePath = "/data/local/tmp/" + taskId + ".pcap";
|
||||
if (T.StrUtil.isNotEmpty(packageName)) {
|
||||
log.info("[startTcpdump] [capture app package] [pkg: {}]", packageName);
|
||||
String dumpsysResult = CommandExec.exec(AdbCommandBuilder.builder()
|
||||
@@ -541,12 +572,16 @@ public class AdbUtil {
|
||||
.build());
|
||||
log.info("[startTcpdump] [iptables -L] [result: {}]", ruleList);
|
||||
|
||||
// pcap 格式:capture_{userId}_{pcakageName}_{taskId}.pcap
|
||||
String pcapFilePath = "/data/local/tmp/capture_" + userId + "_" + packageName + "_" + taskId + ".pcap";
|
||||
CommandExec.execForProcess(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell tcpdump -i nflog:%s -w %s &", userId, pcapFilePath))
|
||||
.build());
|
||||
} else {
|
||||
log.info("[startTcpdump] [capture all package]");
|
||||
// pcap 格式:capture_all_{taskId}.pcap
|
||||
String pcapFilePath = "/data/local/tmp/capture_all_" + taskId + ".pcap";
|
||||
CommandExec.execForProcess(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell tcpdump -w %s &", pcapFilePath))
|
||||
@@ -566,12 +601,39 @@ public class AdbUtil {
|
||||
* kill -INT {pid}
|
||||
*/
|
||||
public CommandResult stopTcpdump(String id) {
|
||||
String pcapFilePath = CommandExec.exec(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep %s | awk '{print $NF}' \"", id))
|
||||
.build());
|
||||
if (T.StrUtil.isNotEmpty(pcapFilePath)) {
|
||||
if (!pcapFilePath.contains("capture_all_")) {
|
||||
// 删除 iptables rule
|
||||
String[] split = T.FileUtil.mainName(pcapFilePath).split("_");
|
||||
String userId = split[1];
|
||||
log.info("[stopTcpdump] [remove iptables rule] [userId: {}]", userId);
|
||||
CommandExec.exec(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell iptables -D OUTPUT -m owner --uid-owner %s -j CONNMARK --set-mark %s", userId, userId))
|
||||
.build());
|
||||
CommandExec.exec(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell iptables -D INPUT -m connmark --mark %s -j NFLOG --nflog-group %s", userId, userId))
|
||||
.build());
|
||||
CommandExec.exec(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell iptables -D OUTPUT -m connmark --mark %s -j NFLOG --nflog-group %s", userId, userId))
|
||||
.build());
|
||||
}
|
||||
}
|
||||
String result = CommandExec.exec(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep %s | awk '{print $2}' | xargs kill -INT \"", id))
|
||||
.build());
|
||||
log.info("[stopTcpdump] [id: {}] [result: {}]", id, result);
|
||||
return new CommandResult(T.StrUtil.isEmpty(result) ? 0 : 1, result);
|
||||
log.info("[stopTcpdump] [id: {}] [pcapFilePath: {}] [result: {}]", id, pcapFilePath, result);
|
||||
if (T.StrUtil.isEmpty(result)) {
|
||||
return new CommandResult(0, pcapFilePath);
|
||||
}
|
||||
return new CommandResult(1, result);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -585,6 +647,30 @@ public class AdbUtil {
|
||||
log.info("[execShellCommand] [shellCmd: {}] [result: {}]", shellCmd, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* exec shell command
|
||||
*/
|
||||
public String execShellCommand(String cmd, Integer timeout){
|
||||
Process process = CommandExec.execForProcess(AdbCommandBuilder.builder()
|
||||
.serial(this.getSerial())
|
||||
.buildShellCommand("shell " + cmd)
|
||||
.build());
|
||||
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
Future<String> future = executor.submit(() -> T.IoUtil.read(process.getInputStream(), T.CharsetUtil.CHARSET_UTF_8));
|
||||
try {
|
||||
String result = future.get(timeout, TimeUnit.SECONDS);
|
||||
return result;
|
||||
} catch (TimeoutException e) {
|
||||
process.destroyForcibly();
|
||||
throw new APIException(RCode.TIMEOUT);
|
||||
} catch (ExecutionException | InterruptedException e) {
|
||||
throw new APIException(RCode.ERROR);
|
||||
} finally {
|
||||
executor.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private synchronized ExecutorService getThreadPool() {
|
||||
if (threadPool == null) {
|
||||
threadPool = new ThreadPoolExecutor(
|
||||
|
||||
Reference in New Issue
Block a user