iisnode icon indicating copy to clipboard operation
iisnode copied to clipboard

Unable to retrieve a connection's remote IP address under IISNode

Open agad opened this issue 14 years ago • 21 comments

I can not seem to retrieve the remote IPAddress of a request using node under IISNode. If I try req.connection.remoteAddress I get undefined. Same script run without IIS provides the remote address without a problem.

agad avatar Dec 07 '11 02:12 agad

The reason for this behavior is that iisnode acts as a reverse proxy between the client and the node process. The req.connection represents the connection between iisnode and node.exe, not between the client's machine and iisnode. Furthermore, the connection between iisnode and node.exe is based on named pipes not TCP, and as such does not expose many of the concepts specific to TCP like the IP address.

It would be possible to propagate selected properties describing the client connection using X-* headers of the HTTP request message. Would such mechanism satisfy your requirement?

tjanczuk avatar Dec 07 '11 04:12 tjanczuk

Aha, that makes complete sense now. Yes, I believe that a set of X-Headers would be helpful and could follow the standard type of X-headers from proxies such as X-Forwarded-For to cover the IP address info as well as X-Forwarded-Host to cover the original host the client requested.

agad avatar Dec 07 '11 05:12 agad

The fix for this issue is to add support for X-Forwarded-For request header to iisnode (http://en.wikipedia.org/wiki/X-Forwarded-For).

A new configuration property iisnode@enableXFF controls whether iisnode adds or augments the X-Forwarded-For request header. By default the value is false. If set to true, iisnode will either append the IP address and port number of the next hop client to an X-Forwarded-For request header already existing on the HTTP request, or create a new X-Forwarded-For header in its absence. Both IPv4 and IPv6 addresses are supported. Each address will specify a port number as well.

This is how to enable X-Forwarded-For support through web.config:

<configuration>
  <system.webServer>

    <!-- ... -->

    <iisnode enableXFF="true" />

  </system.webServer>
</configuration>

This is an example of the X-Forwarded-For HTTP request header iisnode will add to the request:

X-Forwarded-For: 127.0.0.1:28736

This is the code node.js application can use to read the value of the header:

var http = require('http');

http.createServer(function (req, res) {
    var xff = req.headers['x-forwarded-for'];
    // ...
}).listen(process.env.PORT); 

tjanczuk avatar Jan 10 '12 19:01 tjanczuk

Thank you.

l5oo00 avatar Mar 12 '12 04:03 l5oo00

Can you put the port under X-Forwarded-Port to be inline with other implementations?

Adding it to the Ip requires code forks for IISNode.

williamkapke avatar Aug 11 '12 07:08 williamkapke

I will look into this, reopening.

tjanczuk avatar Aug 11 '12 08:08 tjanczuk

Does this work in azure websites? I read somewhere that azure websites implement some kind of subset of iisnode. I have created a iisnode.yml file with enableXFF: true as only content, restarted the site like 5 time and still it wont show me the IPs..

crazycactuz avatar Aug 24 '13 13:08 crazycactuz

the site is git deployed from OS X if that matters

crazycactuz avatar Aug 24 '13 13:08 crazycactuz

My problem was that i used "X-Forwarded-For", not "x-forwarded-for".. works now ;P

crazycactuz avatar Aug 24 '13 14:08 crazycactuz

while i am executing req.connection.socket.remoteAddress

i am getting error below ..

TypeError: Cannot read property 'remoteAddress' of undefined at module.exports.home (/var/www/html/tablemaster/api/controllers/IndexController.js:36:31) at wrapperFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:198:5) at _bind.enhancedFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:375:4) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at pass (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:145:5) at nextRoute (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:100:7) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:167:11) at module.exports (/var/www/html/tablemaster/api/policies/flash.js:10:5) at wrapperFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:198:5) at _bind.enhancedFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:375:4) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at pass (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:145:5) at nextRoute (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:100:7) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:167:11) at /var/www/html/tablemaster/node_modules/sails/lib/hooks/i18n/index.js:27:13 at Object.i18nInit as init at routes.before./* (/var/www/html/tablemaster/node_modules/sails/lib/hooks/i18n/index.js:25:16) at _bind.enhancedFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:375:4) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at pass (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:145:5) at nextRoute (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:100:7) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:167:11) at routes.before./* (/var/www/html/tablemaster/node_modules/sails/lib/hooks/csrf/index.js:35:6) at _bind.enhancedFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:375:4) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at pass (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:145:5) at nextRoute (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:100:7) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:167:11) at _addResViewMethod (/var/www/html/tablemaster/node_modules/sails/lib/hooks/views/index.js:337:3) at _bind.enhancedFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:375:4) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at pass (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:145:5) at nextRoute (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:100:7) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:167:11) at addSugarMethods (/var/www/html/tablemaster/node_modules/sails/lib/hooks/request/index.js:68:6) at _bind.enhancedFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:375:4) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at pass (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:145:5) at nextRoute (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:100:7) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:167:11) at /var/www/html/tablemaster/node_modules/sails/node_modules/connect-flash/lib/flash.js:21:5 at supportFlashMsgs (/var/www/html/tablemaster/node_modules/sails/lib/hooks/request/index.js:45:32) at _bind.enhancedFn (/var/www/html/tablemaster/node_modules/sails/lib/router/bind.js:375:4) at callbacks (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:164:37) at param (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:138:11) at pass (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:145:5) at Router._dispatch (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:173:5) at Object.router (/var/www/html/tablemaster/node_modules/sails/node_modules/express/lib/router/index.js:33:10) at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15) at Object.handle (/var/www/html/tablemaster/node_modules/sails/lib/express/index.js:168:4) at Context.next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15) at Context.actions.pass (/var/www/html/tablemaster/node_modules/passport/lib/passport/context/http/actions.js:77:8) at SessionStrategy.authenticate (/var/www/html/tablemaster/node_modules/passport/lib/passport/strategies/session.js:67:10) at attempt (/var/www/html/tablemaster/node_modules/passport/lib/passport/middleware/authenticate.js:243:16) at Passport.authenticate (/var/www/html/tablemaster/node_modules/passport/lib/passport/middleware/authenticate.js:244:7) at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15) at Passport.initialize (/var/www/html/tablemaster/node_modules/passport/lib/passport/middleware/initialize.js:70:5) at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15) at Object.methodOverride as handle at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15) at /var/www/html/tablemaster/node_modules/sails/lib/express/bodyParserRetry.js:46:4 at multipart (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/multipart.js:85:37) at /var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9 at urlencoded (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:49:37) at /var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:55:7 at json (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/json.js:43:37) at bodyParser (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5) at Object.retryBodyParser as handle at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15) at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:192:9) at multipart (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/multipart.js:85:37) at /var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:57:9 at urlencoded (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:49:37) at /var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:55:7 at json (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/json.js:43:37) at bodyParser (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:53:5) at Object.handle (/var/www/html/tablemaster/node_modules/sails/lib/express/index.js:118:12) at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/proto.js:190:15) at next (/var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/session.js:312:9) at /var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/session.js:329:9 at /var/www/html/tablemaster/node_modules/sails/node_modules/express/node_modules/connect/lib/middleware/session/memory.js:55:7 at process._tickDomainCallback (node.js:459:13)

use "x-forwarded-for" as mentioned above.

rramachand21-zz avatar Jan 24 '14 17:01 rramachand21-zz

i want remote ip address like 220.226.23.254 but when i am executing x-forwarded-for i am getting local ip.. i dont want local ip

@jigneshnavsoft are you running on Windows using iisnode? The stack trace has file paths suggesting a non-Windows environment.

tjanczuk avatar Jan 24 '14 17:01 tjanczuk

i am using centos...

On Fri, Jan 24, 2014 at 11:07 PM, Tomasz Janczuk [email protected]:

@jigneshnavsoft https://github.com/jigneshnavsoft are you running on Windows using iisnode? The stack trace has file paths suggesting a non-Windows environment.

— Reply to this email directly or view it on GitHubhttps://github.com/tjanczuk/iisnode/issues/94#issuecomment-33243840 .

if you are directly running node.exe without iisnode (which you must be because you are on Linux/unix env) -- req.connection.socket.remoteAddress only works for HTTPS. try req.connection.remoteAddress OR req.socket.remoteAddress for HTTP connections.

rramachand21-zz avatar Jan 24 '14 17:01 rramachand21-zz

i try also that .. is there any special library needed for that ???

On Fri, Jan 24, 2014 at 11:18 PM, Ranjith Ramachandra < [email protected]> wrote:

if you are directly running node.exe without iisnode (which you must be because you are on Linux/unix env) -- req.connection.socket.remoteAddress only works for HTTPS. try req.connection.remoteAddress OR req.socket.remoteAddress for HTTP connections.

— Reply to this email directly or view it on GitHubhttps://github.com/tjanczuk/iisnode/issues/94#issuecomment-33244737 .

I have try this in web.config

<system.webServer>

<!-- ... -->

<iisnode enableXFF="true" />

</system.webServer>

In node.js, I m trying get the ip using below code

var xff = req.headers['x-forwarded-for'];

xff is showing "undefined". I m using IIS version is 8.5 on windows 2012 r2

Priyankaagrawal avatar Dec 23 '16 05:12 Priyankaagrawal

I vote for having enableXFF true as the default!! Any reason it isn't?

mattgaspar avatar Apr 07 '18 01:04 mattgaspar

enableXFF worked. Thanks

ibrahimozgon avatar Apr 13 '18 14:04 ibrahimozgon

enableXFF worked

Harmonickey avatar Aug 12 '19 22:08 Harmonickey

enableXFF worked. Tks

diegodsp avatar Nov 27 '19 21:11 diegodsp