Skip to content

Commit

Permalink
Remove short-circuiting from n function.
Browse files Browse the repository at this point in the history
  • Loading branch information
devoxin committed Dec 10, 2024
1 parent 0f88619 commit 1f18b75
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ public URI resolveFormatUrl(@NotNull HttpInterface httpInterface, @NotNull Strin

if (!DataFormatTools.isNullOrEmpty(nParameter)) {
try {
uri.setParameter("n", cipher.transform(nParameter, scriptEngine));
String transformed = cipher.transform(nParameter, scriptEngine);

if (nParameter.equals(transformed)) {
log.warn("Transformed n parameter is the same as input, n function possibly short-circuited (in: {}, out: {}, player script: {}, source version: {}",
nParameter, transformed, playerScript, YoutubeSource.VERSION);
}

uri.setParameter("n", transformed);
} catch (ScriptException | NoSuchMethodException e) {
// URLs can still be played without a resolved n parameter. It just means they're
// throttled. But we shouldn't throw an exception anyway as it's not really fatal.
Expand Down Expand Up @@ -267,7 +274,12 @@ private SignatureCipher extractFromScript(@NotNull String script, @NotNull Strin
throw new IllegalStateException("Must find n function from script: " + sourceUrl);
}

SignatureCipher cipherKey = new SignatureCipher(nFunctionMatcher.group(0), scriptTimestamp.group(2), script);
String nFunction = nFunctionMatcher.group(0);
String nfParameterName = DataFormatTools.extractBetween(nFunction, "(", ")");
// remove short-circuit that prevents n challenge transformation
nFunction = nFunction.replaceAll("if\\s*\\(typeof\\s*\\w+\\s*===?.*?\\)return " + nfParameterName + "\\s*;?", "");

SignatureCipher cipherKey = new SignatureCipher(nFunction, scriptTimestamp.group(2), script);

while (matcher.find()) {
String type = matcher.group(1);
Expand Down

0 comments on commit 1f18b75

Please sign in to comment.