mirror of
https://github.com/AlecM33/Werewolf.git
synced 2025-12-26 15:57:50 +01:00
centralize cors
This commit is contained in:
@@ -4,37 +4,37 @@ const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())
|
||||
const logger = require('../modules/Logger')(debugMode);
|
||||
const GameManager = require('../modules/GameManager.js');
|
||||
const rateLimit = require('express-rate-limit').default;
|
||||
const globals = require('../config/globals');
|
||||
const globals = require('../config/globals.js');
|
||||
const cors = require('cors');
|
||||
|
||||
const gameManager = new GameManager().getInstance();
|
||||
|
||||
const apiLimiter = rateLimit({
|
||||
windowMs: 600000,
|
||||
max: 5,
|
||||
windowMs: 60000,
|
||||
max: 100,
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false
|
||||
});
|
||||
|
||||
const corsOptions = process.env.NODE_ENV.trim() === 'development'
|
||||
? {
|
||||
origin: '*',
|
||||
optionsSuccessStatus: 200
|
||||
}
|
||||
: {
|
||||
origin: 'https://playwerewolf.uk.r.appspot.com',
|
||||
optionsSuccessStatus: 200
|
||||
};
|
||||
const gameEndpointLimiter = rateLimit({ // further limit the rate of game creation to 30 games per 10 minutes.
|
||||
windowMs: 600000,
|
||||
max: 30,
|
||||
standardHeaders: true,
|
||||
legacyHeaders: false
|
||||
});
|
||||
|
||||
router.use(cors(corsOptions));
|
||||
router.options('/:code/players', cors(corsOptions));
|
||||
router.use(cors(globals.CORS));
|
||||
router.options('/:code/players', cors(globals.CORS));
|
||||
router.options('/create', cors(globals.CORS));
|
||||
router.options('/restart', cors(globals.CORS));
|
||||
|
||||
if (process.env.NODE_ENV.trim() === 'production') { // in prod, limit clients to creating 5 games per 10 minutes.
|
||||
router.use('/create', apiLimiter);
|
||||
if (process.env.NODE_ENV.trim() === 'production') {
|
||||
router.use(apiLimiter);
|
||||
router.use('/create', gameEndpointLimiter);
|
||||
}
|
||||
|
||||
router.post('/create', function (req, res) {
|
||||
logger.trace('Received request to create new game: ' + JSON.stringify(req.body, null, 4));
|
||||
logger.debug('Received request to create new game: ' + JSON.stringify(req.body, null, 4));
|
||||
const gameCreationPromise = gameManager.createGame(req.body, false);
|
||||
gameCreationPromise.then((result) => {
|
||||
if (result instanceof Error) {
|
||||
|
||||
@@ -3,6 +3,15 @@ const globals = {
|
||||
ACCESS_CODE_LENGTH: 4,
|
||||
ACCESS_CODE_GENERATION_ATTEMPTS: 50,
|
||||
CLOCK_TICK_INTERVAL_MILLIS: 100,
|
||||
CORS: process.env.NODE_ENV.trim() === 'development'
|
||||
? {
|
||||
origin: '*',
|
||||
optionsSuccessStatus: 200
|
||||
}
|
||||
: {
|
||||
origin: 'https://play-werewolf.app',
|
||||
optionsSuccessStatus: 200
|
||||
},
|
||||
STALE_GAME_HOURS: 12,
|
||||
CLIENT_COMMANDS: {
|
||||
FETCH_GAME_STATE: 'fetchGameState',
|
||||
@@ -41,7 +50,8 @@ const globals = {
|
||||
PLAYER_JOINED: 'playerJoined',
|
||||
PLAYER_LEFT: 'playerLeft',
|
||||
SYNC_GAME_STATE: 'syncGameState',
|
||||
NEW_SPECTATOR: 'newSpectator'
|
||||
NEW_SPECTATOR: 'newSpectator',
|
||||
BROADCAST: 'broadcast'
|
||||
},
|
||||
ENVIRONMENT: {
|
||||
LOCAL: 'local',
|
||||
@@ -61,7 +71,8 @@ const globals = {
|
||||
PAUSE_TIMER: 'pauseTimer',
|
||||
RESUME_TIMER: 'resumeTimer',
|
||||
GET_TIME_REMAINING: 'getTimeRemaining'
|
||||
}
|
||||
},
|
||||
MOCK_AUTH: 'mock_auth'
|
||||
};
|
||||
|
||||
module.exports = globals;
|
||||
|
||||
Reference in New Issue
Block a user