5d5870d90200f5cbd5d48b4934a89c9802e0d170
[akkoma] / lib / pleroma / http / adapter_helper / gun.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.HTTP.AdapterHelper.Gun do
6 @behaviour Pleroma.HTTP.AdapterHelper
7
8 alias Pleroma.HTTP.AdapterHelper
9
10 require Logger
11
12 alias Pleroma.Pool.Connections
13
14 @defaults [
15 connect_timeout: 5_000,
16 domain_lookup_timeout: 5_000,
17 tls_handshake_timeout: 5_000,
18 retry: 1,
19 retry_timeout: 1000,
20 await_up_timeout: 5_000
21 ]
22
23 @spec options(keyword(), URI.t()) :: keyword()
24 def options(connection_opts \\ [], %URI{} = uri) do
25 proxy = Pleroma.Config.get([:http, :proxy_url], nil)
26
27 @defaults
28 |> Keyword.merge(Pleroma.Config.get([:http, :adapter], []))
29 |> add_scheme_opts(uri)
30 |> AdapterHelper.maybe_add_proxy(AdapterHelper.format_proxy(proxy))
31 |> maybe_get_conn(uri, connection_opts)
32 end
33
34 @spec after_request(keyword()) :: :ok
35 def after_request(opts) do
36 with conn when not is_nil(conn) <- opts[:conn],
37 body_as when body_as != :chunks <- opts[:body_as] do
38 Connections.checkout(conn, self(), :gun_connections)
39 end
40
41 :ok
42 end
43
44 defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
45
46 defp add_scheme_opts(opts, %URI{scheme: "https", host: host}) do
47 adapter_opts = [
48 certificates_verification: true,
49 transport: :tls,
50 tls_opts: [
51 verify: :verify_peer,
52 cacertfile: CAStore.file_path(),
53 depth: 20,
54 reuse_sessions: false,
55 verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: format_host(host)]},
56 log_level: :warning
57 ]
58 ]
59
60 Keyword.merge(opts, adapter_opts)
61 end
62
63 defp maybe_get_conn(adapter_opts, uri, connection_opts) do
64 {receive_conn?, opts} =
65 adapter_opts
66 |> Keyword.merge(connection_opts)
67 |> Keyword.pop(:receive_conn, true)
68
69 if Connections.alive?(:gun_connections) and receive_conn? do
70 try_to_get_conn(uri, opts)
71 else
72 opts
73 end
74 end
75
76 defp try_to_get_conn(uri, opts) do
77 case Connections.checkin(uri, :gun_connections) do
78 nil ->
79 Logger.debug(
80 "Gun connections pool checkin was not successful. Trying to open conn for next request."
81 )
82
83 Task.start(fn -> Pleroma.Gun.Conn.open(uri, :gun_connections, opts) end)
84 opts
85
86 conn when is_pid(conn) ->
87 Logger.debug("received conn #{inspect(conn)} #{Connections.compose_uri_log(uri)}")
88
89 opts
90 |> Keyword.put(:conn, conn)
91 |> Keyword.put(:close_conn, false)
92 end
93 end
94
95 @spec format_host(String.t()) :: charlist()
96 def format_host(host) do
97 host_charlist = to_charlist(host)
98
99 case :inet.parse_address(host_charlist) do
100 {:error, :einval} ->
101 :idna.encode(host_charlist)
102
103 {:ok, _ip} ->
104 host_charlist
105 end
106 end
107 end