Separate stub Magisk Manager to a module

This commit is contained in:
topjohnwu
2019-03-08 10:16:02 -05:00
parent 745865ee53
commit cf65169c99
292 changed files with 7132 additions and 6642 deletions

View File

@@ -1,25 +0,0 @@
package com.topjohnwu.magisk.utils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import java.io.File;
import androidx.core.content.FileProvider;
public class APKInstall {
public static void install(Context c, File apk) {
Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
install.setData(FileProvider.getUriForFile(c, c.getPackageName() + ".provider", apk));
} else {
apk.setReadable(true, false);
install.setData(Uri.fromFile(apk));
}
c.startActivity(install);
}
}

View File

@@ -0,0 +1,53 @@
package com.topjohnwu.magisk.utils;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;
import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkManager;
import com.topjohnwu.magisk.ClassMap;
import com.topjohnwu.magisk.Config;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.components.UpdateCheckService;
import com.topjohnwu.magisk.tasks.CheckUpdates;
import java.util.concurrent.TimeUnit;
public class AppUtils {
public static void scheduleUpdateCheck() {
if (Config.get(Config.Key.CHECK_UPDATES)) {
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequest request = new PeriodicWorkRequest
.Builder(ClassMap.get(UpdateCheckService.class), 12, TimeUnit.HOURS)
.setConstraints(constraints)
.build();
WorkManager.getInstance().enqueueUniquePeriodicWork(
Const.ID.CHECK_MAGISK_UPDATE_WORKER_ID,
ExistingPeriodicWorkPolicy.REPLACE, request);
} else {
WorkManager.getInstance().cancelUniqueWork(Const.ID.CHECK_MAGISK_UPDATE_WORKER_ID);
CheckUpdates.check();
}
}
public static void openLink(Context context, Uri link) {
Intent intent = new Intent(Intent.ACTION_VIEW, link);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
} else {
Utils.toast(R.string.open_link_failed_toast, Toast.LENGTH_SHORT);
}
}
}

View File

@@ -0,0 +1,92 @@
package com.topjohnwu.magisk.utils;
import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Config;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.uicomponents.ProgressNotification;
import com.topjohnwu.net.Networking;
import com.topjohnwu.net.ResponseListener;
import com.topjohnwu.superuser.Shell;
import java.io.File;
import dalvik.system.DexClassLoader;
public class DownloadApp {
public static void upgrade(String name) {
dlInstall(name, new PatchPackageName());
}
public static void restore() {
String name = Utils.fmt("MagiskManager v%s(%d)",
Config.remoteManagerVersionString, Config.remoteManagerVersionCode);
dlInstall(name, new RestoreManager());
}
private static void dlInstall(String name, ManagerDownloadListener listener) {
File apk = new File(App.self.getCacheDir(), "manager.apk");
ProgressNotification progress = new ProgressNotification(name);
listener.progress = progress;
Networking.get(Config.managerLink)
.setExecutor(App.THREAD_POOL)
.setDownloadProgressListener(progress)
.setErrorHandler((conn, e) -> progress.dlFail())
.getAsFile(apk, listener);
}
private abstract static class ManagerDownloadListener implements ResponseListener<File> {
ProgressNotification progress;
}
private static class PatchPackageName extends ManagerDownloadListener {
@Override
public void onResponse(File apk) {
File patched = apk;
App app = App.self;
if (!app.getPackageName().equals(BuildConfig.APPLICATION_ID)) {
progress.getNotificationBuilder()
.setProgress(0, 0, true)
.setContentTitle(app.getString(R.string.hide_manager_title))
.setContentText("");
progress.update();
patched = new File(apk.getParent(), "patched.apk");
try {
// Try using the new APK to patch itself
ClassLoader loader = new DexClassLoader(apk.getPath(),
apk.getParent(), null, ClassLoader.getSystemClassLoader());
loader.loadClass("a.a")
.getMethod("patchAPK", String.class, String.class, String.class)
.invoke(null, apk.getPath(), patched.getPath(), app.getPackageName());
} catch (Exception e) {
e.printStackTrace();
// Fallback to use the current implementation
PatchAPK.patch(apk.getPath(), patched.getPath(), app.getPackageName());
}
}
APKInstall.install(app, patched);
progress.dismiss();
}
}
private static class RestoreManager extends ManagerDownloadListener {
@Override
public void onResponse(File apk) {
App app = App.self;
progress.getNotificationBuilder()
.setProgress(0, 0, true)
.setContentTitle(app.getString(R.string.restore_img_msg))
.setContentText("");
progress.update();
Config.export();
// Make it world readable
apk.setReadable(true, false);
if (Shell.su("pm install " + apk).exec().isSuccess())
RootUtils.rmAndLaunch(app.getPackageName(), BuildConfig.APPLICATION_ID);
progress.dismiss();
}
}
}

