Merge remote-tracking branch 'pleroma/develop' into features/poll-validation
[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 proxy_type() :: :socks4 | :socks5
12 @type host() :: charlist() | :inet.ip_address()
13
14 alias Pleroma.Config
15 alias Pleroma.HTTP.AdapterHelper
16 require Logger
17
18 @type proxy ::
19 {Connection.host(), pos_integer()}
20 | {Connection.proxy_type(), Connection.host(), pos_integer()}
21
22 @callback options(keyword(), URI.t()) :: keyword()
23 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
24
25 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
26 def format_proxy(nil), do: nil
27
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}
32 _ -> nil
33 end
34 end
35
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)
39
40 @doc """
41 Merge default connection & adapter options with received ones.
42 """
43
44 @spec options(URI.t(), keyword()) :: keyword()
45 def options(%URI{} = uri, opts \\ []) do
46 @defaults
47 |> put_timeout()
48 |> Keyword.merge(opts)
49 |> adapter_helper().options(uri)
50 end
51
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)}
58 else
59 {:hackney_pools, 10_000}
60 end
61
62 timeout = Config.get([config_key, opts[:pool], :timeout], default)
63
64 Keyword.merge(opts, timeout: timeout)
65 end
66
67 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
68 defp adapter, do: Application.get_env(:tesla, :adapter)
69
70 defp adapter_helper do
71 case adapter() do
72 Tesla.Adapter.Gun -> AdapterHelper.Gun
73 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
74 _ -> AdapterHelper.Default
75 end
76 end
77
78 @spec parse_proxy(String.t() | tuple() | nil) ::
79 {:ok, host(), pos_integer()}
80 | {:ok, proxy_type(), host(), pos_integer()}
81 | {:error, atom()}
82 | nil
83
84 def parse_proxy(nil), do: nil
85
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}
90 else
91 {_, _} ->
92 Logger.warn("Parsing port failed #{inspect(proxy)}")
93 {:error, :invalid_proxy_port}
94
95 :error ->
96 Logger.warn("Parsing port failed #{inspect(proxy)}")
97 {:error, :invalid_proxy_port}
98
99 _ ->
100 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
101 {:error, :invalid_proxy}
102 end
103 end
104
105 def parse_proxy(proxy) when is_tuple(proxy) do
106 with {type, host, port} <- proxy do
107 {:ok, type, parse_host(host), port}
108 else
109 _ ->
110 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
111 {:error, :invalid_proxy}
112 end
113 end
114
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)
118
119 def parse_host(host) when is_binary(host) do
120 host = to_charlist(host)
121
122 case :inet.parse_address(host) do
123 {:error, :einval} -> host
124 {:ok, ip} -> ip
125 end
126 end
127
128 @spec format_host(String.t()) :: charlist()
129 def format_host(host) do
130 host_charlist = to_charlist(host)
131
132 case :inet.parse_address(host_charlist) do
133 {:error, :einval} ->
134 :idna.encode(host_charlist)
135
136 {:ok, _ip} ->
137 host_charlist
138 end
139 end
140 end