-
-
Save pcdinh/5677698 to your computer and use it in GitHub Desktop.
This file contains 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
var http = require('http') | |
, request = http.IncomingMessage.prototype; | |
/** | |
* Add a uniform interface for remote address in Node.js | |
* | |
* @api private | |
*/ | |
request.__defineGetter__('remote', function remote () { | |
var connection = this.connection | |
, headers = this.headers | |
, socket = connection.socket; | |
// return early if we are behind a reverse proxy | |
if (headers['x-forwarded-for']) { | |
return { | |
ip: headers['x-forwarded-for'] | |
, port: headers['x-forwarded-port'] | |
} | |
} | |
// regular HTTP servers | |
if (connection.remoteAddress) { | |
return { | |
ip: connection.remoteAddress | |
, port: connection.remotePort | |
}; | |
} | |
// in node 0.4 the remote address for https servers was in a different | |
// location | |
if (socket.remoteAddress) { | |
return { | |
ip: socket.remoteAddress | |
, port: socket.remotePort | |
}; | |
} | |
// last possible location.. | |
return { | |
ip: this.socket.remoteAddress || '0.0.0.0' | |
, port: this.socket.remotePort || 0 | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment