X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Freverse_proxy.ex;h=91cf1bba33006f9947421e5799ad869144cf68de;hb=77ed5fc67453f1030de39ce224b48f1846399397;hp=8ae1157df1630f41e90c647de7028c6bcc27ad7a;hpb=409f694e4f90d34285b43c7e7afc594bc386d893;p=akkoma
diff --git a/lib/pleroma/reverse_proxy.ex b/lib/pleroma/reverse_proxy.ex
index 8ae1157df..91cf1bba3 100644
--- a/lib/pleroma/reverse_proxy.ex
+++ b/lib/pleroma/reverse_proxy.ex
@@ -1,15 +1,15 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors
+# Copyright © 2017-2021 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.ReverseProxy do
@range_headers ~w(range if-range)
- @keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since) ++
+ @keep_req_headers ~w(accept accept-encoding cache-control if-modified-since) ++
~w(if-unmodified-since if-none-match) ++ @range_headers
@resp_cache_headers ~w(etag date last-modified)
@keep_resp_headers @resp_cache_headers ++
~w(content-length content-type content-disposition content-encoding) ++
- ~w(content-range accept-ranges vary)
+ ~w(content-range accept-ranges vary expires)
@default_cache_control_header "public, max-age=1209600"
@valid_resp_codes [200, 206, 304]
@max_read_duration :timer.seconds(30)
@@ -17,6 +17,8 @@ defmodule Pleroma.ReverseProxy do
@failed_request_ttl :timer.seconds(60)
@methods ~w(GET HEAD)
+ @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
+
def max_read_duration_default, do: @max_read_duration
def default_cache_control_header, do: @default_cache_control_header
@@ -55,16 +57,9 @@ defmodule Pleroma.ReverseProxy do
* `false` will add `content-disposition: attachment` to any request,
* a list of whitelisted content types
- * `keep_user_agent` will forward the client's user-agent to the upstream. This may be useful if the upstream is
- doing content transformation (encoding, â¦) depending on the request.
-
* `req_headers`, `resp_headers` additional headers.
- * `http`: options for [hackney](https://github.com/benoitc/hackney) or [gun](https://github.com/ninenines/gun).
-
"""
- @default_options [pool: :media]
-
@inline_content_types [
"image/gif",
"image/jpeg",
@@ -82,8 +77,7 @@ defmodule Pleroma.ReverseProxy do
import Plug.Conn
@type option() ::
- {:keep_user_agent, boolean}
- | {:max_read_duration, :timer.time() | :infinity}
+ {:max_read_duration, :timer.time() | :infinity}
| {:max_body_length, non_neg_integer() | :infinity}
| {:failed_request_ttl, :timer.time() | :infinity}
| {:http, []}
@@ -96,7 +90,7 @@ defmodule Pleroma.ReverseProxy do
def call(_conn, _url, _opts \\ [])
def call(conn = %{method: method}, url, opts) when method in @methods do
- client_opts = Keyword.merge(@default_options, Keyword.get(opts, :http, []))
+ client_opts = Keyword.get(opts, :http, [])
req_headers = build_req_headers(conn.req_headers, opts)
@@ -107,33 +101,41 @@ defmodule Pleroma.ReverseProxy do
opts
end
- with {:ok, nil} <- Cachex.get(:failed_proxy_url_cache, url),
- {:ok, code, headers, client} <- request(method, url, req_headers, client_opts),
+ with {:ok, nil} <- @cachex.get(:failed_proxy_url_cache, url),
+ {:ok, status, headers, body} <- request(method, url, req_headers, client_opts),
:ok <-
header_length_constraint(
headers,
Keyword.get(opts, :max_body_length, @max_body_length)
) do
- response(conn, client, url, code, headers, opts)
+ conn
+ |> put_private(:proxied_url, url)
+ |> response(body, status, headers, opts)
else
{:ok, true} ->
conn
- |> error_or_redirect(url, 500, "Request failed", opts)
+ |> put_private(:proxied_url, url)
+ |> error_or_redirect(500, "Request failed", opts)
|> halt()
- {:ok, code, headers} ->
- head_response(conn, url, code, headers, opts)
+ {:ok, status, headers} ->
+ conn
+ |> put_private(:proxied_url, url)
+ |> head_response(status, headers, opts)
|> halt()
- {:error, {:invalid_http_response, code}} ->
- Logger.error("#{__MODULE__}: request to #{inspect(url)} failed with HTTP status #{code}")
- track_failed_url(url, code, opts)
+ {:error, {:invalid_http_response, status}} ->
+ Logger.error(
+ "#{__MODULE__}: request to #{inspect(url)} failed with HTTP status #{status}"
+ )
+
+ track_failed_url(url, status, opts)
conn
+ |> put_private(:proxied_url, url)
|> error_or_redirect(
- url,
- code,
- "Request failed: " <> Plug.Conn.Status.reason_phrase(code),
+ status,
+ "Request failed: " <> Plug.Conn.Status.reason_phrase(status),
opts
)
|> halt()
@@ -143,7 +145,8 @@ defmodule Pleroma.ReverseProxy do
track_failed_url(url, error, opts)
conn
- |> error_or_redirect(url, 500, "Request failed", opts)
+ |> put_private(:proxied_url, url)
+ |> error_or_redirect(500, "Request failed", opts)
|> halt()
end
end
@@ -158,93 +161,48 @@ defmodule Pleroma.ReverseProxy do
Logger.debug("#{__MODULE__} #{method} #{url} #{inspect(headers)}")
method = method |> String.downcase() |> String.to_existing_atom()
- case client().request(method, url, headers, "", opts) do
- {:ok, code, headers, client} when code in @valid_resp_codes ->
- {:ok, code, downcase_headers(headers), client}
+ opts = opts ++ [receive_timeout: @max_read_duration]
- {:ok, code, headers} when code in @valid_resp_codes ->
- {:ok, code, downcase_headers(headers)}
+ case Pleroma.HTTP.request(method, url, "", headers, opts) do
+ {:ok, %Tesla.Env{status: status, headers: headers, body: body}}
+ when status in @valid_resp_codes ->
+ {:ok, status, downcase_headers(headers), body}
- {:ok, code, _, _} ->
- {:error, {:invalid_http_response, code}}
+ {:ok, %Tesla.Env{status: status, headers: headers}} when status in @valid_resp_codes ->
+ {:ok, status, downcase_headers(headers)}
- {:ok, code, _} ->
- {:error, {:invalid_http_response, code}}
+ {:ok, %Tesla.Env{status: status}} ->
+ {:error, {:invalid_http_response, status}}
{:error, error} ->
{:error, error}
end
end
- defp response(conn, client, url, status, headers, opts) do
- Logger.debug("#{__MODULE__} #{status} #{url} #{inspect(headers)}")
-
- result =
- conn
- |> put_resp_headers(build_resp_headers(headers, opts))
- |> send_chunked(status)
- |> chunk_reply(client, opts)
-
- case result do
- {:ok, conn} ->
- halt(conn)
-
- {:error, :closed, conn} ->
- client().close(client)
- halt(conn)
-
- {:error, error, conn} ->
- Logger.warn(
- "#{__MODULE__} request to #{url} failed while reading/chunking: #{inspect(error)}"
- )
-
- client().close(client)
- halt(conn)
- end
- end
-
- defp chunk_reply(conn, client, opts) do
- chunk_reply(conn, client, opts, 0, 0)
- end
+ defp response(conn, body, status, headers, opts) do
+ Logger.debug("#{__MODULE__} #{status} #{conn.private[:proxied_url]} #{inspect(headers)}")
- defp chunk_reply(conn, client, opts, sent_so_far, duration) do
- with {:ok, duration} <-
- check_read_duration(
- duration,
- Keyword.get(opts, :max_read_duration, @max_read_duration)
- ),
- {:ok, data, client} <- client().stream_body(client),
- {:ok, duration} <- increase_read_duration(duration),
- sent_so_far = sent_so_far + byte_size(data),
- :ok <-
- body_size_constraint(
- sent_so_far,
- Keyword.get(opts, :max_body_length, @max_body_length)
- ),
- {:ok, conn} <- chunk(conn, data) do
- chunk_reply(conn, client, opts, sent_so_far, duration)
- else
- :done -> {:ok, conn}
- {:error, error} -> {:error, error, conn}
- end
+ conn
+ |> put_resp_headers(build_resp_headers(headers, opts))
+ |> send_resp(status, body)
end
- defp head_response(conn, url, code, headers, opts) do
- Logger.debug("#{__MODULE__} #{code} #{url} #{inspect(headers)}")
+ defp head_response(conn, status, headers, opts) do
+ Logger.debug("#{__MODULE__} #{status} #{conn.private[:proxied_url]} #{inspect(headers)}")
conn
|> put_resp_headers(build_resp_headers(headers, opts))
- |> send_resp(code, "")
+ |> send_resp(status, "")
end
- defp error_or_redirect(conn, url, code, body, opts) do
+ defp error_or_redirect(conn, status, body, opts) do
if Keyword.get(opts, :redirect_on_failure, false) do
conn
- |> Phoenix.Controller.redirect(external: url)
+ |> Phoenix.Controller.redirect(external: conn.private[:proxied_url])
|> halt()
else
conn
- |> send_resp(code, body)
+ |> send_resp(status, body)
|> halt
end
end
@@ -274,7 +232,6 @@ defmodule Pleroma.ReverseProxy do
|> downcase_headers()
|> Enum.filter(fn {k, _} -> k in @keep_req_headers end)
|> build_req_range_or_encoding_header(opts)
- |> build_req_user_agent_header(opts)
|> Keyword.merge(Keyword.get(opts, :req_headers, []))
end
@@ -289,19 +246,6 @@ defmodule Pleroma.ReverseProxy do
end
end
- defp build_req_user_agent_header(headers, opts) do
- if Keyword.get(opts, :keep_user_agent, false) do
- List.keystore(
- headers,
- "user-agent",
- 0,
- {"user-agent", Pleroma.Application.user_agent()}
- )
- else
- headers
- end
- end
-
defp build_resp_headers(headers, opts) do
headers
|> Enum.filter(fn {k, _} -> k in @keep_resp_headers end)
@@ -388,37 +332,6 @@ defmodule Pleroma.ReverseProxy do
defp header_length_constraint(_, _), do: :ok
- defp body_size_constraint(size, limit) when is_integer(limit) and limit > 0 and size >= limit do
- {:error, :body_too_large}
- end
-
- defp body_size_constraint(_, _), do: :ok
-
- defp check_read_duration(nil = _duration, max), do: check_read_duration(@max_read_duration, max)
-
- defp check_read_duration(duration, max)
- when is_integer(duration) and is_integer(max) and max > 0 do
- if duration > max do
- {:error, :read_duration_exceeded}
- else
- {:ok, {duration, :erlang.system_time(:millisecond)}}
- end
- end
-
- defp check_read_duration(_, _), do: {:ok, :no_duration_limit, :no_duration_limit}
-
- defp increase_read_duration({previous_duration, started})
- when is_integer(previous_duration) and is_integer(started) do
- duration = :erlang.system_time(:millisecond) - started
- {:ok, previous_duration + duration}
- end
-
- defp increase_read_duration(_) do
- {:ok, :no_duration_limit, :no_duration_limit}
- end
-
- defp client, do: Pleroma.ReverseProxy.Client
-
defp track_failed_url(url, error, opts) do
ttl =
unless error in [:body_too_large, 400, 204] do
@@ -427,6 +340,6 @@ defmodule Pleroma.ReverseProxy do
nil
end
- Cachex.put(:failed_proxy_url_cache, url, true, ttl: ttl)
+ @cachex.put(:failed_proxy_url_cache, url, true, ttl: ttl)
end
end