don't log info ssl messages
[akkoma] / lib / pleroma / http / adapter / gun.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.HTTP.Adapter.Gun do
6 @behaviour Pleroma.HTTP.Adapter
7
8 alias Pleroma.HTTP.Adapter
9
10 require Logger
11
12 alias Pleroma.Pool.Connections
13
14 @defaults [
15 connect_timeout: 20_000,
16 domain_lookup_timeout: 5_000,
17 tls_handshake_timeout: 5_000,
18 retry: 0,
19 await_up_timeout: 5_000
20 ]
21
22 @spec options(keyword(), URI.t()) :: keyword()
23 def options(connection_opts \\ [], %URI{} = uri) do
24 proxy = Pleroma.Config.get([:http, :proxy_url], nil)
25
26 @defaults
27 |> Keyword.merge(Pleroma.Config.get([:http, :adapter], []))
28 |> add_original(uri)
29 |> add_scheme_opts(uri)
30 |> Adapter.maybe_add_proxy(Adapter.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_original(opts, %URI{host: host, port: port}) do
45 formatted_host = Adapter.domain_or_fallback(host)
46
47 Keyword.put(opts, :original, "#{formatted_host}:#{port}")
48 end
49
50 defp add_scheme_opts(opts, %URI{scheme: "http"}), do: opts
51
52 defp add_scheme_opts(opts, %URI{scheme: "https", host: host, port: port}) do
53 adapter_opts = [
54 certificates_verification: true,
55 tls_opts: [
56 verify: :verify_peer,
57 cacertfile: CAStore.file_path(),
58 depth: 20,
59 reuse_sessions: false,
60 verify_fun:
61 {&:ssl_verify_hostname.verify_fun/3, [check_hostname: Adapter.domain_or_fallback(host)]},
62 log_level: :warning
63 ]
64 ]
65
66 adapter_opts =
67 if port != 443 do
68 Keyword.put(adapter_opts, :transport, :tls)
69 else
70 adapter_opts
71 end
72
73 Keyword.merge(opts, adapter_opts)
74 end
75
76 defp maybe_get_conn(adapter_opts, uri, connection_opts) do
77 {receive_conn?, opts} =
78 adapter_opts
79 |> Keyword.merge(connection_opts)
80 |> Keyword.pop(:receive_conn, true)
81
82 if Connections.alive?(:gun_connections) and receive_conn? do
83 try_to_get_conn(uri, opts)
84 else
85 opts
86 end
87 end
88
89 defp try_to_get_conn(uri, opts) do
90 try do
91 case Connections.checkin(uri, :gun_connections) do
92 nil ->
93 Logger.debug(
94 "Gun connections pool checkin was not successful. Trying to open conn for next request."
95 )
96
97 :ok = Connections.open_conn(uri, :gun_connections, opts)
98 opts
99
100 conn when is_pid(conn) ->
101 Logger.debug(
102 "received conn #{inspect(conn)} #{uri.scheme}://#{Connections.compose_uri(uri)}"
103 )
104
105 opts
106 |> Keyword.put(:conn, conn)
107 |> Keyword.put(:close_conn, false)
108 end
109 rescue
110 error ->
111 Logger.warn(
112 "Gun connections pool checkin caused error #{uri.scheme}://#{
113 Connections.compose_uri(uri)
114 } #{inspect(error)}"
115 )
116
117 opts
118 catch
119 :exit, {:timeout, {_, operation, [_, {method, _}, _]}} ->
120 messages_len =
121 :gun_connections
122 |> Process.whereis()
123 |> Process.info(:message_queue_len)
124
125 Logger.warn(
126 "Gun connections pool checkin with timeout error for #{operation} #{method} #{
127 uri.scheme
128 }://#{Connections.compose_uri(uri)}. Messages length: #{messages_len}"
129 )
130
131 opts
132
133 :exit, error ->
134 Logger.warn(
135 "Gun pool checkin exited with error #{uri.scheme}://#{Connections.compose_uri(uri)} #{
136 inspect(error)
137 }"
138 )
139
140 opts
141 end
142 end
143 end