proxy-addr
Advanced tools
Comparing version 1.1.5 to 2.0.0
@@ -0,1 +1,6 @@ | ||
2.0.0 / 2017-08-08 | ||
================== | ||
* Drop support for Node.js below 0.10 | ||
1.1.5 / 2017-07-25 | ||
@@ -2,0 +7,0 @@ ================== |
188
index.js
@@ -14,5 +14,5 @@ /*! | ||
module.exports = proxyaddr; | ||
module.exports.all = alladdrs; | ||
module.exports.compile = compile; | ||
module.exports = proxyaddr | ||
module.exports.all = alladdrs | ||
module.exports.compile = compile | ||
@@ -24,4 +24,4 @@ /** | ||
var forwarded = require('forwarded'); | ||
var ipaddr = require('ipaddr.js'); | ||
var forwarded = require('forwarded') | ||
var ipaddr = require('ipaddr.js') | ||
@@ -33,5 +33,5 @@ /** | ||
var digitre = /^[0-9]+$/; | ||
var isip = ipaddr.isValid; | ||
var parseip = ipaddr.parse; | ||
var DIGIT_REGEXP = /^[0-9]+$/ | ||
var isip = ipaddr.isValid | ||
var parseip = ipaddr.parse | ||
@@ -43,7 +43,7 @@ /** | ||
var ipranges = { | ||
var IP_RANGES = { | ||
linklocal: ['169.254.0.0/16', 'fe80::/10'], | ||
loopback: ['127.0.0.1/8', '::1/128'], | ||
uniquelocal: ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', 'fc00::/7'] | ||
}; | ||
} | ||
@@ -59,22 +59,22 @@ /** | ||
function alladdrs(req, trust) { | ||
function alladdrs (req, trust) { | ||
// get addresses | ||
var addrs = forwarded(req); | ||
var addrs = forwarded(req) | ||
if (!trust) { | ||
// Return all addresses | ||
return addrs; | ||
return addrs | ||
} | ||
if (typeof trust !== 'function') { | ||
trust = compile(trust); | ||
trust = compile(trust) | ||
} | ||
for (var i = 0; i < addrs.length - 1; i++) { | ||
if (trust(addrs[i], i)) continue; | ||
if (trust(addrs[i], i)) continue | ||
addrs.length = i + 1; | ||
addrs.length = i + 1 | ||
} | ||
return addrs; | ||
return addrs | ||
} | ||
@@ -89,31 +89,31 @@ | ||
function compile(val) { | ||
function compile (val) { | ||
if (!val) { | ||
throw new TypeError('argument is required'); | ||
throw new TypeError('argument is required') | ||
} | ||
var trust; | ||
var trust | ||
if (typeof val === 'string') { | ||
trust = [val]; | ||
trust = [val] | ||
} else if (Array.isArray(val)) { | ||
trust = val.slice(); | ||
trust = val.slice() | ||
} else { | ||
throw new TypeError('unsupported trust argument'); | ||
throw new TypeError('unsupported trust argument') | ||
} | ||
for (var i = 0; i < trust.length; i++) { | ||
val = trust[i]; | ||
val = trust[i] | ||
if (!ipranges.hasOwnProperty(val)) { | ||
continue; | ||
if (!IP_RANGES.hasOwnProperty(val)) { | ||
continue | ||
} | ||
// Splice in pre-defined range | ||
val = ipranges[val]; | ||
trust.splice.apply(trust, [i, 1].concat(val)); | ||
i += val.length - 1; | ||
val = IP_RANGES[val] | ||
trust.splice.apply(trust, [i, 1].concat(val)) | ||
i += val.length - 1 | ||
} | ||
return compileTrust(compileRangeSubnets(trust)); | ||
return compileTrust(compileRangeSubnets(trust)) | ||
} | ||
@@ -128,10 +128,10 @@ | ||
function compileRangeSubnets(arr) { | ||
var rangeSubnets = new Array(arr.length); | ||
function compileRangeSubnets (arr) { | ||
var rangeSubnets = new Array(arr.length) | ||
for (var i = 0; i < arr.length; i++) { | ||
rangeSubnets[i] = parseipNotation(arr[i]); | ||
rangeSubnets[i] = parseipNotation(arr[i]) | ||
} | ||
return rangeSubnets; | ||
return rangeSubnets | ||
} | ||
@@ -146,5 +146,5 @@ | ||
function compileTrust(rangeSubnets) { | ||
function compileTrust (rangeSubnets) { | ||
// Return optimized function based on length | ||
var len = rangeSubnets.length; | ||
var len = rangeSubnets.length | ||
return len === 0 | ||
@@ -154,3 +154,3 @@ ? trustNone | ||
? trustSingle(rangeSubnets[0]) | ||
: trustMulti(rangeSubnets); | ||
: trustMulti(rangeSubnets) | ||
} | ||
@@ -165,17 +165,17 @@ | ||
function parseipNotation(note) { | ||
var pos = note.lastIndexOf('/'); | ||
function parseipNotation (note) { | ||
var pos = note.lastIndexOf('/') | ||
var str = pos !== -1 | ||
? note.substring(0, pos) | ||
: note; | ||
: note | ||
if (!isip(str)) { | ||
throw new TypeError('invalid IP address: ' + str); | ||
throw new TypeError('invalid IP address: ' + str) | ||
} | ||
var ip = parseip(str); | ||
var ip = parseip(str) | ||
if (pos === -1 && ip.kind() === 'ipv6' && ip.isIPv4MappedAddress()) { | ||
// Store as IPv4 | ||
ip = ip.toIPv4Address(); | ||
ip = ip.toIPv4Address() | ||
} | ||
@@ -185,23 +185,23 @@ | ||
? 128 | ||
: 32; | ||
: 32 | ||
var range = pos !== -1 | ||
? note.substring(pos + 1, note.length) | ||
: null; | ||
: null | ||
if (range === null) { | ||
range = max; | ||
} else if (digitre.test(range)) { | ||
range = parseInt(range, 10); | ||
range = max | ||
} else if (DIGIT_REGEXP.test(range)) { | ||
range = parseInt(range, 10) | ||
} else if (ip.kind() === 'ipv4' && isip(range)) { | ||
range = parseNetmask(range); | ||
range = parseNetmask(range) | ||
} else { | ||
range = null; | ||
range = null | ||
} | ||
if (range <= 0 || range > max) { | ||
throw new TypeError('invalid range on address: ' + note); | ||
throw new TypeError('invalid range on address: ' + note) | ||
} | ||
return [ip, range]; | ||
return [ip, range] | ||
} | ||
@@ -216,9 +216,9 @@ | ||
function parseNetmask(netmask) { | ||
var ip = parseip(netmask); | ||
var kind = ip.kind(); | ||
function parseNetmask (netmask) { | ||
var ip = parseip(netmask) | ||
var kind = ip.kind() | ||
return kind === 'ipv4' | ||
? ip.prefixLengthFromSubnetMask() | ||
: null; | ||
: null | ||
} | ||
@@ -234,15 +234,15 @@ | ||
function proxyaddr(req, trust) { | ||
function proxyaddr (req, trust) { | ||
if (!req) { | ||
throw new TypeError('req argument is required'); | ||
throw new TypeError('req argument is required') | ||
} | ||
if (!trust) { | ||
throw new TypeError('trust argument is required'); | ||
throw new TypeError('trust argument is required') | ||
} | ||
var addrs = alladdrs(req, trust); | ||
var addr = addrs[addrs.length - 1]; | ||
var addrs = alladdrs(req, trust) | ||
var addr = addrs[addrs.length - 1] | ||
return addr; | ||
return addr | ||
} | ||
@@ -256,4 +256,4 @@ | ||
function trustNone() { | ||
return false; | ||
function trustNone () { | ||
return false | ||
} | ||
@@ -268,16 +268,16 @@ | ||
function trustMulti(subnets) { | ||
return function trust(addr) { | ||
if (!isip(addr)) return false; | ||
function trustMulti (subnets) { | ||
return function trust (addr) { | ||
if (!isip(addr)) return false | ||
var ip = parseip(addr); | ||
var ipconv; | ||
var kind = ip.kind(); | ||
var ip = parseip(addr) | ||
var ipconv | ||
var kind = ip.kind() | ||
for (var i = 0; i < subnets.length; i++) { | ||
var subnet = subnets[i]; | ||
var subnetip = subnet[0]; | ||
var subnetkind = subnetip.kind(); | ||
var subnetrange = subnet[1]; | ||
var trusted = ip; | ||
var subnet = subnets[i] | ||
var subnetip = subnet[0] | ||
var subnetkind = subnetip.kind() | ||
var subnetrange = subnet[1] | ||
var trusted = ip | ||
@@ -287,3 +287,3 @@ if (kind !== subnetkind) { | ||
// Incompatible IP addresses | ||
continue; | ||
continue | ||
} | ||
@@ -295,15 +295,15 @@ | ||
? ip.toIPv4Address() | ||
: ip.toIPv4MappedAddress(); | ||
: ip.toIPv4MappedAddress() | ||
} | ||
trusted = ipconv; | ||
trusted = ipconv | ||
} | ||
if (trusted.match(subnetip, subnetrange)) { | ||
return true; | ||
return true | ||
} | ||
} | ||
return false; | ||
}; | ||
return false | ||
} | ||
} | ||
@@ -318,13 +318,13 @@ | ||
function trustSingle(subnet) { | ||
var subnetip = subnet[0]; | ||
var subnetkind = subnetip.kind(); | ||
var subnetisipv4 = subnetkind === 'ipv4'; | ||
var subnetrange = subnet[1]; | ||
function trustSingle (subnet) { | ||
var subnetip = subnet[0] | ||
var subnetkind = subnetip.kind() | ||
var subnetisipv4 = subnetkind === 'ipv4' | ||
var subnetrange = subnet[1] | ||
return function trust(addr) { | ||
if (!isip(addr)) return false; | ||
return function trust (addr) { | ||
if (!isip(addr)) return false | ||
var ip = parseip(addr); | ||
var kind = ip.kind(); | ||
var ip = parseip(addr) | ||
var kind = ip.kind() | ||
@@ -334,3 +334,3 @@ if (kind !== subnetkind) { | ||
// Incompatible IP addresses | ||
return false; | ||
return false | ||
} | ||
@@ -341,7 +341,7 @@ | ||
? ip.toIPv4Address() | ||
: ip.toIPv4MappedAddress(); | ||
: ip.toIPv4MappedAddress() | ||
} | ||
return ip.match(subnetip, subnetrange); | ||
}; | ||
return ip.match(subnetip, subnetrange) | ||
} | ||
} |
{ | ||
"name": "proxy-addr", | ||
"description": "Determine address of proxied request", | ||
"version": "1.1.5", | ||
"version": "2.0.0", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
@@ -20,4 +20,11 @@ "license": "MIT", | ||
"beautify-benchmark": "0.2.4", | ||
"istanbul": "0.4.5", | ||
"mocha": "~1.21.5" | ||
"eslint": "3.19.0", | ||
"eslint-config-standard": "10.2.1", | ||
"eslint-plugin-import": "2.7.0", | ||
"eslint-plugin-markdown": "1.0.0-beta.6", | ||
"eslint-plugin-node": "5.1.1", | ||
"eslint-plugin-promise": "3.5.0", | ||
"eslint-plugin-standard": "3.0.1", | ||
"mocha": "3.5.0", | ||
"nyc": "10.3.2" | ||
}, | ||
@@ -31,10 +38,11 @@ "files": [ | ||
"engines": { | ||
"node": ">= 0.6" | ||
"node": ">= 0.10" | ||
}, | ||
"scripts": { | ||
"bench": "node benchmark/index.js", | ||
"lint": "eslint --plugin markdown --ext js,md .", | ||
"test": "mocha --reporter spec --bail --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" | ||
"test-cov": "nyc --reporter=text npm test", | ||
"test-travis": "nyc --reporter=html --reporter=text npm test" | ||
} | ||
} |
@@ -23,2 +23,4 @@ # proxy-addr | ||
<!-- eslint-disable no-unused-vars --> | ||
```js | ||
@@ -36,5 +38,7 @@ var proxyaddr = require('proxy-addr') | ||
<!-- eslint-disable no-undef --> | ||
```js | ||
proxyaddr(req, function(addr){ return addr === '127.0.0.1' }) | ||
proxyaddr(req, function(addr, i){ return i < 1 }) | ||
proxyaddr(req, function (addr) { return addr === '127.0.0.1' }) | ||
proxyaddr(req, function (addr, i) { return i < 1 }) | ||
``` | ||
@@ -46,2 +50,4 @@ | ||
<!-- eslint-disable no-undef --> | ||
```js | ||
@@ -56,2 +62,4 @@ proxyaddr(req, '127.0.0.1') | ||
<!-- eslint-disable no-undef --> | ||
```js | ||
@@ -69,2 +77,4 @@ proxyaddr(req, '::1') | ||
<!-- eslint-disable no-undef --> | ||
```js | ||
@@ -94,2 +104,4 @@ proxyaddr(req, 'loopback') | ||
<!-- eslint-disable no-undef --> | ||
```js | ||
@@ -102,2 +114,4 @@ proxyaddr.all(req) | ||
<!-- eslint-disable no-undef --> | ||
```js | ||
@@ -113,5 +127,7 @@ proxyaddr.all(req, 'loopback') | ||
<!-- eslint-disable no-undef, no-unused-vars --> | ||
```js | ||
var trust = proxyaddr.compile('localhost') | ||
var addr = proxyaddr(req, trust) | ||
var addr = proxyaddr(req, trust) | ||
``` | ||
@@ -118,0 +134,0 @@ |
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
15016
157
11