Newer
Older
import { RateLimiter } from "../lib/RateLimiter";
import { prisma } from "../lib/prisma";
import { SocketServer } from "../lib/SocketServer";
import {
Instance,
InstanceNotBanned,
InstanceNotFound,
} from "../models/Instance";
app.use(RateLimiter.ADMIN);
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
app.use(async (req, res, next) => {
if (!req.session.user) {
res.status(401).json({
success: false,
error: "You are not logged in",
});
return;
}
const user = await User.fromAuthSession(req.session.user);
if (!user) {
res.status(400).json({
success: false,
error: "User data does not exist?",
});
return;
}
if (!user.isAdmin) {
res.status(403).json({
success: false,
error: "user is not admin",
});
return;
}
next();
});
app.get("/check", (req, res) => {
res.send({ success: true });
});
app.get("/canvas/size", async (req, res) => {
const config = Canvas.getCanvasConfig();
res.json({
success: true,
size: {
width: config.size[0],
height: config.size[1],
},
});
});
/**
* Update canvas size
*
* @header X-Audit
* @body width number
* @body height number
*/
app.post("/canvas/size", async (req, res) => {
const width = parseInt(req.body.width || "-1");
const height = parseInt(req.body.height || "-1");
if (
isNaN(width) ||
isNaN(height) ||
width < 1 ||
height < 1 ||
width > 10000 ||
height > 10000
) {
res.status(400).json({ success: false, error: "what are you doing" });
return;
}
await Canvas.setSize(width, height);
const user = (await User.fromAuthSession(req.session.user!))!;
const auditLog = AuditLog.Factory(user.sub)
.doing("CANVAS_SIZE")
.reason(req.header("X-Audit") || null)
.withComment(`Changed canvas size to ${width}x${height}`)
.create();
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
/**
* Get canvas frozen status
*/
app.get("/canvas/freeze", async (req, res) => {
res.send({ success: true, frozen: Canvas.frozen });
});
/**
* Freeze the canvas
*
* @header X-Audit
*/
app.post("/canvas/freeze", async (req, res) => {
await Canvas.setFrozen(true);
const user = (await User.fromAuthSession(req.session.user!))!;
const auditLog = AuditLog.Factory(user.sub)
.doing("CANVAS_FREEZE")
.reason(req.header("X-Audit") || null)
.withComment(`Freezed the canvas`)
.create();
res.send({ success: true, auditLog });
});
/**
* Unfreeze the canvas
*
* @header X-Audit
*/
app.delete("/canvas/freeze", async (req, res) => {
await Canvas.setFrozen(false);
const user = (await User.fromAuthSession(req.session.user!))!;
const auditLog = AuditLog.Factory(user.sub)
.doing("CANVAS_UNFREEZE")
.reason(req.header("X-Audit") || null)
.withComment(`Un-Freezed the canvas`)
.create();
res.send({ success: true, auditLog });
});

Grant
committed
app.put("/canvas/heatmap", async (req, res) => {
try {
await Canvas.generateHeatmap();
res.send({ success: true });
} catch (e) {
Logger.error(e);
res.send({ success: false, error: "Failed to generate" });
}
});
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
app.post("/canvas/forceUpdateTop", async (req, res) => {
Logger.info("Starting force updating isTop");
await Canvas.forceUpdatePixelIsTop();
Logger.info("Finished force updating isTop");
res.send({ success: true });
});
app.get("/canvas/:x/:y", async (req, res) => {
const x = parseInt(req.params.x);
const y = parseInt(req.params.y);
res.json(await Canvas.getPixel(x, y));
});
app.post("/canvas/stress", async (req, res) => {
if (
typeof req.body?.width !== "number" ||
typeof req.body?.height !== "number"
) {
res.status(400).json({ success: false, error: "width/height is invalid" });
return;
}
const width: number = req.body.width;
const height: number = req.body.height;
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let color = Math.floor(Math.random() * 30) + 1;
SocketServer.instance.io.emit("pixel", {
x,
y,
color,
});
}
}
res.send("ok");
});
/**
* Fill an area
*
* @header X-Audit
* @body start.x number
* @body start.y number
* @body end.x number
* @body end.y number
* @body color number Palette color index
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
*/
app.put("/canvas/fill", async (req, res) => {
if (
typeof req.body?.start?.x !== "number" ||
typeof req.body?.start?.y !== "number"
) {
res
.status(400)
.json({ success: false, error: "start position is invalid" });
return;
}
if (
typeof req.body?.end?.x !== "number" ||
typeof req.body?.end?.y !== "number"
) {
res.status(400).json({ success: false, error: "end position is invalid" });
return;
}
if (typeof req.body.color !== "number") {
res.status(400).json({ success: false, error: "color is invalid" });
return;
}
const user_sub =
req.session.user!.user.username +
"@" +
req.session.user!.service.instance.hostname;
const start_position: [x: number, y: number] = [
req.body.start.x,
req.body.start.y,
];
const end_position: [x: number, y: number] = [req.body.end.x, req.body.end.y];
const palette = await prisma.paletteColor.findFirst({
where: { id: req.body.color },
});
if (!palette) {
res.status(400).json({ success: false, error: "invalid color" });
return;
}
const width = end_position[0] - start_position[0];
const height = end_position[1] - start_position[1];
const area = width * height;
// if (area > 50 * 50) {
// res.status(400).json({ success: false, error: "Area too big" });
// return;
// }
await Canvas.fillArea(
{ sub: user_sub },
start_position,
end_position,
palette.hex
);
SocketServer.instance.io.emit(
"square",
start_position,
end_position,
palette.id
);
const user = (await User.fromAuthSession(req.session.user!))!;
const auditLog = await AuditLog.Factory(user.sub)
.doing("CANVAS_FILL")
.reason(req.header("X-Audit") || null)
.withComment(
`Filled (${start_position.join(",")}) -> (${end_position.join(",")}) with ${palette.hex}`
)
.create();
res.json({ success: true, auditLog });
/**
* Create or ban a user
*
* @header X-Audit
* @param :sub User sub claim
* @body expiresAt string! ISO date time string
* @body publicNote string?
* @body privateNote string?
*/
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
app.put("/user/:sub/ban", async (req, res) => {
let user: User;
let expires: Date;
let publicNote: string | undefined | null;
let privateNote: string | undefined | null;
try {
user = await User.fromSub(req.params.sub);
} catch (e) {
if (e instanceof UserNotFound) {
res.status(404).json({ success: false, error: "User not found" });
} else {
Logger.error(`/user/${req.params.sub}/ban Error ` + (e as any)?.message);
res.status(500).json({ success: false, error: "Internal error" });
}
return;
}
if (typeof req.body.expiresAt !== "string") {
res
.status(400)
.json({ success: false, error: "expiresAt is not a string" });
return;
}
expires = new Date(req.body.expiresAt);
if (!isFinite(expires.getTime())) {
res
.status(400)
.json({ success: false, error: "expiresAt is not a valid date" });
return;
}
if (typeof req.body.publicNote !== "undefined") {
if (
typeof req.body.publicNote !== "string" &&
req.body.privateNote !== null
) {
res.status(400).json({
success: false,
error: "publicNote is set and is not a string",
});
return;
}
publicNote = req.body.publicNote;
}
if (typeof req.body.privateNote !== "undefined") {
if (
typeof req.body.privateNote !== "string" &&
req.body.privateNote !== null
) {
res.status(400).json({
success: false,
error: "privateNote is set and is not a string",
});
return;
}
privateNote = req.body.privateNote;
}
const existingBan = user.getBan();
const ban = await user.ban(expires, publicNote, privateNote);
let shouldNotifyUser = false;
if (existingBan) {
if (existingBan.expires.getTime() !== ban.expiresAt.getTime()) {
shouldNotifyUser = true;
}
} else {
shouldNotifyUser = true;
}
if (shouldNotifyUser) {
user.notify({
is: "modal",
action: "moderation",
dismissable: true,
message_key: "banned",
metadata: {
until: expires.toISOString(),
},
});
}
user.updateStanding();
.doing(existingBan ? "BAN_UPDATE" : "BAN_CREATE")
.reason(req.header("X-Audit") || null)
.withComment(
existingBan
? `Updated ban on ${user.sub}`
: `Created a ban for ${user.sub}`
)
.withBan(ban)
.create();
/**
* Delete a user ban
*
* @header X-Audit
* @param :sub User sub
*/
app.delete("/user/:sub/ban", async (req, res) => {
// delete ban ("unban")
let user: User;
try {
user = await User.fromSub(req.params.sub);
} catch (e) {
if (e instanceof UserNotFound) {
res.status(404).json({ success: false, error: "User not found" });
} else {
Logger.error(`/user/${req.params.sub}/ban Error ` + (e as any)?.message);
res.status(500).json({ success: false, error: "Internal error" });
}
return;
}
try {
await user.unban();
} catch (e) {
if (e instanceof UserNotBanned) {
res.status(404).json({ success: false, error: "User is not banned" });
} else {
Logger.error(
`/instance/${req.params.sub}/ban Error ` + (e as any)?.message
);
res.status(500).json({ success: false, error: "Internal error" });
}
return;
}
user.notify({
is: "modal",
action: "moderation",
dismissable: true,
message_key: "unbanned",
metadata: {},
});
await user.update(true);
user.updateStanding();
.doing("BAN_DELETE")
.reason(req.header("X-Audit") || null)
.withComment(`Deleted ban for ${user.sub}`)
.create();
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
});
app.get("/instance/:domain/ban", async (req, res) => {
// get ban information
let instance: Instance;
try {
instance = await Instance.fromDomain(req.params.domain);
} catch (e) {
if (e instanceof InstanceNotFound) {
res.status(404).json({ success: false, error: "instance not found" });
} else {
Logger.error(
`/instance/${req.params.domain}/ban Error ` + (e as any)?.message
);
res.status(500).json({ success: false, error: "Internal error" });
}
return;
}
const ban = await instance.getEffectiveBan();
if (!ban) {
return res
.status(404)
.json({ success: false, error: "Instance not banned" });
}
res.json({ success: true, ban });
});
/**
* Create or update a ban for an instance (and subdomains)
*
* @header X-Audit
* @param :domain Domain for the instance
* @body expiresAt string! ISO date time string
* @body publicNote string?
* @body privateNote string?
*/
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
app.put("/instance/:domain/ban", async (req, res) => {
// ban domain & subdomains
let instance: Instance;
let expires: Date;
let publicNote: string | null | undefined;
let privateNote: string | null | undefined;
try {
instance = await Instance.fromDomain(req.params.domain);
} catch (e) {
if (e instanceof InstanceNotFound) {
res.status(404).json({ success: false, error: "instance not found" });
} else {
Logger.error(
`/instance/${req.params.domain}/ban Error ` + (e as any)?.message
);
res.status(500).json({ success: false, error: "Internal error" });
}
return;
}
if (typeof req.body.expiresAt !== "string") {
res
.status(400)
.json({ success: false, error: "expiresAt is not a string" });
return;
}
expires = new Date(req.body.expiresAt);
if (!isFinite(expires.getTime())) {
res
.status(400)
.json({ success: false, error: "expiresAt is not a valid date" });
return;
}
if (typeof req.body.publicNote !== "undefined") {
if (
typeof req.body.publicNote !== "string" &&
req.body.privateNote !== null
) {
res.status(400).json({
success: false,
error: "publicNote is set and is not a string",
});
return;
}
publicNote = req.body.publicNote;
}
if (typeof req.body.privateNote !== "undefined") {
if (
typeof req.body.privateNote !== "string" &&
req.body.privateNote !== null
) {
res.status(400).json({
success: false,
error: "privateNote is set and is not a string",
});
return;
}
privateNote = req.body.privateNote;
}
const hasExistingBan = await instance.getBan();
const user = (await User.fromAuthSession(req.session.user!))!;
const ban = await instance.ban(expires, publicNote, privateNote);
.doing(hasExistingBan ? "BAN_UPDATE" : "BAN_CREATE")
.reason(req.header("X-Audit") || null)
.withComment(
hasExistingBan
? `Updated ban for ${instance.hostname}`
: `Created a ban for ${instance.hostname}`
)
.withBan(ban)
.create();
res.json({
success: true,
/**
* Delete an instance ban
*
* @header X-Audit
* @param :domain The instance domain
*/
app.delete("/instance/:domain/ban", async (req, res) => {
// unban domain & subdomains
let instance: Instance;
try {
instance = await Instance.fromDomain(req.params.domain);
} catch (e) {
if (e instanceof InstanceNotFound) {
res.status(404).json({ success: false, error: "instance not found" });
} else {
Logger.error(
`/instance/${req.params.domain}/ban Error ` + (e as any)?.message
);
res.status(500).json({ success: false, error: "Internal error" });
}
return;
}
} catch (e) {
if (e instanceof InstanceNotBanned) {
res.status(404).json({ success: false, error: "instance not banned" });
} else {
Logger.error(
`/instance/${req.params.domain}/ban Error ` + (e as any)?.message
);
res.status(500).json({ success: false, error: "Internal error" });
}
return;
}
.doing("BAN_DELETE")
.reason(req.header("X-Audit") || null)
.withComment(`Deleted ban for ${instance.hostname}`)
.create();
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
});
/**
* Get all audit logs
*
* TODO: pagination
*/
app.get("/audit", async (req, res) => {
const auditLogs = await prisma.auditLog.findMany({
orderBy: {
createdAt: "desc",
},
});
res.json({ success: true, auditLogs });
});
/**
* Get audit log entry by ID
*
* @param :id Audit log ID
*/
app.get("/audit/:id", async (req, res) => {
let id = parseInt(req.params.id);
if (isNaN(id)) {
return res
.status(400)
.json({ success: false, error: "id is not a number" });
}
const auditLog = await prisma.auditLog.findFirst({ where: { id } });
if (!auditLog) {
return res
.status(404)
.json({ success: false, error: "Audit log not found" });
}
res.json({ success: true, auditLog });
});
/**
* Update audit log reason
*
* @param :id Audit log id
* @body reason string|null
*/
app.put("/audit/:id/reason", async (req, res) => {
let id = parseInt(req.params.id);
let reason: string;
if (isNaN(id)) {
return res
.status(400)
.json({ success: false, error: "id is not a number" });
}
if (typeof req.body.reason !== "string" && req.body.reason !== null) {
return res
.status(400)
.json({ success: false, error: "reason is not a string or null" });
}
reason = req.body.reason;
const auditLog = await prisma.auditLog.findFirst({
where: {
id,
},
});
if (!auditLog) {
return res
.status(404)
.json({ success: false, error: "audit log is not found" });
}
const newAudit = await prisma.auditLog.update({
where: { id },
data: {
reason,
updatedAt: new Date(),
},
});
res.json({
success: true,
auditLog: newAudit,
});