Hapi: Question: Getting IP of request

Created on 18 Apr 2013  路  6Comments  路  Source: hapijs/hapi

I've been using hapi and so far it's been an awesome server framework!
One feature I've been trying to find is being able to get the IP of the request made. The purpose in which we'd use this sort of feature is to be able to track some statistical data about usage of the API on a per-IP basis.

I looked through the available documentation pretty thoroughly -- if the feature exists, it's not documented yet (and I understand there may very well be a good reason why you guys haven't documented it yet if it exists).

Simply enough, my question is:
Is there a way to be able to grab the IP address from the request? If so, how?

Thanks!

support

Most helpful comment

@gauravvgat in hapi 8 use the info.remoteAddress and info.remotePort. Both values come from the socket address and port: https://github.com/joyent/node/blob/master/lib/net.js#L555-L579

http://nodejs.org/api/net.html#net_socket_remoteaddress

All 6 comments

request.info.address should have the client IP. Also on info are referrer and received for the referrer header and the time that the request was received. This should be documented shortly, it is a relatively new object.

Thanks for the extremely fast response! I appreciate it.

I tried using request.info.address, but I get a null. I also tried using request.info.remoteAddress. Now the weird issue is that all the ip's logged started with 172.31.x.x. How is it possible?

@gauravvgat in hapi 8 use the info.remoteAddress and info.remotePort. Both values come from the socket address and port: https://github.com/joyent/node/blob/master/lib/net.js#L555-L579

http://nodejs.org/api/net.html#net_socket_remoteaddress

Could this be used somehow to authenticate an API call? I have a particular API call which should be allowed from only one other machine, how secure is it to check this IP address before returning a result? Can it be spoofed somehow?

@mr-e- the IP can be spoofed but you could authenticate using a key in a header and using https.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hovmand picture hovmand  路  3Comments

midknight41 picture midknight41  路  4Comments

mahnunchik picture mahnunchik  路  4Comments

RohovDmytro picture RohovDmytro  路  4Comments

leore picture leore  路  4Comments