mirror of
https://github.com/plausible/analytics.git
synced 2024-12-25 02:24:55 +03:00
99fe03701e
* Add Ecto.Network dependency * Migration: Add ip block list table * If Cachex errors out, mark the cache as not ready * Add IPRule schema * Seed IPRules * Add Shields context module * Implement IPRuleCache * Start IPRuleCache * Drop blocklisted IPs on ingestion * Cosmetic rename * Add settings sidebar item * Consider IPRuleCache readiness on health checks * Fix typo * Implement IP blocklist live view * Update moduledocs * Extend contextual module tests * Convert IPRules LiveView into LiveComponent * Keep live flashes on the tabs view * Update changelog * Format * Credo * Remove garbage * Update drop reason typespecs * Update typespecs for cache keys * Keep track of who added a rule and when * Test if adding via LV prefills the updated_by tooltip * Update ecto_network dependency * s/updated_by/added_by * s/drop_blocklist_ip/drop_shield_rule_ip * Add docs link * s/Updated/Added
60 lines
1.8 KiB
Elixir
60 lines
1.8 KiB
Elixir
defmodule PlausibleWeb.RemoteIP do
|
|
@moduledoc """
|
|
Implements the strategy of retrieving client's remote IP
|
|
"""
|
|
|
|
def get(conn) do
|
|
x_plausible_ip = List.first(Plug.Conn.get_req_header(conn, "x-plausible-ip"))
|
|
cf_connecting_ip = List.first(Plug.Conn.get_req_header(conn, "cf-connecting-ip"))
|
|
x_forwarded_for = List.first(Plug.Conn.get_req_header(conn, "x-forwarded-for"))
|
|
b_forwarded_for = List.first(Plug.Conn.get_req_header(conn, "b-forwarded-for"))
|
|
forwarded = List.first(Plug.Conn.get_req_header(conn, "forwarded"))
|
|
|
|
cond do
|
|
x_plausible_ip ->
|
|
clean_ip(x_plausible_ip)
|
|
|
|
cf_connecting_ip ->
|
|
clean_ip(cf_connecting_ip)
|
|
|
|
b_forwarded_for ->
|
|
parse_forwarded_for(b_forwarded_for)
|
|
|
|
x_forwarded_for ->
|
|
parse_forwarded_for(x_forwarded_for)
|
|
|
|
forwarded ->
|
|
Regex.named_captures(~r/for=(?<for>[^;,]+).*$/, forwarded)
|
|
|> Map.get("for")
|
|
# IPv6 addresses are enclosed in quote marks and square brackets: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Forwarded
|
|
|> String.trim("\"")
|
|
|> clean_ip()
|
|
|
|
true ->
|
|
to_string(:inet_parse.ntoa(conn.remote_ip))
|
|
end
|
|
end
|
|
|
|
# Removes port from both IPv4 and IPv6 addresses. From https://regexr.com/3hpvt
|
|
# Removes surrounding [] of an IPv6 address
|
|
@port_regex ~r/((\.\d+)|(\]))(?<port>:[0-9]+)$/
|
|
defp clean_ip(ip_and_port) do
|
|
ip =
|
|
case Regex.named_captures(@port_regex, ip_and_port) do
|
|
%{"port" => port} -> String.trim_trailing(ip_and_port, port)
|
|
_ -> ip_and_port
|
|
end
|
|
|
|
ip
|
|
|> String.trim_leading("[")
|
|
|> String.trim_trailing("]")
|
|
end
|
|
|
|
defp parse_forwarded_for(header) do
|
|
String.split(header, ",")
|
|
|> Enum.map(&String.trim/1)
|
|
|> List.first()
|
|
|> clean_ip()
|
|
end
|
|
end
|