diff --git a/snet.apk b/snet.apk deleted file mode 100644 index 3867c4803..000000000 Binary files a/snet.apk and /dev/null differ diff --git a/src/full/java/com/topjohnwu/magisk/MagiskFragment.java b/src/full/java/com/topjohnwu/magisk/MagiskFragment.java index 82e8794b7..9e63c578a 100644 --- a/src/full/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/src/full/java/com/topjohnwu/magisk/MagiskFragment.java @@ -23,6 +23,7 @@ import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.components.AlertDialogBuilder; import com.topjohnwu.magisk.components.ExpandableView; import com.topjohnwu.magisk.components.Fragment; +import com.topjohnwu.magisk.utils.ISafetyNetHelper; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.RootUtils; import com.topjohnwu.magisk.utils.ShowUI; @@ -39,14 +40,6 @@ import butterknife.Unbinder; public class MagiskFragment extends Fragment implements Topic.Subscriber, SwipeRefreshLayout.OnRefreshListener, ExpandableView { - private static final int CAUSE_SERVICE_DISCONNECTED = 0x01; - private static final int CAUSE_NETWORK_LOST = 0x02; - private static final int RESPONSE_ERR = 0x04; - private static final int CONNECTION_FAIL = 0x08; - - private static final int BASIC_PASS = 0x10; - private static final int CTS_PASS = 0x20; - private Container expandableContainer = new Container(); private MagiskManager mm; @@ -285,12 +278,12 @@ public class MagiskFragment extends Fragment safetyNetStatusText.setText(R.string.safetyNet_check_success); boolean b; - b = (response & CTS_PASS) != 0; + b = (response & ISafetyNetHelper.CTS_PASS) != 0; ctsStatusText.setText("ctsProfile: " + b); ctsStatusIcon.setImageResource(b ? R.drawable.ic_check_circle : R.drawable.ic_cancel); ctsStatusIcon.setColorFilter(b ? colorOK : colorBad); - b = (response & BASIC_PASS) != 0; + b = (response & ISafetyNetHelper.BASIC_PASS) != 0; basicStatusText.setText("basicIntegrity: " + b); basicStatusIcon.setImageResource(b ? R.drawable.ic_check_circle : R.drawable.ic_cancel); basicStatusIcon.setColorFilter(b ? colorOK : colorBad); @@ -299,16 +292,16 @@ public class MagiskFragment extends Fragment } else { @StringRes int resid; switch (response) { - case CAUSE_SERVICE_DISCONNECTED: + case ISafetyNetHelper.CAUSE_SERVICE_DISCONNECTED: resid = R.string.safetyNet_network_loss; break; - case CAUSE_NETWORK_LOST: + case ISafetyNetHelper.CAUSE_NETWORK_LOST: resid = R.string.safetyNet_service_disconnected; break; - case RESPONSE_ERR: + case ISafetyNetHelper.RESPONSE_ERR: resid = R.string.safetyNet_res_invalid; break; - case CONNECTION_FAIL: + case ISafetyNetHelper.CONNECTION_FAIL: default: resid = R.string.safetyNet_api_error; break; diff --git a/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java b/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java index 7f64eaa0c..657da4bdd 100644 --- a/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java +++ b/src/full/java/com/topjohnwu/magisk/asyncs/CheckSafetyNet.java @@ -3,6 +3,7 @@ package com.topjohnwu.magisk.asyncs; import android.app.Activity; import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.utils.ISafetyNetHelper; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.superuser.Shell; @@ -15,7 +16,6 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Proxy; import java.net.HttpURLConnection; import dalvik.system.DexClassLoader; @@ -24,32 +24,33 @@ public class CheckSafetyNet extends ParallelTask { public static final File dexPath = new File(MagiskManager.get().getFilesDir().getParent() + "/snet", "snet.apk"); - private DexClassLoader loader; - private Class helperClazz, callbackClazz; + private ISafetyNetHelper helper; public CheckSafetyNet(Activity activity) { super(activity); } - private void dlSnet() throws IOException { + private void dlSnet() throws Exception { Shell.Sync.sh("rm -rf " + dexPath.getParent()); - HttpURLConnection conn = WebService.request(Const.Url.SNET_URL, null); dexPath.getParentFile().mkdir(); + HttpURLConnection conn = WebService.request(Const.Url.SNET_URL, null); try ( OutputStream out = new BufferedOutputStream(new FileOutputStream(dexPath)); InputStream in = new BufferedInputStream(conn.getInputStream())) { ShellUtils.pump(in, out); + } finally { + conn.disconnect(); } - conn.disconnect(); } private void dyload() throws Exception { - loader = new DexClassLoader(dexPath.getPath(), dexPath.getParent(), - null, ClassLoader.getSystemClassLoader()); - helperClazz = loader.loadClass(Const.SNET_PKG + ".SafetyNetHelper"); - callbackClazz = loader.loadClass(Const.SNET_PKG + ".SafetyNetCallback"); - int snet_ver = (int) helperClazz.getMethod("getVersion").invoke(null); - if (snet_ver != Const.SNET_VER) { + DexClassLoader loader = new DexClassLoader(dexPath.getPath(), dexPath.getParent(), + null, ISafetyNetHelper.class.getClassLoader()); + Class clazz = loader.loadClass("com.topjohnwu.snet.SafetyNetHelper"); + helper = (ISafetyNetHelper) clazz.getConstructors()[0] + .newInstance(getActivity(), (ISafetyNetHelper.Callback) + code -> MagiskManager.get().safetyNetDone.publish(false, code)); + if (helper.getVersion() != Const.SNET_VER) { throw new Exception(); } } @@ -72,21 +73,13 @@ public class CheckSafetyNet extends ParallelTask { } @Override - protected void onPostExecute(Exception err) { - MagiskManager mm = MagiskManager.get(); - try { - if (err != null) throw err; - Object helper = helperClazz.getConstructors()[0].newInstance( - getActivity(), dexPath.getPath(), Proxy.newProxyInstance( - loader, new Class[] { callbackClazz }, (proxy, method, args) -> { - mm.safetyNetDone.publish(false, args[0]); - return null; - })); - helperClazz.getMethod("attest").invoke(helper); - } catch (Exception e) { + protected void onPostExecute(Exception e) { + if (e == null) { + helper.attest(); + } else { e.printStackTrace(); - mm.safetyNetDone.publish(false, -1); + MagiskManager.get().safetyNetDone.publish(false, -1); } - super.onPostExecute(err); + super.onPostExecute(e); } } diff --git a/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java b/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java index 91179498f..7b7f307db 100644 --- a/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java +++ b/src/full/java/com/topjohnwu/magisk/components/FlavorActivity.java @@ -17,7 +17,7 @@ public abstract class FlavorActivity extends AppCompatActivity { private AssetManager swappedAssetManager = null; private Resources swappedResources = null; - private Resources.Theme swappedTheme = null; + private Resources.Theme backupTheme = null; @StyleRes public int getDarkTheme() { @@ -25,7 +25,7 @@ public abstract class FlavorActivity extends AppCompatActivity { } public MagiskManager getMagiskManager() { - return (MagiskManager) super.getApplicationContext(); + return (MagiskManager) super.getApplication(); } @Override @@ -48,17 +48,6 @@ public abstract class FlavorActivity extends AppCompatActivity { super.onDestroy(); } - private static AssetManager getAssets(String apk) { - try { - AssetManager asset = AssetManager.class.newInstance(); - AssetManager.class.getMethod("addAssetPath", String.class).invoke(asset, apk); - return asset; - } catch (Exception e) { - e.printStackTrace(); - return null; - } - } - protected void setFloating() { boolean isTablet = getResources().getBoolean(R.bool.isTablet); if (isTablet) { @@ -75,7 +64,7 @@ public abstract class FlavorActivity extends AppCompatActivity { @Override public Resources.Theme getTheme() { - return swappedTheme == null ? super.getTheme() : swappedTheme; + return backupTheme == null ? super.getTheme() : backupTheme; } @Override @@ -83,26 +72,37 @@ public abstract class FlavorActivity extends AppCompatActivity { return swappedAssetManager == null ? super.getAssets() : swappedAssetManager; } + private AssetManager getAssets(String apk) { + try { + AssetManager asset = AssetManager.class.newInstance(); + AssetManager.class.getMethod("addAssetPath", String.class).invoke(asset, apk); + return asset; + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + @Override public Resources getResources() { return swappedResources == null ? super.getResources() : swappedResources; } @Keep - public void swapResources(String dexPath, int resId) { - swappedAssetManager = getAssets(dexPath); - if (swappedAssetManager == null) - return; - Resources res = super.getResources(); - swappedResources = new Resources(swappedAssetManager, res.getDisplayMetrics(), res.getConfiguration()); - swappedTheme = swappedResources.newTheme(); - swappedTheme.applyStyle(resId, true); + public void swapResources(String dexPath) { + AssetManager asset = getAssets(dexPath); + if (asset != null) { + backupTheme = super.getTheme(); + Resources res = super.getResources(); + swappedResources = new Resources(asset, res.getDisplayMetrics(), res.getConfiguration()); + swappedAssetManager = asset; + } } @Keep public void restoreResources() { swappedAssetManager = null; swappedResources = null; - swappedTheme = null; + backupTheme = null; } } diff --git a/src/full/java/com/topjohnwu/magisk/utils/ISafetyNetHelper.java b/src/full/java/com/topjohnwu/magisk/utils/ISafetyNetHelper.java new file mode 100644 index 000000000..e4c329ec4 --- /dev/null +++ b/src/full/java/com/topjohnwu/magisk/utils/ISafetyNetHelper.java @@ -0,0 +1,22 @@ +package com.topjohnwu.magisk.utils; + +import android.support.annotation.Keep; + +public interface ISafetyNetHelper { + + int CAUSE_SERVICE_DISCONNECTED = 0x01; + int CAUSE_NETWORK_LOST = 0x02; + int RESPONSE_ERR = 0x04; + int CONNECTION_FAIL = 0x08; + + int BASIC_PASS = 0x10; + int CTS_PASS = 0x20; + + void attest(); + int getVersion(); + + interface Callback { + @Keep + void onResponse(int responseCode); + } +} diff --git a/src/main/java/com/topjohnwu/magisk/utils/Const.java b/src/main/java/com/topjohnwu/magisk/utils/Const.java index 38c7f08e8..9360ffa85 100644 --- a/src/main/java/com/topjohnwu/magisk/utils/Const.java +++ b/src/main/java/com/topjohnwu/magisk/utils/Const.java @@ -14,7 +14,6 @@ public class Const { public static final String DEBUG_TAG = "MagiskManager"; public static final String ORIG_PKG_NAME = BuildConfig.APPLICATION_ID; - public static final String SNET_PKG = "com.topjohnwu.snet"; public static final String MAGISKHIDE_PROP = "persist.magisk.hide"; // APK content @@ -37,7 +36,7 @@ public class Const { // Versions public static final int UPDATE_SERVICE_VER = 1; - public static final int SNET_VER = 7; + public static final int SNET_VER = 8; public static int MIN_MODULE_VER() { return MagiskManager.get().magiskVersionCode >= MAGISK_VER.REMOVE_LEGACY_LINK ? 1500 : 1400; @@ -81,7 +80,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/a82a5e5a49285df65da91d2e8b24f4783841b515/snet.apk"; + public static final String SNET_URL = "https://github.com/topjohnwu/MagiskManager/raw/788f01f499714471949613820d43bc364786e85d/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";