refactor(boostforreddit): SpoofClientPatch

This commit is contained in:
Pun Butrach
2026-01-12 15:44:53 +07:00
parent 968dfde7a8
commit e2bc428b29
2 changed files with 25 additions and 17 deletions

View File

@@ -1,15 +1,15 @@
package app.revanced.patches.reddit.customclients.boostforreddit.api
import app.revanced.patcher.fingerprint
import app.revanced.patcher.BytecodePatchContextMethodMatching.gettingFirstMutableMethodDeclaratively
import app.revanced.patcher.definingClass
import app.revanced.patcher.name
import app.revanced.patcher.patch.BytecodePatchContext
internal val buildUserAgentFingerprint = fingerprint {
strings("%s:%s:%s (by /u/%s)")
}
internal val getClientIdFingerprint = fingerprint {
custom { method, classDef ->
if (!classDef.endsWith("Credentials;")) return@custom false
method.name == "getClientId"
}
internal val BytecodePatchContext.buildUserAgentMethod by gettingFirstMutableMethodDeclaratively(
"%s:%s:%s (by /u/%s)"
)
internal val BytecodePatchContext.getClientIdMethod by gettingFirstMutableMethodDeclaratively {
name("getClientId")
definingClass { endsWith("Credentials;") }
}

View File

@@ -3,9 +3,14 @@ package app.revanced.patches.reddit.customclients.boostforreddit.api
import app.revanced.patcher.extensions.getInstruction
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patches.reddit.customclients.spoofClientPatch
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.returnEarly
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.StringReference
@Suppress("unused")
val spoofClientPatch = spoofClientPatch(redirectUri = "http://rubenmayayo.com") { clientIdOption ->
compatibleWith("com.rubenmayayo.reddit")
@@ -14,7 +19,7 @@ val spoofClientPatch = spoofClientPatch(redirectUri = "http://rubenmayayo.com")
apply {
// region Patch client id.
getClientIdFingerprint.method.returnEarly(clientId!!)
getClientIdMethod.returnEarly(clientId!!)
// endregion
@@ -23,11 +28,14 @@ val spoofClientPatch = spoofClientPatch(redirectUri = "http://rubenmayayo.com")
// Use a random user agent.
val randomName = (0..100000).random()
val userAgent = "$randomName:app.revanced.$randomName:v1.0.0 (by /u/revanced)"
buildUserAgentFingerprint.let {
val userAgentTemplateIndex = it.stringMatches.first().index
val register = it.method.getInstruction<OneRegisterInstruction>(userAgentTemplateIndex).registerA
it.method.replaceInstruction(userAgentTemplateIndex, "const-string v$register, \"$userAgent\"")
buildUserAgentMethod.apply {
val userAgentTemplateIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.CONST_STRING && getReference<StringReference>()?.string == "%s:%s:%s (by /u/%s)"
}
val register = getInstruction<OneRegisterInstruction>(userAgentTemplateIndex).registerA
replaceInstruction(userAgentTemplateIndex, "const-string v$register, \"$userAgent\"")
}
// endregion