tweak root detection

fixes #76

Signed-off-by: androidacy-user <opensource@androidacy.com>
pull/102/head
androidacy-user 2 years ago
parent e0fafde550
commit 340ec5adc7

@ -4,15 +4,11 @@
package com.fox2code.mmm.installer package com.fox2code.mmm.installer
import com.fox2code.mmm.Constants
import com.fox2code.mmm.MainApplication import com.fox2code.mmm.MainApplication
import com.fox2code.mmm.NotificationType import com.fox2code.mmm.NotificationType
import com.fox2code.mmm.utils.io.Files.Companion.existsSU
import com.topjohnwu.superuser.NoShellException import com.topjohnwu.superuser.NoShellException
import com.topjohnwu.superuser.Shell import com.topjohnwu.superuser.Shell
import ly.count.android.sdk.Countly
import timber.log.Timber import timber.log.Timber
import java.io.File
@Suppress("unused") @Suppress("unused")
class InstallerInitializer { class InstallerInitializer {
@ -139,79 +135,75 @@ class InstallerInitializer {
if (MainApplication.forceDebugLogging) { if (MainApplication.forceDebugLogging) {
Timber.e("Failed to check for ramdisk") Timber.e("Failed to check for ramdisk")
} }
return null Companion.hsRmdsk = false
}
if (MainApplication.forceDebugLogging) {
Timber.i("Found ramdisk: %s", output[0])
if (MainApplication.forceDebugLogging) Timber.i(
"Searching for Magisk path. Current path: %s",
mgskPth
)
} }
// reset output
output.clear() output.clear()
// try to use magisk --path. if that fails, check for /data/adb/ksu for kernelsu support if (Shell.cmd("su -v").to(output).exec().isSuccess) {
if (Shell.cmd("magisk --path", "su -V").to(output) Timber.i("SU version small: %s", output[0])
.exec().isSuccess && output[0].isNotEmpty() && !output[0].contains( if (output.size != 0) {
"not found" // try su -V
) if (Shell.cmd("su -V").exec().isSuccess) {
) { val suVer = Shell.cmd("su -V").exec().out
mgskPth = output[0] Timber.i("SU version: %s", suVer[0])
if (MainApplication.forceDebugLogging) { // use regex to get version code
Timber.i("Magisk path 1: %s", mgskPth) val matcher2 = Regex("(\\d+)").find(suVer[0])
if (matcher2 != null) {
mgskVerCode = matcher2.groupValues[1].toInt()
if (mgskVerCode > verCode) {
verCode = mgskVerCode
Timber.i("SU version: %d", mgskVerCode)
}
} else {
if (MainApplication.forceDebugLogging) {
Timber.e("Failed to get su version: matcher2 is null")
}
verCode = 0
}
} else {
if (MainApplication.forceDebugLogging) {
Timber.e("Failed to get su version: su -V: unsuccessful")
}
verCode = 0
return null
}
} else {
if (MainApplication.forceDebugLogging) {
Timber.e("Failed to get su version: su -v: output size is 0")
}
verCode = 0
} }
} else if (Shell.cmd( mgskPth = "/data/adb/modules" // hardcoded path. all modern versions of ksu and magisk use this path
"if [ -d /data/adb/ksu ]; then echo true; else echo false; fi",
"/data/adb/ksud -V"
).to(
output
).exec().isSuccess && "true" == output[0] && output[1].isNotEmpty() && !output[1].contains(
"not found", true)
) {
if (MainApplication.forceDebugLogging) { if (MainApplication.forceDebugLogging) {
Timber.i("Kernelsu detected") Timber.i("Magisk path: %s", mgskPth)
} }
verCode = output[1].toInt() Companion.mgskPth = mgskPth
mgskPth = "/data/adb" val suVer2 = Shell.cmd("su -v").exec().out
isKsu = true // if output[0] contains kernelsu, then it's ksu. if it contains magisk, then it's magisk. otherwise, it's something we don't know and we return null
// if analytics enabled, set breadcrumb for countly if (suVer2[0].contains("kernelsu", true)) {
if (MainApplication.analyticsAllowed()) { isKsu = true
Countly.sharedInstance().crashes().addCrashBreadcrumb("ksu detected") if (MainApplication.forceDebugLogging) {
} Timber.i("SU version: ksu")
if (MainApplication.forceDebugLogging) { }
Timber.e("[ANOMALY] Kernelsu not detected but /data/adb/ksu exists - maybe outdated?") } else if (suVer2[0].contains("magisk", true)) {
isKsu = false
if (MainApplication.forceDebugLogging) {
Timber.i("SU version: magisk")
}
} else {
if (MainApplication.forceDebugLogging) {
Timber.e("Failed to get su version: unknown su")
}
verCode = 0
return null
} }
return mgskPth return mgskPth
} else { } else {
if (MainApplication.forceDebugLogging) { if (MainApplication.forceDebugLogging) {
Timber.e("Failed to get Magisk path") Timber.e("Failed to get su version")
} }
verCode = 0
return null return null
} }
if (MainApplication.forceDebugLogging) Timber.i("Magisk runtime path: %s", mgskPth)
mgskVerCode = output[1].toInt()
if (MainApplication.forceDebugLogging) Timber.i(
"Magisk version code: %s",
mgskVerCode
)
if (mgskVerCode >= Constants.MAGISK_VER_CODE_FLAT_MODULES && mgskVerCode < Constants.MAGISK_VER_CODE_PATH_SUPPORT && (mgskPth.isEmpty() || !File(
mgskPth
).exists())
) {
mgskPth = "/sbin"
}
if (mgskPth.isNotEmpty() && existsSU(File(mgskPth))) {
Companion.mgskPth = mgskPth
} else {
Timber.e("Failed to get Magisk path (Got $mgskPth)")
mgskPth = null
}
// if mgskPth is null, but we're granted root, log an error
if (mgskPth == null && Shell.isAppGrantedRoot() == true) {
Timber.e("[ANOMALY] Failed to get Magisk path but granted root")
}
verCode = mgskVerCode
return mgskPth
} catch (ignored: Exception) { } catch (ignored: Exception) {
// work around edge case // work around edge case
return if (tries <= 10) { return if (tries <= 10) {

Loading…
Cancel
Save