Repository: tallarium/reverse_proxy_plug (elixir)

Issue:
Rewrite add_x_fwd_for_header
- IPV6 separators are `:`, not `.`
- Should use `List.key*` functions in general to be idiomatic

Pull request description:
x-forwarded-for fixes
fixes #214 

Patch:
diff --git a/lib/reverse_proxy_plug.ex b/lib/reverse_proxy_plug.ex
index f978d00..389f39b 100644
--- a/lib/reverse_proxy_plug.ex
+++ b/lib/reverse_proxy_plug.ex
@@ -306,7 +306,7 @@ defmodule ReverseProxyPlug do
   defp add_x_fwd_for_header(headers, conn) do
     {x_fwd_for, headers} = Enum.split_with(headers, fn {k, _v} -> k == "x-forwarded-for" end)
 
-    remote_ip = conn.remote_ip |> Tuple.to_list() |> Enum.join(".")
+    remote_ip = conn.remote_ip |> :inet.ntoa() |> to_string()
 
     x_forwarded_for =
       case x_fwd_for do


Test patch:
diff --git a/test/reverse_proxy_plug_test.exs b/test/reverse_proxy_plug_test.exs
index 6a1c1f3..e448030 100644
--- a/test/reverse_proxy_plug_test.exs
+++ b/test/reverse_proxy_plug_test.exs
@@ -266,6 +266,24 @@ defmodule ReverseProxyPlugTest do
     assert_header(headers, "x-forwarded-for", ["127.0.0.2, 127.0.0.1"])
   end
 
+  test_stream_and_buffer "supports IPv6 addresses for x-forwarded-for header" do
+    %{req_function: req_function, opts: opts, get_responder: get_responder} = test_reuse_opts
+
+    ReverseProxyPlug.HTTPClientMock
+    |> expect(req_function, fn %{headers: headers} = request ->
+      send(self(), {:headers, headers})
+      get_responder.(%{}).(request)
+    end)
+
+    conn(:get, "/")
+    # ::1
+    |> Map.put(:remote_ip, {0, 0, 0, 0, 0, 0, 0, 1})
+    |> ReverseProxyPlug.call(ReverseProxyPlug.init(opts))
+
+    assert_receive {:headers, headers}
+    assert_header(headers, "x-forwarded-for", ["::1"])
+  end
+
   test_stream_and_buffer "removes hop-by-hop headers before forwarding request" do
     %{req_function: req_function, opts: opts, get_responder: get_responder} = test_reuse_opts