Use a custom pool-aware FollowRedirects middleware
[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 get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
28
29 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
30 def format_proxy(nil), do: nil
31
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}
36 _ -> nil
37 end
38 end
39
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)
43
44 @doc """
45 Merge default connection & adapter options with received ones.
46 """
47
48 @spec options(URI.t(), keyword()) :: keyword()
49 def options(%URI{} = uri, opts \\ []) do
50 @defaults
51 |> pool_timeout()
52 |> Keyword.merge(opts)
53 |> adapter_helper().options(uri)
54 end
55
56 defp pool_timeout(opts) do
57 {config_key, default} =
58 if adapter() == Tesla.Adapter.Gun do
59 {:pools, Config.get([:pools, :default, :timeout])}
60 else
61 {:hackney_pools, 10_000}
62 end
63
64 timeout = Config.get([config_key, opts[:pool], :timeout], default)
65
66 Keyword.merge(opts, timeout: timeout)
67 end
68
69 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
70 defp adapter, do: Application.get_env(:tesla, :adapter)
71
72 defp adapter_helper do
73 case adapter() do
74 Tesla.Adapter.Gun -> AdapterHelper.Gun
75 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
76 _ -> AdapterHelper.Default
77 end
78 end
79
80 @spec parse_proxy(String.t() | tuple() | nil) ::
81 {:ok, host(), pos_integer()}
82 | {:ok, proxy_type(), host(), pos_integer()}
83 | {:error, atom()}
84 | nil
85
86 def parse_proxy(nil), do: nil
87
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}
92 else
93 {_, _} ->
94 Logger.warn("Parsing port failed #{inspect(proxy)}")
95 {:error, :invalid_proxy_port}
96
97 :error ->
98 Logger.warn("Parsing port failed #{inspect(proxy)}")
99 {:error, :invalid_proxy_port}
100
101 _ ->
102 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
103 {:error, :invalid_proxy}
104 end
105 end
106
107 def parse_proxy(proxy) when is_tuple(proxy) do
108 with {type, host, port} <- proxy do
109 {:ok, type, parse_host(host), port}
110 else
111 _ ->
112 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
113 {:error, :invalid_proxy}
114 end
115 end
116
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)
120
121 def parse_host(host) when is_binary(host) do
122 host = to_charlist(host)
123
124 case :inet.parse_address(host) do
125 {:error, :einval} -> host
126 {:ok, ip} -> ip
127 end
128 end
129
130 @spec format_host(String.t()) :: charlist()
131 def format_host(host) do
132 host_charlist = to_charlist(host)
133
134 case :inet.parse_address(host_charlist) do
135 {:error, :einval} ->
136 :idna.encode(host_charlist)
137
138 {:ok, _ip} ->
139 host_charlist
140 end
141 end
142 end