-
Notifications
You must be signed in to change notification settings - Fork 62
/
runtime.js
433 lines (368 loc) · 12 KB
/
runtime.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// HOLY UNBLOCKER RUNTIME!!
// this code shouldn't be bundled by astro
// vanilla JS = best
// - starts the discord bot (if configured to do so)
// - checks permissions and verifies everything is OK
// - exports http "request" event handler
// - sets x-robots-tag
// - sets cross-origin-resource-policy on /cdn/ and /compat/
// - sets up mirroring the game cdn, /cdn/
// - sets up mirroring the theatre API, /api/theatre/
// - exports http "upgrade" event handler
// - wisp
// - that's just about it
import http from "node:http";
import https from "node:https";
import { dirname, resolve } from "node:path";
import { access, copyFile, readFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import send from "@fastify/send";
import parseUrl from "parseurl";
import wisp from "wisp-server-node";
import { ActivityType, Client, PermissionsBitField } from "discord.js";
import chalk from "chalk";
import compression from "compression";
let startupTag = chalk.grey(chalk.bold("Holy Unblocker:"));
// when vite bundles this, it will complain about being unable to import(configFile)
// however, this is annoying and stupid
// import.meta.url is very reliable for this!!
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const configFile = resolve(__dirname, "./config/config.js");
try {
await access(configFile);
} catch (err) {
if (err.code !== "ENOENT") throw err;
console.log(startupTag, chalk.yellow("No config.js file found. Making one."));
copyFile("./config/config.example.js", "./config/config.js");
console.log(
startupTag,
chalk.italic("copying config.example.js -> config.js")
);
}
/**
* @type {import("./config/config").AppConfig}
*/
let appConfig;
try {
appConfig = (await import("./config/config.js")).appConfig;
} catch (err) {
// user config error
console.error(
chalk.bold("An error occurred while trying to load ./config/config.js!")
);
console.error(err);
console.error(
chalk.grey.italic(
"This error was likely caused as a result of you changing something."
)
);
console.error(chalk.grey.italic("Please check your config then try again!"));
process.exit(1);
}
const { db, giveTierDiscordRoles, accountsEnabled } = await import(
"./config/apis.js"
);
// check runtime requirements
// in both astro dev server & runtime
const majorNodeVersion = Number(process.versions.node.split(".")[0]);
if (majorNodeVersion < 19) {
console.error("Your NodeJS version is unsupported!");
console.error("You need at least NodeJS v19 to run Holy Unblocker");
console.error(
"You can fix this by upgrading NodeJS. Try installing nvm: https://github.com/nvm-sh/nvm"
);
process.exit(1);
}
const client = new Client({ intents: [] });
client.on("ready", async () => {
console.log(`${chalk.bold("Discord:")} Logged in as ${client.user.tag}!`);
const rolesRes = await fetch(
`https://discord.com/api/v10/guilds/${appConfig.discord.guildId}/roles`,
{
headers: {
authorization: `Bot ${appConfig.discord.botToken}`,
},
}
);
if (rolesRes.status !== 200) {
console.error(
"Error fetching roles for guild:",
appConfig.discord.guildId,
rolesRes.status
);
console.error(await rolesRes.text());
if (rolesRes.status === 404) {
console.log(
"Make sure you invited the bot to your server! Or that the guild ID is correct"
);
console.log("Use this link to invite the bot:");
// we need MANAGE_ROLES to assign ppl their roles
// this link should have that permission set
console.log(
`https://discord.com/oauth2/authorize?client_id=${appConfig.discord.clientId}&scope=bot&permissions=268435456`
);
process.exit(1);
}
}
const serverRoles = await rolesRes.json();
const clientMember = await (
await fetch(
`https://discord.com/api/v10/guilds/${appConfig.discord.guildId}/members/${appConfig.discord.clientId}`,
{
headers: {
authorization: `Bot ${appConfig.discord.botToken}`,
},
}
)
).json();
if (clientMember.roles.length === 0) {
console.error(
"In order to give users their subscription roles, the discord bot needs a role with Manage Roles."
);
console.error(
"You need to create a role, move it above the subscriber roles, and assign it to the Discord bot."
);
process.exit(1);
}
// check if we can manage roles
const canManageRoles = clientMember.roles.some((role) =>
new PermissionsBitField(
serverRoles.find((e) => e.id === role).permissions
).has("ManageRoles")
);
if (!canManageRoles) {
console.error(
"In order to give users their subscription roles, the Discord bot needs the Manage Roles permission."
);
console.error("You need to give the Discord bot a role with permission.");
process.exit(1);
}
const highestRoleId = clientMember.roles[clientMember.roles.length - 1];
const highestRole = serverRoles.find((e) => e.id === highestRoleId);
for (const tier in appConfig.discord.roleIds) {
const id = appConfig.discord.roleIds[tier];
const role = serverRoles.find((r) => r.id === id);
if (role === undefined) {
console.error("Invalid role id", id, "for tier", tier);
process.exit(1);
}
if (role.position > highestRole.position) {
console.error("Cannot give users the role", role.name);
console.error(
"You need to give the Discord bot a role that's higher than",
role.name
);
process.exit();
}
}
console.log(chalk.bold("Discord bot permissions look good."));
client.user.setPresence({
status: "dnd",
activities: [
{
name: "for new members",
state: "",
url: appConfig.mainWebsite,
type: ActivityType.Watching,
},
],
});
// process.exit(1);
});
client.on("guildMemberAdd", async (member) => {
console.log("Member", member.id, "just joined guild", member.guild.id);
if (member.guild.id !== appConfig.discord.guildId) {
console.log("Guild isn't part of config, ignoring!");
return;
}
/**
* @type {import("@lib/util").m.UserModel}
*/
const user = (
await db.query("SELECT * FROM users WHERE discord_id = $1;", [
member.user.id,
])
).rows[0];
if (user) {
console.log("Found user:", user);
await giveTierDiscordRoles(user);
}
});
// start the discord bot here
if (accountsEnabled && appConfig.discord.listenForJoins)
client.login(appConfig.discord.botToken);
/**
*
* cursed ultraviolet config loader
* configs can be location-aware
* @param {string} host
* @param {string} url
* @returns {import("@titaniumnetwork-dev/ultraviolet").UVConfig}
*/
function getUVConfig(host, url) {
const mockEnv = {
Ultraviolet: {
codec: { xor: {} },
},
location: new URL(`http://${host}${url}`),
};
mockEnv.self = mockEnv;
new Function(...Object.keys(mockEnv), uvConfigSrc)(...Object.values(mockEnv));
// console.log(mockEnv);
return mockEnv.__uv$config;
}
/**
* @typedef {Object} AppMirror
* @property {string} prefix
* @property {string} url
*/
// simple mirror middleware that supports GET/POST
const hasTheatreFiles = "theatreFilesPath" in appConfig;
const cdnAbs =
hasTheatreFiles && resolve(__dirname, appConfig.theatreFilesPath);
/**
* @type {AppMirror[]}
*/
const appMirrors = [];
// setup theatre mirror
if (!hasTheatreFiles)
appMirrors.push({ prefix: "/cdn/", url: appConfig.theatreFilesMirror });
// setup theatre api mirror
if (!("db" in appConfig))
appMirrors.push({ prefix: "/api/theatre/", url: appConfig.theatreApiMirror });
// now we can use self.__uv$config in the backend
// this is kinda cursed
const uvConfigSrc = await readFile(
new URL("./public/uv/uv.config.js", import.meta.url),
"utf-8"
);
const compress = compression();
/**
*
* @param {import("http").IncomingMessage} req
* @param {import("http").OutgoingMessage} res
* @param {() => void} middleware
*/
export function handleReq(req, res, middleware) {
// hooks the res
compress(req, res, () => {});
const isCDN = req.url.startsWith("/cdn/");
// THIS SHOULD ALWAYS BE SET ON THEATRE FILES AND /compat/
// DO NOT DO NOT SET THIS ON /pro/ OR ACCOUNT DETAILS WILL BE LEAKED
// if (isCDN || req.url.startsWith("/compat/")) {
if (!req.url.startsWith("/pro")) {
// this makes loading epoxy TLS faster
// thanks r58
res.setHeader(
"cross-origin-opener-policy-report-only",
"same-origin-allow-popups"
);
res.setHeader("cross-origin-resource-policy", "same-origin");
}
const isMainWebsite =
!("mainWebsite" in appConfig) || req.headers.host === appConfig.mainWebsite;
// prevent scraping of website
if (!isMainWebsite) {
res.setHeader("x-robots-tag", "noindex");
if (req.url.startsWith("/sitemap")) {
req.url = "/404";
return middleware();
}
}
// docs: https://github.com/vercel/serve-handler
if (isCDN && hasTheatreFiles) {
// docs: https://www.npmjs.com/package/@fastify/send
req.url = req.url.slice("/cdn".length);
send(req, parseUrl(req).pathname, {
root: cdnAbs,
}).then(({ statusCode, headers, stream }) => {
if (statusCode === 404) {
// internal redirect to 404 page
req.url = "/404";
middleware(req, res);
} else {
// normalize the url
if ("Location" in headers) headers.Location = "/cdn" + headers.Location;
stream.pipe(res);
res.writeHead(statusCode, headers);
}
});
return;
}
const __uv$config = getUVConfig(req.headers.host, req.url);
// console.log("got config", __uv$config);
// we want the uv service page to fallback to a page that registers the service worker
// internal redirect
if (req.url.startsWith(__uv$config.prefix)) {
req.url = "/register-uv";
// app(req, res);
return middleware();
}
// MIRRORS HTTP PROXDY
for (const mirror of appMirrors) {
if (!req.url.startsWith(mirror.prefix)) continue;
const sendBody = !["HEAD", "GET"].includes(req.method);
const mirrorURL = mirror.url + req.url.slice(mirror.prefix.length);
// console.log("Proxy:", req.url, "->", mirrorURL);
/**
* @type {Record<string, string>}
*/
const mirrorHeaders = {};
const ct = req.headers["content-type"];
if (typeof ct === "string") mirrorHeaders["content-type"] = ct;
// make the request
const mirrorReq = (mirrorURL.startsWith("https:") ? https : http).request(
mirrorURL,
{
method: req.method,
headers: mirrorHeaders,
}
);
mirrorReq.on("error", (err) => {
console.error("error when loading proxy", mirrorURL);
console.error(err);
res.writeHead(502);
res.end();
});
mirrorReq.on("response", (mirrorRes) => {
if (mirrorRes.statusCode === 404) {
// display astro 404 page
req.url = "/404";
return middleware();
}
// support redirects
const loc = mirrorRes.headers["location"];
if (typeof loc === "string") res.setHeader("location", loc);
const ct = mirrorRes.headers["content-type"];
if (typeof ct === "string") res.setHeader("content-type", ct);
const ce = mirrorRes.headers["content-encoding"];
if (
typeof ce === "string" &&
["gzip", "compress", "deflate", "br", "zstd"].includes(ce)
)
res.setHeader("content-encoding", ce);
res.writeHead(mirrorRes.statusCode);
mirrorRes.pipe(res);
});
// pipe request body into mirror
if (sendBody) req.pipe(mirrorReq);
// or just send the request
else mirrorReq.end();
return;
}
return middleware();
}
// handle 'upgrade' event on http server
// the url / is reserved for astro dev server HMR
const wispServerLogging = false;
export function handleUpgrade(req, socket, head) {
if (req.url === "/api/wisp/" && !("separateWispServer" in appConfig)) {
wisp.routeRequest(req, socket, head, { logging: wispServerLogging });
} else {
console.log("bad websocket req @", req.url);
// kill the request so it isn't stuck loading
socket.end();
}
}
console.log(startupTag, "Runtime loaded");