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 {