Skip to content

Commit

Permalink
Merge pull request #934 from AudricV/fix-yt-throttling-decryption-fun…
Browse files Browse the repository at this point in the history
…ction-regex

[YouTube] Fix throttling decryption function regex
  • Loading branch information
Stypox authored Sep 25, 2022
2 parents 7244be7 + 8067c43 commit 5c710da
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ public final class YoutubeThrottlingDecrypter {

private static final Pattern N_PARAM_PATTERN = Pattern.compile("[&?]n=([^&]+)");
private static final Pattern DECRYPT_FUNCTION_NAME_PATTERN = Pattern.compile(
"\\.get\\(\"n\"\\)\\)&&\\(b=([a-zA-Z0-9$]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9]\\)");
// CHECKSTYLE:OFF
"\\.get\\(\"n\"\\)\\)&&\\([a-zA-Z0-9$_]=([a-zA-Z0-9$_]+)(?:\\[(\\d+)])?\\([a-zA-Z0-9$_]\\)");
// CHECKSTYLE:ON

// Escape the curly end brace to allow compatibility with Android's regex engine
// See https://stackoverflow.com/q/45074813
@SuppressWarnings("RegExpRedundantEscape")
private static final String DECRYPT_FUNCTION_BODY_REGEX =
"=\\s*function([\\S\\s]*?\\}\\s*return [\\w$]+?\\.join\\(\"\"\\)\\s*\\};)";

private static final Map<String, String> N_PARAMS_CACHE = new HashMap<>();
private static String decryptFunction;
Expand Down Expand Up @@ -128,11 +136,9 @@ private static String parseDecodeFunction(final String playerJsCode, final Strin
@Nonnull
private static String parseWithRegex(final String playerJsCode, final String functionName)
throws Parser.RegexException {
// Escape the curly end brace to allow compatibility with Android's regex engine
// See https://stackoverflow.com/q/45074813
//noinspection RegExpRedundantEscape
final Pattern functionPattern = Pattern.compile(functionName + "=function(.*?\\};)\n",
Pattern.DOTALL);
// Quote the function name, as it may contain special regex characters such as dollar
final Pattern functionPattern = Pattern.compile(
Pattern.quote(functionName) + DECRYPT_FUNCTION_BODY_REGEX, Pattern.DOTALL);
return validateFunction("function "
+ functionName
+ Parser.matchGroup1(functionPattern, playerJsCode));
Expand Down

0 comments on commit 5c710da

Please sign in to comment.