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()
15 alias Pleroma.HTTP.AdapterHelper
19 {Connection.host(), pos_integer()}
20 | {Connection.proxy_type(), Connection.host(), pos_integer()}
22 @callback options(keyword(), URI.t()) :: keyword()
23 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
25 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
26 def format_proxy(nil), do: nil
28 def format_proxy(proxy_url) do
29 case parse_proxy(proxy_url) do
30 {:ok, host, port} -> {host, port}
31 {:ok, type, host, port} -> {type, host, port}
36 @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
37 def maybe_add_proxy(opts, nil), do: opts
38 def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
41 Merge default connection & adapter options with received ones.
44 @spec options(URI.t(), keyword()) :: keyword()
45 def options(%URI{} = uri, opts \\ []) do
48 |> Keyword.merge(opts)
49 |> adapter_helper().options(uri)
52 # For Hackney, this is the time a connection can stay idle in the pool.
53 # For Gun, this is the timeout to receive a message from Gun.
54 defp put_timeout(opts) 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 timeout = Config.get([config_key, opts[:pool], :timeout], default)
64 Keyword.merge(opts, timeout: timeout)
67 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
68 defp adapter, do: Application.get_env(:tesla, :adapter)
70 defp adapter_helper do
72 Tesla.Adapter.Gun -> AdapterHelper.Gun
73 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
74 _ -> AdapterHelper.Default
78 @spec parse_proxy(String.t() | tuple() | nil) ::
79 {:ok, host(), pos_integer()}
80 | {:ok, proxy_type(), host(), pos_integer()}
84 def parse_proxy(nil), do: nil
86 def parse_proxy(proxy) when is_binary(proxy) do
87 with [host, port] <- String.split(proxy, ":"),
88 {port, ""} <- Integer.parse(port) do
89 {:ok, parse_host(host), port}
92 Logger.warn("Parsing port failed #{inspect(proxy)}")
93 {:error, :invalid_proxy_port}
96 Logger.warn("Parsing port failed #{inspect(proxy)}")
97 {:error, :invalid_proxy_port}
100 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
101 {:error, :invalid_proxy}
105 def parse_proxy(proxy) when is_tuple(proxy) do
106 with {type, host, port} <- proxy do
107 {:ok, type, parse_host(host), port}
110 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
111 {:error, :invalid_proxy}
115 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
116 def parse_host(host) when is_list(host), do: host
117 def parse_host(host) when is_atom(host), do: to_charlist(host)
119 def parse_host(host) when is_binary(host) do
120 host = to_charlist(host)
122 case :inet.parse_address(host) do
123 {:error, :einval} -> host
128 @spec format_host(String.t()) :: charlist()
129 def format_host(host) do
130 host_charlist = to_charlist(host)
132 case :inet.parse_address(host_charlist) do
134 :idna.encode(host_charlist)