fix: 调整 adb init 时日志
This commit is contained in:
@@ -9,7 +9,9 @@ import net.geedge.common.Constant;
|
|||||||
import net.geedge.common.RCode;
|
import net.geedge.common.RCode;
|
||||||
import net.geedge.common.T;
|
import net.geedge.common.T;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
@@ -718,24 +720,11 @@ public class AdbUtil {
|
|||||||
* exec shell command
|
* exec shell command
|
||||||
*/
|
*/
|
||||||
public String execShellCommand(String cmd, Integer timeout){
|
public String execShellCommand(String cmd, Integer timeout){
|
||||||
Process process = commandExec.execForProcess(AdbCommandBuilder.builder()
|
String result = commandExec.exec(AdbCommandBuilder.builder()
|
||||||
.serial(this.getSerial())
|
.serial(this.getSerial())
|
||||||
.buildShellCommand("shell " + cmd)
|
.buildShellCommand("shell " + cmd)
|
||||||
.build());
|
.build());
|
||||||
|
return result;
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -750,7 +739,7 @@ public class AdbUtil {
|
|||||||
.serial(this.getSerial())
|
.serial(this.getSerial())
|
||||||
.buildShellCommand(String.format("shell \"iptables -L OUTPUT --line-numbers | grep ASW_OUTPUT\""))
|
.buildShellCommand(String.format("shell \"iptables -L OUTPUT --line-numbers | grep ASW_OUTPUT\""))
|
||||||
.build());
|
.build());
|
||||||
log.info("[addAswOutputChain] [ASW_OUTPUT in OUTPUT Chain] [result: {}]", outputChainResult);
|
log.info("[addAswOutputChain] [ASW_OUTPUT in OUTPUT Chain] [exist: {}]", T.StrUtil.isEmpty(outputChainResult));
|
||||||
if (T.StrUtil.isEmpty(outputChainResult)) {
|
if (T.StrUtil.isEmpty(outputChainResult)) {
|
||||||
// ASW_OUTPUT 添加到 OUTPUT 链中
|
// ASW_OUTPUT 添加到 OUTPUT 链中
|
||||||
this.execShellCommand("shell iptables -A OUTPUT -j ASW_OUTPUT");
|
this.execShellCommand("shell iptables -A OUTPUT -j ASW_OUTPUT");
|
||||||
@@ -864,20 +853,25 @@ public class AdbUtil {
|
|||||||
.build();
|
.build();
|
||||||
Process process = commandExec.execForProcess(command);
|
Process process = commandExec.execForProcess(command);
|
||||||
|
|
||||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), logFile, "UTF-8");
|
||||||
Future<String> future = executor.submit(() -> T.IoUtil.read(process.getInputStream(), T.CharsetUtil.CHARSET_UTF_8));
|
InputStreamReader inputStreamReader = null;
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
try {
|
try {
|
||||||
|
inputStreamReader = new InputStreamReader(process.getInputStream(), "UTF-8");
|
||||||
|
bufferedReader = new BufferedReader(inputStreamReader);
|
||||||
|
String line;
|
||||||
|
while ((line = bufferedReader.readLine()) != null) {
|
||||||
|
// 处理每一行输出
|
||||||
|
T.FileUtil.appendString(T.StrUtil.concat(true, line, "\n"), logFile, "UTF-8");
|
||||||
|
}
|
||||||
int exitCode = process.waitFor();
|
int exitCode = process.waitFor();
|
||||||
String result = future.get(10, TimeUnit.SECONDS);
|
return new CommandResult(exitCode, T.StrUtil.EMPTY);
|
||||||
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), logFile, "UTF-8");
|
|
||||||
T.FileUtil.appendString(T.StrUtil.concat(true, result.stripTrailing(), "\n"), logFile, "UTF-8");
|
|
||||||
|
|
||||||
return new CommandResult(exitCode, result);
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
process.destroyForcibly();
|
process.destroyForcibly();
|
||||||
throw new APIException(RCode.ERROR);
|
throw new APIException(RCode.ERROR);
|
||||||
}finally {
|
}finally {
|
||||||
executor.shutdown();
|
T.IoUtil.close(inputStreamReader);
|
||||||
|
T.IoUtil.close(bufferedReader);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,54 @@
|
|||||||
package net.geedge.api.util;
|
package net.geedge.api.util;
|
||||||
|
|
||||||
|
import net.geedge.common.APIException;
|
||||||
|
import net.geedge.common.RCode;
|
||||||
import net.geedge.common.T;
|
import net.geedge.common.T;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class CommandExec {
|
public class CommandExec {
|
||||||
|
|
||||||
private File logFile;
|
private File logFile;
|
||||||
|
private ProcessBuilder processBuilder;
|
||||||
|
|
||||||
public String exec(List<String> command) {
|
public String exec(List<String> command) {
|
||||||
String str = T.RuntimeUtil.execForStr(T.CharsetUtil.CHARSET_UTF_8, command.stream().toArray(String[]::new));
|
|
||||||
if (logFile != null) {
|
if (logFile != null) {
|
||||||
T.FileUtil.appendString(T.StrUtil.concat(true, "$ ", command.stream().collect(Collectors.joining(" ")), "\n"), this.logFile, "UTF-8");
|
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();
|
|
||||||
|
InputStreamReader inputStreamReader = null;
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
|
StringBuilder stringBuilder = null;
|
||||||
|
Process process = null;
|
||||||
|
InputStream inputStream = null;
|
||||||
|
try {
|
||||||
|
process = processBuilder.command(command).start();
|
||||||
|
stringBuilder = new StringBuilder();
|
||||||
|
inputStream = process.getInputStream();
|
||||||
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
|
||||||
|
String line;
|
||||||
|
while ((line = bufferedReader.readLine()) != null) {
|
||||||
|
if (logFile != null) {
|
||||||
|
// 处理每一行输出
|
||||||
|
T.FileUtil.appendString(T.StrUtil.concat(true, line, "\n"), this.logFile, "UTF-8");
|
||||||
|
}
|
||||||
|
stringBuilder.append(line).append(System.lineSeparator());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new APIException(RCode.ERROR);
|
||||||
|
}finally {
|
||||||
|
if (process != null) {
|
||||||
|
process.destroy();
|
||||||
|
}
|
||||||
|
T.IoUtil.close(inputStreamReader);
|
||||||
|
T.IoUtil.close(bufferedReader);
|
||||||
|
T.IoUtil.close(inputStream);
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringBuilder.toString().stripTrailing();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Process execForProcess(List<String> command) {
|
public Process execForProcess(List<String> command) {
|
||||||
@@ -26,5 +58,6 @@ public class CommandExec {
|
|||||||
|
|
||||||
public CommandExec(File logFile ) {
|
public CommandExec(File logFile ) {
|
||||||
this.logFile = logFile;
|
this.logFile = logFile;
|
||||||
|
this.processBuilder = new ProcessBuilder();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user