48 lines
1.9 KiB
JavaScript
48 lines
1.9 KiB
JavaScript
const express = require('express')
|
|
import { mkdir, rmSync, readdirSync } from 'node:fs';
|
|
const app = express();
|
|
import { rateLimit } from 'express-rate-limit'
|
|
app.use(express.json());
|
|
const port = process.env.PORT || 3000;
|
|
const limiter = rateLimit({
|
|
windowMs: 60 * 1000, // 1 minute
|
|
limit: 60, // Limit each IP to 50 requests per `window` (here, per 1 minute).
|
|
standardHeaders: 'draft-8', // draft-6: `RateLimit-*` headers; draft-7 & draft-8: combined `RateLimit` header
|
|
legacyHeaders: false, // Disable the `X-RateLimit-*` headers.
|
|
ipv6Subnet: 56, // Set to 60 or 64 to be less aggressive, or 52 or 48 to be more aggressive
|
|
});
|
|
app.use(limiter);
|
|
|
|
app.use("/", express.static("./public", { extensions: ["html"] }));
|
|
|
|
app.use("/new", async (req, res) => {
|
|
let uuid = crypto.randomUUID();
|
|
mkdir("./data/" + uuid, ()=>{});
|
|
let image = await (await fetch("https://freedns.afraid.org/securimage/securimage_show.php")).arrayBuffer();
|
|
await Bun.write("./data/" + uuid + "/image.png", image);
|
|
res.send(uuid);
|
|
await setTimeout(async ()=>{
|
|
let file = Bun.file("./data/" + uuid + "/answer.txt");
|
|
if(!(await file.exists())) {
|
|
rmSync("./data/" + uuid, { recursive: true });
|
|
}
|
|
}, 30000)
|
|
});
|
|
app.use("/image/:id", async (req, res) => {
|
|
res.send(await Bun.file("./data/" + req.params.id + "/image.png").bytes());
|
|
});
|
|
app.use("/solved", async (req, res) => {
|
|
res.send(readdirSync("./data").length);
|
|
});
|
|
app.post("/solve", async (req, res) => {
|
|
let file = "./data/" + req.body.uuid + "/image.png";
|
|
if(await file.exists() && (req.body.answer.length == 4 || req.body.answer.length == 5)) {
|
|
await Bun.write("./data/" + req.body.uuid + "/answer.txt", req.body.answer);
|
|
}
|
|
res.sendStatus(200);
|
|
});
|
|
const server = app.listen(port, () => {
|
|
console.log("Express is online.");
|
|
console.log("- http://localhost:" + port);
|
|
});
|