logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

adapter_helper.ex (3373B)


  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. defmodule Pleroma.HTTP.AdapterHelper do
  5. @moduledoc """
  6. Configure Tesla.Client with default and customized adapter options.
  7. """
  8. @defaults [pool: :federation, connect_timeout: 5_000, recv_timeout: 5_000]
  9. @type proxy_type() :: :socks4 | :socks5
  10. @type host() :: charlist() | :inet.ip_address()
  11. alias Pleroma.HTTP.AdapterHelper
  12. require Logger
  13. @type proxy ::
  14. {host(), pos_integer()}
  15. | {proxy_type(), host(), pos_integer()}
  16. @callback options(keyword(), URI.t()) :: keyword()
  17. @spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
  18. def format_proxy(nil), do: nil
  19. def format_proxy(proxy_url) do
  20. case parse_proxy(proxy_url) do
  21. {:ok, host, port} -> {host, port}
  22. {:ok, type, host, port} -> {type, host, port}
  23. _ -> nil
  24. end
  25. end
  26. @spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
  27. def maybe_add_proxy(opts, nil), do: opts
  28. def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
  29. @doc """
  30. Merge default connection & adapter options with received ones.
  31. """
  32. @spec options(URI.t(), keyword()) :: keyword()
  33. def options(%URI{} = uri, opts \\ []) do
  34. @defaults
  35. |> Keyword.merge(opts)
  36. |> adapter_helper().options(uri)
  37. end
  38. defp adapter, do: Application.get_env(:tesla, :adapter)
  39. defp adapter_helper do
  40. case adapter() do
  41. Tesla.Adapter.Gun -> AdapterHelper.Gun
  42. Tesla.Adapter.Hackney -> AdapterHelper.Hackney
  43. _ -> AdapterHelper.Default
  44. end
  45. end
  46. @spec parse_proxy(String.t() | tuple() | nil) ::
  47. {:ok, host(), pos_integer()}
  48. | {:ok, proxy_type(), host(), pos_integer()}
  49. | {:error, atom()}
  50. | nil
  51. def parse_proxy(nil), do: nil
  52. def parse_proxy(proxy) when is_binary(proxy) do
  53. with [host, port] <- String.split(proxy, ":"),
  54. {port, ""} <- Integer.parse(port) do
  55. {:ok, parse_host(host), port}
  56. else
  57. {_, _} ->
  58. Logger.warning("Parsing port failed #{inspect(proxy)}")
  59. {:error, :invalid_proxy_port}
  60. :error ->
  61. Logger.warning("Parsing port failed #{inspect(proxy)}")
  62. {:error, :invalid_proxy_port}
  63. _ ->
  64. Logger.warning("Parsing proxy failed #{inspect(proxy)}")
  65. {:error, :invalid_proxy}
  66. end
  67. end
  68. def parse_proxy(proxy) when is_tuple(proxy) do
  69. with {type, host, port} <- proxy do
  70. {:ok, type, parse_host(host), port}
  71. else
  72. _ ->
  73. Logger.warning("Parsing proxy failed #{inspect(proxy)}")
  74. {:error, :invalid_proxy}
  75. end
  76. end
  77. @spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
  78. def parse_host(host) when is_list(host), do: host
  79. def parse_host(host) when is_atom(host), do: to_charlist(host)
  80. def parse_host(host) when is_binary(host) do
  81. host = to_charlist(host)
  82. case :inet.parse_address(host) do
  83. {:error, :einval} -> host
  84. {:ok, ip} -> ip
  85. end
  86. end
  87. @spec format_host(String.t()) :: charlist()
  88. def format_host(host) do
  89. host_charlist = to_charlist(host)
  90. case :inet.parse_address(host_charlist) do
  91. {:error, :einval} ->
  92. :idna.encode(host_charlist)
  93. {:ok, _ip} ->
  94. host_charlist
  95. end
  96. end
  97. end