From 0f567cc39abdc675ff0b5eafb5f0f1209b8a82f8 Mon Sep 17 00:00:00 2001 From: Toastie Date: Sat, 7 Sep 2024 15:13:13 +1200 Subject: [PATCH] Added files. --- .gitignore | 2 + package-lock.json | 265 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 24 +++++ src/index.ts | 136 ++++++++++++++++++++++++ src/template.js | 15 +++ tsconfig.json | 34 ++++++ 6 files changed, 476 insertions(+) create mode 100644 .gitignore create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 src/index.ts create mode 100644 src/template.js create mode 100644 tsconfig.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..76add87 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +dist \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..9e3a2fe --- /dev/null +++ b/package-lock.json @@ -0,0 +1,265 @@ +{ + "name": "vc-routes", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "vc-routes", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "astring": "^1.8.6", + "estree-toolkit": "^1.7.5", + "meriyah": "^4.5.0", + "node-fetch": "^3.3.2", + "typescript": "^4.9.5" + }, + "devDependencies": { + "@types/node-fetch": "^2.6.11" + } + }, + "node_modules/@types/estree": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", + "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==", + "license": "MIT" + }, + "node_modules/@types/estree-jsx": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/@types/estree-jsx/-/estree-jsx-1.0.5.tgz", + "integrity": "sha512-52CcUVNFyfb1A2ALocQw/Dd1BQFNmSdkuC3BkZ6iqhdMfQz7JWOFRuJFloOzjk+6WijU56m9oKXFAXc7o3Towg==", + "license": "MIT", + "dependencies": { + "@types/estree": "*" + } + }, + "node_modules/@types/node": { + "version": "22.5.4", + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.5.4.tgz", + "integrity": "sha512-FDuKUJQm/ju9fT/SeX/6+gBzoPzlVCzfzmGkwKvRHQVxi4BntVbyIwf6a4Xn62mrvndLiml6z/UBXIdEVjQLXg==", + "dev": true, + "license": "MIT", + "dependencies": { + "undici-types": "~6.19.2" + } + }, + "node_modules/@types/node-fetch": { + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.11.tgz", + "integrity": "sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*", + "form-data": "^4.0.0" + } + }, + "node_modules/astring": { + "version": "1.9.0", + "resolved": "https://registry.npmjs.org/astring/-/astring-1.9.0.tgz", + "integrity": "sha512-LElXdjswlqjWrPpJFg1Fx4wpkOCxj1TDHlSV4PlaRxHGWko024xICaa97ZkMfs6DRKlCguiAI+rbXv5GWwXIkg==", + "license": "MIT", + "bin": { + "astring": "bin/astring" + } + }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true, + "license": "MIT" + }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "license": "MIT", + "engines": { + "node": ">= 12" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/estree-toolkit": { + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/estree-toolkit/-/estree-toolkit-1.7.7.tgz", + "integrity": "sha512-HBbdqx4TB7uQM+c4NeFXc2/qOc3VqRkyTOdCn4ukyruVEbqDxT4M/3Hce1C8D4SACqWwHNhQdeM8sdBZmDNaAA==", + "license": "MIT", + "dependencies": { + "@types/estree": "^1.0.5", + "@types/estree-jsx": "^1.0.5" + } + }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dev": true, + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "license": "MIT", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, + "node_modules/meriyah": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/meriyah/-/meriyah-4.5.0.tgz", + "integrity": "sha512-Rbiu0QPIxTXgOXwiIpRVJfZRQ2FWyfzYrOGBs9SN5RbaXg1CN5ELn/plodwWwluX93yzc4qO/bNIen1ThGFCxw==", + "license": "ISC", + "engines": { + "node": ">=10.4.0" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dev": true, + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/node-domexception": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", + "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "github", + "url": "https://paypal.me/jimmywarting" + } + ], + "license": "MIT", + "engines": { + "node": ">=10.5.0" + } + }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "license": "MIT", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, + "node_modules/typescript": { + "version": "4.9.5", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.9.5.tgz", + "integrity": "sha512-1FXk9E2Hm+QzZQ7z+McJiHL4NW1F2EzMu9Nq9i3zAaGqibafqYwCVU6WyWAuyQRRzOlxou8xZSyXLEN8oKj24g==", + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, + "node_modules/undici-types": { + "version": "6.19.8", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz", + "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", + "dev": true, + "license": "MIT" + }, + "node_modules/web-streams-polyfill": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz", + "integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==", + "license": "MIT", + "engines": { + "node": ">= 8" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c5a298d --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "vc-routes", + "version": "1.0.0", + "description": "", + "main": "dist/index.js", + "scripts": { + "build": "tsc -b", + "watch": "tsc -b -w", + "start": "node dist/index.js" + }, + "author": "", + "license": "ISC", + "dependencies": { + "astring": "^1.8.6", + "estree-toolkit": "^1.7.5", + "meriyah": "^4.5.0", + "node-fetch": "^3.3.2", + "typescript": "^4.9.5" + }, + "type": "module", + "devDependencies": { + "@types/node-fetch": "^2.6.11" + } +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..4713111 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,136 @@ +import { generate } from "astring"; +import ESTree from "estree"; +import { traverse } from "estree-toolkit"; +import fs from "fs"; +import * as meriyah from "meriyah"; +import fetch from "node-fetch"; + +const JAVASCRIPT_ENVIRONMENT = fs.readFileSync("./src/template.js").toString(); + +// Not used in the client +const ignoredAdditional: Set = new Set([]); + +const getClientSource = async () => { + const index = await fetch("https://canary.discord.com/app").then(x => x.text()); + const script = [...index.matchAll(/web.[A-Fa-f0-9]{20}.js/g)].reverse()[0]; + + return fetch(`https://canary.discord.com/assets/${script[0]}`).then((x) => + x.text() + ); +}; + +const MeriyahOptions: meriyah.Options = { + webcompat: true, +}; + +const findClientRoutes = (source: string): string[] => { + const tree = meriyah.parseModule(source, MeriyahOptions) as ESTree.Node; + + let called = 0; + let out = ""; + traverse(tree, { + $: { scope: true }, + CallExpression: { + enter: (path) => { + const node = path.node; + if (!node) return; + if ( + node.type == "CallExpression" && + node.callee.type == "MemberExpression" && + node.callee.object.type == "Identifier" && + node.callee.object.name == "Object" && + node.callee.property.type == "Identifier" && + node.callee.property.name == "freeze" && + node.arguments.length == 1 && + node.arguments[0].type == "ObjectExpression" && + node.arguments[0].properties.find( + (x) => + x.type == "Property" && + x.key.type == "Identifier" && + x.key.name == "USER" && + x.value.type == "ArrowFunctionExpression" + ) + ) { + called++; + + // this is our routes list + const generated = generate(node); + out = JAVASCRIPT_ENVIRONMENT.replace("undefined;// --- GENERATED_CODE_MARKER ---", generated); + } + }, + }, + }); + + if (called > 1) { + console.warn("\nWarning: More than one result was found while traversing the AST. This will probably break the output.\n"); + } + + out = out.replaceAll(/..\.JOIN/g, `"JOIN"`); + out = out.replaceAll(/.\..\.DEVICE_CODE/g, `"device_code"`); + + return eval(out); +}; + +const getDcOpenAPI = async () => { + return fetch( + "https://github.com/discord/discord-api-spec/raw/main/specs/openapi_preview.json" + ) + .then((x) => x.json()) + .then((x: any) => Object.keys(x.paths).map((y) => y.replace(/\/$/, "").replace(/{\w+?}/g, ":id"))); +}; + +const getSbOpenAPI = async () => { + return fetch( + "https://toastielab.dev/ValkyrieChat/Server/src/branch/main/assets/openapi.json" + ) + .then((x) => x.json()) + .then((x: any) => Object.keys(x.paths).map((y) => y.replace(/\/$/, "").replace(/{\w+?}/g, ":id"))); +}; + +const compare = (discord: string[], valkyrie: string[]) => { + const missing = []; + const additional = []; + + for (const route of discord) { + const regex = route.replaceAll("/", "\\/"); + + const found = valkyrie.some((x) => x.match(regex)); + + if (!found) { + missing.push(route); + } + } + + for (const route of valkyrie) { + const regex = route.replaceAll("/", "\\/"); + + const found = discord.some((x) => x.match(regex)); + + if (!found && !ignoredAdditional.has(route)) { + additional.push(route); + } + } + + return [missing, additional]; +}; + +(async () => { + const source = await getClientSource(); + const dcOpenAPI = await getDcOpenAPI(); + const dcRoutes: string[] = Array.from(new Set([...findClientRoutes(source), ...dcOpenAPI])); + + const sbRoutes = await getSbOpenAPI(); + const [missing, additional] = compare(dcRoutes, sbRoutes); + + console.log(`Valkyrie is missing ${missing.length}`); + console.log(`Valkyrie implements ${sbRoutes.length}`); + console.log(`Discord implements ${dcRoutes.length}`); + + fs.writeFileSync("./missing.json", JSON.stringify({ + missing: missing.length, + valkyrie: sbRoutes.length, + discord: dcRoutes.length, + routes: missing.sort(), + additional: additional.sort(), + }, null, 2)); +})(); \ No newline at end of file diff --git a/src/template.js b/src/template.js new file mode 100644 index 0000000..a366ff9 --- /dev/null +++ b/src/template.js @@ -0,0 +1,15 @@ +// Some application ID, was 1096190356233670716 on 20/06/2023 +const Oe = ":id"; + +const window = { + GLOBAL_ENV: { + WEBAPP_ENDPOINT: "webapp://" + } +}; + +const routes = undefined;// --- GENERATED_CODE_MARKER --- + +// we're inside an eval, so this code is what is implicitly returned +Object.values(routes).map((route) => { + return typeof route == "function" ? route(...new Array(20).fill(":id")) : route; +}); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..7e2e44e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,34 @@ +{ + "include": ["./src/**/*.ts"], + "exclude": ["node_modules/**/*"], + "compilerOptions": { + /* Visit https://aka.ms/tsconfig to read more about this file */ + + /* Projects */ + + /* Language and Environment */ + "target": "ES2022" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, + + /* Modules */ + "module": "ES2022" /* Specify what module code is generated. */, + "rootDir": "./src" /* Specify the root folder within your source files. */, + "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */, + + /* JavaScript Support */ + + /* Emit */ + "sourceMap": true /* Create source map files for emitted JavaScript files. */, + // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ + "outDir": "./dist" /* Specify an output folder for all emitted files. */, + + /* Interop Constraints */ + "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */, + "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */, + + /* Type Checking */ + "strict": true /* Enable all strict type-checking options. */, + + /* Completeness */ + "skipLibCheck": true /* Skip type checking all .d.ts files. */ + } +} \ No newline at end of file