1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.HTTP.AdapterHelper do
7 Configure Tesla.Client with default and customized adapter options.
9 @defaults [pool: :federation, connect_timeout: 5_000, recv_timeout: 5_000]
11 @type proxy_type() :: :socks4 | :socks5
12 @type host() :: charlist() | :inet.ip_address()
14 alias Pleroma.HTTP.AdapterHelper
18 {Connection.host(), pos_integer()}
19 | {Connection.proxy_type(), Connection.host(), pos_integer()}
21 @callback options(keyword(), URI.t()) :: keyword()
23 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
24 def format_proxy(nil), do: nil
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}
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)
39 Merge default connection & adapter options with received ones.
42 @spec options(URI.t(), keyword()) :: keyword()
43 def options(%URI{} = uri, opts \\ []) do
45 |> Keyword.merge(opts)
46 |> adapter_helper().options(uri)
49 defp adapter, do: Application.get_env(:tesla, :adapter)
51 defp adapter_helper do
53 Tesla.Adapter.Gun -> AdapterHelper.Gun
54 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
55 _ -> AdapterHelper.Default
59 @spec parse_proxy(String.t() | tuple() | nil) ::
60 {:ok, host(), pos_integer()}
61 | {:ok, proxy_type(), host(), pos_integer()}
65 def parse_proxy(nil), do: nil
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}
73 Logger.warn("Parsing port failed #{inspect(proxy)}")
74 {:error, :invalid_proxy_port}
77 Logger.warn("Parsing port failed #{inspect(proxy)}")
78 {:error, :invalid_proxy_port}
81 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
82 {:error, :invalid_proxy}
86 def parse_proxy(proxy) when is_tuple(proxy) do
87 with {type, host, port} <- proxy do
88 {:ok, type, parse_host(host), port}
91 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
92 {:error, :invalid_proxy}
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)
100 def parse_host(host) when is_binary(host) do
101 host = to_charlist(host)
103 case :inet.parse_address(host) do
104 {:error, :einval} -> host
109 @spec format_host(String.t()) :: charlist()
110 def format_host(host) do
111 host_charlist = to_charlist(host)
113 case :inet.parse_address(host_charlist) do
115 :idna.encode(host_charlist)