2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-17 13:39:59 +00:00
|
|
|
defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
|
|
|
|
require Logger
|
|
|
|
|
2019-02-09 15:16:26 +00:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-03-05 02:52:23 +00:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2019-09-16 10:03:37 +00:00
|
|
|
alias Pleroma.Web.Streamer
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2019-02-28 15:17:01 +00:00
|
|
|
@behaviour :cowboy_websocket
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2020-05-11 14:28:53 +00:00
|
|
|
# Client ping period.
|
|
|
|
@tick :timer.seconds(30)
|
2020-05-07 09:13:32 +00:00
|
|
|
# Cowboy timeout period.
|
2020-05-11 14:28:53 +00:00
|
|
|
@timeout :timer.seconds(60)
|
2020-05-07 09:13:32 +00:00
|
|
|
# Hibernate every X messages
|
|
|
|
@hibernate_every 100
|
2024-04-04 15:19:58 +00:00
|
|
|
# Tune garabge collect for long-lived websocket process
|
|
|
|
@fullsweep_after 20
|
2020-05-07 09:13:32 +00:00
|
|
|
|
2019-02-28 16:23:24 +00:00
|
|
|
def init(%{qs: qs} = req, state) do
|
2020-05-12 16:04:47 +00:00
|
|
|
with params <- Enum.into(:cow_qs.parse_qs(qs), %{}),
|
2019-07-06 18:26:08 +00:00
|
|
|
sec_websocket <- :cowboy_req.header("sec-websocket-protocol", req, nil),
|
2020-05-12 16:04:47 +00:00
|
|
|
access_token <- Map.get(params, "access_token"),
|
2020-09-19 16:16:55 +00:00
|
|
|
{:ok, user, oauth_token} <- authenticate_request(access_token, sec_websocket),
|
|
|
|
{:ok, topic} <- Streamer.get_topic(params["stream"], user, oauth_token, params) do
|
2019-10-15 10:10:22 +00:00
|
|
|
req =
|
2019-10-18 04:36:37 +00:00
|
|
|
if sec_websocket do
|
2019-10-15 10:10:22 +00:00
|
|
|
:cowboy_req.set_resp_header("sec-websocket-protocol", sec_websocket, req)
|
|
|
|
else
|
|
|
|
req
|
|
|
|
end
|
|
|
|
|
2022-08-17 00:22:59 +00:00
|
|
|
{:cowboy_websocket, req,
|
|
|
|
%{
|
|
|
|
user: user,
|
|
|
|
topic: topic,
|
|
|
|
count: 0,
|
|
|
|
timer: nil,
|
|
|
|
subscriptions: [],
|
|
|
|
oauth_token: oauth_token
|
|
|
|
}, %{idle_timeout: @timeout}}
|
2018-12-17 13:39:59 +00:00
|
|
|
else
|
2020-05-12 16:04:47 +00:00
|
|
|
{:error, :bad_topic} ->
|
|
|
|
Logger.debug("#{__MODULE__} bad topic #{inspect(req)}")
|
2020-09-10 18:26:52 +00:00
|
|
|
req = :cowboy_req.reply(404, req)
|
2019-02-28 16:23:55 +00:00
|
|
|
{:ok, req, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2020-05-12 16:04:47 +00:00
|
|
|
{:error, :unauthorized} ->
|
|
|
|
Logger.debug("#{__MODULE__} authentication error: #{inspect(req)}")
|
2020-09-10 18:26:52 +00:00
|
|
|
req = :cowboy_req.reply(401, req)
|
2019-02-28 16:23:55 +00:00
|
|
|
{:ok, req, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-28 15:17:01 +00:00
|
|
|
def websocket_init(state) do
|
2020-05-07 09:13:32 +00:00
|
|
|
Logger.debug(
|
2021-10-06 06:08:21 +00:00
|
|
|
"#{__MODULE__} accepted websocket connection for user #{(state.user || %{id: "anonymous"}).id}, topic #{state.topic}"
|
2020-05-07 09:13:32 +00:00
|
|
|
)
|
|
|
|
|
2024-04-04 15:19:58 +00:00
|
|
|
# process is long-lived and can sometimes accumulate stale data in such a way it's
|
|
|
|
# not freed by young garbage cycles, thus make full collection sweeps more frequent
|
|
|
|
:erlang.process_flag(:fullsweep_after, @fullsweep_after)
|
|
|
|
|
2022-08-19 17:19:38 +00:00
|
|
|
Streamer.add_socket(state.topic, state.oauth_token)
|
2020-05-11 14:28:53 +00:00
|
|
|
{:ok, %{state | timer: timer()}}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Client's Pong frame.
|
|
|
|
def websocket_handle(:pong, state) do
|
|
|
|
if state.timer, do: Process.cancel_timer(state.timer)
|
|
|
|
{:ok, %{state | timer: timer()}}
|
2019-02-28 15:17:01 +00:00
|
|
|
end
|
|
|
|
|
2020-09-10 10:47:53 +00:00
|
|
|
# We only receive pings for now
|
|
|
|
def websocket_handle(:ping, state), do: {:ok, state}
|
|
|
|
|
2022-08-17 00:22:59 +00:00
|
|
|
def websocket_handle({:text, ping}, state) when ping in ~w[ping PING] do
|
2022-07-22 14:55:38 +00:00
|
|
|
if state.timer, do: Process.cancel_timer(state.timer)
|
|
|
|
{:reply, {:text, "pong"}, %{state | timer: timer()}}
|
|
|
|
end
|
|
|
|
|
2022-08-17 00:22:59 +00:00
|
|
|
def websocket_handle({:text, text}, state) do
|
|
|
|
with {:ok, json} <- Jason.decode(text) do
|
|
|
|
websocket_handle({:json, json}, state)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.error("#{__MODULE__} received text frame: #{text}")
|
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def websocket_handle(
|
|
|
|
{:json, %{"type" => "subscribe", "stream" => stream_name}},
|
|
|
|
%{user: user, oauth_token: token} = state
|
|
|
|
) do
|
|
|
|
with {:ok, topic} <- Streamer.get_topic(stream_name, user, token, %{}) do
|
|
|
|
new_subscriptions =
|
|
|
|
[topic | Map.get(state, :subscriptions, [])]
|
|
|
|
|> Enum.uniq()
|
|
|
|
|
|
|
|
{:ok, _topic} = Streamer.add_socket(topic, user)
|
|
|
|
|
|
|
|
{:ok, Map.put(state, :subscriptions, new_subscriptions)}
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.error("#{__MODULE__} received invalid topic: #{stream_name}")
|
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-11 14:28:53 +00:00
|
|
|
def websocket_handle(frame, state) do
|
|
|
|
Logger.error("#{__MODULE__} received frame: #{inspect(frame)}")
|
2019-02-28 15:17:01 +00:00
|
|
|
{:ok, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2022-08-17 00:22:59 +00:00
|
|
|
def websocket_info({:render_with_user, view, template, item, topic}, state) do
|
2020-05-07 09:13:32 +00:00
|
|
|
user = %User{} = User.get_cached_by_ap_id(state.user.ap_id)
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2020-05-07 09:13:32 +00:00
|
|
|
unless Streamer.filtered_by_user?(user, item) do
|
2022-08-17 00:22:59 +00:00
|
|
|
websocket_info({:text, view.render(template, item, user, topic)}, %{state | user: user})
|
2020-05-07 09:13:32 +00:00
|
|
|
else
|
|
|
|
{:ok, state}
|
|
|
|
end
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2019-02-28 15:17:01 +00:00
|
|
|
def websocket_info({:text, message}, state) do
|
2020-05-07 09:13:32 +00:00
|
|
|
# If the websocket processed X messages, force an hibernate/GC.
|
|
|
|
# We don't hibernate at every message to balance CPU usage/latency with RAM usage.
|
|
|
|
if state.count > @hibernate_every do
|
|
|
|
{:reply, {:text, message}, %{state | count: 0}, :hibernate}
|
|
|
|
else
|
|
|
|
{:reply, {:text, message}, %{state | count: state.count + 1}}
|
|
|
|
end
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2020-05-11 14:28:53 +00:00
|
|
|
# Ping tick. We don't re-queue a timer there, it is instead queued when :pong is received.
|
|
|
|
# As we hibernate there, reset the count to 0.
|
|
|
|
# If the client misses :pong, Cowboy will automatically timeout the connection after
|
|
|
|
# `@idle_timeout`.
|
|
|
|
def websocket_info(:tick, state) do
|
|
|
|
{:reply, :ping, %{state | timer: nil, count: 0}, :hibernate}
|
|
|
|
end
|
|
|
|
|
2022-08-19 17:19:38 +00:00
|
|
|
def websocket_info(:close, state) do
|
|
|
|
{:stop, state}
|
|
|
|
end
|
|
|
|
|
2020-09-10 10:48:24 +00:00
|
|
|
# State can be `[]` only in case we terminate before switching to websocket,
|
|
|
|
# we already log errors for these cases in `init/1`, so just do nothing here
|
|
|
|
def terminate(_reason, _req, []), do: :ok
|
|
|
|
|
2019-02-28 15:17:01 +00:00
|
|
|
def terminate(reason, _req, state) do
|
2018-12-17 13:39:59 +00:00
|
|
|
Logger.debug(
|
2021-10-06 06:08:21 +00:00
|
|
|
"#{__MODULE__} terminating websocket connection for user #{(state.user || %{id: "anonymous"}).id}, topic #{state.topic || "?"}: #{inspect(reason)}"
|
2018-12-17 13:39:59 +00:00
|
|
|
)
|
|
|
|
|
2020-05-07 09:13:32 +00:00
|
|
|
Streamer.remove_socket(state.topic)
|
2018-12-17 13:39:59 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# Public streams without authentication.
|
2020-05-12 16:04:47 +00:00
|
|
|
defp authenticate_request(nil, nil) do
|
2020-09-19 16:16:55 +00:00
|
|
|
{:ok, nil, nil}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticated streams.
|
2020-05-12 16:04:47 +00:00
|
|
|
defp authenticate_request(access_token, sec_websocket) do
|
|
|
|
token = access_token || sec_websocket
|
2019-07-06 18:26:08 +00:00
|
|
|
|
|
|
|
with true <- is_bitstring(token),
|
2020-09-19 16:16:55 +00:00
|
|
|
oauth_token = %Token{user_id: user_id} <- Repo.get_by(Token, token: token),
|
2019-04-22 07:20:43 +00:00
|
|
|
user = %User{} <- User.get_cached_by_id(user_id) do
|
2020-09-19 16:16:55 +00:00
|
|
|
{:ok, user, oauth_token}
|
2018-12-17 13:39:59 +00:00
|
|
|
else
|
2020-05-12 16:04:47 +00:00
|
|
|
_ -> {:error, :unauthorized}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-11 14:28:53 +00:00
|
|
|
defp timer do
|
|
|
|
Process.send_after(self(), :tick, @tick)
|
|
|
|
end
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|