Skip to content

Commit

Permalink
feat(infinityforreddit): add change-oauth-client-id patch (ReVanced…
Browse files Browse the repository at this point in the history
…#2452)

Co-authored-by: oSumAtrIX <[email protected]>
  • Loading branch information
johnconner122 and oSumAtrIX authored Jun 24, 2023
1 parent 8a6c5c3 commit 9efd790
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package app.revanced.patches.reddit.customclients

import android.os.Environment
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprintResult
import app.revanced.patcher.patch.*
import java.io.File

abstract class AbstractChangeOAuthClientIdPatch(
private val redirectUri: String,
private val options: ChangeOAuthClientIdOptionsContainer,
private val fingerprint: MethodFingerprint,
) : BytecodePatch(listOf(fingerprint)) {
private val fingerprints: List<MethodFingerprint>
) : BytecodePatch(fingerprints) {
override fun execute(context: BytecodeContext): PatchResult {
if (options.clientId == null) {
// Test if on Android
// Ensure device runs Android.
try {
Class.forName("android.os.Environment")
} catch (e: ClassNotFoundException) {
Expand All @@ -36,10 +38,10 @@ abstract class AbstractChangeOAuthClientIdPatch(
}.let { options.clientId = it.readText().trim() }
}

return fingerprint.patch(context)
return fingerprints.map { it.result ?: throw it.toErrorResult() }.patch(context)
}

abstract fun MethodFingerprint.patch(context: BytecodeContext): PatchResult
abstract fun List<MethodFingerprintResult>.patch(context: BytecodeContext): PatchResult

companion object Options {
open class ChangeOAuthClientIdOptionsContainer : OptionsContainer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package app.revanced.patches.reddit.customclients.boostforreddit.api.patch

import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.*
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprintResult
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.Patch
Expand All @@ -19,16 +18,18 @@ import app.revanced.patches.reddit.customclients.boostforreddit.api.fingerprints
class ChangeOAuthClientIdPatch : AbstractChangeOAuthClientIdPatch(
"http://rubenmayayo.com",
Options,
GetClientIdFingerprint
listOf(GetClientIdFingerprint)
) {
override fun MethodFingerprint.patch(context: BytecodeContext): PatchResult {
result?.mutableMethod?.addInstructions(
0,
"""
const-string v0, "$clientId"
return-object v0
override fun List<MethodFingerprintResult>.patch(context: BytecodeContext): PatchResult {
forEach {
it.mutableMethod.addInstructions(
0,
"""
) ?: return toErrorResult()
const-string v0, "$clientId"
return-object v0
"""
)
}

return PatchResultSuccess()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package app.revanced.patches.reddit.customclients.infinityforreddit.api.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint

abstract class AbstractClientIdFingerprint(classTypeSuffix: String, methodName: String) : MethodFingerprint(
strings = listOf("NOe2iKrPPzwscA"),
customFingerprint = custom@{ methodDef, classDef ->
if (!classDef.type.endsWith(classTypeSuffix)) return@custom false

methodDef.name == methodName
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app.revanced.patches.reddit.customclients.infinityforreddit.api.fingerprints

object GetHTTPBasicAuthHeaderFingerprint : AbstractClientIdFingerprint(
"APIUtils;",
"getHTTPBasicAuthHeader"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package app.revanced.patches.reddit.customclients.infinityforreddit.api.fingerprints

object LoginActivityOnCreateFingerprint : AbstractClientIdFingerprint(
"LoginActivity;",
"onCreate"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package app.revanced.patches.reddit.customclients.infinityforreddit.api.patch

import app.revanced.patcher.annotation.*
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprintResult
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.reddit.customclients.AbstractChangeOAuthClientIdPatch
import app.revanced.patches.reddit.customclients.infinityforreddit.api.fingerprints.GetHTTPBasicAuthHeaderFingerprint
import app.revanced.patches.reddit.customclients.infinityforreddit.api.fingerprints.LoginActivityOnCreateFingerprint
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction

@Patch
@Name("change-oauth-client-id")
@Description("Changes the OAuth client ID.")
@Compatibility([Package("ml.docilealligator.infinityforreddit")])
@Version("0.0.1")
class ChangeOAuthClientIdPatch : AbstractChangeOAuthClientIdPatch(
"infinity://localhost",
Options,
listOf(GetHTTPBasicAuthHeaderFingerprint, LoginActivityOnCreateFingerprint)
) {
override fun List<MethodFingerprintResult>.patch(context: BytecodeContext): PatchResult {
forEach {
val clientIdIndex = it.scanResult.stringsScanResult!!.matches.first().index
it.mutableMethod.apply {
val oAuthClientIdRegister = getInstruction<OneRegisterInstruction>(clientIdIndex).registerA

replaceInstruction(
clientIdIndex,
"const-string v$oAuthClientIdRegister, \"$clientId\""
)
}
}

return PatchResultSuccess()
}

companion object Options : AbstractChangeOAuthClientIdPatch.Options.ChangeOAuthClientIdOptionsContainer()
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,41 @@ import java.util.*
class ChangeOAuthClientIdPatch : AbstractChangeOAuthClientIdPatch(
"http://redditsync/auth",
Options,
GetAuthorizationStringFingerprint,
listOf(GetAuthorizationStringFingerprint)
) {
override fun MethodFingerprint.patch(context: BytecodeContext): PatchResult {
result?.also { result ->
GetBearerTokenFingerprint.also { it.resolve(context, result.classDef) }.result?.mutableMethod?.apply {
val auth = Base64.getEncoder().encodeToString("$clientId:".toByteArray(Charsets.UTF_8))
addInstructions(
0,
"""
override fun List<MethodFingerprint>.patch(context: BytecodeContext): PatchResult {
map { it.result ?: return it.toErrorResult() }.forEach { fingerprintResult ->
fingerprintResult.also { result ->
GetBearerTokenFingerprint.also { it.resolve(context, result.classDef) }.result?.mutableMethod?.apply {
val auth = Base64.getEncoder().encodeToString("$clientId:".toByteArray(Charsets.UTF_8))
addInstructions(
0,
"""
const-string v0, "Basic $auth"
return-object v0
"""
)
} ?: return GetBearerTokenFingerprint.toErrorResult()
}?.let {
val occurrenceIndex = it.scanResult.stringsScanResult!!.matches.first().index
)
} ?: return GetBearerTokenFingerprint.toErrorResult()
}.let {
val occurrenceIndex = it.scanResult.stringsScanResult!!.matches.first().index

it.mutableMethod.apply {
val authorizationStringInstruction = getInstruction<ReferenceInstruction>(occurrenceIndex)
val targetRegister = (authorizationStringInstruction as OneRegisterInstruction).registerA
val reference = authorizationStringInstruction.reference as StringReference
it.mutableMethod.apply {
val authorizationStringInstruction = getInstruction<ReferenceInstruction>(occurrenceIndex)
val targetRegister = (authorizationStringInstruction as OneRegisterInstruction).registerA
val reference = authorizationStringInstruction.reference as StringReference

val newAuthorizationUrl = reference.string.replace(
"client_id=.*?&".toRegex(),
"client_id=$clientId&"
)
val newAuthorizationUrl = reference.string.replace(
"client_id=.*?&".toRegex(),
"client_id=$clientId&"
)

replaceInstruction(
occurrenceIndex,
"const-string v$targetRegister, \"$newAuthorizationUrl\""
)
replaceInstruction(
occurrenceIndex,
"const-string v$targetRegister, \"$newAuthorizationUrl\""
)
}
}
} ?: return toErrorResult()
}

return PatchResultSuccess()
}
Expand Down

0 comments on commit 9efd790

Please sign in to comment.