Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function check(req)
{
var ip_address = (req.connection.remoteAddress ? req.connection.remoteAddress : req.remoteAddress);
if (typeof req.headers['cf-connecting-ip'] === 'undefined')
{
return false;
}
else
{
var range_check = require('range_check');
if (range_check.vaild_ip(ip_address))
{
var ip_ver = range_check.ver(ip_address);
if (ip_ver === 4)
{
return range_check.in_range(ip_address, ranges.v4);
}
else if (ip_ver === 6)
{
return range_check.in_range(ip_address, ranges.v6);
}
else
{
return false;
}
}
else
{
return false;
return function(req,res,next){
var remoteIP = {
ip: req.ip.replace("::ffff:",""), //app.set trust proxy could potentially modify this and cause issues
v: "ip"+range_check.ver(req.ip.replace("::ffff:",""))
};
req.cf_ip = remoteIP.ip;//override this if cloudflare present
if (req.headers['cf-connecting-ip'] == undefined){
return next(); //no cloudflare IP, continue on like this never happened. Shhhh!
}
if (range_check.in_range(remoteIP.ip, ipRanges[remoteIP.v])){
req.cf_ip = req.headers['cf-connecting-ip'];
}
next();
};
};