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

Commit

Permalink
Add support for X-Forwarded headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Valls Fernández committed Nov 6, 2017
1 parent e11fe48 commit 13a2e00
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
15 changes: 14 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var r = require('rethinkdb'),
Cb = require('y-callback'),
proxyProtocol = require('./main/proxyProtocol.js'),
bindServer = require('./main/bindServer.js'),
injectXFH = require('./main/injectXFH.js'),
otherSocket = Symbol(),
connection = Symbol(),
walker = Symbol(),
Expand Down Expand Up @@ -201,7 +202,9 @@ function find(backend){

function* proxy(e,d,hosts,server,aliases){
var parts,target,backend,
host,dest;
host,dest,socket;

socket = e.socket;

if(hosts[e.host || '']) target = hosts[e.host || ''];
else{
Expand Down Expand Up @@ -267,6 +270,16 @@ function* proxy(e,d,hosts,server,aliases){
if(!dest.to.stripProxy) dest.socket.write(e.proxyHeader);
if(dest.to.prependHost) dest.socket.write('host: ' + dest.to.prependHost + '\r\n');

if(dest.to.XFH){
try{
yield injectXFH(socket,e.socket,dest.socket);
}catch(err){
dest.socket.destroy();
e.socket.destroy();
return;
}
}

dest.socket.pipe(e.socket).pipe(dest.socket);
dest.socket[otherSocket] = e.socket;
e.socket[otherSocket] = dest.socket;
Expand Down
14 changes: 1 addition & 13 deletions main/bindServer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var Emitter = require('y-emitter'),
walk = require('y-walk'),
Cb = require('y-callback'),
read = require('./read'),
emitter = Symbol(),
hostsMap = Symbol(),
processSocket,read;
Expand Down Expand Up @@ -193,18 +193,6 @@ processSocket = walk.wrap(function*(socket,emitter){

});

read = walk.wrap(function*(socket,n){
var cb,data;

while(true){
data = socket.read(n);
if(data) return data;
socket.once('readable',cb = Cb());
yield cb;
}

});

/*/ exports /*/

module.exports = bindServer;
30 changes: 30 additions & 0 deletions main/injectXFH.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var walk = require('y-walk'),
ip = require('ip'),
read = require('./read');

module.exports = walk.wrap(function*(originalSocket,socketFrom,socketTo){
var sequence = [0x0d, 0x0a, 0x0d, 0x0a],
i,j,b;

loop: while(true){

for(i = 0;i < sequence.length;i++){
b = yield read(socketFrom, 1);

if(b[0] != sequence[i]){
socketTo.write(new Buffer(sequence.slice(0,i)));
socketTo.write(b);
continue loop;
}
}

socketTo.write(new Buffer(sequence.slice(0,2)));
socketTo.write(`X-Forwarded-For: ${ip.toString(ip.toBuffer(originalSocket.remoteAddress))}\r\n`);
if(originalSocket != socketFrom)
socketTo.write('X-Forwarded-Proto: https\r\n');
socketTo.write(new Buffer(sequence.slice(2,4)));
return;

}

});
14 changes: 14 additions & 0 deletions main/read.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var walk = require('y-walk'),
Cb = require('y-callback');

module.exports = walk.wrap(function*(socket,n){
var cb,data;

while(true){
data = socket.read(n);
if(data) return data;
socket.once('readable',cb = Cb());
yield cb;
}

});
1 change: 1 addition & 0 deletions test/main/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ exports.rules = desc => t(desc,function*(){
}
},
to: {
XFH: true,
port: 8001,
host: '127.0.0.1'
}
Expand Down

0 comments on commit 13a2e00

Please sign in to comment.