@20syldev/api 3.3.2 → 3.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/app.js +23 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<a href="https://api.sylvain.pro"><img src="https://api.sylvain.pro/favicon.ico" alt="Logo" width="25%" height="auto"/></a>
|
|
3
3
|
|
|
4
4
|
# API Personnelle
|
|
5
|
-
[](https://github.com/20syldev/api/releases/latest)
|
|
6
6
|
</div>
|
|
7
7
|
|
|
8
8
|
---
|
|
@@ -46,7 +46,7 @@ Enfin, **exécutez** le script de build, qui installera les **dépendances** et
|
|
|
46
46
|
$ npm run build
|
|
47
47
|
```
|
|
48
48
|
```console
|
|
49
|
-
> @20syldev/api@3.3.
|
|
49
|
+
> @20syldev/api@3.3.3 build
|
|
50
50
|
> npm install && node app.js
|
|
51
51
|
|
|
52
52
|
[...]
|
package/app.js
CHANGED
|
@@ -39,7 +39,7 @@ const random = (arr) => arr[Math.floor(Math.random() * arr.length)];
|
|
|
39
39
|
const logs = [], chat = [], privateChats = {}, sessions = {}, rateLimits = {}, ipLimits = {}, games = {};
|
|
40
40
|
|
|
41
41
|
// Define global variables
|
|
42
|
-
let contributions, lastFetch = 0, requests = 0, requestLimit
|
|
42
|
+
let contributions, lastFetch = 0, requests = 0, requestLimit, resetTime = Date.now() + 3600000;
|
|
43
43
|
|
|
44
44
|
// ----------- ----------- MAIN FUNCTIONS ----------- ----------- //
|
|
45
45
|
|
|
@@ -108,14 +108,17 @@ app.use((req, res, next) => {
|
|
|
108
108
|
// Too many requests
|
|
109
109
|
app.use((req, res, next) => {
|
|
110
110
|
const ip = req.headers['cf-connecting-ip'] || req.socket.remoteAddress;
|
|
111
|
-
const now = Date.now();
|
|
112
111
|
const token = req.headers.authorization?.split(' ')[1] || '';
|
|
113
112
|
|
|
114
|
-
const
|
|
113
|
+
const now = Date.now();
|
|
114
|
+
const minute = Math.floor(now / 60000) % 60;
|
|
115
|
+
const hour = Math.floor(now / 3600000) % 24;
|
|
116
|
+
|
|
117
|
+
const business = process.env.BUSINESS_TOKEN_LIST?.split(' ') || [];
|
|
115
118
|
const pro = process.env.PRO_TOKEN_LIST?.split(' ') || [];
|
|
116
119
|
const advanced = process.env.ADVANCED_TOKEN_LIST?.split(' ') || [];
|
|
117
120
|
|
|
118
|
-
if (token && ![...
|
|
121
|
+
if (token && ![...business, ...pro, ...advanced].includes(token) || token === 'undefined') {
|
|
119
122
|
return res.status(401).jsonResponse({
|
|
120
123
|
message: 'Unauthorized',
|
|
121
124
|
error: 'Invalid token.',
|
|
@@ -123,23 +126,28 @@ app.use((req, res, next) => {
|
|
|
123
126
|
});
|
|
124
127
|
}
|
|
125
128
|
|
|
126
|
-
if (
|
|
127
|
-
else if (pro.includes(token) && !pro.includes('undefined')) requestLimit =
|
|
128
|
-
else if (advanced.includes(token) && !advanced.includes('undefined')) requestLimit =
|
|
129
|
+
if (business.includes(token) && !business.includes('undefined')) requestLimit = process.env.BUSINESS_LIMIT;
|
|
130
|
+
else if (pro.includes(token) && !pro.includes('undefined')) requestLimit = process.env.PRO_LIMIT;
|
|
131
|
+
else if (advanced.includes(token) && !advanced.includes('undefined')) requestLimit = process.env.ADVANCED_LIMIT;
|
|
132
|
+
else requestLimit = process.env.DEFAULT_LIMIT;
|
|
129
133
|
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
if (now > resetTime) requests = 0, resetTime = now + 60000;
|
|
133
|
-
if (++requests > Math.max(globalLimit / Math.max(1, Object.keys(ipLimits).length), requestLimit)) {
|
|
134
|
+
if (now > resetTime) requests = 0, resetTime = now + 3600000;
|
|
135
|
+
if (++requests > Math.max(process.env.GLOBAL_LIMIT / Math.max(1, Object.keys(ipLimits).length), requestLimit)) {
|
|
134
136
|
return res.status(429).jsonResponse({ message: 'Too Many Requests' });
|
|
135
137
|
}
|
|
136
138
|
|
|
137
|
-
if (!ipLimits[ip]) ipLimits[ip] =
|
|
138
|
-
ipLimits[ip]
|
|
139
|
+
if (!ipLimits[ip]) ipLimits[ip] = {};
|
|
140
|
+
if (!ipLimits[ip][hour]) ipLimits[ip][hour] = {};
|
|
141
|
+
ipLimits[ip][hour][minute] = (ipLimits[ip][hour][minute] || 0) + 1;
|
|
142
|
+
|
|
143
|
+
if (ipLimits[ip][hour][minute] > requestLimit) return res.status(429).jsonResponse({
|
|
144
|
+
message: 'Too Many Requests (IP limited), authenticate to increase the limit.',
|
|
145
|
+
error: `You have exceeded the limit of ${requestLimit} requests per hour.`,
|
|
146
|
+
status: '429'
|
|
147
|
+
});
|
|
139
148
|
|
|
140
|
-
|
|
149
|
+
Object.keys(ipLimits[ip]).forEach(h => { if (h != hour) delete ipLimits[ip][h]; });
|
|
141
150
|
|
|
142
|
-
ipLimits[ip].push(now);
|
|
143
151
|
next();
|
|
144
152
|
});
|
|
145
153
|
|
package/package.json
CHANGED