Skip to content

Commit

Permalink
Create firewall.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 7b869ed commit 4795588
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/security/firewall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// src/security/firewall.js
const rateLimit = require('express-rate-limit');
const express = require('express');

const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100, // Limit each IP to 100 requests per windowMs
message: 'Too many requests, please try again later.',
});

const whitelist = ['127.0.0.1', '::1']; // Add your allowed IPs here

function ipWhitelist(req, res, next) {
if (!whitelist.includes(req.ip)) {
return res.status(403).send('Forbidden');
}
next();
}

function applyFirewall(app) {
app.use(limiter);
app.use(ipWhitelist);
}

module.exports = applyFirewall;

0 comments on commit 4795588

Please sign in to comment.