How to detect the source of HTTP request?

Is there a way to detect the source of HTTP request? for ex: domain name of server which tried to access API endpoint at phoenix server, is this possible?

The Plug.Conn.t does have some fields, one of it is :remote_ip, as I read its description it seems to be what you want:

  • remote_ip - the IP of the client, example: {151, 236, 219, 228}. This field is meant to be overwritten by plugs that understand e.g. the X-Forwarded-For header or HAProxy’s PROXY protocol. It defaults to peer’s IP.
1 Like

Just to mention the Plug.Conn source code where You may find Conn structure in the comments.

UPDATE: @NobbZ I was unable to find Conn docs in Phoenix docs, So I linked to source code… Of course I could not find them, they are in fact in the Plug docs. Here is the link https://hexdocs.pm/plug/Plug.Conn.html

1 Like

I’d not rely on things that are only described in the source. :remote_ip is actually documented properly. Grabbing random stuff from sources might result in code that relies on private API and breaks with the next update.

1 Like

I’ve found conn.host :

  • host - the requested host as a binary, example: "www.example.com"

I think this is more than enough for me, very useful.

1 Like

Thats not the source, thats YOU.

ops! bad me…

As I said, :remote_ip is what you are searching for. If you really need a hostname, you’ll have to do a reverse-DNS.

If You want to get more into Conn details, there is this nice blogpost

https://shankardevy.com/code/elixir-phoenix-conn-request-life-cycle/

from Shankardevy, author of Phoenix inside out.

1 Like

Thanks for info