How to get real ip address from user client when visit my website using rails in development mode

1.1k views Asked by At

i want to get real ip address from user client when visit my website using rails in development mode?

but when i typing request.remote_ip, i get ip address 127.0.0.1??

how to get real ip address???

and when i try curl -H"X-Forwarded-For: 8.8.8.8" http://httpbin.org/ip, i get my real ip address but when i try implements gem https://github.com/geokit/geokit-rails to get country based ip address... my ip it's not valid?

i try this :

IpGeocoder.geocode('my.real.ip.address')

and get error like this :

Geokit::Geocoders::GeocodeError: Geokit::Geocoders::GeocodeError

but if i try ip adrress from example :

IpGeocoder.geocode('12.215.42.19')

it's works

=> Provider: hostip
Street: 
City: Aurora
State: TX
Zip: 
Latitude: 33.0582
Longitude: -97.5159
Country: US
Success: true 

whats wrong in my ip address? i get my real ip address from "curl -H"X-Forwarded-For: 8.8.8.8" http://httpbin.org/ip"

if i wrong plase tell me, thanks

2

There are 2 answers

0
Richard Peck On

Here's an idea which may help:


Localhost

When you load the Rails server in development, you're accessing your localhost. This means that any requests made to this server are going to be treated as local connections (127.0.0.1)

You have to remember Rails is a server-side technology, which means it can only process requests as received (it has no bearing on where those requests come from). You can see a glimpse of this with the ActiveDispatch::Request middleware:

local?() True if the request came from localhost, 127.0.0.1.

This means if you send a request to your local Rails server, it'll just treat it as a local request (127.0.0.1)


GeoLocation IP

Your Geolocation worked for your WAN IP (your remote IP) manually

The reason is because the GeoLocation API / service which the gem uses will ping a third-party server, which will either look up the DNS or other geographic locations for that IP:

From GeoKit GitHub:

IP-based location lookup utilizing hostip.info. Provide an IP address, and get city name and latitude/longitude in return

Like any API, the response will depend on the input. The problem is that because Rails is treating your requests as local (from 12.7.0.0.1), it will only send that IP, which has no bearing on GeoLocation (it's local to your system, not the world)


JS

To get around this, you'll have to find a way to find your system's WAN IP in development, which you may be able to do with JS:

How to get client's IP address using javascript only? (see Chad Grant's answer):

<script type="application/javascript">
    function getip(json){
      alert(json.ip); // alerts the ip address
    }
</script>

<script type="application/javascript" src="http://jsonip.appspot.com/?callback=getip"></script>

I've not tested this, but hopefully it will give you an ideas as to what might be going wrong

0
malandrina On

Try using ngrok to create a tunnel to your local server.