Skip to content

Commit

Permalink
Merge pull request #13 from c-rack/format
Browse files Browse the repository at this point in the history
Problem: source is not formatted properly
  • Loading branch information
c-rack authored Aug 17, 2018
2 parents 73f57c0 + 1ec56cb commit 2bcc60b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: [".formatter.exs", "mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
6 changes: 2 additions & 4 deletions lib/mix/tasks/update_ips.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Mix.Tasks.Update.Ips do

@moduledoc false

use Mix.Task
Expand All @@ -13,12 +12,11 @@ defmodule Mix.Tasks.Update.Ips do

def run(_) do
System.cmd("rm", ["ips-v4", "ips-v6"])
for url <- @urls, do: fetch url
for url <- @urls, do: fetch(url)
end

defp fetch(url) do
IO.puts "Fetching #{url}"
IO.puts("Fetching #{url}")
System.cmd("wget", ["--quiet", url])
end

end
15 changes: 7 additions & 8 deletions lib/plug_cloudflare.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule Plug.CloudFlare do

@moduledoc """
Parses CloudFlare's CF-Connecting-IP header into Plug.Conn's remote_ip field.
"""
Expand All @@ -11,31 +10,31 @@ defmodule Plug.CloudFlare do

@doc "Callback implementation for Plug.call/2"
def call(conn, _options) do
if (conn.remote_ip |> is_from_cloudflare) do
if conn.remote_ip |> is_from_cloudflare do
conn |> Conn.get_req_header("cf-connecting-ip") |> parse(conn)
else
conn
end
end

defp parse([], conn), do: conn

defp parse([ip_address], conn) do
case (ip_address |> String.to_charlist |> :inet.parse_address) do
case ip_address |> String.to_charlist() |> :inet.parse_address() do
{:ok, remote_ip} -> %Conn{conn | remote_ip: remote_ip}
{:error, _} -> conn
{:error, _} -> conn
end
end

cidrs =
[__DIR__, "../ips-v4"]
|> Path.join
|> Path.join()
|> File.stream!([], :line)
|> Enum.to_list
|> Enum.to_list()
|> Enum.map(&CIDR.parse/1)
|> Enum.map(&Macro.escape/1)

defp is_from_cloudflare(ip), do: is_from_cloudflare(unquote(cidrs), ip)
defp is_from_cloudflare([h|t], ip), do: CIDR.match!(h, ip) or is_from_cloudflare(t, ip)
defp is_from_cloudflare([h | t], ip), do: CIDR.match!(h, ip) or is_from_cloudflare(t, ip)
defp is_from_cloudflare([], _ip), do: false

end
9 changes: 4 additions & 5 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ defmodule Plug.Cloudflare.Mixfile do
def project do
[
app: :plug_cloudflare,
build_embedded: Mix.env == :prod,
build_embedded: Mix.env() == :prod,
deps: [
{:cidr, ">= 1.0.0"},
{:cidr, ">= 1.0.0"},
{:credo, ">= 0.3.5", only: [:dev, :test]},
{:plug, ">= 1.1.2"}
{:plug, ">= 1.1.2"}
],
description: """
Convert CloudFlare's CF-Connecting-IP header to Plug.Conn's remote_ip field.
Expand All @@ -29,13 +29,12 @@ defmodule Plug.Cloudflare.Mixfile do
"GitHub" => "https://github.com/c-rack/plug_cloudflare"
}
},
start_permanent: Mix.env == :prod,
start_permanent: Mix.env() == :prod,
version: "1.3.0"
]
end

def application do
[applications: []]
end

end
12 changes: 5 additions & 7 deletions test/plug_cloudflare_test.exs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
defmodule TestRouter do
use Plug.Router

plug :match
plug :dispatch
plug Plug.CloudFlare
plug(:match)
plug(:dispatch)
plug(Plug.CloudFlare)

get "/" do
send_resp(conn, 200, "test")
end

match _, do: send_resp(conn, 404, "not found")

match(_, do: send_resp(conn, 404, "not found"))
end

defmodule Plug.CloudFlareTest do
Expand Down Expand Up @@ -70,7 +69,7 @@ defmodule Plug.CloudFlareTest do
assert conn.status == 200
assert conn.resp_body == "test"
assert conn.remote_ip == {192, 168, 1, 1}
#103.21.244.0
# 103.21.244.0
end

test "should not skip if from CloudFlare IP" do
Expand All @@ -82,5 +81,4 @@ defmodule Plug.CloudFlareTest do
assert conn.resp_body == "test"
assert conn.remote_ip == {192, 168, 1, 1}
end

end

0 comments on commit 2bcc60b

Please sign in to comment.