update task messages
[akkoma] / lib / pleroma / gun / conn.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Gun.Conn do
6 @moduledoc """
7 Struct for gun connection data
8 """
9 alias Pleroma.Gun
10 alias Pleroma.Pool.Connections
11
12 require Logger
13
14 @type gun_state :: :up | :down
15 @type conn_state :: :active | :idle
16
17 @type t :: %__MODULE__{
18 conn: pid(),
19 gun_state: gun_state(),
20 conn_state: conn_state(),
21 used_by: [pid()],
22 last_reference: pos_integer(),
23 crf: float(),
24 retries: pos_integer()
25 }
26
27 defstruct conn: nil,
28 gun_state: :open,
29 conn_state: :init,
30 used_by: [],
31 last_reference: 0,
32 crf: 1,
33 retries: 0
34
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)
38
39 def open(%URI{} = uri, name, opts) do
40 pool_opts = Pleroma.Config.get([:connections_pool], [])
41
42 opts =
43 opts
44 |> Enum.into(%{})
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)
49
50 key = "#{uri.scheme}:#{uri.host}:#{uri.port}"
51
52 max_connections = pool_opts[:max_connections] || 250
53
54 conn_pid =
55 if Connections.count(name) < max_connections do
56 do_open(uri, opts)
57 else
58 close_least_used_and_do_open(name, uri, opts)
59 end
60
61 if is_pid(conn_pid) do
62 conn = %Pleroma.Gun.Conn{
63 conn: conn_pid,
64 gun_state: :up,
65 conn_state: :active,
66 last_reference: :os.system_time(:second)
67 }
68
69 :ok = Gun.set_owner(conn_pid, Process.whereis(name))
70 Connections.add_conn(name, key, conn)
71 end
72 end
73
74 defp maybe_add_tls_opts(opts, %URI{scheme: "http"}), do: opts
75
76 defp maybe_add_tls_opts(opts, %URI{scheme: "https", host: host}) do
77 tls_opts = [
78 verify: :verify_peer,
79 cacertfile: CAStore.file_path(),
80 depth: 20,
81 reuse_sessions: false,
82 verify_fun:
83 {&:ssl_verify_hostname.verify_fun/3,
84 [check_hostname: Pleroma.HTTP.Connection.format_host(host)]}
85 ]
86
87 tls_opts =
88 if Keyword.keyword?(opts[:tls_opts]) do
89 Keyword.merge(tls_opts, opts[:tls_opts])
90 else
91 tls_opts
92 end
93
94 Map.put(opts, :tls_opts, tls_opts)
95 end
96
97 defp do_open(uri, %{proxy: {proxy_host, proxy_port}} = opts) do
98 connect_opts =
99 uri
100 |> destination_opts()
101 |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
102
103 with open_opts <- Map.delete(opts, :tls_opts),
104 {:ok, conn} <- Gun.open(proxy_host, proxy_port, open_opts),
105 {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]),
106 stream <- Gun.connect(conn, connect_opts),
107 {:response, :fin, 200, _} <- Gun.await(conn, stream) do
108 conn
109 else
110 error ->
111 Logger.warn(
112 "Opening proxied connection to #{compose_uri_log(uri)} failed with error #{
113 inspect(error)
114 }"
115 )
116
117 error
118 end
119 end
120
121 defp do_open(uri, %{proxy: {proxy_type, proxy_host, proxy_port}} = opts) do
122 version =
123 proxy_type
124 |> to_string()
125 |> String.last()
126 |> case do
127 "4" -> 4
128 _ -> 5
129 end
130
131 socks_opts =
132 uri
133 |> destination_opts()
134 |> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
135 |> Map.put(:version, version)
136
137 opts =
138 opts
139 |> Map.put(:protocols, [:socks])
140 |> Map.put(:socks_opts, socks_opts)
141
142 with {:ok, conn} <- Gun.open(proxy_host, proxy_port, opts),
143 {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
144 conn
145 else
146 error ->
147 Logger.warn(
148 "Opening socks proxied connection to #{compose_uri_log(uri)} failed with error #{
149 inspect(error)
150 }"
151 )
152
153 error
154 end
155 end
156
157 defp do_open(%URI{host: host, port: port} = uri, opts) do
158 host = Pleroma.HTTP.Connection.parse_host(host)
159
160 with {:ok, conn} <- Gun.open(host, port, opts),
161 {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
162 conn
163 else
164 error ->
165 Logger.warn(
166 "Opening connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
167 )
168
169 error
170 end
171 end
172
173 defp destination_opts(%URI{host: host, port: port}) do
174 host = Pleroma.HTTP.Connection.parse_host(host)
175 %{host: host, port: port}
176 end
177
178 defp add_http2_opts(opts, "https", tls_opts) do
179 Map.merge(opts, %{protocols: [:http2], transport: :tls, tls_opts: tls_opts})
180 end
181
182 defp add_http2_opts(opts, _, _), do: opts
183
184 defp close_least_used_and_do_open(name, uri, opts) do
185 with [{key, conn} | _conns] <- Connections.get_unused_conns(name),
186 :ok <- Gun.close(conn.conn) do
187 Connections.remove_conn(name, key)
188
189 do_open(uri, opts)
190 else
191 [] -> {:error, :pool_overflowed}
192 end
193 end
194
195 def compose_uri_log(%URI{scheme: scheme, host: host, path: path}) do
196 "#{scheme}://#{host}#{path}"
197 end
198 end