This repository was archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
77 lines (64 loc) · 2.14 KB
/
index.js
File metadata and controls
77 lines (64 loc) · 2.14 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
require("dotenv").config();
const Express = require('express');
const {text} = require('express');
const app = Express();
const {existsSync, mkdirSync, promises} = require("fs");
const {writeFile} = promises;
const {join} = require("path");
const {webcrypto} = require("crypto");
// basic security
const helmet = require("helmet");
const CORS = require("cors");
app.use(helmet()).use(CORS());
// create bin folder
const binPath = join(__dirname, "bin");
if (!existsSync(binPath)) {
mkdirSync(binPath);
};
app.get(["/", "/:fileid"], (req, res) => {
const fileID = req?.params?.fileid;
if (!fileID?.length) return res.sendStatus(200);
const path = join(binPath, fileID);
return res.type("text/plain").sendFile(path, (err) => {
if (err) {
res.status(400).send("unable to retrieve bin content.");
};
})
});
app.post("/", text({ type: "text/plain", inflate: true }), async (req, res) => {
// we use auth for this one, just in case
if (!req.headers.authorization || req.headers.authorization !== process.env.SECRET) {
return res.sendStatus(403);
};
// limit
const limit = 1e6; // 1m
if (req.headers["content-type"] !== "text/plain") return res.status(400).send("invalid content type.");
if (!req?.body?.length) return res.status(411).send("the body is mandatory.");
if (req.body.length > limit) return res.sendStatus(413);
const fileID = randomID();
try {
await writeFile(join(binPath, fileID), req.body);
return res.status(200).type("text/plain").send(fileID);
} catch (err) {
console.error(err);
return res.status(500).end();
};
});
const PORT = process.env.PORT;
app.listen(PORT, () => {
console.log(`Bin: Ready, with port [${PORT}]`);
})
function getPseudoRandomNumber(min, max) {
const array = new Uint32Array(1);
webcrypto.getRandomValues(array);
return min + (array[0] % (max - min + 1));
};
function randomID() {
let length = getPseudoRandomNumber(4, 16);
let charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
let retVal = "";
for (let i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(getPseudoRandomNumber(0, n));
};
return retVal;
};