Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:moonlight-mod/moonlight into dev…
Browse files Browse the repository at this point in the history
…elop
  • Loading branch information
NotNite committed Oct 7, 2024
2 parents 253be05 + 775432f commit ea8f858
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 37 deletions.
17 changes: 0 additions & 17 deletions packages/core-extensions/src/disableSentry/host.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { join } from "node:path";
import { Module } from "node:module";
import { BrowserWindow } from "electron";

const logger = moonlightHost.getLogger("disableSentry");

Expand All @@ -24,19 +23,3 @@ if (moonlightHost.asarPath !== "moonlightDesktop") {
logger.error("Failed to stub Sentry host side:", err);
}
}

moonlightHost.events.on("window-created", (window: BrowserWindow) => {
window.webContents.session.webRequest.onBeforeRequest(
{
urls: [
"https://*.sentry.io/*",
"https://*.discord.com/error-reporting-proxy/*",
"https://discord.com/assets/sentry.*.js",
"https://*.discord.com/assets/sentry.*.js"
]
},
function (details, callback) {
callback({ cancel: true });
}
);
});
8 changes: 7 additions & 1 deletion packages/core-extensions/src/disableSentry/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"tagline": "Turns off Discord's error reporting systems",
"authors": ["Cynosphere", "NotNite"],
"tags": ["privacy"]
}
},
"blocked": [
"https://*.sentry.io/*",
"https://*.discord.com/error-reporting-proxy/*",
"https://discord.com/assets/sentry.*.js",
"https://*.discord.com/assets/sentry.*.js"
]
}
15 changes: 0 additions & 15 deletions packages/core-extensions/src/noTrack/host.ts

This file was deleted.

6 changes: 5 additions & 1 deletion packages/core-extensions/src/noTrack/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
"tagline": "Disables /api/science and analytics",
"authors": ["Cynosphere", "NotNite"],
"tags": ["privacy"]
}
},
"blocked": [
"https://*.discord.com/api/v*/science",
"https://*.discord.com/api/v*/metrics"
]
}
40 changes: 40 additions & 0 deletions packages/injector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const logger = new Logger("injector");

let oldPreloadPath: string | undefined;
let corsAllow: string[] = [];
let blockedUrls: RegExp[] = [];
let isMoonlightDesktop = false;
let hasOpenAsar = false;
let openAsarConfigPreload: string | undefined;
Expand All @@ -41,6 +42,39 @@ ipcMain.handle(constants.ipcSetCorsList, (_, list) => {
corsAllow = list;
});

const reEscapeRegExp = /[\\^$.*+?()[\]{}|]/g;
const reMatchPattern =
/^(?<scheme>\*|[a-z][a-z0-9+.-]*):\/\/(?<host>.+?)\/(?<path>.+)?$/;

const escapeRegExp = (s: string) => s.replace(reEscapeRegExp, "\\$&");
ipcMain.handle(constants.ipcSetBlockedList, (_, list: string[]) => {
// We compile the patterns into a RegExp based on a janky match pattern-like syntax
const compiled = list
.map((pattern) => {
const match = pattern.match(reMatchPattern);
if (!match?.groups) return;

let regex = "";
if (match.groups.scheme === "*") regex += ".+?";
else regex += escapeRegExp(match.groups.scheme);
regex += ":\\/\\/";

const parts = match.groups.host.split(".");
if (parts[0] === "*") {
parts.shift();
regex += "(?:.+?\\.)?";
}
regex += escapeRegExp(parts.join("."));

regex += "\\/" + escapeRegExp(match.groups.path).replace("\\*", ".*?");

return new RegExp("^" + regex + "$");
})
.filter(Boolean) as RegExp[];

blockedUrls = compiled;
});

function patchCsp(headers: Record<string, string[]>) {
const directives = [
"style-src",
Expand Down Expand Up @@ -118,6 +152,12 @@ class BrowserWindow extends ElectronBrowserWindow {
}
});

// Allow plugins to block some URLs,
// this is needed because multiple webRequest handlers cannot be registered at once
this.webContents.session.webRequest.onBeforeRequest((details, cb) => {
cb({ cancel: blockedUrls.some((u) => u.test(details.url)) });
});

if (hasOpenAsar) {
// Remove DOM injections
// Settings can still be opened via:
Expand Down
11 changes: 8 additions & 3 deletions packages/node-preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ async function injectGlobals() {
await loadProcessedExtensions(processedExtensions);
contextBridge.exposeInMainWorld("moonlightNode", moonlightNode);

const extCors = moonlightNode.processedExtensions.extensions
.map((x) => x.manifest.cors ?? [])
.flat();
const extCors = moonlightNode.processedExtensions.extensions.flatMap(
(x) => x.manifest.cors ?? []
);

for (const repo of moonlightNode.config.repositories) {
const url = new URL(repo);
Expand All @@ -63,6 +63,11 @@ async function injectGlobals() {
}

ipcRenderer.invoke(constants.ipcSetCorsList, extCors);

const extBlocked = moonlightNode.processedExtensions.extensions.flatMap(
(e) => e.manifest.blocked ?? []
);
ipcRenderer.invoke(constants.ipcSetBlockedList, extBlocked);
}

async function loadPreload() {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ export const ipcGetAppData = "_moonlight_getAppData";
export const ipcGetIsMoonlightDesktop = "_moonlight_getIsMoonlightDesktop";
export const ipcMessageBox = "_moonlight_messageBox";
export const ipcSetCorsList = "_moonlight_setCorsList";
export const ipcSetBlockedList = "_moonlight_setBlockedList";

export const apiLevel = 2;
2 changes: 2 additions & 0 deletions packages/types/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export type ExtensionManifest = {
incompatible?: string[];

settings?: Record<string, ExtensionSettingsManifest>;

cors?: string[];
blocked?: string[];
};

export enum ExtensionLoadSource {
Expand Down

0 comments on commit ea8f858

Please sign in to comment.