"csv-parse": "^5.4.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
+ "express-rate-limit": "^6.9.0",
"ioredis": "^5.3.2",
"knex": "^2.4.2",
"lodash": "^4.17.21",
"node": ">= 0.10.0"
}
},
+ "node_modules/express-rate-limit": {
+ "version": "6.9.0",
+ "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-6.9.0.tgz",
+ "integrity": "sha512-AnISR3V8qy4gpKM62/TzYdoFO9NV84fBx0POXzTryHU/qGUJBWuVGd+JhbvtVmKBv37t8/afmqdnv16xWoQxag==",
+ "engines": {
+ "node": ">= 14.0.0"
+ },
+ "peerDependencies": {
+ "express": "^4 || ^5"
+ }
+ },
"node_modules/express/node_modules/body-parser": {
"version": "1.20.1",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
}
}
},
+ "express-rate-limit": {
+ "version": "6.9.0",
+ "resolved": "https://registry.npmjs.org/express-rate-limit/-/express-rate-limit-6.9.0.tgz",
+ "integrity": "sha512-AnISR3V8qy4gpKM62/TzYdoFO9NV84fBx0POXzTryHU/qGUJBWuVGd+JhbvtVmKBv37t8/afmqdnv16xWoQxag==",
+ "requires": {}
+ },
"extend": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz",
import express, {Request, Response} from 'express';
import bodyParser from 'body-parser';
import xss from 'xss';
+import { rateLimit } from 'express-rate-limit';
import http from 'http';
import { Server, Socket } from 'socket.io';
next();
});
+const fightRateLimiter = rateLimit({
+ windowMs: parseInt(process.env.RATE_LIMIT_WINDOW || '30000'),
+ max: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS || '20'),
+ standardHeaders: true,
+ legacyHeaders: false,
+ handler: (req, res, next, options) => {
+ logger.log(`Blocked request: [${req.headers['x-authtoken']}: ${req.method} ${req.path}]`);
+ res.status(options.statusCode).send(options.message);
+ }
+});
+
async function bootstrapSocket(socket: Socket, player: Player) {
// ref to get the socket id for a particular player
cache.set(`socket:${player.id}`, socket.id);
res.send(html + travelSection + playerBar);
});
-app.post('/fight', authEndpoint, async (req: AuthRequest, res: Response) => {
+app.post('/fight', fightRateLimiter, authEndpoint, async (req: AuthRequest, res: Response) => {
if(req.player.hp <= 0) {
logger.log(`Player didn\'t have enough hp`);
return res.sendStatus(400);