Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
Merge a3e2d13 into 06f7787
Browse files Browse the repository at this point in the history
  • Loading branch information
FORBI committed Jan 5, 2018
2 parents 06f7787 + a3e2d13 commit 2feb817
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 132 deletions.
48 changes: 28 additions & 20 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
{
"rules": {
"brace-style": [ 2, "stroustrup" ],
"curly": [2, "all"],
"indent": [ 2, 4 ],
"linebreak-style": [ 2, "unix" ],
"no-console": 1,
"no-mixed-spaces-and-tabs": 2,
"no-var": 1,
"object-shorthand": [ 2, "properties" ],
"prefer-const": 2,
"quotes": [ 2, "single" ],
"semi": [ 2, "always" ],
"semi-spacing": [ 2, { "before": false, "after": true } ],
"space-before-function-paren": [ 2, "always" ],
},
"env": {
"node": true,
"es6": true
},
"extends": "eslint:recommended"
"rules": {
"brace-style": [2, "stroustrup"],
"curly": [2, "all"],
"indent": [2, 4],
"linebreak-style": [2, "unix"],
"no-console": 1,
"no-mixed-spaces-and-tabs": 2,
"no-var": 1,
"object-shorthand": [2, "properties"],
"prefer-const": 2,
"quotes": [2, "single"],
"semi": [2, "always"],
"semi-spacing": [2, { "before": false, "after": true }],
"space-before-function-paren": [2, "always"]
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 8,
"ecmaFeatures": {
"jsx": true,
"experimentalObjectRestSpread": true
}
},
"env": {
"node": true,
"es6": true
},
"extends": "eslint:recommended"
}
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
language: node_js
node_js:
- node
- 7
- 6
- 4
- 8
28 changes: 15 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ This plugin sets `request.info.remotePort` to the value of the `x-forwarded-port
### Usage

``` javascript
const Hapi = require('hapi');

const server = new Hapi.Server();
server.connection();

server.register(require('therealyou'), function (err) {

// Assuming no err, start server

server.start(function () {
// ..
});
});
const Hapi = require("hapi");

const server = new Hapi.Server({
host: 'localhost',
port: 3000
})


server.register({
plugin: require('./lib')
}).then(_=>{
server.start(function() {
console.log("up");
});
})
```

## License
Expand Down
43 changes: 20 additions & 23 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
'use strict';

exports.register = function (server, options, next) {

server.ext('onRequest', function (request, reply) {

if (request.headers['x-forwarded-for']) {

request.info.remoteAddress = request.headers['x-forwarded-for'].split(',')[0].trim();
}

if (request.headers['x-forwarded-port']) {

request.info.remotePort = request.headers['x-forwarded-port'];
}

return reply.continue();
});

return next();
};

exports.register.attributes = {
exports.plugin = {
register: (server) => {
server.ext({
type: 'onRequest',
method: function (request, h) {
if (request.headers['x-forwarded-for']) {
request.info.remoteAddress = request.headers[
'x-forwarded-for'
]
.split(',')[0]
.trim();
}
if (request.headers['x-forwarded-port']) {
request.info.remotePort =
request.headers['x-forwarded-port'];
}
return h.continue;
}
});
},
pkg: require('../package.json')
};
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "therealyou",
"version": "1.1.0",
"version": "2.0.0",
"description": "hapi.js plugin for setting the remoteAddress and remotePort based on the X-Forwarded-For and X-Forwarded-Port headers",
"main": "lib/index.js",
"author": "Brian Delahunty <brian@briandela.com>",
"scripts": {
"test": "lab -r lcov | node node_modules/coveralls/bin/coveralls.js; lab -a code -v -t 100 -L",
"test": "lab -r lcov | node ./node_modules/coveralls/bin/coveralls.js lab -a code -v -t 100 -L",
"lint": "lab -L -d"
},
"repository": {
Expand All @@ -31,10 +31,10 @@
],
"homepage": "https://github.com/briandela/therealyou",
"devDependencies": {
"code": "^4.0.0",
"coveralls": "^2.11.14",
"hapi": "^15.2.0",
"lab": "^11.2.0",
"code": "5.1.2",
"coveralls": "^3.0.0",
"hapi": "^17",
"lab": "15",
"pre-commit": "^1.1.3"
}
}
109 changes: 43 additions & 66 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@ const Lab = require('lab');
const Hapi = require('hapi');
const Code = require('code');

const lab = exports.lab = Lab.script();
const lab = (exports.lab = Lab.script());
const expect = Code.expect;
const describe = lab.describe;
const it = lab.it;
const before = lab.before;

describe('x-forwarded-for', function () {

describe('x-forwarded-for', () => {
let server;

before(function (done) {

before(() => {
server = new Hapi.Server();
server.connection();

server
.register({
plugin: require('..')
})
.then(() => {
server.start();
});
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {

return reply(request.info.remoteAddress);
handler: function (request) {
return request.info.remoteAddress;
}
});

server.register(require('..'), done);
});

it('sets request.info.remoteAddress to the first value of x-forwarded-for', function (done) {

it('sets request.info.remoteAddress to the first value of x-forwarded-for', async () => {
const requestOptions = {
method: 'GET',
url: '/',
Expand All @@ -41,17 +41,12 @@ describe('x-forwarded-for', function () {
}
};

server.inject(requestOptions, function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('192.16.184.0');

done();
});
const res = await server.inject(requestOptions);
expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('192.16.184.0');
});

it('works when there are multiple addresses in x-forwarded-for', function (done) {

it('works when there are multiple addresses in x-forwarded-for', async () => {
const requestOptions = {
method: 'GET',
url: '/',
Expand All @@ -60,85 +55,67 @@ describe('x-forwarded-for', function () {
}
};

server.inject(requestOptions, function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('192.16.184.5');

done();
});
const res = await server.inject(requestOptions);
expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('192.16.184.5');
});

it('does not change request.info.remoteAddress if there is no x-forwarded-for', function (done) {

it('does not change request.info.remoteAddress if there is no x-forwarded-for', async () => {
const requestOptions = {
method: 'GET',
url: '/'
};

server.inject(requestOptions, function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('127.0.0.1');

done();
});
const res = await server.inject(requestOptions);
expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('127.0.0.1');
});
});

describe('x-forwarded-port', function () {

describe('x-forwarded-port', () => {
let server;

before(function (done) {

before(() => {
server = new Hapi.Server();
server.connection();

server
.register({
plugin: require('..')
})
.then(() => {
server.start();
});
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {

return reply(request.info.remotePort);
handler: function (request) {
return request.info.remoteAddress;
}
});

server.register(require('..'), done);
});

it('sets request.info.remotePort to the value of x-forwarded-for', function (done) {

it('sets request.info.remotePort to the value of x-forwarded-for', async () => {
const requestOptions = {
method: 'GET',
url: '/',
headers: {
'x-forwarded-port': '3781'
}
};

server.inject(requestOptions, function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.equal('3781');

done();
});
const res = await server.inject(requestOptions);
expect(res.statusCode).to.equal(200);
expect(res.request.info.remotePort).to.equal('3781');
});

it('does not change request.info.remotePort if there is no x-forwarded-port', function (done) {

it('does not change request.info.remotePort if there is no x-forwarded-port', async () => {
const requestOptions = {
method: 'GET',
url: '/'
};

server.inject(requestOptions, function (res) {

expect(res.statusCode).to.equal(200);
expect(res.result).to.be.null();
const res = await server.inject(requestOptions);

done();
});
expect(res.statusCode).to.equal(200);
expect(res.request.info.remotePort).to.be.equal('');
});
});

0 comments on commit 2feb817

Please sign in to comment.