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 after_request(keyword()) :: :ok
28 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
30 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
31 def format_proxy(nil), do: nil
33 def format_proxy(proxy_url) do
34 case parse_proxy(proxy_url) do
35 {:ok, host, port} -> {host, port}
36 {:ok, type, host, port} -> {type, host, port}
41 @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
42 def maybe_add_proxy(opts, nil), do: opts
43 def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
46 Merge default connection & adapter options with received ones.
49 @spec options(URI.t(), keyword()) :: keyword()
50 def options(%URI{} = uri, opts \\ []) do
53 |> Keyword.merge(opts)
54 |> adapter_helper().options(uri)
57 defp pool_timeout(opts) do
58 {config_key, default} =
59 if adapter() == Tesla.Adapter.Gun do
60 {:pools, Config.get([:pools, :default, :timeout])}
62 {:hackney_pools, 10_000}
65 timeout = Config.get([config_key, opts[:pool], :timeout], default)
67 Keyword.merge(opts, timeout: timeout)
70 @spec after_request(keyword()) :: :ok
71 def after_request(opts), do: adapter_helper().after_request(opts)
73 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
74 defp adapter, do: Application.get_env(:tesla, :adapter)
76 defp adapter_helper do
78 Tesla.Adapter.Gun -> AdapterHelper.Gun
79 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
80 _ -> AdapterHelper.Default
84 @spec parse_proxy(String.t() | tuple() | nil) ::
85 {:ok, host(), pos_integer()}
86 | {:ok, proxy_type(), host(), pos_integer()}
90 def parse_proxy(nil), do: nil
92 def parse_proxy(proxy) when is_binary(proxy) do
93 with [host, port] <- String.split(proxy, ":"),
94 {port, ""} <- Integer.parse(port) do
95 {:ok, parse_host(host), port}
98 Logger.warn("Parsing port failed #{inspect(proxy)}")
99 {:error, :invalid_proxy_port}
102 Logger.warn("Parsing port failed #{inspect(proxy)}")
103 {:error, :invalid_proxy_port}
106 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
107 {:error, :invalid_proxy}
111 def parse_proxy(proxy) when is_tuple(proxy) do
112 with {type, host, port} <- proxy do
113 {:ok, type, parse_host(host), port}
116 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
117 {:error, :invalid_proxy}
121 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | ip_address()
122 def parse_host(host) when is_list(host), do: host
123 def parse_host(host) when is_atom(host), do: to_charlist(host)
125 def parse_host(host) when is_binary(host) do
126 host = to_charlist(host)
128 case :inet.parse_address(host) do
129 {:error, :einval} -> host
134 @spec format_host(String.t()) :: charlist()
135 def format_host(host) do
136 host_charlist = to_charlist(host)
138 case :inet.parse_address(host_charlist) do
140 :idna.encode(host_charlist)