Skip to content

Commit b48c950

Browse files
committed
Provides xforward setting to fix #24
1 parent 7f71e91 commit b48c950

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ module.exports = function(grunt) {
5050
https: true,
5151
rejectUnauthorized: true,
5252
changeOrigin: true,
53+
xforward: true,
5354
rewrite: {
5455
'^/full': '/anothercontext'
5556
}

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ grunt.initConfig({
3737
host: '10.10.2.202',
3838
port: 8080,
3939
https: false,
40-
changeOrigin: false
40+
changeOrigin: false,
41+
xforward: false
4142
}
4243
]
4344
}
@@ -148,6 +149,16 @@ Default: false
148149
149150
Whether to reject self-signed certificates when https: true is set. Defaults to accept self-signed certs since proxy is meant for development environments.
150151
152+
#### options.xforward:
153+
Type: `Boolean`
154+
Default: false
155+
156+
Whether to add x-forward headers to the proxy request, such as
157+
"x-forwarded-for": "127.0.0.1",
158+
"x-forwarded-port": 50892,
159+
"x-forwarded-proto": "http"
160+
161+
151162
#### options.appendProxies
152163
Type: `Boolean`
153164
Default: true
@@ -225,3 +236,4 @@ grunt.registerTask('e2etest', function (target) {
225236
* 0.1.3 Bumped http-proxy dependency to 0.10.2
226237
* 0.1.4 Added proxy rewrite support (thanks to @slawrence)
227238
* 0.1.5 Default rejectUnauthorized to false to allow self-signed certificates over SSL
239+
* 0.1.6 Add xforward option

lib/utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ utils.reset = function() {
2323
};
2424

2525
utils.validateRewrite = function (rule) {
26-
if (!rule ||
26+
if (!rule ||
2727
typeof rule.from === 'undefined' ||
2828
typeof rule.to === 'undefined' ||
2929
typeof rule.from !== 'string' ||
@@ -68,7 +68,7 @@ utils.proxyRequest = function (req, res, next) {
6868

6969
var source = req.originalUrl;
7070
var target = (proxy.server.target.https ? 'https://' : 'http://') + proxy.server.target.host + ':' + proxy.server.target.port + req.url;
71-
utils.log.verbose.writeln('Proxied request: ' + source + ' -> ' + target);
71+
utils.log.verbose.writeln('Proxied request: ' + source + ' -> ' + target + '\n' + JSON.stringify(req.headers, true, 2));
7272
}
7373
});
7474
if (!proxied) {

tasks/connect_proxy.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ module.exports = function(grunt) {
4343
port: 80,
4444
https: false,
4545
changeOrigin: false,
46+
xforward: false,
4647
rejectUnauthorized: false,
4748
rules: []
4849
});
@@ -51,7 +52,10 @@ module.exports = function(grunt) {
5152
utils.registerProxy({
5253
server: new httpProxy.HttpProxy({
5354
target: proxyOption,
54-
changeOrigin: proxyOption.changeOrigin
55+
changeOrigin: proxyOption.changeOrigin,
56+
enable : {
57+
xforward: proxyOption.xforward // enables X-Forwarded-For
58+
}
5559
}),
5660
config: proxyOption
5761
});

test/connect_proxy_test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ exports.connect_proxy = {
2929
done();
3030
},
3131
default_options: function(test) {
32-
test.expect(8);
32+
test.expect(9);
3333
var proxies = utils.proxies();
3434

3535
test.equal(proxies.length, 4, 'should return four valid proxies');
@@ -40,11 +40,12 @@ exports.connect_proxy = {
4040
test.equal(proxies[0].config.https, false, 'should have default http');
4141
test.equal(proxies[0].config.rejectUnauthorized, false, 'should have reject unauthorized default to false');
4242
test.equal(proxies[0].config.changeOrigin, false, 'should have default change origin');
43+
test.equal(proxies[0].config.xforward, false, 'should have default xforward');
4344

4445
test.done();
4546
},
4647
full_options: function(test) {
47-
test.expect(11);
48+
test.expect(12);
4849
var proxies = utils.proxies();
4950

5051
test.equal(proxies.length, 4, 'should return four valid proxies');
@@ -55,6 +56,7 @@ exports.connect_proxy = {
5556
test.equal(proxies[1].config.https, true, 'should have http set from config');
5657
test.equal(proxies[1].config.rejectUnauthorized, true, 'should have reject unauthorized set from config');
5758
test.equal(proxies[1].config.changeOrigin, true, 'should have change origin set from config');
59+
test.equal(proxies[1].config.xforward, true, 'should have xforward set from config');
5860
test.deepEqual(proxies[1].config.rewrite, { '^/full': '/anothercontext' }, 'should have rewrite set from config');
5961
test.equal(proxies[1].config.rules.length, 1, 'rules array should have an item');
6062
test.deepEqual(proxies[1].config.rules[0], { from: new RegExp('^/full'), to: '/anothercontext'}, 'rules object should be converted to regex');

0 commit comments

Comments
 (0)