Skip to content

Commit

Permalink
fix: update decipher regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
skick1234 committed Dec 15, 2024
1 parent 0e0dbbc commit 6e95321
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions lib/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ const VARIABLE_PART_DEFINE = `\\"?${VARIABLE_PART}\\"?`;
const BEFORE_ACCESS = '(?:\\[\\"|\\.)';
const AFTER_ACCESS = '(?:\\"\\]|)';
const VARIABLE_PART_ACCESS = BEFORE_ACCESS + VARIABLE_PART + AFTER_ACCESS;
const REVERSE_PART = ":function\\(a\\)\\{(?:return )?a\\.reverse\\(\\)\\}";
const SLICE_PART = ":function\\(a,b\\)\\{return a\\.slice\\(b\\)\\}";
const SPLICE_PART = ":function\\(a,b\\)\\{a\\.splice\\(0,b\\)\\}";
const REVERSE_PART = ":function\\(\\w\\)\\{(?:return )?\\w\\.reverse\\(\\)\\}";
const SLICE_PART = ":function\\(\\w,\\w\\)\\{return \\w\\.slice\\(\\w\\)\\}";
const SPLICE_PART = ":function\\(\\w,\\w\\)\\{\\w\\.splice\\(0,\\w\\)\\}";
const SWAP_PART =
":function\\(a,b\\)\\{" + "var c=a\\[0\\];a\\[0\\]=a\\[b%a\\.length\\];a\\[b(?:%a.length|)\\]=c(?:;return a)?\\}";
":function\\(\\w,\\w\\)\\{var \\w=\\w\\[0\\];\\w\\[0\\]=\\w\\[\\w%\\w\\.length\\];\\w\\[\\w(?:%\\w.length|)\\]=\\w(?:;return \\w)?\\}";

const DECIPHER_REGEXP =
`function(?: ${VARIABLE_PART})?\\(a\\)\\{` +
`a=a\\.split\\(""\\);\\s*` +
`((?:(?:a=)?${VARIABLE_PART}${VARIABLE_PART_ACCESS}\\(a,\\d+\\);)+)` +
`return a\\.join\\(""\\)` +
`function(?: ${VARIABLE_PART})?\\(([a-zA-Z])\\)\\{` +
'\\1=\\1\\.split\\(""\\);\\s*' +
`((?:(?:\\1=)?${VARIABLE_PART}${VARIABLE_PART_ACCESS}\\(\\1,\\d+\\);)+)` +
'return \\1\\.join\\(""\\)' +
`\\}`;

