forwarded-for
Advanced tools
Comparing version 0.1.0 to 0.1.1
21
index.js
@@ -90,2 +90,11 @@ 'use strict'; | ||
// | ||
// Shift the most recently found proxy header to the front of the proxies | ||
// array. This optimizes future calls, placing the most commonly found headers | ||
// near the front of the array. | ||
// | ||
if (i !== 0) { | ||
proxies.unshift(proxies.splice(i, 1)[0]); | ||
} | ||
// | ||
// We've gotten a match on a HTTP header, we need to parse it further as it | ||
@@ -114,3 +123,3 @@ // could consist of multiple hops. The pattern for multiple hops is: | ||
*/ | ||
module.exports = function parse(obj, headers, whitelist) { | ||
function parse(obj, headers, whitelist) { | ||
var proxied = forwarded(headers || {}, whitelist) | ||
@@ -165,2 +174,10 @@ , connection = obj.connection | ||
return new Forwarded(); | ||
}; | ||
} | ||
// | ||
// Expose the module and all of it's interfaces. | ||
// | ||
parse.Forwarded = Forwarded; | ||
parse.forwarded = forwarded; | ||
parse.proxies = proxies; | ||
module.exports = parse; |
{ | ||
"name": "forwarded-for", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Abstraction for retrieving ip address information from a Node.js connection. Searches for proxy headers before degrading req.address", | ||
@@ -21,3 +21,5 @@ "main": "index.js", | ||
"parser", | ||
"real-ip", | ||
"ssl-terminator", | ||
"x-forwarded", | ||
"x-forwarded-for", | ||
@@ -24,0 +26,0 @@ "x-forwarded-proto", |
# forwarded-for | ||
[![Build Status](https://travis-ci.org/primus/forwarded-for.svg?branch=master)](https://travis-ci.org/primus/forwarded-for) | ||
[![NPM version](https://badge.fury.io/js/forwarded-for.svg)](http://badge.fury.io/js/forwarded-for) | ||
When you are hosting your applications behind a reverse load balancer the | ||
@@ -4,0 +7,0 @@ incoming requests will no longer have the IP address of your user but of the |
52
test.js
@@ -11,2 +11,30 @@ describe('forwarded-for', function () { | ||
it('exposes the proxy array', function () { | ||
assume(parser.proxies).to.be.a('array'); | ||
}); | ||
it('exposes the Forwarded class', function () { | ||
assume(parser.Forwarded).to.be.a('function'); | ||
}); | ||
it('sorts the proxy array and sets the last match as first item', function () { | ||
var last = parser.proxies[parser.proxies.length - 1] | ||
, spec = { ip: '222.1.2.242', port: '4900', proto: 'https' }; | ||
assume(parser.proxies[0].ip).to.not.equal(last.ip); | ||
var forwarded = parser({ | ||
remoteAddress: '127.1.2.0', | ||
remotePort: 490 | ||
}, Object.keys(spec).reduce(function reduce(headers, key) { | ||
headers[last[key]] = spec[key]; | ||
return headers; | ||
}, {})); | ||
assume(parser.proxies[0].ip).to.equal(last.ip); | ||
assume(forwarded.ip).to.equal('222.1.2.242'); | ||
assume(forwarded.port).to.equal(4900); | ||
assume(forwarded.secure).to.equal(true); | ||
}); | ||
it('extracts the `remoteAdress` and port from the given object', function() { | ||
@@ -50,2 +78,26 @@ var forwarded = parser({ | ||
it('works when shuffling the proxies array', function() { | ||
var i = 0, forwarded, hs = [ | ||
{headers: {'fastly-client-ip': '1.2.3.4'}, expected: '1.2.3.4'}, | ||
{headers: {'x-forwarded-for': '9.9.9.9'}, expected: '9.9.9.9'}, | ||
{headers: {'forwarded': '3.4.5.6'}, expected: '3.4.5.6'}, | ||
{headers: {'x-real-ip': '7.8.9.10'}, expected: '7.8.9.10'}, | ||
]; | ||
// Fisher-Yates shuffle | ||
function shuffle(o){ //v1.0 | ||
for (var j, x, i = o.length; i; | ||
j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | ||
return o; | ||
} | ||
for (; i < 8; i++) { | ||
hs = shuffle(hs); | ||
hs.forEach(function(v) { | ||
forwarded = parser({}, v.headers); | ||
assume(forwarded.ip).to.equal(v.expected); | ||
}); | ||
} | ||
}); | ||
describe('fastly.com', function () { | ||
@@ -52,0 +104,0 @@ it('extracts information from fastly headers', function () { |
14675
6
335
70