1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Plugs.RemoteIp do
7 This is a shim to call [`RemoteIp`](https://git.pleroma.social/pleroma/remote_ip) but with runtime configuration.
16 def call(%{remote_ip: original_remote_ip} = conn, _) do
17 if Config.get([__MODULE__, :enabled]) do
18 {headers, proxies} = remote_ip_opts()
19 new_remote_ip = RemoteIp.from(conn.req_headers, headers: headers, proxies: proxies)
21 if new_remote_ip != original_remote_ip do
22 Map.put(conn, :remote_ip, new_remote_ip)
31 defp remote_ip_opts do
32 headers = Config.get([__MODULE__, :headers], [])
33 reserved = Config.get([__MODULE__, :reserved], [])
36 Config.get([__MODULE__, :proxies], [])
37 |> Enum.concat(reserved)
38 |> Enum.map(&maybe_add_cidr/1)
43 defp maybe_add_cidr(proxy) when is_binary(proxy) do
45 "/" in String.codepoints(proxy) -> proxy
46 InetCidr.v4?(InetCidr.parse_address!(proxy)) -> proxy <> "/32"
47 InetCidr.v6?(InetCidr.parse_address!(proxy)) -> proxy <> "/128"