refactor(syncforreddit): SpoofClientPatch

This commit is contained in:
Pun Butrach
2026-01-12 17:01:25 +07:00
parent 357051105c
commit fcce0a6948
2 changed files with 40 additions and 38 deletions

View File

@@ -1,19 +1,12 @@
package app.revanced.patches.reddit.customclients.sync.syncforreddit.api
import app.revanced.patcher.fingerprint
import app.revanced.patcher.BytecodePatchContextMethodMatching.gettingFirstMutableMethodDeclaratively
import app.revanced.patcher.patch.BytecodePatchContext
internal val getAuthorizationStringFingerprint = fingerprint {
strings("authorize.compact?client_id")
}
internal val BytecodePatchContext.getAuthorizationStringMethod by gettingFirstMutableMethodDeclaratively("authorize.compact?client_id")
internal val getBearerTokenFingerprint = fingerprint {
strings("Basic")
}
internal val BytecodePatchContext.getBearerTokenMethod by gettingFirstMutableMethodDeclaratively("Basic")
internal val getUserAgentFingerprint = fingerprint {
strings("android:com.laurencedawson.reddit_sync")
}
internal val BytecodePatchContext.getUserAgentMethod by gettingFirstMutableMethodDeclaratively("android:com.laurencedawson.reddit_sync")
internal val imgurImageAPIFingerprint = fingerprint {
strings("https://imgur-apiv3.p.rapidapi.com/3/image")
}
internal val BytecodePatchContext.imgurImageAPIMethod by gettingFirstMutableMethodDeclaratively("https://imgur-apiv3.p.rapidapi.com/3/image")

View File

@@ -3,19 +3,22 @@ package app.revanced.patches.reddit.customclients.sync.syncforreddit.api
import app.revanced.patcher.extensions.getInstruction
import app.revanced.patcher.extensions.replaceInstruction
import app.revanced.patches.reddit.customclients.`Spoof client`
import app.revanced.patches.reddit.customclients.sync.detection.piracy.disablePiracyDetectionPatch
import app.revanced.patches.reddit.customclients.sync.detection.piracy.`Disable piracy detection`
import app.revanced.patches.shared.misc.string.replaceStringPatch
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.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.reference.StringReference
import java.util.Base64
@Suppress("unused")
val spoofClientPatch = `Spoof client`(
redirectUri = "http://redditsync/auth",
) { clientIdOption ->
dependsOn(
disablePiracyDetectionPatch,
`Disable piracy detection`,
// Redirects from SSL to WWW domain are bugged causing auth problems.
// Manually rewrite the URLs to fix this.
replaceStringPatch("ssl.reddit.com", "www.reddit.com")
@@ -32,28 +35,30 @@ val spoofClientPatch = `Spoof client`(
apply {
// region Patch client id.
getBearerTokenFingerprint.match(getAuthorizationStringFingerprint.originalClassDef).method.apply {
getBearerTokenMethod.apply {
val auth = Base64.getEncoder().encodeToString("$clientId:".toByteArray(Charsets.UTF_8))
returnEarly("Basic $auth")
}
val occurrenceIndex =
getAuthorizationStringFingerprint.stringMatches.first().index
getAuthorizationStringFingerprint.method.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&",
)
replaceInstruction(
occurrenceIndex,
"const-string v$targetRegister, \"$newAuthorizationUrl\"",
)
getAuthorizationStringMethod.apply {
val occurrenceIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.CONST_STRING &&
getReference<StringReference>()?.string?.contains("client_id=") == true
}
val authorizationStringInstruction = getInstruction<OneRegisterInstruction>(occurrenceIndex)
val targetRegister = authorizationStringInstruction.registerA
val reference = authorizationStringInstruction.getReference<StringReference>()!!
val newAuthorizationUrl = reference.string.replace(
"client_id=.*?&".toRegex(),
"client_id=$clientId&",
)
replaceInstruction(
occurrenceIndex,
"const-string v$targetRegister, \"$newAuthorizationUrl\"",
)
}
// endregion
@@ -64,15 +69,19 @@ val spoofClientPatch = `Spoof client`(
val randomName = (0..100000).random()
val userAgent = "$randomName:app.revanced.$randomName:v1.0.0 (by /u/revanced)"
getUserAgentFingerprint.method.returnEarly(userAgent)
getUserAgentMethod.returnEarly(userAgent)
// endregion
// region Patch Imgur API URL.
imgurImageAPIFingerprint.let {
val apiUrlIndex = it.stringMatches.first().index
it.method.replaceInstruction(
imgurImageAPIMethod.apply {
val apiUrlIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.CONST_STRING &&
getReference<StringReference>()?.string == "https://api.imgur.com/3/image"
}
replaceInstruction(
apiUrlIndex,
"const-string v1, \"https://api.imgur.com/3/image\"",
)