Migrate to new endpoints

This commit is contained in:
topjohnwu 2021-03-05 05:09:25 -08:00
parent 851404205b
commit 15e27e54fb
7 changed files with 44 additions and 84 deletions

View File

@ -30,8 +30,8 @@ object Const {
// Versions // Versions
const val SNET_EXT_VER = 15 const val SNET_EXT_VER = 15
const val SNET_REVISION = "18ab78817087c337ae0edd1ecac38aec49217880" const val SNET_REVISION = "22.0"
const val BOOTCTL_REVISION = "18ab78817087c337ae0edd1ecac38aec49217880" const val BOOTCTL_REVISION = "22.0"
// Misc // Misc
val USER_ID = Process.myUid() / 100000 val USER_ID = Process.myUid() / 100000
@ -62,7 +62,7 @@ object Const {
const val GITHUB_RAW_URL = "https://raw.githubusercontent.com/" const val GITHUB_RAW_URL = "https://raw.githubusercontent.com/"
const val GITHUB_API_URL = "https://api.github.com/" const val GITHUB_API_URL = "https://api.github.com/"
const val GITHUB_PAGE_URL = "https://topjohnwu.github.io/magisk_files/" const val GITHUB_PAGE_URL = "https://topjohnwu.github.io/magisk-files/"
const val JS_DELIVR_URL = "https://cdn.jsdelivr.net/gh/" const val JS_DELIVR_URL = "https://cdn.jsdelivr.net/gh/"
const val OFFICIAL_REPO = "https://magisk-modules-repo.github.io/submission/modules.json" const val OFFICIAL_REPO = "https://magisk-modules-repo.github.io/submission/modules.json"
} }

View File

@ -16,8 +16,7 @@ data class MagiskJson(
val version: String = "", val version: String = "",
val versionCode: Int = -1, val versionCode: Int = -1,
val link: String = "", val link: String = "",
val note: String = "", val note: String = ""
val md5: String = ""
) : Parcelable ) : Parcelable
@Parcelize @Parcelize

View File

