X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fgun%2Fconn.ex;h=cd25a2e746d1889f9b263daff6a3200917177780;hb=e090191d03b21020a75c1ef91a200c3e4807c2d1;hp=ddb9f30b024153fbe91875020baeb5abba1a5396;hpb=509c81e4b10bd8ba6d3a93889cd6fbbbfbcbab21;p=akkoma
diff --git a/lib/pleroma/gun/conn.ex b/lib/pleroma/gun/conn.ex
index ddb9f30b0..cd25a2e74 100644
--- a/lib/pleroma/gun/conn.ex
+++ b/lib/pleroma/gun/conn.ex
@@ -1,12 +1,12 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Gun.Conn do
@moduledoc """
Struct for gun connection data
"""
- alias Pleroma.Gun.API
+ alias Pleroma.Gun
alias Pleroma.Pool.Connections
require Logger
@@ -42,19 +42,20 @@ defmodule Pleroma.Gun.Conn do
opts =
opts
|> Enum.into(%{})
- |> Map.put_new(:retry, pool_opts[:retry] || 0)
- |> Map.put_new(:retry_timeout, pool_opts[:retry_timeout] || 100)
+ |> Map.put_new(:retry, pool_opts[:retry] || 1)
+ |> Map.put_new(:retry_timeout, pool_opts[:retry_timeout] || 1000)
|> Map.put_new(:await_up_timeout, pool_opts[:await_up_timeout] || 5_000)
+ |> maybe_add_tls_opts(uri)
key = "#{uri.scheme}:#{uri.host}:#{uri.port}"
- Logger.debug("opening new connection #{Connections.compose_uri_log(uri)}")
+ max_connections = pool_opts[:max_connections] || 250
conn_pid =
- if Connections.count(name) < opts[:max_connection] do
+ if Connections.count(name) < max_connections do
do_open(uri, opts)
else
- try_do_open(name, uri, opts)
+ close_least_used_and_do_open(name, uri, opts)
end
if is_pid(conn_pid) do
@@ -65,11 +66,34 @@ defmodule Pleroma.Gun.Conn do
last_reference: :os.system_time(:second)
}
- :ok = API.set_owner(conn_pid, Process.whereis(name))
+ :ok = Gun.set_owner(conn_pid, Process.whereis(name))
Connections.add_conn(name, key, conn)
end
end
+ defp maybe_add_tls_opts(opts, %URI{scheme: "http"}), do: opts
+
+ defp maybe_add_tls_opts(opts, %URI{scheme: "https", host: host}) do
+ tls_opts = [
+ verify: :verify_peer,
+ cacertfile: CAStore.file_path(),
+ depth: 20,
+ reuse_sessions: false,
+ verify_fun:
+ {&:ssl_verify_hostname.verify_fun/3,
+ [check_hostname: Pleroma.HTTP.Connection.format_host(host)]}
+ ]
+
+ tls_opts =
+ if Keyword.keyword?(opts[:tls_opts]) do
+ Keyword.merge(tls_opts, opts[:tls_opts])
+ else
+ tls_opts
+ end
+
+ Map.put(opts, :tls_opts, tls_opts)
+ end
+
defp do_open(uri, %{proxy: {proxy_host, proxy_port}} = opts) do
connect_opts =
uri
@@ -77,20 +101,20 @@ defmodule Pleroma.Gun.Conn do
|> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
with open_opts <- Map.delete(opts, :tls_opts),
- {:ok, conn} <- API.open(proxy_host, proxy_port, open_opts),
- {:ok, _} <- API.await_up(conn, opts[:await_up_timeout]),
- stream <- API.connect(conn, connect_opts),
- {:response, :fin, 200, _} <- API.await(conn, stream) do
+ {:ok, conn} <- Gun.open(proxy_host, proxy_port, open_opts),
+ {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]),
+ stream <- Gun.connect(conn, connect_opts),
+ {:response, :fin, 200, _} <- Gun.await(conn, stream) do
conn
else
error ->
Logger.warn(
- "Received error on opening connection with http proxy #{
- Connections.compose_uri_log(uri)
- } #{inspect(error)}"
+ "Opening proxied connection to #{compose_uri_log(uri)} failed with error #{
+ inspect(error)
+ }"
)
- nil
+ error
end
end
@@ -115,41 +139,39 @@ defmodule Pleroma.Gun.Conn do
|> Map.put(:protocols, [:socks])
|> Map.put(:socks_opts, socks_opts)
- with {:ok, conn} <- API.open(proxy_host, proxy_port, opts),
- {:ok, _} <- API.await_up(conn, opts[:await_up_timeout]) do
+ with {:ok, conn} <- Gun.open(proxy_host, proxy_port, opts),
+ {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
conn
else
error ->
Logger.warn(
- "Received error on opening connection with socks proxy #{
- Connections.compose_uri_log(uri)
- } #{inspect(error)}"
+ "Opening socks proxied connection to #{compose_uri_log(uri)} failed with error #{
+ inspect(error)
+ }"
)
- nil
+ error
end
end
defp do_open(%URI{host: host, port: port} = uri, opts) do
- {_type, host} = Pleroma.HTTP.Adapter.domain_or_ip(host)
+ host = Pleroma.HTTP.Connection.parse_host(host)
- with {:ok, conn} <- API.open(host, port, opts),
- {:ok, _} <- API.await_up(conn, opts[:await_up_timeout]) do
+ with {:ok, conn} <- Gun.open(host, port, opts),
+ {:ok, _} <- Gun.await_up(conn, opts[:await_up_timeout]) do
conn
else
error ->
Logger.warn(
- "Received error on opening connection #{Connections.compose_uri_log(uri)} #{
- inspect(error)
- }"
+ "Opening connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
)
- nil
+ error
end
end
defp destination_opts(%URI{host: host, port: port}) do
- {_type, host} = Pleroma.HTTP.Adapter.domain_or_ip(host)
+ host = Pleroma.HTTP.Connection.parse_host(host)
%{host: host, port: port}
end
@@ -159,17 +181,18 @@ defmodule Pleroma.Gun.Conn do
defp add_http2_opts(opts, _, _), do: opts
- defp try_do_open(name, uri, opts) do
- Logger.debug("try to open conn #{Connections.compose_uri_log(uri)}")
-
- with [{close_key, least_used} | _conns] <-
- Connections.get_unused_conns(name),
- :ok <- Pleroma.Gun.API.close(least_used.conn) do
- Connections.remove_conn(name, close_key)
+ defp close_least_used_and_do_open(name, uri, opts) do
+ with [{key, conn} | _conns] <- Connections.get_unused_conns(name),
+ :ok <- Gun.close(conn.conn) do
+ Connections.remove_conn(name, key)
do_open(uri, opts)
else
- [] -> nil
+ [] -> {:error, :pool_overflowed}
end
end
+
+ def compose_uri_log(%URI{scheme: scheme, host: host, path: path}) do
+ "#{scheme}://#{host}#{path}"
+ end
end