Merge branch 'docs/kyclos' into 'develop'
[akkoma] / lib / pleroma / plugs / remote_ip.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Plugs.RemoteIp do
6 @moduledoc """
7 This is a shim to call [`RemoteIp`](https://git.pleroma.social/pleroma/remote_ip) but with runtime configuration.
8 """
9
10 @behaviour Plug
11
12 @headers ~w[
13 x-forwarded-for
14 ]
15
16 # https://en.wikipedia.org/wiki/Localhost
17 # https://en.wikipedia.org/wiki/Private_network
18 @reserved ~w[
19 127.0.0.0/8
20 ::1/128
21 fc00::/7
22 10.0.0.0/8
23 172.16.0.0/12
24 192.168.0.0/16
25 ]
26
27 def init(_), do: nil
28
29 def call(conn, _) do
30 config = Pleroma.Config.get(__MODULE__, [])
31
32 if Keyword.get(config, :enabled, false) do
33 RemoteIp.call(conn, remote_ip_opts(config))
34 else
35 conn
36 end
37 end
38
39 defp remote_ip_opts(config) do
40 headers = config |> Keyword.get(:headers, @headers) |> MapSet.new()
41 reserved = Keyword.get(config, :reserved, @reserved)
42
43 proxies =
44 config
45 |> Keyword.get(:proxies, [])
46 |> Enum.concat(reserved)
47 |> Enum.map(&InetCidr.parse/1)
48
49 {headers, proxies}
50 end
51 end