2019-09-27 21:59:23 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-02 05:08:45 +00:00
|
|
|
# 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.
|
|
|
|
"""
|
|
|
|
|
2020-03-13 18:15:42 +00:00
|
|
|
import Plug.Conn
|
|
|
|
|
2019-09-27 21:59:23 +00:00
|
|
|
@behaviour Plug
|
|
|
|
|
|
|
|
@headers ~w[
|
|
|
|
x-forwarded-for
|
|
|
|
]
|
|
|
|
|
|
|
|
# https://en.wikipedia.org/wiki/Localhost
|
|
|
|
# https://en.wikipedia.org/wiki/Private_network
|
|
|
|
@reserved ~w[
|
|
|
|
127.0.0.0/8
|
|
|
|
::1/128
|
|
|
|
fc00::/7
|
|
|
|
10.0.0.0/8
|
|
|
|
172.16.0.0/12
|
|
|
|
192.168.0.0/16
|
|
|
|
]
|
|
|
|
|
|
|
|
def init(_), do: nil
|
|
|
|
|
2020-03-13 18:15:42 +00:00
|
|
|
def call(%{remote_ip: original_remote_ip} = conn, _) do
|
2019-09-27 21:59:23 +00:00
|
|
|
config = Pleroma.Config.get(__MODULE__, [])
|
|
|
|
|
|
|
|
if Keyword.get(config, :enabled, false) do
|
2020-03-13 18:15:42 +00:00
|
|
|
%{remote_ip: new_remote_ip} = conn = RemoteIp.call(conn, remote_ip_opts(config))
|
|
|
|
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(config) do
|
|
|
|
headers = config |> Keyword.get(:headers, @headers) |> MapSet.new()
|
|
|
|
reserved = Keyword.get(config, :reserved, @reserved)
|
|
|
|
|
|
|
|
proxies =
|
|
|
|
config
|
|
|
|
|> Keyword.get(:proxies, [])
|
|
|
|
|> Enum.concat(reserved)
|
|
|
|
|> Enum.map(&InetCidr.parse/1)
|
|
|
|
|
|
|
|
{headers, proxies}
|
|
|
|
end
|
|
|
|
end
|