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

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