diff --git a/api/api.js b/api/api.js index 63c4b388d15..fbfe954844e 100644 --- a/api/api.js +++ b/api/api.js @@ -117,6 +117,13 @@ function validateUserForMgmtReadAPI(callback, params) { }); } +function getIpAddress(req) { + var ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.socket.remoteAddress || req.connection.socket.remoteAddress; + + /* Since x-forwarded-for: client, proxy1, proxy2, proxy3 */ + return ipAddress.split(',')[0]; +} + if (cluster.isMaster) { var workerCount = (common.config.api.workers)? common.config.api.workers : os.cpus().length; @@ -297,9 +304,7 @@ if (cluster.isMaster) { } case '/i': { - var ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress; - - params.ip_address = params.qstring.ip_address || ipAddress.split(",")[0]; + params.ip_address = getIpAddress(req); params.user = { 'country':'Unknown', 'city':'Unknown'