fix(clientAddress): allow location of the client ip address in forward headers to be specified in config

This commit is contained in:
Danny Coates 2016-04-05 20:41:38 -07:00 коммит произвёл Vijay Budhram
Родитель 3dcdaf81d8
Коммит 517fbff776
2 изменённых файлов: 8 добавлений и 1 удалений

Просмотреть файл

@ -354,6 +354,12 @@ var conf = convict({
format: String,
env: 'CORS_ORIGIN',
default: '*'
},
clientAddressDepth: {
doc: 'location of the client ip address in the remote address chain',
format: Number,
env: 'CLIENT_ADDRESS_DEPTH',
default: 3
}
})

Просмотреть файл

@ -194,9 +194,10 @@ function create(log, error, config, routes, db) {
// Construct source-ip-address chain for logging.
var xff = (request.headers['x-forwarded-for'] || '').split(/\s*,\s*/)
xff.push(request.info.remoteAddress)
var clientAddressIndex = xff.length - (config.clientAddressDepth || 1)
// Remove empty items from the list, in case of badly-formed header.
request.app.remoteAddressChain = xff.filter(function(x){ return x })
request.app.clientAddress = request.app.remoteAddressChain[0]
request.app.clientAddress = request.app.remoteAddressChain[clientAddressIndex]
request.app.acceptLanguage = trimLocale(request.headers['accept-language'])
if (request.headers.authorization) {