Skip to content

Commit 975a19d

Browse files
committed
Cherry-Picked rate limits
This commit consists of one `npm i` action and three commits from the `rate-limiting` branch. May be reverted if issues arise. Use both slow down and rate limits Should further prevent API abuse Fixed small delay bug Hits are now correctly counted Basic rate limiting implented Exact values need tweaking, but the functionality is there. Possibly might implement route specific limits.
1 parent 22e33c5 commit 975a19d

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

api/maple.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const express = require('express');
22
const rateLimit = require("express-rate-limit");
3+
const slowDown = require("express-slow-down");
34
const cors = require('cors');
45
const login = require('./auth/login');
56
const getPath = require('./get/get.js');
@@ -53,10 +54,20 @@ var options = {
5354
}
5455
};
5556

56-
/* const limiter = rateLimit({
57+
const slower = slowDown({
5758
windowMs: 2 * 60 * 1000,
58-
max: 20,
59-
}); */
59+
delayAfter: 5,
60+
delayMs: (hits) => {
61+
if (hits <= 15) return hits * 100;
62+
return (hits - 15) * 1000 + 2000;
63+
},
64+
maxDelayMs: 15000,
65+
});
66+
67+
const limiter = rateLimit({
68+
windowMs: 2 * 60 * 1000,
69+
limit: 35,
70+
})
6071

6172
try {
6273

@@ -84,8 +95,9 @@ try {
8495
console.log('[7] Setting up routes...');
8596
const friends = require('./user/friends.js');
8697

87-
/* app.use(limiter);
88-
*/
98+
app.use(limiter);
99+
app.use(slower);
100+
89101
app.use(cors(corsOptions));
90102

91103
app.get('/', (req, res) => {

api/package-lock.json

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"dotenv": "^16.4.7",
2727
"express": "^4.21.2",
2828
"express-rate-limit": "^7.5.0",
29+
"express-slow-down": "^3.0.1",
2930
"express-validator": "^7.3.1",
3031
"jsonwebtoken": "^9.0.2",
3132
"multer": "^2.0.2",

0 commit comments

Comments
 (0)