clients.md: Add Kyclos
[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 forwarded
14 x-forwarded-for
15 x-client-ip
16 x-real-ip
17 ]
18
19 # https://en.wikipedia.org/wiki/Localhost
20 # https://en.wikipedia.org/wiki/Private_network
21 @reserved ~w[
22 127.0.0.0/8
23 ::1/128
24 fc00::/7
25 10.0.0.0/8
26 172.16.0.0/12
27 192.168.0.0/16
28 ]
29
30 def init(_), do: nil
31
32 def call(conn, _) do
33 config = Pleroma.Config.get(__MODULE__, [])
34
35 if Keyword.get(config, :enabled, false) do
36 RemoteIp.call(conn, remote_ip_opts(config))
37 else
38 conn
39 end
40 end
41
42 defp remote_ip_opts(config) do
43 headers = config |> Keyword.get(:headers, @headers) |> MapSet.new()
44 reserved = Keyword.get(config, :reserved, @reserved)
45
46 proxies =
47 config
48 |> Keyword.get(:proxies, [])
49 |> Enum.concat(reserved)
50 |> Enum.map(&InetCidr.parse/1)
51
52 {headers, proxies}
53 end
54 end