pleroma/lib/pleroma/http/connection.ex

132 lines
3.4 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2018-12-31 16:41:47 +01:00
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.HTTP.Connection do
2018-12-04 15:48:55 +01:00
@moduledoc """
Connection for http-requests.
"""
2019-08-07 19:04:37 +02:00
@options [
connect_timeout: 10_000,
2019-08-19 16:43:06 +02:00
timeout: 20_000,
2019-08-25 14:05:34 +02:00
pool: :federation
]
2019-08-23 11:57:52 +02:00
require Logger
@doc """
Configure a client connection
# Returns
Tesla.Env.client
"""
@spec new(Keyword.t()) :: Tesla.Env.client()
def new(opts \\ []) do
2019-08-07 19:04:37 +02:00
middleware = [Tesla.Middleware.FollowRedirects]
adapter = Application.get_env(:tesla, :adapter)
2019-08-14 19:08:15 +02:00
Tesla.client(middleware, {adapter, options(opts)})
end
2019-08-07 19:04:37 +02:00
# fetch http options
#
2019-08-07 19:04:37 +02:00
def options(opts) do
options = Keyword.get(opts, :adapter, [])
adapter_options = Pleroma.Config.get([:http, :adapter], [])
2019-08-23 11:57:52 +02:00
2019-05-28 08:49:53 +02:00
proxy_url = Pleroma.Config.get([:http, :proxy_url], nil)
2019-08-23 11:57:52 +02:00
proxy =
case parse_proxy(proxy_url) do
{:ok, proxy_host, proxy_port} -> {proxy_host, proxy_port}
_ -> nil
end
2019-08-20 17:37:22 +02:00
options =
@options
|> Keyword.merge(adapter_options)
|> Keyword.merge(options)
2019-08-23 11:57:52 +02:00
|> Keyword.merge(proxy: proxy)
2019-08-20 17:37:22 +02:00
pool = options[:pool]
url = options[:url]
if not is_nil(url) and not is_nil(pool) and Pleroma.Gun.Connections.alive?(pool) do
get_conn_for_gun(url, options, pool)
else
options
end
end
defp get_conn_for_gun(url, options, pool) do
2019-08-25 14:05:34 +02:00
case Pleroma.Gun.Connections.checkin(url, options, pool) do
2019-08-20 17:37:22 +02:00
nil ->
options
conn ->
%{host: host, port: port} = URI.parse(url)
2019-08-22 11:38:39 +02:00
# verify sertificates opts for gun
tls_opts = [
verify: :verify_peer,
cacerts: :certifi.cacerts(),
depth: 20,
server_name_indication: to_charlist(host),
reuse_sessions: false,
verify_fun: {&:ssl_verify_hostname.verify_fun/3, [check_hostname: to_charlist(host)]}
]
2019-08-20 17:37:22 +02:00
Keyword.put(options, :conn, conn)
|> Keyword.put(:close_conn, false)
|> Keyword.put(:original, "#{host}:#{port}")
2019-08-22 11:38:39 +02:00
|> Keyword.put(:tls_opts, tls_opts)
2019-08-20 17:37:22 +02:00
end
end
2019-08-23 11:57:52 +02:00
@spec parse_proxy(String.t() | tuple() | nil) ::
{tuple, pos_integer()} | {:error, atom()} | nil
def parse_proxy(nil), do: nil
def parse_proxy(proxy) when is_binary(proxy) do
with [host, port] <- String.split(proxy, ":"),
{port, ""} <- Integer.parse(port) do
{:ok, parse_host(host), port}
else
{_, _} ->
Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
{:error, :error_parsing_port_in_proxy}
:error ->
Logger.warn("parsing port in proxy fail #{inspect(proxy)}")
{:error, :error_parsing_port_in_proxy}
_ ->
Logger.warn("parsing proxy fail #{inspect(proxy)}")
{:error, :error_parsing_proxy}
end
end
def parse_proxy(proxy) when is_tuple(proxy) do
with {_type, host, port} <- proxy do
{:ok, parse_host(host), port}
else
_ ->
Logger.warn("parsing proxy fail #{inspect(proxy)}")
{:error, :error_parsing_proxy}
end
end
@spec parse_host(String.t() | tuple()) :: charlist() | atom()
def parse_host(host) when is_atom(host), do: to_charlist(host)
def parse_host(host) when is_binary(host) do
host = to_charlist(host)
case :inet.parse_address(host) do
{:error, :einval} -> host
{:ok, ip} -> ip
end
end
end