From 211ca379431334cffa9d316d5d5a53cbcd3aa1a6 Mon Sep 17 00:00:00 2001 From: syt0r Date: Thu, 23 Feb 2023 13:48:13 +0200 Subject: [PATCH] Add additional way to execute root commands (-c doesn't work in avd) --- .../NetworkDataCollector.kt | 117 ++++++++++-------- 1 file changed, 66 insertions(+), 51 deletions(-) diff --git a/app/src/main/java/ua/sytor/wifipass/core/network_data_collector/NetworkDataCollector.kt b/app/src/main/java/ua/sytor/wifipass/core/network_data_collector/NetworkDataCollector.kt index b386c09..2735056 100644 --- a/app/src/main/java/ua/sytor/wifipass/core/network_data_collector/NetworkDataCollector.kt +++ b/app/src/main/java/ua/sytor/wifipass/core/network_data_collector/NetworkDataCollector.kt @@ -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) + ) + } + } } \ No newline at end of file