File size: 1,440 Bytes
e151afc 6222fc9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
var Database = require('./database.js');
const CheckMilWare = async (app) => {
return async (req, res, next) => {
try {
const dbClient = new Database("AkenoXJs", "FastJsAPI");
const xForwardedFor = req.headers['x-forwarded-for'];
const xRealIP = req.headers['x-real-ip'];
const cfConnectingIP = req.headers['cf-connecting-ip'];
let realIP = req.ip;
if (xForwardedFor) {
realIP = xForwardedFor.split(',')[0].trim();
} else if (xRealIP) {
realIP = xRealIP;
} else if (cfConnectingIP) {
realIP = cfConnectingIP;
}
req.realIP = realIP;
const isBlocked = await dbClient.CheckIsBlocked(realIP);
if (isBlocked && isBlocked.blocked === true) {
return res.status(403).send("Access denied: IP is blocked");
}
if (req.path === '/env') {
console.log("Check path /env");
await dbClient.AddIpisBlocked(realIP);
return res.status(403).send("Access denied: IP is blocked..");
}
await dbClient.IPAddressAndUpdate(realIP);
console.log(`Real IP address is: ${realIP}, header: ${xForwardedFor ? "x-forwarded-for" : xRealIP ? "x-real-ip" : cfConnectingIP ? "cf-connecting-ip" : "req.ip"}`);
next();
} catch (error) {
console.error("Error in middleware: " + error);
res.status(500).send("Something bad happened");
}
};
};
module.exports = { CheckMilWare }; |