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 ip_address :: ipv4_address() | ipv6_address()
12 @type ipv4_address :: {0..255, 0..255, 0..255, 0..255}
14 {0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535, 0..65_535}
15 @type proxy_type() :: :socks4 | :socks5
16 @type host() :: charlist() | ip_address()
19 alias Pleroma.HTTP.AdapterHelper
23 {Connection.host(), pos_integer()}
24 | {Connection.proxy_type(), Connection.host(), pos_integer()}
26 @callback options(keyword(), URI.t()) :: keyword()
27 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
29 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
30 def format_proxy(nil), do: nil
32 def format_proxy(proxy_url) do
33 case parse_proxy(proxy_url) do
34 {:ok, host, port} -> {host, port}
35 {:ok, type, host, port} -> {type, host, port}
40 @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
41 def maybe_add_proxy(opts, nil), do: opts
42 def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
45 Merge default connection & adapter options with received ones.
48 @spec options(URI.t(), keyword()) :: keyword()
49 def options(%URI{} = uri, opts \\ []) do
52 |> Keyword.merge(opts)
53 |> adapter_helper().options(uri)
56 defp pool_timeout(opts) do
57 {config_key, default} =
58 if adapter() == Tesla.Adapter.Gun do
59 {:pools, Config.get([:pools, :default, :timeout])}
61 {:hackney_pools, 10_000}
64 timeout = Config.get([config_key, opts[:pool], :timeout], default)
66 Keyword.merge(opts, timeout: timeout)
69 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
70 defp adapter, do: Application.get_env(:tesla, :adapter)
72 defp adapter_helper do
74 Tesla.Adapter.Gun -> AdapterHelper.Gun
75 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
76 _ -> AdapterHelper.Default
80 @spec parse_proxy(String.t() | tuple() | nil) ::
81 {:ok, host(), pos_integer()}
82 | {:ok, proxy_type(), host(), pos_integer()}
86 def parse_proxy(nil), do: nil
88 def parse_proxy(proxy) when is_binary(proxy) do
89 with [host, port] <- String.split(proxy, ":"),
90 {port, ""} <- Integer.parse(port) do
91 {:ok, parse_host(host), port}
94 Logger.warn("Parsing port failed #{inspect(proxy)}")
95 {:error, :invalid_proxy_port}
98 Logger.warn("Parsing port failed #{inspect(proxy)}")
99 {:error, :invalid_proxy_port}
102 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
103 {:error, :invalid_proxy}
107 def parse_proxy(proxy) when is_tuple(proxy) do
108 with {type, host, port} <- proxy do
109 {:ok, type, parse_host(host), port}
112 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
113 {:error, :invalid_proxy}
117 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | ip_address()
118 def parse_host(host) when is_list(host), do: host
119 def parse_host(host) when is_atom(host), do: to_charlist(host)
121 def parse_host(host) when is_binary(host) do
122 host = to_charlist(host)
124 case :inet.parse_address(host) do
125 {:error, :einval} -> host
130 @spec format_host(String.t()) :: charlist()
131 def format_host(host) do
132 host_charlist = to_charlist(host)
134 case :inet.parse_address(host_charlist) do
136 :idna.encode(host_charlist)