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