Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose the last_forwarded_ip/2 function for usage outside of Plug #9

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ end

Note that, due to limitations in the [inet_cidr](https://github.com/Cobenian/inet_cidr) library used to parse them, `:proxies` **must** be written in full CIDR notation, even if specifying just a single IP. So instead of `"127.0.0.1"` and `"a:b::c:d"`, you would use `"127.0.0.1/32"` and `"a:b::c:d/128"`.

### Without plug

For usage outside of plug, there is the `RemoteIp.last_forwarded_ip/2` function, which can be used like so:

```elixir
RemoteIp.last_forwarded_ip([{"x-forwarded-for", "1.2.3.4"}]) => {1, 2, 3, 4}
```

The headers can also be customized like so:

```elixir
RemoteIp.last_forwarded_ip([{"x-foo", "1.2.3.4"}],
headers: ~w[x-foo x-bar x-baz],
proxies: ~w[1.2.0.0/16]
) => {1, 2, 3, 4}


## Background

### Problem: Your app is behind a proxy and you want to know the original client's IP address.
Expand Down
23 changes: 14 additions & 9 deletions lib/remote_ip.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
defmodule RemoteIp do
@moduledoc """
A plug to overwrite the `Plug.Conn`'s `remote_ip` based on headers such as
`X-Forwarded-For`.
`X-Forwarded-For`. The `last_forwarded_ip/2` function can be used directly
for usage outside of plug.

To use, add the `RemoteIp` plug to your app's plug pipeline:

Expand Down Expand Up @@ -73,20 +74,24 @@ defmodule RemoteIp do
end

def call(conn, {headers, proxies}) do
case last_forwarded_ip(conn, headers, proxies) do
case last_forwarded_ip(conn.req_headers, headers: headers, proxies: proxies) do
nil -> conn
ip -> %{conn | remote_ip: ip}
end
end

defp last_forwarded_ip(conn, headers, proxies) do
conn
|> ips_from(headers)
|> last_ip_forwarded_through(proxies)
end
@doc """
Given a list of headers, return the last ip address.
"""
def last_forwarded_ip(req_headers, opts \\ []) do
headers =
Keyword.get(opts, :headers, @headers)
|> MapSet.new()
proxies = Keyword.get(opts, :proxies, @proxies) ++ @reserved

defp ips_from(%Plug.Conn{req_headers: headers}, allowed) do
RemoteIp.Headers.parse(headers, allowed)
req_headers
|> RemoteIp.Headers.parse(headers)
|> last_ip_forwarded_through(proxies)
end

defp last_ip_forwarded_through(ips, proxies) do
Expand Down
11 changes: 11 additions & 0 deletions test/remote_ip_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ defmodule RemoteIpTest do
#
@conn %Plug.Conn{remote_ip: :peer}

describe "last_forwarded_ip/2" do
test "using the default options" do
assert RemoteIp.last_forwarded_ip([]) == nil
assert RemoteIp.last_forwarded_ip([{"x-forwarded-for", "1.2.3.4"}]) == {1, 2, 3, 4}
end

test "using custom headers" do
assert RemoteIp.last_forwarded_ip([{"x-custom", "1.2.3.4"}], headers: ["x-custom"]) == {1, 2, 3, 4}
end
end

test "zero hops (i.e., no forwarding headers)" do
assert :peer == @conn |> remote_ip
assert :peer == @conn |> remote_ip(headers: ~w[])
Expand Down