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 defp pool_timeout(opts) do
53 {config_key, default} =
54 if adapter() == Tesla.Adapter.Gun do
55 {:pools, Config.get([:pools, :default, :timeout])}
57 {:hackney_pools, 10_000}
60 timeout = Config.get([config_key, opts[:pool], :timeout], default)
62 Keyword.merge(opts, timeout: timeout)
65 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
66 defp adapter, do: Application.get_env(:tesla, :adapter)
68 defp adapter_helper do
70 Tesla.Adapter.Gun -> AdapterHelper.Gun
71 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
72 _ -> AdapterHelper.Default
76 @spec parse_proxy(String.t() | tuple() | nil) ::
77 {:ok, host(), pos_integer()}
78 | {:ok, proxy_type(), host(), pos_integer()}
82 def parse_proxy(nil), do: nil
84 def parse_proxy(proxy) when is_binary(proxy) do
85 with [host, port] <- String.split(proxy, ":"),
86 {port, ""} <- Integer.parse(port) do
87 {:ok, parse_host(host), port}
90 Logger.warn("Parsing port failed #{inspect(proxy)}")
91 {:error, :invalid_proxy_port}
94 Logger.warn("Parsing port failed #{inspect(proxy)}")
95 {:error, :invalid_proxy_port}
98 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
99 {:error, :invalid_proxy}
103 def parse_proxy(proxy) when is_tuple(proxy) do
104 with {type, host, port} <- proxy do
105 {:ok, type, parse_host(host), port}
108 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
109 {:error, :invalid_proxy}
113 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
114 def parse_host(host) when is_list(host), do: host
115 def parse_host(host) when is_atom(host), do: to_charlist(host)
117 def parse_host(host) when is_binary(host) do
118 host = to_charlist(host)
120 case :inet.parse_address(host) do
121 {:error, :einval} -> host
126 @spec format_host(String.t()) :: charlist()
127 def format_host(host) do
128 host_charlist = to_charlist(host)
130 case :inet.parse_address(host_charlist) do
132 :idna.encode(host_charlist)