2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-17 06:39:59 -07:00
|
|
|
defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
|
|
|
|
require Logger
|
|
|
|
|
2019-02-09 08:16:26 -07:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2019-09-16 04:03:37 -06:00
|
|
|
alias Pleroma.Web.Streamer
|
2023-03-31 20:55:52 -06:00
|
|
|
alias Pleroma.Web.StreamerView
|
2018-12-17 06:39:59 -07:00
|
|
|
|
2019-02-28 08:17:01 -07:00
|
|
|
@behaviour :cowboy_websocket
|
2018-12-17 06:39:59 -07:00
|
|
|
|
2020-05-11 08:28:53 -06:00
|
|
|
# Client ping period.
|
|
|
|
@tick :timer.seconds(30)
|
2020-05-07 03:13:32 -06:00
|
|
|
# Cowboy timeout period.
|
2020-05-11 08:28:53 -06:00
|
|
|
@timeout :timer.seconds(60)
|
2020-05-07 03:13:32 -06:00
|
|
|
# Hibernate every X messages
|
|
|
|
@hibernate_every 100
|
|
|
|
|
2019-02-28 09:23:24 -07:00
|
|
|
def init(%{qs: qs} = req, state) do
|
2020-05-12 10:04:47 -06:00
|
|
|
with params <- Enum.into(:cow_qs.parse_qs(qs), %{}),
|
2019-07-06 12:26:08 -06:00
|
|
|
sec_websocket <- :cowboy_req.header("sec-websocket-protocol", req, nil),
|
2020-05-12 10:04:47 -06:00
|
|
|
access_token <- Map.get(params, "access_token"),
|
2020-09-19 10:16:55 -06: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 04:10:22 -06:00
|
|
|
req =
|
2019-10-17 22:36:37 -06:00
|
|
|
if sec_websocket do
|
2019-10-15 04:10:22 -06:00
|
|
|
:cowboy_req.set_resp_header("sec-websocket-protocol", sec_websocket, req)
|
|
|
|
else
|
|
|
|
req
|
|
|
|
end
|
|
|
|
|
2023-03-31 19:47:37 -06:00
|
|
|
topics =
|
|
|
|
if topic do
|
|
|
|
[topic]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2022-08-19 12:10:07 -06:00
|
|
|
{:cowboy_websocket, req,
|
2023-03-31 19:47:37 -06:00
|
|
|
%{user: user, topics: topics, oauth_token: oauth_token, count: 0, timer: nil},
|
2020-05-11 08:28:53 -06:00
|
|
|
%{idle_timeout: @timeout}}
|
2018-12-17 06:39:59 -07:00
|
|
|
else
|
2020-05-12 10:04:47 -06:00
|
|
|
{:error, :bad_topic} ->
|
|
|
|
Logger.debug("#{__MODULE__} bad topic #{inspect(req)}")
|
2020-09-10 12:26:52 -06:00
|
|
|
req = :cowboy_req.reply(404, req)
|
2019-02-28 09:23:55 -07:00
|
|
|
{:ok, req, state}
|
2018-12-17 06:39:59 -07:00
|
|
|
|
2020-05-12 10:04:47 -06:00
|
|
|
{:error, :unauthorized} ->
|
|
|
|
Logger.debug("#{__MODULE__} authentication error: #{inspect(req)}")
|
2020-09-10 12:26:52 -06:00
|
|
|
req = :cowboy_req.reply(401, req)
|
2019-02-28 09:23:55 -07:00
|
|
|
{:ok, req, state}
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-28 08:17:01 -07:00
|
|
|
def websocket_init(state) do
|
2020-05-07 03:13:32 -06:00
|
|
|
Logger.debug(
|
2023-03-31 19:47:37 -06:00
|
|
|
"#{__MODULE__} accepted websocket connection for user #{(state.user || %{id: "anonymous"}).id}, topics #{state.topics}"
|
2020-05-07 03:13:32 -06:00
|
|
|
)
|
|
|
|
|
2023-03-31 19:47:37 -06:00
|
|
|
Enum.each(state.topics, fn topic -> Streamer.add_socket(topic, state.oauth_token) end)
|
2020-05-11 08:28:53 -06: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 08:17:01 -07:00
|
|
|
end
|
|
|
|
|
2020-09-10 04:47:53 -06:00
|
|
|
# We only receive pings for now
|
|
|
|
def websocket_handle(:ping, state), do: {:ok, state}
|
|
|
|
|
2023-03-31 20:55:52 -06:00
|
|
|
def websocket_handle({:text, text}, state) do
|
|
|
|
with {:ok, %{} = event} <- Jason.decode(text) do
|
|
|
|
handle_client_event(event, state)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.error("#{__MODULE__} received non-JSON event: #{inspect(text)}")
|
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-11 08:28:53 -06:00
|
|
|
def websocket_handle(frame, state) do
|
|
|
|
Logger.error("#{__MODULE__} received frame: #{inspect(frame)}")
|
2019-02-28 08:17:01 -07:00
|
|
|
{:ok, state}
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
|
2023-03-31 23:25:13 -06:00
|
|
|
def websocket_info({:render_with_user, view, template, item, topic}, state) do
|
2020-05-07 03:13:32 -06:00
|
|
|
user = %User{} = User.get_cached_by_ap_id(state.user.ap_id)
|
2018-12-17 06:39:59 -07:00
|
|
|
|
2020-05-07 03:13:32 -06:00
|
|
|
unless Streamer.filtered_by_user?(user, item) do
|
2023-03-31 23:25:13 -06:00
|
|
|
websocket_info({:text, view.render(template, item, user, topic)}, %{state | user: user})
|
2020-05-07 03:13:32 -06:00
|
|
|
else
|
|
|
|
{:ok, state}
|
|
|
|
end
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
|
2019-02-28 08:17:01 -07:00
|
|
|
def websocket_info({:text, message}, state) do
|
2020-05-07 03:13:32 -06: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 06:39:59 -07:00
|
|
|
end
|
|
|
|
|
2020-05-11 08:28:53 -06: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 11:19:38 -06:00
|
|
|
def websocket_info(:close, state) do
|
|
|
|
{:stop, state}
|
|
|
|
end
|
|
|
|
|
2020-09-10 04:48:24 -06: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 08:17:01 -07:00
|
|
|
def terminate(reason, _req, state) do
|
2018-12-17 06:39:59 -07:00
|
|
|
Logger.debug(
|
2023-03-31 19:47:37 -06:00
|
|
|
"#{__MODULE__} terminating websocket connection for user #{(state.user || %{id: "anonymous"}).id}, topics #{state.topics || "?"}: #{inspect(reason)}"
|
2018-12-17 06:39:59 -07:00
|
|
|
)
|
|
|
|
|
2023-03-31 19:47:37 -06:00
|
|
|
Enum.each(state.topics, fn topic -> Streamer.remove_socket(topic) end)
|
2018-12-17 06:39:59 -07:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# Public streams without authentication.
|
2020-05-12 10:04:47 -06:00
|
|
|
defp authenticate_request(nil, nil) do
|
2020-09-19 10:16:55 -06:00
|
|
|
{:ok, nil, nil}
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticated streams.
|
2020-05-12 10:04:47 -06:00
|
|
|
defp authenticate_request(access_token, sec_websocket) do
|
|
|
|
token = access_token || sec_websocket
|
2019-07-06 12:26:08 -06:00
|
|
|
|
|
|
|
with true <- is_bitstring(token),
|
2020-09-19 10:16:55 -06:00
|
|
|
oauth_token = %Token{user_id: user_id} <- Repo.get_by(Token, token: token),
|
2019-04-22 01:20:43 -06:00
|
|
|
user = %User{} <- User.get_cached_by_id(user_id) do
|
2020-09-19 10:16:55 -06:00
|
|
|
{:ok, user, oauth_token}
|
2018-12-17 06:39:59 -07:00
|
|
|
else
|
2020-05-12 10:04:47 -06:00
|
|
|
_ -> {:error, :unauthorized}
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-11 08:28:53 -06:00
|
|
|
defp timer do
|
|
|
|
Process.send_after(self(), :tick, @tick)
|
|
|
|
end
|
2023-03-31 20:55:52 -06:00
|
|
|
|
|
|
|
defp handle_client_event(%{"type" => "subscribe", "stream" => _topic} = params, state) do
|
|
|
|
with {_, {:ok, topic}} <-
|
|
|
|
{:topic, Streamer.get_topic(params["stream"], state.user, state.oauth_token, params)},
|
|
|
|
{_, false} <- {:subscribed, topic in state.topics} do
|
|
|
|
Streamer.add_socket(topic, state.oauth_token)
|
|
|
|
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "subscribe", result: "success"})}
|
|
|
|
], %{state | topics: [topic | state.topics]}}
|
|
|
|
else
|
|
|
|
{:subscribed, true} ->
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "subscribe", result: "ignored"})}
|
|
|
|
], state}
|
|
|
|
|
|
|
|
{:topic, {:error, error}} ->
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "subscribe",
|
|
|
|
result: "error",
|
|
|
|
error: error
|
|
|
|
})}
|
|
|
|
], state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_client_event(%{"type" => "unsubscribe", "stream" => _topic} = params, state) do
|
|
|
|
with {_, {:ok, topic}} <-
|
|
|
|
{:topic, Streamer.get_topic(params["stream"], state.user, state.oauth_token, params)},
|
|
|
|
{_, true} <- {:subscribed, topic in state.topics} do
|
|
|
|
Streamer.remove_socket(topic)
|
|
|
|
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "unsubscribe", result: "success"})}
|
|
|
|
], %{state | topics: List.delete(state.topics, topic)}}
|
|
|
|
else
|
|
|
|
{:subscribed, false} ->
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "unsubscribe", result: "ignored"})}
|
|
|
|
], state}
|
|
|
|
|
|
|
|
{:topic, {:error, error}} ->
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "unsubscribe",
|
|
|
|
result: "error",
|
|
|
|
error: error
|
|
|
|
})}
|
|
|
|
], state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-31 21:19:57 -06:00
|
|
|
defp handle_client_event(
|
|
|
|
%{"type" => "pleroma.authenticate", "token" => access_token} = _params,
|
|
|
|
state
|
|
|
|
) do
|
|
|
|
with {:auth, nil, nil} <- {:auth, state.user, state.oauth_token},
|
|
|
|
{:ok, user, oauth_token} <- authenticate_request(access_token, nil) do
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "pleroma.authenticate",
|
|
|
|
result: "success"
|
|
|
|
})}
|
|
|
|
], %{state | user: user, oauth_token: oauth_token}}
|
|
|
|
else
|
|
|
|
{:auth, _, _} ->
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "pleroma.authenticate",
|
|
|
|
result: "error",
|
|
|
|
error: :already_authenticated
|
|
|
|
})}
|
|
|
|
], state}
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
{[
|
|
|
|
{:text,
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "pleroma.authenticate",
|
|
|
|
result: "error",
|
|
|
|
error: :unauthorized
|
|
|
|
})}
|
|
|
|
], state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-31 20:55:52 -06:00
|
|
|
defp handle_client_event(params, state) do
|
|
|
|
Logger.error("#{__MODULE__} received unknown event: #{inspect(params)}")
|
|
|
|
{[], state}
|
|
|
|
end
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|