[2.0 beta] Querying the user's IP (i.e., remote address).

1,924 views
Skip to first unread message

Nuanda

unread,
Feb 1, 2012, 5:32:45 PM2/1/12
to play-framework
Hi all,

I feel like I must be smoking the crack pipe, I've searched and can't
find the call to query the remote user's IP in the version 2.0 Scala
API ?

There's plenty of info regarding v1 Java method of access; but
play.api.mvc.Request doesn't seem to have the same behaviour...
(i.e., no remoteAddress field on offer).

Does the remote IP get stashed as a HTTP header or is there a quick
call to retrieve it ?

Many Thanks,

Nuanda

Ben McCann

unread,
Feb 1, 2012, 6:30:45 PM2/1/12
to play-fr...@googlegroups.com
Yeah, you have to do most of that yourself in Play 2 for now.  It's pretty easy to get all the headers and print them out to see if it's there.

-Ben

Nuanda

unread,
Feb 2, 2012, 1:38:00 AM2/2/12
to play-framework
Hi, I checked the available HTTP headers (in request.headers). The
source IP isn't in that set (source IP isn't a HTTP Header field after
all, being an artefact of the HTTP connection itself).

So that's weird, you can't yet lookup the client IP address in
Play2 ? That will make my GeoIP location resolution code fairly
unspectacular !

Nuanda

Guillaume Bort

unread,
Feb 2, 2012, 3:37:26 AM2/2/12
to play-fr...@googlegroups.com
Well anyway when using a reverse proxy the remote IP address will be
the one of the proxy server. So the real remote IP address has to be
copied in the HTTP headers. Most reverse proxy use X-Real-IP.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>

--
Guillaume Bort

Drew Kutcharian

unread,
Feb 2, 2012, 11:04:51 PM2/2/12
to play-fr...@googlegroups.com
Actually it's not only the IP you need to worry about when using a reverse proxy, you need both IP and the original protocol (http or https) since most of the time the proxy will do the SSL decode too. In my experience you usually end up with two header "X-Forwarded-Proto" and "X-Forwarded-For"

http://en.wikipedia.org/wiki/X-Forwarded-For
http://tools.ietf.org/html/draft-petersson-forwarded-for-02

I think it would be a good idea that Play checks for the presence of X-Forwarded-For header and return that instead of the IP since X-Forwarded-For is pretty standard.

Also something I've been thinking about, doesn't using a proxy defeat the purpose of Netty's evented-IO?

-- Drew

Nuanda

unread,
Feb 8, 2012, 1:44:04 AM2/8/12
to play-framework
Ok thanks Guillaume,

For anybody chasing client IPs who finds this thread, I just put Nginx
in front of the Play service to inject the header:

proxy_set_header X-Forwarded-For
$http_x_forwarded_for;

Note: this is because my Nginx is behind an existing Apache reverse
proxy I can't edit, if the first proxy it would have been this in
nginx.conf:

proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;

Then it's easy to query the source IP from Play2:

val clientAddress = request.headers.get("X-Forwarded-
For").getOrElse("127.0.0.1")

-- Nuanda

Daniel Berndt

unread,
Mar 21, 2012, 12:04:46 PM3/21/12
to play-fr...@googlegroups.com
And for all the others who would like to access the IP address from the stand-alone netty server, there are two implementations suggested here:

Nilanjan

unread,
Mar 21, 2012, 5:33:24 PM3/21/12
to play-framework
Could we please get this change in? I am also looking for a way to get
the remote address and some of my clients are pure java script
clients.

Nilanjan

On Mar 21, 10:04 am, Daniel Berndt <daniel.ber...@googlemail.com>
wrote:
> And for all the others who would like to access the IP address from the
> stand-alone netty server, there are two implementations suggested here:https://play.lighthouseapp.com/projects/82401-play-20/tickets/256-add...

Olivier Refalo

unread,
Mar 22, 2012, 5:52:47 PM3/22/12
to play-fr...@googlegroups.com
I am one of those.

Play2 should take care of it natively
Message has been deleted

Guillaume Bort

unread,
Apr 7, 2012, 6:19:23 AM4/7/12
to play-fr...@googlegroups.com
There is a pull request waiting to be integrated:

https://github.com/playframework/Play20/pull/200

Unfortunately it needs a few improvements before to be integrated:
proper support for reverse-proxy and proper security handling.

On Sat, Apr 7, 2012 at 11:25 AM, Irfan Ahmad <Ir...@cloudphysics.com> wrote:
> Hmmm... do the maintainers think this should be done eventually or is the
> verdict to hack it in ourselves?
>
> Irfan

> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/Sh9k1SEsUMAJ.

Jay Q.

unread,
Jun 9, 2012, 7:54:43 AM6/9/12
to play-fr...@googlegroups.com
Is this still the case? I need the remote IP for Google reCAPTCHA:

Guillaume Bort

unread,
Jun 9, 2012, 7:57:19 AM6/9/12
to play-fr...@googlegroups.com
It's available in master.
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/heZlkwJAwfoJ.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.



--
Guillaume Bort, http://guillaume.bort.fr

Will Sargent

unread,
Jun 13, 2012, 4:34:12 PM6/13/12
to play-fr...@googlegroups.com
Just for reference, I'm having the same problem.  I need to pass the remote IP address to recaptcha, and all I have is request.host.

Will.


On Saturday, June 9, 2012 4:57:19 AM UTC-7, Guillaume Bort wrote:
It's available in master.

On Sat, Jun 9, 2012 at 1:54 PM, Jay Q. <jqui...@gmail.com> wrote:
> Is this still the case? I need the remote IP for Google reCAPTCHA:
> https://developers.google.com/recaptcha/docs/verify
>
>
>
> On Thursday, February 2, 2012 6:32:45 AM UTC+8, Nuanda wrote:
>>
>> Hi all,
>>
>> I feel like I must be smoking the crack pipe, I've searched and can't
>> find the call to query the remote user's IP in the version 2.0 Scala
>> API ?
>>
>> There's plenty of info regarding v1 Java method of access; but
>> play.api.mvc.Request doesn't seem to have the same behaviour...
>> (i.e., no remoteAddress field on offer).
>>
>> Does the remote IP get stashed as a HTTP header or is there a quick
>> call to retrieve it ?
>>
>> Many Thanks,
>>
>> Nuanda
>
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/heZlkwJAwfoJ.
>
> To post to this group, send email to play-framework@googlegroups.com.
> To unsubscribe from this group, send email to

Gaëtan Renaudeau

unread,
Jun 18, 2012, 8:21:20 AM6/18/12
to play-fr...@googlegroups.com
2012/6/16 Giacomo <giac...@gmail.com>
When will this be available? I feel like it is a pretty substantial drawback (compared to any other http framework I know).

It is available in play-2.0.2 (currently in RC)

You have access to the remote IP with response.remoteAddress

if you use any http server proxy in front of your play application, also check that you forward the X-Forwarded-For header, then you can use response.remoteAddress transparently.


And having a reverse proxy to overcome this isn't pleasant either.

Thanks!

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/cBzukaYDvpYJ.

To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.



--
Gaëtan Renaudeau, greweb.fr

Giacomo Lo Giusto

unread,
Jun 18, 2012, 1:34:54 PM6/18/12
to play-fr...@googlegroups.com

Thanks Gaetan.
Sorry about the probably obvious question, but do I need to download the latest jar or can I simply change the configuration somewhere(where?) on Sbt to upgrade the play dependency (once it passes the rc stage)?

Reply all
Reply to author
Forward
0 new messages