aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRitesh Ghosh <[email protected]>2024-04-21 20:04:06 +0530
committerGitHub <[email protected]>2024-04-21 20:04:06 +0530
commita2c10d3f2b7a1308c1947cac7a3dded51ed04b33 (patch)
treeed475bbb4f0cfd5d50064fc9d6011cfdd18af1c2 /src
parent47e104438ae4799436105c357ef0b7213bfd8f62 (diff)
parent33bdd5adcd7c43925b9a6250d958060fd89a7dd1 (diff)
downloadaniwatch-api-a2c10d3f2b7a1308c1947cac7a3dded51ed04b33.tar.xz
aniwatch-api-a2c10d3f2b7a1308c1947cac7a3dded51ed04b33.zip
Merge pull request #41 from WBRK-dev/main
fix: megacloud extractor grabbing correct key
Diffstat (limited to 'src')
-rw-r--r--src/extractors/megacloud.ts111
1 files changed, 46 insertions, 65 deletions
diff --git a/src/extractors/megacloud.ts b/src/extractors/megacloud.ts
index 88b6d94..1ab74b5 100644
--- a/src/extractors/megacloud.ts
+++ b/src/extractors/megacloud.ts
@@ -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;
-
- 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]);
+ encryptedSource = "",
+ encryptedSourceArray = encryptedString.split(""),
+ currentIndex = 0;
+
+ 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;