-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
728 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ node_js: | |
- "0.8" | ||
- "0.10" | ||
- "0.11" | ||
- "0.12" | ||
matrix: | ||
allow_failures: | ||
- node_js: "0.11" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,65 @@ | ||
/*! | ||
* forwarded | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* Module exports. | ||
*/ | ||
'use strict' | ||
|
||
module.exports = forwarded | ||
var Processor = require('./lib/processor') | ||
var schemas = require('./lib/schemas') | ||
|
||
/** | ||
* Get all addresses in the request, using the `X-Forwarded-For` header. | ||
* | ||
* @param {Object} req | ||
* @param {http.IncomingMessage} req | ||
* @api public | ||
*/ | ||
|
||
function forwarded(req) { | ||
module.exports = function forwarded (req, options) { | ||
options = options || {} | ||
|
||
var opts = { | ||
// default to only common + standard | ||
// array order matters here | ||
schemas: options.schemas || [ | ||
'xff', | ||
'rfc7239' | ||
] | ||
} | ||
|
||
// consistent case | ||
opts.schemas.map(Function.prototype.call, String.prototype.toLowerCase) | ||
|
||
if (!req) { | ||
throw new TypeError('argument req is required') | ||
} | ||
|
||
// simple header parsing | ||
var proxyAddrs = (req.headers['x-forwarded-for'] || '') | ||
.split(/ *, */) | ||
.filter(Boolean) | ||
.reverse() | ||
var socketAddr = req.connection.remoteAddress | ||
var addrs = [socketAddr].concat(proxyAddrs) | ||
// start with default values from socket connection | ||
var forwarded = { | ||
addrs: [req.connection.remoteAddress], | ||
by: null, | ||
host: req.headers && req.headers.host ? req.headers.host : undefined, | ||
port: req.connection.remotePort.toString(), | ||
ports: [req.connection.remotePort.toString()], | ||
proto: req.connection.encrypted ? 'https' : 'http' | ||
} | ||
|
||
// alias "for" to keep with RFC7239 naming | ||
forwarded.for = forwarded.addrs | ||
|
||
return opts.schemas | ||
// check if schemas exist | ||
.filter(function (name) { | ||
return schemas[name] | ||
}) | ||
|
||
// process schemas | ||
.reduce(function (forwarded, name) { | ||
var result = new Processor(req, schemas[name]) | ||
|
||
// return all addresses | ||
return addrs | ||
// update forwarded object | ||
return { | ||
addrs: forwarded.addrs.concat(result.addrs).filter(Boolean), | ||
by: result.by ? result.by : forwarded.by, | ||
host: result.host ? result.host : forwarded.host, | ||
port: result.port ? result.port : forwarded.port, | ||
ports: forwarded.ports.concat([result.port]).filter(Boolean), | ||
proto: result.proto ? result.proto : forwarded.proto | ||
} | ||
}, forwarded) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
'use strict' | ||
|
||
var debug = require('debug')('forwarded') | ||
|
||
function processor (req, schema) { | ||
if (typeof schema === 'function') { | ||
return schema(req) | ||
} | ||
|
||
this.req = req | ||
this.schema = schema | ||
|
||
return { | ||
addrs: this.addrs(), | ||
host: this.host(), | ||
port: this.port(), | ||
proto: this.protocol() | ||
} | ||
} | ||
|
||
processor.prototype.host = function () { | ||
if (this.schema.host && this.req.headers[this.schema.host]) { | ||
var value = this.req.headers[this.schema.host] | ||
|
||
debug('found header [%s = %s]', this.schema.host, value) | ||
|
||
return this.req.headers[this.schema.host] | ||
} | ||
} | ||
|
||
processor.prototype.port = function () { | ||
if (this.schema.port && this.req.headers[this.schema.port]) { | ||
var value = this.req.headers[this.schema.port] | ||
|
||
debug('found header [%s = %s]', this.schema.port, value) | ||
|
||
return value | ||
} | ||
} | ||
|
||
processor.prototype.addrs = function () { | ||
if (this.schema.addrs && this.req.headers[this.schema.addrs]) { | ||
var value = this.req.headers[this.schema.addrs] | ||
|
||
debug('found header [%s = %s]', this.schema.addrs, value) | ||
|
||
return value | ||
.split(/ *, */) | ||
.filter(Boolean) | ||
.reverse() | ||
} | ||
} | ||
|
||
processor.prototype.protocol = function () { | ||
// utility | ||
function runner (obj) { | ||
// multiple possible values | ||
if (Array.isArray(obj)) { | ||
// get the last succesful item | ||
return obj.map(runner.bind(this)).reduce(function (prev, curr) { | ||
return curr ? curr : prev | ||
}) | ||
} | ||
|
||
if (typeof obj === 'function') { | ||
return obj.call(this, this.req) | ||
} | ||
|
||
if (this.req.headers[obj]) { | ||
debug('found header [%s = %s]', obj, this.req.headers[obj]) | ||
|
||
return this.req.headers[obj] | ||
} | ||
} | ||
|
||
// actually run | ||
if (this.schema.proto) { | ||
return runner.call(this, this.schema.proto) | ||
} | ||
} | ||
|
||
module.exports = processor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
var debug = require('debug')('forwarded') | ||
|
||
function isSecure (req) { | ||
try { | ||
var cf = JSON.parse(req.headers['cf-visitor']) | ||
return cf.scheme !== undefined | ||
} catch (e) { | ||
debug('could not parse "cf-visitor" header: %s', req.headers['cf-visitor']) | ||
} | ||
|
||
return false | ||
} | ||
|
||
module.exports = { | ||
addrs: 'cf-connecting-ip', | ||
proto: isSecure | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict' | ||
|
||
function isSecure (req) { | ||
return ~['1', 'true'].indexOf(req.headers['fastly-ssl']) | ||
} | ||
|
||
module.exports = { | ||
addrs: 'fastly-client-ip', | ||
port: 'fastly-client-port', | ||
proto: isSecure | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
cloudflare: require('./cloudflare'), | ||
fastly: require('./fastly'), | ||
microsoft: require('./microsoft'), | ||
nginx: require('./nginx'), | ||
rackspace: require('./rackspace'), | ||
rfc7239: require('./rfc7239'), | ||
xff: require('./xff'), | ||
zscaler: require('./zscaler') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
proto: function isSecure (req) { | ||
return req.headers['front-end-https'] === 'on' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
addrs: 'x-real-ip', | ||
port: 'x-real-port', | ||
proto: ['x-real-proto', 'x-url-scheme'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
addrs: 'x-cluster-client-ip' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
function splitMap (string, separator, cb) { | ||
// split into elements | ||
return string.split(separator) | ||
.filter(Boolean) | ||
.forEach(cb) | ||
} | ||
|
||
function parsePart (part) { | ||
var pair = part.split(/ *= */) | ||
|
||
var name = pair[0].toLowerCase() | ||
var value = pair[1] | ||
|
||
if (value) { | ||
switch (typeof this[name]) { | ||
case 'undefined': | ||
this[name] = value | ||
break | ||
|
||
// convert to array | ||
case 'string': | ||
this[name] = [this[name], value] | ||
break | ||
|
||
// append to array | ||
case 'object': | ||
this[name].push(value) | ||
break | ||
} | ||
} | ||
} | ||
|
||
var ELEMENT_SEPARATOR = / *; */ | ||
var PART_SEPARATOR = / *, */ | ||
|
||
module.exports = function (req) { | ||
var forwarded = {} | ||
var header = req.headers.forwarded | ||
|
||
if (!header) { | ||
return forwarded | ||
} | ||
|
||
splitMap(header, ELEMENT_SEPARATOR, function parseElement (el) { | ||
return splitMap(el, PART_SEPARATOR, parsePart.bind(forwarded)) | ||
}) | ||
|
||
// re-mapping | ||
forwarded.addrs = forwarded.for | ||
|
||
return forwarded | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict' | ||
|
||
function isSecure (req) { | ||
return req.headers['x-forwarded-ssl'] === 'on' | ||
} | ||
|
||
module.exports = { | ||
addrs: 'x-forwarded-for', | ||
host: 'x-forwarded-host', | ||
port: 'x-forwarded-port', | ||
proto: ['x-forwarded-proto', 'x-forwarded-protocol', isSecure], | ||
protoFn: isSecure | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
module.exports = { | ||
addrs: 'z-forwarded-for', | ||
host: 'z-forwarded-host', | ||
port: 'z-forwarded-port', | ||
proto: ['z-forwarded-proto', 'z-forwarded-protocol'] | ||
} |
Oops, something went wrong.