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(
encryptedString as string,
vars
@@ -128,78 +132,43 @@ class MegaCloud {
}
}
extractVariables(text: string, sourceName: string) {
// extract needed variables
let allvars;
if (sourceName !== "MEGACLOUD") {
allvars =
text
.match(
/const (?:\w{1,2}=(?:'.{0,50}?'|\w{1,2}\(.{0,20}?\)).{0,20}?,){7}.+?;/gm
)
?.at(-1) ?? "";
} else {
allvars =
text
.match(/const \w{1,2}=new URLSearchParams.+?;(?=function)/gm)
?.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);
extractVariables(text: string) {
// copied from github issue #30 'https://github.com/ghoshRitesh12/aniwatch-api/issues/30'
const regex =
/case\s*0x[0-9a-f]+:(?![^;]*=partKey)\s*\w+\s*=\s*(\w+)\s*,\s*\w+\s*=\s*(\w+);/g;
const matches = text.matchAll(regex);
const vars = Array.from(matches, (match) => {
const matchKey1 = this.matchingKey(match[1], text);
const matchKey2 = this.matchingKey(match[2], text);
try {
return [parseInt(matchKey1, 16), parseInt(matchKey2, 16)];
} catch (e) {
return [];
}
}).filter((pair) => pair.length > 0);
return vars;
}
getSecret(encryptedString: string, values: number[]) {
getSecret(encryptedString: string, values: number[][]) {
let secret = "",
encryptedSource = encryptedString,
totalInc = 0;
encryptedSource = "",
encryptedSourceArray = encryptedString.split(""),
currentIndex = 0;
for (let i = 0; i < values[0]!; i++) {
let start, inc;
switch (i) {
case 0:
(start = values[2]), (inc = values[1]);
break;
case 1:
(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]);
for (const index of values) {
const start = index[0] + currentIndex;
const end = start + index[1];
for (let i = start; i < end; i++) {
secret += encryptedString[i];
encryptedSourceArray[i] = "";
}
const from = start! + totalInc,
to = from + inc!;
(secret += encryptedString.slice(from, to)),
(encryptedSource = encryptedSource.replace(
encryptedString.substring(from, to),
""
)),
(totalInc += inc!);
currentIndex += index[1];
}
encryptedSource = encryptedSourceArray.join("");
return { secret, encryptedSource };
}
@@ -240,6 +209,18 @@ class MegaCloud {
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;