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 {
// for (String cmd : shellList) {
// Process process = Runtime.getRuntime().exec(cmd);
// // 将整个命令包裹在单引号中,作为 -c 后的唯一参数
// String[] cmds = {
// "su",
// "-c",
// "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);
// }
// System.out.println("" + i);
// } catch (Exception e) {
// e.printStackTrace();
// }
try {
Process process = null;
if (shellList != null && shellList.size() > 0) {
if (ifsu) {
// 使用 su 获取 root 权限
Process process = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("su");
} else {
process = Runtime.getRuntime().exec("sh");
}
}
if (process == null) {
return;
}
// 获取输出流,用于发送命令
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
for (int i = 0; i < shellList.size(); i++) {
outputStream.writeBytes(shellList.get(i)+"\n");
outputStream.writeBytes(shellList.get(i) + "\n");
}
// 执行命令(例如:列出 /data 目录)
@ -58,7 +93,6 @@ public class AdbUtil {
while ((line = reader.readLine()) != null) {
Log.d("Output", line); // 打印输出
}
// 等待命令执行完成
int i = process.waitFor();
} catch (IOException | InterruptedException e) {

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

Loading…
Cancel
Save