Tidy up network services

Add jsdelivr CDN for several files
This commit is contained in:
topjohnwu 2020-10-06 04:58:46 -07:00
parent d462873e74
commit 9225b47568
17 changed files with 132 additions and 127 deletions

View File

@ -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"
@ -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"

View File

@ -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
@ -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)

View File

@ -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
@ -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

View File

@ -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
@ -23,7 +23,7 @@ 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)
@ -31,7 +31,7 @@ data class Repo(
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)
@ -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)

View File

@ -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
@ -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() {

View File

@ -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
@ -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)

View File

@ -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
@ -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
) {
@ -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

View File

@ -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")
@ -16,30 +25,25 @@ interface GithubPageServices {
suspend fun fetchBetaUpdate(): UpdateInfo
}
interface JSDelivrServices {
@GET("$MAGISK_FILES@{$REVISION}/snet")
@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
}
interface GithubRawServices {
//region topjohnwu/magisk_files
@GET("$MAGISK_FILES/master/stable.json")
suspend fun fetchStableUpdate(): UpdateInfo
@GET("$MAGISK_FILES/master/beta.json")
suspend fun fetchBetaUpdate(): UpdateInfo
@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
@ -47,8 +51,6 @@ interface GithubRawServices {
@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.
@ -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>>
}

View File

@ -1,39 +0,0 @@
package com.topjohnwu.magisk.data.repository
import com.topjohnwu.magisk.core.Config
import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.data.network.GithubPageServices
import com.topjohnwu.magisk.data.network.GithubRawServices
import retrofit2.HttpException
import timber.log.Timber
import java.io.IOException
class MagiskRepository(
private val rawSvc: GithubRawServices,
private val pageSvc: GithubPageServices
) {
suspend fun fetchUpdate() = try {
var info = when (Config.updateChannel) {
Config.Value.DEFAULT_CHANNEL, Config.Value.STABLE_CHANNEL -> pageSvc.fetchStableUpdate()
Config.Value.BETA_CHANNEL -> pageSvc.fetchBetaUpdate()
Config.Value.CANARY_CHANNEL -> rawSvc.fetchCanaryUpdate()
Config.Value.CUSTOM_CHANNEL -> rawSvc.fetchCustomUpdate(Config.customChannelUrl)
else -> throw IllegalArgumentException()
}
if (info.magisk.versionCode < Info.env.magiskVersionCode &&
Config.updateChannel == Config.Value.DEFAULT_CHANNEL) {
Config.updateChannel = Config.Value.BETA_CHANNEL
info = pageSvc.fetchBetaUpdate()
}
Info.remote = info
info
} catch (e: IOException) {
Timber.e(e)
null
} catch (e: HttpException) {
Timber.e(e)
null
}
}

View File

@ -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)
}

View File

@ -1,16 +0,0 @@
package com.topjohnwu.magisk.data.repository
import com.topjohnwu.magisk.core.model.module.Repo
import com.topjohnwu.magisk.data.network.GithubRawServices
class StringRepository(
private val api: GithubRawServices
) {
suspend fun getString(url: String) = api.fetchString(url)
suspend fun getMetadata(repo: Repo) = api.fetchModuleFile(repo.id, "module.prop")
suspend fun getReadme(repo: Repo) = api.fetchModuleFile(repo.id, "README.md")
}

View File

