1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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]
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()
22 @callback get_conn(URI.t(), keyword()) :: {:ok, term()} | {:error, term()}
24 @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
25 def format_proxy(nil), do: nil
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}
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)
40 Merge default connection & adapter options with received ones.
43 @spec options(URI.t(), keyword()) :: keyword()
44 def options(%URI{} = uri, opts \\ []) do
46 |> Keyword.merge(opts)
47 |> adapter_helper().options(uri)
50 @spec get_conn(URI.t(), keyword()) :: {:ok, keyword()} | {:error, atom()}
51 def get_conn(uri, opts), do: adapter_helper().get_conn(uri, opts)
53 defp adapter, do: Application.get_env(:tesla, :adapter)
55 defp adapter_helper do
57 Tesla.Adapter.Gun -> AdapterHelper.Gun
58 Tesla.Adapter.Hackney -> AdapterHelper.Hackney
59 _ -> AdapterHelper.Default
63 @spec parse_proxy(String.t() | tuple() | nil) ::
64 {:ok, host(), pos_integer()}
65 | {:ok, proxy_type(), host(), pos_integer()}
69 def parse_proxy(nil), do: nil
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}
77 Logger.warn("Parsing port failed #{inspect(proxy)}")
78 {:error, :invalid_proxy_port}
81 Logger.warn("Parsing port failed #{inspect(proxy)}")
82 {:error, :invalid_proxy_port}
85 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
86 {:error, :invalid_proxy}
90 def parse_proxy(proxy) when is_tuple(proxy) do
91 with {type, host, port} <- proxy do
92 {:ok, type, parse_host(host), port}
95 Logger.warn("Parsing proxy failed #{inspect(proxy)}")
96 {:error, :invalid_proxy}
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)
104 def parse_host(host) when is_binary(host) do
105 host = to_charlist(host)
107 case :inet.parse_address(host) do
108 {:error, :einval} -> host
113 @spec format_host(String.t()) :: charlist()
114 def format_host(host) do
115 host_charlist = to_charlist(host)
117 case :inet.parse_address(host_charlist) do
119 :idna.encode(host_charlist)