Skip to content

Commit

Permalink
Tidy up network services
Browse files Browse the repository at this point in the history
Add jsdelivr CDN for several files
  • Loading branch information
topjohnwu committed Oct 6, 2020
1 parent d462873 commit 9225b47
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 125 deletions.
8 changes: 4 additions & 4 deletions app/src/main/java/com/topjohnwu/magisk/core/Const.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ object Const {

// Versions
const val SNET_EXT_VER = 15
const val SNET_REVISION = "d494bc726e86166913a13629e3b1336728ec5d7f"
const val BOOTCTL_REVISION = "a6c47f86f10b310358afa9dbe837037dd5d561df"
const val SNET_REVISION = "18ab78817087c337ae0edd1ecac38aec49217880"
const val BOOTCTL_REVISION = "18ab78817087c337ae0edd1ecac38aec49217880"

// Misc
const val ANDROID_MANIFEST = "AndroidManifest.xml"
Expand Down Expand Up @@ -51,14 +51,14 @@ object Const {
const val SOURCE_CODE_URL = "https://github.com/topjohnwu/Magisk"

const val GITHUB_RAW_URL = "https://raw.githubusercontent.com/"
const val GITHUB_API_URL = "https://api.github.com/users/Magisk-Modules-Repo/"
const val GITHUB_API_URL = "https://api.github.com/"
const val GITHUB_PAGE_URL = "https://topjohnwu.github.io/magisk_files/"
const val JS_DELIVR_URL = "https://cdn.jsdelivr.net/gh/"
}

object Key {
// others
const val LINK_KEY = "Link"
const val IF_NONE_MATCH = "If-None-Match"
const val ETAG_KEY = "ETag"
// intents
const val OPEN_SECTION = "section"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.topjohnwu.magisk.core
import android.content.Context
import androidx.work.*
import com.topjohnwu.magisk.BuildConfig
import com.topjohnwu.magisk.data.repository.MagiskRepository
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.view.Notifications
import com.topjohnwu.superuser.Shell
import kotlinx.coroutines.Dispatchers
Expand All @@ -15,14 +15,14 @@ import java.util.concurrent.TimeUnit
class UpdateCheckService(context: Context, workerParams: WorkerParameters)
: CoroutineWorker(context, workerParams), KoinComponent {

private val magiskRepo: MagiskRepository by inject()
private val svc: NetworkService by inject()

override suspend fun doWork(): Result {
// Make sure shell initializer was ran
withContext(Dispatchers.IO) {
Shell.getShell()
}
return magiskRepo.fetchUpdate()?.let {
return svc.fetchUpdate()?.let {
if (BuildConfig.VERSION_CODE < it.app.versionCode)
Notifications.managerUpdate(applicationContext)
else if (Info.env.isActive && Info.env.magiskVersionCode < it.magisk.versionCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.topjohnwu.magisk.core.base.BaseService
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.checkSum
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.core.utils.ProgressInputStream
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.ktx.withStreams
import com.topjohnwu.magisk.view.Notifications
import kotlinx.coroutines.CoroutineScope
Expand All @@ -34,7 +34,7 @@ abstract class BaseDownloader : BaseService(), KoinComponent {
private val notifications = Collections.synchronizedMap(HashMap<Int, Notification.Builder>())
private val coroutineScope = CoroutineScope(Dispatchers.IO)

val service: GithubRawServices by inject()
val service: NetworkService by inject()

// -- Service overrides

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.ktx.get
import com.topjohnwu.magisk.ktx.legalFilename
import kotlinx.android.parcel.Parcelize
Expand All @@ -23,15 +23,15 @@ data class Repo(
var last_update: Long
) : BaseModule(), Parcelable {

private val stringRepo: StringRepository get() = get()
private val svc: NetworkService get() = get()

val lastUpdate get() = Date(last_update)

val lastUpdateString: String get() = dateFormat.format(lastUpdate)

val downloadFilename: String get() = "$name-$version($versionCode).zip".legalFilename()

suspend fun readme() = stringRepo.getReadme(this)
suspend fun readme() = svc.fetchReadme(this)

val zipUrl: String get() = Const.Url.ZIP_URL.format(id)

Expand All @@ -53,7 +53,7 @@ data class Repo(
@Throws(IllegalRepoException::class)
suspend fun update(lastUpdate: Date? = null) {
lastUpdate?.let { last_update = it.time }
loadProps(stringRepo.getMetadata(this))
loadProps(svc.fetchMetadata(this))
}

class IllegalRepoException(message: String) : Exception(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.utils.MediaStoreUtils
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.inputStream
import com.topjohnwu.magisk.core.utils.MediaStoreUtils.outputStream
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.di.Protected
import com.topjohnwu.magisk.events.dialog.EnvFixDialog
import com.topjohnwu.magisk.ktx.reboot
Expand Down Expand Up @@ -53,7 +53,7 @@ abstract class MagiskInstallImpl : KoinComponent {
private val logs: MutableList<String>
private var tarOut: TarOutputStream? = null

private val service: GithubRawServices by inject()
private val service: NetworkService by inject()
protected val context: Context by inject()

protected constructor() {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/topjohnwu/magisk/core/tasks/PatchAPK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.isRunningAsStub
import com.topjohnwu.magisk.core.utils.AXML
import com.topjohnwu.magisk.core.utils.Keygen
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.ktx.get
import com.topjohnwu.magisk.ktx.writeTo
import com.topjohnwu.magisk.utils.Utils
Expand Down Expand Up @@ -91,7 +91,7 @@ object PatchAPK {
val dlStub = !isRunningAsStub && SDK_INT >= 28 && Const.Version.atLeast_20_2()
val src = if (dlStub) {
val stub = File(context.cacheDir, "stub.apk")
val svc = get<GithubRawServices>()
val svc = get<NetworkService>()
try {
svc.fetchFile(Info.remote.stub.link).byteStream().use {
it.writeTo(stub)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.squareup.moshi.JsonClass
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.data.database.RepoDao
import com.topjohnwu.magisk.data.network.GithubApiServices
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.ktx.synchronized
import kotlinx.coroutines.*
import timber.log.Timber
Expand All @@ -14,7 +14,7 @@ import java.util.*
import kotlin.collections.HashSet

class RepoUpdater(
private val api: GithubApiServices,
private val svc: NetworkService,
private val repoDB: RepoDao
) {

Expand Down Expand Up @@ -66,7 +66,7 @@ class RepoUpdater(
etag: String = ""
): PageResult = coroutineScope {
runCatching {
val result = api.fetchRepos(page, etag)
val result = svc.fetchRepos(page, etag)
result.run {
if (code() == HttpURLConnection.HTTP_NOT_MODIFIED)
return@coroutineScope PageResult.CACHED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.*

private const val REVISION = "revision"
private const val MODULE = "module"
private const val FILE = "file"
private const val IF_NONE_MATCH = "If-None-Match"

private const val MAGISK_FILES = "topjohnwu/magisk_files"
private const val MAGISK_MASTER = "topjohnwu/Magisk/master"
private const val MAGISK_MODULES = "Magisk-Modules-Repo"

interface GithubPageServices {

@GET("stable.json")
Expand All @@ -16,39 +25,32 @@ interface GithubPageServices {
suspend fun fetchBetaUpdate(): UpdateInfo
}

interface GithubRawServices {
interface JSDelivrServices {

//region topjohnwu/magisk_files
@GET("$MAGISK_FILES@{$REVISION}/snet")
@Streaming
suspend fun fetchSafetynet(@Path(REVISION) revision: String = Const.SNET_REVISION): ResponseBody

@GET("$MAGISK_FILES/master/stable.json")
suspend fun fetchStableUpdate(): UpdateInfo
@GET("$MAGISK_FILES@{$REVISION}/bootctl")
@Streaming
suspend fun fetchBootctl(@Path(REVISION) revision: String = Const.BOOTCTL_REVISION): ResponseBody
}

@GET("$MAGISK_FILES/master/beta.json")
suspend fun fetchBetaUpdate(): UpdateInfo
interface GithubRawServices {

@GET("$MAGISK_FILES/canary/debug.json")
suspend fun fetchCanaryUpdate(): UpdateInfo

@GET
suspend fun fetchCustomUpdate(@Url url: String): UpdateInfo

@GET("$MAGISK_FILES/{$REVISION}/snet.jar")
@Streaming
suspend fun fetchSafetynet(@Path(REVISION) revision: String = Const.SNET_REVISION): ResponseBody

@GET("$MAGISK_FILES/{$REVISION}/bootctl")
@Streaming
suspend fun fetchBootctl(@Path(REVISION) revision: String = Const.BOOTCTL_REVISION): ResponseBody

@GET("$MAGISK_MASTER/scripts/module_installer.sh")
@Streaming
suspend fun fetchInstaller(): ResponseBody

@GET("$MAGISK_MODULES/{$MODULE}/master/{$FILE}")
suspend fun fetchModuleFile(@Path(MODULE) id: String, @Path(FILE) file: String): String

//endregion

/**
* 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.
Expand All @@ -60,28 +62,17 @@ interface GithubRawServices {
@GET
suspend fun fetchString(@Url url: String): String


companion object {
private const val REVISION = "revision"
private const val MODULE = "module"
private const val FILE = "file"


private const val MAGISK_FILES = "topjohnwu/magisk_files"
private const val MAGISK_MASTER = "topjohnwu/Magisk/master"
private const val MAGISK_MODULES = "Magisk-Modules-Repo"
}

}

interface GithubApiServices {

@GET("repos")
@GET("users/${MAGISK_MODULES}/repos")
@Headers("Accept: application/vnd.github.v3+json")
suspend fun fetchRepos(
@Query("page") page: Int,
@Header(Const.Key.IF_NONE_MATCH) etag: String,
@Header(IF_NONE_MATCH) etag: String,
@Query("sort") sort: String = "pushed",
@Query("per_page") count: Int = 100
): Response<List<GithubRepoInfo>>

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.topjohnwu.magisk.data.repository

import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Config.Value.BETA_CHANNEL
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.DEFAULT_CHANNEL
import com.topjohnwu.magisk.core.Config.Value.STABLE_CHANNEL
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.data.network.GithubApiServices
import com.topjohnwu.magisk.data.network.GithubPageServices
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.data.network.JSDelivrServices
import retrofit2.HttpException
import timber.log.Timber
import java.io.IOException

class NetworkService(
private val pages: GithubPageServices,
private val raw: GithubRawServices,
private val jsd: JSDelivrServices,
private val api: GithubApiServices
) {
suspend fun fetchUpdate() = try {
var info = when (Config.updateChannel) {
DEFAULT_CHANNEL, STABLE_CHANNEL -> fetchStableUpdate()
BETA_CHANNEL -> fetchBetaUpdate()
CANARY_CHANNEL -> fetchCanaryUpdate()
CUSTOM_CHANNEL -> fetchCustomUpdate(Config.customChannelUrl)
else -> throw IllegalArgumentException()
}
if (info.magisk.versionCode < Info.env.magiskVersionCode &&
Config.updateChannel == DEFAULT_CHANNEL
) {
Config.updateChannel = BETA_CHANNEL
info = fetchBetaUpdate()
}
Info.remote = info
info
} catch (e: IOException) {
Timber.e(e)
null
} catch (e: HttpException) {
Timber.e(e)
null
}

// UpdateInfo
suspend fun fetchStableUpdate() = pages.fetchStableUpdate()
suspend fun fetchBetaUpdate() = pages.fetchBetaUpdate()
suspend fun fetchCanaryUpdate() = raw.fetchCanaryUpdate()
suspend fun fetchCustomUpdate(url: String) = raw.fetchCustomUpdate(url)

// Byte streams
suspend fun fetchSafetynet() = jsd.fetchSafetynet()
suspend fun fetchBootctl() = jsd.fetchBootctl()
suspend fun fetchInstaller() = raw.fetchInstaller()
suspend fun fetchFile(url: String) = raw.fetchFile(url)

// Strings
suspend fun fetchMetadata(repo: Repo) = raw.fetchModuleFile(repo.id, "module.prop")
suspend fun fetchReadme(repo: Repo) = raw.fetchModuleFile(repo.id, "README.md")
suspend fun fetchString(url: String) = raw.fetchString(url)

// API calls
suspend fun fetchRepos(page: Int, etag: String) = api.fetchRepos(page, etag)
}

This file was deleted.

Loading

0 comments on commit 9225b47

Please sign in to comment.