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
const val SNET_EXT_VER = 15
const val SNET_REVISION = "18ab78817087c337ae0edd1ecac38aec49217880"
const val BOOTCTL_REVISION = "18ab78817087c337ae0edd1ecac38aec49217880"
const val SNET_REVISION = "22.0"
const val BOOTCTL_REVISION = "22.0"
// Misc
val USER_ID = Process.myUid() / 100000
@ -62,7 +62,7 @@ object Const {
const val GITHUB_RAW_URL = "https://raw.githubusercontent.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 OFFICIAL_REPO = "https://magisk-modules-repo.github.io/submission/modules.json"
}

View File

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

View File

@ -10,17 +10,15 @@ import retrofit2.http.*
private const val REVISION = "revision"
private const val BRANCH = "branch"
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"
interface GithubPageServices {
@GET("stable.json")
suspend fun fetchStableUpdate(): UpdateInfo
@GET("beta.json")
suspend fun fetchBetaUpdate(): UpdateInfo
@GET("{$FILE}")
suspend fun fetchUpdateJSON(@Path(FILE) file: String): UpdateInfo
}
interface JSDelivrServices {
@ -33,9 +31,6 @@ interface JSDelivrServices {
@Streaming
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")
@Streaming
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.Const
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 retrofit2.HttpException
import timber.log.Timber
@ -40,22 +37,10 @@ class NetworkService(
}
// UpdateInfo
private suspend fun fetchStableUpdate() = pages.fetchStableUpdate()
private suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate()
private suspend fun fetchStableUpdate() = pages.fetchUpdateJSON("stable.json")
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 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? {
return try {
@ -89,6 +74,5 @@ class NetworkService(
suspend fun fetchFile(url: String) = wrap { raw.fetchFile(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
}

View File

@ -5,13 +5,16 @@ plugins {
android {
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 {
applicationId = "com.topjohnwu.magisk"
minSdkVersion(21)
versionCode = 1
versionName = Config.version
buildConfigField("int", "STUB_VERSION", Config.stubVersion)
buildConfigField("String", "DEV_CHANNEL", Config["DEV_CHANNEL"] ?: "null")
buildConfigField("boolean", "CANARY", if (canary) "true" else "false")
buildConfigField("String", "APK_URL", url?.let { "\"$it\"" } ?: "null" )
}
buildTypes {

View File

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

View File

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