mirror of
https://github.com/kvorschk/Magisk.git
synced 2026-12-05 23:14:03 -05:00
Added safetynet implementation
The implementation itself was moved from fragment to self contained event. The result resolution might be moved to the event as well
This commit is contained in:
@@ -15,3 +15,5 @@ sealed class PolicyUpdateEvent(val item: MagiskPolicy) : RxBus.Event {
|
||||
}
|
||||
|
||||
class ModuleUpdatedEvent(val item: ModuleRvItem) : RxBus.Event
|
||||
|
||||
data class SafetyNetResult(val responseCode: Int) : RxBus.Event
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.topjohnwu.magisk.model.events
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.karumi.dexter.Dexter
|
||||
@@ -8,9 +9,25 @@ import com.karumi.dexter.MultiplePermissionsReport
|
||||
import com.karumi.dexter.PermissionToken
|
||||
import com.karumi.dexter.listener.PermissionRequest
|
||||
import com.karumi.dexter.listener.multi.MultiplePermissionsListener
|
||||
import com.topjohnwu.magisk.Const
|
||||
import com.topjohnwu.magisk.R
|
||||
import com.topjohnwu.magisk.data.repository.MagiskRepository
|
||||
import com.topjohnwu.magisk.extensions.DynamicClassLoader
|
||||
import com.topjohnwu.magisk.extensions.subscribeK
|
||||
import com.topjohnwu.magisk.extensions.writeTo
|
||||
import com.topjohnwu.magisk.model.entity.module.Repo
|
||||
import com.topjohnwu.magisk.model.permissions.PermissionRequestBuilder
|
||||
import com.topjohnwu.magisk.utils.RxBus
|
||||
import com.topjohnwu.magisk.utils.SafetyNetHelper
|
||||
import com.topjohnwu.magisk.view.MagiskDialog
|
||||
import com.topjohnwu.superuser.Shell
|
||||
import dalvik.system.DexFile
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.subjects.PublishSubject
|
||||
import org.koin.core.KoinComponent
|
||||
import org.koin.core.inject
|
||||
import java.io.File
|
||||
import java.lang.reflect.InvocationHandler
|
||||
|
||||
/**
|
||||
* Class for passing events from ViewModels to Activities/Fragments
|
||||
@@ -34,7 +51,88 @@ class MagiskChangelogEvent : ViewEvent()
|
||||
class UninstallEvent : ViewEvent()
|
||||
class EnvFixEvent : ViewEvent()
|
||||
|
||||
class UpdateSafetyNetEvent : ViewEvent()
|
||||
class UpdateSafetyNetEvent : ViewEvent(), ContextExecutor, KoinComponent, SafetyNetHelper.Callback {
|
||||
|
||||
private val magiskRepo by inject<MagiskRepository>()
|
||||
private val rxBus by inject<RxBus>()
|
||||
|
||||
private lateinit var EXT_APK: File
|
||||
private lateinit var EXT_DEX: File
|
||||
|
||||
override fun invoke(context: Context) {
|
||||
val die = ::EXT_APK.isInitialized
|
||||
|
||||
EXT_APK = File("${context.filesDir.parent}/snet", "snet.jar")
|
||||
EXT_DEX = File(EXT_APK.parent, "snet.dex")
|
||||
|
||||
Completable.fromAction {
|
||||
val loader = DynamicClassLoader(EXT_APK)
|
||||
val dex = DexFile.loadDex(EXT_APK.path, EXT_DEX.path, 0)
|
||||
|
||||
// Scan through the dex and find our helper class
|
||||
var helperClass: Class<*>? = null
|
||||
for (className in dex.entries()) {
|
||||
if (className.startsWith("x.")) {
|
||||
val cls = loader.loadClass(className)
|
||||
if (InvocationHandler::class.java.isAssignableFrom(cls)) {
|
||||
helperClass = cls
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
helperClass ?: throw Exception()
|
||||
|
||||
val helper = helperClass.getMethod(
|
||||
"get",
|
||||
Class::class.java, Context::class.java, Any::class.java
|
||||
)
|
||||
.invoke(null, SafetyNetHelper::class.java, context, this) as SafetyNetHelper
|
||||
|
||||
if (helper.version < Const.SNET_EXT_VER)
|
||||
throw Exception()
|
||||
|
||||
helper.attest()
|
||||
}.subscribeK(onError = {
|
||||
if (die) {
|
||||
rxBus.post(SafetyNetResult(-1))
|
||||
} else {
|
||||
Shell.sh("rm -rf " + EXT_APK.parent).exec()
|
||||
EXT_APK.parentFile?.mkdir()
|
||||
download(context, true)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@Suppress("SameParameterValue")
|
||||
private fun download(context: Context, askUser: Boolean) {
|
||||
fun downloadInternal() = magiskRepo.fetchSafetynet()
|
||||
.map { it.byteStream().writeTo(EXT_APK) }
|
||||
.subscribeK { invoke(context) }
|
||||
|
||||
if (!askUser) {
|
||||
downloadInternal()
|
||||
return
|
||||
}
|
||||
|
||||
MagiskDialog(context)
|
||||
.applyTitle(R.string.proprietary_title)
|
||||
.applyMessage(R.string.proprietary_notice)
|
||||
.cancellable(false)
|
||||
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
|
||||
titleRes = R.string.yes
|
||||
onClick { downloadInternal() }
|
||||
}
|
||||
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
|
||||
titleRes = R.string.no_thanks
|
||||
onClick { rxBus.post(SafetyNetResult(-2)) }
|
||||
}
|
||||
.reveal()
|
||||
}
|
||||
|
||||
override fun onResponse(responseCode: Int) {
|
||||
rxBus.post(SafetyNetResult(responseCode))
|
||||
}
|
||||
}
|
||||
|
||||
class ViewActionEvent(val action: Activity.() -> Unit) : ViewEvent(), ActivityExecutor {
|
||||
override fun invoke(activity: AppCompatActivity) = activity.run(action)
|
||||
|
||||
Reference in New Issue
Block a user