YoutubeThrottlingDecrypter: Check if returned string is a valid JavaScript function
This commit is contained in:
parent
ed0a07af4e
commit
9e93d6b193
2 changed files with 26 additions and 4 deletions
|
@ -5,12 +5,13 @@ import org.schabi.newpipe.extractor.utils.JavaScript;
|
||||||
import org.schabi.newpipe.extractor.utils.Parser;
|
import org.schabi.newpipe.extractor.utils.Parser;
|
||||||
import org.schabi.newpipe.extractor.utils.StringUtils;
|
import org.schabi.newpipe.extractor.utils.StringUtils;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* YouTube's streaming URLs of HTML5 clients are protected with a cipher, which modifies their
|
* YouTube's streaming URLs of HTML5 clients are protected with a cipher, which modifies their
|
||||||
* {@code n} query parameter.
|
* {@code n} query parameter.
|
||||||
|
@ -154,8 +155,9 @@ public class YoutubeThrottlingDecrypter {
|
||||||
private static String parseWithParenthesisMatching(final String playerJsCode,
|
private static String parseWithParenthesisMatching(final String playerJsCode,
|
||||||
final String functionName) {
|
final String functionName) {
|
||||||
final String functionBase = functionName + "=function";
|
final String functionBase = functionName + "=function";
|
||||||
return functionBase + StringUtils.matchToClosingParenthesis(playerJsCode, functionBase)
|
return validateFunction(functionBase
|
||||||
+ ";";
|
+ StringUtils.matchToClosingParenthesis(playerJsCode, functionBase)
|
||||||
|
+ ";");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nonnull
|
@Nonnull
|
||||||
|
@ -163,7 +165,15 @@ public class YoutubeThrottlingDecrypter {
|
||||||
throws Parser.RegexException {
|
throws Parser.RegexException {
|
||||||
final Pattern functionPattern = Pattern.compile(functionName + "=function(.*?};)\n",
|
final Pattern functionPattern = Pattern.compile(functionName + "=function(.*?};)\n",
|
||||||
Pattern.DOTALL);
|
Pattern.DOTALL);
|
||||||
return "function " + functionName + Parser.matchGroup1(functionPattern, playerJsCode);
|
return validateFunction("function "
|
||||||
|
+ functionName
|
||||||
|
+ Parser.matchGroup1(functionPattern, playerJsCode));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nonnull
|
||||||
|
private static String validateFunction(@Nonnull final String function) {
|
||||||
|
JavaScript.compileOrThrow(function);
|
||||||
|
return function;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
|
|
|
@ -9,6 +9,18 @@ public final class JavaScript {
|
||||||
private JavaScript() {
|
private JavaScript() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void compileOrThrow(final String function) {
|
||||||
|
try {
|
||||||
|
final Context context = Context.enter();
|
||||||
|
context.setOptimizationLevel(-1);
|
||||||
|
|
||||||
|
// If it doesn't compile it throws an exception here
|
||||||
|
context.compileString(function, null, 1, null);
|
||||||
|
} finally {
|
||||||
|
Context.exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static String run(final String function,
|
public static String run(final String function,
|
||||||
final String functionName,
|
final String functionName,
|
||||||
final String... parameters) {
|
final String... parameters) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue