Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between remote_ip and peer?

I'm writing a plug which put the access log into a file, like mediocre web server. As the data source of client's IP address, conn has remote_ip and peer. Which should I use, and what's the difference between them?

Is there any documents which describe about the each entities in conn?

Also, my plug is like the following snippets, is it natural from Elixir/Phoenix point of view?

Logger.info(
  Enum.join([
    "type:" <> "request",
    "remoteip:" <> Enum.join(Tuple.to_list(conn.remote_ip), ","),
    "method:" <> conn.method,
    "path:" <> conn.request_path,
    "status:" <> to_string(conn.status),
    "size_res:" <> to_string(byte_size(to_string(conn.resp_body))),
  ], ",")
)
like image 695
hykw Avatar asked Mar 14 '23 17:03

hykw


1 Answers

From the Plug.Conn docs:

peer - the actual TCP peer that connected, example: {{127, 0, 0, 1}, 12345}. Often this is not the actual IP and port of the client, but rather of a load-balancer or request-router.

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.

If you are using a load balancer (such as http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/x-forwarded-headers.html) then the peer ip address will refer to the load balancer and not the client. Load balancers provide the actual IP address of the client in a header (X-Forwarded-For for AWS) that will be stored in the Plug.Conn struct as remote_ip.

See also https://www.rfc-editor.org/rfc/rfc7239

like image 151
Gazler Avatar answered May 13 '23 03:05

Gazler