2020-02-11 00:12:57 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-02-11 00:12:57 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-03-03 08:53:44 -07:00
|
|
|
defmodule Pleroma.HTTP.AdapterHelper do
|
2020-05-05 16:51:10 -06:00
|
|
|
@moduledoc """
|
|
|
|
Configure Tesla.Client with default and customized adapter options.
|
|
|
|
"""
|
2022-07-04 10:30:38 -06:00
|
|
|
@defaults [name: MyFinch, connect_timeout: 5_000, recv_timeout: 5_000]
|
2020-05-05 16:51:10 -06:00
|
|
|
|
|
|
|
@type proxy_type() :: :socks4 | :socks5
|
2020-06-16 07:20:28 -06:00
|
|
|
@type host() :: charlist() | :inet.ip_address()
|
2020-05-05 16:51:10 -06:00
|
|
|
|
|
|
|
alias Pleroma.HTTP.AdapterHelper
|
|
|
|
require Logger
|
2020-02-11 00:12:57 -07:00
|
|
|
|
|
|
|
@type proxy ::
|
|
|
|
{Connection.host(), pos_integer()}
|
2020-03-12 09:28:54 -06:00
|
|
|
| {Connection.proxy_type(), Connection.host(), pos_integer()}
|
2020-02-11 00:12:57 -07:00
|
|
|
|
|
|
|
@callback options(keyword(), URI.t()) :: keyword()
|
|
|
|
|
|
|
|
@spec format_proxy(String.t() | tuple() | nil) :: proxy() | nil
|
|
|
|
def format_proxy(nil), do: nil
|
|
|
|
|
|
|
|
def format_proxy(proxy_url) do
|
2020-05-05 16:51:10 -06:00
|
|
|
case parse_proxy(proxy_url) do
|
2020-03-03 05:44:13 -07:00
|
|
|
{:ok, host, port} -> {host, port}
|
2020-02-11 00:12:57 -07:00
|
|
|
{:ok, type, host, port} -> {type, host, port}
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec maybe_add_proxy(keyword(), proxy() | nil) :: keyword()
|
|
|
|
def maybe_add_proxy(opts, nil), do: opts
|
|
|
|
def maybe_add_proxy(opts, proxy), do: Keyword.put_new(opts, :proxy, proxy)
|
2020-05-05 16:51:10 -06:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Merge default connection & adapter options with received ones.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@spec options(URI.t(), keyword()) :: keyword()
|
|
|
|
def options(%URI{} = uri, opts \\ []) do
|
|
|
|
@defaults
|
|
|
|
|> Keyword.merge(opts)
|
2022-07-04 10:30:38 -06:00
|
|
|
|> AdapterHelper.Default.options(uri)
|
2020-05-05 16:51:10 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
@spec parse_proxy(String.t() | tuple() | nil) ::
|
|
|
|
{:ok, host(), pos_integer()}
|
|
|
|
| {:ok, proxy_type(), host(), 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 failed #{inspect(proxy)}")
|
|
|
|
{:error, :invalid_proxy_port}
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
Logger.warn("Parsing port failed #{inspect(proxy)}")
|
|
|
|
{:error, :invalid_proxy_port}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
Logger.warn("Parsing proxy failed #{inspect(proxy)}")
|
|
|
|
{:error, :invalid_proxy}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_proxy(proxy) when is_tuple(proxy) do
|
|
|
|
with {type, host, port} <- proxy do
|
|
|
|
{:ok, type, parse_host(host), port}
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.warn("Parsing proxy failed #{inspect(proxy)}")
|
|
|
|
{:error, :invalid_proxy}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-16 07:20:28 -06:00
|
|
|
@spec parse_host(String.t() | atom() | charlist()) :: charlist() | :inet.ip_address()
|
2020-05-05 16:51:10 -06:00
|
|
|
def parse_host(host) when is_list(host), do: host
|
|
|
|
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
|
|
|
|
|
|
|
|
@spec format_host(String.t()) :: charlist()
|
|
|
|
def format_host(host) do
|
|
|
|
host_charlist = to_charlist(host)
|
|
|
|
|
|
|
|
case :inet.parse_address(host_charlist) do
|
|
|
|
{:error, :einval} ->
|
|
|
|
:idna.encode(host_charlist)
|
|
|
|
|
|
|
|
{:ok, _ip} ->
|
|
|
|
host_charlist
|
|
|
|
end
|
|
|
|
end
|
2020-02-11 00:12:57 -07:00
|
|
|
end
|