ajvondrak / remote_ip

A plug to rewrite the Plug.Conn's remote_ip based on forwarding headers.
MIT License
252 stars 31 forks source link

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

Closed Gazler closed 5 years ago

Gazler commented 5 years ago

In some circumstances, it is possible to get a subset of headers without the full conn. An example of this is in Phoenix sockets, where only x_headers can be fetched.

https://hexdocs.pm/phoenix/Phoenix.Endpoint.html#socket/3-shared-configuration

In this case, currently using RemoteIP involves doing something like this as conn is not available:

def connect({connect_info: %{x_headers: headers}}) do
  RemoteIp.call(%Plug.Conn{req_headers: headers}, RemoteIp.init()).remote_ip
end

With this PR, someone using this library with Phoenix sockets could do:

  RemoteIp.last_forwarded_ip(headers)
ajvondrak commented 5 years ago

Sorry for the delayed response. It's a great idea, and I appreciate the PR. I'll probably want to massage around the actual implementation, both to appease my delicate sensibilities (I get weird about things 😅) and in light of possibly-changing APIs from #8. I'll try to get to this soon. 👍