Skip to content

Commit

Permalink
Initial implementation; copy of express logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dougwilson committed May 5, 2014
0 parents commit c6f6bfa
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test/
.travis.yml
9 changes: 9 additions & 0 deletions .travis.yml
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
4 changes: 4 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
HEAD
====

* Initial release
22 changes: 22 additions & 0 deletions LICENSE
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.
25 changes: 25 additions & 0 deletions README.md
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)
48 changes: 48 additions & 0 deletions index.js
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.

Copy link
@Fishrock123

Fishrock123 May 5, 2014

Member

Is this actually any better than !!val?

This comment has been minimized.

Copy link
@dougwilson

dougwilson May 5, 2014

Author Contributor

One less op?

This comment has been minimized.

Copy link
@jonathanong

jonathanong May 5, 2014

Member

lol why do you even need to do this? just pass in Boolean

This comment has been minimized.

Copy link
@dougwilson

dougwilson May 5, 2014

Author Contributor

Yea, I thought about doing that :P

This comment has been minimized.

Copy link
@Fishrock123

Fishrock123 May 5, 2014

Member

I'd wager that v8 optimizes !!val to just one op anyways, but the latest commit makes more sense.

This comment has been minimized.

Copy link
@dougwilson

dougwilson May 5, 2014

Author Contributor

Who knows :P

30 changes: 30 additions & 0 deletions package.json
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/"
}
}
86 changes: 86 additions & 0 deletions test/test.js
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 || {}
};
}

0 comments on commit c6f6bfa

Please sign in to comment.