request-ip
Advanced tools
Comparing version 2.0.1 to 2.0.2
14
index.js
@@ -23,3 +23,14 @@ const is = require('is_js'); | ||
// source: http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/x-forwarded-headers.html | ||
const forwardedIps = value.split(',').map(e => e.trim()); | ||
// Azure Web App's also adds a port for some reason, so we'll only use the first part (the IP) | ||
const forwardedIps = value.split(',').map((e) => { | ||
const ip = e.trim(); | ||
if (ip.includes(':')) { | ||
const splitted = ip.split(':'); | ||
// make sure we only use this if it's ipv4 (ip:port) | ||
if (splitted.length === 2) { | ||
return splitted[0]; | ||
} | ||
} | ||
return ip; | ||
}); | ||
@@ -39,3 +50,2 @@ // Sometimes IP addresses in this header can be 'unknown' (http://stackoverflow.com/a/11285650). | ||
function getClientIp(req) { | ||
// Server is probably behind a proxy. | ||
@@ -42,0 +52,0 @@ if (req.headers) { |
{ | ||
"name": "request-ip", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"description": "A small node.js module to retrieve the request's IP address", | ||
@@ -55,6 +55,3 @@ "main": "index.js", | ||
"is_js": "^0.9.0" | ||
}, | ||
"engines": { | ||
"node": "<6" | ||
} | ||
} |
@@ -130,2 +130,27 @@ const http = require('http'); | ||
test('x-forwarded-for with ipv4:port', (t) => { | ||
t.plan(1); | ||
const options = { | ||
url: '', | ||
headers: { | ||
'x-forwarded-for': '93.186.30.120:12345', | ||
}, | ||
}; | ||
// create new server for each test so we can easily close it after the test is done | ||
// prevents tests from hanging and competing against closing a global server | ||
const server = new ServerFactory(); | ||
server.listen(0, serverInfo.host); | ||
server.on('listening', () => { | ||
options.url = `http://${serverInfo.host}:${server.address().port}`; | ||
request(options, (error, response, found) => { | ||
if (!error && response.statusCode === 200) { | ||
// make sure response ip is the same as the one we passed in | ||
const firstIp = options.headers['x-forwarded-for'].split(',')[0].trim().split(':')[0]; | ||
t.equal(firstIp, found); | ||
server.close(); | ||
} | ||
}); | ||
}); | ||
}); | ||
test('cf-connecting-ip', (t) => { | ||
@@ -132,0 +157,0 @@ t.plan(1); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34755
608