get the client ip address

Asked by marc

Hi,

I try to retrieve the client ip address. I was using request.remoteAddress and thought it was working but it returns 127.0.0.1 all the time. What could I use?

Question information

Language:
English Edit question
Status:
Solved
For:
play framework Edit question
Assignee:
No assignee Edit question
Solved by:
marc
Solved:
Last query:
Last reply:
Revision history for this message
Guillaume Bort (guillaume-bort) said :
#1

Do you have an HTTP proxy server set in front of your application ?

On 18 avr. 09, at 20:22, marc <email address hidden>
wrote:

> New question #67851 on play framework:
> https://answers.launchpad.net/play/+question/67851
>
> Hi,
>
> I try to retrieve the client ip address. I was using
> request.remoteAddress and thought it was working but it returns
> 127.0.0.1 all the time. What could I use?
>
> --
> You received this question notification because you are a member of
> play
> framework developers, which is an answer contact for play framework.

Revision history for this message
marc (marc-ttux) said :
#2

I don't think so. That's a dedicated server from dedibox http://www.dedibox.fr/ and I didn't install a proxy on the server.

Revision history for this message
marc (marc-ttux) said :
#3

I get this problem on stable4 so I try to see if I have the same problem with stable5 but I get this 404 problem.

Revision history for this message
marc (marc-ttux) said :
#4

Actually, I modified play.server.HttpRequest to print the ip address of the client when there is a 404 or 500 and that works so the request.remoteAddress doesn't work. At least in stable4, going to try stable5 and let you know.

Revision history for this message
marc (marc-ttux) said :
#5

Ok so it doesn't work with stable5. I try to fix it and then I'll let you know what I've done.

Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#6

Hi,

If you're using apache or another webserver forwarding request to a play instance, from the play application requests comes from 127.0.0.1 (eg the frontal webserver is issuing the requests). There are proxy settings to add in your webserver and in play to emulate direct connections.

 Do you have the same problems on your dev machine, connecting directly to the play instance ?

Revision history for this message
marc (marc-ttux) said :
#7

My dev machine returns 127.0.0.1 and you're right, I am using lighttpd.

I am getting what I want now. I have modified play.server.HttpHandler.java

I replaced:
request.remoteAddress = ((InetSocketAddress) session.getRemoteAddress()).getAddress().getHostAddress();

by:
List<String> remoteAddresses = minaRequest.getHeaders().get("X-Forwarded-For");

if(remoteAddresses!=null && remoteAddresses.size()>0)
   request.remoteAddress = remoteAddresses.get(remoteAddresses.size()-1);

Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#8

Hi,

You don't really need to modify HttpHandler, it already does exactly this http://bazaar.launchpad.net/%7Eplay-developers/play/stable3-hacks/annotate/head%3A/framework/src/play/server/HttpHandler.java#L151

request.remoteAddress = xForwardedSupport && minaRequest.containsHeader("X-Forwarded-For") ? minaRequest.getHeader("X-Forwarded-For") : ((InetSocketAddress) session.getRemoteAddress()).getAddress().getHostAddress();

xForwardedSupport is enabled in your application.conf with
XForwardedSupport=enabled

Using apache+mod proxy, you just need to add a
ProxyPreserveHost on
in your config file.

we'll add theses infos in the manual asap.

Revision history for this message
Jean-Francois POUX (jfp-taldius) said :
#9

Hi,

I've added some doc http://www.playframework.org/manual/contents/production (bottom).

XForwardedSupport=enabled

is changed to
XForwardedSupport=comma separated list of proxy ip addresses

in the latest revision so that play won't mess headers unless requested from a known proxy.

Revision history for this message
Guillaume Bort (guillaume-bort) said :
#10

cool

On Mon, Apr 20, 2009 at 5:58 PM, Jean-Francois POUX
<email address hidden> wrote:
> Question #67851 on play framework changed:
> https://answers.launchpad.net/play/+question/67851
>
> Jean-Francois POUX posted a new comment:
> Hi,
>
> I've added some doc
> http://www.playframework.org/manual/contents/production (bottom).
>
> XForwardedSupport=enabled
>
> is changed to
> XForwardedSupport=comma separated list of proxy ip addresses
>
> in the latest revision so that play won't mess headers unless requested
> from a known proxy.
>
> --
> You received this question notification because you are a member of play
> framework developers, which is an answer contact for play framework.
>