-
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
26 changed files
with
882 additions
and
67 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
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
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 !~[undefined, 'http'].indexOf(cf.scheme) | ||
} 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 req.headers['fastly-ssl'] !== undefined | ||
} | ||
|
||
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,59 @@ | ||
'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)) | ||
}) | ||
|
||
// ensure result is an array | ||
if (forwarded.for && !Array.isArray(forwarded.for)) { | ||
forwarded.for = [forwarded.for] | ||
} | ||
|
||
// create alias | ||
forwarded.addrs = forwarded.for | ||
|
||
return forwarded | ||
} |
Oops, something went wrong.