Download GitHub files through CDN

This commit is contained in:
topjohnwu 2020-10-07 01:51:05 -07:00
parent 8dc62a0232
commit 4e272b70ef
3 changed files with 58 additions and 21 deletions

View File

@ -41,3 +41,13 @@ data class StubJson(
val versionCode: Int = -1, val versionCode: Int = -1,
val link: String = "" val link: String = ""
) : Parcelable ) : Parcelable
@JsonClass(generateAdapter = true)
data class CommitInfo(
val sha: String
)
@JsonClass(generateAdapter = true)
data class BranchInfo(
val commit: CommitInfo
)

View File

@ -1,6 +1,7 @@
package com.topjohnwu.magisk.data.network package com.topjohnwu.magisk.data.network
import com.topjohnwu.magisk.core.Const import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.model.BranchInfo
import com.topjohnwu.magisk.core.model.UpdateInfo import com.topjohnwu.magisk.core.model.UpdateInfo
import com.topjohnwu.magisk.core.tasks.GithubRepoInfo import com.topjohnwu.magisk.core.tasks.GithubRepoInfo
import okhttp3.ResponseBody import okhttp3.ResponseBody
@ -11,10 +12,12 @@ private const val REVISION = "revision"
private const val MODULE = "module" private const val MODULE = "module"
private const val FILE = "file" private const val FILE = "file"
private const val IF_NONE_MATCH = "If-None-Match" private const val IF_NONE_MATCH = "If-None-Match"
private const val BRANCH = "branch"
private const val REPO = "repo"
private const val MAGISK_FILES = "topjohnwu/magisk_files" const val MAGISK_FILES = "topjohnwu/magisk_files"
private const val MAGISK_MASTER = "topjohnwu/Magisk/master" const val MAGISK_MAIN = "topjohnwu/Magisk"
private const val MAGISK_MODULES = "Magisk-Modules-Repo" private const val MAGISK_MODULES = "Magick-Modules-Repo"
interface GithubPageServices { interface GithubPageServices {
@ -34,27 +37,23 @@ interface JSDelivrServices {
@GET("$MAGISK_FILES@{$REVISION}/bootctl") @GET("$MAGISK_FILES@{$REVISION}/bootctl")
@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")
@Streaming
suspend fun fetchInstaller(@Path(REVISION) revision: String): ResponseBody
} }
interface GithubRawServices { interface GithubRawServices {
@GET("$MAGISK_FILES/canary/debug.json")
suspend fun fetchCanaryUpdate(): UpdateInfo
@GET @GET
suspend fun fetchCustomUpdate(@Url url: String): UpdateInfo suspend fun fetchCustomUpdate(@Url url: String): UpdateInfo
@GET("$MAGISK_MASTER/scripts/module_installer.sh")
@Streaming
suspend fun fetchInstaller(): ResponseBody
@GET("$MAGISK_MODULES/{$MODULE}/master/{$FILE}") @GET("$MAGISK_MODULES/{$MODULE}/master/{$FILE}")
suspend fun fetchModuleFile(@Path(MODULE) id: String, @Path(FILE) file: String): String suspend fun fetchModuleFile(@Path(MODULE) id: String, @Path(FILE) file: String): String
/**
* This method shall be used exclusively for fetching files from urls from previous requests.
* Him, who uses it in a wrong way, shall die in an eternal flame.
* */
@GET @GET
@Streaming @Streaming
suspend fun fetchFile(@Url url: String): ResponseBody suspend fun fetchFile(@Url url: String): ResponseBody
@ -66,7 +65,7 @@ interface GithubRawServices {
interface GithubApiServices { interface GithubApiServices {
@GET("users/${MAGISK_MODULES}/repos") @GET("users/$MAGISK_MODULES/repos")
@Headers("Accept: application/vnd.github.v3+json") @Headers("Accept: application/vnd.github.v3+json")
suspend fun fetchRepos( suspend fun fetchRepos(
@Query("page") page: Int, @Query("page") page: Int,
@ -74,5 +73,12 @@ interface GithubApiServices {
@Query("sort") sort: String = "pushed", @Query("sort") sort: String = "pushed",
@Query("per_page") count: Int = 100 @Query("per_page") count: Int = 100
): Response<List<GithubRepoInfo>> ): Response<List<GithubRepoInfo>>
@GET("repos/{$REPO}/branches/{$BRANCH}")
@Headers("Accept: application/vnd.github.v3+json")
suspend fun fetchBranch(
@Path(REPO, encoded = true) repo: String,
@Path(BRANCH) branch: String
): BranchInfo
} }

View File

@ -6,12 +6,12 @@ import com.topjohnwu.magisk.core.Config.Value.CANARY_CHANNEL
import com.topjohnwu.magisk.core.Config.Value.CUSTOM_CHANNEL import com.topjohnwu.magisk.core.Config.Value.CUSTOM_CHANNEL
import com.topjohnwu.magisk.core.Config.Value.DEFAULT_CHANNEL 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.Info import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.model.*
import com.topjohnwu.magisk.core.model.module.Repo import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.data.network.GithubApiServices import com.topjohnwu.magisk.data.network.*
import com.topjohnwu.magisk.data.network.GithubPageServices import okhttp3.ResponseBody
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.data.network.JSDelivrServices
import retrofit2.HttpException import retrofit2.HttpException
import timber.log.Timber import timber.log.Timber
import java.io.IOException import java.io.IOException
@ -49,13 +49,32 @@ class NetworkService(
// UpdateInfo // UpdateInfo
suspend fun fetchStableUpdate() = pages.fetchStableUpdate() suspend fun fetchStableUpdate() = pages.fetchStableUpdate()
suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate() suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate()
suspend fun fetchCanaryUpdate() = raw.fetchCanaryUpdate()
suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url) suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url)
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 ManagerJson.updateCopy() = copy(link = genCDNUrl(link), note = genCDNUrl(note))
fun MagiskJson.updateCopy() = copy(link = genCDNUrl(link), note = genCDNUrl(note))
fun StubJson.updateCopy() = copy(link = genCDNUrl(link))
fun UninstallerJson.updateCopy() = copy(link = genCDNUrl(link))
return info.copy(
app = info.app.updateCopy(),
magisk = info.magisk.updateCopy(),
stub = info.stub.updateCopy(),
uninstaller = info.uninstaller.updateCopy()
)
}
// Byte streams // Byte streams
suspend fun fetchSafetynet() = jsd.fetchSafetynet() suspend fun fetchSafetynet() = jsd.fetchSafetynet()
suspend fun fetchBootctl() = jsd.fetchBootctl() suspend fun fetchBootctl() = jsd.fetchBootctl()
suspend fun fetchInstaller() = raw.fetchInstaller() suspend fun fetchInstaller(): ResponseBody {
val sha = fetchMainVersion()
return jsd.fetchInstaller(sha)
}
suspend fun fetchFile(url: String) = raw.fetchFile(url) suspend fun fetchFile(url: String) = raw.fetchFile(url)
// Strings // Strings
@ -65,4 +84,6 @@ class NetworkService(
// API calls // API calls
suspend fun fetchRepos(page: Int, etag: String) = api.fetchRepos(page, etag) suspend fun fetchRepos(page: Int, etag: String) = api.fetchRepos(page, etag)
private suspend fun fetchCanaryVersion() = api.fetchBranch(MAGISK_FILES, "canary").commit.sha
private suspend fun fetchMainVersion() = api.fetchBranch(MAGISK_MAIN, "master").commit.sha
} }