Skip to content

Commit

Permalink
fix: deploy to adb
Browse files Browse the repository at this point in the history
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
oSumAtrIX committed May 6, 2022
1 parent 3776ab9 commit f9b987e
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 56 deletions.
31 changes: 19 additions & 12 deletions src/main/kotlin/app/revanced/cli/MainCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@ package app.revanced.cli

import app.revanced.patch.PatchLoader
import app.revanced.patch.Patches
import picocli.CommandLine
import app.revanced.utils.adb.Adb
import picocli.CommandLine.*
import java.io.File

@Command(
name = "ReVanced-CLI", version = ["1.0.0"], mixinStandardHelpOptions = true
)
internal object MainCommand : Runnable {
@Option(names = ["-p", "--patches"], description = ["One or more bundles of patches"])
internal var patchBundles = arrayOf<File>()

@Parameters(
paramLabel = "INCLUDE",
description = ["Which patches to include. If none is specified, all compatible patches will be included"]
)
internal var includedPatches = arrayOf<String>()

@Option(names = ["-c", "--cache"], description = ["Output resource cache directory"], required = true)
@Option(names = ["-p", "--patches"], description = ["One or more bundles of patches"])
internal var patchBundles = arrayOf<File>()

@Option(names = ["-t", "--temp-dir"], description = ["Temporal resource cache directory"], required = true)
internal lateinit var cacheDirectory: String

@Option(names = ["-r", "--resource-patcher"], description = ["Enable patching resources"])
internal var patchResources: Boolean = false

@Option(names = ["-w", "--wipe-after"], description = ["Wipe the temporal directory before exiting the patcher"])
internal var wipe: Boolean = false

@Option(
names = ["-c", "--clean"],
description = ["Clean the temporal resource cache directory. This will be done anyways when running the patcher"]
)
internal var clean: Boolean = false

@Option(names = ["-l", "--list"], description = ["List patches only"])
internal var listOnly: Boolean = false
Expand All @@ -44,7 +46,6 @@ internal object MainCommand : Runnable {
@Option(names = ["-d", "--deploy-on"], description = ["If specified, deploy to adb device with given name"])
internal var deploy: String? = null


override fun run() {
if (listOnly) {
patchBundles.forEach {
Expand All @@ -61,17 +62,23 @@ internal object MainCommand : Runnable {
cacheDirectory,
patchResources
)

Patcher.start(patcher)

if (!wipe) return
File(cacheDirectory).deleteRecursively()
if (clean) {
File(cacheDirectory).deleteRecursively()
}

val outputFile = File(outputPath)

deploy?.let {
Adb(
File(outputPath),
outputFile,
patcher.packageName,
deploy!!
).deploy()
}

if (clean) outputFile.delete()
}
}
53 changes: 31 additions & 22 deletions src/main/kotlin/app/revanced/utils/adb/Adb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,49 @@ internal class Adb(
device = JadbConnection().devices.find { it.serial == deviceName }
?: throw IllegalArgumentException("No such device with name $deviceName")

if (device.run("su -h") == 0)
if (device.run("su -h", false) != 0)
throw IllegalArgumentException("Root required on $deviceName. Deploying failed.")
}

private fun String.replacePlaceholder(): String {
return this.replace(Constants.PLACEHOLDER, packageName)
}

internal fun deploy() {
// create revanced path
device.run(Constants.COMMAND_CREATE_DIR + Constants.PATH_DATA)
device.run("${Constants.COMMAND_CREATE_DIR} ${Constants.PATH_REVANCED}")

// push patched file
device.copy(Constants.PATH_INIT_PUSH, apk)
// install apk
device.run(Constants.COMMAND_INSTALL_APK.replacePlaceholder())

// create mount script
// push mount script
device.createFile(
Constants.PATH_INIT_PUSH,
Constants.CONTENT_MOUNT_SCRIPT.replace(Constants.PLACEHOLDER, packageName)
Constants.CONTENT_MOUNT_SCRIPT.replacePlaceholder()
)
// install mount script
device.run(Constants.COMMAND_INSTALL_MOUNT.replacePlaceholder())

// move the mount script to the revanced path
device.run(Constants.COMMAND_MOVE_MOUNT)
// make the mount script executable
device.run(Constants.COMMAND_CHMOD_MOUNT + Constants.NAME_MOUNT_SCRIPT)
// push umount script
device.createFile(
Constants.PATH_INIT_PUSH,
Constants.CONTENT_UMOUNT_SCRIPT.replacePlaceholder()
)
// install mount script
device.run(Constants.COMMAND_INSTALL_UMOUNT.replacePlaceholder())

// push patched file
device.copy(Constants.PATH_INIT_PUSH, apk)
// move patched file to revanced path
device.run(Constants.COMMAND_MOVE_BASE)
// unmount the apk for sanity
device.run(Constants.PATH_UMOUNT.replacePlaceholder())
// mount the apk
device.run(Constants.PATH_MOUNT.replacePlaceholder())

// kill, mount & run app
device.run(Constants.COMMAND_KILL_APP.replace(Constants.PLACEHOLDER, packageName))
device.run(Constants.COMMAND_MOUNT)
device.run(Constants.COMMAND_RUN_APP.replace(Constants.PLACEHOLDER, packageName))
// relaunch app
device.run(Constants.COMMAND_RESTART.replacePlaceholder())

// log the app
log()

// unmount it, after it closes
device.run(Constants.COMMAND_UNMOUNT.replace(Constants.PLACEHOLDER, packageName))
}

private fun log() {
Expand All @@ -61,16 +70,16 @@ internal class Adb(
ProcessBuilder.Redirect.PIPE
}

val process = device.buildCommand(Constants.COMMAND_LOGCAT.replace(Constants.PLACEHOLDER, packageName))
val process = device.buildCommand(Constants.COMMAND_LOGCAT.replacePlaceholder())
.redirectOutput(pipe)
.redirectError(pipe)
.useExecutor(executor)
.start()

Thread.sleep(250) // give the app some time to start up.
Thread.sleep(500) // give the app some time to start up.
while (true) {
try {
while (device.run(Constants.COMMAND_PID_OF + packageName) == 0) {
while (device.run("${Constants.COMMAND_PID_OF} $packageName") == 0) {
Thread.sleep(1000)
}
break
Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/app/revanced/utils/adb/Commands.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import se.vidstige.jadb.ShellProcessBuilder
import java.io.File

internal fun JadbDevice.buildCommand(command: String, su: Boolean = true): ShellProcessBuilder {
if (su) {
return shellProcessBuilder("su -c \'$command\'")
}

val args = command.split(" ") as ArrayList<String>
val cmd = args.removeFirst()

return shellProcessBuilder(cmd, *args.toTypedArray())
}

internal fun JadbDevice.run(command: String, su: Boolean = true): Int {
return this.buildCommand(command).start().waitFor()
return this.buildCommand(command, su).start().waitFor()
}

internal fun JadbDevice.copy(targetPath: String, file: File) {
push(file, RemoteFile(targetPath))
}

internal fun JadbDevice.createFile(targetFile: String, content: String, su: Boolean = true) {
internal fun JadbDevice.createFile(targetFile: String, content: String) {
push(content.byteInputStream(), System.currentTimeMillis(), 644, RemoteFile(targetFile))
}
63 changes: 43 additions & 20 deletions src/main/kotlin/app/revanced/utils/adb/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
package app.revanced.utils.adb

internal object Constants {
// template placeholder to replace a string in commands
internal const val PLACEHOLDER = "TEMPLATE_PACKAGE_NAME"

internal const val NAME_MOUNT_SCRIPT = "mount.sh"
// utility commands
private const val COMMAND_CHMOD_MOUNT = "chmod +x"
internal const val COMMAND_PID_OF = "pidof -s"
internal const val COMMAND_CREATE_DIR = "mkdir -p"
internal const val COMMAND_LOGCAT = "logcat -c && logcat --pid=$($COMMAND_PID_OF $PLACEHOLDER)"
internal const val COMMAND_RESTART = "monkey -p $PLACEHOLDER 1 && kill ${'$'}($COMMAND_PID_OF $PLACEHOLDER)"

internal const val PATH_DATA = "/data/adb/revanced/"
internal const val PATH_INIT_PUSH = "/sdcard/revanced"
// default mount file name
private const val NAME_MOUNT_SCRIPT = "mount_$PLACEHOLDER.sh"

internal const val COMMAND_PID_OF = "pidof -s "
internal const val COMMAND_CREATE_DIR = "mkdir -p "
internal const val COMMAND_MOVE_BASE = "mv $PATH_INIT_PUSH $PATH_DATA/base.apk"
internal const val COMMAND_MOVE_MOUNT = "mv $PATH_INIT_PUSH $PATH_DATA/$NAME_MOUNT_SCRIPT"
internal const val COMMAND_CHMOD_MOUNT = "chmod +x $PATH_DATA"
internal const val COMMAND_MOUNT = "./$PATH_DATA/$NAME_MOUNT_SCRIPT"
internal const val COMMAND_UNMOUNT = "umount -l $(pm path $PLACEHOLDER | grep base | sed 's/package://g')"
internal const val COMMAND_LOGCAT = "logcat -c && logcat --pid=$($COMMAND_PID_OF $PLACEHOLDER)"
internal const val COMMAND_RUN_APP = "monkey -p $PLACEHOLDER 1"
internal const val COMMAND_KILL_APP = "kill \$($COMMAND_PID_OF $PLACEHOLDER)"
// initial directory to push files to via adb push
internal const val PATH_INIT_PUSH = "/sdcard/revanced.delete"

// revanced path
internal const val PATH_REVANCED = "/data/adb/revanced/"

// revanced apk path
private const val PATH_REVANCED_APP = "$PATH_REVANCED$PLACEHOLDER.apk"

// (un)mount script paths
internal const val PATH_MOUNT = "/data/adb/service.d/$NAME_MOUNT_SCRIPT"
internal const val PATH_UMOUNT = "/data/adb/post-fs-data.d/un$NAME_MOUNT_SCRIPT"

// move to revanced apk path & set permissions
internal const val COMMAND_INSTALL_APK =
"base_path=\"$PATH_REVANCED_APP\" && mv $PATH_INIT_PUSH ${'$'}base_path && chmod 644 ${'$'}base_path && chown system:system ${'$'}base_path && chcon u:object_r:apk_data_file:s0 ${'$'}base_path"

// install mount script & set permissions
internal const val COMMAND_INSTALL_MOUNT = "mv $PATH_INIT_PUSH $PATH_MOUNT && $COMMAND_CHMOD_MOUNT $PATH_MOUNT"

// install umount script & set permissions
internal const val COMMAND_INSTALL_UMOUNT = "mv $PATH_INIT_PUSH $PATH_UMOUNT && $COMMAND_CHMOD_MOUNT $PATH_UMOUNT"

// unmount script
internal val CONTENT_UMOUNT_SCRIPT =
"""
#!/system/bin/sh
while read line; do echo ${'$'}{line} | grep $PLACEHOLDER | awk '{print ${'$'}2}' | xargs umount -l; done< /proc/mounts
""".trimIndent()

// mount script
internal val CONTENT_MOUNT_SCRIPT =
"""
base_path="$PATH_DATA/base.apk"
#!/system/bin/sh
while [ "${'$'}(getprop sys.boot_completed | tr -d '\r')" != "1" ]; do sleep 1; done
base_path="$PATH_REVANCED_APP"
stock_path=${'$'}{ pm path $PLACEHOLDER | grep base | sed 's/package://g' }
umount -l ${'$'}stock_path
rm ${'$'}base_path
mv "$PATH_INIT_PUSH" ${'$'}base_path
chmod 644 ${'$'}base_path
chown system:system ${'$'}base_path
chcon u:object_r:apk_data_file:s0 ${'$'}base_path
mount -o bind ${'$'}base_path ${'$'}stock_path
""".trimIndent()
}

0 comments on commit f9b987e

Please sign in to comment.