prop/lib/microwaveprop_web/plugs/remote_ip.ex
Graham McIntire 45fb9736fa
Log client IP from X-Forwarded-For in request logs
Add RemoteIp plug that extracts real client IP from X-Forwarded-For
header (set by nginx/dokku proxy) and adds it to Logger metadata.
2026-03-31 12:05:05 -05:00

48 lines
1 KiB
Elixir

defmodule MicrowavepropWeb.Plugs.RemoteIp do
@moduledoc """
Extracts the client IP from X-Forwarded-For (set by nginx/dokku proxy)
and assigns it to conn.remote_ip and Logger metadata.
"""
@behaviour Plug
@impl true
def init(opts), do: opts
@impl true
def call(conn, _opts) do
ip =
case Plug.Conn.get_req_header(conn, "x-forwarded-for") do
[forwarded | _] ->
forwarded
|> String.split(",")
|> hd()
|> String.trim()
|> parse_ip()
_ ->
nil
end
conn =
if ip do
%{conn | remote_ip: ip}
else
conn
end
Logger.metadata(remote_ip: format_ip(conn.remote_ip))
conn
end
defp parse_ip(str) do
case :inet.parse_address(String.to_charlist(str)) do
{:ok, ip} -> ip
_ -> nil
end
end
defp format_ip({a, b, c, d}), do: "#{a}.#{b}.#{c}.#{d}"
defp format_ip({a, b, c, d, e, f, g, h}), do: "#{a}:#{b}:#{c}:#{d}:#{e}:#{f}:#{g}:#{h}"
defp format_ip(_), do: "unknown"
end