Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
agad opened this issue Dec 7, 2011 · 21 comments
Open

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

agad opened this issue Dec 7, 2011 · 21 comments
Labels

Comments

@agad
Copy link

agad commented Dec 7, 2011

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.

@tjanczuk
Copy link
Owner

tjanczuk commented Dec 7, 2011

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?

@agad
Copy link
Author

agad commented Dec 7, 2011

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.

@tjanczuk
Copy link
Owner

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); 

@l5oo00
Copy link

l5oo00 commented Mar 12, 2012

Thank you.

@williamkapke
Copy link

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.

@tjanczuk tjanczuk reopened this Aug 11, 2012
@tjanczuk
Copy link
Owner

I will look into this, reopening.

@crazycactuz
Copy link

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
Copy link

the site is git deployed from OS X if that matters

@crazycactuz
Copy link

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

@JigneshPansuriaDeveloper

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)

@rramachand21-zz
Copy link
Collaborator

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

@JigneshPansuriaDeveloper

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

@tjanczuk
Copy link
Owner

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

@JigneshPansuriaDeveloper

i am using centos...

On Fri, Jan 24, 2014 at 11:07 PM, Tomasz Janczuk
notifications@github.comwrote:

@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//issues/94#issuecomment-33243840
.

@rramachand21-zz
Copy link
Collaborator

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.

@JigneshPansuriaDeveloper

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

On Fri, Jan 24, 2014 at 11:18 PM, Ranjith Ramachandra <
notifications@github.com> 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//issues/94#issuecomment-33244737
.

gravling added a commit to gravling/node-github-hook that referenced this issue Oct 28, 2015
Broke since iisnode is not able to resolve remoteAddress. See tjanczuk/iisnode#94 for more information
@Priyankaagrawal
Copy link

Priyankaagrawal commented Dec 23, 2016

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

@mattgaspar
Copy link

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

@ibrahimozgon
Copy link

enableXFF worked. Thanks

@Harmonickey
Copy link

enableXFF worked

@diegodsp
Copy link

enableXFF worked. Tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests