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.Gun.Conn do
7 Struct for gun connection data
10 alias Pleroma.Pool.Connections
14 @type gun_state :: :up | :down
15 @type conn_state :: :active | :idle
17 @type t :: %__MODULE__{
19 gun_state: gun_state(),
20 conn_state: conn_state(),
22 last_reference: pos_integer(),
24 retries: pos_integer()
35 @spec open(String.t() | URI.t(), atom(), keyword()) :: :ok | nil
36 def open(url, name, opts \\ [])
37 def open(url, name, opts) when is_binary(url), do: open(URI.parse(url), name, opts)
39 def open(%URI{} = uri, name, opts) do
40 pool_opts = Pleroma.Config.get([:connections_pool], [])
45 |> Map.put_new(:retry, pool_opts[:retry] || 1)
46 |> Map.put_new(:retry_timeout, pool_opts[:retry_timeout] || 1000)
47 |> Map.put_new(:await_up_timeout, pool_opts[:await_up_timeout] || 5_000)
48 |> maybe_add_tls_opts(uri)
50 key = "#{uri.scheme}:#{uri.host}:#{uri.port}"
53 if Connections.count(name) < opts[:max_connection] do
56 close_least_used_and_do_open(name, uri, opts)
59 if is_pid(conn_pid) do
60 conn = %Pleroma.Gun.Conn{
64 last_reference: :os.system_time(:second)
67 :ok = Gun.set_owner(conn_pid, Process.whereis(name))
68 Connections.add_conn(name, key, conn)
72 defp maybe_add_tls_opts(opts, %URI{scheme: "http"}), do: opts
74 defp maybe_add_tls_opts(opts, %URI{scheme: "https", host: host}) do
77 cacertfile: CAStore.file_path(),
79 reuse_sessions: false,
81 {&:ssl_verify_hostname.verify_fun/3,
82 [check_hostname: Pleroma.HTTP.Connection.format_host(host)]}
86 if Keyword.keyword?(opts[:tls_opts]) do
87 Keyword.merge(tls_opts, opts[:tls_opts])
92 Map.put(opts, :tls_opts, tls_opts)
95 defp do_open(uri, %{proxy: {proxy_host, proxy_port}} = opts) do
99 |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
101 with open_opts <- Map.delete(opts, :tls_opts),
102 {:ok, conn} <- Gun.open(proxy_host, proxy_port, open_opts),
103 {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]),
104 stream <- Gun.connect(conn, connect_opts),
105 {:response, :fin, 200, _} <- Gun.await(conn, stream) do
110 "Opening proxied connection to #{compose_uri_log(uri)} failed with error #{
119 defp do_open(uri, %{proxy: {proxy_type, proxy_host, proxy_port}} = opts) do
131 |> destination_opts()
132 |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
133 |> Map.put(:version, version)
137 |> Map.put(:protocols, [:socks])
138 |> Map.put(:socks_opts, socks_opts)
140 with {:ok, conn} <- Gun.open(proxy_host, proxy_port, opts),
141 {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
146 "Opening socks proxied connection to #{compose_uri_log(uri)} failed with error #{
155 defp do_open(%URI{host: host, port: port} = uri, opts) do
156 host = Pleroma.HTTP.Connection.parse_host(host)
158 with {:ok, conn} <- Gun.open(host, port, opts),
159 {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
164 "Opening connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
171 defp destination_opts(%URI{host: host, port: port}) do
172 host = Pleroma.HTTP.Connection.parse_host(host)
173 %{host: host, port: port}
176 defp add_http2_opts(opts, "https", tls_opts) do
177 Map.merge(opts, %{protocols: [:http2], transport: :tls, tls_opts: tls_opts})
180 defp add_http2_opts(opts, _, _), do: opts
182 defp close_least_used_and_do_open(name, uri, opts) do
183 with [{key, conn} | _conns] <- Connections.get_unused_conns(name),
184 :ok <- Gun.close(conn.conn) do
185 Connections.remove_conn(name, key)
189 [] -> {:error, :pool_overflowed}
193 def compose_uri_log(%URI{scheme: scheme, host: host, path: path}) do
194 "#{scheme}://#{host}#{path}"