diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ipcMain/constants.ts | 35 | ||||
-rw-r--r-- | src/ipcMain/crxToZip.ts | 57 | ||||
-rw-r--r-- | src/ipcMain/extensions.ts | 93 | ||||
-rw-r--r-- | src/ipcMain/index.ts | 16 | ||||
-rw-r--r-- | src/patcher.ts | 7 |
5 files changed, 189 insertions, 19 deletions
diff --git a/src/ipcMain/constants.ts b/src/ipcMain/constants.ts new file mode 100644 index 0000000..18726fc --- /dev/null +++ b/src/ipcMain/constants.ts @@ -0,0 +1,35 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. +*/ + +import { join } from "path"; +import { app } from "electron"; + +export const DATA_DIR = process.env.VENCORD_USER_DATA_DIR ?? ( + process.env.DISCORD_USER_DATA_DIR + ? join(process.env.DISCORD_USER_DATA_DIR, "..", "VencordData") + : join(app.getPath("userData"), "..", "Vencord") +); +export const SETTINGS_DIR = join(DATA_DIR, "settings"); +export const QUICKCSS_PATH = join(SETTINGS_DIR, "quickCss.css"); +export const SETTINGS_FILE = join(SETTINGS_DIR, "settings.json"); +export const ALLOWED_PROTOCOLS = [ + "https:", + "http:", + "steam:", + "spotify:" +]; diff --git a/src/ipcMain/crxToZip.ts b/src/ipcMain/crxToZip.ts new file mode 100644 index 0000000..ca43890 --- /dev/null +++ b/src/ipcMain/crxToZip.ts @@ -0,0 +1,57 @@ +/* eslint-disable header/header */ + +/*! + * crxToZip + * Copyright (c) 2013 Rob Wu <rob@robwu.nl> + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +export function crxToZip(buf: Buffer) { + function calcLength(a: number, b: number, c: number, d: number) { + let length = 0; + + length += a << 0; + length += b << 8; + length += c << 16; + length += d << 24 >>> 0; + return length; + } + + // 50 4b 03 04 + // This is actually a zip file + if (buf[0] === 80 && buf[1] === 75 && buf[2] === 3 && buf[3] === 4) { + return buf; + } + + // 43 72 32 34 (Cr24) + if (buf[0] !== 67 || buf[1] !== 114 || buf[2] !== 50 || buf[3] !== 52) { + throw new Error("Invalid header: Does not start with Cr24"); + } + + // 02 00 00 00 + // or + // 03 00 00 00 + const isV3 = buf[4] === 3; + const isV2 = buf[4] === 2; + + if ((!isV2 && !isV3) || buf[5] || buf[6] || buf[7]) { + throw new Error("Unexpected crx format version number."); + } + + if (isV2) { + const publicKeyLength = calcLength(buf[8], buf[9], buf[10], buf[11]); + const signatureLength = calcLength(buf[12], buf[13], buf[14], buf[15]); + + // 16 = Magic number (4), CRX format version (4), lengths (2x4) + const zipStartOffset = 16 + publicKeyLength + signatureLength; + + return buf.subarray(zipStartOffset, buf.length); + } + // v3 format has header size and then header + const headerSize = calcLength(buf[8], buf[9], buf[10], buf[11]); + const zipStartOffset = 12 + headerSize; + + return buf.subarray(zipStartOffset, buf.length); +} diff --git a/src/ipcMain/extensions.ts b/src/ipcMain/extensions.ts new file mode 100644 index 0000000..9bebdaf --- /dev/null +++ b/src/ipcMain/extensions.ts @@ -0,0 +1,93 @@ +/* + * Vencord, a modification for Discord's desktop app + * Copyright (c) 2022 Vendicated and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. +*/ + +import https from "https"; +import { mkdir, rm, writeFile, access } from "fs/promises"; +import { constants as fsConstants } from "fs"; +import { join } from "path"; +import { DATA_DIR } from "./constants"; +import { unzip } from "fflate"; +import { session } from "electron"; +import { crxToZip } from "./crxToZip"; + +const extensionCacheDir = join(DATA_DIR, "ExtensionCache"); + +function download(url: string) { + return new Promise<Buffer>((resolve, reject) => { + https.get(url, res => { + const { statusCode, statusMessage, headers } = res; + if (statusCode! >= 400) + return void reject(`${statusCode}: ${statusMessage} - ${url}`); + if (statusCode! >= 300) + return void resolve(download(headers.location!)); + + const chunks = [] as Buffer[]; + res.on("error", reject); + + res.on("data", chunk => chunks.push(chunk)); + res.once("end", () => resolve(Buffer.concat(chunks))); + }); + }); +} + +async function extract(data: Buffer, outDir: string) { + await mkdir(outDir, { recursive: true }); + return new Promise<void>((resolve, reject) => { + unzip(data, (err, files) => { + if (err) return void reject(err); + Promise.all(Object.keys(files).map(async f => { + // Signature stuff + // 'Cannot load extension with file or directory name + // _metadata. Filenames starting with "_" are reserved for use by the system.'; + if (f.startsWith("_metadata/")) return; + + if (f.endsWith("/")) return void mkdir(join(outDir, f), { recursive: true }); + + const pathElements = f.split("/"); + const name = pathElements.pop()!; + const directories = pathElements.join("/"); + const dir = join(outDir, directories); + + if (directories) { + await mkdir(dir, { recursive: true }); + } + + await writeFile(join(dir, name), files[f]); + })) + .then(() => resolve()) + .catch(err => { + rm(outDir, { recursive: true, force: true }); + reject(err); + }); + }); + }); +} + +export async function installExt(id: string) { + const extDir = join(extensionCacheDir, `${id}`); + + try { + await access(extDir, fsConstants.F_OK); + } catch (err) { + const url = `https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&x=id%3D${id}%26uc&prodversion=32`; + const buf = await download(url); + await extract(crxToZip(buf), extDir); + } + + session.defaultSession.loadExtension(extDir); +} diff --git a/src/ipcMain/index.ts b/src/ipcMain/index.ts index dc05079..d815142 100644 --- a/src/ipcMain/index.ts +++ b/src/ipcMain/index.ts @@ -26,21 +26,9 @@ import monacoHtml from "@fileContent/../components/monacoWin.html;base64"; import "./updater"; import { Queue } from "../utils/Queue"; +import { QUICKCSS_PATH, ALLOWED_PROTOCOLS, SETTINGS_DIR, SETTINGS_FILE } from "./constants"; + -const DATA_DIR = process.env.VENCORD_USER_DATA_DIR ?? ( - process.env.DISCORD_USER_DATA_DIR - ? join(process.env.DISCORD_USER_DATA_DIR, "..", "VencordData") - : join(app.getPath("userData"), "..", "Vencord") -); -const SETTINGS_DIR = join(DATA_DIR, "settings"); -const QUICKCSS_PATH = join(SETTINGS_DIR, "quickCss.css"); -const SETTINGS_FILE = join(SETTINGS_DIR, "settings.json"); -const ALLOWED_PROTOCOLS = [ - "https:", - "http:", - "steam:", - "spotify:" -]; mkdirSync(SETTINGS_DIR, { recursive: true }); diff --git a/src/patcher.ts b/src/patcher.ts index 06f03d8..eca35f0 100644 --- a/src/patcher.ts +++ b/src/patcher.ts @@ -19,6 +19,7 @@ import electron, { app, BrowserWindowConstructorOptions } from "electron"; import { join } from "path"; import { initIpc } from "./ipcMain"; +import { installExt } from "./ipcMain/extensions"; import { readSettings } from "./ipcMain/index"; console.log("[Vencord] Starting up..."); @@ -68,11 +69,7 @@ electron.app.whenReady().then(() => { try { const settings = JSON.parse(readSettings()); if (settings.enableReactDevtools) - import("electron-devtools-installer") - .then(({ default: inst, REACT_DEVELOPER_TOOLS }) => - // @ts-ignore: cursed fake esm turns it into exports.default.default - (inst.default ?? inst)(REACT_DEVELOPER_TOOLS) - ) + installExt("fmkadmapgofadopljbjfkapdkoienihi") .then(() => console.info("[Vencord] Installed React Developer Tools")) .catch(err => console.error("[Vencord] Failed to install React Developer Tools", err)); } catch { } |