File size: 1,022 Bytes
4551c7e
221aef6
4551c7e
 
 
2b65d87
c7254fd
 
5e47bf4
dbf311b
4551c7e
2b65d87
 
 
4551c7e
68e73f6
 
5e47bf4
221aef6
2b65d87
5e47bf4
4551c7e
221aef6
 
2b65d87
 
68e73f6
 
4551c7e
2b65d87
68e73f6
4551c7e
 
7c9d1d2
c7254fd
4551c7e
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
const express = require("express");
const { CaptchaGenerator } = require("captcha-canvas");
const fs = require("fs");
const os = require("os");
const path = require("path");
const crypto = require("crypto");

const app = express();
app.set('json spaces', 4)
const tempDir = os.tmpdir();

function generateRandomHex(length) {
    return crypto.randomBytes(length).toString("hex");
}

app.use("/tmp", express.static(tempDir));

app.all("/", async (req, res) => {
    const captcha = new CaptchaGenerator({ height: 200, width: 600 })

    const fileName = `temp_${generateRandomHex(8)}.png`;
    const filePath = path.join(tempDir, fileName);
  
    const buffer = await captcha.generate()

    fs.writeFile(filePath, buffer, (err) => {
        if (err) return res.status(500).json({ error: "Failed to save captcha" });

        res.json({
            answer: captcha.text,
            url: `https://${req.hostname}/tmp/${fileName}`
        });
    });
});

app.listen(7860, () => console.log("Server running on port 7860"));