Skip to content

Commit 1332409

Browse files
otaviojrindexzero
authored andcommitted
working on x-forwarded-for
1 parent 916d44e commit 1332409

File tree

2 files changed

+6
-74
lines changed

2 files changed

+6
-74
lines changed

lib/node-http-proxy/http-proxy.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
158158
req.headers['x-forwarded-proto'] = getProto(req);
159159
}
160160
}
161+
console.log(req.headers);
161162

162163
//
163164
// Emit the `start` event indicating that we have begun the proxy operation.
@@ -414,21 +415,21 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, head, buffer)
414415
// * `x-forwarded-proto`: Protocol of the original request
415416
// * `x-forwarded-port`: Port of the original request.
416417
//
417-
if (this.enable.xforward && req.connection && req.connection.socket) {
418+
if (this.enable.xforward && req.connection) {
418419
if (req.headers['x-forwarded-for']){
419-
var addressToAppend = "," + req.connection.remoteAddress || req.connection.socket.remoteAddress;
420+
var addressToAppend = "," + req.connection.remoteAddress || socket.remoteAddress;
420421
req.headers['x-forwarded-for'] += addressToAppend;
421422
}
422423
else {
423-
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress;
424+
req.headers['x-forwarded-for'] = req.connection.remoteAddress || socket.remoteAddress;
424425
}
425426

426427
if (req.headers['x-forwarded-port']){
427-
var portToAppend = "," + req.connection.remotePort || req.connection.socket.remotePort;
428+
var portToAppend = "," + req.connection.remotePort || socket.remotePort;
428429
req.headers['x-forwarded-port'] += portToAppend;
429430
}
430431
else {
431-
req.headers['x-forwarded-port'] = req.connection.remotePort || req.connection.socket.remotePort;
432+
req.headers['x-forwarded-port'] = req.connection.remotePort || socket.remotePort;
432433
}
433434

434435
if (req.headers['x-forwarded-proto']){

lib/node-http-proxy/routing-proxy.js

-69
Original file line numberDiff line numberDiff line change
@@ -180,41 +180,6 @@ function getProto(req) {
180180
//
181181
RoutingProxy.prototype.proxyRequest = function (req, res, options) {
182182
options = options || {};
183-
184-
//
185-
// Add common proxy headers to the request so that they can
186-
// be availible to the proxy target server. If the proxy is
187-
// part of proxy chain it will append the address:
188-
//
189-
// * `x-forwarded-for`: IP Address of the original request
190-
// * `x-forwarded-proto`: Protocol of the original request
191-
// * `x-forwarded-port`: Port of the original request.
192-
//
193-
if (this.enable.xforward && req.connection && req.socket) {
194-
if (req.headers['x-forwarded-for']){
195-
var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress;
196-
req.headers['x-forwarded-for'] += addressToAppend;
197-
}
198-
else {
199-
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
200-
}
201-
202-
if (req.headers['x-forwarded-port']){
203-
var portToAppend = "," + req.connection.remotePort || req.socket.remotePort;
204-
req.headers['x-forwarded-port'] += portToAppend;
205-
}
206-
else {
207-
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
208-
}
209-
210-
if (req.headers['x-forwarded-proto']){
211-
var protoToAppend = "," + getProto(req);
212-
req.headers['x-forwarded-proto'] += protoToAppend;
213-
}
214-
else {
215-
req.headers['x-forwarded-proto'] = getProto(req);
216-
}
217-
}
218183
var location;
219184

220185
//
@@ -286,40 +251,6 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) {
286251
RoutingProxy.prototype.proxyWebSocketRequest = function (req, socket, head, options) {
287252
options = options || {};
288253

289-
//
290-
// Add common proxy headers to the request so that they can
291-
// be availible to the proxy target server. If the proxy is
292-
// part of proxy chain it will append the address:
293-
//
294-
// * `x-forwarded-for`: IP Address of the original request
295-
// * `x-forwarded-proto`: Protocol of the original request
296-
// * `x-forwarded-port`: Port of the original request.
297-
//
298-
if (this.enable.xforward && req.connection && req.socket) {
299-
if (req.headers['x-forwarded-for']){
300-
var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress;
301-
req.headers['x-forwarded-for'] += addressToAppend;
302-
}
303-
else {
304-
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
305-
}
306-
307-
if (req.headers['x-forwarded-port']){
308-
var portToAppend = "," + req.connection.remotePort || req.socket.remotePort;
309-
req.headers['x-forwarded-port'] += portToAppend;
310-
}
311-
else {
312-
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
313-
}
314-
315-
if (req.headers['x-forwarded-proto']){
316-
var protoToAppend = "," + getProto(req);
317-
req.headers['x-forwarded-proto'] += protoToAppend;
318-
}
319-
else {
320-
req.headers['x-forwarded-proto'] = getProto(req);
321-
}
322-
}
323254
var location,
324255
proxy,
325256
key;

0 commit comments

Comments
 (0)