Proxy modules object for patching
This commit is contained in:
parent
5f8b96dced
commit
2a77941dff
|
@ -465,6 +465,12 @@ async function runtime(token: string) {
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
// Call the getter for all the values in the modules object
|
||||||
|
// So modules that were not required get patched by our proxy
|
||||||
|
for (const id in wreq!.m) {
|
||||||
|
wreq!.m[id];
|
||||||
|
}
|
||||||
|
|
||||||
console.log("[PUP_DEBUG]", "Finished loading all chunks!");
|
console.log("[PUP_DEBUG]", "Finished loading all chunks!");
|
||||||
|
|
||||||
for (const patch of Vencord.Plugins.patches) {
|
for (const patch of Vencord.Plugins.patches) {
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export const WEBPACK_CHUNK = "webpackChunkdiscord_app";
|
|
||||||
export const REACT_GLOBAL = "Vencord.Webpack.Common.React";
|
export const REACT_GLOBAL = "Vencord.Webpack.Common.React";
|
||||||
export const SUPPORT_CHANNEL_ID = "1026515880080842772";
|
export const SUPPORT_CHANNEL_ID = "1026515880080842772";
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { UNCONFIGURABLE_PROPERTIES } from "./misc";
|
||||||
|
|
||||||
export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
|
export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
|
||||||
let tries = 0;
|
let tries = 0;
|
||||||
let cache: T;
|
let cache: T;
|
||||||
|
@ -29,10 +31,6 @@ export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Proxies demand that these properties be unmodified, so proxyLazy
|
|
||||||
// will always return the function default for them.
|
|
||||||
const unconfigurable = ["arguments", "caller", "prototype"];
|
|
||||||
|
|
||||||
const handler: ProxyHandler<any> = {};
|
const handler: ProxyHandler<any> = {};
|
||||||
|
|
||||||
const kGET = Symbol.for("vencord.lazy.get");
|
const kGET = Symbol.for("vencord.lazy.get");
|
||||||
|
@ -59,14 +57,14 @@ for (const method of [
|
||||||
handler.ownKeys = target => {
|
handler.ownKeys = target => {
|
||||||
const v = target[kGET]();
|
const v = target[kGET]();
|
||||||
const keys = Reflect.ownKeys(v);
|
const keys = Reflect.ownKeys(v);
|
||||||
for (const key of unconfigurable) {
|
for (const key of UNCONFIGURABLE_PROPERTIES) {
|
||||||
if (!keys.includes(key)) keys.push(key);
|
if (!keys.includes(key)) keys.push(key);
|
||||||
}
|
}
|
||||||
return keys;
|
return keys;
|
||||||
};
|
};
|
||||||
|
|
||||||
handler.getOwnPropertyDescriptor = (target, p) => {
|
handler.getOwnPropertyDescriptor = (target, p) => {
|
||||||
if (typeof p === "string" && unconfigurable.includes(p))
|
if (typeof p === "string" && UNCONFIGURABLE_PROPERTIES.includes(p))
|
||||||
return Reflect.getOwnPropertyDescriptor(target, p);
|
return Reflect.getOwnPropertyDescriptor(target, p);
|
||||||
|
|
||||||
const descriptor = Reflect.getOwnPropertyDescriptor(target[kGET](), p);
|
const descriptor = Reflect.getOwnPropertyDescriptor(target[kGET](), p);
|
||||||
|
|
|
@ -99,3 +99,6 @@ export const isPluginDev = (id: string) => Object.hasOwn(DevsById, id);
|
||||||
export function pluralise(amount: number, singular: string, plural = singular + "s") {
|
export function pluralise(amount: number, singular: string, plural = singular + "s") {
|
||||||
return amount === 1 ? `${amount} ${singular}` : `${amount} ${plural}`;
|
return amount === 1 ? `${amount} ${singular}` : `${amount} ${plural}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Unconfigurable properties for proxies */
|
||||||
|
export const UNCONFIGURABLE_PROPERTIES = ["arguments", "caller", "prototype"];
|
||||||
|
|
|
@ -1,23 +1,11 @@
|
||||||
/*
|
/*
|
||||||
* Vencord, a modification for Discord's desktop app
|
* Vencord, a Discord client mod
|
||||||
* Copyright (c) 2022 Vendicated and contributors
|
* Copyright (c) 2024 Vendicated and contributors
|
||||||
*
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
* 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 { WEBPACK_CHUNK } from "@utils/constants";
|
|
||||||
import { Logger } from "@utils/Logger";
|
import { Logger } from "@utils/Logger";
|
||||||
|
import { UNCONFIGURABLE_PROPERTIES } from "@utils/misc";
|
||||||
import { canonicalizeMatch, canonicalizeReplacement } from "@utils/patches";
|
import { canonicalizeMatch, canonicalizeReplacement } from "@utils/patches";
|
||||||
import { PatchReplacement } from "@utils/types";
|
import { PatchReplacement } from "@utils/types";
|
||||||
import { WebpackInstance } from "discord-types/other";
|
import { WebpackInstance } from "discord-types/other";
|
||||||
|
@ -29,29 +17,30 @@ import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, s
|
||||||
const logger = new Logger("WebpackInterceptor", "#8caaee");
|
const logger = new Logger("WebpackInterceptor", "#8caaee");
|
||||||
const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/);
|
const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/);
|
||||||
|
|
||||||
let webpackChunk: any[];
|
const modulesProxyhandler: ProxyHandler<any> = {
|
||||||
|
...Object.fromEntries(Object.getOwnPropertyNames(Reflect).map(propName =>
|
||||||
|
[propName, (target: any, ...args: any[]) => Reflect[propName](target, ...args)]
|
||||||
|
)),
|
||||||
|
get: (target, p: string) => {
|
||||||
|
const mod = Reflect.get(target, p);
|
||||||
|
|
||||||
// Patch the window webpack chunk setter to monkey patch the push method before any chunks are pushed
|
// If the property is not a module id, return the value of it without trying to patch
|
||||||
// This way we can patch the factory of everything being pushed to the modules array
|
if (mod == null || mod.$$vencordOriginal != null || Number.isNaN(Number(p))) return mod;
|
||||||
Object.defineProperty(window, WEBPACK_CHUNK, {
|
|
||||||
configurable: true,
|
|
||||||
|
|
||||||
get: () => webpackChunk,
|
const patchedMod = patchFactory(p, mod);
|
||||||
set: v => {
|
Reflect.set(target, p, patchedMod);
|
||||||
if (v?.push) {
|
|
||||||
if (!v.push.$$vencordOriginal) {
|
|
||||||
logger.info(`Patching ${WEBPACK_CHUNK}.push`);
|
|
||||||
patchPush(v);
|
|
||||||
|
|
||||||
// @ts-ignore
|
return patchedMod;
|
||||||
delete window[WEBPACK_CHUNK];
|
},
|
||||||
window[WEBPACK_CHUNK] = v;
|
set: (target, p, newValue) => Reflect.set(target, p, newValue),
|
||||||
|
ownKeys: target => {
|
||||||
|
const keys = Reflect.ownKeys(target);
|
||||||
|
for (const key of UNCONFIGURABLE_PROPERTIES) {
|
||||||
|
if (!keys.includes(key)) keys.push(key);
|
||||||
}
|
}
|
||||||
|
return keys;
|
||||||
}
|
}
|
||||||
|
};
|
||||||
webpackChunk = v;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// wreq.O is the webpack onChunksLoaded function
|
// wreq.O is the webpack onChunksLoaded function
|
||||||
// Discord uses it to await for all the chunks to be loaded before initializing the app
|
// Discord uses it to await for all the chunks to be loaded before initializing the app
|
||||||
|
@ -108,141 +97,51 @@ Object.defineProperty(Function.prototype, "O", {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// wreq.m is the webpack module factory.
|
// wreq.m is the webpack object containing module factories.
|
||||||
// normally, this is populated via webpackGlobal.push, which we patch below.
|
// This is pre-populated with modules, and is also populated via webpackGlobal.push
|
||||||
// However, Discord has their .m prepopulated.
|
// We replace its prototype with our proxy, which is responsible for returning patched module factories containing our patches
|
||||||
// Thus, we use this hack to immediately access their wreq.m and patch all already existing factories
|
|
||||||
//
|
|
||||||
// Update: Discord now has TWO webpack instances. Their normal one and sentry
|
|
||||||
// Sentry does not push chunks to the global at all, so this same patch now also handles their sentry modules
|
|
||||||
Object.defineProperty(Function.prototype, "m", {
|
Object.defineProperty(Function.prototype, "m", {
|
||||||
configurable: true,
|
configurable: true,
|
||||||
|
|
||||||
set(v: any) {
|
set(originalModules: any) {
|
||||||
// When using react devtools or other extensions, we may also catch their webpack here.
|
// When using react devtools or other extensions, we may also catch their webpack here.
|
||||||
// This ensures we actually got the right one
|
// This ensures we actually got the right one
|
||||||
const { stack } = new Error();
|
const { stack } = new Error();
|
||||||
if (stack?.includes("discord.com") || stack?.includes("discordapp.com")) {
|
if (stack?.includes("discord.com") || stack?.includes("discordapp.com")) {
|
||||||
logger.info("Found Webpack module factory", stack.match(/\/assets\/(.+?\.js)/)?.[1] ?? "");
|
logger.info("Found Webpack module factory", stack.match(/\/assets\/(.+?\.js)/)?.[1] ?? "");
|
||||||
patchFactories(v);
|
|
||||||
|
// The new object which will contain the factories
|
||||||
|
const modules = Object.assign({}, originalModules);
|
||||||
|
|
||||||
|
// Clear the original object so pre-populated factories are patched
|
||||||
|
for (const propName in originalModules) {
|
||||||
|
delete originalModules[propName];
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.setPrototypeOf(originalModules, new Proxy(modules, modulesProxyhandler));
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(this, "m", {
|
Object.defineProperty(this, "m", {
|
||||||
value: v,
|
value: originalModules,
|
||||||
configurable: true
|
configurable: true
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function patchPush(webpackGlobal: any) {
|
|
||||||
function handlePush(chunk: any) {
|
|
||||||
try {
|
|
||||||
patchFactories(chunk[1]);
|
|
||||||
} catch (err) {
|
|
||||||
logger.error("Error in handlePush", err);
|
|
||||||
}
|
|
||||||
|
|
||||||
return handlePush.$$vencordOriginal.call(webpackGlobal, chunk);
|
|
||||||
}
|
|
||||||
|
|
||||||
handlePush.$$vencordOriginal = webpackGlobal.push;
|
|
||||||
handlePush.toString = handlePush.$$vencordOriginal.toString.bind(handlePush.$$vencordOriginal);
|
|
||||||
// Webpack overwrites .push with its own push like so: `d.push = n.bind(null, d.push.bind(d));`
|
|
||||||
// it wraps the old push (`d.push.bind(d)`). this old push is in this case our handlePush.
|
|
||||||
// If we then repatched the new push, we would end up with recursive patching, which leads to our patches
|
|
||||||
// being applied multiple times.
|
|
||||||
// Thus, override bind to use the original push
|
|
||||||
handlePush.bind = (...args: unknown[]) => handlePush.$$vencordOriginal.bind(...args);
|
|
||||||
|
|
||||||
Object.defineProperty(webpackGlobal, "push", {
|
|
||||||
configurable: true,
|
|
||||||
|
|
||||||
get: () => handlePush,
|
|
||||||
set(v) {
|
|
||||||
handlePush.$$vencordOriginal = v;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let webpackNotInitializedLogged = false;
|
let webpackNotInitializedLogged = false;
|
||||||
|
|
||||||
function patchFactories(factories: Record<string, (module: any, exports: any, require: WebpackInstance) => void>) {
|
function patchFactory(id: string, mod: (module: any, exports: any, require: WebpackInstance) => void) {
|
||||||
for (const id in factories) {
|
|
||||||
let mod = factories[id];
|
|
||||||
|
|
||||||
const originalMod = mod;
|
|
||||||
const patchedBy = new Set();
|
|
||||||
|
|
||||||
const factory = factories[id] = function (module: any, exports: any, require: WebpackInstance) {
|
|
||||||
if (wreq == null && IS_DEV) {
|
|
||||||
if (!webpackNotInitializedLogged) {
|
|
||||||
webpackNotInitializedLogged = true;
|
|
||||||
logger.error("WebpackRequire was not initialized, running modules without patches instead.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return void originalMod(module, exports, require);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
mod(module, exports, require);
|
|
||||||
} catch (err) {
|
|
||||||
// Just rethrow discord errors
|
|
||||||
if (mod === originalMod) throw err;
|
|
||||||
|
|
||||||
logger.error("Error in patched module", err);
|
|
||||||
return void originalMod(module, exports, require);
|
|
||||||
}
|
|
||||||
|
|
||||||
exports = module.exports;
|
|
||||||
|
|
||||||
if (!exports) return;
|
|
||||||
|
|
||||||
// There are (at the time of writing) 11 modules exporting the window
|
|
||||||
// Make these non enumerable to improve webpack search performance
|
|
||||||
if (exports === window && require.c) {
|
|
||||||
Object.defineProperty(require.c, id, {
|
|
||||||
value: require.c[id],
|
|
||||||
enumerable: false,
|
|
||||||
configurable: true,
|
|
||||||
writable: true
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const callback of moduleListeners) {
|
|
||||||
try {
|
|
||||||
callback(exports, id);
|
|
||||||
} catch (err) {
|
|
||||||
logger.error("Error in Webpack module listener:\n", err, callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const [filter, callback] of subscriptions) {
|
|
||||||
try {
|
|
||||||
if (filter(exports)) {
|
|
||||||
subscriptions.delete(filter);
|
|
||||||
callback(exports, id);
|
|
||||||
} else if (exports.default && filter(exports.default)) {
|
|
||||||
subscriptions.delete(filter);
|
|
||||||
callback(exports.default, id);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
logger.error("Error while firing callback for Webpack subscription:\n", err, filter, callback);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} as any as { toString: () => string, original: any, (...args: any[]): void; };
|
|
||||||
|
|
||||||
factory.toString = originalMod.toString.bind(originalMod);
|
|
||||||
factory.original = originalMod;
|
|
||||||
|
|
||||||
for (const factoryListener of factoryListeners) {
|
for (const factoryListener of factoryListeners) {
|
||||||
try {
|
try {
|
||||||
factoryListener(originalMod);
|
factoryListener(mod);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error("Error in Webpack factory listener:\n", err, factoryListener);
|
logger.error("Error in Webpack factory listener:\n", err, factoryListener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const originalMod = mod;
|
||||||
|
const patchedBy = new Set();
|
||||||
|
|
||||||
// Discords Webpack chunks for some ungodly reason contain random
|
// Discords Webpack chunks for some ungodly reason contain random
|
||||||
// newlines. Cyn recommended this workaround and it seems to work fine,
|
// newlines. Cyn recommended this workaround and it seems to work fine,
|
||||||
// however this could potentially break code, so if anything goes weird,
|
// however this could potentially break code, so if anything goes weird,
|
||||||
|
@ -354,5 +253,69 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
|
||||||
|
|
||||||
if (!patch.all) patches.splice(i--, 1);
|
if (!patch.all) patches.splice(i--, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function patchedFactory(module: any, exports: any, require: WebpackInstance) {
|
||||||
|
if (wreq == null && IS_DEV) {
|
||||||
|
if (!webpackNotInitializedLogged) {
|
||||||
|
webpackNotInitializedLogged = true;
|
||||||
|
logger.error("WebpackRequire was not initialized, running modules without patches instead.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return void originalMod(module, exports, require);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
mod(module, exports, require);
|
||||||
|
} catch (err) {
|
||||||
|
// Just rethrow Discord errors
|
||||||
|
if (mod === originalMod) throw err;
|
||||||
|
|
||||||
|
logger.error("Error in patched module", err);
|
||||||
|
return void originalMod(module, exports, require);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Webpack sometimes sets the value of module.exports directly, so assign exports to it to make sure we properly handle it
|
||||||
|
exports = module.exports;
|
||||||
|
if (exports == null) return;
|
||||||
|
|
||||||
|
// There are (at the time of writing) 11 modules exporting the window
|
||||||
|
// Make these non enumerable to improve webpack search performance
|
||||||
|
if (exports === window && require.c) {
|
||||||
|
Object.defineProperty(require.c, id, {
|
||||||
|
value: require.c[id],
|
||||||
|
configurable: true,
|
||||||
|
writable: true,
|
||||||
|
enumerable: false
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const callback of moduleListeners) {
|
||||||
|
try {
|
||||||
|
callback(exports, id);
|
||||||
|
} catch (err) {
|
||||||
|
logger.error("Error in Webpack module listener:\n", err, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const [filter, callback] of subscriptions) {
|
||||||
|
try {
|
||||||
|
if (filter(exports)) {
|
||||||
|
subscriptions.delete(filter);
|
||||||
|
callback(exports, id);
|
||||||
|
} else if (exports.default && filter(exports.default)) {
|
||||||
|
subscriptions.delete(filter);
|
||||||
|
callback(exports.default, id);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
logger.error("Error while firing callback for Webpack subscription:\n", err, filter, callback);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
patchedFactory.toString = originalMod.toString.bind(originalMod);
|
||||||
|
// @ts-ignore
|
||||||
|
patchedFactory.$$vencordOriginal = originalMod;
|
||||||
|
|
||||||
|
return patchedFactory;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue