add express rate limiting

This commit is contained in:
Evan
2025-02-21 08:33:16 -08:00
parent b711ea4975
commit 19794e4c65
4 changed files with 44 additions and 0 deletions
+26
View File
@@ -31,6 +31,8 @@ import { SecretManagerServiceClient } from "@google-cloud/secret-manager";
import dotenv from "dotenv";
import crypto from "crypto";
dotenv.config();
import rateLimit from "express-rate-limit";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -51,6 +53,22 @@ let DISCORD_CLIENT_SECRET: string;
app.use(express.static(path.join(__dirname, "../../out")));
app.use(express.json());
app.set("trust proxy", 2);
app.use(
rateLimit({
windowMs: 1000, // 1 second
max: 20, // 20 requests per IP per second
}),
);
app.set("trust proxy", 2);
app.use(
rateLimit({
windowMs: 1000, // 1 second
max: 20, // 20 requests per IP per second
}),
);
const gm = new GameManager(serverConfig);
let lobbiesString = "";
@@ -208,6 +226,14 @@ app.get("/private_lobby/:id", (req, res) => {
});
});
app.get("/debug-ip", (req, res) => {
res.send({
"x-forwarded-for": req.headers["x-forwarded-for"],
"real-ip": req.ip,
"raw-headers": req.rawHeaders,
});
});
app.get("*", function (req, res) {
// SPA routing
res.sendFile(path.join(__dirname, "../../out/index.html"));