0

I am building a site in ruby on rails on a heroku server to deliver content to a user based on their location. I am using ipinfo_io to pull a user's ip and location via request.env['ipinfo'].ip & request.env['ipinfo'].loc I am then geocoding the request.env['ipinfo'].loc via the ruby geocoder gem. I am using google api to geocode location.

This is all working fine. Where I am having an issue is when I pull the ip address. Viewing the logs and testing on my home devices, the GET request is done on a different IP address than what my home device is.

My home device ip will be 136.xx.xx.xxx and the GET request ip will be 198.xx.xx.xxx which is no where near where I currently am. How do I set up my environment to pull in the correct IP?

I have put the logs below with my correct ip and the GET ip:

info method=GET path="/favicon.ico" host=www.example.com fwd="136.xx.xx.xxx,172.xx.xx.xx" dyno=web.1 connect=1ms service=8ms status=200 bytes=143 protocol=https
INFO -- : Started GET "/" for 198.xx.xx.xx at 2021-04-07 17:49:01 +0000

Rails version 6.0.3.4 ruby version 2.6.3

Splohr
  • 67
  • 9
  • It is unclear what you are asking or what you mean by "My home device ip will be 136.xx.xx.xxx and the GET request ip will be 198.xx.xx.xxx". Are you talking about your intranet IP (network) vs internet IP (public)? Are you using a VPN which would cause this issue? How are you "connecting" to the application? Is the application hosted? We would need more details to understand what you are actually experiencing and how you expect to resolve this. – engineersmnky Apr 07 '21 at 18:45
  • 2
    For accurate geolocation you should use the HTML location API – max pleaner Apr 07 '21 at 18:50
  • Why do you think your home IP should be in the 136.x.x.x range? How did you determine that? Why do you think 109.x.x.x is nowhere near you? I would guess that 136.x.x.x might be your external IP address which was assigned from your ISP to your router. But you are very likely use a different IP range internally in your local network which is assigned from your router to the devices. [198.x.x.x](https://en.wikipedia.org/wiki/Reserved_IP_addresses#IPv4) is a reserved IP range for private networks that is very often used by routers for this purpose. – spickermann Apr 07 '21 at 19:26
  • @spickermann 192.168.0.0/16 is reserved. 198.x.x.x is not private address space. – tadman Apr 07 '21 at 19:58
  • @tadman The Wikipedia article tells that parts of 198.x.x.x are reserved for private networks too. – spickermann Apr 08 '21 at 05:52
  • @spickermann That's 198.18.0.0/15 and 198.51.0.0/24 specifically, not all of 198.0.0.0/8. They're also for "testing", which normally negates their use as private networks. 198.0.0.0/16 is actually Comcast. It's real IP space. The 198.18 chunk is missing from their allocation. – tadman Apr 08 '21 at 22:09
  • @engineersmnky appreciate the comment. I found my home device ip by looking it up on google and I am not using a VPN. This is my personal website that I'm able to hit from the public internet so I am looking at the logs when I hit it to try and match up my home device ip and the ip in the logs. Which the ipinfo helpers are still pulling the wrong ip. – Splohr Apr 13 '21 at 06:05

1 Answers1

1

Have you tried with request.remote_ip ?

https://apidock.com/rails/ActionDispatch/Request/remote_ip

Ernesto L.
  • 23
  • 5
  • This should do the trick. The ip being seen is an IP of the proxy or load balancer of Heroku. remote_ip should look at the request headers and get the forwarded ip of the client. – D-Rock Apr 07 '21 at 22:37
  • Thanks for the suggestion. I have tried this and even this helper isn't giving me the origin IP I need. Based on this [article](https://stackoverflow.com/questions/19317255/rails-how-to-obtain-visitors-ip-address) I found that I can use `request.env['HTTP_X_FORWARDED_FOR'] ` to get the correct origin IP but now I'm not sure how to pass that to IPInfo to get the location or if the ipinfo-rails gem already has something that can pull that for me. Will provide the answer once I find it. – Splohr Apr 13 '21 at 06:31