Skip to content

Commit

Permalink
Add additional way to execute root commands (-c doesn't work in avd)
Browse files Browse the repository at this point in the history
  • Loading branch information
syt0r committed Feb 23, 2023
1 parent f403b60 commit 211ca37
Showing 1 changed file with 66 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,74 @@ import ua.sytor.wifipass.core.logger.Logger
import ua.sytor.wifipass.core.network_data_collector.NetworkDataCollectorContract.CollectingResult

class NetworkDataCollector(
private val commandExecutor: CommandExecutorContract.CommandExecutor
private val commandExecutor: CommandExecutorContract.CommandExecutor
) : NetworkDataCollectorContract.Collector {

override suspend fun collect(): CollectingResult {
Logger.log("collect")
if (!isRooted()) {
return CollectingResult.Failure(CollectingResult.FailureReason.NO_ROOT_ACCESS)
}

val (configPath, configOptions) = configs
.filter { (filePath, _) ->
isConfigurationFileExists(filePath)
}
.asSequence()
.firstOrNull()
?: return CollectingResult.Failure(CollectingResult.FailureReason.CONFIG_NOT_FOUND)


val fileContent = readFileContent(configPath)
?: return CollectingResult.Failure(CollectingResult.FailureReason.UNKNOWN)

val data = configOptions.parser.parseFileContent(fileContent)

return CollectingResult.Success(data, fileContent)
}

private suspend fun isRooted(): Boolean {
return try {
commandExecutor.execCommand(
cmdArray = arrayOf("su", "-c", "ls")
)
true
} catch (e: Exception) {
false
}
}

private suspend fun isConfigurationFileExists(path: String): Boolean {
return try {
val output = commandExecutor.execCommand(
cmdArray = arrayOf("su", "-c", "test -e $path && echo 1 || echo 0")
)
output.contains("1")
} catch (e: Exception) {
false
}
}

private suspend fun readFileContent(path: String): String? {
return commandExecutor.execCommand(
cmdArray = arrayOf("su", "-c", "cat $path")
)
}
companion object {
private const val ROOT_CHECK_COMMAND = "ls"
}

override suspend fun collect(): CollectingResult {
if (!isRooted()) {
return CollectingResult.Failure(CollectingResult.FailureReason.NO_ROOT_ACCESS)
}

val (configPath, configOptions) = configs
.filter { (filePath, _) ->
isConfigurationFileExists(filePath)
}
.asSequence()
.firstOrNull()
?: return CollectingResult.Failure(CollectingResult.FailureReason.CONFIG_NOT_FOUND)


val fileContent = readFileContent(configPath)
?: return CollectingResult.Failure(CollectingResult.FailureReason.UNKNOWN)

val data = configOptions.parser.parseFileContent(fileContent)

return CollectingResult.Success(data, fileContent)
}

private suspend fun isRooted(): Boolean {
return try {
runRootCommand(ROOT_CHECK_COMMAND)
true
} catch (e: Exception) {
Logger.log(e.toString())
false
}
}

private suspend fun isConfigurationFileExists(path: String): Boolean {
return try {
val output = runRootCommand("test -e $path && echo 1 || echo 0")
output.contains("1")
} catch (e: Exception) {
Logger.log(e.toString())
false
}
}

private suspend fun readFileContent(path: String): String? {
return kotlin.runCatching {
runRootCommand("cat $path")
}.getOrElse {
Logger.log(it.toString())
null
}
}

private suspend fun runRootCommand(command: String): String {
return kotlin.runCatching {
commandExecutor.execCommand(
cmdArray = arrayOf("su", "-c", command)
)
}.getOrElse {
commandExecutor.execCommand(
cmdArray = arrayOf("su", "0", command)
)
}
}

}

0 comments on commit 211ca37

Please sign in to comment.