[refactor] start working on forwardStream

This commit is contained in:
yawnt 2013-06-24 12:01:28 +02:00 committed by cronopio
parent a56ae94894
commit c8d9e0f139
3 changed files with 77 additions and 56 deletions

View File

@ -24,10 +24,10 @@
*/
var util = require('util'),
http = require('http'),
https = require('https'),
events = require('events'),
var util = require('util'),
http = require('http'),
https = require('https'),
events = require('events'),
maxSockets = 100;
//

View File

@ -24,10 +24,10 @@
*/
var events = require('events'),
http = require('http'),
util = require('util'),
url = require('url'),
var events = require('events'),
http = require('http'),
util = require('util'),
url = require('url'),
httpProxy = require('../node-http-proxy');
//
@ -91,10 +91,12 @@ var HttpProxy = exports.HttpProxy = function (options) {
//
// Setup opt-in features
//
this.enable = options.enable || {};
this.enable.xforward = typeof this.enable.xforward === 'boolean'
? this.enable.xforward
: true;
this.enable = options.enable || {};
if(typeof this.enable.xforward !== 'boolean') {
this.enable.xforward = true;
}
//
// Setup additional options for WebSocket proxying. When forcing
@ -140,29 +142,17 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
// * `x-forwarded-port`: Port of the original request.
//
if (this.enable.xforward && req.connection && req.socket) {
if (req.headers['x-forwarded-for']) {
var addressToAppend = "," + req.connection.remoteAddress || req.socket.remoteAddress;
req.headers['x-forwarded-for'] += addressToAppend;
}
else {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.socket.remoteAddress;
}
req.headers['x-forwarded-for'] = (req.headers['x-forwarded-for'] || '') +
(req.headers['x-forwarded-for'] ? ',' : '') +
(req.connection.remoteAddress || socket.remoteAddress);
req.headers['x-forwarded-port'] = (req.headers['x-forwarded-port'] || '') +
(req.headers['x-forwarded-port'] ? ',' : '') +
(req.connection.remotePort || socket.remotePort);
if (req.headers['x-forwarded-port']) {
var portToAppend = "," + req.connection.remotePort || req.socket.remotePort;
req.headers['x-forwarded-port'] += portToAppend;
}
else {
req.headers['x-forwarded-port'] = req.connection.remotePort || req.socket.remotePort;
}
if (req.headers['x-forwarded-proto']) {
var protoToAppend = "," + getProto(req);
req.headers['x-forwarded-proto'] += protoToAppend;
}
else {
req.headers['x-forwarded-proto'] = getProto(req);
}
req.headers['x-forwarded-proto'] = (req.headers['x-forwarded-proto'] || '') +
(req.headers['x-forwarded-proto'] ? ',' : '') +
getProto(req);
}
if (this.timeout) {
@ -469,29 +459,17 @@ HttpProxy.prototype.proxyWebSocketRequest = function (req, socket, upgradeHead,
// * `x-forwarded-port`: Port of the original request.
//
if (this.enable.xforward && req.connection) {
if (req.headers['x-forwarded-for']) {
var addressToAppend = "," + req.connection.remoteAddress || socket.remoteAddress;
req.headers['x-forwarded-for'] += addressToAppend;
}
else {
req.headers['x-forwarded-for'] = req.connection.remoteAddress || socket.remoteAddress;
}
req.headers['x-forwarded-for'] = (req.headers['x-forwarded-for'] || '') +
(req.headers['x-forwarded-for'] ? ',' : '') +
(req.connection.remoteAddress || socket.remoteAddress);
req.headers['x-forwarded-port'] = (req.headers['x-forwarded-port'] || '') +
(req.headers['x-forwarded-port'] ? ',' : '') +
(req.connection.remotePort || socket.remotePort);
if (req.headers['x-forwarded-port']) {
var portToAppend = "," + req.connection.remotePort || socket.remotePort;
req.headers['x-forwarded-port'] += portToAppend;
}
else {
req.headers['x-forwarded-port'] = req.connection.remotePort || socket.remotePort;
}
if (req.headers['x-forwarded-proto']) {
var protoToAppend = "," + (req.connection.pair ? 'wss' : 'ws');
req.headers['x-forwarded-proto'] += protoToAppend;
}
else {
req.headers['x-forwarded-proto'] = req.connection.pair ? 'wss' : 'ws';
}
req.headers['x-forwarded-proto'] = (req.headers['x-forwarded-proto'] || '') +
(req.headers['x-forwarded-proto'] ? ',' : '') +
(req.connection.pair ? 'wss' : 'ws');
}
self.emit('websocket:start', req, socket, head, this.target);

View File

@ -0,0 +1,43 @@
var Writable = require('stream').Writable,
http = require('http'),
https = require('https'),
util = require('util');
var ForwardStream = module.exports = function ForwardStream(options) {
Writable.call(this);
var self = this;
this.once('pipe', function(req) {
self.outgoing = options.https ? https : http;
[
'host',
'hostname',
'port',
'socketPath',
'agent'
].forEach(function(elem) {
outgoing[elem] = target[elem];
});
[
'method',
'path',
'headers'
].forEach(function(elem) {
outgoing[elem] = req[elem];
});
});
}
ForwardStream.prototype._write = function() {
}
util.inherits(ForwardStream, Writable);