Merge pull request #1202 from thelounge/xpaw/trust-proxy

Parse X-Forwarded-For header correctly
This commit is contained in:
Jérémie Astori 2017-06-26 01:52:53 -04:00 committed by GitHub
commit e0ed1195fa

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:/, "");