Skip to content

Commit

Permalink
Inject module-installer.sh if new format is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Mar 24, 2019
1 parent b45db44 commit 4859ee2
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/topjohnwu/magisk/Const.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private static String getRaw(String where, String name) {
public static final String REPO_URL = "https://api.github.com/users/Magisk-Modules-Repo/repos?per_page=100&sort=pushed&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";
public static final String MODULE_INSTALLER = "https://raw.githubusercontent.com/topjohnwu/Magisk/master/scripts/module_installer.sh";
public static final String PAYPAL_URL = "https://www.paypal.me/topjohnwu";
public static final String PATREON_URL = "https://www.patreon.com/topjohnwu";
public static final String TWITTER_URL = "https://twitter.com/topjohnwu";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ private void dlProcessInstall(Repo repo, boolean install) {
InputStream in = Networking.get(repo.getZipUrl())
.setDownloadProgressListener(progress)
.execForInputStream().getResult();
removeTopFolder(in, new BufferedOutputStream(new FileOutputStream(output)));
OutputStream out = new BufferedOutputStream(new FileOutputStream(output));
processZip(in, out, repo.isNewInstaller());
if (install) {
progress.dismiss();
Intent intent = new Intent(this, ClassMap.get(FlashActivity.class));
Expand All @@ -80,17 +81,36 @@ private void dlProcessInstall(Repo repo, boolean install) {
}
}

private void removeTopFolder(InputStream in, OutputStream out) throws IOException {
private void processZip(InputStream in, OutputStream out, boolean inject)
throws IOException {
try (ZipInputStream zin = new ZipInputStream(in);
ZipOutputStream zout = new ZipOutputStream(out)) {
ZipEntry entry;

if (inject) {
// Inject latest module-installer.sh as update-binary
zout.putNextEntry(new ZipEntry("META-INF/"));
zout.putNextEntry(new ZipEntry("META-INF/com/"));
zout.putNextEntry(new ZipEntry("META-INF/com/google/"));
zout.putNextEntry(new ZipEntry("META-INF/com/google/android/"));
zout.putNextEntry(new ZipEntry("META-INF/com/google/android/update-binary"));
try (InputStream update_bin = Networking.get(Const.Url.MODULE_INSTALLER)
.execForInputStream().getResult()) {
ShellUtils.pump(update_bin, zout);
}
zout.putNextEntry(new ZipEntry("META-INF/com/google/android/updater-script"));
zout.write("#MAGISK\n".getBytes("UTF-8"));
}

int off = -1;
ZipEntry entry;
while ((entry = zin.getNextEntry()) != null) {
if (off < 0)
off = entry.getName().indexOf('/') + 1;
String path = entry.getName().substring(off);
if (path.isEmpty())
continue;
if (inject && path.startsWith("META-INF"))
continue;
zout.putNextEntry(new ZipEntry(path));
if (!entry.isDirectory())
ShellUtils.pump(zin, zout);
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/com/topjohnwu/magisk/container/Repo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.net.Networking;
import com.topjohnwu.net.Request;

import java.text.DateFormat;
import java.util.Date;
Expand Down Expand Up @@ -82,11 +84,21 @@ public String getZipUrl() {
}

public String getPropUrl() {
return String.format(Const.Url.FILE_URL, getId(), "module.prop");
return getFileUrl("module.prop");
}

public String getDetailUrl() {
return String.format(Const.Url.FILE_URL, getId(), "README.md");
return getFileUrl("README.md");
}

public String getFileUrl(String file) {
return String.format(Const.Url.FILE_URL, getId(), file);
}

public boolean isNewInstaller() {
try (Request req = Networking.get(getFileUrl("install.sh"))) {
return req.connect().isSuccess();
}
}

public String getLastUpdateString() {
Expand Down
56 changes: 51 additions & 5 deletions net/src/main/java/com/topjohnwu/net/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilterInputStream;
Expand All @@ -19,7 +21,7 @@
import java.net.HttpURLConnection;
import java.util.concurrent.Executor;

public class Request {
public class Request implements Closeable {
private HttpURLConnection conn;
private Executor executor = null;
private DownloadProgressListener progress = null;
Expand All @@ -42,6 +44,10 @@ public int getCode() {
return code;
}

public boolean isSuccess() {
return code >= 200 && code <= 299;
}

public HttpURLConnection getConnection() {
return conn;
}
Expand All @@ -51,6 +57,11 @@ public HttpURLConnection getConnection() {
conn = c;
}

@Override
public void close() {
conn.disconnect();
}

public Request addHeaders(String key, String value) {
conn.setRequestProperty(key, value);
return this;
Expand All @@ -71,6 +82,16 @@ public Request setExecutor(Executor e) {
return this;
}

public Result<Void> connect() {
try {
connect0();
} catch (IOException e) {
if (err != null)
err.onError(conn, e);
}
return new Result<>();
}

public Result<InputStream> execForInputStream() {
return exec(this::getInputStream);
}
Expand All @@ -83,6 +104,14 @@ public void execForFile(File out) {
exec(() -> dlFile(out));
}

public void getAsBytes(ResponseListener<byte[]> rs) {
submit(this::dlBytes, rs);
}

public Result<byte[]> execForBytes() {
return exec(this::dlBytes);
}

public void getAsString(ResponseListener<String> rs) {
submit(this::dlString, rs);
}
Expand All @@ -107,6 +136,11 @@ public Result<JSONArray> execForJSONArray() {
return exec(this::dlJSONArray);
}

private void connect0() throws IOException {
conn.connect();
code = conn.getResponseCode();
}

private <T> Result<T> exec(Requestor<T> req) {
Result<T> res = new Result<>();
try {
Expand Down Expand Up @@ -135,8 +169,7 @@ private <T> void submit(Requestor<T> req, ResponseListener<T> rs) {
}

private BufferedInputStream getInputStream() throws IOException {
conn.connect();
code = conn.getResponseCode();
connect0();
InputStream in = conn.getInputStream();
if (progress != null) {
in = new ProgressInputStream(in, conn.getContentLength(), progress) {
Expand All @@ -162,7 +195,7 @@ private String dlString() throws IOException {
StringBuilder builder = new StringBuilder();
try (Reader reader = new InputStreamReader(getInputStream())) {
int len;
char buf[] = new char[4096];
char[] buf = new char[4096];
while ((len = reader.read(buf)) != -1) {
builder.append(buf, 0, len);
}
Expand All @@ -182,11 +215,24 @@ private File dlFile(File f) throws IOException {
try (InputStream in = getInputStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream(f))) {
int len;
byte buf[] = new byte[4096];
byte[] buf = new byte[4096];
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
return f;
}

private byte[] dlBytes() throws IOException {
int len = conn.getContentLength();
len = len > 0 ? len : 32;
ByteArrayOutputStream out = new ByteArrayOutputStream(len);
try (InputStream in = getInputStream()) {
byte[] buf = new byte[4096];
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
}
return out.toByteArray();
}
}

0 comments on commit 4859ee2

Please sign in to comment.