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

View File

@ -84,7 +84,7 @@ public class Const {
public static class Url {
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 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 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";
@ -159,7 +159,7 @@ public class Const {
public static final String FLASH_MAGISK = "magisk";
public static final int[] timeoutList = {0, -1, 10, 20, 30, 60};
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;
}
}