Merge branch 'bugfix/mastoapi-lists' into 'develop'
[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.HTTP.AdapterHelper
15 require Logger
16
17 @type proxy ::
18 {Connection.host(), pos_integer()}
19 | {Connection.proxy_type(), Connection.host(), pos_integer()}
20
21 @callback options(keyword(), URI.t()) :: keyword()
22 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
23
24 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
25 def format_proxy(nil), do: nil
26
27 def format_proxy(proxy_url) do
28 case parse_proxy(proxy_url) do
29 {:ok, host, port} -> {host, port}
30 {:ok, type, host, port} -> {type, host, port}
31 _ -> nil
32 end
33 end
34
35 @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
36 def maybe_add_proxy(opts, nil), do: opts
37 def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
38
39 @doc """
40 Merge default connection & adapter options with received ones.
41 """
42
43 @spec options(URI.t(), keyword()) :: keyword()
44 def options(%URI{} = uri, opts \\ []) do
45 @defaults
46 |> Keyword.merge(opts)
47 |> adapter_helper().options(uri)
48 end
49
50 @spec get_conn(URI.t(), keyword()) :: {:ok, keyword()} | {:error, atom()}
51 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
52
53 defp adapter, do: Application.get_env(:tesla, :adapter)
54
55 defp adapter_helper do
56 case adapter() do
57 Tesla.Adapter.Gun -> AdapterHelper.Gun
58 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
59 _ -> AdapterHelper.Default
60 end
61 end
62
63 @spec parse_proxy(String.t() | tuple() | nil) ::
64 {:ok, host(), pos_integer()}
65 | {:ok, proxy_type(), host(), pos_integer()}
66 | {:error, atom()}
67 | nil
68
69 def parse_proxy(nil), do: nil
70
71 def parse_proxy(proxy) when is_binary(proxy) do
72 with [host, port] <- String.split(proxy, ":"),
73 {port, ""} <- Integer.parse(port) do
74 {:ok, parse_host(host), port}
75 else
76 {_, _} ->
77 Logger.warn("Parsing port failed #{inspect(proxy)}")
78 {:error, :invalid_proxy_port}
79
80 :error ->
81 Logger.warn("Parsing port failed #{inspect(proxy)}")
82 {:error, :invalid_proxy_port}
83
84 _ ->
85 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
86 {:error, :invalid_proxy}
87 end
88 end
89
90 def parse_proxy(proxy) when is_tuple(proxy) do
91 with {type, host, port} <- proxy do
92 {:ok, type, parse_host(host), port}
93 else
94 _ ->
95 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
96 {:error, :invalid_proxy}
97 end
98 end
99
100 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
101 def parse_host(host) when is_list(host), do: host
102 def parse_host(host) when is_atom(host), do: to_charlist(host)
103
104 def parse_host(host) when is_binary(host) do
105 host = to_charlist(host)
106
107 case :inet.parse_address(host) do
108 {:error, :einval} -> host
109 {:ok, ip} -> ip
110 end
111 end
112
113 @spec format_host(String.t()) :: charlist()
114 def format_host(host) do
115 host_charlist = to_charlist(host)
116
117 case :inet.parse_address(host_charlist) do
118 {:error, :einval} ->
119 :idna.encode(host_charlist)
120
121 {:ok, _ip} ->
122 host_charlist
123 end
124 end
125 end