2020-02-11 07:12:57 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-02-11 07:12:57 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Gun.Conn do
|
2020-03-03 16:24:14 +00:00
|
|
|
alias Pleroma.Gun
|
2020-02-24 16:56:27 +00:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2020-05-05 22:51:10 +00:00
|
|
|
def open(%URI{} = uri, opts) do
|
2020-02-24 16:56:27 +00:00
|
|
|
pool_opts = Pleroma.Config.get([:connections_pool], [])
|
|
|
|
|
|
|
|
opts =
|
|
|
|
opts
|
|
|
|
|> Enum.into(%{})
|
2020-09-04 16:05:08 +00:00
|
|
|
|> Map.put_new(:connect_timeout, pool_opts[:connect_timeout] || 5_000)
|
2020-05-05 22:51:10 +00:00
|
|
|
|> Map.put_new(:supervise, false)
|
2020-03-10 12:54:11 +00:00
|
|
|
|> maybe_add_tls_opts(uri)
|
2020-02-24 16:56:27 +00:00
|
|
|
|
2020-05-05 22:51:10 +00:00
|
|
|
do_open(uri, opts)
|
2020-02-24 16:56:27 +00:00
|
|
|
end
|
|
|
|
|
2020-03-10 12:54:11 +00:00
|
|
|
defp maybe_add_tls_opts(opts, %URI{scheme: "http"}), do: opts
|
|
|
|
|
2020-07-08 14:02:57 +00:00
|
|
|
defp maybe_add_tls_opts(opts, %URI{scheme: "https"}) do
|
2020-03-10 12:54:11 +00:00
|
|
|
tls_opts = [
|
|
|
|
verify: :verify_peer,
|
|
|
|
cacertfile: CAStore.file_path(),
|
|
|
|
depth: 20,
|
|
|
|
reuse_sessions: false,
|
2020-07-08 13:12:09 +00:00
|
|
|
log_level: :warning,
|
|
|
|
customize_hostname_check: [match_fun: :public_key.pkix_verify_hostname_match_fun(:https)]
|
2020-03-10 12:54:11 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-02-24 16:56:27 +00:00
|
|
|
defp do_open(uri, %{proxy: {proxy_host, proxy_port}} = opts) do
|
|
|
|
connect_opts =
|
|
|
|
uri
|
|
|
|
|> destination_opts()
|
|
|
|
|> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
|
|
|
|
|
|
|
|
with open_opts <- Map.delete(opts, :tls_opts),
|
2020-03-03 16:24:14 +00:00
|
|
|
{:ok, conn} <- Gun.open(proxy_host, proxy_port, open_opts),
|
2020-09-11 11:22:54 +00:00
|
|
|
{:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]),
|
2020-03-03 16:24:14 +00:00
|
|
|
stream <- Gun.connect(conn, connect_opts),
|
|
|
|
{:response, :fin, 200, _} <- Gun.await(conn, stream) do
|
2020-09-11 11:22:54 +00:00
|
|
|
{:ok, conn, protocol}
|
2020-02-24 16:56:27 +00:00
|
|
|
else
|
|
|
|
error ->
|
2023-11-28 17:17:46 +00:00
|
|
|
Logger.warning(
|
2021-10-06 06:08:21 +00:00
|
|
|
"Opening proxied connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
|
2020-02-24 16:56:27 +00:00
|
|
|
)
|
|
|
|
|
2020-03-03 13:27:46 +00:00
|
|
|
error
|
2020-02-24 16:56:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_open(uri, %{proxy: {proxy_type, proxy_host, proxy_port}} = opts) do
|
|
|
|
version =
|
|
|
|
proxy_type
|
|
|
|
|> to_string()
|
|
|
|
|> String.last()
|
|
|
|
|> case do
|
|
|
|
"4" -> 4
|
|
|
|
_ -> 5
|
|
|
|
end
|
|
|
|
|
|
|
|
socks_opts =
|
|
|
|
uri
|
|
|
|
|> destination_opts()
|
|
|
|
|> add_http2_opts(uri.scheme, Map.get(opts, :tls_opts, []))
|
|
|
|
|> Map.put(:version, version)
|
|
|
|
|
|
|
|
opts =
|
|
|
|
opts
|
|
|
|
|> Map.put(:protocols, [:socks])
|
|
|
|
|> Map.put(:socks_opts, socks_opts)
|
|
|
|
|
2020-03-03 16:24:14 +00:00
|
|
|
with {:ok, conn} <- Gun.open(proxy_host, proxy_port, opts),
|
2020-09-11 11:22:54 +00:00
|
|
|
{:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]) do
|
|
|
|
{:ok, conn, protocol}
|
2020-02-24 16:56:27 +00:00
|
|
|
else
|
|
|
|
error ->
|
2023-11-28 17:17:46 +00:00
|
|
|
Logger.warning(
|
2021-10-06 06:08:21 +00:00
|
|
|
"Opening socks proxied connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
|
2020-02-24 16:56:27 +00:00
|
|
|
)
|
|
|
|
|
2020-03-03 13:27:46 +00:00
|
|
|
error
|
2020-02-24 16:56:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_open(%URI{host: host, port: port} = uri, opts) do
|
2020-05-05 22:51:10 +00:00
|
|
|
host = Pleroma.HTTP.AdapterHelper.parse_host(host)
|
2020-02-24 16:56:27 +00:00
|
|
|
|
2020-03-03 16:24:14 +00:00
|
|
|
with {:ok, conn} <- Gun.open(host, port, opts),
|
2020-09-11 11:22:54 +00:00
|
|
|
{:ok, protocol} <- Gun.await_up(conn, opts[:connect_timeout]) do
|
|
|
|
{:ok, conn, protocol}
|
2020-02-24 16:56:27 +00:00
|
|
|
else
|
|
|
|
error ->
|
2023-11-28 17:17:46 +00:00
|
|
|
Logger.warning(
|
2020-03-12 15:28:54 +00:00
|
|
|
"Opening connection to #{compose_uri_log(uri)} failed with error #{inspect(error)}"
|
2020-02-24 16:56:27 +00:00
|
|
|
)
|
|
|
|
|
2020-03-03 13:27:46 +00:00
|
|
|
error
|
2020-02-24 16:56:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp destination_opts(%URI{host: host, port: port}) do
|
2020-05-05 22:51:10 +00:00
|
|
|
host = Pleroma.HTTP.AdapterHelper.parse_host(host)
|
2020-02-24 16:56:27 +00:00
|
|
|
%{host: host, port: port}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp add_http2_opts(opts, "https", tls_opts) do
|
|
|
|
Map.merge(opts, %{protocols: [:http2], transport: :tls, tls_opts: tls_opts})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp add_http2_opts(opts, _, _), do: opts
|
|
|
|
|
2020-03-12 15:28:54 +00:00
|
|
|
def compose_uri_log(%URI{scheme: scheme, host: host, path: path}) do
|
|
|
|
"#{scheme}://#{host}#{path}"
|
|
|
|
end
|
2020-02-11 07:12:57 +00:00
|
|
|
end
|