@ -10,17 +10,15 @@ import retrofit2.http.*
private const val REVISION = "revision" private const val REVISION = "revision"
private const val BRANCH = "branch" private const val BRANCH = "branch"
private const val REPO = "repo" private const val REPO = "repo"
private const val FILE = "file"
const val MAGISK_FILES = "topjohnwu/magisk_files" const val MAGISK_FILES = "topjohnwu/magisk-files"
const val MAGISK_MAIN = "topjohnwu/Magisk" const val MAGISK_MAIN = "topjohnwu/Magisk"
interface GithubPageServices { interface GithubPageServices {
@GET("stable.json") @GET("{$FILE}")
suspend fun fetchStableUpdate(): UpdateInfo suspend fun fetchUpdateJSON(@Path(FILE) file: String): UpdateInfo
@GET("beta.json")
suspend fun fetchBetaUpdate(): UpdateInfo
} }
interface JSDelivrServices { interface JSDelivrServices {
@ -33,9 +31,6 @@ interface JSDelivrServices {
@Streaming @Streaming
suspend fun fetchBootctl(@Path(REVISION) revision: String = Const.BOOTCTL_REVISION): ResponseBody suspend fun fetchBootctl(@Path(REVISION) revision: String = Const.BOOTCTL_REVISION): ResponseBody
@GET("$MAGISK_FILES@{$REVISION}/canary.json")
suspend fun fetchCanaryUpdate(@Path(REVISION) revision: String): UpdateInfo
@GET("$MAGISK_MAIN@{$REVISION}/scripts/module_installer.sh") @GET("$MAGISK_MAIN@{$REVISION}/scripts/module_installer.sh")
@Streaming @Streaming
suspend fun fetchInstaller(@Path(REVISION) revision: String): ResponseBody suspend fun fetchInstaller(@Path(REVISION) revision: String): ResponseBody

View File

@ -8,9 +8,6 @@ import com.topjohnwu.magisk.core.Config.Value.DEFAULT_CHANNEL
import com.topjohnwu.magisk.core.Config.Value.STABLE_CHANNEL import com.topjohnwu.magisk.core.Config.Value.STABLE_CHANNEL
import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.Info import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.model.MagiskJson
import com.topjohnwu.magisk.core.model.StubJson
import com.topjohnwu.magisk.core.model.UpdateInfo
import com.topjohnwu.magisk.data.network.* import com.topjohnwu.magisk.data.network.*
import retrofit2.HttpException import retrofit2.HttpException
import timber.log.Timber import timber.log.Timber
@ -40,22 +37,10 @@ class NetworkService(
} }
// UpdateInfo // UpdateInfo
private suspend fun fetchStableUpdate() = pages.fetchStableUpdate() private suspend fun fetchStableUpdate() = pages.fetchUpdateJSON("stable.json")
private suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate() private suspend fun fetchBetaUpdate() = pages.fetchUpdateJSON("beta.json")
private suspend fun fetchCanaryUpdate() = pages.fetchUpdateJSON("canary.json")
private suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url) private suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url)
private suspend fun fetchCanaryUpdate(): UpdateInfo {
val sha = fetchCanaryVersion()
val info = jsd.fetchCanaryUpdate(sha)
fun genCDNUrl(name: String) = "${Const.Url.JS_DELIVR_URL}${MAGISK_FILES}@${sha}/${name}"
fun MagiskJson.updateCopy() = copy(link = genCDNUrl(link), note = genCDNUrl(note))
fun StubJson.updateCopy() = copy(link = genCDNUrl(link))
return info.copy(
magisk = info.magisk.updateCopy(),
stub = info.stub.updateCopy()
)
}
private inline fun <T> safe(factory: () -> T): T? { private inline fun <T> safe(factory: () -> T): T? {
return try { return try {
@ -89,6 +74,5 @@ class NetworkService(
suspend fun fetchFile(url: String) = wrap { raw.fetchFile(url) } suspend fun fetchFile(url: String) = wrap { raw.fetchFile(url) }
suspend fun fetchString(url: String) = wrap { raw.fetchString(url) } suspend fun fetchString(url: String) = wrap { raw.fetchString(url) }
private suspend fun fetchCanaryVersion() = api.fetchBranch(MAGISK_FILES, "canary").commit.sha
private suspend fun fetchMainVersion() = api.fetchBranch(MAGISK_MAIN, "master").commit.sha private suspend fun fetchMainVersion() = api.fetchBranch(MAGISK_MAIN, "master").commit.sha
} }

View File

@ -5,13 +5,16 @@ plugins {
android { android {
val canary = !Config.version.contains(".") val canary = !Config.version.contains(".")
val url = Config["DEV_CHANNEL"] ?: if (canary) null
else "https://cdn.jsdelivr.net/gh/topjohnwu/magisk-files@${Config.version}/app-release.apk"
defaultConfig { defaultConfig {
applicationId = "com.topjohnwu.magisk" applicationId = "com.topjohnwu.magisk"
minSdkVersion(21)
versionCode = 1 versionCode = 1
versionName = Config.version versionName = Config.version
buildConfigField("int", "STUB_VERSION", Config.stubVersion) buildConfigField("int", "STUB_VERSION", Config.stubVersion)
buildConfigField("String", "DEV_CHANNEL", Config["DEV_CHANNEL"] ?: "null") buildConfigField("String", "APK_URL", url?.let { "\"$it\"" } ?: "null" )
buildConfigField("boolean", "CANARY", if (canary) "true" else "false")
} }
buildTypes { buildTypes {

View File

@ -4,7 +4,6 @@ import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Build;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -18,9 +17,8 @@ public class DelegateApplication extends Application {
protected void attachBaseContext(Context base) { protected void attachBaseContext(Context base) {
super.attachBaseContext(base); super.attachBaseContext(base);
// Only dynamic load full APK if hidden and supported // Only dynamic load full APK if hidden
dynLoad = Build.VERSION.SDK_INT >= 21 && dynLoad = !base.getPackageName().equals(BuildConfig.APPLICATION_ID);
!base.getPackageName().equals(BuildConfig.APPLICATION_ID);
receiver = InjectAPK.setup(this); receiver = InjectAPK.setup(this);
if (receiver != null) try { if (receiver != null) try {

View File

@ -15,7 +15,6 @@ import com.topjohnwu.magisk.net.Request;
import com.topjohnwu.magisk.utils.APKInstall; import com.topjohnwu.magisk.utils.APKInstall;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import java.io.File; import java.io.File;
@ -31,21 +30,23 @@ import static com.topjohnwu.magisk.R.string.upgrade_msg;
public class DownloadActivity extends Activity { public class DownloadActivity extends Activity {
private static final String APP_NAME = "Magisk"; private static final String APP_NAME = "Magisk";
private static final String CDN_URL = "https://cdn.jsdelivr.net/gh/topjohnwu/magisk_files@%s/%s"; private static final String CANARY_URL = "https://topjohnwu.github.io/magisk-files/canary.json";
private String apkLink; private String apkLink = BuildConfig.APK_URL;
private String sha;
private Context themed; private Context themed;
private ProgressDialog dialog; private ProgressDialog dialog;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Networking.init(this);
themed = new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault); themed = new ContextThemeWrapper(this, android.R.style.Theme_DeviceDefault);
if (Networking.checkNetworkStatus(this)) { if (Networking.checkNetworkStatus(this)) {
fetchAPKURL(); if (apkLink == null) {
fetchCanary();
} else {
showDialog();
}
} else { } else {
new AlertDialog.Builder(themed) new AlertDialog.Builder(themed)
.setCancelable(false) .setCancelable(false)
@ -56,21 +57,6 @@ public class DownloadActivity extends Activity {
} }
} }
private void fetchAPKURL() {
dialog = ProgressDialog.show(themed, "", "", true);
String url;
if (BuildConfig.DEV_CHANNEL != null) {
url = BuildConfig.DEV_CHANNEL;
} else if (!BuildConfig.CANARY) {
url = "https://topjohnwu.github.io/magisk_files/stable.json";
} else {
url = "https://api.github.com/repos/topjohnwu/magisk_files/branches/canary";
request(url).getAsJSONObject(this::handleCanary);
return;
}
request(url).getAsJSONObject(this::handleJSON);
}
private void error(Throwable e) { private void error(Throwable e) {
Log.e(getClass().getSimpleName(), "", e); Log.e(getClass().getSimpleName(), "", e);
finish(); finish();
@ -80,32 +66,27 @@ public class DownloadActivity extends Activity {
return Networking.get(url).setErrorHandler((conn, e) -> error(e)); return Networking.get(url).setErrorHandler((conn, e) -> error(e));
} }
private void handleCanary(JSONObject json) { private void showDialog() {
try { new AlertDialog.Builder(themed)
sha = json.getJSONObject("commit").getString("sha"); .setCancelable(false)
String url = String.format(CDN_URL, sha, "canary.json"); .setTitle(APP_NAME)
request(url).getAsJSONObject(this::handleJSON); .setMessage(getString(upgrade_msg))
} catch (JSONException e) { .setPositiveButton(yes, (d, w) -> dlAPK())
error(e); .setNegativeButton(no, (d, w) -> finish())
} .show();
} }
private void handleJSON(JSONObject json) { private void fetchCanary() {
dialog.dismiss(); dialog = ProgressDialog.show(themed, "", "", true);
try { request(CANARY_URL).getAsJSONObject(json -> {
apkLink = json.getJSONObject("app").getString("link"); dialog.dismiss();
if (!apkLink.startsWith("http")) try {
apkLink = String.format(CDN_URL, sha, apkLink); apkLink = json.getJSONObject("magisk").getString("link");
new AlertDialog.Builder(themed) showDialog();
.setCancelable(false) } catch (JSONException e) {
.setTitle(APP_NAME) error(e);
.setMessage(getString(upgrade_msg)) }
.setPositiveButton(yes, (d, w) -> dlAPK()) });
.setNegativeButton(no, (d, w) -> finish())
.show();
} catch (JSONException e) {
error(e);
}
} }
private void dlAPK() { private void dlAPK() {