forked from ReVanced/revanced-patches-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(boostforreddit): add
change-oauth-client-id
patch
- Loading branch information
Showing
11 changed files
with
179 additions
and
108 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...main/kotlin/app/revanced/patches/reddit/customclients/AbstractChangeOAuthClientIdPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package app.revanced.patches.reddit.customclients | ||
|
||
import android.os.Environment | ||
import app.revanced.patcher.data.BytecodeContext | ||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
import app.revanced.patcher.patch.* | ||
import app.revanced.patches.reddit.customclients.boostforreddit.api.patch.ChangeOAuthClientIdPatch | ||
import app.revanced.patches.reddit.customclients.syncforreddit.api.patch.ChangeOAuthClientIdPatch.Companion.clientId | ||
import java.io.File | ||
|
||
abstract class AbstractChangeOAuthClientIdPatch( | ||
private val redirectUri: String, | ||
private val fingerprint: MethodFingerprint, | ||
) : BytecodePatch(listOf(fingerprint)) { | ||
override fun execute(context: BytecodeContext): PatchResult { | ||
if (ChangeOAuthClientIdPatch.clientId == null) { | ||
// Test if on Android | ||
try { | ||
Class.forName("android.os.Environment") | ||
} catch (e: ClassNotFoundException) { | ||
return PatchResultError("No client ID provided") | ||
} | ||
|
||
File(Environment.getExternalStorageDirectory(), "reddit_client_id_revanced.txt").also { | ||
if (it.exists()) return@also | ||
|
||
val error = """ | ||
In order to use this patch, you need to provide a client ID. | ||
You can do this by creating a file at ${it.absolutePath} with the client ID as its content. | ||
Alternatively, you can provide the client ID using patch options. | ||
You can get your client ID from https://www.reddit.com/prefs/apps. | ||
The application type has to be "installed app" and the redirect URI has to be set to "$redirectUri" | ||
""".trimIndent() | ||
|
||
return PatchResultError(error) | ||
}.let { clientId = it.readText().trim() } | ||
} | ||
|
||
return fingerprint.patch(context) | ||
} | ||
|
||
abstract fun MethodFingerprint.patch(context: BytecodeContext): PatchResult | ||
|
||
companion object Options { | ||
open class ChangeOAuthClientIdOptionsContainer : OptionsContainer() { | ||
var clientId by option( | ||
PatchOption.StringOption( | ||
"client-id", | ||
null, | ||
"OAuth client ID", | ||
"The Reddit OAuth client ID." | ||
) | ||
) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ed/patches/reddit/customclients/boostforreddit/api/fingerprints/GetClientIdFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package app.revanced.patches.reddit.customclients.boostforreddit.api.fingerprints | ||
|
||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
|
||
object GetClientIdFingerprint : MethodFingerprint( | ||
customFingerprint = custom@{ methodDef, classDef -> | ||
if (!classDef.type.endsWith("Credentials;")) return@custom false | ||
|
||
methodDef.name == "getClientId" | ||
} | ||
) |
36 changes: 36 additions & 0 deletions
36
...evanced/patches/reddit/customclients/boostforreddit/api/patch/ChangeOAuthClientIdPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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.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.boostforreddit.api.fingerprints.GetClientIdFingerprint | ||
|
||
@Patch | ||
@Name("change-oauth-client-id") | ||
@Description("Changes the OAuth client ID in Boost for Reddit.") | ||
@Compatibility([Package("com.rubenmayayo.reddit")]) | ||
@Version("0.0.1") | ||
class ChangeOAuthClientIdPatch : AbstractChangeOAuthClientIdPatch( | ||
"http://rubenmayayo.com", | ||
GetClientIdFingerprint | ||
) { | ||
override fun MethodFingerprint.patch(context: BytecodeContext): PatchResult { | ||
result?.mutableMethod?.addInstructions( | ||
0, | ||
""" | ||
const-string v0, "$clientId" | ||
return-object v0 | ||
""" | ||
) ?: return toErrorResult() | ||
|
||
return PatchResultSuccess() | ||
} | ||
|
||
companion object : Options.ChangeOAuthClientIdOptionsContainer() | ||
} |
2 changes: 1 addition & 1 deletion
2
...s/fingerprints/IsAdsEnabledFingerprint.kt → ...s/fingerprints/IsAdsEnabledFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ints/GetAuthorizationStringFingerprint.kt → ...ints/GetAuthorizationStringFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...fingerprints/GetBearerTokenFingerprint.kt → ...fingerprints/GetBearerTokenFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...revanced/patches/reddit/customclients/syncforreddit/api/patch/ChangeOAuthClientIdPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package app.revanced.patches.reddit.customclients.syncforreddit.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.extensions.InstructionExtensions.getInstruction | ||
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction | ||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve | ||
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.syncforreddit.api.fingerprints.GetAuthorizationStringFingerprint | ||
import app.revanced.patches.reddit.customclients.syncforreddit.api.fingerprints.GetBearerTokenFingerprint | ||
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction | ||
import org.jf.dexlib2.iface.instruction.ReferenceInstruction | ||
import org.jf.dexlib2.iface.reference.StringReference | ||
import java.util.* | ||
|
||
|
||
@Patch | ||
@Name("change-oauth-client-id") | ||
@Description("Changes the OAuth client ID.") | ||
@Compatibility([Package("com.laurencedawson.reddit_sync"), Package("com.laurencedawson.reddit_sync.pro")]) | ||
@Version("0.0.1") | ||
class ChangeOAuthClientIdPatch : AbstractChangeOAuthClientIdPatch( | ||
"http://redditsync/auth", | ||
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, | ||
""" | ||
const-string v0, "Basic $auth" | ||
return-object v0 | ||
""" | ||
) | ||
} ?: 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 | ||
|
||
val newAuthorizationUrl = reference.string.replace( | ||
"client_id=.*?&".toRegex(), | ||
"client_id=${clientId!!}&" | ||
) | ||
|
||
replaceInstruction( | ||
occurrenceIndex, | ||
"const-string v$targetRegister, \"$newAuthorizationUrl\"" | ||
) | ||
} | ||
} ?: return toErrorResult() | ||
|
||
return PatchResultSuccess() | ||
} | ||
|
||
companion object : Options.ChangeOAuthClientIdOptionsContainer() | ||
} |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...racy/patch/DisablePiracyDetectionPatch.kt → ...racy/patch/DisablePiracyDetectionPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 0 additions & 101 deletions
101
src/main/kotlin/app/revanced/patches/syncforreddit/api/patch/ChangeOAuthClientIdPatch.kt
This file was deleted.
Oops, something went wrong.