fix: 调整 playbook 执行时serial 参数获取

This commit is contained in:
zhangshuai
2024-10-31 17:02:52 +08:00
parent 1d6b710f63
commit 97adfa39cd
2 changed files with 220 additions and 126 deletions

View File

@@ -15,6 +15,7 @@ import java.util.*;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class AdbUtil {
@@ -32,8 +33,14 @@ public class AdbUtil {
private Integer vncPort;
private File logFile;
private ExecutorService threadPool;
public void setLogFile(File logFile) {
this.logFile = logFile;
}
public String getSerial() {
return T.StrUtil.isNotEmpty(this.serial) ? serial : String.format("%s:%s", this.host, this.port);
}
@@ -86,7 +93,7 @@ public class AdbUtil {
}
} else {
// remote
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile ,false).exec(AdbCommandBuilder.builder()
.buildConnectCommand(this.host, this.port)
.build());
log.info("[connect] [result: {}]", result);
@@ -104,7 +111,7 @@ public class AdbUtil {
*/
public void init(boolean install) {
// adb root
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile ,false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildRootCommand()
.build()
@@ -113,26 +120,26 @@ public class AdbUtil {
if (install) {
// install droidVNC NG
CommandResult installed = this.install(DEFAULT_DROIDVNC_NG_APK_PATH, true, true);
CommandResult installed = this.install(DEFAULT_DROIDVNC_NG_APK_PATH, true, true,false);
log.info("[init] [install droidVNC NG] [result: {}]", installed);
// 上传默认配置
this.execShellCommand("shell mkdir -p /storage/emulated/0/Android/data/net.christianbeier.droidvnc_ng/files");
this.execShellCommand("shell mkdir -p /storage/emulated/0/Android/data/net.christianbeier.droidvnc_ng/files",false);
this.push(DEFAULT_DROIDVNC_NG_DEFAULTS_JSON_PATH, "/storage/emulated/0/Android/data/net.christianbeier.droidvnc_ng/files/defaults.json");
// 无障碍权限
this.execShellCommand("shell settings put secure enabled_accessibility_services net.christianbeier.droidvnc_ng/.InputService:$(settings get secure enabled_accessibility_services)");
this.execShellCommand("shell settings put secure enabled_accessibility_services net.christianbeier.droidvnc_ng/.InputService:$(settings get secure enabled_accessibility_services)",false);
// 存储空间权限
this.execShellCommand("shell pm grant net.christianbeier.droidvnc_ng android.permission.WRITE_EXTERNAL_STORAGE");
this.execShellCommand("shell pm grant net.christianbeier.droidvnc_ng android.permission.WRITE_EXTERNAL_STORAGE",false);
// 屏幕录制权限
this.execShellCommand("shell appops set net.christianbeier.droidvnc_ng PROJECT_MEDIA allow");
this.execShellCommand("shell appops set net.christianbeier.droidvnc_ng PROJECT_MEDIA allow",false);
// ACTION_STOP
this.execShellCommand("shell am start-foreground-service -n net.christianbeier.droidvnc_ng/.MainService -a net.christianbeier.droidvnc_ng.ACTION_STOP --es net.christianbeier.droidvnc_ng.EXTRA_ACCESS_KEY d042e2b5d5f348588a4e1a243eb7a9a0");
this.execShellCommand("shell am start-foreground-service -n net.christianbeier.droidvnc_ng/.MainService -a net.christianbeier.droidvnc_ng.ACTION_STOP --es net.christianbeier.droidvnc_ng.EXTRA_ACCESS_KEY d042e2b5d5f348588a4e1a243eb7a9a0",false);
}
// ACTION_START
this.execShellCommand("shell am start-foreground-service -n net.christianbeier.droidvnc_ng/.MainService -a net.christianbeier.droidvnc_ng.ACTION_START --es net.christianbeier.droidvnc_ng.EXTRA_ACCESS_KEY d042e2b5d5f348588a4e1a243eb7a9a0");
this.execShellCommand("shell am start-foreground-service -n net.christianbeier.droidvnc_ng/.MainService -a net.christianbeier.droidvnc_ng.ACTION_START --es net.christianbeier.droidvnc_ng.EXTRA_ACCESS_KEY d042e2b5d5f348588a4e1a243eb7a9a0",false);
// 添加自定义链
this.addAswOutputChain();
@@ -172,7 +179,7 @@ public class AdbUtil {
m.put("type", type);
// check root
String checkRootResult = CommandExec.exec(AdbCommandBuilder.builder()
String checkRootResult = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildCheckRootCommand()
.build()
@@ -188,7 +195,7 @@ public class AdbUtil {
* @return
*/
private AdbDevice getAdbDevice() {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.buildDevicesCommand()
.build()
);
@@ -213,7 +220,7 @@ public class AdbUtil {
* @return
*/
private Map<String, String> getProp() {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildGetpropCommand()
.build()
@@ -230,7 +237,7 @@ public class AdbUtil {
}
// 分辨率 Physical size: 1440x3040
String wmSize = CommandExec.exec(AdbCommandBuilder.builder()
String wmSize = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildWmSizeCommand()
.build()
@@ -244,7 +251,7 @@ public class AdbUtil {
* md5sum
*/
private CommandResult md5sum(String path) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildMd5sumCommand(path)
.build()
@@ -262,7 +269,7 @@ public class AdbUtil {
* 0 success; !0 failed
*/
public CommandResult push(String local, String remote) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildPushCommand(local, remote)
.build()
@@ -275,8 +282,8 @@ public class AdbUtil {
* pull
* 0 success; !0 failed
*/
public CommandResult pull(String remote, String local) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
public CommandResult pull(String remote, String local, boolean isRecordLog) {
String result = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildPullCommand(remote, local)
.build()
@@ -291,7 +298,7 @@ public class AdbUtil {
* stat filename
*/
public List<Map> listDir(String path) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildLsDirCommand(path)
.build()
@@ -329,7 +336,7 @@ public class AdbUtil {
String statCommand = "shell stat -c \"'%N %A %g %u %s %a %X %Y'\" " + statFilePath;
futureList.add(
CompletableFuture.supplyAsync(() -> {
String statResult = CommandExec.exec(AdbCommandBuilder.builder()
String statResult = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(statCommand.replaceAll("\\\\", "/"))
.build()
@@ -390,7 +397,7 @@ public class AdbUtil {
* @return
*/
public List<Map> listApp(String arg) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildPmListPackagesCommand(arg)
.build()
@@ -406,7 +413,7 @@ public class AdbUtil {
String packageName = T.StrUtil.trim(line.substring(prefix.length()));
if (T.StrUtil.equals(DEFAULT_DROIDVNC_NG_PKG_NAME, packageName)) continue;
String dumpsysResult = CommandExec.exec(AdbCommandBuilder.builder()
String dumpsysResult = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell dumpsys package " + packageName)
.build()
@@ -430,7 +437,7 @@ public class AdbUtil {
String md5Value = md5sumRes.output();
File localApk = T.FileUtil.file(Constant.TEMP_PATH, md5Value + ".apk");
if (!T.FileUtil.exist(localApk)) {
CommandResult pulled = this.pull(finalApkPath, localApk.getAbsolutePath());
CommandResult pulled = this.pull(finalApkPath, localApk.getAbsolutePath(), false);
if (0 != pulled.exitCode()) {
log.warn("[listApp] [pull apk error] [pkg: {}]", packageName);
return null;
@@ -487,12 +494,12 @@ public class AdbUtil {
* install app
* adb install apk
*/
public CommandResult install(String localFilePath, boolean isDebugApk, boolean isReInstall) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
public CommandResult install(String localFilePath, boolean isDebugApk, boolean isReInstall, boolean isRecordLog) {
String result = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildInstallCommand(localFilePath, isDebugApk, isReInstall)
.build()
);
.build());
log.info("[install] [localFilePath: {}] [isDebugApk: {}] [isReInstall: {}] [result: {}]", localFilePath, isDebugApk, isReInstall, result);
return new CommandResult(T.StrUtil.containsAny(result, "Success") ? 0 : 1, result);
}
@@ -502,7 +509,7 @@ public class AdbUtil {
* adb uninstall package_name
*/
public CommandResult uninstall(String packageName) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildUnInstallCommand(packageName)
.build()
@@ -515,8 +522,8 @@ public class AdbUtil {
* stop app
* adb shell am force-stop package_name
*/
public CommandResult stopApp(String packageName) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
public CommandResult stopApp(String packageName, boolean isRecordLog) {
String result = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildStopAppCommand(packageName)
.build()
@@ -532,13 +539,13 @@ public class AdbUtil {
@Deprecated
private void cleanIptables() {
// Delete all rules in chain or all chains
CommandExec.exec(AdbCommandBuilder.builder()
new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell iptables -F")
.build()
);
// Delete user-defined chain
CommandExec.exec(AdbCommandBuilder.builder()
new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell iptables -X")
.build()
@@ -549,7 +556,7 @@ public class AdbUtil {
* list tcpdump
*/
public List<Map> listTcpdump() {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep capture_ | awk '{print $NF}' \""))
.build());
@@ -584,11 +591,11 @@ public class AdbUtil {
* start Tcpdump
* tcpdump pcap
*/
public CommandResult startTcpdump(String packageName) {
public CommandResult startTcpdump(String packageName, boolean isRecordLog) {
String taskId = T.IdUtil.fastSimpleUUID();
if (T.StrUtil.isNotEmpty(packageName)) {
log.info("[startTcpdump] [capture app package] [pkg: {}]", packageName);
String dumpsysResult = CommandExec.exec(AdbCommandBuilder.builder()
String dumpsysResult = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell dumpsys package " + packageName)
.build()
@@ -600,20 +607,20 @@ public class AdbUtil {
.map(s -> T.StrUtil.trim(s).replaceAll("userId=", ""))
.orElseThrow(() -> new APIException("Not found userId by package name. package name: " + packageName));
CommandExec.exec(AdbCommandBuilder.builder()
new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell iptables -A OUTPUT -m owner --uid-owner %s -j CONNMARK --set-mark %s", userId, userId))
.build());
CommandExec.exec(AdbCommandBuilder.builder()
new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell iptables -A INPUT -m connmark --mark %s -j NFLOG --nflog-group %s", userId, userId))
.build());
CommandExec.exec(AdbCommandBuilder.builder()
new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell iptables -A OUTPUT -m connmark --mark %s -j NFLOG --nflog-group %s", userId, userId))
.build());
String ruleList = CommandExec.exec(AdbCommandBuilder.builder()
String ruleList = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell iptables -L")
.build());
@@ -621,7 +628,7 @@ public class AdbUtil {
// pcap 格式capture_{userId}_{pcakageName}_{taskId}.pcap
String pcapFilePath = "/data/local/tmp/capture_" + userId + "_" + packageName + "_" + taskId + ".pcap";
CommandExec.execForProcess(AdbCommandBuilder.builder()
new CommandExec(logFile, isRecordLog).execForProcess(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell tcpdump -i nflog:%s -w %s &", userId, pcapFilePath))
.build());
@@ -629,13 +636,13 @@ public class AdbUtil {
log.info("[startTcpdump] [capture all package]");
// pcap 格式capture_all_{taskId}.pcap
String pcapFilePath = "/data/local/tmp/capture_all_" + taskId + ".pcap";
CommandExec.execForProcess(AdbCommandBuilder.builder()
new CommandExec(logFile, isRecordLog).execForProcess(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell tcpdump not port %s -w %s &", this.vncPort, pcapFilePath))
.build());
}
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep %s | awk '{print $2}' \"", taskId))
.build());
@@ -647,8 +654,8 @@ public class AdbUtil {
* stop tcpdump
* kill -INT {pid}
*/
public CommandResult stopTcpdump(String id) {
String pcapFilePath = CommandExec.exec(AdbCommandBuilder.builder()
public CommandResult stopTcpdump(String id, boolean isRecordLog) {
String pcapFilePath = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep %s | awk '{print $NF}' \"", id))
.build());
@@ -658,21 +665,21 @@ public class AdbUtil {
String[] split = T.FileUtil.mainName(pcapFilePath).split("_");
String userId = split[1];
log.info("[stopTcpdump] [remove iptables rule] [userId: {}]", userId);
CommandExec.exec(AdbCommandBuilder.builder()
new CommandExec(logFile, isRecordLog).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()
new CommandExec(logFile, isRecordLog).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()
new CommandExec(logFile, isRecordLog).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()
String result = new CommandExec(logFile, isRecordLog).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());
@@ -681,7 +688,7 @@ public class AdbUtil {
for (int i = 0; i < 10; i++) {
T.ThreadUtil.sleep(500);
String str = CommandExec.exec(AdbCommandBuilder.builder()
String str = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"ps -ef | grep tcpdump | grep -v grep | grep %s \"", id))
.build());
@@ -701,8 +708,8 @@ public class AdbUtil {
/**
* exec shell command
*/
public void execShellCommand(String shellCmd) {
String result = CommandExec.exec(AdbCommandBuilder.builder()
public void execShellCommand(String shellCmd, boolean isRecordLog) {
String result = new CommandExec(logFile, isRecordLog).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(shellCmd)
.build());
@@ -713,7 +720,7 @@ public class AdbUtil {
* exec shell command
*/
public String execShellCommand(String cmd, Integer timeout){
Process process = CommandExec.execForProcess(AdbCommandBuilder.builder()
Process process = new CommandExec(logFile, false).execForProcess(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand("shell " + cmd)
.build());
@@ -739,16 +746,16 @@ public class AdbUtil {
*/
private void addAswOutputChain() {
// name=ASW_OUTPUT
this.execShellCommand("shell iptables -N ASW_OUTPUT");
this.execShellCommand("shell iptables -N ASW_OUTPUT", false);
String outputChainResult = CommandExec.exec(AdbCommandBuilder.builder()
String outputChainResult = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildShellCommand(String.format("shell \"iptables -L OUTPUT --line-numbers | grep ASW_OUTPUT\""))
.build());
log.info("[addAswOutputChain] [ASW_OUTPUT in OUTPUT Chain] [result: {}]", outputChainResult);
if (T.StrUtil.isEmpty(outputChainResult)) {
// ASW_OUTPUT 添加到 OUTPUT 链中
this.execShellCommand("shell iptables -A OUTPUT -j ASW_OUTPUT");
this.execShellCommand("shell iptables -A OUTPUT -j ASW_OUTPUT", false);
}
}
@@ -757,7 +764,7 @@ public class AdbUtil {
* iptables -nL ASW_OUTPUT --line-numbers
*/
public List<Map> listAcl() {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildIptablesLnRulesCommand("ASW_OUTPUT")
.build());
@@ -804,7 +811,7 @@ public class AdbUtil {
this.addAswOutputChain();
// add chain ruls
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildIptablesAddRuleCommand("ASW_OUTPUT", protocol, ip, port)
.build());
@@ -818,7 +825,7 @@ public class AdbUtil {
*/
public void deleteAcl(String protocol, String ip, String port) {
// add chain ruls
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildIptablesDelRuleCommand("ASW_OUTPUT", protocol, ip, port)
.build());
@@ -830,7 +837,7 @@ public class AdbUtil {
* iptables -F ASW_OUTPUT
*/
public CommandResult flushAcl() {
String result = CommandExec.exec(AdbCommandBuilder.builder()
String result = new CommandExec(logFile, false).exec(AdbCommandBuilder.builder()
.serial(this.getSerial())
.buildIptablesFlushRuleCommand("ASW_OUTPUT")
.build());
@@ -851,30 +858,48 @@ public class AdbUtil {
return threadPool;
}
class CommandExec {
public static String exec(List<String> command) {
private File logFile;
private boolean isRecordLog;
public String exec(List<String> command) {
String str = T.RuntimeUtil.execForStr(T.CharsetUtil.CHARSET_UTF_8, command.stream().toArray(String[]::new));
if (isRecordLog) {
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), this.logFile, "UTF-8");
T.FileUtil.appendString(T.StrUtil.concat(true, str.stripTrailing(), "\n"), this.logFile, "UTF-8");
}
return str.stripTrailing();
}
public static Process execForProcess(List<String> command) {
public Process execForProcess(List<String> command) {
Process process = T.RuntimeUtil.exec(command.stream().toArray(String[]::new));
return process;
}
public CommandExec(File logFile, boolean isRecordLog) {
this.logFile = logFile;
this.isRecordLog = isRecordLog;
}
}
public CommandResult execPlaybook(String playbookPath) {
public CommandResult execPlaybook(String playbookPath, boolean isRecordLog) {
log.info("[execPlaybook] [begin!] [serial:{}]", this.getSerial());
Process process = CommandExec.execForProcess(new AdbCommandBuilder("airtest")
List<String> command = new AdbCommandBuilder("airtest")
.buildRunPlaybook(playbookPath, this.getSerial())
.build());
.build();
Process process = new CommandExec(logFile, isRecordLog).execForProcess(command);
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> future = executor.submit(() -> T.IoUtil.read(process.getInputStream(), T.CharsetUtil.CHARSET_UTF_8));
try {
int exitCode = process.waitFor();
String result = future.get(10, TimeUnit.SECONDS);
log.info("[execPlaybook] [result: {}]", result);
if (isRecordLog){
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), this.logFile, "UTF-8");
T.FileUtil.appendString(T.StrUtil.concat(true, result.stripTrailing(), "\n"), this.logFile, "UTF-8");
}
return new CommandResult(exitCode, result);
} catch (Exception e) {
process.destroyForcibly();