Move Plugs.RemoteIP.maybe_add_cidr/1 to InetHelper.parse_cidr/1

This commit is contained in:
Haelwenn (lanodan) Monnier 2023-12-16 18:22:32 +01:00
parent 06c58bce00
commit f271ea6e43
2 changed files with 13 additions and 12 deletions

View File

@ -16,4 +16,15 @@ def parse_address(ip) when is_binary(ip) do
def parse_address(ip) do def parse_address(ip) do
:inet.parse_address(ip) :inet.parse_address(ip)
end end
def parse_cidr(proxy) when is_binary(proxy) do
proxy =
cond do
"/" in String.codepoints(proxy) -> proxy
InetCidr.v4?(InetCidr.parse_address!(proxy)) -> proxy <> "/32"
InetCidr.v6?(InetCidr.parse_address!(proxy)) -> proxy <> "/128"
end
InetCidr.parse(proxy, true)
end
end end

View File

@ -8,6 +8,7 @@ defmodule Pleroma.Web.Plugs.RemoteIp do
""" """
alias Pleroma.Config alias Pleroma.Config
alias Pleroma.Helpers.InetHelper
import Plug.Conn import Plug.Conn
@behaviour Plug @behaviour Plug
@ -30,19 +31,8 @@ defp remote_ip_opts do
proxies = proxies =
Config.get([__MODULE__, :proxies], []) Config.get([__MODULE__, :proxies], [])
|> Enum.concat(reserved) |> Enum.concat(reserved)
|> Enum.map(&maybe_add_cidr/1) |> Enum.map(&InetHelper.parse_cidr/1)
{headers, proxies} {headers, proxies}
end end
defp maybe_add_cidr(proxy) when is_binary(proxy) do
proxy =
cond do
"/" in String.codepoints(proxy) -> proxy
InetCidr.v4?(InetCidr.parse_address!(proxy)) -> proxy <> "/32"
InetCidr.v6?(InetCidr.parse_address!(proxy)) -> proxy <> "/128"
end
InetCidr.parse(proxy, true)
end
end end