Matthew 1 month ago
commit 713f69048f

@ -23,28 +23,63 @@ public class AdbUtil {
} }
} }
public static void ShellList(ArrayList<String> shellList) { public static void ShellList(boolean ifsu, ArrayList<String> shellList) {
// try { // try {
// for (String cmd : shellList) { // // 将整个命令包裹在单引号中,作为 -c 后的唯一参数
// Process process = Runtime.getRuntime().exec(cmd); // String[] cmds = {
// // 处理输出和错误流 // "su",
// int i = process.waitFor(); // "-c",
// System.out.println(""+i); // "rm -rf /sdcard/com.xypower.mpapp/tmp/*"
// } // };
// Process process = Runtime.getRuntime().exec(cmds);
// // 消费输入流(防止阻塞)
// new Thread(() -> {
// try (BufferedReader reader = new BufferedReader(
// new InputStreamReader(process.getInputStream())
// )) {
// while (reader.readLine() != null); // 静默消费
// } catch (IOException e) {
// e.printStackTrace();
// }
// }).start();
// // 消费错误流(关键!)
// new Thread(() -> {
// try (BufferedReader errReader = new BufferedReader(
// new InputStreamReader(process.getErrorStream())
// )) {
// String line;
// while ((line = errReader.readLine()) != null) {
// System.err.println("SU_ERROR: " + line); // 输出错误信息
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }).start();
// // 处理输出和错误流
// int i = process.waitFor();
// System.out.println("" + i);
// } catch (Exception e) { // } catch (Exception e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
try { try {
// 使用 su 获取 root 权限 Process process = null;
Process process = Runtime.getRuntime().exec("su"); if (shellList != null && shellList.size() > 0) {
if (ifsu) {
// 使用 su 获取 root 权限
process = Runtime.getRuntime().exec("su");
} else {
process = Runtime.getRuntime().exec("sh");
}
}
if (process == null) {
return;
}
// 获取输出流,用于发送命令 // 获取输出流,用于发送命令
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
for (int i = 0; i < shellList.size(); i++) { for (int i = 0; i < shellList.size(); i++) {
outputStream.writeBytes(shellList.get(i)+"\n"); outputStream.writeBytes(shellList.get(i) + "\n");
} }
// 执行命令(例如:列出 /data 目录) // 执行命令(例如:列出 /data 目录)
@ -58,7 +93,6 @@ public class AdbUtil {
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null) {
Log.d("Output", line); // 打印输出 Log.d("Output", line); // 打印输出
} }
// 等待命令执行完成 // 等待命令执行完成
int i = process.waitFor(); int i = process.waitFor();
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {

@ -564,6 +564,7 @@ public class SimUtil {
sendmessage = getSendString(content, ifmessageCorrect); sendmessage = getSendString(content, ifmessageCorrect);
} else if (content.contains(SmsTypeEnum.SHELL.value())) { } else if (content.contains(SmsTypeEnum.SHELL.value())) {
ifmessageCorrect = true; ifmessageCorrect = true;
boolean ifsu = false;
ArrayList<String> shellList = new ArrayList<>(); ArrayList<String> shellList = new ArrayList<>();
int startindex = content.indexOf("="); int startindex = content.indexOf("=");
if (startindex != -1) { if (startindex != -1) {
@ -574,8 +575,14 @@ public class SimUtil {
String[] strings = StringUtils.splitString2(substring); String[] strings = StringUtils.splitString2(substring);
if (strings != null && strings.length > 0) { if (strings != null && strings.length > 0) {
for (int i = 0; i < strings.length; i++) { for (int i = 0; i < strings.length; i++) {
shellList.add(strings[i]); if ("su".equals(strings[0])) {
ifsu = true;
} else {
ifsu = false;
shellList.add(strings[i]);
}
} }
boolean finalIfsu = ifsu;
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -584,7 +591,7 @@ public class SimUtil {
} catch (InterruptedException e) { } catch (InterruptedException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
AdbUtil.ShellList(shellList); AdbUtil.ShellList(finalIfsu, shellList);
} }
}).start(); }).start();
} }

Loading…
Cancel
Save