forked from real2two/Exion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
96 lines (73 loc) · 2.51 KB
/
functions.js
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
let rate_limits = {};
module.exports = {
async validateEmail(email) { // https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript
let re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
},
async makeid(length) { // https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript
let result = [];
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result.push(characters.charAt(Math.floor(Math.random() * charactersLength)));
};
return result.join('');
},
async ifValidAPI(req, res) {
let auth = req.headers['authorization'];
if (auth) {
if (auth.startsWith("Bearer ") && auth !== "Bearer ") {
let given_token = auth.slice("Bearer ".length);
if (await process.db.tokens_to_ids.get(given_token)) return true;
};
};
//res.status(403);
//res.send({ error: "You have provided an invalid token." });
this.notFoundAPI(req, res);
return false;
},
async notFound(req, res) {
res.status(404);
res.render("404", {
req: req
});
},
async notFoundAPI(req, res) {
res.status(404);
res.json({ error: "Invalid API endpoint." });
},
async rateLimitCheck(id, res) {
if (rate_limits[id]) {
res.send({ error: "You are being rate limited." });
return false;
};
rate_limits[id] = true;
setTimeout(() => {
delete rate_limits[id];
}, 1000);
return true;
},
async rateLimitCheckWS(id) {
if (rate_limits[id]) {
return false;
};
rate_limits[id] = true;
setTimeout(() => {
delete rate_limits[id];
}, 200);
return true;
},
async rateLimitCheckMessage(id) {
if (rate_limits[id]) {
return false;
};
rate_limits[id] = true;
setTimeout(() => {
delete rate_limits[id];
}, 200);
return true;
},
async createID() {
return `${Date.now()}${Math.floor(Math.random() * 10)}`;
}
};