Ruby app thinks client's IP address is always the same - heroku

Sinatra app.
<%= request.ip %>
gives 127.0.0.1 IP address if looked at from local server.
gives 83.245.226.68 IP address if looked at from Heroku server, no matter from which device.
So I guess it returns server's IP address and not client's. How do I find out what client's IP address is then? Did I misunderstand request.ip method?

This happens when the HTTP Server in front of the Rack HTTP Server (e.g. used for load balancing, ssl, etc) is not forwarding the client IP address to the Rack HTTP Server. In that case you get the IP address of the HTTP Server.
This can be configured on HTTP Servers like Nginx and Apache and you get the client IP address in request.ip.
As far as I know Heroku is doing it in a way that the client IP address is in a header called "x-forwarded-for", which is a comma separated list of IP addresses, the last element is the client ip.
Sinatra:
env['HTTP_X_FORWARDED_FOR'].split(',').last
Rails:
request.headers['x-forwarded-for'].split(',').last

Related

How do I find the IPv4 address on a Google Cloud service?

I have a Go service, deployed on Heroku, which pulls the IPv4 address from the request header successfully.
ip := net.ParseIP(strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0]).String()
I have deployed the identical code as a service to Google Cloud, and the IP addresses are frequently IPv6 in about 25% of the time. After examining the full Request Header, there is no IPv4 address available anywhere, only IPv6.
Heroku's Request Header X-Forwarded-For ALWAYS contains the IPv4 address, yet Google Cloud doesn't. Does anyone know a way to force the IPv4 format for Request Headers in Google Cloud?
Clients can connect via IPv4 or IPv6 but not both. Only one address family will be used by the client and only one IP address will be recorded by the proxy.
Additional information:
Heroku does not support IPv6 so clients are forced to connect using IPv4. reference
If you only want IPv4 connections, do not enable the IPv6 frontends. However, I recommend using IPv6 where possible.

How to access the ip address of a client in java

In my code in want to print the ip address of the client system.My front end and back end codes are in same server.My client systems ip is 192.168.2.3(not actual) and server up is 192.168.38.1(not actual).When i run the services its getting that local host address only (ie localhost).
request.getRemoteAddr();
I am using this code to get the ip.
Why its happening and how can I resolve that?

Bind Ruby TCP Client to a specific IP address

I have a server with 4 ip addresses and four clients that each register their ip address with a service. Each client runs the same ruby script that connects via a TCP socket to the service.
The problem I have is all four scripts are connecting via the same IP address so three are receiving authorisation errors because they have the wrong ip address.
Is it possible to tell the TCP client to use a specific ip address?

Ruby TCPSocket Server - Can I tell to what host a client was connecting?

I have a ruby server based on TCPSocket (non-HTTP).
I have 2 different domains, both pointing with an A-Record to my servers IP Address (the same one). So, there are clients connecting to one of those domains.
Is it possible to tell which domain a client was connecting to?
I saw that this is possible in other protocols, but I'm not sure if this is based on manually added headers or really extracted from the basic tcp/ip connection. E.g. in PHP there is $_SERVER["HTTP_HOST"] which shows to which domain a client was connecting.
At the TCP socket level, the only things that are known are the source and destination IP addresses (and ports) of the connection. How the IP address was resolved via DNS is not possible to know at this layer. Even though HTTP works on top of TCP, HTTP servers have to look at the HTTP headers from the client to know which domain they are making a request to. (That's how the HTTP_HOST value gets filled in.)
One possible solution is to configure your server to have an additional IP address. This can be by assigning an additional IP address to the NIC or adding an additional NIC. Then have each domain use a different IP address. Otherwise, this is not possible and you may want to consider your application protocol on top of TCP to convey this information.

Countering Fuckip IP Anonymity FireFox Addon

http://ipfuck.paulds.fr/
We've been recently getting hammered by this Firefox plug-in. It sends a fake IP in the headers so when our nginx web server picks up the IP it is a fake one.
Is there any way to get a real IP address or block out requests that have this plug-in installed?
There is actually no client IP entries in any HTTP Headers. There are only some un-official proxy headers which are added to a request, so that a proxy server can tell you the real ip of the connecting client (since the tcp socket will only reveal the IP address of the proxy server).
The plugin you linked to adds those proxy headers, to "fake" a proxy request, by adding a X-Real-IP: 1.2.3.4 or X-Forwarded-For: 1.2.3.4 header to the request. But no one forces you to use that IP address (which can be fake, like the 1.2.3.4 example here), you can always use the IP address of the socket that initiated the connection - which will be the client's real IP address if he uses the mentioned plugin.
Within the location section of your nginx configuration, you get the socket IP address through the $remote_addr variable. To retrieve the "fake" IP address, you can use $http_x_forwarded_for or $http_x_real_ip variable.
If you are using any application/cgi backend, you usually can examine the full headers and the socket IP address (i.e. in PHP you should check $_REQUEST and $_HEADERS variables)

Resources