Musique/node_modules/ytdl-core/lib/sig.js

122 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2021-12-04 19:00:33 +00:00
const querystring = require('querystring');
const Cache = require('./cache');
const utils = require('./utils');
2022-01-13 12:08:36 +00:00
const vm = require('vm');
2021-12-04 19:00:33 +00:00
2022-01-13 12:08:36 +00:00
// A shared cache to keep track of html5player js functions.
2021-12-04 19:00:33 +00:00
exports.cache = new Cache();
/**
2022-01-13 12:08:36 +00:00
* Extract signature deciphering and n parameter transform functions from html5player file.
2021-12-04 19:00:33 +00:00
*
* @param {string} html5playerfile
* @param {Object} options
* @returns {Promise<Array.<string>>}
*/
2022-01-13 12:08:36 +00:00
exports.getFunctions = (html5playerfile, options) => exports.cache.getOrSet(html5playerfile, async() => {
2021-12-04 19:00:33 +00:00
const body = await utils.exposedMiniget(html5playerfile, options).text();
2022-01-13 12:08:36 +00:00
const functions = exports.extractFunctions(body);
if (!functions || !functions.length) {
throw Error('Could not extract functions');
2021-12-04 19:00:33 +00:00
}
2022-01-13 12:08:36 +00:00
exports.cache.set(html5playerfile, functions);
return functions;
2021-12-04 19:00:33 +00:00
});
/**
2022-01-13 12:08:36 +00:00
* Extracts the actions that should be taken to decipher a signature
* and tranform the n parameter
2021-12-04 19:00:33 +00:00
*
* @param {string} body
* @returns {Array.<string>}
*/
2022-01-13 12:08:36 +00:00
exports.extractFunctions = body => {
const functions = [];
const extractManipulations = caller => {
const functionName = utils.between(caller, `a=a.split("");`, `.`);
if (!functionName) return '';
const functionStart = `var ${functionName}={`;
const ndx = body.indexOf(functionStart);
if (ndx < 0) return '';
const subBody = body.slice(ndx + functionStart.length - 1);
return `var ${functionName}=${utils.cutAfterJSON(subBody)}`;
};
const extractDecipher = () => {
const functionName = utils.between(body, `a.set("alr","yes");c&&(c=`, `(decodeURIC`);
if (functionName && functionName.length) {
const functionStart = `${functionName}=function(a)`;
const ndx = body.indexOf(functionStart);
if (ndx >= 0) {
const subBody = body.slice(ndx + functionStart.length);
let functionBody = `var ${functionStart}${utils.cutAfterJSON(subBody)}`;
functionBody = `${extractManipulations(functionBody)};${functionBody};${functionName}(sig);`;
functions.push(functionBody);
}
2021-12-04 19:00:33 +00:00
}
2022-01-13 12:08:36 +00:00
};
const extractNCode = () => {
const functionName = utils.between(body, `&&(b=a.get("n"))&&(b=`, `(b)`);
if (functionName && functionName.length) {
const functionStart = `${functionName}=function(a)`;
const ndx = body.indexOf(functionStart);
if (ndx >= 0) {
const subBody = body.slice(ndx + functionStart.length);
const functionBody = `var ${functionStart}${utils.cutAfterJSON(subBody)};${functionName}(ncode);`;
functions.push(functionBody);
}
}
};
extractDecipher();
extractNCode();
return functions;
2021-12-04 19:00:33 +00:00
};
/**
2022-01-13 12:08:36 +00:00
* Apply decipher and n-transform to individual format
*
2021-12-04 19:00:33 +00:00
* @param {Object} format
2022-01-13 12:08:36 +00:00
* @param {vm.Script} decipherScript
* @param {vm.Script} nTransformScript
2021-12-04 19:00:33 +00:00
*/
2022-01-13 12:08:36 +00:00
exports.setDownloadURL = (format, decipherScript, nTransformScript) => {
const decipher = url => {
const args = querystring.parse(url);
if (!args.s || !decipherScript) return args.url;
const components = new URL(decodeURIComponent(args.url));
components.searchParams.set(args.sp ? args.sp : 'signature',
decipherScript.runInNewContext({ sig: decodeURIComponent(args.s) }));
return components.toString();
};
const ncode = url => {
const components = new URL(decodeURIComponent(url));
const n = components.searchParams.get('n');
if (!n || !nTransformScript) return url;
components.searchParams.set('n', nTransformScript.runInNewContext({ ncode: n }));
return components.toString();
};
const cipher = !format.url;
const url = format.url || format.signatureCipher || format.cipher;
format.url = cipher ? ncode(decipher(url)) : ncode(url);
delete format.signatureCipher;
delete format.cipher;
2021-12-04 19:00:33 +00:00
};
/**
2022-01-13 12:08:36 +00:00
* Applies decipher and n parameter transforms to all format URL's.
2021-12-04 19:00:33 +00:00
*
* @param {Array.<Object>} formats
* @param {string} html5player
* @param {Object} options
*/
exports.decipherFormats = async(formats, html5player, options) => {
let decipheredFormats = {};
2022-01-13 12:08:36 +00:00
let functions = await exports.getFunctions(html5player, options);
const decipherScript = functions.length ? new vm.Script(functions[0]) : null;
const nTransformScript = functions.length > 1 ? new vm.Script(functions[1]) : null;
2021-12-04 19:00:33 +00:00
formats.forEach(format => {
2022-01-13 12:08:36 +00:00
exports.setDownloadURL(format, decipherScript, nTransformScript);
2021-12-04 19:00:33 +00:00
decipheredFormats[format.url] = format;
});
return decipheredFormats;
};