feat: tcpdump 时过滤掉 vnc 数据包

1. 项目启动时从 ./lib/droidvnc-np-defaults.json 读取 vnc port
2. 不论是否输入package name 全部过滤掉 vnc 流量
3. 结束捕包时删除 pcap 文件
This commit is contained in:
shizhendong
2024-09-09 13:55:39 +08:00
parent b940421c69
commit 87e211987b
4 changed files with 41 additions and 19 deletions

View File

@@ -1,7 +1,9 @@
package net.geedge; package net.geedge;
import cn.hutool.extra.spring.EnableSpringUtil; import cn.hutool.extra.spring.EnableSpringUtil;
import cn.hutool.log.Log;
import net.geedge.api.entity.EnvApiYml; import net.geedge.api.entity.EnvApiYml;
import net.geedge.common.T;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -14,6 +16,8 @@ import java.util.TimeZone;
@SpringBootApplication @SpringBootApplication
public class EnvApiApplication { public class EnvApiApplication {
private final static Log log = Log.get();
public static void main(String[] args) { public static void main(String[] args) {
TimeZone.setDefault(TimeZone.getTimeZone("UTC")); TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
SpringApplication.run(EnvApiApplication.class, args); SpringApplication.run(EnvApiApplication.class, args);
@@ -36,6 +40,10 @@ public class EnvApiApplication {
adb.setHost(environment.getProperty("adb.host")); adb.setHost(environment.getProperty("adb.host"));
adb.setPort(environment.getProperty("adb.port", Integer.class)); adb.setPort(environment.getProperty("adb.port", Integer.class));
String droidvncDefaultConfig = T.FileUtil.readUtf8String(T.FileUtil.file(T.WebPathUtil.getRootPath(), "./lib/droidvnc-np-defaults.json"));
Integer vncPort = T.JSONUtil.parseObj(droidvncDefaultConfig).getInt("port", 5900);
adb.setVncPort(vncPort);
EnvApiYml.Vnc vnc = apiYml.new Vnc(); EnvApiYml.Vnc vnc = apiYml.new Vnc();
vnc.setHost(environment.getProperty("vnc.host")); vnc.setHost(environment.getProperty("vnc.host"));
vnc.setPort(environment.getProperty("vnc.port", Integer.class)); vnc.setPort(environment.getProperty("vnc.port", Integer.class));
@@ -43,6 +51,8 @@ public class EnvApiApplication {
apiYml.setEnv(envEntity); apiYml.setEnv(envEntity);
apiYml.setAdb(adb); apiYml.setAdb(adb);
apiYml.setVnc(vnc); apiYml.setVnc(vnc);
log.info("[envProperties] [value: {}]", T.JSONUtil.toJsonStr(apiYml));
return apiYml; return apiYml;
} }
} }

View File

@@ -150,11 +150,12 @@ public class APIController {
throw new APIException(result.output()); throw new APIException(result.output());
} }
String filePath = result.output();
try {
if (returnFile) { if (returnFile) {
// response pcap file // response pcap file
File tempFile = T.FileUtil.file(Constant.TEMP_PATH, id + ".pcap"); File tempFile = T.FileUtil.file(Constant.TEMP_PATH, id + ".pcap");
try { try {
String filePath = result.output();
if (T.StrUtil.isEmpty(filePath)) { if (T.StrUtil.isEmpty(filePath)) {
throw new APIException(RCode.NOT_EXISTS); throw new APIException(RCode.NOT_EXISTS);
} }
@@ -170,6 +171,12 @@ public class APIController {
// response taskid // response taskid
response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData("id", id))); response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData("id", id)));
} }
} finally {
if (T.StrUtil.isNotEmpty(filePath)) {
// remove pcap file
adbUtil.execShellCommand(String.format("shell rm -rf %s", filePath));
}
}
} }
@PostMapping("/shell") @PostMapping("/shell")

View File

@@ -21,6 +21,8 @@ public class EnvApiYml {
String serial; String serial;
String host; String host;
Integer port; Integer port;
Integer vncPort;
} }
@Data @Data

View File

@@ -29,6 +29,8 @@ public class AdbUtil {
private String host; private String host;
private Integer port; private Integer port;
private Integer vncPort;
private ExecutorService threadPool; private ExecutorService threadPool;
public String getSerial() { public String getSerial() {
@@ -42,6 +44,7 @@ public class AdbUtil {
this.serial = T.StrUtil.emptyToDefault(adb.getSerial(), ""); this.serial = T.StrUtil.emptyToDefault(adb.getSerial(), "");
this.host = adb.getHost(); this.host = adb.getHost();
this.port = adb.getPort(); this.port = adb.getPort();
this.vncPort = adb.getVncPort();
// adb connect // adb connect
this.connect(); this.connect();
// init // init
@@ -585,7 +588,7 @@ public class AdbUtil {
String pcapFilePath = "/data/local/tmp/capture_" + userId + "_" + packageName + "_" + taskId + ".pcap"; String pcapFilePath = "/data/local/tmp/capture_" + userId + "_" + packageName + "_" + taskId + ".pcap";
CommandExec.execForProcess(AdbCommandBuilder.builder() CommandExec.execForProcess(AdbCommandBuilder.builder()
.serial(this.getSerial()) .serial(this.getSerial())
.buildShellCommand(String.format("shell tcpdump -i nflog:%s -w %s &", userId, pcapFilePath)) .buildShellCommand(String.format("shell tcpdump not port %s -i nflog:%s -w %s &", this.vncPort, userId, pcapFilePath))
.build()); .build());
} else { } else {
log.info("[startTcpdump] [capture all package]"); log.info("[startTcpdump] [capture all package]");
@@ -593,7 +596,7 @@ public class AdbUtil {
String pcapFilePath = "/data/local/tmp/capture_all_" + taskId + ".pcap"; String pcapFilePath = "/data/local/tmp/capture_all_" + taskId + ".pcap";
CommandExec.execForProcess(AdbCommandBuilder.builder() CommandExec.execForProcess(AdbCommandBuilder.builder()
.serial(this.getSerial()) .serial(this.getSerial())
.buildShellCommand(String.format("shell tcpdump -w %s &", pcapFilePath)) .buildShellCommand(String.format("shell tcpdump not port %s -w %s &", this.vncPort, pcapFilePath))
.build()); .build());
} }