2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-17 14:39:59 +01:00
|
|
|
defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
|
|
|
|
require Logger
|
|
|
|
|
2019-02-09 16:16:26 +01:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-03-05 03:52:23 +01:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2019-09-16 12:03:37 +02:00
|
|
|
alias Pleroma.Web.Streamer
|
2018-12-17 14:39:59 +01:00
|
|
|
|
2019-02-28 16:17:01 +01:00
|
|
|
@behaviour :cowboy_websocket
|
2018-12-17 14:39:59 +01:00
|
|
|
|
2020-05-11 16:28:53 +02:00
|
|
|
# Client ping period.
|
|
|
|
@tick :timer.seconds(30)
|
2020-05-07 11:13:32 +02:00
|
|
|
# Cowboy timeout period.
|
2020-05-11 16:28:53 +02:00
|
|
|
@timeout :timer.seconds(60)
|
2020-05-07 11:13:32 +02:00
|
|
|
# Hibernate every X messages
|
|
|
|
@hibernate_every 100
|
|
|
|
|
2018-12-17 14:39:59 +01:00
|
|
|
@streams [
|
|
|
|
"public",
|
|
|
|
"public:local",
|
|
|
|
"public:media",
|
|
|
|
"public:local:media",
|
|
|
|
"user",
|
2019-06-16 12:33:25 +02:00
|
|
|
"user:notification",
|
2018-12-17 14:39:59 +01:00
|
|
|
"direct",
|
|
|
|
"list",
|
|
|
|
"hashtag"
|
|
|
|
]
|
|
|
|
@anonymous_streams ["public", "public:local", "hashtag"]
|
|
|
|
|
2019-02-28 17:23:24 +01:00
|
|
|
def init(%{qs: qs} = req, state) do
|
2019-02-28 17:02:48 +01:00
|
|
|
with params <- :cow_qs.parse_qs(qs),
|
2019-07-06 20:26:08 +02:00
|
|
|
sec_websocket <- :cowboy_req.header("sec-websocket-protocol", req, nil),
|
|
|
|
access_token <- List.keyfind(params, "access_token", 0),
|
2018-12-17 14:39:59 +01:00
|
|
|
{_, stream} <- List.keyfind(params, "stream", 0),
|
2019-07-06 20:26:08 +02:00
|
|
|
{:ok, user} <- allow_request(stream, [access_token, sec_websocket]),
|
2018-12-17 14:39:59 +01:00
|
|
|
topic when is_binary(topic) <- expand_topic(stream, params) do
|
2019-10-15 12:10:22 +02:00
|
|
|
req =
|
2019-10-18 06:36:37 +02:00
|
|
|
if sec_websocket do
|
2019-10-15 12:10:22 +02:00
|
|
|
:cowboy_req.set_resp_header("sec-websocket-protocol", sec_websocket, req)
|
|
|
|
else
|
|
|
|
req
|
|
|
|
end
|
|
|
|
|
2020-05-11 16:28:53 +02:00
|
|
|
{:cowboy_websocket, req, %{user: user, topic: topic, count: 0, timer: nil},
|
|
|
|
%{idle_timeout: @timeout}}
|
2018-12-17 14:39:59 +01:00
|
|
|
else
|
|
|
|
{:error, code} ->
|
|
|
|
Logger.debug("#{__MODULE__} denied connection: #{inspect(code)} - #{inspect(req)}")
|
|
|
|
{:ok, req} = :cowboy_req.reply(code, req)
|
2019-02-28 17:23:55 +01:00
|
|
|
{:ok, req, state}
|
2018-12-17 14:39:59 +01:00
|
|
|
|
|
|
|
error ->
|
|
|
|
Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}")
|
2019-02-28 17:02:48 +01:00
|
|
|
{:ok, req} = :cowboy_req.reply(400, req)
|
2019-02-28 17:23:55 +01:00
|
|
|
{:ok, req, state}
|
2018-12-17 14:39:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-28 16:17:01 +01:00
|
|
|
def websocket_init(state) do
|
2020-05-07 11:13:32 +02:00
|
|
|
Logger.debug(
|
|
|
|
"#{__MODULE__} accepted websocket connection for user #{
|
|
|
|
(state.user || %{id: "anonymous"}).id
|
|
|
|
}, topic #{state.topic}"
|
|
|
|
)
|
|
|
|
|
|
|
|
Streamer.add_socket(state.topic, state.user)
|
2020-05-11 16:28:53 +02: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 16:17:01 +01:00
|
|
|
end
|
|
|
|
|
2018-12-17 14:39:59 +01:00
|
|
|
# We never receive messages.
|
2020-05-11 16:28:53 +02:00
|
|
|
def websocket_handle(frame, state) do
|
|
|
|
Logger.error("#{__MODULE__} received frame: #{inspect(frame)}")
|
2019-02-28 16:17:01 +01:00
|
|
|
{:ok, state}
|
2018-12-17 14:39:59 +01:00
|
|
|
end
|
|
|
|
|
2020-05-07 11:13:32 +02:00
|
|
|
def websocket_info({:render_with_user, view, template, item}, state) do
|
|
|
|
user = %User{} = User.get_cached_by_ap_id(state.user.ap_id)
|
2018-12-17 14:39:59 +01:00
|
|
|
|
2020-05-07 11:13:32 +02:00
|
|
|
unless Streamer.filtered_by_user?(user, item) do
|
2020-05-07 15:13:24 +02:00
|
|
|
websocket_info({:text, view.render(template, item, user)}, %{state | user: user})
|
2020-05-07 11:13:32 +02:00
|
|
|
else
|
|
|
|
{:ok, state}
|
|
|
|
end
|
2018-12-17 14:39:59 +01:00
|
|
|
end
|
|
|
|
|
2019-02-28 16:17:01 +01:00
|
|
|
def websocket_info({:text, message}, state) do
|
2020-05-07 11:13:32 +02: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 14:39:59 +01:00
|
|
|
end
|
|
|
|
|
2020-05-11 16:28:53 +02: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
|
|
|
|
|
2019-02-28 16:17:01 +01:00
|
|
|
def terminate(reason, _req, state) do
|
2018-12-17 14:39:59 +01:00
|
|
|
Logger.debug(
|
|
|
|
"#{__MODULE__} terminating websocket connection for user #{
|
|
|
|
(state.user || %{id: "anonymous"}).id
|
|
|
|
}, topic #{state.topic || "?"}: #{inspect(reason)}"
|
|
|
|
)
|
|
|
|
|
2020-05-07 11:13:32 +02:00
|
|
|
Streamer.remove_socket(state.topic)
|
2018-12-17 14:39:59 +01:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# Public streams without authentication.
|
2019-07-06 20:26:08 +02:00
|
|
|
defp allow_request(stream, [nil, nil]) when stream in @anonymous_streams do
|
2018-12-17 14:39:59 +01:00
|
|
|
{:ok, nil}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticated streams.
|
2019-07-06 20:26:08 +02:00
|
|
|
defp allow_request(stream, [access_token, sec_websocket]) when stream in @streams do
|
|
|
|
token =
|
|
|
|
with {"access_token", token} <- access_token do
|
|
|
|
token
|
|
|
|
else
|
|
|
|
_ -> sec_websocket
|
|
|
|
end
|
|
|
|
|
|
|
|
with true <- is_bitstring(token),
|
|
|
|
%Token{user_id: user_id} <- Repo.get_by(Token, token: token),
|
2019-04-22 09:20:43 +02:00
|
|
|
user = %User{} <- User.get_cached_by_id(user_id) do
|
2018-12-17 14:39:59 +01:00
|
|
|
{:ok, user}
|
|
|
|
else
|
|
|
|
_ -> {:error, 403}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Not authenticated.
|
|
|
|
defp allow_request(stream, _) when stream in @streams, do: {:error, 403}
|
|
|
|
|
|
|
|
# No matching stream.
|
|
|
|
defp allow_request(_, _), do: {:error, 404}
|
|
|
|
|
|
|
|
defp expand_topic("hashtag", params) do
|
|
|
|
case List.keyfind(params, "tag", 0) do
|
|
|
|
{_, tag} -> "hashtag:#{tag}"
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp expand_topic("list", params) do
|
|
|
|
case List.keyfind(params, "list", 0) do
|
|
|
|
{_, list} -> "list:#{list}"
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp expand_topic(topic, _), do: topic
|
2020-05-11 16:28:53 +02:00
|
|
|
|
|
|
|
defp timer do
|
|
|
|
Process.send_after(self(), :tick, @tick)
|
|
|
|
end
|
2018-12-17 14:39:59 +01:00
|
|
|
end
|