1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP.AdapterHelper do
7 Configure Tesla.Client with default and customized adapter options.
9 @defaults [pool: :federation]
11 @type proxy_type() :: :socks4 | :socks5
12 @type host() :: charlist() | :inet.ip_address()
13 @type pool() :: :federation | :upload | :media | :default
16 alias Pleroma.HTTP.AdapterHelper
20 {Connection.host(), pos_integer()}
21 | {Connection.proxy_type(), Connection.host(), pos_integer()}
23 @callback options(keyword(), URI.t()) :: keyword()
24 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
26 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
27 def format_proxy(nil), do: nil
29 def format_proxy(proxy_url) do
30 case parse_proxy(proxy_url) do
31 {:ok, host, port} -> {host, port}
32 {:ok, type, host, port} -> {type, host, port}
37 @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
38 def maybe_add_proxy(opts, nil), do: opts
39 def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
42 Merge default connection & adapter options with received ones.
45 @spec options(URI.t(), keyword()) :: keyword()
46 def options(%URI{} = uri, opts \\ []) do
48 |> Keyword.merge(opts)
50 |> adapter_helper().options(uri)
53 @spec pool_timeout(pool()) :: non_neg_integer()
54 def pool_timeout(pool) do
55 {config_key, default} =
56 if adapter() == Tesla.Adapter.Gun do
57 {:pools, Config.get([:pools, :default, :timeout], 5_000)}
59 {:hackney_pools, 10_000}
62 Config.get([config_key, pool, :timeout], default)
65 # For Hackney, this is the time a connection can stay idle in the pool.
66 # For Gun, this is the timeout to receive a message from Gun.
67 defp put_timeout(opts) do
68 Keyword.put_new(opts, :timeout, pool_timeout(opts[:pool]))
71 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
72 defp adapter, do: Application.get_env(:tesla, :adapter)
74 defp adapter_helper do
76 Tesla.Adapter.Gun -> AdapterHelper.Gun
77 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
78 _ -> AdapterHelper.Default
82 @spec parse_proxy(String.t() | tuple() | nil) ::
83 {:ok, host(), pos_integer()}
84 | {:ok, proxy_type(), host(), pos_integer()}
88 def parse_proxy(nil), do: nil
90 def parse_proxy(proxy) when is_binary(proxy) do
91 with [host, port] <- String.split(proxy, ":"),
92 {port, ""} <- Integer.parse(port) do
93 {:ok, parse_host(host), port}
96 Logger.warn("Parsing port failed #{inspect(proxy)}")
97 {:error, :invalid_proxy_port}
100 Logger.warn("Parsing port failed #{inspect(proxy)}")
101 {:error, :invalid_proxy_port}
104 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
105 {:error, :invalid_proxy}
109 def parse_proxy(proxy) when is_tuple(proxy) do
110 with {type, host, port} <- proxy do
111 {:ok, type, parse_host(host), port}
114 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
115 {:error, :invalid_proxy}
119 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
120 def parse_host(host) when is_list(host), do: host
121 def parse_host(host) when is_atom(host), do: to_charlist(host)
123 def parse_host(host) when is_binary(host) do
124 host = to_charlist(host)
126 case :inet.parse_address(host) do
127 {:error, :einval} -> host
132 @spec format_host(String.t()) :: charlist()
133 def format_host(host) do
134 host_charlist = to_charlist(host)
136 case :inet.parse_address(host_charlist) do
138 :idna.encode(host_charlist)