From 456268c682884773be8a89eeaec8a7e570c1051a Mon Sep 17 00:00:00 2001 From: liuguijing <1440265357@qq.com> Date: Mon, 12 May 2025 10:36:12 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=88=87=E6=8D=A2=E5=88=B0sh?= =?UTF-8?q?=EF=BC=8C=E6=94=B6=E5=88=B0su=E7=9A=84=E6=97=B6=E5=80=99?= =?UTF-8?q?=E5=88=87=E6=8D=A2=E5=88=B0su?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpmaster/sms/AdbUtil.java | 60 +++++++++++++++---- .../com/xypower/mpmaster/sms/SimUtil.java | 11 +++- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java index efcaf708..1516f20d 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/AdbUtil.java @@ -23,28 +23,63 @@ public class AdbUtil { } } - public static void ShellList(ArrayList shellList) { + public static void ShellList(boolean ifsu, ArrayList shellList) { // try { -// for (String cmd : shellList) { -// Process process = Runtime.getRuntime().exec(cmd); -// // 处理输出和错误流 -// int i = process.waitFor(); -// System.out.println(""+i); -// } +// // 将整个命令包裹在单引号中,作为 -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); // } catch (Exception e) { // e.printStackTrace(); // } - - try { - // 使用 su 获取 root 权限 - Process process = Runtime.getRuntime().exec("su"); + Process process = null; + 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()); 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) { diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java index 849c581a..2fd4c6b2 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/sms/SimUtil.java @@ -564,6 +564,7 @@ public class SimUtil { sendmessage = getSendString(content, ifmessageCorrect); } else if (content.contains(SmsTypeEnum.SHELL.value())) { ifmessageCorrect = true; + boolean ifsu = false; ArrayList 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++) { - 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() { @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(); }