analytics/lib/plausible_web/remote_ip.ex
2020-08-11 11:04:26 +03:00

14 lines
333 B
Elixir

defmodule PlausibleWeb.RemoteIp do
def get(conn) do
forwarded_for = List.first(Plug.Conn.get_req_header(conn, "x-forwarded-for"))
if forwarded_for do
String.split(forwarded_for, ",")
|> Enum.map(&String.trim/1)
|> List.first()
else
to_string(:inet_parse.ntoa(conn.remote_ip))
end
end
end