This repository was archived by the owner on Jun 14, 2024. It is now read-only.
generated from kernel-addons/PluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
117 lines (97 loc) · 4.04 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
const {app} = require("electron");
const Module = require("module");
const {config: {
MAX_ATTEMPTS = 10,
DELETE_HEADERS = [],
APPEND_HEADERS = {},
BLOCKED_DOMAINS = [],
EXPERIMENTAL_SENTRY_BLOCK = false
}} = require("./index.json");
let sentryRegex = /ignoreErrors.*BetterDiscord/si;
let sentryURL = null;
// TODO: Implement better searching algorithm that works when discord updates the chunk url during runtime.
const oLoad = Module._load;
const fakeSentry = {
init: () => { },
captureException: console.error,
setUser: () => void 0,
setTag: () => void 0,
setTags: () => void 0,
addBreadcrumb: () => void 0,
setExtra: () => void 0
};
Module._load = function (mod) {
if (mod.indexOf("sentry") > -1) return fakeSentry;
return oLoad.apply(this, arguments);
};
const showConsoleLog = (tries, win) => process.nextTick(() => {
win.webContents.executeJavaScript(`(() => {
console.log("[SentryBlock] Blocked sentry", {
url: ${JSON.stringify(sentryURL)},
attempts: ${JSON.stringify(tries)}
});
})()`);
console.log("[SentryBlock] Blocked sentry", {
url: sentryURL,
attempts: tries
});
});
app.whenReady().then(() => {
app.on("browser-window-created", (_, win) => {
let tries = 0;
let queue = new Set();
win.webContents.session.webRequest.onHeadersReceived((opts, callback) => {
const {responseHeaders} = opts;
const currentHeaders = Object.keys(responseHeaders);
for (const header in APPEND_HEADERS) {
// Checking case sensitive for the header, otherwise we run into issues with duplicate headers.
if (currentHeaders.some(h => h.toLowerCase() === header.toLowerCase())) continue;
responseHeaders[header] = APPEND_HEADERS[header];
}
for (const header of DELETE_HEADERS) {
const actualName = currentHeaders.find(h => h === header.toLowerCase());
if (!actualName) continue;
delete responseHeaders[actualName];
}
for (const matcher of BLOCKED_DOMAINS) {
const regex = new RegExp(matcher);
if (regex.test(opts.url)) {
return callback({cancel: true, responseHeaders});
}
}
if (EXPERIMENTAL_SENTRY_BLOCK) {
return callback({cancel: false, responseHeaders});
}
if (sentryURL) {
const isSentry = sentryURL === opts.url;
if (isSentry) showConsoleLog(tries, win);
return callback({cancel: isSentry, responseHeaders});
}
if (queue.has(opts.url)) return callback({responseHeaders});
if (tries > MAX_ATTEMPTS) return callback({responseHeaders});
if (opts.resourceType !== "script") return callback({responseHeaders});
if (opts.url.indexOf("discord.com/assets") < 0) return callback({responseHeaders});
require("https").get(opts.url, (res) => {
const data = [];
res.on("data", d => data.push(d));
res.on("end", () => {
if (sentryURL) return callback({responseHeaders});
const body = data.join("");
queue.delete(opts.url);
const isSentry = sentryRegex.test(body);
if (isSentry) {
sentryURL = opts.url;
showConsoleLog(tries, win);
callback({cancel: true, responseHeaders});
queue.clear();
} else {
callback({responseHeaders});
}
});
});
queue.add(opts.url);
tries++;
if (tries === MAX_ATTEMPTS) console.warn("Could not find sentry chunk.");
});
});
});