From 6bc8849d3c39fd64c982a283b87897f74cea5ab1 Mon Sep 17 00:00:00 2001 From: Matthew Date: Mon, 12 May 2025 18:27:28 +0800 Subject: [PATCH] =?UTF-8?q?N938=E9=9C=80=E8=A6=81=E8=AE=BE=E7=BD=AEip?= =?UTF-8?q?=E5=92=8C=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xypower/mpapp/MicroPhotoService.java | 75 ++++++++++++++++++- 1 file changed, 71 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java index 7c0c26e4..d4d69dcb 100644 --- a/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java +++ b/app/src/main/java/com/xypower/mpapp/MicroPhotoService.java @@ -1154,11 +1154,14 @@ public class MicroPhotoService extends Service { new Thread(new Runnable() { @Override public void run() { - setEthernetRoute(iface, "192.168.68.0", 24); + if (getCustomAppId() == 2) { + setStaticNetwork(iface, "192.168.68.91", "192.168.68.91", "192.168.68.0", 24); + } else { + setEthernetRoute(iface, "192.168.68.0", 24); + } } }).start(); - List addresses = lp.getLinkAddresses(); if (addresses != null && addresses.size() > 0) { for (LinkAddress linkAddress : addresses) { @@ -1773,8 +1776,72 @@ public class MicroPhotoService extends Service { } } - public void setEthernetRoute(String iface, String ipPrefix, int ipPrefixLength) - { + public void setStaticNetwork(String iface, String ip, String gateway, String ipPrefix, int ipPrefixLength) { + int exitValue = -1; + boolean success = false; + try { + + Process process = Runtime.getRuntime().exec("/system/xbin/su root"); + DataOutputStream os = new DataOutputStream(process.getOutputStream()); + + os.writeBytes("/system/bin/ip link set eth0 down"); + os.writeBytes("/system/bin/ip addr add 192.168.68.91/24 dev eth0"); + os.writeBytes("/system/bin/ip link set eth0 up"); + // 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\n"); + os.writeBytes("/system/bin/ip route flush cache\n"); + os.writeBytes("exit\n"); // 重要:退出su shell + os.flush(); + exitValue = process.waitFor(); + if (exitValue != 0) { + } + + sleep(100); + for (int idx = 0; idx < 10; idx++) { + 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 || commandExitCode == 2) { + infoLog("Add route successfully Code=" + exitValue); + break; + } else { + + } + sleep(500); + } + + if (exitValue != 0) { + } + } catch (Exception e) { + Log.e(TAG, "Failed to set interface down: " + e.getMessage()); + } + } + + public void setEthernetRoute(String iface, String ipPrefix, int ipPrefixLength) { int exitValue = -1; try {