2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-01 05:26:59 +00:00
|
|
|
defmodule Pleroma.HTTP.Connection do
|
2018-12-04 14:48:55 +00:00
|
|
|
@moduledoc """
|
2020-02-11 07:12:57 +00:00
|
|
|
Configure Tesla.Client with default and customized adapter options.
|
2018-12-04 14:48:55 +00:00
|
|
|
"""
|
2020-03-06 18:24:19 +00:00
|
|
|
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.HTTP.AdapterHelper
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@defaults [pool: :federation]
|
|
|
|
|
2020-02-11 07:12:57 +00:00
|
|
|
@type ip_address :: ipv4_address() | ipv6_address()
|
|
|
|
@type ipv4_address :: {0..255, 0..255, 0..255, 0..255}
|
|
|
|
@type ipv6_address ::
|
|
|
|
{0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535}
|
|
|
|
@type proxy_type() :: :socks4 | :socks5
|
|
|
|
@type host() :: charlist() | ip_address()
|
2018-12-04 14:48:55 +00:00
|
|
|
|
2020-02-11 07:12:57 +00:00
|
|
|
@doc """
|
|
|
|
Merge default connection & adapter options with received ones.
|
2018-12-01 05:26:59 +00:00
|
|
|
"""
|
2020-02-11 07:12:57 +00:00
|
|
|
|
|
|
|
@spec options(URI.t(), keyword()) :: keyword()
|
|
|
|
def options(%URI{} = uri, opts \\ []) do
|
|
|
|
@defaults
|
|
|
|
|> pool_timeout()
|
|
|
|
|> Keyword.merge(opts)
|
|
|
|
|> adapter().options(uri)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp pool_timeout(opts) do
|
2020-02-21 11:33:55 +00:00
|
|
|
{config_key, default} =
|
|
|
|
if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
|
|
|
|
{:pools, Config.get([:pools, :default, :timeout])}
|
|
|
|
else
|
|
|
|
{:hackney_pools, 10_000}
|
|
|
|
end
|
|
|
|
|
|
|
|
timeout = Config.get([config_key, opts[:pool], :timeout], default)
|
2020-02-11 07:12:57 +00:00
|
|
|
|
|
|
|
Keyword.merge(opts, timeout: timeout)
|
2018-12-01 05:26:59 +00:00
|
|
|
end
|
|
|
|
|
2020-02-11 07:12:57 +00:00
|
|
|
@spec after_request(keyword()) :: :ok
|
|
|
|
def after_request(opts), do: adapter().after_request(opts)
|
|
|
|
|
|
|
|
defp adapter do
|
|
|
|
case Application.get_env(:tesla, :adapter) do
|
2020-03-03 15:53:44 +00:00
|
|
|
Tesla.Adapter.Gun -> AdapterHelper.Gun
|
|
|
|
Tesla.Adapter.Hackney -> AdapterHelper.Hackney
|
|
|
|
_ -> AdapterHelper
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec parse_proxy(String.t() | tuple() | nil) ::
|
|
|
|
{:ok, host(), pos_integer()}
|
|
|
|
| {:ok, proxy_type(), host(), pos_integer()}
|
|
|
|
| {:error, atom()}
|
|
|
|
| nil
|
|
|
|
|
|
|
|
def parse_proxy(nil), do: nil
|
|
|
|
|
|
|
|
def parse_proxy(proxy) when is_binary(proxy) do
|
|
|
|
with [host, port] <- String.split(proxy, ":"),
|
|
|
|
{port, ""} <- Integer.parse(port) do
|
|
|
|
{:ok, parse_host(host), port}
|
|
|
|
else
|
|
|
|
{_, _} ->
|
|
|
|
Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
|
2020-03-03 14:27:22 +00:00
|
|
|
{:error, :invalid_proxy_port}
|
2020-02-11 07:12:57 +00:00
|
|
|
|
|
|
|
:error ->
|
|
|
|
Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
|
2020-03-03 14:27:22 +00:00
|
|
|
{:error, :invalid_proxy_port}
|
2020-02-11 07:12:57 +00:00
|
|
|
|
|
|
|
_ ->
|
|
|
|
Logger.warn("parsing proxy fail #{inspect(proxy)}")
|
2020-03-03 14:27:22 +00:00
|
|
|
{:error, :invalid_proxy}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_proxy(proxy) when is_tuple(proxy) do
|
|
|
|
with {type, host, port} <- proxy do
|
|
|
|
{:ok, type, parse_host(host), port}
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.warn("parsing proxy fail #{inspect(proxy)}")
|
2020-03-03 14:27:22 +00:00
|
|
|
{:error, :invalid_proxy}
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec parse_host(String.t() | atom() | charlist()) :: charlist() | ip_address()
|
|
|
|
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
|
2018-12-01 05:26:59 +00:00
|
|
|
end
|
2020-03-10 12:54:11 +00:00
|
|
|
|
|
|
|
@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
|
2018-12-01 05:26:59 +00:00
|
|
|
end
|