2020-02-11 07:12:57 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-02-11 07:12:57 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-03-03 15:53:44 +00:00
|
|
|
defmodule Pleroma.HTTP.AdapterHelper do
|
2020-05-05 22:51:10 +00:00
|
|
|
@moduledoc """
|
|
|
|
Configure Tesla.Client with default and customized adapter options.
|
|
|
|
"""
|
2022-08-14 23:13:49 +00:00
|
|
|
@defaults [name: MyFinch, pool_timeout: 5_000, receive_timeout: 5_000]
|
2020-05-05 22:51:10 +00:00
|
|
|
|
|
|
|
@type proxy_type() :: :socks4 | :socks5
|
2020-06-16 13:20:28 +00:00
|
|
|
@type host() :: charlist() | :inet.ip_address()
|
2020-05-05 22:51:10 +00:00
|
|
|
|
|
|
|
alias Pleroma.HTTP.AdapterHelper
|
|
|
|
require Logger
|
2020-02-11 07:12:57 +00:00
|
|
|
|
2022-12-14 12:38:48 +00:00
|
|
|
@type proxy :: {Connection.proxy_type(), Connection.host(), pos_integer(), list()}
|
2020-02-11 07:12:57 +00:00
|
|
|
|
|
|
|
@callback options(keyword(), URI.t()) :: keyword()
|
|
|
|
|
|
|
|
@spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
|
|
|
|
def format_proxy(nil), do: nil
|
|
|
|
|
|
|
|
def format_proxy(proxy_url) do
|
2020-05-05 22:51:10 +00:00
|
|
|
case parse_proxy(proxy_url) do
|
2022-08-14 23:13:49 +00:00
|
|
|
{:ok, type, host, port} -> {type, host, port, []}
|
2020-02-11 07:12:57 +00:00
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
|
|
|
|
def maybe_add_proxy(opts, nil), do: opts
|
2022-08-14 23:13:49 +00:00
|
|
|
|
|
|
|
def maybe_add_proxy(opts, proxy) do
|
|
|
|
Keyword.put(opts, :proxy, proxy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def maybe_add_proxy_pool(opts, nil), do: opts
|
|
|
|
|
|
|
|
def maybe_add_proxy_pool(opts, proxy) do
|
|
|
|
Logger.info("Using HTTP Proxy: #{inspect(proxy)}")
|
|
|
|
|
|
|
|
opts
|
|
|
|
|> maybe_add_pools()
|
|
|
|
|> maybe_add_default_pool()
|
|
|
|
|> maybe_add_conn_opts()
|
|
|
|
|> put_in([:pools, :default, :conn_opts, :proxy], proxy)
|
|
|
|
end
|
|
|
|
|
2022-12-16 18:33:00 +00:00
|
|
|
def add_pool_size(opts, pool_size) do
|
|
|
|
opts
|
|
|
|
|> maybe_add_pools()
|
|
|
|
|> maybe_add_default_pool()
|
|
|
|
|> put_in([:pools, :default, :size], pool_size)
|
|
|
|
end
|
|
|
|
|
2022-08-14 23:13:49 +00:00
|
|
|
defp maybe_add_pools(opts) do
|
|
|
|
if Keyword.has_key?(opts, :pools) do
|
|
|
|
opts
|
|
|
|
else
|
|
|
|
Keyword.put(opts, :pools, %{})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_default_pool(opts) do
|
|
|
|
pools = Keyword.get(opts, :pools)
|
|
|
|
|
|
|
|
if Map.has_key?(pools, :default) do
|
|
|
|
opts
|
|
|
|
else
|
|
|
|
put_in(opts, [:pools, :default], [])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp maybe_add_conn_opts(opts) do
|
|
|
|
conn_opts = get_in(opts, [:pools, :default, :conn_opts])
|
|
|
|
|
|
|
|
unless is_nil(conn_opts) do
|
|
|
|
opts
|
|
|
|
else
|
|
|
|
put_in(opts, [:pools, :default, :conn_opts], [])
|
|
|
|
end
|
|
|
|
end
|
2020-05-05 22:51:10 +00:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Merge default connection & adapter options with received ones.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@spec options(URI.t(), keyword()) :: keyword()
|
|
|
|
def options(%URI{} = uri, opts \\ []) do
|
|
|
|
@defaults
|
|
|
|
|> Keyword.merge(opts)
|
2022-07-04 16:30:38 +00:00
|
|
|
|> AdapterHelper.Default.options(uri)
|
2020-05-05 22:51:10 +00:00
|
|
|
end
|
|
|
|
|
2022-08-14 23:13:49 +00:00
|
|
|
defp proxy_type("http"), do: {:ok, :http}
|
|
|
|
defp proxy_type("https"), do: {:ok, :https}
|
|
|
|
defp proxy_type(_), do: {:error, :unknown}
|
|
|
|
|
2020-05-05 22:51:10 +00:00
|
|
|
@spec parse_proxy(String.t() | tuple() | nil) ::
|
2022-12-14 12:38:48 +00:00
|
|
|
{:ok, proxy_type(), host(), pos_integer()}
|
2020-05-05 22:51:10 +00:00
|
|
|
| {:error, atom()}
|
|
|
|
| nil
|
|
|
|
def parse_proxy(nil), do: nil
|
2022-11-18 11:14:35 +00:00
|
|
|
def parse_proxy(""), do: nil
|
2020-05-05 22:51:10 +00:00
|
|
|
|
|
|
|
def parse_proxy(proxy) when is_binary(proxy) do
|
2022-08-14 23:13:49 +00:00
|
|
|
with %URI{} = uri <- URI.parse(proxy),
|
|
|
|
{:ok, type} <- proxy_type(uri.scheme) do
|
|
|
|
{:ok, type, uri.host, uri.port}
|
2020-05-05 22:51:10 +00:00
|
|
|
else
|
2022-08-14 23:13:49 +00:00
|
|
|
e ->
|
|
|
|
Logger.warn("Parsing proxy failed #{inspect(proxy)}, #{inspect(e)}")
|
2020-05-05 22:51:10 +00:00
|
|
|
{:error, :invalid_proxy}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_proxy(proxy) when is_tuple(proxy) do
|
|
|
|
with {type, host, port} <- proxy do
|
2022-08-14 23:13:49 +00:00
|
|
|
{:ok, type, host, port}
|
2020-05-05 22:51:10 +00:00
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.warn("Parsing proxy failed #{inspect(proxy)}")
|
|
|
|
{:error, :invalid_proxy}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-16 13:20:28 +00:00
|
|
|
@spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
|
2020-05-05 22:51:10 +00:00
|
|
|
def parse_host(host) when is_list(host), do: host
|
|
|
|
def parse_host(host) when is_atom(host), do: to_charlist(host)
|
|
|
|
|
|
|
|
def parse_host(host) when is_binary(host) do
|
|
|
|
host = to_charlist(host)
|
|
|
|
|
|
|
|
case :inet.parse_address(host) do
|
|
|
|
{:error, :einval} -> host
|
|
|
|
{:ok, ip} -> ip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec format_host(String.t()) :: charlist()
|
|
|
|
def format_host(host) do
|
|
|
|
host_charlist = to_charlist(host)
|
|
|
|
|
|
|
|
case :inet.parse_address(host_charlist) do
|
|
|
|
{:error, :einval} ->
|
|
|
|
:idna.encode(host_charlist)
|
|
|
|
|
|
|
|
{:ok, _ip} ->
|
|
|
|
host_charlist
|
|
|
|
end
|
|
|
|
end
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|