You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.3 KiB
49 lines
1.3 KiB
5 years ago
|
# Pleroma: A lightweight social networking server
|
||
5 years ago
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||
5 years ago
|
# 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.
|
||
|
"""
|
||
|
|
||
4 years ago
|
alias Pleroma.Config
|
||
5 years ago
|
import Plug.Conn
|
||
|
|
||
5 years ago
|
@behaviour Plug
|
||
|
|
||
|
def init(_), do: nil
|
||
|
|
||
5 years ago
|
def call(%{remote_ip: original_remote_ip} = conn, _) do
|
||
4 years ago
|
if Config.get([__MODULE__, :enabled]) do
|
||
|
%{remote_ip: new_remote_ip} = conn = RemoteIp.call(conn, remote_ip_opts())
|
||
5 years ago
|
assign(conn, :remote_ip_found, original_remote_ip != new_remote_ip)
|
||
5 years ago
|
else
|
||
|
conn
|
||
|
end
|
||
|
end
|
||
|
|
||
4 years ago
|
defp remote_ip_opts do
|
||
4 years ago
|
headers = Config.get([__MODULE__, :headers], []) |> MapSet.new()
|
||
|
reserved = Config.get([__MODULE__, :reserved], [])
|
||
5 years ago
|
|
||
|
proxies =
|
||
4 years ago
|
Config.get([__MODULE__, :proxies], [])
|
||
5 years ago
|
|> Enum.concat(reserved)
|
||
4 years ago
|
|> Enum.map(&maybe_add_cidr/1)
|
||
5 years ago
|
|
||
|
{headers, proxies}
|
||
|
end
|
||
4 years ago
|
|
||
|
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
|
||
|
|
||
4 years ago
|
InetCidr.parse(proxy, true)
|
||
4 years ago
|
end
|
||
5 years ago
|
end
|