Use finch everywhere (#33)
[akkoma] / lib / pleroma / http / adapter_helper.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 [name: MyFinch, 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 |> AdapterHelper.Default.options(uri)
47 end
48
49 @spec parse_proxy(String.t() | tuple() | nil) ::
50 {:ok, host(), pos_integer()}
51 | {:ok, proxy_type(), host(), pos_integer()}
52 | {:error, atom()}
53 | nil
54
55 def parse_proxy(nil), do: nil
56
57 def parse_proxy(proxy) when is_binary(proxy) do
58 with [host, port] <- String.split(proxy, ":"),
59 {port, ""} <- Integer.parse(port) do
60 {:ok, parse_host(host), port}
61 else
62 {_, _} ->
63 Logger.warn("Parsing port failed #{inspect(proxy)}")
64 {:error, :invalid_proxy_port}
65
66 :error ->
67 Logger.warn("Parsing port failed #{inspect(proxy)}")
68 {:error, :invalid_proxy_port}
69
70 _ ->
71 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
72 {:error, :invalid_proxy}
73 end
74 end
75
76 def parse_proxy(proxy) when is_tuple(proxy) do
77 with {type, host, port} <- proxy do
78 {:ok, type, parse_host(host), port}
79 else
80 _ ->
81 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
82 {:error, :invalid_proxy}
83 end
84 end
85
86 @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
87 def parse_host(host) when is_list(host), do: host
88 def parse_host(host) when is_atom(host), do: to_charlist(host)
89
90 def parse_host(host) when is_binary(host) do
91 host = to_charlist(host)
92
93 case :inet.parse_address(host) do
94 {:error, :einval} -> host
95 {:ok, ip} -> ip
96 end
97 end
98
99 @spec format_host(String.t()) :: charlist()
100 def format_host(host) do
101 host_charlist = to_charlist(host)
102
103 case :inet.parse_address(host_charlist) do
104 {:error, :einval} ->
105 :idna.encode(host_charlist)
106
107 {:ok, _ip} ->
108 host_charlist
109 end
110 end
111 end