Parse x-forwarded-for header correctly

This commit is contained in:
Pavel Djundik 2017-06-11 22:33:04 +03:00
parent da87482b66
commit b0efbf8a1e

View File

@ -13,6 +13,7 @@ var dns = require("dns");
var Helper = require("./helper"); var Helper = require("./helper");
var ldap = require("ldapjs"); var ldap = require("ldapjs");
var colors = require("colors/safe"); var colors = require("colors/safe");
const net = require("net");
const Identification = require("./identification"); const Identification = require("./identification");
var manager = null; var manager = null;
@ -113,13 +114,15 @@ in ${config.public ? "public" : "private"} mode`);
}); });
}; };
function getClientIp(req) { function getClientIp(request) {
var ip; let ip = request.connection.remoteAddress;
if (!Helper.config.reverseProxy) { if (Helper.config.reverseProxy) {
ip = req.connection.remoteAddress; const forwarded = (request.headers["x-forwarded-for"] || "").split(/\s*,\s*/).filter(Boolean);
} else {
ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress; if (forwarded.length && net.isIP(forwarded[0])) {
ip = forwarded[0];
}
} }
return ip.replace(/^::ffff:/, ""); return ip.replace(/^::ffff:/, "");