Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit c65f71f

Browse files
committed
Rewrite module install script, auto detect if a patch is needed on remote modules.
1 parent dfa9009 commit c65f71f

File tree

9 files changed

+41
-31
lines changed

9 files changed

+41
-31
lines changed

app/src/main/java/com/fox2code/mmm/ActionButtonType.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ public void doAction(ImageButton button, ModuleHolder moduleHolder) {
8585
// Re-render each time in cse of config changes
8686
desc = markwon.render(markwon.parse(localModuleInfo.updateChangeLog));
8787
}
88-
noPatch = true;
89-
} else {
90-
noPatch = false;
9188
}
9289

9390
if (desc == null || desc.length() == 0) {
@@ -103,7 +100,7 @@ public void doAction(ImageButton button, ModuleHolder moduleHolder) {
103100
R.string.update_module : R.string.install_module, (x, y) -> {
104101
String updateZipChecksum = moduleHolder.getUpdateZipChecksum();
105102
IntentHelper.openInstaller(button.getContext(), updateZipUrl,
106-
moduleInfo.name, moduleInfo.config, updateZipChecksum, noPatch);
103+
moduleInfo.name, moduleInfo.config, updateZipChecksum);
107104
});
108105
}
109106
int dim5dp = CompatDisplay.dpToPixel(5);

app/src/main/java/com/fox2code/mmm/Constants.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class Constants {
1414
public static final String EXTRA_INSTALL_NAME = "extra_install_name";
1515
public static final String EXTRA_INSTALL_CONFIG = "extra_install_config";
1616
public static final String EXTRA_INSTALL_CHECKSUM = "extra_install_checksum";
17-
public static final String EXTRA_INSTALL_NO_PATCH = "extra_install_no_patch";
1817
public static final String EXTRA_INSTALL_NO_EXTENSIONS = "extra_install_no_extensions";
1918
public static final String EXTRA_INSTALL_TEST_ROOTLESS = "extra_install_test_rootless";
2019
public static final String EXTRA_ANDROIDACY_ALLOW_INSTALL = "extra_androidacy_allow_install";

app/src/main/java/com/fox2code/mmm/MainActivity.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ else if (AppUpdateManager.getAppUpdateManager().checkUpdate(true))
262262
if (!NotificationType.NO_INTERNET.shouldRemove()) {
263263
this.moduleViewListBuilder.addNotification(NotificationType.NO_INTERNET);
264264
}
265+
RepoManager.getINSTANCE().updateEnabledStates();
265266
this.moduleViewListBuilder.appendRemoteModules();
266267
this.moduleViewListBuilder.applyTo(moduleList, moduleViewAdapter);
267268
},"Repo update thread").start();

app/src/main/java/com/fox2code/mmm/NotificationType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public boolean shouldRemove() {
8989
} else {
9090
IntentHelper.openInstaller(compatActivity, d.getAbsolutePath(),
9191
compatActivity.getString(
92-
R.string.local_install_title), null, null, false,
92+
R.string.local_install_title), null, null,
9393
BuildConfig.DEBUG && // Use debug mode if no root
9494
InstallerInitializer.peekMagiskPath() == null);
9595
}
@@ -102,7 +102,7 @@ public boolean shouldRemove() {
102102
} else if (s == IntentHelper.RESPONSE_URL) {
103103
IntentHelper.openInstaller(compatActivity, u.toString(),
104104
compatActivity.getString(
105-
R.string.remote_install_title), null, null, false,
105+
R.string.remote_install_title), null, null,
106106
BuildConfig.DEBUG && // Use debug mode if no root
107107
InstallerInitializer.peekMagiskPath() == null);
108108
}

