2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 08:41:47 -07:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-11-22 11:06:07 -07:00
|
|
|
defmodule Pleroma.Web.MediaProxy.MediaProxyController do
|
|
|
|
use Pleroma.Web, :controller
|
2018-11-23 09:40:45 -07:00
|
|
|
alias Pleroma.{Web.MediaProxy, ReverseProxy}
|
2017-11-22 11:06:07 -07:00
|
|
|
|
2018-12-07 11:36:44 -07:00
|
|
|
@default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]]
|
2018-11-30 09:44:42 -07:00
|
|
|
|
2018-11-23 09:40:45 -07:00
|
|
|
def remote(conn, params = %{"sig" => sig64, "url" => url64}) do
|
2018-12-02 03:24:02 -07:00
|
|
|
with config <- Pleroma.Config.get([:media_proxy], []),
|
2018-11-23 09:40:45 -07:00
|
|
|
true <- Keyword.get(config, :enabled, false),
|
|
|
|
{:ok, url} <- MediaProxy.decode_url(sig64, url64),
|
|
|
|
:ok <- filename_matches(Map.has_key?(params, "filename"), conn.request_path, url) do
|
2018-12-02 03:24:02 -07:00
|
|
|
ReverseProxy.call(conn, url, Keyword.get(config, :proxy_opts, @default_proxy_opts))
|
2017-11-28 13:44:25 -07:00
|
|
|
else
|
2018-03-30 07:01:53 -06:00
|
|
|
false ->
|
2018-11-23 09:40:45 -07:00
|
|
|
send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404))
|
2018-03-30 07:01:53 -06:00
|
|
|
|
|
|
|
{:error, :invalid_signature} ->
|
2018-11-23 09:40:45 -07:00
|
|
|
send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403))
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2018-11-23 09:40:45 -07:00
|
|
|
{:wrong_filename, filename} ->
|
|
|
|
redirect(conn, external: MediaProxy.build_url(sig64, url64, filename))
|
2017-11-22 11:06:07 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-23 09:40:45 -07:00
|
|
|
def filename_matches(has_filename, path, url) do
|
2018-12-07 13:44:04 -07:00
|
|
|
filename =
|
|
|
|
url
|
|
|
|
|> MediaProxy.filename()
|
|
|
|
|> URI.decode()
|
|
|
|
|
|
|
|
path = URI.decode(path)
|
2018-03-30 07:01:53 -06:00
|
|
|
|
2018-12-09 23:39:57 -07:00
|
|
|
if has_filename && filename && Path.basename(path) != filename do
|
|
|
|
{:wrong_filename, filename}
|
|
|
|
else
|
|
|
|
:ok
|
2017-12-10 18:31:37 -07:00
|
|
|
end
|
|
|
|
end
|
2017-11-22 11:06:07 -07:00
|
|
|
end
|