diff --git a/src/main/java/net/geedge/EnvApiApplication.java b/src/main/java/net/geedge/EnvApiApplication.java index 42f8818..4983142 100644 --- a/src/main/java/net/geedge/EnvApiApplication.java +++ b/src/main/java/net/geedge/EnvApiApplication.java @@ -1,7 +1,9 @@ package net.geedge; import cn.hutool.extra.spring.EnableSpringUtil; +import cn.hutool.log.Log; import net.geedge.api.entity.EnvApiYml; +import net.geedge.common.T; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -14,6 +16,8 @@ import java.util.TimeZone; @SpringBootApplication public class EnvApiApplication { + private final static Log log = Log.get(); + public static void main(String[] args) { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); SpringApplication.run(EnvApiApplication.class, args); @@ -36,6 +40,10 @@ public class EnvApiApplication { adb.setHost(environment.getProperty("adb.host")); 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(); vnc.setHost(environment.getProperty("vnc.host")); vnc.setPort(environment.getProperty("vnc.port", Integer.class)); @@ -43,6 +51,8 @@ public class EnvApiApplication { apiYml.setEnv(envEntity); apiYml.setAdb(adb); apiYml.setVnc(vnc); + + log.info("[envProperties] [value: {}]", T.JSONUtil.toJsonStr(apiYml)); return apiYml; } } \ No newline at end of file diff --git a/src/main/java/net/geedge/api/controller/APIController.java b/src/main/java/net/geedge/api/controller/APIController.java index 477b489..182f9d6 100644 --- a/src/main/java/net/geedge/api/controller/APIController.java +++ b/src/main/java/net/geedge/api/controller/APIController.java @@ -150,25 +150,32 @@ public class APIController { throw new APIException(result.output()); } - if (returnFile) { - // response pcap file - File tempFile = T.FileUtil.file(Constant.TEMP_PATH, id + ".pcap"); - try { - String filePath = result.output(); - if (T.StrUtil.isEmpty(filePath)) { - throw new APIException(RCode.NOT_EXISTS); + String filePath = result.output(); + try { + if (returnFile) { + // response pcap file + File tempFile = T.FileUtil.file(Constant.TEMP_PATH, id + ".pcap"); + try { + 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()); + } + T.ResponseUtil.downloadFile(response, tempFile.getName(), T.FileUtil.readBytes(tempFile)); + } finally { + T.FileUtil.del(tempFile); } - AdbUtil.CommandResult pulled = adbUtil.pull(filePath, tempFile.getAbsolutePath()); - if (0 != pulled.exitCode()) { - throw new APIException(pulled.output()); - } - T.ResponseUtil.downloadFile(response, tempFile.getName(), T.FileUtil.readBytes(tempFile)); - } finally { - T.FileUtil.del(tempFile); + } else { + // response taskid + 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)); } - } else { - // response taskid - response.getWriter().write(T.JSONUtil.toJsonStr(R.ok().putData("id", id))); } } diff --git a/src/main/java/net/geedge/api/entity/EnvApiYml.java b/src/main/java/net/geedge/api/entity/EnvApiYml.java index e568262..547ab0a 100644 --- a/src/main/java/net/geedge/api/entity/EnvApiYml.java +++ b/src/main/java/net/geedge/api/entity/EnvApiYml.java @@ -21,6 +21,8 @@ public class EnvApiYml { String serial; String host; Integer port; + + Integer vncPort; } @Data diff --git a/src/main/java/net/geedge/api/util/AdbUtil.java b/src/main/java/net/geedge/api/util/AdbUtil.java index 9a72fa6..a17230b 100644 --- a/src/main/java/net/geedge/api/util/AdbUtil.java +++ b/src/main/java/net/geedge/api/util/AdbUtil.java @@ -29,6 +29,8 @@ public class AdbUtil { private String host; private Integer port; + private Integer vncPort; + private ExecutorService threadPool; public String getSerial() { @@ -42,6 +44,7 @@ public class AdbUtil { this.serial = T.StrUtil.emptyToDefault(adb.getSerial(), ""); this.host = adb.getHost(); this.port = adb.getPort(); + this.vncPort = adb.getVncPort(); // adb connect this.connect(); // init @@ -585,7 +588,7 @@ public class AdbUtil { 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)) + .buildShellCommand(String.format("shell tcpdump not port %s -i nflog:%s -w %s &", this.vncPort, userId, pcapFilePath)) .build()); } else { log.info("[startTcpdump] [capture all package]"); @@ -593,7 +596,7 @@ public class AdbUtil { String pcapFilePath = "/data/local/tmp/capture_all_" + taskId + ".pcap"; CommandExec.execForProcess(AdbCommandBuilder.builder() .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()); }