|
| 1 | +package com.xayah.dex; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.net.wifi.WifiConfiguration; |
| 5 | +import android.net.wifi.WifiConfigurationHidden; |
| 6 | +import android.net.wifi.WifiManagerHidden; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.nio.file.Files; |
| 10 | +import java.util.HashSet; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +import dev.rikka.tools.refine.Refine; |
| 15 | + |
| 16 | +public class NetworkUtil { |
| 17 | + private static final String NETWORK_PREFIX = "network"; |
| 18 | + private static final String NETWORK_SPLIT_SYMBOL = "_"; |
| 19 | + |
| 20 | + private static void onHelp() { |
| 21 | + System.out.println("NetworkUtil commands:"); |
| 22 | + System.out.println(" help"); |
| 23 | + System.out.println(); |
| 24 | + System.out.println(" getNetworks"); |
| 25 | + System.out.println(); |
| 26 | + System.out.println(" saveNetworks PATH"); |
| 27 | + System.out.println(); |
| 28 | + System.out.println(" restoreNetworks PATH"); |
| 29 | + } |
| 30 | + |
| 31 | + private static void onCommand(String cmd, String[] args) { |
| 32 | + switch (cmd) { |
| 33 | + case "getNetworks": |
| 34 | + getNetworks(args); |
| 35 | + break; |
| 36 | + case "saveNetworks": |
| 37 | + saveNetworks(args); |
| 38 | + break; |
| 39 | + case "restoreNetworks": |
| 40 | + restoreNetworks(args); |
| 41 | + break; |
| 42 | + case "help": |
| 43 | + onHelp(); |
| 44 | + break; |
| 45 | + default: |
| 46 | + System.out.println("Unknown command: " + cmd); |
| 47 | + System.exit(1); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public static void main(String[] args) { |
| 52 | + String cmd; |
| 53 | + if (args != null && args.length > 0) { |
| 54 | + cmd = args[0]; |
| 55 | + onCommand(cmd, args); |
| 56 | + } else { |
| 57 | + onHelp(); |
| 58 | + } |
| 59 | + System.exit(0); |
| 60 | + } |
| 61 | + |
| 62 | + private static void getNetworks(String[] args) { |
| 63 | + try { |
| 64 | + Context ctx = HiddenApiHelper.getContext(); |
| 65 | + WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE)); |
| 66 | + List<WifiConfiguration> networks = wifiManager.getPrivilegedConfiguredNetworks(); |
| 67 | + Set<Integer> networkIds = new HashSet<>(); |
| 68 | + for (int i = 0; i < networks.size(); i++) { |
| 69 | + WifiConfiguration network = networks.get(i); |
| 70 | + int networkId = network.networkId; |
| 71 | + if (!networkIds.contains(networkId)) { |
| 72 | + String ssid = network.SSID; |
| 73 | + String preSharedKey = network.preSharedKey; |
| 74 | + StringBuilder out = new StringBuilder(); |
| 75 | + out.append(networkId).append(" ").append(ssid); |
| 76 | + if (preSharedKey != null) { |
| 77 | + out.append(" ").append(preSharedKey); |
| 78 | + } |
| 79 | + System.out.println(out); |
| 80 | + networkIds.add(networkId); |
| 81 | + } |
| 82 | + } |
| 83 | + System.exit(0); |
| 84 | + } catch (Exception e) { |
| 85 | + e.printStackTrace(System.out); |
| 86 | + System.exit(1); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static void saveNetworks(String[] args) { |
| 91 | + try { |
| 92 | + int status = 0; |
| 93 | + String savePath = args[1]; |
| 94 | + Context ctx = HiddenApiHelper.getContext(); |
| 95 | + WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE)); |
| 96 | + List<WifiConfiguration> networks = wifiManager.getPrivilegedConfiguredNetworks(); |
| 97 | + Set<Integer> networkIds = new HashSet<>(); |
| 98 | + |
| 99 | + File savePathDir = new File(savePath); |
| 100 | + if (!savePathDir.exists()) { |
| 101 | + savePathDir.mkdirs(); |
| 102 | + } |
| 103 | + |
| 104 | + for (int i = 0; i < networks.size(); i++) { |
| 105 | + WifiConfiguration network = networks.get(i); |
| 106 | + int networkId = network.networkId; |
| 107 | + String fileName; |
| 108 | + if (!networkIds.contains(networkId)) { |
| 109 | + fileName = NETWORK_PREFIX + NETWORK_SPLIT_SYMBOL + network.networkId + NETWORK_SPLIT_SYMBOL + "a"; |
| 110 | + networkIds.add(networkId); |
| 111 | + System.out.println(network.SSID + " saved"); |
| 112 | + } else { |
| 113 | + fileName = NETWORK_PREFIX + NETWORK_SPLIT_SYMBOL + network.networkId + NETWORK_SPLIT_SYMBOL + "b"; |
| 114 | + } |
| 115 | + byte[] config = ParcelableHelper.marshall(network); |
| 116 | + File configFile = new File(savePath, fileName); |
| 117 | + configFile.delete(); |
| 118 | + configFile.createNewFile(); |
| 119 | + Files.write(configFile.toPath(), config); |
| 120 | + wifiManager.addNetwork(network); |
| 121 | + } |
| 122 | + System.exit(status); |
| 123 | + } catch (Exception e) { |
| 124 | + e.printStackTrace(System.out); |
| 125 | + System.exit(1); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void restoreNetworks(String[] args) { |
| 130 | + try { |
| 131 | + int status = 0; |
| 132 | + String savePath = args[1]; |
| 133 | + Context ctx = HiddenApiHelper.getContext(); |
| 134 | + WifiManagerHidden wifiManager = Refine.unsafeCast(ctx.getSystemService(Context.WIFI_SERVICE)); |
| 135 | + Set<Integer> networkIds = new HashSet<>(); |
| 136 | + |
| 137 | + File savePathDir = new File(savePath); |
| 138 | + if (!savePathDir.exists()) { |
| 139 | + System.out.println(savePath + " is not exists!"); |
| 140 | + System.exit(1); |
| 141 | + } |
| 142 | + |
| 143 | + File[] networkFiles = savePathDir.listFiles(); |
| 144 | + if (networkFiles != null) { |
| 145 | + for (File networkFile : networkFiles) { |
| 146 | + String fileName = networkFile.getName(); |
| 147 | + String[] fileNameArgs = fileName.split(NETWORK_SPLIT_SYMBOL); |
| 148 | + if (fileNameArgs.length == 3 && fileNameArgs[0].equals(NETWORK_PREFIX)) { |
| 149 | + int networkId = Integer.parseInt(fileNameArgs[1]); |
| 150 | + ParcelableHelper.unmarshall(Files.readAllBytes(networkFile.toPath()), parcel -> { |
| 151 | + WifiConfiguration network = WifiConfigurationHidden.CREATOR.createFromParcel(parcel); |
| 152 | + wifiManager.addNetwork(network); |
| 153 | + if (!networkIds.contains(networkId)) { |
| 154 | + networkIds.add(networkId); |
| 155 | + System.out.println(network.SSID + " restored"); |
| 156 | + } |
| 157 | + }); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + System.exit(status); |
| 163 | + } catch (Exception e) { |
| 164 | + e.printStackTrace(System.out); |
| 165 | + System.exit(1); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments