Merge pull request #41 from WBRK-dev/main

fix: megacloud extractor grabbing correct key
This commit is contained in:
Ritesh Ghosh
2024-04-21 20:04:06 +05:30
committed by GitHub
+45 -64
View File
@@ -102,7 +102,11 @@ class MegaCloud {
); );
} }
const vars = this.extractVariables(text, "MEGACLOUD"); const vars = this.extractVariables(text);
if (!vars.length) {
throw new Error("Can't find variables. Perhaps the extractor is outdated.");
}
const { secret, encryptedSource } = this.getSecret( const { secret, encryptedSource } = this.getSecret(
encryptedString as string, encryptedString as string,
vars vars
@@ -128,78 +132,43 @@ class MegaCloud {
} }
} }
extractVariables(text: string, sourceName: string) { extractVariables(text: string) {
// extract needed variables // copied from github issue #30 'https://github.com/ghoshRitesh12/aniwatch-api/issues/30'
let allvars; const regex =
if (sourceName !== "MEGACLOUD") { /case\s*0x[0-9a-f]+:(?![^;]*=partKey)\s*\w+\s*=\s*(\w+)\s*,\s*\w+\s*=\s*(\w+);/g;
allvars = const matches = text.matchAll(regex);
text const vars = Array.from(matches, (match) => {
.match( const matchKey1 = this.matchingKey(match[1], text);
/const (?:\w{1,2}=(?:'.{0,50}?'|\w{1,2}\(.{0,20}?\)).{0,20}?,){7}.+?;/gm const matchKey2 = this.matchingKey(match[2], text);
) try {
?.at(-1) ?? ""; return [parseInt(matchKey1, 16), parseInt(matchKey2, 16)];
} else { } catch (e) {
allvars = return [];
text }
.match(/const \w{1,2}=new URLSearchParams.+?;(?=function)/gm) }).filter((pair) => pair.length > 0);
?.at(-1) ?? "";
}
// and convert their values into an array of numbers
const vars = allvars
.slice(0, -1)
.split("=")
.slice(1)
.map((pair) => Number(pair.split(",").at(0)))
.filter((num) => num === 0 || num);
return vars; return vars;
} }
getSecret(encryptedString: string, values: number[]) { getSecret(encryptedString: string, values: number[][]) {
let secret = "", let secret = "",
encryptedSource = encryptedString, encryptedSource = "",
totalInc = 0; encryptedSourceArray = encryptedString.split(""),
currentIndex = 0;
for (let i = 0; i < values[0]!; i++) { for (const index of values) {
let start, inc; const start = index[0] + currentIndex;
switch (i) { const end = start + index[1];
case 0:
(start = values[2]), (inc = values[1]); for (let i = start; i < end; i++) {
break; secret += encryptedString[i];
case 1: encryptedSourceArray[i] = "";
(start = values[4]), (inc = values[3]);
break;
case 2:
(start = values[6]), (inc = values[5]);
break;
case 3:
(start = values[8]), (inc = values[7]);
break;
case 4:
(start = values[10]), (inc = values[9]);
break;
case 5:
(start = values[12]), (inc = values[11]);
break;
case 6:
(start = values[14]), (inc = values[13]);
break;
case 7:
(start = values[16]), (inc = values[15]);
break;
case 8:
(start = values[18]), (inc = values[17]);
} }
const from = start! + totalInc, currentIndex += index[1];
to = from + inc!;
(secret += encryptedString.slice(from, to)),
(encryptedSource = encryptedSource.replace(
encryptedString.substring(from, to),
""
)),
(totalInc += inc!);
} }
encryptedSource = encryptedSourceArray.join("");
return { secret, encryptedSource }; return { secret, encryptedSource };
} }
@@ -240,6 +209,18 @@ class MegaCloud {
return decrypted; return decrypted;
} }
// function copied from github issue #30 'https://github.com/ghoshRitesh12/aniwatch-api/issues/30'
matchingKey(value: string, script: string) {
const regex = new RegExp(`,${value}=((?:0x)?([0-9a-fA-F]+))`);
const match = script.match(regex);
if (match) {
return match[1].replace(/^0x/, "");
} else {
throw new Error("Failed to match the key");
}
}
} }
export default MegaCloud; export default MegaCloud;