-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation; copy of express logic
- Loading branch information
0 parents
commit c6f6bfa
Showing
9 changed files
with
227 additions
and
0 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
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,2 @@ | ||
test/ | ||
.travis.yml |
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,9 @@ | ||
language: node_js | ||
node_js: | ||
- "0.8" | ||
- "0.10" | ||
- "0.11" | ||
matrix: | ||
allow_failures: | ||
- node_js: "0.11" | ||
fast_finish: true |
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,4 @@ | ||
HEAD | ||
==== | ||
|
||
* Initial release |
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,22 @@ | ||
(The MIT License) | ||
|
||
Copyright (c) 2014 Douglas Christopher Wilson | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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,25 @@ | ||
# proxy-addr [![Build Status](https://travis-ci.org/expressjs/proxy-addr.svg?branch=master)](https://travis-ci.org/expressjs/proxy-addr) [![NPM version](https://badge.fury.io/js/proxy-addr.svg)](http://badge.fury.io/js/proxy-addr) | ||
|
||
Determine address of proxied request | ||
|
||
## Install | ||
|
||
npm install proxy-addr | ||
|
||
## API | ||
|
||
var proxyaddr = require('proxy-addr'); | ||
|
||
### proxyaddr(req) | ||
|
||
Return the address of the request. This returns the furthest-away | ||
address. | ||
|
||
### proxyaddr.all(req) | ||
|
||
Return all the addresses of the request. This array is ordered from | ||
closest to furthest (i.e. `arr[0] === req.connection.remoteAddress`). | ||
|
||
## License | ||
|
||
[MIT](LICENSE) |
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,48 @@ | ||
/*! | ||
* proxy-addr | ||
* Copyright(c) 2014 Douglas Christopher Wilson | ||
* MIT Licensed | ||
*/ | ||
|
||
/** | ||
* Module exports. | ||
*/ | ||
|
||
module.exports = proxyaddr; | ||
module.exports.all = proxyaddrs; | ||
|
||
/** | ||
* Determine IP of proxied request. | ||
* | ||
* @param {Object} request | ||
* @api public | ||
*/ | ||
|
||
function proxyaddr(req) { | ||
var addrs = proxyaddrs(req); | ||
|
||
return addrs[addrs.length - 1]; | ||
} | ||
|
||
/** | ||
* Get all addresses in the request. | ||
* | ||
* @param {Object} request | ||
* @api public | ||
*/ | ||
|
||
function proxyaddrs(req) { | ||
if (!req) throw new TypeError('req argument is required'); | ||
|
||
var proxyAddrs = (req.headers['x-forwarded-for'] || '') | ||
.split(/ *, */) | ||
.filter(isTruthy) | ||
.reverse(); | ||
var socketAddr = req.connection.remoteAddress; | ||
|
||
return [socketAddr].concat(proxyAddrs); | ||
} | ||
|
||
function isTruthy(val) { | ||
return Boolean(val); | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Fishrock123
Member
|
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,30 @@ | ||
{ | ||
"name": "proxy-addr", | ||
"description": "Determine address of proxied request", | ||
"version": "0.0.0", | ||
"author": "Douglas Christopher Wilson <doug@somethingdoug.com>", | ||
"license": "MIT", | ||
"keywords": [ | ||
"ip", | ||
"proxy", | ||
"x-forwarded-for" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/expressjs/proxy-ip.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/expressjs/proxy-ip/issues" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"mocha": "~1.18.2", | ||
"should": "~3.3.1" | ||
}, | ||
"engines": { | ||
"node": ">= 0.8.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha --reporter spec test/" | ||
} | ||
} |
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,86 @@ | ||
|
||
var proxyaddr = require('..'); | ||
var should = require('should'); | ||
|
||
describe('proxyaddr(req)', function () { | ||
describe('arguments', function () { | ||
describe('req', function () { | ||
it('should be required', function () { | ||
proxyaddr.bind().should.throw(/req.*required/); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with no headers', function () { | ||
it('should return socket address', function () { | ||
var req = createReq('127.0.0.1'); | ||
proxyaddr(req).should.equal('127.0.0.1'); | ||
}); | ||
}); | ||
|
||
describe('with x-forwarded-for header', function () { | ||
describe('single', function () { | ||
it('should return x-forwarded-for', function () { | ||
var req = createReq('127.0.0.1', { | ||
'x-forwarded-for': '10.0.0.1' | ||
}); | ||
proxyaddr(req).should.equal('10.0.0.1'); | ||
}); | ||
}); | ||
|
||
describe('multiple', function () { | ||
it('should return left-most', function () { | ||
var req = createReq('127.0.0.1', { | ||
'x-forwarded-for': '10.0.0.1, 10.0.0.2' | ||
}); | ||
proxyaddr(req).should.equal('10.0.0.1'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('proxyaddr.all(req)', function () { | ||
describe('arguments', function () { | ||
describe('req', function () { | ||
it('should be required', function () { | ||
proxyaddr.all.bind().should.throw(/req.*required/); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with no headers', function () { | ||
it('should return socket address', function () { | ||
var req = createReq('127.0.0.1'); | ||
proxyaddr.all(req).should.eql(['127.0.0.1']); | ||
}); | ||
}); | ||
|
||
describe('with x-forwarded-for header', function () { | ||
describe('single', function () { | ||
it('should include x-forwarded-for', function () { | ||
var req = createReq('127.0.0.1', { | ||
'x-forwarded-for': '10.0.0.1' | ||
}); | ||
proxyaddr.all(req).should.eql(['127.0.0.1', '10.0.0.1']); | ||
}); | ||
}); | ||
|
||
describe('multiple', function () { | ||
it('should include x-forwarded-for', function () { | ||
var req = createReq('127.0.0.1', { | ||
'x-forwarded-for': '10.0.0.1, 10.0.0.2' | ||
}); | ||
proxyaddr.all(req).should.eql(['127.0.0.1', '10.0.0.2', '10.0.0.1']); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function createReq(socketAddr, headers) { | ||
return { | ||
connection: { | ||
remoteAddress: socketAddr | ||
}, | ||
headers: headers || {} | ||
}; | ||
} |
Is this actually any better than
!!val
?