-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.tsx
More file actions
793 lines (739 loc) · 34.1 KB
/
server.tsx
File metadata and controls
793 lines (739 loc) · 34.1 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
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
485
486
487
488
489
490
491
492
493
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Moepictures - A cute and moe anime image board ❤ *
* Copyright © 2026 Moebytes <moebytes.com> *
* Licensed under CC BY-NC 4.0. See license.txt for details. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
import "dotenv/config"
import path from "path"
import cors from "cors"
import mime from "mime"
import {Readable} from "stream"
import {Pool} from "pg"
import fs from "fs"
import express, {Request, Response, NextFunction} from "express"
import session from "express-session"
import PGSession from "connect-pg-simple"
import rateLimit from "express-rate-limit"
import {renderToString} from "react-dom/server"
import {StaticRouter as Router} from "react-router-dom"
import {Provider} from "react-redux"
import store from "./store"
import permissions from "./structures/Permissions"
import functions from "./functions/Functions"
import encryption from "./structures/Encryption"
import serverFunctions, {keyGenerator, handler, apiKeyLogin, csrfProtection} from "./server/ServerFunctions"
import sql from "./sql/SQLQuery"
import $2FARoutes from "./routes/2FARoutes"
import CommentRoutes from "./routes/CommentRoutes"
import CutenessRoutes from "./routes/CutenessRoutes"
import FavoriteRoutes from "./routes/FavoriteRoutes"
import MiscRoutes from "./routes/MiscRoutes"
import PostRoutes from "./routes/PostRoutes"
import SearchRoutes from "./routes/SearchRoutes"
import TagRoutes from "./routes/TagRoutes"
import UploadRoutes from "./routes/UploadRoutes"
import UserRoutes from "./routes/UserRoutes"
import NoteRoutes from "./routes/NoteRoutes"
import ThreadRoutes from "./routes/ThreadRoutes"
import MessageRoutes from "./routes/MessageRoutes"
import GroupRoutes from "./routes/GroupRoutes"
import App from "./App"
import torIPs from "./assets/json/tor-ip.json"
import {imageLock, imageMissing} from "./structures/ImageLock"
import {ServerSession, Storage, PostFull} from "./types/Types"
const __dirname = path.resolve()
const app = express() as any
app.use(express.urlencoded({extended: true, limit: "300mb", parameterLimit: 10000}))
app.use(express.json({limit: "300mb"}))
app.use(cors({credentials: true, origin: true}))
app.disable("x-powered-by")
app.set("trust proxy", "loopback")
declare module "express-session" {
interface SessionData extends ServerSession {}
}
const pgPool = functions.config.isLocalHost() ? new Pool({
user: process.env.PG_LOCAL_USER,
host: process.env.PG_LOCAL_HOST,
database: process.env.PG_LOCAL_DATABASE,
password: process.env.PG_LOCAL_PASSWORD,
port: Number(process.env.PG_LOCAL_PORT)
}) : new Pool({
user: process.env.PG_USER,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: Number(process.env.PG_PORT)
})
const pgSession = PGSession(session)
app.use(session({
store: new pgSession({
pool: pgPool,
tableName: "sessions",
sidColumnName: "sessionID",
sessColumnName: "session",
expireColumnName: "expires"
}),
secret: process.env.COOKIE_SECRET!,
cookie: {maxAge: 30 * 24 * 60 * 60 * 1000, sameSite: "lax", secure: process.env.TESTING === "no"},
rolling: true,
resave: false,
saveUninitialized: false
}))
app.use(express.static(path.join(__dirname, "./public")))
app.use(express.static(path.join(__dirname, "./dist/client"), {index: false}))
app.use("/emojis", express.static(path.join(__dirname, "./assets/emojis"), {maxAge: 2678400}))
app.use(apiKeyLogin)
let blacklist = null as unknown as Set<string>
app.use(async (req: Request, res: Response, next: NextFunction) => {
if (!blacklist) {
const blacklistObj = await sql.report.blacklist()
const blacklistSet = new Set(torIPs)
for (const entry of blacklistObj) {
blacklistSet.add(entry.ip?.trim())
}
blacklist = blacklistSet
}
let ip = serverFunctions.util.ip(req)
if (["127.0.0.1", "::1", "0.0.0.0"].includes(ip)) return next()
if (!req.session.username) {
const sessionCount = await sql.user.countIPSessions(ip)
if (sessionCount > 300) {
const allowedBot = await serverFunctions.util.isAllowedBot(ip)
if (!allowedBot) {
// Created over 300 sessions in a 24 hour period, spam bot?
await sql.report.insertBlacklist(ip, "automatic")
await sql.user.pruneIPSessions(ip)
blacklist.add(ip)
} else {
await sql.user.destroyOtherIPSessions(ip, req.sessionID)
}
}
}
if (blacklist.has(ip)) {
return res.status(403).json({message: "Your IP address has been blocked."})
}
if (ip !== req.session.ip) req.session.ip = ip
req.session.url = req.originalUrl || req.url || ""
next()
})
$2FARoutes(app)
CommentRoutes(app)
CutenessRoutes(app)
FavoriteRoutes(app)
MiscRoutes(app)
PostRoutes(app)
SearchRoutes(app)
TagRoutes(app)
UploadRoutes(app)
UserRoutes(app)
NoteRoutes(app)
ThreadRoutes(app)
MessageRoutes(app)
GroupRoutes(app)
const imageLimiter = rateLimit({
windowMs: 60 * 1000,
max: 2000,
standardHeaders: true,
legacyHeaders: false,
keyGenerator,
handler
})
const imageUpdateLimiter = rateLimit({
windowMs: 60 * 1000,
max: 100,
standardHeaders: true,
legacyHeaders: false,
keyGenerator,
handler
})
app.post("/api/misc/blacklistip", imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
const {ip, reason} = req.body as {ip: string, reason: string}
if (!req.session.username || !req.session.emailVerified) return res.status(403).send("Unauthorized")
if (!permissions.isAdmin(req.session)) return res.status(403).end()
if (!ip) return res.status(400).send("Bad ip")
await sql.report.insertBlacklist(ip, reason)
await sql.user.pruneIPSessions(ip)
blacklist = null as any
res.status(200).send("Success")
})
app.delete("/api/misc/unblacklistip", imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
const {ip} = req.query as {ip: string}
if (!req.session.username || !req.session.emailVerified) return res.status(403).send("Unauthorized")
if (!permissions.isAdmin(req.session)) return res.status(403).end()
if (!ip) return res.status(400).send("Bad ip")
await sql.report.deleteBlacklist(ip)
blacklist = null as any
res.status(200).send("Success")
})
let originalFolders = ["image", "comic", "animation", "video", "audio", "model", "live2d"]
let originalEncrypted = ["image", "comic", "animation", "audio", "model", "live2d"]
let noCache = ["artist", "character", "series", "pfp", "tag", "history"]
let folders = [...originalFolders, ...originalFolders.map((folder) => `${folder}-upscaled`), ...noCache]
let encrypted = [...originalEncrypted, ...originalEncrypted.map((folder) => `${folder}-upscaled`)]
const lastModified = new Date().toUTCString()
for (let i = 0; i < folders.length; i++) {
app.get(`/${folders[i]}/*`, imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
try {
const referer = req.headers.referer || req.headers.referrer as string
if (!serverFunctions.util.isAllowedReferer(referer)) return res.status(403).end()
const pixelHash = new URL(`${functions.config.getDomain()}${req.originalUrl}`).searchParams.get("hash") ?? ""
const upscaleParam = new URL(`${functions.config.getDomain()}${req.originalUrl}`).searchParams.get("upscaled") ?? ""
let url = req.url.replace(/\?.*$/, "")
const mimeType = mime.getType(req.path)
if (mimeType) res.setHeader("Content-Type", mimeType)
if (folders[i] === "tag") {
if (!url.endsWith(".png") && !url.endsWith(".jpg") && !url.endsWith(".jpeg") &&
!url.endsWith(".webp") && !url.endsWith(".gif")) return next()
}
res.setHeader("Last-Modified", lastModified)
res.setHeader("Cache-Control", "public, max-age=2678400")
const key = decodeURIComponent(req.path.slice(1))
let upscaled = req.session.upscaledImages ?? false
if (upscaleParam) upscaled = upscaleParam === "true"
if (req.headers["x-force-upscale"]) upscaled = req.headers["x-force-upscale"] === "true"
if (req.session.captchaNeeded) upscaled = false
let r18 = false
const postID = key.match(/(?<=\/)\d+(?=-)/)?.[0]
if (!noCache.includes(folders[i]) && postID) {
let post = await sql.getCache(`cached-post/${postID}`) as PostFull | undefined
if (!post) {
post = await sql.post.post(postID)
await sql.setCache(`cached-post/${postID}`, post)
}
if (post && functions.post.isR18(post.rating)) {
if (!req.session.showR18) return res.status(404).end()
r18 = true
}
if (post && post.hidden) {
if (!permissions.isMod(req.session)) return res.status(404).end()
}
if (post) {
let matches = req.path.includes("history/post")
for (const image of post.images) {
if (image.pixelHash === pixelHash) matches = true
}
if (!matches) return res.status(404).end()
}
}
let body = await serverFunctions.files.getFile(key, upscaled, r18, pixelHash)
if (!body.byteLength) body = await serverFunctions.files.getFile(key, false, r18, pixelHash)
let contentLength = body.byteLength
if (!contentLength) return res.status(404).end()
if (!noCache.includes(folders[i]) && req.session.captchaNeeded) {
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
body = await imageLock(body, false)
return res.status(200).send(body)
}
if (encrypted.includes(folders[i]) || req.path.includes("history/post")) {
if (!permissions.noEncryption(req.session) && !req.session.publicKey) return res.status(401).end()
body = encryption.encrypt(body, req.session.publicKey!, req.session)
}
if (req.headers.range) {
const parts = req.headers.range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0])
const end = parts[1] ? parseInt(parts[1]) : contentLength - 1
res.writeHead(206, {
"Content-Range": `bytes ${start}-${end}/${contentLength}`,
"Accept-Ranges": "bytes",
"Content-Length": end - start + 1
})
const stream = Readable.from(body.subarray(start, end + 1))
return stream.pipe(res)
}
res.setHeader("Content-Length", contentLength)
res.status(200).send(body)
} catch {
res.status(400).end()
}
})
app.get(`/thumbnail/${folders[i]}/*`, imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
try {
const referer = req.headers.referer || req.headers.referrer as string
if (!serverFunctions.util.isAllowedReferer(referer)) return res.status(403).end()
const pixelHash = new URL(`${functions.config.getDomain()}${req.originalUrl}`).searchParams.get("hash") ?? ""
const mimeType = mime.getType(req.path)
if (mimeType) res.setHeader("Content-Type", mimeType)
res.setHeader("Last-Modified", lastModified)
res.setHeader("Cache-Control", "public, max-age=2678400")
const key = decodeURIComponent(req.path.replace(`/thumbnail/`, ""))
let r18 = false
const postID = key.match(/(?<=\/)\d+(?=-)/)?.[0]
if (!noCache.includes(folders[i]) && postID) {
let post = await sql.getCache(`cached-post/${postID}`) as PostFull | undefined
if (!post) {
post = await sql.post.post(postID)
await sql.setCache(`cached-post/${postID}`, post)
}
if (post && functions.post.isR18(post.rating)) {
if (!req.session.showR18) return res.status(404).end()
r18 = true
}
if (post && post.hidden) {
if (!permissions.isMod(req.session)) return res.status(404).end()
}
if (post) {
let matches = req.path.includes("history/post")
for (const image of post.images) {
if (image.pixelHash === pixelHash) matches = true
}
if (!matches) return res.status(404).end()
}
}
const [id, order, name] = path.basename(key, path.extname(key)).split("-")
let thumbKey = `thumbnail/${folders[i]}/${id}-${order}${path.extname(key)}`
if (req.path.includes("history/post")) thumbKey = key
let body = await serverFunctions.files.getFile(thumbKey, false, r18, pixelHash)
if (!body.byteLength) body = await serverFunctions.files.getFile(key, false, r18, pixelHash)
let contentLength = body.byteLength
if (!contentLength) return res.status(404).end()
if (!noCache.includes(folders[i]) && req.session.captchaNeeded) {
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
body = await imageLock(body)
return res.status(200).send(body)
}
if (encrypted.includes(folders[i]) || req.path.includes("history/post")) {
if (!permissions.noEncryption(req.session) && !req.session.publicKey) return res.status(401).end()
body = encryption.encrypt(body, req.session.publicKey!, req.session)
}
if (req.headers.range) {
const parts = req.headers.range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0])
const end = parts[1] ? parseInt(parts[1]) : contentLength - 1
res.writeHead(206, {
"Content-Range": `bytes ${start}-${end}/${contentLength}`,
"Accept-Ranges": "bytes",
"Content-Length": end - start + 1
})
const stream = Readable.from(body.subarray(start, end + 1))
return stream.pipe(res)
}
res.setHeader("Content-Length", contentLength)
res.status(200).send(body)
} catch (e) {
console.log(e)
res.status(400).end()
}
})
app.get(`/unverified/${folders[i]}/*`, imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
try {
const referer = req.headers.referer || req.headers.referrer as string
if (!serverFunctions.util.isAllowedReferer(referer)) return res.status(403).end()
const upscaleParam = new URL(`${functions.config.getDomain()}${req.originalUrl}`).searchParams.get("upscaled") ?? ""
const mimeType = mime.getType(req.path)
if (mimeType) res.setHeader("Content-Type", mimeType)
res.setHeader("Cache-Control", "public, max-age=2678400")
const key = decodeURIComponent(req.path.replace("/unverified/", ""))
const postID = key.match(/(?<=\/)\d+(?=-)/)?.[0]
if (!noCache.includes(folders[i]) && postID) {
const post = await sql.post.unverifiedPost(postID)
if (post?.uploader !== req.session.username && !permissions.isMod(req.session)) return res.status(404).end()
} else {
if (!permissions.isMod(req.session)) return res.status(404).end()
}
let upscaled = false
if (folders[i] === "image" || folders[i] === "comic" || folders[i] === "animation") {
upscaled = req.session.upscaledImages ?? false
if (upscaleParam) upscaled = upscaleParam === "true"
if (req.headers["x-force-upscale"]) upscaled = req.headers["x-force-upscale"] === "true"
}
const body = await serverFunctions.files.getUnverifiedFile(key, upscaled)
const contentLength = body.byteLength
if (!contentLength) {
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
const noImg = await imageMissing()
return res.status(200).send(noImg)
}
if (req.headers.range) {
const parts = req.headers.range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0])
const end = parts[1] ? parseInt(parts[1]) : contentLength - 1
res.writeHead(206, {
"Content-Range": `bytes ${start}-${end}/${contentLength}`,
"Accept-Ranges": "bytes",
"Content-Length": end - start + 1
})
const stream = Readable.from(body.subarray(start, end + 1))
return stream.pipe(res)
}
res.setHeader("Content-Length", contentLength)
res.status(200).send(body)
} catch (e) {
console.log(e)
res.status(400).end()
}
})
app.get(`/thumbnail/unverified/${folders[i]}/*`, imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
try {
const referer = req.headers.referer || req.headers.referrer as string
if (!serverFunctions.util.isAllowedReferer(referer)) return res.status(403).end()
const mimeType = mime.getType(req.path)
if (mimeType) res.setHeader("Content-Type", mimeType)
res.setHeader("Cache-Control", "public, max-age=2678400")
const key = decodeURIComponent(req.path.replace(`/thumbnail/unverified/`, ""))
const postID = key.match(/(?<=\/)\d+(?=-)/)?.[0]
if (!noCache.includes(folders[i]) && postID) {
const post = await sql.post.unverifiedPost(postID)
if (post?.uploader !== req.session.username && !permissions.isMod(req.session)) return res.status(404).end()
} else {
if (!permissions.isMod(req.session)) return res.status(404).end()
}
const [id, order, name] = path.basename(key, path.extname(key)).split("-")
let thumbKey = `thumbnail/${folders[i]}/${id}-${order}${path.extname(key)}`
if (req.path.includes("history/post")) thumbKey = key
let body = await serverFunctions.files.getUnverifiedFile(thumbKey, false)
if (!body.byteLength) body = await serverFunctions.files.getUnverifiedFile(key, false)
let contentLength = body.byteLength
if (!contentLength) {
res.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, proxy-revalidate")
const noImg = await imageMissing()
return res.status(200).send(noImg)
}
if (req.headers.range) {
const parts = req.headers.range.replace(/bytes=/, "").split("-")
const start = parseInt(parts[0])
const end = parts[1] ? parseInt(parts[1]) : contentLength - 1
res.writeHead(206, {
"Content-Range": `bytes ${start}-${end}/${contentLength}`,
"Accept-Ranges": "bytes",
"Content-Length": end - start + 1
})
const stream = Readable.from(body.subarray(start, end + 1))
return stream.pipe(res)
}
res.setHeader("Content-Length", contentLength)
res.status(200).send(body)
} catch {
res.status(400).end()
}
})
}
const storageMap = new Map<string, Storage>()
app.post("/storage", imageUpdateLimiter, csrfProtection, async (req: Request, res: Response, next: NextFunction) => {
try {
const upscaleParam = new URL(`${functions.config.getDomain()}${req.originalUrl}`).searchParams.get("upscaled") ?? ""
const {link, songCover} = req.body as {link: string, songCover?: boolean}
let ip = serverFunctions.util.ip(req)
let userKey = req.session.username ? req.session.username : ip
const pixelHash = new URL(link).searchParams.get("hash") ?? ""
const key = decodeURIComponent(link.replace(/\?.*$/, "").split("/").slice(3).join("/"))
let upscaled = req.session.upscaledImages ?? false
if (upscaleParam) upscaled = upscaleParam === "true"
if (req.headers["x-force-upscale"]) upscaled = req.headers["x-force-upscale"] === "true"
if (req.session.captchaNeeded) {
storageMap.delete(userKey)
return res.status(403).end()
}
const postID = key.match(/(?<=\/)\d+(?=-)/)?.[0]
let r18 = false
if (postID) {
let post = await sql.getCache(`cached-post/${postID}`) as PostFull | undefined
if (!post) {
post = await sql.post.post(postID)
await sql.setCache(`cached-post/${postID}`, post)
}
if (post && functions.post.isR18(post.rating)) {
if (!req.session.showR18) return res.status(404).end()
r18 = true
}
if (post && post.hidden) {
if (!permissions.isMod(req.session)) return res.status(404).end()
}
if (post) {
let matches = req.path.includes("history/post")
for (const image of post.images) {
if (image.pixelHash === pixelHash) matches = true
}
if (!matches) return res.status(404).end()
}
}
const secret = encryption.generateAPIKey(16)
storageMap.set(userKey, {secret, key, upscaled, r18, pixelHash, songCover})
let ext = songCover ? ".jpg" : path.extname(key)
const url = `${functions.config.getDomain()}/storage/${userKey}${ext}?secret=${secret}`
res.status(200).send(url)
} catch {
res.status(400).end()
}
})
app.get("/storage/:username", imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
try {
const secret = req.query.secret
const storage = storageMap.get(path.basename(req.params.username, path.extname(req.params.username)))
if (!storage) return res.status(404).json({message: "Not found"})
if (secret !== storage.secret) return res.status(403).json({message: "Unauthorized"})
let body = await serverFunctions.files.getFile(storage.key, storage.upscaled, storage.r18, storage.pixelHash)
if (!body.byteLength) body = await serverFunctions.files.getFile(storage.key, false, storage.r18, storage.pixelHash)
if (storage.songCover) body = await serverFunctions.util.songCover(body)
const mimeType = mime.getType(req.path)
if (mimeType) res.setHeader("Content-Type", mimeType)
res.setHeader("Content-Length", body.byteLength)
res.status(200).send(body)
} catch (e) {
console.log(e)
res.status(400).end()
}
})
app.get("/social-preview/:id", imageLimiter, async (req: Request, res: Response, next: NextFunction) => {
try {
if (req.session.captchaNeeded) return void res.status(403).end()
const pixelHash = new URL(`${functions.config.getDomain()}${req.originalUrl}`).searchParams.get("hash") ?? ""
const postID = path.basename(req.params.id, path.extname(req.params.id))
let body = Buffer.from("")
let r18 = false
if (postID) {
let post = await sql.getCache(`cached-post/${postID}`) as PostFull | undefined
if (!post) {
post = await sql.post.post(postID)
await sql.setCache(`cached-post/${postID}`, post)
}
if (post && functions.post.isR18(post.rating)) {
if (!req.session.showR18) return void res.status(404).end()
r18 = true
}
if (post && post.hidden) {
if (!permissions.isMod(req.session)) return void res.status(404).end()
}
if (post) {
let matches = req.path.includes("history/post")
for (const image of post.images) {
if (image.pixelHash === pixelHash) matches = true
}
if (!matches) return res.status(404).end()
}
if (post) {
const img = post.images[0]
const imagePath = img.thumbnail ? functions.link.getThumbnailImagePath(img.type, img.thumbnail)
: functions.link.getImagePath(img.type, img.postID, img.order, img.filename)
const imageBuffer = await serverFunctions.files.getFile(imagePath, false, r18, post.images[0].pixelHash)
body = await serverFunctions.util.squareCrop(imageBuffer, 500)
}
}
const mimeType = mime.getType(req.path)
if (mimeType) res.setHeader("Content-Type", mimeType)
res.setHeader("Content-Length", body.byteLength)
res.status(200).send(body)
} catch {
res.status(400).end()
}
})
app.get("/*", async (req: Request, res: Response) => {
try {
if (/\.\w+$/.test(req.path) && process.env.TESTING !== "yes") {
return res.status(404).json({message: "Path not found."})
}
if (!req.session.csrfToken) {
const {secret, token} = serverFunctions.generateCSRF()
req.session.csrfSecret = secret
req.session.csrfToken = token
}
const mimeType = mime.getType(req.path)
if (mimeType) res.setHeader("Content-Type", mimeType)
//res.setHeader("Cross-Origin-Opener-Policy", "same-origin")
//res.setHeader("Cross-Origin-Embedder-Policy", "require-corp")
const document = fs.readFileSync(path.join(__dirname, "./dist/client/index.html"), {encoding: "utf-8"})
let title = "Moepictures: Cute Anime Girl Art"
let description = "Search for cute and moe anime girl artwork. With our detailed tagging system you can easily find your favorite characters and artists."
let image = "/assets/images/mainimg.png"
let url = "https://moepictures.net"
const key = decodeURIComponent(req.path.slice(1))
const postID = key.match(/(?<=post\/)\d+(?=\/)/)?.[0]
if (postID) {
let post = await sql.getCache(`cached-post/${postID}`) as PostFull | undefined
if (!post) {
post = await sql.post.post(postID)
await sql.setCache(`cached-post/${postID}`, post)
}
if (post && functions.post.isR18(post.rating)) {
if (!req.session.showR18) post = undefined
}
if (post && post.hidden) {
if (!permissions.isMod(req.session)) post = undefined
}
if (post) {
title = `${post.englishTitle || post.title} by ${post.artist}`
description = post.englishCommentary || post.commentary || ""
const img = post.images[0]
image = `${functions.config.getDomain()}/social-preview/${post.postID}${path.extname(img.filename)}?hash=${img.pixelHash}`
url = `${functions.config.getDomain()}/post/${post.postID}/${post.slug}`
}
}
const newDocument = document
.replace(/<title>.*?<\/title>/, `<title>${title}</title>`)
.replace(/<meta name="description" content=".*?">/, `<meta name="description" content="${description}">`)
.replace(/<meta property="og:title" content=".*?">/, `<meta property="og:title" content="${title}">`)
.replace(/<meta property="og:description" content=".*?">/, `<meta property="og:description" content="${description}">`)
.replace(/<meta property="og:image" content=".*?">/, `<meta property="og:image" content="${image}">`)
.replace(/<meta property="og:url" content=".*?">/, `<meta property="og:url" content="${url}">`)
const html = renderToString(<Router location={req.url}><Provider store={store}><App/></Provider></Router>)
res.status(200).send(newDocument.replace(`<div id="root"></div>`, `<div id="root">${html}</div>`))
} catch (e) {
console.log(e)
return res.status(500).json({message: "Internal server error."})
}
})
const defaultTagInserts = async () => {
/** Unverified tags */
await sql.tag.insertUnverifiedTag("unknown-artist", "artist")
await sql.tag.insertUnverifiedTag("unknown-character", "character")
await sql.tag.insertUnverifiedTag("unknown-series", "series")
await sql.tag.insertUnverifiedTag("needs-tags", "meta")
/* Default artist tags */
let exists = await sql.tag.insertTag("unknown-artist", "artist")
if (!exists) await sql.tag.updateTag("unknown-artist", "description", "The artist is unknown.")
exists = await sql.tag.insertTag("original", "artist")
if (!exists) await sql.tag.updateTag("original", "description", "The character is an original creation, ie. this is not fanart.")
exists = await sql.tag.insertTag("official-art", "artist")
if (!exists) await sql.tag.updateTag("official-art", "description", "Art made by the official company of the series (where the original artist is unknown).")
/* Default character tags */
exists = await sql.tag.insertTag("unknown-character", "character")
if (!exists) await sql.tag.updateTag("unknown-character", "description", "The character is unknown.")
exists = await sql.tag.insertTag("no-character", "character")
if (!exists) await sql.tag.updateTag("no-character", "description", "The character is not applicable.")
/* Default series tags */
exists = await sql.tag.insertTag("unknown-series", "series")
if (!exists) await sql.tag.updateTag("unknown-series", "description", "The series is unknown.")
exists = await sql.tag.insertTag("no-series", "series")
if (!exists) await sql.tag.updateTag("no-series", "description", "The series is not applicable.")
/* Default meta tags */
exists = await sql.tag.insertTag("needs-tags", "meta")
if (!exists) await sql.tag.updateTag("needs-tags", "description", "The post needs tags.")
exists = await sql.tag.insertTag("no-audio", "meta")
if (!exists) await sql.tag.updateTag("no-audio", "description", "The post is a video with no audio.")
exists = await sql.tag.insertTag("with-audio", "meta")
if (!exists) await sql.tag.updateTag("with-audio", "description", "The post is a video with audio.")
exists = await sql.tag.insertTag("self-upload", "meta")
if (!exists) await sql.tag.updateTag("self-upload", "description", "The artwork was posted by the original creator.")
exists = await sql.tag.insertTag("transparent", "meta")
if (!exists) await sql.tag.updateTag("transparent", "description", "The post has a transparent background.")
exists = await sql.tag.insertTag("text", "meta")
if (!exists) await sql.tag.updateTag("text", "description", "The post has contains text.")
exists = await sql.tag.insertTag("commentary", "meta")
if (!exists) await sql.tag.updateTag("commentary", "description", "The post has artist commentary.")
exists = await sql.tag.insertTag("translated", "meta")
if (!exists) await sql.tag.updateTag("translated", "description", "The post contains complete translations.")
exists = await sql.tag.insertTag("untranslated", "meta")
if (!exists) await sql.tag.updateTag("untranslated", "description", "The post is untranslated.")
exists = await sql.tag.insertTag("partially-translated", "meta")
if (!exists) await sql.tag.updateTag("partially-translated", "description", "Post is only partially translated.")
exists = await sql.tag.insertTag("check-translation", "meta")
if (!exists) await sql.tag.updateTag("check-translation", "description", "Check the translations, because they might be incorrect.")
exists = await sql.tag.insertTag("multiple-artists", "meta")
if (!exists) await sql.tag.updateTag("multiple-artists", "description", "The post has multiple artists.")
exists = await sql.tag.insertTag("bad-pixiv-id", "meta")
if (!exists) await sql.tag.updateTag("bad-pixiv-id", "description", "The pixiv id was deleted.")
exists = await sql.tag.insertTag("paid-reward-available", "meta")
if (!exists) await sql.tag.updateTag("paid-reward-available", "description", "The artist offers a paid reward for this post.")
exists = await sql.tag.insertTag("third-party-edit", "meta")
if (!exists) await sql.tag.updateTag("third-party-edit", "description", "The post is a third party edit.")
exists = await sql.tag.insertTag("third-party-source", "meta")
if (!exists) await sql.tag.updateTag("third-party-source", "description", "The source of the post is a repost (not posted by the original artist).")
}
const deleteExpiredTokens = async () => {
const emailTokens = await sql.token.emailTokens()
const now = new Date()
for (const tokenData of emailTokens) {
const expireDate = new Date(tokenData.expires)
if (now > expireDate) {
await sql.token.deleteEmailToken(tokenData.email)
}
}
const passwordTokens = await sql.token.passwordTokens()
for (const tokenData of passwordTokens) {
const expireDate = new Date(tokenData.expires)
if (now > expireDate) {
await sql.token.deletePasswordToken(tokenData.username)
}
}
const ipTokens = await sql.token.ipTokens()
for (const tokenData of ipTokens) {
const expireDate = new Date(tokenData.expires)
if (now > expireDate) {
await sql.token.deleteIPToken(tokenData.username)
}
}
}
const deleteQueuedPosts = async () => {
const deleted = await sql.search.deletedPosts()
const now = new Date()
for (const post of deleted) {
if (!post.deletionDate) continue
const deletionDate = new Date(post.deletionDate)
if (now > deletionDate) {
try {
await serverFunctions.posts.deletePost(post)
} catch (e) {
console.log(e)
}
}
}
}
const deleteQueuedUnverifiedPosts = async () => {
const deletedUnverified = await sql.search.deletedUnverifiedPosts()
const now = new Date()
for (const unverified of deletedUnverified) {
if (!unverified.deletionDate) continue
const deletionDate = new Date(unverified.deletionDate)
if (now > deletionDate) {
try {
await serverFunctions.posts.deleteUnverifiedPost(unverified)
} catch (e) {
console.log(e)
}
}
}
}
const deleteQueuedUsers = async () => {
const deleted = await sql.user.deletedUsers()
const now = new Date()
for (const user of deleted) {
if (!user.deletionDate) continue
if (user.role === "deleted") continue
const deletionDate = new Date(user.deletionDate)
if (now > deletionDate) {
try {
await serverFunctions.users.deleteUser(user)
} catch (e) {
console.log(e)
}
}
}
}
const pruneEmptyTags = async () => {
let tagCounts = await sql.tag.tagCounts([])
let empty = tagCounts.filter((c) => Number(c.count) === 0)
if (empty.length) {
for (const tag of empty) {
if (tag.type !== "meta") await sql.tag.deleteTag(tag.tag)
}
}
}
const backupDatabase = async () => {
await sql.backupDB()
}
const runOnce = async () => {
await sql.createDB()
await serverFunctions.util.downloadTextDetector()
await serverFunctions.util.downloadAnimeDetector()
await serverFunctions.util.downloadWDTagger()
await serverFunctions.util.downloadImageRater()
await serverFunctions.util.downloadSegmentator()
await serverFunctions.util.downloadLineartExtractor()
}
const runDaily = async () => {
await backupDatabase()
await deleteExpiredTokens()
await deleteQueuedPosts()
await deleteQueuedUnverifiedPosts()
await deleteQueuedUsers()
await pruneEmptyTags()
await sql.user.pruneAnonSessions()
await sql.user.pruneExpiredSessions()
}
const run = async () => {
runOnce()
runDaily()
setInterval(runDaily, 24 * 60 * 60 * 1000)
let port = process.env.PORT || 8082
app.listen(port, "0.0.0.0", () => console.log(`Started the web server! http://localhost:${port}`))
}
run()