spc-pleroma/lib/pleroma/plugs/remote_ip.ex

49 lines
1.3 KiB
Elixir
Raw Normal View History

2019-09-27 21:59:23 +00:00
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
2019-09-27 21:59:23 +00:00
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Plugs.RemoteIp do
@moduledoc """
This is a shim to call [`RemoteIp`](https://git.pleroma.social/pleroma/remote_ip) but with runtime configuration.
"""
alias Pleroma.Config
import Plug.Conn
2019-09-27 21:59:23 +00:00
@behaviour Plug
def init(_), do: nil
def call(%{remote_ip: original_remote_ip} = conn, _) do
if Config.get([__MODULE__, :enabled]) do
%{remote_ip: new_remote_ip} = conn = RemoteIp.call(conn, remote_ip_opts())
assign(conn, :remote_ip_found, original_remote_ip != new_remote_ip)
2019-09-27 21:59:23 +00:00
else
conn
end
end
defp remote_ip_opts() do
headers = Config.get([__MODULE__, :headers], []) |> MapSet.new()
reserved = Config.get([__MODULE__, :reserved], [])
2019-09-27 21:59:23 +00:00
proxies =
Config.get([__MODULE__, :proxies], [])
2019-09-27 21:59:23 +00:00
|> Enum.concat(reserved)
|> Enum.map(&maybe_add_cidr/1)
2019-09-27 21:59:23 +00:00
{headers, proxies}
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)
end
2019-09-27 21:59:23 +00:00
end