X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Fhttp%2Fadapter_helper.ex;h=e5da3ffa8f03e9578f18152b5f238b1a81db91ab;hb=a079ec3a3cdfd42d2cbd51c7698c2c87828e5778;hp=2c13666ecebff86375d4555a37d7fe13a9ac628f;hpb=b2eb1124d115beda0907121c3c6f10783b34f352;p=akkoma
diff --git a/lib/pleroma/http/adapter_helper.ex b/lib/pleroma/http/adapter_helper.ex
index 2c13666ec..e5da3ffa8 100644
--- a/lib/pleroma/http/adapter_helper.ex
+++ b/lib/pleroma/http/adapter_helper.ex
@@ -1,41 +1,153 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.AdapterHelper do
- alias Pleroma.HTTP.Connection
+ @moduledoc """
+ Configure Tesla.Client with default and customized adapter options.
+ """
+ @defaults [name: MyFinch, pool_timeout: 5_000, receive_timeout: 5_000]
- @type proxy ::
- {Connection.host(), pos_integer()}
- | {Connection.proxy_type(), pos_integer()}
+ @type proxy_type() :: :socks4 | :socks5
+ @type host() :: charlist() | :inet.ip_address()
- @callback options(keyword(), URI.t()) :: keyword()
- @callback after_request(keyword()) :: :ok
-
- @spec options(keyword(), URI.t()) :: keyword()
- def options(opts, _uri) do
- proxy = Pleroma.Config.get([:http, :proxy_url], nil)
- maybe_add_proxy(opts, format_proxy(proxy))
- end
+ alias Pleroma.HTTP.AdapterHelper
+ require Logger
- @spec maybe_get_conn(URI.t(), keyword()) :: keyword()
- def maybe_get_conn(_uri, opts), do: opts
+ @type proxy :: {Connection.proxy_type(), Connection.host(), pos_integer(), list()}
- @spec after_request(keyword()) :: :ok
- def after_request(_opts), do: :ok
+ @callback options(keyword(), URI.t()) :: keyword()
@spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
def format_proxy(nil), do: nil
def format_proxy(proxy_url) do
- case Connection.parse_proxy(proxy_url) do
- {:ok, host, port} -> {host, port}
- {:ok, type, host, port} -> {type, host, port}
+ case parse_proxy(proxy_url) do
+ {:ok, type, host, port} -> {type, host, port, []}
_ -> nil
end
end
@spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
def maybe_add_proxy(opts, nil), do: opts
- def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
+
+ def maybe_add_proxy(opts, proxy) do
+ Keyword.put(opts, :proxy, proxy)
+ end
+
+ def maybe_add_proxy_pool(opts, nil), do: opts
+
+ def maybe_add_proxy_pool(opts, proxy) do
+ Logger.info("Using HTTP Proxy: #{inspect(proxy)}")
+
+ opts
+ |> maybe_add_pools()
+ |> maybe_add_default_pool()
+ |> maybe_add_conn_opts()
+ |> put_in([:pools, :default, :conn_opts, :proxy], proxy)
+ end
+
+ def add_pool_size(opts, pool_size) do
+ opts
+ |> maybe_add_pools()
+ |> maybe_add_default_pool()
+ |> put_in([:pools, :default, :size], pool_size)
+ end
+
+ defp maybe_add_pools(opts) do
+ if Keyword.has_key?(opts, :pools) do
+ opts
+ else
+ Keyword.put(opts, :pools, %{})
+ end
+ end
+
+ defp maybe_add_default_pool(opts) do
+ pools = Keyword.get(opts, :pools)
+
+ if Map.has_key?(pools, :default) do
+ opts
+ else
+ put_in(opts, [:pools, :default], [])
+ end
+ end
+
+ defp maybe_add_conn_opts(opts) do
+ conn_opts = get_in(opts, [:pools, :default, :conn_opts])
+
+ unless is_nil(conn_opts) do
+ opts
+ else
+ put_in(opts, [:pools, :default, :conn_opts], [])
+ end
+ end
+
+ @doc """
+ Merge default connection & adapter options with received ones.
+ """
+
+ @spec options(URI.t(), keyword()) :: keyword()
+ def options(%URI{} = uri, opts \\ []) do
+ @defaults
+ |> Keyword.merge(opts)
+ |> AdapterHelper.Default.options(uri)
+ end
+
+ defp proxy_type("http"), do: {:ok, :http}
+ defp proxy_type("https"), do: {:ok, :https}
+ defp proxy_type(_), do: {:error, :unknown}
+
+ @spec parse_proxy(String.t() | tuple() | nil) ::
+ {:ok, proxy_type(), host(), pos_integer()}
+ | {:error, atom()}
+ | nil
+ def parse_proxy(nil), do: nil
+ def parse_proxy(""), do: nil
+
+ def parse_proxy(proxy) when is_binary(proxy) do
+ with %URI{} = uri <- URI.parse(proxy),
+ {:ok, type} <- proxy_type(uri.scheme) do
+ {:ok, type, uri.host, uri.port}
+ else
+ e ->
+ Logger.warn("Parsing proxy failed #{inspect(proxy)}, #{inspect(e)}")
+ {:error, :invalid_proxy}
+ end
+ end
+
+ def parse_proxy(proxy) when is_tuple(proxy) do
+ with {type, host, port} <- proxy do
+ {:ok, type, host, port}
+ else
+ _ ->
+ Logger.warn("Parsing proxy failed #{inspect(proxy)}")
+ {:error, :invalid_proxy}
+ end
+ end
+
+ @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
+ def parse_host(host) when is_list(host), do: host
+ def parse_host(host) when is_atom(host), do: to_charlist(host)
+
+ def parse_host(host) when is_binary(host) do
+ host = to_charlist(host)
+
+ case :inet.parse_address(host) do
+ {:error, :einval} -> host
+ {:ok, ip} -> ip
+ end
+ end
+
+ @spec format_host(String.t()) :: charlist()
+ def format_host(host) do
+ host_charlist = to_charlist(host)
+
+ case :inet.parse_address(host_charlist) do
+ {:error, :einval} ->
+ :idna.encode(host_charlist)
+
+ {:ok, _ip} ->
+ host_charlist
+ end
+ end
end