@ -9,6 +9,7 @@ import com.topjohnwu.magisk.core.Info
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 com.topjohnwu.magisk.ktx.precomputedText
import com.topjohnwu.magisk.net.Networking
import com.topjohnwu.magisk.net.NoSSLv3SocketFactory
@ -19,6 +20,7 @@ import okhttp3.Dns
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.dnsoverhttps.DnsOverHttps
import okhttp3.logging.HttpLoggingInterceptor
import org.koin.dsl.module
import retrofit2.Retrofit
import retrofit2.converter.moshi.MoshiConverterFactory
@ -32,6 +34,7 @@ val networkingModule = module {
single { createApiService<GithubRawServices>(get(), Const.Url.GITHUB_RAW_URL) }
single { createApiService<GithubApiServices>(get(), Const.Url.GITHUB_API_URL) }
single { createApiService<GithubPageServices>(get(), Const.Url.GITHUB_PAGE_URL) }
single { createApiService<JSDelivrServices>(get(), Const.Url.JS_DELIVR_URL) }
single { createMarkwon(get(), get()) }
}
@ -75,10 +78,10 @@ private class DnsResolver(client: OkHttpClient) : Dns {
fun createOkHttpClient(context: Context): OkHttpClient {
val builder = OkHttpClient.Builder()
// val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
// level = HttpLoggingInterceptor.Level.HEADERS
// }
// builder.addInterceptor(httpLoggingInterceptor)
val httpLoggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.HEADERS
}
builder.addInterceptor(httpLoggingInterceptor)
if (!Networking.init(context)) {
Info.hasGMS = false

View File

@ -1,13 +1,11 @@
package com.topjohnwu.magisk.di
import com.topjohnwu.magisk.data.repository.LogRepository
import com.topjohnwu.magisk.data.repository.MagiskRepository
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.data.repository.NetworkService
import org.koin.dsl.module
val repositoryModule = module {
single { MagiskRepository(get(), get()) }
single { LogRepository(get()) }
single { StringRepository(get()) }
single { NetworkService(get(), get(), get(), get()) }
}

View File

@ -12,7 +12,7 @@ import com.topjohnwu.magisk.core.download.Subject
import com.topjohnwu.magisk.core.download.Subject.Manager
import com.topjohnwu.magisk.core.model.MagiskJson
import com.topjohnwu.magisk.core.model.ManagerJson
import com.topjohnwu.magisk.data.repository.MagiskRepository
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.events.OpenInappLinkEvent
import com.topjohnwu.magisk.events.SnackbarEvent
import com.topjohnwu.magisk.events.dialog.EnvFixDialog
@ -32,7 +32,7 @@ enum class MagiskState {
}
class HomeViewModel(
private val repoMagisk: MagiskRepository
private val svc: NetworkService
) : BaseViewModel() {
@get:Bindable
@ -84,7 +84,7 @@ class HomeViewModel(
state = State.LOADING
notifyPropertyChanged(BR.showUninstall)
notifyPropertyChanged(BR.showSafetyNet)
repoMagisk.fetchUpdate()?.apply {
svc.fetchUpdate()?.apply {
state = State.LOADED
stateMagisk = when {
!Info.env.isActive -> MagiskState.NOT_INSTALLED

View File

@ -11,7 +11,7 @@ import com.topjohnwu.magisk.core.Info
import com.topjohnwu.magisk.core.download.Action
import com.topjohnwu.magisk.core.download.DownloadService
import com.topjohnwu.magisk.core.download.Subject
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.events.MagiskInstallFileEvent
import com.topjohnwu.magisk.events.dialog.SecondSlotWarningDialog
import com.topjohnwu.magisk.utils.set
@ -21,7 +21,7 @@ import org.koin.core.get
import kotlin.math.roundToInt
class InstallViewModel(
stringRepo: StringRepository
svc: NetworkService
) : BaseViewModel(State.LOADED) {
val isRooted = Shell.rootAccess()
@ -64,7 +64,7 @@ class InstallViewModel(
init {
viewModelScope.launch {
notes = stringRepo.getString(Info.remote.magisk.note)
notes = svc.fetchString(Info.remote.magisk.note)
}
}

View File

@ -5,7 +5,7 @@ import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.arch.ContextExecutor
import com.topjohnwu.magisk.arch.ViewEventWithScope
import com.topjohnwu.magisk.core.Const
import com.topjohnwu.magisk.data.network.GithubRawServices
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.ktx.DynamicClassLoader
import com.topjohnwu.magisk.ktx.writeTo
import com.topjohnwu.magisk.view.MagiskDialog
@ -28,7 +28,7 @@ class CheckSafetyNetEvent(
private val callback: (SafetyNetResult) -> Unit = {}
) : ViewEventWithScope(), ContextExecutor, KoinComponent, SafetyNetHelper.Callback {
private val svc by inject<GithubRawServices>()
private val svc by inject<NetworkService>()
private lateinit var apk: File
private lateinit var dex: File

View File

@ -4,7 +4,7 @@ import android.content.Context
import android.view.LayoutInflater
import android.widget.TextView
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.data.repository.StringRepository
import com.topjohnwu.magisk.data.repository.NetworkService
import com.topjohnwu.magisk.ktx.coroutineScope
import io.noties.markwon.Markwon
import kotlinx.coroutines.CancellationException
@ -18,12 +18,12 @@ import kotlin.coroutines.coroutineContext
object MarkDownWindow : KoinComponent {
private val repo: StringRepository by inject()
private val svc: NetworkService by inject()
private val markwon: Markwon by inject()
suspend fun show(activity: Context, title: String?, url: String) {
show(activity, title) {
repo.getString(url)
svc.fetchString(url)
}
}