Skip to content

Commit 340ec5a

Browse files
tweak root detection
fixes #76 Signed-off-by: androidacy-user <opensource@androidacy.com>
1 parent e0fafde commit 340ec5a

1 file changed

Lines changed: 57 additions & 65 deletions

File tree

app/src/main/kotlin/com/fox2code/mmm/installer/InstallerInitializer.kt

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
package com.fox2code.mmm.installer
66

7-
import com.fox2code.mmm.Constants
87
import com.fox2code.mmm.MainApplication
98
import com.fox2code.mmm.NotificationType
10-
import com.fox2code.mmm.utils.io.Files.Companion.existsSU
119
import com.topjohnwu.superuser.NoShellException
1210
import com.topjohnwu.superuser.Shell
13-
import ly.count.android.sdk.Countly
1411
import timber.log.Timber
15-
import java.io.File
1612

1713
@Suppress("unused")
1814
class InstallerInitializer {
@@ -139,79 +135,75 @@ class InstallerInitializer {
139135
if (MainApplication.forceDebugLogging) {
140136
Timber.e("Failed to check for ramdisk")
141137
}
142-
return null
143-
}
144-
if (MainApplication.forceDebugLogging) {
145-
Timber.i("Found ramdisk: %s", output[0])
146-
if (MainApplication.forceDebugLogging) Timber.i(
147-
"Searching for Magisk path. Current path: %s",
148-
mgskPth
149-
)
138+
Companion.hsRmdsk = false
150139
}
151-
// reset output
152140
output.clear()
153-
// try to use magisk --path. if that fails, check for /data/adb/ksu for kernelsu support
154-
if (Shell.cmd("magisk --path", "su -V").to(output)
155-
.exec().isSuccess && output[0].isNotEmpty() && !output[0].contains(
156-
"not found"
157-
)
158-
) {
159-
mgskPth = output[0]
160-
if (MainApplication.forceDebugLogging) {
161-
Timber.i("Magisk path 1: %s", mgskPth)
141+
if (Shell.cmd("su -v").to(output).exec().isSuccess) {
142+
Timber.i("SU version small: %s", output[0])
143+
if (output.size != 0) {
144+
// try su -V
145+
if (Shell.cmd("su -V").exec().isSuccess) {
146+
val suVer = Shell.cmd("su -V").exec().out
147+
Timber.i("SU version: %s", suVer[0])
148+
// use regex to get version code
149+
val matcher2 = Regex("(\\d+)").find(suVer[0])
150+
if (matcher2 != null) {
151+
mgskVerCode = matcher2.groupValues[1].toInt()
152+
if (mgskVerCode > verCode) {
153+
verCode = mgskVerCode
154+
Timber.i("SU version: %d", mgskVerCode)
155+
}
156+
} else {
157+
if (MainApplication.forceDebugLogging) {
158+
Timber.e("Failed to get su version: matcher2 is null")
159+
}
160+
verCode = 0
161+
}
162+
} else {
163+
if (MainApplication.forceDebugLogging) {
164+
Timber.e("Failed to get su version: su -V: unsuccessful")
165+
}
166+
verCode = 0
167+
return null
168+
}
169+
} else {
170+
if (MainApplication.forceDebugLogging) {
171+
Timber.e("Failed to get su version: su -v: output size is 0")
172+
}
173+
verCode = 0
162174
}
163-
} else if (Shell.cmd(
164-
"if [ -d /data/adb/ksu ]; then echo true; else echo false; fi",
165-
"/data/adb/ksud -V"
166-
).to(
167-
output
168-
).exec().isSuccess && "true" == output[0] && output[1].isNotEmpty() && !output[1].contains(
169-
"not found", true)
170-
) {
175+
mgskPth = "/data/adb/modules" // hardcoded path. all modern versions of ksu and magisk use this path
171176
if (MainApplication.forceDebugLogging) {
172-
Timber.i("Kernelsu detected")
177+
Timber.i("Magisk path: %s", mgskPth)
173178
}
174-
verCode = output[1].toInt()
175-
mgskPth = "/data/adb"
176-
isKsu = true
177-
// if analytics enabled, set breadcrumb for countly
178-
if (MainApplication.analyticsAllowed()) {
179-
Countly.sharedInstance().crashes().addCrashBreadcrumb("ksu detected")
180-
}
181-
if (MainApplication.forceDebugLogging) {
182-
Timber.e("[ANOMALY] Kernelsu not detected but /data/adb/ksu exists - maybe outdated?")
179+
Companion.mgskPth = mgskPth
180+
val suVer2 = Shell.cmd("su -v").exec().out
181+
// 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
182+
if (suVer2[0].contains("kernelsu", true)) {
183+
isKsu = true
184+
if (MainApplication.forceDebugLogging) {
185+
Timber.i("SU version: ksu")
186+
}
187+
} else if (suVer2[0].contains("magisk", true)) {
188+
isKsu = false
189+
if (MainApplication.forceDebugLogging) {
190+
Timber.i("SU version: magisk")
191+
}
192+
} else {
193+
if (MainApplication.forceDebugLogging) {
194+
Timber.e("Failed to get su version: unknown su")
195+
}
196+
verCode = 0
197+
return null
183198
}
184199
return mgskPth
185200
} else {
186201
if (MainApplication.forceDebugLogging) {
187-
Timber.e("Failed to get Magisk path")
202+
Timber.e("Failed to get su version")
188203
}
204+
verCode = 0
189205
return null
190206
}
191-
if (MainApplication.forceDebugLogging) Timber.i("Magisk runtime path: %s", mgskPth)
192-
mgskVerCode = output[1].toInt()
193-
if (MainApplication.forceDebugLogging) Timber.i(
194-
"Magisk version code: %s",
195-
mgskVerCode
196-
)
197-
if (mgskVerCode >= Constants.MAGISK_VER_CODE_FLAT_MODULES && mgskVerCode < Constants.MAGISK_VER_CODE_PATH_SUPPORT && (mgskPth.isEmpty() || !File(
198-
mgskPth
199-
).exists())
200-
) {
201-
mgskPth = "/sbin"
202-
}
203-
if (mgskPth.isNotEmpty() && existsSU(File(mgskPth))) {
204-
Companion.mgskPth = mgskPth
205-
} else {
206-
Timber.e("Failed to get Magisk path (Got $mgskPth)")
207-
mgskPth = null
208-
}
209-
// if mgskPth is null, but we're granted root, log an error
210-
if (mgskPth == null && Shell.isAppGrantedRoot() == true) {
211-
Timber.e("[ANOMALY] Failed to get Magisk path but granted root")
212-
}
213-
verCode = mgskVerCode
214-
return mgskPth
215207
} catch (ignored: Exception) {
216208
// work around edge case
217209
return if (tries <= 10) {

0 commit comments

Comments
 (0)