actual request ip address instead of 0.0.0.0?

277 views
Skip to first unread message

AJ ONeal

unread,
Feb 6, 2012, 1:15:31 PM2/6/12
to node.js mailing list
Can someone suggest a strategy for determining which IP address a request came through?

I'm not looking for the IP address of the remote.

I want the IP address of the server.

AJ ONeal

Ben Noordhuis

unread,
Feb 6, 2012, 2:04:07 PM2/6/12
to nod...@googlegroups.com

You mean the address the connection came in on a server with multiple addresses?

You can't. It's not a Node limitation, most (all?) operating systems
don't provide that information. Bind to each address separately.

Tim Caswell

unread,
Feb 6, 2012, 2:10:31 PM2/6/12
to nod...@googlegroups.com
I should mention that you can use the same request handler for several
http servers. For example.

var Http = require('http');
var Stack = require('stack');
var Creationix = require('creationix');


local handler = Stack(
Creationix.log(),
Creationix.static("/", __dirname, "index.html),
function (req, res, next) {
// custom middleware
// ...
}
);

Http.createServer(handler).listen(80, "65.34.23.44");
Http.createServer(handler).listen(80, "65.34.23.45");
// ...

I just use "stack" and "creationix" here because they don't integrate
tightly with the http.Server instance like Connect and Express do, but
I'm sure the same thing can be done there too.

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Christopher Jeffrey

unread,
Feb 6, 2012, 2:59:50 PM2/6/12
to nodejs
>     local handler = Stack(

Too much Lua in your blood!

On Feb 6, 1:10 pm, Tim Caswell <t...@creationix.com> wrote:
> I should mention that you can use the same request handler for several
> http servers.  For example.
>
>      var Http = require('http');
>     var Stack = require('stack');
>     var Creationix = require('creationix');
>
>     local handler = Stack(
>       Creationix.log(),
>       Creationix.static("/", __dirname, "index.html),
>       function (req, res, next) {
>         // custom middleware
>         // ...
>       }
>     );
>
>     Http.createServer(handler).listen(80, "65.34.23.44");
>     Http.createServer(handler).listen(80, "65.34.23.45");
>     // ...
>
> I just use "stack" and "creationix" here because they don't integrate
> tightly with the http.Server instance like Connect and Express do, but
> I'm sure the same thing can be done there too.
>
>
>
>
>
>
>
> On Mon, Feb 6, 2012 at 1:04 PM, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

Tim Caswell

unread,
Feb 6, 2012, 3:06:41 PM2/6/12
to nod...@googlegroups.com
Lol! At least I used curly braces for the middleware.

deitch

unread,
Feb 8, 2012, 8:05:43 AM2/8/12
to nodejs
Chances are you have a Web server fronting your requests (Apache/
Nginx), which has the actual IP the request came in on (LOL I just
wrote "the actual request the IP came in on" before I fixed it; get
*that* auto-correct).

The http headers (especially if passed on) will include the hostname,
will they also include the request IP? You could probably configure
your front-end server (Apache or Nginx) to add it as an http header in
the request to the node app server, and then pull it from
request.headers, if it really matters.

The more interesting question might be why you are doing this in the
first place? If this is a classic Web app, you shouldn't need it
inside your app, and might make it brittle. If it is some type of
other networking service, I can see how it might be needed. But why do
you need it, and is there a better solution?

On Feb 6, 9:04 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

dolphin278

unread,
Feb 8, 2012, 10:36:31 AM2/8/12
to nod...@googlegroups.com
Indeed, in case of other web server in front of your application you should look how to pass smth like REMOTE_HOST or REMOTE_ADDR (CGI variables) to your application.

AJ ONeal

unread,
Feb 11, 2012, 2:51:25 AM2/11/12
to nod...@googlegroups.com

No, it's a proxy-ish server that dynamically starts other servers for testing and closes them when the test is finished.

I wanted to tell the client the IP address of the server with the assumption that usually it's the same IP as the proxy, but could be different.

Sent from my Android

Avi Deitcher

unread,
Feb 11, 2012, 2:19:18 PM2/11/12
to nod...@googlegroups.com
Now I am confused. You have a proxy server written in node, so you want to know the IP it came in on?

AJ ONeal

unread,
Feb 14, 2012, 5:18:18 PM2/14/12
to nod...@googlegroups.com
It's really a niche use case.

I'm "proxying" http, tcp, and udp over http behind a firewall.

Basically I'm telling the "proxy" to give me an ip address, port where I can tell another service to send data and a uuid by which I can access the message queue.

I then tell the other service to send its data to that ip address and port.

I then use http get and the uuid to get a list of messages in the queue, download them via http get.

The ip of the "proxy" isn't likely to change, so I could hard code it, but I thought it would be nice to have it know what its address is.

AJ ONeal

deitch

unread,
Feb 15, 2012, 1:27:28 AM2/15/12
to nodejs
So it is like http://www.nocrew.org/software/httptunnel.html or
similar. Actually, I think node with its evented model is an excellent
choice for building and http tunnel. If you can open up the project on
github, would love to see it.

Did you get a clean answer on this? What about checking the port? I
don't see anything in node docs that says it passes that info, but
opening a server socket and then accepting a connection should give
you the port on which it is connected if you use the C API (on which
the Java and all others are built anyways), shouldn't it?

On Feb 15, 12:18 am, AJ ONeal <coola...@gmail.com> wrote:
> It's really a niche use case.
>
> I'm "proxying" http, tcp, and udp over http behind a firewall.
>
> Basically I'm telling the "proxy" to give me an ip address, port where I
> can tell another service to send data and a uuid by which I can access the
> message queue.
>
> I then tell the other service to send its data to that ip address and port.
>
> I then use http get and the uuid to get a list of messages in the queue,
> download them via http get.
>
> The ip of the "proxy" isn't likely to change, so I could hard code it, but
> I thought it would be nice to have it know what its address is.
>
> AJ ONeal
>
>
>
>
>
>
>
> On Sat, Feb 11, 2012 at 12:19 PM, Avi Deitcher <a...@deitcher.net> wrote:
> > Now I am confused. You have a proxy server written in node, so you want to
> > know the IP it came in on?
>

AJ ONeal

unread,
Feb 20, 2012, 2:38:14 PM2/20/12
to nod...@googlegroups.com
Here it is on github:

I haven't gone back to it, but I think I'll just parse the output of `ip addr`, `ifconfig`, or `/proc/net` for the time being. The nginx header looked promising.

I'll keep you posted.

AJ ONeal
Reply all
Reply to author
Forward
0 new messages