app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyWebAPI.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ public void install(String moduleUrl, String installTitle,String checksum) {
8888
Uri uri = Uri.parse(moduleUrl);
8989
if (uri.getScheme().equals("https") && uri.getHost().endsWith(".androidacy.com")) {
9090
this.activity.backOnResume = true;
91-
IntentHelper.openInstaller(
92-
this.activity, moduleUrl, installTitle,
93-
null, checksum, true);
91+
IntentHelper.openInstaller(this.activity,
92+
moduleUrl, installTitle, null, checksum);
9493
} else {
9594
this.activity.forceBackPressed();
9695
}

app/src/main/java/com/fox2code/mmm/installer/InstallerActivity.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import com.topjohnwu.superuser.internal.UiThreadHandler;
3232
import com.topjohnwu.superuser.io.SuFile;
3333

34+
import java.io.ByteArrayInputStream;
3435
import java.io.File;
3536
import java.io.FileOutputStream;
3637
import java.io.IOException;
3738
import java.io.OutputStream;
39+
import java.util.zip.ZipEntry;
40+
import java.util.zip.ZipInputStream;
3841

3942
public class InstallerActivity extends CompatActivity {
4043
private static final String TAG = "InstallerActivity";
@@ -59,7 +62,6 @@ protected void onCreate(Bundle savedInstanceState) {
5962
final String target;
6063
final String name;
6164
final String checksum;
62-
final boolean noPatch;
6365
final boolean noExtensions;
6466
final boolean rootless;
6567
// Should we allow 3rd part app to install modules?
@@ -72,7 +74,6 @@ protected void onCreate(Bundle savedInstanceState) {
7274
target = intent.getStringExtra(Constants.EXTRA_INSTALL_PATH);
7375
name = intent.getStringExtra(Constants.EXTRA_INSTALL_NAME);
7476
checksum = intent.getStringExtra(Constants.EXTRA_INSTALL_CHECKSUM);
75-
noPatch = intent.getBooleanExtra(Constants.EXTRA_INSTALL_NO_PATCH, false);
7677
noExtensions = intent.getBooleanExtra(// Allow intent to disable extensions
7778
Constants.EXTRA_INSTALL_NO_EXTENSIONS, false);
7879
rootless = intent.getBooleanExtra(// For debug only
@@ -140,7 +141,31 @@ protected void onCreate(Bundle savedInstanceState) {
140141
}
141142
}
142143
if (this.canceled) return;
144+
Files.fixJavaZipHax(rawModule);
145+
boolean noPatch = false;
146+
boolean isModule = false;
147+
errMessage = "File is not a valid zip file";
148+
try (ZipInputStream zipInputStream = new ZipInputStream(
149+
new ByteArrayInputStream(rawModule))) {
150+
ZipEntry zipEntry;
151+
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
152+
String entryName = zipEntry.getName();
153+
if (entryName.equals("module.prop")) {
154+
noPatch = true;
155+
isModule = true;
156+
break;
157+
} else if (entryName.endsWith("/module.prop")) {
158+
isModule = true;
159+
}
160+
}
161+
}
162+
if (!isModule) {
163+
this.setInstallStateFinished(false,
164+
"! File is not a valid magisk module", "");
165+
return;
166+
}
143167
if (noPatch) {
168+
errMessage = "Failed to save module zip";
144169
try (OutputStream outputStream = new FileOutputStream(moduleCache)) {
145170
outputStream.write(rawModule);
146171
outputStream.flush();

app/src/main/java/com/fox2code/mmm/repo/RepoManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ private RepoManager(MainApplication mainApplication) {
6969
this.modules = new HashMap<>();
7070
// We do not have repo list config yet.
7171
this.addRepoData(MAGISK_ALT_REPO);
72-
this.addAndroidacyRepoData();
7372
this.addRepoData(MAGISK_REPO);
73+
this.addAndroidacyRepoData();
7474
// Populate default cache
7575
for (RepoData repoData:this.repoData.values()) {
7676
for (RepoModule repoModule:repoData.moduleHashMap.values()) {

app/src/main/java/com/fox2code/mmm/utils/Files.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,13 @@ public static byte[] readAllBytes(InputStream inputStream) throws IOException {
6464
return buffer.toByteArray();
6565
}
6666

67-
68-
public static byte[] patchModuleSimple(byte[] bytes) throws IOException {
69-
ByteArrayOutputStream byteArrayOutputStream =
70-
new ByteArrayOutputStream((int) (bytes.length * 1.2F));
71-
patchModuleSimple(bytes, byteArrayOutputStream);
72-
return byteArrayOutputStream.toByteArray();
67+
public static void fixJavaZipHax(byte[] bytes) {
68+
if (bytes.length > 8 && bytes[0x6] == 0x0 && bytes[0x7] == 0x0 && bytes[0x8] == 0x8)
69+
bytes[0x7] = 0x8; // Known hax to prevent java zip file read
7370
}
7471

7572
public static void patchModuleSimple(byte[] bytes,OutputStream outputStream) throws IOException {
76-
if (bytes[0x6] == 0x0 && bytes[0x7] == 0x0 && bytes[0x8] == 0x8) bytes[0x7] = 0x8;
77-
patchModuleSimple(new ByteArrayInputStream(bytes), outputStream);
73+
fixJavaZipHax(bytes); patchModuleSimple(new ByteArrayInputStream(bytes), outputStream);
7874
}
7975

8076
public static void patchModuleSimple(InputStream inputStream,OutputStream outputStream) throws IOException {

app/src/main/java/com/fox2code/mmm/utils/IntentHelper.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,11 @@ public static void openMarkdown(Context context, String url, String title, Strin
122122

123123
public static void openInstaller(Context context, String url, String title,
124124
String config, String checksum) {
125-
openInstaller(context, url, title, config, checksum, false, false);
125+
openInstaller(context, url, title, config, checksum, false);
126126
}
127127

128128
public static void openInstaller(Context context, String url, String title, String config,
129-
String checksum, boolean noPatch) {
130-
openInstaller(context, url, title, config, checksum, noPatch, false);
131-
}
132-
133-
public static void openInstaller(Context context, String url, String title, String config,
134-
String checksum, boolean noPatch,boolean testDebug) {
129+
String checksum,boolean testDebug) {
135130
try {
136131
Intent intent = new Intent(context, InstallerActivity.class);
137132
intent.setAction(Constants.INTENT_INSTALL_INTERNAL);
@@ -142,8 +137,6 @@ public static void openInstaller(Context context, String url, String title, Stri
142137
intent.putExtra(Constants.EXTRA_INSTALL_CONFIG, config);
143138
if (checksum != null && !checksum.isEmpty())
144139
intent.putExtra(Constants.EXTRA_INSTALL_CHECKSUM, checksum);
145-
if (noPatch)
146-
intent.putExtra(Constants.EXTRA_INSTALL_NO_PATCH, true);
147140
if (testDebug && BuildConfig.DEBUG)
148141
intent.putExtra(Constants.EXTRA_INSTALL_TEST_ROOTLESS, true);
149142
startActivity(context, intent, true);
@@ -234,7 +227,7 @@ public static void openFileTo(CompatActivity compatActivity, File destination,
234227
callback.onReceived(destination, null, RESPONSE_ERROR);
235228
return;
236229
}
237-
Log.d("IntentHelper", "FilePicker returned " + uri.toString());
230+
Log.d("IntentHelper", "FilePicker returned " + uri);
238231
if ("http".equals(uri.getScheme()) ||
239232
"https".equals(uri.getScheme())) {
240233
callback.onReceived(destination, uri, RESPONSE_URL);

0 commit comments

Comments
 (0)