Adapter Helper: Use built-in ip address type
[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 |> pool_timeout()
48 |> Keyword.merge(opts)
49 |> adapter_helper().options(uri)
50 end
51
52 defp pool_timeout(opts) do
53 {config_key, default} =
54 if adapter() == Tesla.Adapter.Gun do
55 {:pools, Config.get([:pools, :default, :timeout])}
56 else
57 {:hackney_pools, 10_000}
58 end
59
60 timeout = Config.get([config_key, opts[:pool], :timeout], default)
61
62 Keyword.merge(opts, timeout: timeout)
63 end
64
65 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
66 defp adapter, do: Application.get_env(:tesla, :adapter)
67
68 defp adapter_helper do
69 case adapter() do
70 Tesla.Adapter.Gun -> AdapterHelper.Gun
71 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
72 _ -> AdapterHelper.Default
73 end
74 end
75
76 @spec parse_proxy(String.t() | tuple() | nil) ::
77 {:ok, host(), pos_integer()}
78 | {:ok, proxy_type(), host(), pos_integer()}
79 | {:error, atom()}
80 | nil
81
82 def parse_proxy(nil), do: nil
83
84 def parse_proxy(proxy) when is_binary(proxy) do
85 with [host, port] <- String.split(proxy, ":"),
86 {port, ""} <- Integer.parse(port) do
87 {:ok, parse_host(host), port}
88 else
89 {_, _} ->
90 Logger.warn("Parsing port failed #{inspect(proxy)}")
91 {:error, :invalid_proxy_port}
92
93 :error ->
94 Logger.warn("Parsing port failed #{inspect(proxy)}")
95 {:error, :invalid_proxy_port}
96
97 _ ->
98 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
99 {:error, :invalid_proxy}
100 end
101 end
102
103 def parse_proxy(proxy) when is_tuple(proxy) do
104 with {type, host, port} <- proxy do
105 {:ok, type, parse_host(host), port}
106 else
107 _ ->
108 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
109 {:error, :invalid_proxy}
110 end
111 end
112
113 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
114 def parse_host(host) when is_list(host), do: host
115 def parse_host(host) when is_atom(host), do: to_charlist(host)
116
117 def parse_host(host) when is_binary(host) do
118 host = to_charlist(host)
119
120 case :inet.parse_address(host) do
121 {:error, :einval} -> host
122 {:ok, ip} -> ip
123 end
124 end
125
126 @spec format_host(String.t()) :: charlist()
127 def format_host(host) do
128 host_charlist = to_charlist(host)
129
130 case :inet.parse_address(host_charlist) do
131 {:error, :einval} ->
132 :idna.encode(host_charlist)
133
134 {:ok, _ip} ->
135 host_charlist
136 end
137 end
138 end