Read Cf-Connecting-Ip header for real client IP through Cloudflare tunnel
This commit is contained in:
parent
b6a5e709b7
commit
9485df4b8d
1 changed files with 17 additions and 10 deletions
|
|
@ -10,19 +10,26 @@ defmodule MicrowavepropWeb.Plugs.RemoteIp do
|
|||
def init(opts), do: opts
|
||||
|
||||
@impl true
|
||||
# Cloudflare sets Cf-Connecting-Ip with the real client IP.
|
||||
# Fall back to X-Forwarded-For for non-tunneled requests (dev, direct).
|
||||
@ip_headers ["cf-connecting-ip", "x-forwarded-for"]
|
||||
|
||||
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()
|
||||
@ip_headers
|
||||
|> Enum.find_value(fn header ->
|
||||
case Plug.Conn.get_req_header(conn, header) do
|
||||
[value | _] ->
|
||||
value
|
||||
|> String.split(",")
|
||||
|> hd()
|
||||
|> String.trim()
|
||||
|> parse_ip()
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end)
|
||||
|
||||
conn =
|
||||
if ip do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue