Refactor Kpm.kt, optimize file type checking logic, add ELF file detection, simplify string command execution
This commit is contained in:
@@ -32,6 +32,7 @@ import com.sukisu.ultra.ui.util.*
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import androidx.core.content.edit
|
import androidx.core.content.edit
|
||||||
import com.sukisu.ultra.R
|
import com.sukisu.ultra.R
|
||||||
|
import java.io.FileInputStream
|
||||||
import java.net.*
|
import java.net.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -40,7 +41,6 @@ import java.net.*
|
|||||||
* 开发者:zako, Liaokong
|
* 开发者:zako, Liaokong
|
||||||
*/
|
*/
|
||||||
var globalModuleFileName: String = ""
|
var globalModuleFileName: String = ""
|
||||||
|
|
||||||
@OptIn(ExperimentalMaterial3Api::class)
|
@OptIn(ExperimentalMaterial3Api::class)
|
||||||
@Destination<RootGraph>
|
@Destination<RootGraph>
|
||||||
@Composable
|
@Composable
|
||||||
@@ -168,29 +168,14 @@ fun KpmScreen(
|
|||||||
if (!isCorrectMimeType) {
|
if (!isCorrectMimeType) {
|
||||||
var shouldShowSnackbar = true
|
var shouldShowSnackbar = true
|
||||||
try {
|
try {
|
||||||
val command = arrayOf("su", "-c", "strings ${tempFile.absolutePath} | grep -E 'name=|version=|license=|author='")
|
val matchCount = checkStringsCommand(tempFile)
|
||||||
val process = Runtime.getRuntime().exec(command)
|
val isElf = isElfFile(tempFile)
|
||||||
val inputStream = process.inputStream
|
|
||||||
val reader = java.io.BufferedReader(java.io.InputStreamReader(inputStream))
|
if (matchCount >= 1 || isElf) {
|
||||||
var line: String?
|
|
||||||
var matchCount = 0
|
|
||||||
val keywords = listOf("name=", "version=", "license=", "author=")
|
|
||||||
while (reader.readLine().also { line = it } != null) {
|
|
||||||
for (keyword in keywords) {
|
|
||||||
if (line!!.startsWith(keyword)) {
|
|
||||||
matchCount++
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
process.waitFor()
|
|
||||||
if (matchCount < 2) {
|
|
||||||
shouldShowSnackbar = true
|
|
||||||
} else {
|
|
||||||
shouldShowSnackbar = false
|
shouldShowSnackbar = false
|
||||||
}
|
}
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("KsuCli", "Failed to execute strings command: ${e.message}", e)
|
Log.e("KsuCli", "Failed to execute checks: ${e.message}", e)
|
||||||
}
|
}
|
||||||
if (shouldShowSnackbar) {
|
if (shouldShowSnackbar) {
|
||||||
snackBarHost.showSnackbar(
|
snackBarHost.showSnackbar(
|
||||||
@@ -581,3 +566,40 @@ private fun KpmModuleItem(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun checkStringsCommand(tempFile: File): Int {
|
||||||
|
val command = arrayOf("su", "-c", "strings ${tempFile.absolutePath} | grep -E 'name=|version=|license=|author='")
|
||||||
|
val process = Runtime.getRuntime().exec(command)
|
||||||
|
val inputStream = process.inputStream
|
||||||
|
val reader = java.io.BufferedReader(java.io.InputStreamReader(inputStream))
|
||||||
|
var line: String?
|
||||||
|
var matchCount = 0
|
||||||
|
val keywords = listOf("name=", "version=", "license=", "author=")
|
||||||
|
var nameExists = false
|
||||||
|
|
||||||
|
while (reader.readLine().also { line = it } != null) {
|
||||||
|
if (!nameExists && line!!.startsWith("name=")) {
|
||||||
|
nameExists = true
|
||||||
|
matchCount++
|
||||||
|
} else if (nameExists) {
|
||||||
|
for (keyword in keywords) {
|
||||||
|
if (line!!.startsWith(keyword)) {
|
||||||
|
matchCount++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
process.waitFor()
|
||||||
|
|
||||||
|
return if (nameExists) matchCount else 0
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isElfFile(tempFile: File): Boolean {
|
||||||
|
val elfMagic = byteArrayOf(0x7F, 'E'.code.toByte(), 'L'.code.toByte(), 'F'.code.toByte())
|
||||||
|
val fileBytes = ByteArray(4)
|
||||||
|
FileInputStream(tempFile).use { input ->
|
||||||
|
input.read(fileBytes)
|
||||||
|
}
|
||||||
|
return fileBytes.contentEquals(elfMagic)
|
||||||
|
}
|
||||||
@@ -266,5 +266,4 @@
|
|||||||
<string name="confirm_uninstall_title_with_filename">Uninstall</string>
|
<string name="confirm_uninstall_title_with_filename">Uninstall</string>
|
||||||
<string name="confirm_uninstall_content">The following KPM will be uninstalled: %s</string>
|
<string name="confirm_uninstall_content">The following KPM will be uninstalled: %s</string>
|
||||||
<string name="settings_susfs_toggle_summary">Disable kprobe hooks created by KernelSU, using inline hooks instead, which is similar to non-GKI kernel hooking method.</string>
|
<string name="settings_susfs_toggle_summary">Disable kprobe hooks created by KernelSU, using inline hooks instead, which is similar to non-GKI kernel hooking method.</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user