Add ability to block certain IP addresses
parent
42ea7fe682
commit
3a1c9e67cd
@ -0,0 +1,19 @@
|
||||
defmodule PlausibleWeb.Firewall do
|
||||
import Plug.Conn
|
||||
|
||||
def init(options) do
|
||||
blocklist = Keyword.fetch!(Application.get_env(:plausible, __MODULE__), :blocklist)
|
||||
|> String.split(",")
|
||||
|> Enum.map(&String.trim/1)
|
||||
|
||||
Keyword.merge(options, blocklist: blocklist)
|
||||
end
|
||||
|
||||
def call(conn, opts) do
|
||||
if PlausibleWeb.RemoteIp.get(conn) in opts[:blocklist] do
|
||||
send_resp(conn, 404, "Not found") |> halt
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,13 @@
|
||||
defmodule PlausibleWeb.RemoteIp do
|
||||
def get(conn) do
|
||||
forwarded_for = List.first(Plug.Conn.get_req_header(conn, "x-forwarded-for"))
|
||||
|
||||
if forwarded_for do
|
||||
String.split(forwarded_for, ",")
|
||||
|> Enum.map(&String.trim/1)
|
||||
|> List.first()
|
||||
else
|
||||
to_string(:inet_parse.ntoa(conn.remote_ip))
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue