Refactor gun pooling and simplify adapter option insertion
[akkoma] / lib / pleroma / http / adapter_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.AdapterHelper do
6 @moduledoc """
7 Configure Tesla.Client with default and customized adapter options.
8 """
9 @defaults [pool: :federation]
10
11 @type ip_address :: ipv4_address() | ipv6_address()
12 @type ipv4_address :: {0..255, 0..255, 0..255, 0..255}
13 @type ipv6_address ::
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()
17
18 alias Pleroma.Config
19 alias Pleroma.HTTP.AdapterHelper
20 require Logger
21
22 @type proxy ::
23 {Connection.host(), pos_integer()}
24 | {Connection.proxy_type(), Connection.host(), pos_integer()}
25
26 @callback options(keyword(), URI.t()) :: keyword()
27 @callback after_request(keyword()) :: :ok
28 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
29
30 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
31 def format_proxy(nil), do: nil
32
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}
37 _ -> nil
38 end
39 end
40
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)
44
45 @doc """
46 Merge default connection & adapter options with received ones.
47 """
48
49 @spec options(URI.t(), keyword()) :: keyword()
50 def options(%URI{} = uri, opts \\ []) do
51 @defaults
52 |> pool_timeout()
53 |> Keyword.merge(opts)
54 |> adapter_helper().options(uri)
55 end
56
57 defp pool_timeout(opts) do
58 {config_key, default} =
59 if adapter() == Tesla.Adapter.Gun do
60 {:pools, Config.get([:pools, :default, :timeout])}
61 else
62 {:hackney_pools, 10_000}
63 end
64
65 timeout = Config.get([config_key, opts[:pool], :timeout], default)
66
67 Keyword.merge(opts, timeout: timeout)
68 end
69
70 @spec after_request(keyword()) :: :ok
71 def after_request(opts), do: adapter_helper().after_request(opts)
72
73 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
74 defp adapter, do: Application.get_env(:tesla, :adapter)
75
76 defp adapter_helper do
77 case adapter() do
78 Tesla.Adapter.Gun -> AdapterHelper.Gun
79 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
80 _ -> AdapterHelper.Default
81 end
82 end
83
84 @spec parse_proxy(String.t() | tuple() | nil) ::
85 {:ok, host(), pos_integer()}
86 | {:ok, proxy_type(), host(), pos_integer()}
87 | {:error, atom()}
88 | nil
89
90 def parse_proxy(nil), do: nil
91
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}
96 else
97 {_, _} ->
98 Logger.warn("Parsing port failed #{inspect(proxy)}")
99 {:error, :invalid_proxy_port}
100
101 :error ->
102 Logger.warn("Parsing port failed #{inspect(proxy)}")
103 {:error, :invalid_proxy_port}
104
105 _ ->
106 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
107 {:error, :invalid_proxy}
108 end
109 end
110
111 def parse_proxy(proxy) when is_tuple(proxy) do
112 with {type, host, port} <- proxy do
113 {:ok, type, parse_host(host), port}
114 else
115 _ ->
116 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
117 {:error, :invalid_proxy}
118 end
119 end
120
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)
124
125 def parse_host(host) when is_binary(host) do
126 host = to_charlist(host)
127
128 case :inet.parse_address(host) do
129 {:error, :einval} -> host
130 {:ok, ip} -> ip
131 end
132 end
133
134 @spec format_host(String.t()) :: charlist()
135 def format_host(host) do
136 host_charlist = to_charlist(host)
137
138 case :inet.parse_address(host_charlist) do
139 {:error, :einval} ->
140 :idna.encode(host_charlist)
141
142 {:ok, _ip} ->
143 host_charlist
144 end
145 end
146 end