2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
2023-04-03 19:16:29 -04:00
|
|
|
* Copyright (c) 2023 Vendicated and contributors
|
2022-10-21 19:17:06 -04:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2023-04-03 19:16:29 -04:00
|
|
|
import { app, protocol, session } from "electron";
|
|
|
|
import { join } from "path";
|
2022-10-22 12:18:41 -04:00
|
|
|
|
2023-04-03 19:16:29 -04:00
|
|
|
import { getSettings } from "./ipcMain";
|
|
|
|
import { IS_VANILLA } from "./utils/constants";
|
|
|
|
import { installExt } from "./utils/extensions";
|
2022-12-02 08:10:40 -05:00
|
|
|
|
2023-04-03 19:16:29 -04:00
|
|
|
if (IS_VENCORD_DESKTOP || !IS_VANILLA) {
|
2023-03-30 19:18:57 -04:00
|
|
|
app.whenReady().then(() => {
|
2022-12-02 08:10:40 -05:00
|
|
|
// Source Maps! Maybe there's a better way but since the renderer is executed
|
|
|
|
// from a string I don't think any other form of sourcemaps would work
|
2023-03-30 19:18:57 -04:00
|
|
|
protocol.registerFileProtocol("vencord", ({ url: unsafeUrl }, cb) => {
|
2022-12-02 08:10:40 -05:00
|
|
|
let url = unsafeUrl.slice("vencord://".length);
|
|
|
|
if (url.endsWith("/")) url = url.slice(0, -1);
|
|
|
|
switch (url) {
|
|
|
|
case "renderer.js.map":
|
|
|
|
case "preload.js.map":
|
|
|
|
case "patcher.js.map": // doubt
|
2023-04-04 15:09:47 -04:00
|
|
|
case "main.js.map":
|
2022-12-02 08:10:40 -05:00
|
|
|
cb(join(__dirname, url));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cb({ statusCode: 403 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2023-04-03 19:16:29 -04:00
|
|
|
if (getSettings().enableReactDevtools)
|
2022-12-02 08:10:40 -05:00
|
|
|
installExt("fmkadmapgofadopljbjfkapdkoienihi")
|
|
|
|
.then(() => console.info("[Vencord] Installed React Developer Tools"))
|
|
|
|
.catch(err => console.error("[Vencord] Failed to install React Developer Tools", err));
|
|
|
|
} catch { }
|
|
|
|
|
|
|
|
|
|
|
|
// Remove CSP
|
2022-12-02 10:43:37 -05:00
|
|
|
type PolicyResult = Record<string, string[]>;
|
|
|
|
|
|
|
|
const parsePolicy = (policy: string): PolicyResult => {
|
|
|
|
const result: PolicyResult = {};
|
|
|
|
policy.split(";").forEach(directive => {
|
|
|
|
const [directiveKey, ...directiveValue] = directive.trim().split(/\s+/g);
|
|
|
|
if (directiveKey && !Object.prototype.hasOwnProperty.call(result, directiveKey)) {
|
|
|
|
result[directiveKey] = directiveValue;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const stringifyPolicy = (policy: PolicyResult): string =>
|
|
|
|
Object.entries(policy)
|
|
|
|
.filter(([, values]) => values?.length)
|
|
|
|
.map(directive => directive.flat().join(" "))
|
|
|
|
.join("; ");
|
|
|
|
|
2022-12-02 08:10:40 -05:00
|
|
|
function patchCsp(headers: Record<string, string[]>, header: string) {
|
|
|
|
if (header in headers) {
|
2022-12-02 10:43:37 -05:00
|
|
|
const csp = parsePolicy(headers[header][0]);
|
|
|
|
|
|
|
|
for (const directive of ["style-src", "connect-src", "img-src", "font-src", "media-src", "worker-src"]) {
|
|
|
|
csp[directive] = ["*", "blob:", "data:", "'unsafe-inline'"];
|
2022-12-02 08:10:40 -05:00
|
|
|
}
|
|
|
|
// TODO: Restrict this to only imported packages with fixed version.
|
|
|
|
// Perhaps auto generate with esbuild
|
2022-12-02 10:43:37 -05:00
|
|
|
csp["script-src"] ??= [];
|
|
|
|
csp["script-src"].push("'unsafe-eval'", "https://unpkg.com", "https://cdnjs.cloudflare.com");
|
|
|
|
headers[header] = [stringifyPolicy(csp)];
|
2022-11-02 17:15:55 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 19:18:57 -04:00
|
|
|
session.defaultSession.webRequest.onHeadersReceived(({ responseHeaders, resourceType }, cb) => {
|
2022-12-02 08:10:40 -05:00
|
|
|
if (responseHeaders) {
|
|
|
|
if (resourceType === "mainFrame")
|
|
|
|
patchCsp(responseHeaders, "content-security-policy");
|
2022-09-25 11:45:59 -04:00
|
|
|
|
2022-12-02 08:10:40 -05:00
|
|
|
// Fix hosts that don't properly set the css content type, such as
|
|
|
|
// raw.githubusercontent.com
|
|
|
|
if (resourceType === "stylesheet")
|
|
|
|
responseHeaders["content-type"] = ["text/css"];
|
|
|
|
}
|
|
|
|
cb({ cancel: false, responseHeaders });
|
|
|
|
});
|
2023-03-30 19:18:57 -04:00
|
|
|
|
|
|
|
// assign a noop to onHeadersReceived to prevent other mods from adding their own incompatible ones.
|
|
|
|
// For instance, OpenAsar adds their own that doesn't fix content-type for stylesheets which makes it
|
|
|
|
// impossible to load css from github raw despite our fix above
|
|
|
|
session.defaultSession.webRequest.onHeadersReceived = () => { };
|
2022-08-28 20:25:27 -04:00
|
|
|
});
|
2022-12-02 08:10:40 -05:00
|
|
|
}
|
2022-10-22 12:17:02 -04:00
|
|
|
|
2023-04-03 19:16:29 -04:00
|
|
|
if (IS_DISCORD_DESKTOP) {
|
|
|
|
require("./patcher");
|
|
|
|
}
|