Add version check within binary

This commit is contained in:
topjohnwu 2017-12-26 03:59:14 +08:00
parent a82a5e5a49
commit b2483ba437
2 changed files with 38 additions and 22 deletions

View File

@ -12,6 +12,7 @@ import java.io.BufferedInputStream;
import java.io.BufferedOutputStream; import java.io.BufferedOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Proxy; import java.lang.reflect.Proxy;
@ -23,50 +24,65 @@ public class CheckSafetyNet extends ParallelTask<Void, Void, Exception> {
private File dexPath; private File dexPath;
private DexClassLoader loader; private DexClassLoader loader;
private Class<?> helperClazz, callbackClazz;
public CheckSafetyNet(Activity activity) { public CheckSafetyNet(Activity activity) {
super(activity); super(activity);
dexPath = new File(activity.getCacheDir().getParent() + "/snet", "snet.apk"); dexPath = new File(activity.getCacheDir().getParent() + "/snet", "snet.apk");
} }
@Override private void dlSnet() throws IOException {
protected void onPreExecute() { Shell.sh("rm -rf " + dexPath.getParent());
MagiskManager mm = MagiskManager.get(); HttpURLConnection conn = WebService.request(Const.Url.SNET_URL, null);
if (mm.snetVersion != Const.Value.SNET_VER) { dexPath.getParentFile().mkdir();
Shell.sh("rm -rf " + dexPath.getParent()); try (
OutputStream out = new BufferedOutputStream(new FileOutputStream(dexPath));
InputStream in = new BufferedInputStream(conn.getInputStream())) {
Utils.inToOut(in, out);
} }
mm.snetVersion = Const.Value.SNET_VER; conn.disconnect();
mm.prefs.edit().putInt(Const.Key.SNET_VER, Const.Value.SNET_VER).apply(); }
private void loadClasses() throws ClassNotFoundException {
loader = new DexClassLoader(dexPath.toString(), dexPath.getParent(),
null, ClassLoader.getSystemClassLoader());
helperClazz = loader.loadClass(Const.SNET_PKG + ".SafetyNetHelper");
callbackClazz = loader.loadClass(Const.SNET_PKG + ".SafetyNetCallback");
} }
@Override @Override
protected Exception doInBackground(Void... voids) { protected Exception doInBackground(Void... voids) {
int snet_ver = -1;
try { try {
if (!dexPath.exists()) { if (!dexPath.exists())
HttpURLConnection conn = WebService.request(Const.Url.SNET_URL, null); dlSnet();
dexPath.getParentFile().mkdir(); loadClasses();
try (
OutputStream out = new BufferedOutputStream(new FileOutputStream(dexPath)); try {
InputStream in = new BufferedInputStream(conn.getInputStream())) { snet_ver = (int) helperClazz.getMethod("getVersion").invoke(null);
Utils.inToOut(in, out); } catch (NoSuchMethodException e) {
} e.printStackTrace();
conn.disconnect(); }
if (snet_ver != Const.Value.SNET_VER) {
dlSnet();
loadClasses();
} }
loader = new DexClassLoader(dexPath.toString(), dexPath.getParent(),
null, ClassLoader.getSystemClassLoader());
} catch (Exception e) { } catch (Exception e) {
return e; return e;
} }
return null; return null;
} }
@Override @Override
protected void onPostExecute(Exception err) { protected void onPostExecute(Exception err) {
MagiskManager mm = MagiskManager.get(); MagiskManager mm = MagiskManager.get();
mm.snetVersion = Const.Value.SNET_VER;
mm.prefs.edit().putInt(Const.Key.SNET_VER, Const.Value.SNET_VER).apply();
try { try {
if (err != null) throw err; if (err != null) throw err;
Class<?> helperClazz = loader.loadClass(Const.SNET_PKG + ".SafetyNetHelper");
Class<?> callbackClazz = loader.loadClass(Const.SNET_PKG + ".SafetyNetCallback");
Object helper = helperClazz.getConstructors()[0].newInstance( Object helper = helperClazz.getConstructors()[0].newInstance(
getActivity(), dexPath.getPath(), Proxy.newProxyInstance( getActivity(), dexPath.getPath(), Proxy.newProxyInstance(
loader, new Class[] { callbackClazz }, (proxy, method, args) -> { loader, new Class[] { callbackClazz }, (proxy, method, args) -> {

View File

@ -84,7 +84,7 @@ public class Const {
public static class Url { public static class Url {
public static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/stable.json"; public static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/stable.json";
public static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/beta.json"; public static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/beta.json";
public static final String SNET_URL = "https://github.com/topjohnwu/MagiskManager/raw/afff3c0a49cec8d797e486be3092e256b4bf5375/snet.apk"; public static final String SNET_URL = "https://github.com/topjohnwu/MagiskManager/raw/a82a5e5a49285df65da91d2e8b24f4783841b515/snet.apk";
public static final String REPO_URL = "https://api.github.com/users/Magisk-Modules-Repo/repos?per_page=100&page=%d"; public static final String REPO_URL = "https://api.github.com/users/Magisk-Modules-Repo/repos?per_page=100&page=%d";
public static final String FILE_URL = "https://raw.githubusercontent.com/Magisk-Modules-Repo/%s/master/%s"; public static final String FILE_URL = "https://raw.githubusercontent.com/Magisk-Modules-Repo/%s/master/%s";
public static final String ZIP_URL = "https://github.com/Magisk-Modules-Repo/%s/archive/master.zip"; public static final String ZIP_URL = "https://github.com/Magisk-Modules-Repo/%s/archive/master.zip";
@ -159,7 +159,7 @@ public class Const {
public static final String FLASH_MAGISK = "magisk"; public static final String FLASH_MAGISK = "magisk";
public static final int[] timeoutList = {0, -1, 10, 20, 30, 60}; public static final int[] timeoutList = {0, -1, 10, 20, 30, 60};
public static final int UPDATE_SERVICE_VER = 1; public static final int UPDATE_SERVICE_VER = 1;
public static final int SNET_VER = 6; public static final int SNET_VER = 7;
public static final int MIN_MODULE_VER = 1400; public static final int MIN_MODULE_VER = 1400;
} }
} }