View File

@@ -0,0 +1,148 @@
package com.topjohnwu.magisk.utils;
import android.widget.Toast;
import androidx.core.app.NotificationCompat;
import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.Config;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.uicomponents.Notifications;
import com.topjohnwu.signing.JarMap;
import com.topjohnwu.signing.SignAPK;
import com.topjohnwu.superuser.Shell;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.IntBuffer;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
public class PatchAPK {
public static final String LOWERALPHA = "abcdefghijklmnopqrstuvwxyz";
public static final String UPPERALPHA = LOWERALPHA.toUpperCase();
public static final String ALPHA = LOWERALPHA + UPPERALPHA;
public static final String DIGITS = "0123456789";
public static final String ALPHANUM = ALPHA + DIGITS;
public static final String ALPHANUMDOTS = ALPHANUM + "............";
private static String genPackageName(String prefix, int length) {
StringBuilder builder = new StringBuilder(length);
builder.append(prefix);
length -= prefix.length();
SecureRandom random = new SecureRandom();
char next, prev = prefix.charAt(prefix.length() - 1);
for (int i = 0; i < length; ++i) {
if (prev == '.' || i == length - 1) {
next = ALPHA.charAt(random.nextInt(ALPHA.length()));
} else {
next = ALPHANUMDOTS.charAt(random.nextInt(ALPHANUMDOTS.length()));
}
builder.append(next);
prev = next;
}
return builder.toString();
}
private static boolean findAndPatch(byte xml[], String from, String to) {
if (from.length() != to.length())
return false;
CharBuffer buf = ByteBuffer.wrap(xml).order(ByteOrder.LITTLE_ENDIAN).asCharBuffer();
List<Integer> offList = new ArrayList<>();
for (int i = 0; i < buf.length() - from.length(); ++i) {
boolean match = true;
for (int j = 0; j < from.length(); ++j) {
if (buf.get(i + j) != from.charAt(j)) {
match = false;
break;
}
}
if (match) {
offList.add(i);
i += from.length();
}
}
if (offList.isEmpty())
return false;
for (int off : offList) {
buf.position(off);
buf.put(to);
}
return true;
}
private static boolean findAndPatch(byte xml[], int a, int b) {
IntBuffer buf = ByteBuffer.wrap(xml).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
int len = xml.length / 4;
for (int i = 0; i < len; ++i) {
if (buf.get(i) == a) {
buf.put(i, b);
return true;
}
}
return false;
}
private static boolean patchAndHide() {
App app = App.self;
// Generate a new app with random package name
File repack = new File(app.getFilesDir(), "patched.apk");
String pkg = genPackageName("com.", BuildConfig.APPLICATION_ID.length());
if (!patch(app.getPackageCodePath(), repack.getPath(), pkg))
return false;
// Install the application
repack.setReadable(true, false);
if (!Shell.su("pm install " + repack).exec().isSuccess())
return false;
Config.set(Config.Key.SU_MANAGER, pkg);
Config.export();
RootUtils.rmAndLaunch(BuildConfig.APPLICATION_ID, pkg);
return true;
}
public static boolean patch(String in, String out, String pkg) {
try {
JarMap jar = new JarMap(in);
JarEntry je = jar.getJarEntry(Const.ANDROID_MANIFEST);
byte xml[] = jar.getRawData(je);
if (!findAndPatch(xml, BuildConfig.APPLICATION_ID, pkg) ||
!findAndPatch(xml, R.string.app_name, R.string.re_app_name))
return false;
// Write in changes
jar.getOutputStream(je).write(xml);
SignAPK.sign(jar, new BufferedOutputStream(new FileOutputStream(out)));
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public static void hideManager() {
App.THREAD_POOL.execute(() -> {
App app = App.self;
NotificationCompat.Builder progress =
Notifications.progress(app.getString(R.string.hide_manager_title));
Notifications.mgr.notify(Const.ID.HIDE_MANAGER_NOTIFICATION_ID, progress.build());
if(!patchAndHide())
Utils.toast(R.string.hide_manager_fail_toast, Toast.LENGTH_LONG);
Notifications.mgr.cancel(Const.ID.HIDE_MANAGER_NOTIFICATION_ID);
});
}
}