Created
November 8, 2012 08:51
-
-
Save Suor/4037635 to your computer and use it in GitHub Desktop.
A connect middleware, broken yet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function (ip) { | |
ip = ip || '127.0.0.1'; | |
// Function to test whether or not the request is coming through the trusted reverse proxy. | |
var trust = function (req) { | |
return req.connection.remoteAddress === ip; | |
}; | |
// Function to fetch the remote address from the browser that connected to the reverse proxy. | |
var remoteAddress = function (req) { | |
return req.headers['x-real-ip'] || req.headers['x-forwarded-for']; | |
}; | |
// Check if the request is proxied by a trusted reverse proxy. | |
// And set the client's remote address to the remote address of the browser. | |
return function(req, res, next) { | |
console.log('tp', trust(req)); | |
if (trust(req)) { | |
console.log('set ip', remoteAddress(req)); | |
req.connection.remoteAddress = remoteAddress(req); | |
console.log('after set ip', req.connection.remoteAddress); | |
// console.log('connection type', req.connection); | |
} | |
next(); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment