forked from Legcord/Legcord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.ts
57 lines (56 loc) · 2.18 KB
/
protocol.ts
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
import path from "node:path";
import Url from "node:url";
import { net, app, protocol } from "electron";
protocol.registerSchemesAsPrivileged([
{
scheme: "legcord",
privileges: {
standard: true,
secure: true,
supportFetchAPI: true,
corsEnabled: false,
bypassCSP: true,
stream: true,
},
},
]);
void app.whenReady().then(() => {
protocol.handle("legcord", (req) => {
if (req.url.startsWith("legcord://plugins/")) {
const url = req.url.replace("legcord://plugins/", "").split("/");
const filePath = path.join(import.meta.dirname, "plugins", `/${url[0]}/${url[1]}`);
if (filePath.includes("..")) {
return new Response("bad", {
status: 400,
headers: { "content-type": "text/html" },
});
}
return net.fetch(Url.pathToFileURL(filePath).toString());
} else if (req.url.startsWith("legcord://assets/")) {
const file = req.url.replace("legcord://assets/", "");
const filePath = path.join(import.meta.dirname, "assets", "app", `${file}`);
if (filePath.includes("..")) {
return new Response("bad", {
status: 400,
headers: { "content-type": "text/html" },
});
}
return net.fetch(Url.pathToFileURL(filePath).toString());
} else if (req.url.startsWith("legcord://local/")) {
const file = req.url.replace("legcord://local/", "");
const userDataPath = path.join(app.getPath("userData"), "userAssets");
const filePath = path.normalize(path.join(userDataPath, `${file}`));
if (!filePath.startsWith(userDataPath)) {
return new Response("bad", {
status: 400,
headers: { "content-type": "text/html" },
});
}
return net.fetch(Url.pathToFileURL(filePath).toString());
}
return new Response("bad", {
status: 400,
headers: { "content-type": "text/html" },
});
});
});