网络可用时直接设置路由

lowmem
Matthew 1 month ago
parent c1a06203e3
commit 05b9a7a5e5

@ -1149,6 +1149,16 @@ public class MicroPhotoService extends Service {
NetworkInfo ni = mConnectivityManager.getNetworkInfo(network);
LinkProperties lp = mConnectivityManager.getLinkProperties(network);
if (lp != null) {
final String iface = lp.getInterfaceName();
new Thread(new Runnable() {
@Override
public void run() {
setEthernetRoute(iface, "192.168.68.0", 24);
}
}).start();
List<LinkAddress> addresses = lp.getLinkAddresses();
if (addresses != null && addresses.size() > 0) {
for (LinkAddress linkAddress : addresses) {
@ -1656,6 +1666,7 @@ public class MicroPhotoService extends Service {
public void setStaticNetwork(String iface, String ip, String netmask, String gateway)
{
if (!TextUtils.equals("0.0.0.0", ip)) {
if (false) {
Intent intent = new Intent();
@ -1676,11 +1687,12 @@ public class MicroPhotoService extends Service {
boolean success = false;
try {
/*
Process process = Runtime.getRuntime().exec("/system/xbin/su root");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("/system/bin/ifconfig " + iface + " down\n");
os.writeBytes("/system/bin/ifconfig " + iface + " " + ip + " netmask " + netmask + " up\n");
// os.writeBytes("/system/bin/ifconfig " + iface + " down\n");
// os.writeBytes("/system/bin/ifconfig " + iface + " " + ip + " netmask " + netmask + " up\n");
// os.writeBytes("/system/bin/ip route add default via " + gateway + " dev " + iface + "\n");
os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table eth0 2>/dev/null || true\n");
os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table eth0\n");
@ -1695,6 +1707,8 @@ public class MicroPhotoService extends Service {
if (exitValue != 0) {
}
*/
/*
String downCommand = "/system/xbin/su root ifconfig " + iface + " down";
Process downProcess = Runtime.getRuntime().exec(downCommand);
@ -1721,6 +1735,8 @@ 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 eth0 prio 1000";
for (int idx = 0; idx < 10; idx++) {
@ -1747,6 +1763,8 @@ public class MicroPhotoService extends Service {
if (exitValue != 0) {
}
*/
// os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20\n");
// os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20\n");
} catch (Exception e) {
@ -1755,6 +1773,60 @@ public class MicroPhotoService extends Service {
}
}
public void setEthernetRoute(String iface, String ipPrefix, int ipPrefixLength)
{
int exitValue = -1;
try {
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 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 route flush cache\n");
os.writeBytes("exit\n"); // 重要退出su shell
os.flush();
exitValue = process.waitFor();
if (exitValue != 0) {
}
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);
exitValue = routeProcess3.waitFor();
if (exitValue == 0 || exitValue == 2) {
infoLog("Add route successfully");
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);
}
if (exitValue != 0) {
}
// os.writeBytes("/system/bin/ip route delete 192.168.68.0/24 table 20\n");
// os.writeBytes("/system/bin/ip route add 192.168.68.0/24 dev eth0 proto static scope link table 20\n");
} catch (Exception e) {
Log.e(TAG, "Failed to set interface down: " + e.getMessage());
}
}
public int executeCommand(String cmd)
{
int resCode = -1;

Loading…
Cancel
Save