diff options
author | V <vendicated@riseup.net> | 2023-04-04 01:16:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-04 01:16:29 +0200 |
commit | 6b26c12bfa1f28d40834478b50d2f7b09c9f54fb (patch) | |
tree | 48687349f9bf6803ee8993b3eafa3ea1e4f3dff2 /src/main/utils | |
parent | 5bb08bdb6465eebc9dcf80ba2971d894b804abb8 (diff) | |
download | Vencord-6b26c12bfa1f28d40834478b50d2f7b09c9f54fb.tar.gz Vencord-6b26c12bfa1f28d40834478b50d2f7b09c9f54fb.tar.bz2 Vencord-6b26c12bfa1f28d40834478b50d2f7b09c9f54fb.zip |
Add additional build flavours for Vencord Desktop (#765)
Diffstat (limited to 'src/main/utils')
-rw-r--r-- | src/main/utils/constants.ts | 37 | ||||
-rw-r--r-- | src/main/utils/crxToZip.ts | 57 | ||||
-rw-r--r-- | src/main/utils/extensions.ts | 85 | ||||
-rw-r--r-- | src/main/utils/simpleGet.ts | 37 |
4 files changed, 216 insertions, 0 deletions
diff --git a/src/main/utils/constants.ts b/src/main/utils/constants.ts new file mode 100644 index 0000000..cc9f459 --- /dev/null +++ b/src/main/utils/constants.ts @@ -0,0 +1,37 @@ +/* + * 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 { app } from "electron"; +import { join } from "path"; + +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:" +]; + +export const IS_VANILLA = /* @__PURE__ */ process.argv.includes("--vanilla"); diff --git a/src/main/utils/crxToZip.ts b/src/main/utils/crxToZip.ts new file mode 100644 index 0000000..ca43890 --- /dev/null +++ b/src/main/utils/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/main/utils/extensions.ts b/src/main/utils/extensions.ts new file mode 100644 index 0000000..d8f8437 --- /dev/null +++ b/src/main/utils/extensions.ts @@ -0,0 +1,85 @@ +/* + * 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 { session } from "electron"; +import { unzip } from "fflate"; +import { constants as fsConstants } from "fs"; +import { access, mkdir, rm, writeFile } from "fs/promises"; +import { join } from "path"; + +import { DATA_DIR } from "./constants"; +import { crxToZip } from "./crxToZip"; +import { get } from "./simpleGet"; + +const extensionCacheDir = join(DATA_DIR, "ExtensionCache"); + +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 = id === "fmkadmapgofadopljbjfkapdkoienihi" + // React Devtools v4.25 + // v4.27 is broken in Electron, see https://github.com/facebook/react/issues/25843 + // Unfortunately, Google does not serve old versions, so this is the only way + ? "https://raw.githubusercontent.com/Vendicated/random-files/f6f550e4c58ac5f2012095a130406c2ab25b984d/fmkadmapgofadopljbjfkapdkoienihi.zip" + : `https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&x=id%3D${id}%26uc&prodversion=32`; + const buf = await get(url, { + headers: { + "User-Agent": "Vencord (https://github.com/Vendicated/Vencord)" + } + }); + await extract(crxToZip(buf), extDir).catch(console.error); + } + + session.defaultSession.loadExtension(extDir); +} diff --git a/src/main/utils/simpleGet.ts b/src/main/utils/simpleGet.ts new file mode 100644 index 0000000..1a8302c --- /dev/null +++ b/src/main/utils/simpleGet.ts @@ -0,0 +1,37 @@ +/* + * 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"; + +export function get(url: string, options: https.RequestOptions = {}) { + return new Promise<Buffer>((resolve, reject) => { + https.get(url, options, res => { + const { statusCode, statusMessage, headers } = res; + if (statusCode! >= 400) + return void reject(`${statusCode}: ${statusMessage} - ${url}`); + if (statusCode! >= 300) + return void resolve(get(headers.location!, options)); + + const chunks = [] as Buffer[]; + res.on("error", reject); + + res.on("data", chunk => chunks.push(chunk)); + res.once("end", () => resolve(Buffer.concat(chunks))); + }); + }); +} |