const HELPER_REGEXP = `var (${VARIABLE_PART})=\\{((?:(?:${VARIABLE_PART_DEFINE}${REVERSE_PART}|${
Expand All @@ -57,21 +57,12 @@ const MCR = `${SCVR}+`;
const AAR = "\\[(\\d+)]";
const N_TRANSFORM_NAME_REGEXPS = [
// NewPipeExtractor regexps
`${SCVR}="nn"\\[\\+${MCR}\\.${MCR}],${MCR}\\(${MCR}\\),${MCR}=${MCR}\\.${MCR}\\[${MCR}]\\|\\|null\\).+\\|\\|(${
MCR
})\\(""\\)`,
`${SCVR}="nn"\\[\\+${MCR}\\.${MCR}],${MCR}\\(${MCR}\\),${MCR}=${MCR}\\.${MCR}\\[${MCR}]\\|\\|null\\)&&\\(${MCR}=(${
MCR
})${AAR}`,
`${SCVR}="nn"\\[\\+${MCR}\\.${MCR}],${MCR}\\(${MCR}\\),${MCR}=${MCR}\\.${MCR}\\[${MCR}]\\|\\|null\\).+\\|\\|(${MCR})\\(""\\)`,
`${SCVR}="nn"\\[\\+${MCR}\\.${MCR}],${MCR}\\(${MCR}\\),${MCR}=${MCR}\\.${MCR}\\[${MCR}]\\|\\|null\\)&&\\(${MCR}=(${MCR})${AAR}`,
`${SCVR}="nn"\\[\\+${MCR}\\.${MCR}],${MCR}=${MCR}\\.get\\(${MCR}\\)\\).+\\|\\|(${MCR})\\(""\\)`,
`${SCVR}="nn"\\[\\+${MCR}\\.${MCR}],${MCR}=${MCR}\\.get\\(${MCR}\\)\\)&&\\(${MCR}=(${MCR})\\[(\\d+)]`,
`\\(${SCVR}=String\\.fromCharCode\\(110\\),${SCVR}=${SCVR}\\.get\\(${SCVR}\\)\\)&&\\(${SCVR}=(${MCR})(?:${AAR})?\\(${
SCVR
}\\)`,
`\\(${SCVR}=String\\.fromCharCode\\(110\\),${SCVR}=${SCVR}\\.get\\(${SCVR}\\)\\)&&\\(${SCVR}=(${MCR})(?:${AAR})?\\(${SCVR}\\)`,
`\\.get\\("n"\\)\\)&&\\(${SCVR}=(${MCR})(?:${AAR})?\\(${SCVR}\\)`,
// Skick regexps
'(\\w+).length\\|\\|\\w+\\(""\\)',
'\\w+.length\\|\\|(\\w+)\\(""\\)',
];

// LavaPlayer regexps
Expand All @@ -80,14 +71,15 @@ const N_TRANSFORM_REGEXP =
"var\\s*(\\w+)=(?:\\1\\.split\\(.*?\\)|String\\.prototype\\.split\\.call\\(\\1,.*?\\))," +
"\\s*(\\w+)=(\\[.*?]);\\s*\\3\\[\\d+]" +
"(.*?try)(\\{.*?})catch\\(\\s*(\\w+)\\s*\\)\\s*\\{" +
'\\s*return"enhanced_except_([A-z0-9-]+)"\\s*\\+\\s*\\1\\s*}' +
'\\s*return"[\\w-]+([A-z0-9-]+)"\\s*\\+\\s*\\1\\s*}' +
'\\s*return\\s*(\\2\\.join\\(""\\)|Array\\.prototype\\.join\\.call\\(\\2,.*?\\))};';

const DECIPHER_ARGUMENT = "sig";
const N_ARGUMENT = "ncode";

const matchRegex = (regex, str) => {
const match = str.match(new RegExp(regex, "s"));
console.log(match)
if (!match) throw new Error(`Could not match ${regex}`);
return match;
};
Expand Down Expand Up @@ -116,7 +108,7 @@ const getFuncName = (body, regexps) => {
};

const DECIPHER_FUNC_NAME = "DisTubeDecipherFunc";
const extractDecipherFunc = body => {
const extractDecipherFunc = (exports.d1 = body => {
try {
const helperObject = matchFirst(HELPER_REGEXP, body);
const decipherFunc = matchFirst(DECIPHER_REGEXP, body);
Expand All @@ -126,9 +118,9 @@ const extractDecipherFunc = body => {
} catch (e) {
return null;
}
};
});

const extractDecipherWithName = body => {
const extractDecipherWithName = (exports.d2 = body => {
try {
const decipherFuncName = getFuncName(body, DECIPHER_NAME_REGEXPS);
const funcPattern = `(${decipherFuncName.replace(/\$/g, "\\$")}=function\\([a-zA-Z0-9_]+\\)\\{.+?\\})`;
Expand All @@ -141,7 +133,7 @@ const extractDecipherWithName = body => {
} catch (e) {
return null;
}
};
});

const getExtractFunctions = (extractFunctions, body) => {
for (const extractFunction of extractFunctions) {
Expand Down Expand Up @@ -175,7 +167,7 @@ const extractDecipher = body => {
};

const N_TRANSFORM_FUNC_NAME = "DisTubeNTransformFunc";
const extractNTransformFunc = body => {
const extractNTransformFunc = (exports.n1 = body => {
try {
const nFunc = matchFirst(N_TRANSFORM_REGEXP, body);
const resultFunc = `var ${N_TRANSFORM_FUNC_NAME}=${nFunc}`;
Expand All @@ -184,22 +176,20 @@ const extractNTransformFunc = body => {
} catch (e) {
return null;
}
};
});

const extractNTransformWithName = body => {
const extractNTransformWithName = (exports.n2 = body => {
try {
const nFuncName = getFuncName(body, N_TRANSFORM_NAME_REGEXPS);
const funcPattern = `(${
nFuncName.replace(/\$/g, "\\$")
// eslint-disable-next-line max-len
}=\\s*function([\\S\\s]*?\\}\\s*return (([\\w$]+?\\.join\\(""\\))|(Array\\.prototype\\.join\\.call\\([\\w$]+?,[\\n\\s]*(("")|(\\("",""\\)))\\)))\\s*\\}))`;
console.log(nFuncName);
const funcPattern = `(${nFuncName.replace(/\$/g, "\\$")}=function\\([a-zA-Z0-9_]+\\)\\{.+?\\})`;
const nTransformFunc = `var ${matchGroup1(funcPattern, body)};`;
const callerFunc = `${nFuncName}(${N_ARGUMENT});`;
return nTransformFunc + callerFunc;
} catch (e) {
return null;
}
};
});

let nTransformWarning = false;
const extractNTransform = body => {
Expand Down

0 comments on commit 6e95321

Please sign in to comment.