From b916e63a00ccbda396e90ede1b500eeabf0bad80 Mon Sep 17 00:00:00 2001 From: Matthew Date: Sun, 11 May 2025 11:53:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=91=BD=E4=BB=A4=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E4=BB=A5=E5=8F=8A=E4=BC=98=E5=8C=96=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E8=A7=84=E5=88=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpapp/MicroPhotoService.java | 43 ++++++++++++------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 832b70ee..7c0c26e4 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1781,12 +1781,12 @@ public class MicroPhotoService extends Service { Process process = Runtime.getRuntime().exec("/system/xbin/su root"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); - os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20 2>/dev/null || true\n"); - os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20\n"); + // os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20 2>/dev/null || true\n"); + os.writeBytes("/system/bin/ip route replace 192.168.68.0/24 dev eth0 proto static scope link table 20\n"); os.writeBytes("/system/bin/ip route flush cache\n"); // os.writeBytes("echo 'nameserver 8.8.8.8' > /etc/resolv.conf\n"); os.writeBytes("/system/bin/ip rule del to 192.168.68.0/24 2>/dev/null || true\n"); - os.writeBytes("/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000"); + os.writeBytes("/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000\n"); os.writeBytes("/system/bin/ip route flush cache\n"); os.writeBytes("exit\n"); // 重要:退出su shell os.flush(); @@ -1795,24 +1795,35 @@ public class MicroPhotoService extends Service { } sleep(100); - String routeCommand = "/system/xbin/su root /system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000"; for (int idx = 0; idx < 10; idx++) { - Process routeProcess3 = Runtime.getRuntime().exec(routeCommand); + Process routeProcess3 = Runtime.getRuntime().exec("/system/xbin/su root"); + DataOutputStream os3 = new DataOutputStream(routeProcess3.getOutputStream()); + // os3.writeBytes("/system/bin/ip rule del to 192.168.68.0/24 2>/dev/null || true\n"); + os3.writeBytes("/system/bin/ip rule add from all to 192.168.68.0/24 lookup 20 prio 1000\n"); + os3.writeBytes("CMD_EXIT_CODE=$?\n"); // 保存返回值 + os3.writeBytes("echo \"CMD_RESULT:$CMD_EXIT_CODE\"\n"); // 输出标记和返回值 + os3.writeBytes("/system/bin/ip route flush cache\n"); + os3.writeBytes("exit\n"); // 重要:退出su shell + os3.flush(); + + int commandExitCode = -1; + BufferedReader reader = new BufferedReader(new InputStreamReader(routeProcess3.getErrorStream())); + String line; + StringBuilder error = new StringBuilder(); + while ((line = reader.readLine()) != null) { + if (line.startsWith("CMD_RESULT:")) { + commandExitCode = Integer.parseInt(line.substring(11)); + Log.d("RouteConfig", "Command exit code: " + commandExitCode); + break; + } + } + exitValue = routeProcess3.waitFor(); - if (exitValue == 0 || exitValue == 2) { - infoLog("Add route successfully"); + if (exitValue == 0 || commandExitCode == 2) { + infoLog("Add route successfully Code=" + exitValue); break; } else { - BufferedReader reader = new BufferedReader(new InputStreamReader(routeProcess3.getErrorStream())); - String line; - StringBuilder error = new StringBuilder(); - while ((line = reader.readLine()) != null) { - error.append(line).append("\n"); - } - if (error.length() > 0) { - Log.e("RouteConfig", "Error output: " + error); - } } sleep(500); }