2020-07-28 09:35:16 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-07-28 09:35:16 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-02 01:29:36 -06:00
|
|
|
defmodule Pleroma.Web.Plugs.FrontendStatic do
|
2020-07-28 09:35:16 -06:00
|
|
|
require Pleroma.Constants
|
|
|
|
|
2023-03-12 17:59:10 -06:00
|
|
|
@frontend_cookie_name "preferred_frontend"
|
|
|
|
|
2020-07-28 09:35:16 -06:00
|
|
|
@moduledoc """
|
|
|
|
This is a shim to call `Plug.Static` but with runtime `from` configuration`. It dispatches to the different frontends.
|
|
|
|
"""
|
|
|
|
@behaviour Plug
|
|
|
|
|
2023-03-12 17:59:10 -06:00
|
|
|
defp instance_static_path do
|
|
|
|
Pleroma.Config.get([:instance, :static_dir], "instance/static")
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_path(path, frontend_type \\ :primary)
|
2020-07-28 09:35:16 -06:00
|
|
|
|
2023-03-12 17:59:10 -06:00
|
|
|
def file_path(path, frontend_type) when is_atom(frontend_type) do
|
|
|
|
if configuration = Pleroma.Config.get([:frontends, frontend_type]) do
|
2020-07-28 09:35:16 -06:00
|
|
|
Path.join([
|
2023-03-12 17:59:10 -06:00
|
|
|
instance_static_path(),
|
2020-07-28 09:35:16 -06:00
|
|
|
"frontends",
|
|
|
|
configuration["name"],
|
|
|
|
configuration["ref"],
|
|
|
|
path
|
|
|
|
])
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 17:59:10 -06:00
|
|
|
def file_path(path, frontend_type) when is_binary(frontend_type) do
|
|
|
|
Path.join([
|
|
|
|
instance_static_path(),
|
|
|
|
"frontends",
|
|
|
|
frontend_type,
|
|
|
|
path
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2020-07-28 09:35:16 -06:00
|
|
|
def init(opts) do
|
|
|
|
opts
|
|
|
|
|> Keyword.put(:from, "__unconfigured_frontend_static_plug")
|
|
|
|
|> Plug.Static.init()
|
2020-07-29 05:02:48 -06:00
|
|
|
|> Map.put(:frontend_type, opts[:frontend_type])
|
2022-07-14 04:17:14 -06:00
|
|
|
|> Map.put(:if, Keyword.get(opts, :if, true))
|
2020-07-28 09:35:16 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, opts) do
|
2021-02-24 14:23:45 -07:00
|
|
|
with false <- api_route?(conn.path_info),
|
|
|
|
false <- invalid_path?(conn.path_info),
|
2022-07-14 04:17:14 -06:00
|
|
|
true <- enabled?(opts[:if]),
|
2023-03-12 17:59:10 -06:00
|
|
|
fallback_frontend_type <- Map.get(opts, :frontend_type, :primary),
|
|
|
|
frontend_type <- preferred_or_fallback(conn, fallback_frontend_type),
|
2020-10-27 13:58:55 -06:00
|
|
|
path when not is_nil(path) <- file_path("", frontend_type) do
|
|
|
|
call_static(conn, opts, path)
|
2020-07-28 09:35:16 -06:00
|
|
|
else
|
2020-10-27 13:58:55 -06:00
|
|
|
_ ->
|
|
|
|
conn
|
2020-07-28 09:35:16 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-12 17:59:10 -06:00
|
|
|
def preferred_frontend(conn) do
|
|
|
|
%{req_cookies: cookies} =
|
|
|
|
conn
|
|
|
|
|> Plug.Conn.fetch_cookies()
|
|
|
|
|
|
|
|
Map.get(cookies, @frontend_cookie_name)
|
|
|
|
end
|
|
|
|
|
2023-03-28 05:44:52 -06:00
|
|
|
# Only override primary frontend
|
|
|
|
def preferred_or_fallback(conn, :primary) do
|
2023-03-12 17:59:10 -06:00
|
|
|
case preferred_frontend(conn) do
|
|
|
|
nil ->
|
2023-03-28 05:44:52 -06:00
|
|
|
:primary
|
2023-03-12 17:59:10 -06:00
|
|
|
|
|
|
|
frontend ->
|
2023-04-14 10:42:40 -06:00
|
|
|
if Enum.member?(Pleroma.Config.get([:frontends, :pickable], []), frontend) do
|
|
|
|
frontend
|
|
|
|
else
|
|
|
|
:primary
|
|
|
|
end
|
2023-03-12 17:59:10 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-14 08:20:55 -06:00
|
|
|
def preferred_or_fallback(_conn, fallback), do: fallback
|
2023-03-28 05:44:52 -06:00
|
|
|
|
2022-07-14 04:17:14 -06:00
|
|
|
defp enabled?(if_opt) when is_function(if_opt), do: if_opt.()
|
|
|
|
defp enabled?(true), do: true
|
|
|
|
defp enabled?(_), do: false
|
|
|
|
|
2020-10-27 13:58:55 -06:00
|
|
|
defp invalid_path?(list) do
|
|
|
|
invalid_path?(list, :binary.compile_pattern(["/", "\\", ":", "\0"]))
|
|
|
|
end
|
2020-07-28 09:35:16 -06:00
|
|
|
|
2020-10-27 13:58:55 -06:00
|
|
|
defp invalid_path?([h | _], _match) when h in [".", "..", ""], do: true
|
|
|
|
defp invalid_path?([h | t], match), do: String.contains?(h, match) or invalid_path?(t)
|
|
|
|
defp invalid_path?([], _match), do: false
|
|
|
|
|
2021-02-25 09:07:29 -07:00
|
|
|
defp api_route?([]), do: false
|
2021-02-24 14:23:45 -07:00
|
|
|
|
2021-05-21 16:37:34 -06:00
|
|
|
defp api_route?([h | t]) do
|
|
|
|
api_routes = Pleroma.Web.Router.get_api_routes()
|
|
|
|
if h in api_routes, do: true, else: api_route?(t)
|
|
|
|
end
|
|
|
|
|
2020-10-27 13:58:55 -06:00
|
|
|
defp call_static(conn, opts, from) do
|
|
|
|
opts = Map.put(opts, :from, from)
|
2020-07-28 09:35:16 -06:00
|
|
|
Plug.Static.call(conn, opts)
|
|
|
|
end
|
|
|
|
end
|