Comparing version 0.0.8 to 1.0.0
@@ -0,1 +1,9 @@ | ||
## 1.0.0 | ||
Enhancement: | ||
- Remove Leaks | ||
- Add trusted proxy support | ||
- Add node 6.x.x support | ||
- Change to MIT licensing | ||
## 0.0.8 | ||
@@ -2,0 +10,0 @@ |
@@ -15,2 +15,10 @@ { | ||
"IPWARE_HTTP_HEADER_PROXY_PRECEDENCE_ORDER": [ | ||
"HTTP_X_FORWARDED_FOR", | ||
"X_FORWARDED_FOR" | ||
], | ||
"IPWARE_TRUSTED_PROXY_LIST": [ | ||
], | ||
"IPV4_EXTERNALLY_NON_ROUTABLE_IP_PREFIX": [ | ||
@@ -82,2 +90,3 @@ "0." | ||
] | ||
} |
79
index.js
@@ -1,5 +0,6 @@ | ||
var is_initialized = false; | ||
var ipware_defs = null; | ||
var ipware_precedence_list = []; | ||
var ipware_proxy_precedence_list = []; | ||
var ipware_proxy_list = []; | ||
var ipware_prefix_list = []; | ||
@@ -20,2 +21,18 @@ | ||
function get_proxy_precedence_list() { | ||
try { | ||
ipware_proxy_precedence_list = ipware_defs.IPWARE_HTTP_HEADER_PROXY_PRECEDENCE_ORDER; | ||
} catch(e) { | ||
throw e; | ||
} | ||
} | ||
function get_proxy_list() { | ||
try { | ||
ipware_proxy_list = ipware_defs.IPWARE_TRUSTED_PROXY_LIST; | ||
} catch(e) { | ||
throw e; | ||
} | ||
} | ||
function get_non_routable_prefix_list() { | ||
@@ -45,2 +62,4 @@ for (var prefix in ipware_defs) { | ||
get_precedence_list(); | ||
get_proxy_precedence_list(); | ||
get_proxy_list(); | ||
get_non_routable_prefix_list(); | ||
@@ -52,4 +71,4 @@ is_initialized = true; | ||
_me.is_loopback_ip = function (ip) { | ||
var _ip = ip.toLowerCase().trim(); | ||
return _ip === '127.0.0.1' || _ip === '::1'; | ||
var ip = ip.toLowerCase().trim(); | ||
return ip === '127.0.0.1' || ip === '::1'; | ||
} | ||
@@ -69,3 +88,3 @@ | ||
_me.is_valid_ipv4 = function (ip) { | ||
ipv4_pattern = /^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/; | ||
var ipv4_pattern = /^(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)\.(\d?\d?\d)$/; | ||
if (!ipv4_pattern.test(ip)) { | ||
@@ -79,3 +98,3 @@ return false; | ||
_me.is_valid_ipv6 = function (ip) { | ||
ipv6_pattern = /^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$/; | ||
var ipv6_pattern = /^::|^::1|^([a-fA-F0-9]{1,4}::?){1,7}([a-fA-F0-9]{1,4})$/; | ||
return ipv6_pattern.test(ip) | ||
@@ -89,3 +108,3 @@ } | ||
_me.get_headers_attribute = function (headers, key) { | ||
key_upper = key.toUpperCase(); | ||
var key_upper = key.toUpperCase(); | ||
if (key_upper in headers) { | ||
@@ -95,3 +114,3 @@ return headers[key_upper]; | ||
key_lower = key.toLowerCase(); | ||
var key_lower = key.toLowerCase(); | ||
if (key_lower in headers) { | ||
@@ -101,3 +120,3 @@ return headers[key_lower]; | ||
alt_key_lower = key_lower.replace(/_/g, '-'); | ||
var alt_key_lower = key_lower.replace(/_/g, '-'); | ||
if (alt_key_lower in headers) { | ||
@@ -107,3 +126,3 @@ return headers[alt_key_lower]; | ||
alt_key_upper = alt_key_lower.toUpperCase() | ||
var alt_key_upper = alt_key_lower.toUpperCase() | ||
if (alt_key_upper in headers) { | ||
@@ -168,7 +187,45 @@ return headers[alt_key_upper]; | ||
req.clientIp = _me.get_local_ip(req); | ||
if (!_me.is_private_ip(req.clientIp)){ | ||
req.clientIpRoutable = true; | ||
req.clientIpRoutable = !_me.is_private_ip(req.clientIp); | ||
} | ||
return {clientIp: req.clientIp, clientIpRoutable: req.clientIpRoutable} | ||
}; | ||
_me.get_trusted_ip = function (req, trusted_proxies, right_most_proxy) { | ||
initialize(); | ||
var trusted_proxies = trusted_proxies || ipware_proxy_list; | ||
var right_most_proxy = right_most_proxy || false; | ||
req.clientIpRoutable = false; | ||
req.clientIp = null; | ||
var value = null; | ||
if (trusted_proxies.length >= 1) { | ||
for (var i = 0; i < ipware_proxy_precedence_list.length; i++) { | ||
value = _me.get_headers_attribute(req.headers, ipware_proxy_precedence_list[i].trim()); | ||
if (value) { | ||
var ips = value.split(','); | ||
if (ips.length > 1 && right_most_proxy) { | ||
ips = ips.reverse(); | ||
} | ||
if (ips.length > 1) { | ||
for (var j = 0; j < trusted_proxies.length; j++) { | ||
if (trusted_proxies[j] === ips[ips.length-1].trim()) { | ||
var ip = ips[0].trim(); | ||
if (ip && _me.is_valid_ip(ip)) { | ||
req.clientIp = ip; | ||
req.clientIpRoutable = !_me.is_private_ip(ip); | ||
return {clientIp: req.clientIp, clientIpRoutable: req.clientIpRoutable} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (!req.clientIp) { | ||
req.clientIp = _me.get_local_ip(req); | ||
req.clientIpRoutable = !_me.is_private_ip(req.clientIp); | ||
} | ||
return {clientIp: req.clientIp, clientIpRoutable: req.clientIpRoutable} | ||
@@ -175,0 +232,0 @@ }; |
{ | ||
"name": "ipware", | ||
"version": "0.0.8", | ||
"version": "1.0.0", | ||
"description": "Returns the real IP address of users in Node.js", | ||
@@ -14,6 +14,5 @@ "main": "index.js", | ||
"user IP address" | ||
], | ||
"author": "Val Neekman @ Neekware Inc.", | ||
"license": "BSD", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -28,4 +27,4 @@ "url": "https://github.com/un33k/node-ipware/issues" | ||
"scripts": { | ||
"test": "mocha --ignore-leaks --reporter spec test/*_test.js" | ||
"test": "mocha --reporter spec test/*_test.js" | ||
} | ||
} |
@@ -45,4 +45,4 @@ Node IPware | ||
// Advanced option: By default the left most address in the `HTTP_X_FORWARDED_FOR` is | ||
// returned. However, depending on your preference and needs, you can change this | ||
// Advanced option: By default the left most address in the `HTTP_X_FORWARDED_FOR` or `X_FORWARDED_FOR` | ||
// is returned. However, depending on your preference and needs, you can change this | ||
// behavior by passing the `right_most_proxy=True` to the API. | ||
@@ -59,2 +59,27 @@ // Note: Not all proxies are equal. So left to right or right to left preference is not a | ||
```javascript | ||
// 1. Trusted Proxies: | ||
// ************************* | ||
// To only get client ip addresses from your own trusted proxy server(s), use `get_trusted_ip()`. | ||
// In your js file (e.g. app.js) | ||
var get_trusted_ip = require('ipware')().get_trusted_ip; | ||
var trusted_proxies = ['177.144.11.100', '177.144.11.101']; | ||
app.use(function(req, res, next) { | ||
var ip_info = get_trusted_ip(req, trusted_proxies); | ||
console.log(ip_info); | ||
// { clientIp: '177.100.44.22', clientIpRoutable: true } | ||
next(); | ||
}); | ||
// Alternatively, you can pass in the trusted proxies via the configuration file. | ||
{ | ||
... | ||
"IPWARE_TRUSTED_PROXY_LIST": [ | ||
'177.144.11.100', | ||
'177.144.11.101' | ||
], | ||
... | ||
} | ||
// 2. Customizable configuration file: | ||
// *********************************** | ||
// You can also use your own config file as below. | ||
@@ -91,2 +116,10 @@ // for `IPWARE_HTTP_HEADER_PRECEDENCE_ORDER` items, the | ||
"IPWARE_HTTP_HEADER_PROXY_PRECEDENCE_ORDER": [ | ||
"HTTP_X_FORWARDED_FOR", | ||
"X_FORWARDED_FOR" | ||
], | ||
"IPWARE_TRUSTED_PROXY_LIST": [ | ||
], | ||
"IPV4_EXTERNALLY_NON_ROUTABLE_IP_PREFIX": [ | ||
@@ -173,2 +206,2 @@ "0.", | ||
Released under a ([BSD](LICENSE.md)) license. | ||
Released under a ([MIT](LICENSE)) license. |
var get_ip = require('..')().get_ip, | ||
get_trusted_ip = require('..')().get_trusted_ip, | ||
assert = require('assert'); | ||
@@ -246,1 +247,55 @@ | ||
}); | ||
describe('get_trusted_ip(): IPV4: EMPTY DEFAULT IPWARE_TRUSTED_PROXY_LIST', function() { | ||
it('test_trusted_ip_default_config', function() { | ||
var request = {headers: {}}; | ||
request.headers.HTTP_X_FORWARDED_FOR = '177.139.100.100, 177.139.233.200, 177.139.233.139'; | ||
get_trusted_ip(request); | ||
assert.equal(request.clientIp, '127.0.0.1'); | ||
assert.equal(request.clientIpRoutable, false); | ||
}); | ||
}); | ||
describe('get_trusted_ip(): IPV4: PROXY LIST AS PARAM (VALID)', function() { | ||
it('test_trusted_ip_proxy_list_as_params_single', function() { | ||
var request = {headers: {}}; | ||
request.headers.HTTP_X_FORWARDED_FOR = '177.139.100.100, 177.139.233.200, 177.139.233.139'; | ||
var trusted_proxy_list = ['177.139.233.139']; | ||
get_trusted_ip(request, trusted_proxy_list); | ||
assert.equal(request.clientIp, '177.139.100.100'); | ||
assert.equal(request.clientIpRoutable, true); | ||
}); | ||
}); | ||
describe('get_trusted_ip(): IPV4: PROXY LIST AS PARAM (VALID)', function() { | ||
it('test_trusted_ip_proxy_list_as_params_multi', function() { | ||
var request = {headers: {}}; | ||
request.headers.HTTP_X_FORWARDED_FOR = '177.139.100.100, 177.139.233.200, 177.139.233.139'; | ||
var trusted_proxy_list = ['177.139.233.135', '177.139.233.139', '177.139.233.140']; | ||
get_trusted_ip(request, trusted_proxy_list); | ||
assert.equal(request.clientIp, '177.139.100.100'); | ||
assert.equal(request.clientIpRoutable, true); | ||
}); | ||
}); | ||
describe('get_trusted_ip(): IPV4: PROXY LIST AS PARAM (INVALID)', function() { | ||
it('test_trusted_ip_proxy_list_as_params_invalid', function() { | ||
var request = {headers: {}}; | ||
request.headers.HTTP_X_FORWARDED_FOR = '177.139.100.100, 177.139.233.200, 177.139.233.130'; | ||
var trusted_proxy_list = ['177.139.233.139']; | ||
get_trusted_ip(request, trusted_proxy_list); | ||
assert.equal(request.clientIp, '127.0.0.1'); | ||
assert.equal(request.clientIpRoutable, false); | ||
}); | ||
}); | ||
describe('get_trusted_ip(): IPV4: PROXY LIST AS PARAM - X_FORWARDED_FOR(VALID)', function() { | ||
it('test_trusted_ip_proxy_list_as_params_multi_x_forwarded_for', function() { | ||
var request = {headers: {}}; | ||
request.headers.X_FORWARDED_FOR = '177.139.100.100, 177.139.233.200, 177.139.233.139'; | ||
var trusted_proxy_list = ['177.139.233.135', '177.139.233.139', '177.139.233.140']; | ||
get_trusted_ip(request, trusted_proxy_list); | ||
assert.equal(request.clientIp, '177.139.100.100'); | ||
assert.equal(request.clientIpRoutable, true); | ||
}); | ||
}); |
var get_ip = require('..')().get_ip, | ||
get_trusted_ip = require('..')().get_trusted_ip, | ||
assert = require('assert'); | ||
@@ -251,1 +252,12 @@ | ||
}); | ||
describe('get_trusted_ip(): IPV6: HTTP_X_FORWARDED_FOR', function() { | ||
it('test_http_x_forwarded_for_multiple', function() { | ||
var request = {headers: {}}; | ||
request.headers.HTTP_X_FORWARDED_FOR = '3ffe:1900:4545:3:200:f8ff:fe21:67cf, 74dc::02ba, 74dc::02bb'; | ||
var trusted_proxy_list = ['74dc::02bb']; | ||
get_trusted_ip(request, trusted_proxy_list); | ||
assert.equal(request.clientIp, '3ffe:1900:4545:3:200:f8ff:fe21:67cf'); | ||
assert.equal(request.clientIpRoutable, true); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
39202
0
781
0
204