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