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
|
|
|
|
|
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 01:44:03 -06:00
|
|
|
alias Pleroma.Web.Streamer
|
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
|
|
|
|
|
|
|
@streams [
|
|
|
|
"public",
|
|
|
|
"public:local",
|
|
|
|
"public:media",
|
|
|
|
"public:local:media",
|
|
|
|
"user",
|
2019-06-16 04:33:25 -06:00
|
|
|
"user:notification",
|
2018-12-17 06:39:59 -07:00
|
|
|
"direct",
|
|
|
|
"list",
|
|
|
|
"hashtag"
|
|
|
|
]
|
|
|
|
@anonymous_streams ["public", "public:local", "hashtag"]
|
|
|
|
|
2019-09-16 01:44:03 -06:00
|
|
|
# Handled by periodic keepalive in Pleroma.Web.Streamer.Ping.
|
2019-02-28 09:23:24 -07:00
|
|
|
@timeout :infinity
|
|
|
|
|
|
|
|
def init(%{qs: qs} = req, state) do
|
2019-02-28 09:02:48 -07:00
|
|
|
with params <- :cow_qs.parse_qs(qs),
|
2019-07-06 12:26:08 -06:00
|
|
|
sec_websocket <- :cowboy_req.header("sec-websocket-protocol", req, nil),
|
|
|
|
access_token <- List.keyfind(params, "access_token", 0),
|
2018-12-17 06:39:59 -07:00
|
|
|
{_, stream} <- List.keyfind(params, "stream", 0),
|
2019-07-06 12:26:08 -06:00
|
|
|
{:ok, user} <- allow_request(stream, [access_token, sec_websocket]),
|
2018-12-17 06:39:59 -07:00
|
|
|
topic when is_binary(topic) <- expand_topic(stream, params) do
|
2019-02-28 09:23:24 -07:00
|
|
|
{:cowboy_websocket, req, %{user: user, topic: topic}, %{idle_timeout: @timeout}}
|
2018-12-17 06:39:59 -07:00
|
|
|
else
|
|
|
|
{:error, code} ->
|
|
|
|
Logger.debug("#{__MODULE__} denied connection: #{inspect(code)} - #{inspect(req)}")
|
|
|
|
{:ok, req} = :cowboy_req.reply(code, req)
|
2019-02-28 09:23:55 -07:00
|
|
|
{:ok, req, state}
|
2018-12-17 06:39:59 -07:00
|
|
|
|
|
|
|
error ->
|
|
|
|
Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}")
|
2019-02-28 09:02:48 -07:00
|
|
|
{:ok, req} = :cowboy_req.reply(400, 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
|
|
|
|
send(self(), :subscribe)
|
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
|
2018-12-17 06:39:59 -07:00
|
|
|
# We never receive messages.
|
2019-02-28 08:17:01 -07:00
|
|
|
def websocket_handle(_frame, state) do
|
|
|
|
{:ok, state}
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
|
2019-02-28 08:17:01 -07:00
|
|
|
def websocket_info(:subscribe, state) do
|
2018-12-17 06:39:59 -07:00
|
|
|
Logger.debug(
|
|
|
|
"#{__MODULE__} accepted websocket connection for user #{
|
|
|
|
(state.user || %{id: "anonymous"}).id
|
|
|
|
}, topic #{state.topic}"
|
|
|
|
)
|
|
|
|
|
2019-09-16 01:44:03 -06:00
|
|
|
Streamer.add_socket(state.topic, streamer_socket(state))
|
2019-02-28 08:17:01 -07:00
|
|
|
{:ok, state}
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
|
2019-02-28 08:17:01 -07:00
|
|
|
def websocket_info({:text, message}, state) do
|
|
|
|
{:reply, {:text, message}, state}
|
2018-12-17 06:39:59 -07:00
|
|
|
end
|
|
|
|
|
2019-02-28 08:17:01 -07:00
|
|
|
def terminate(reason, _req, state) do
|
2018-12-17 06:39:59 -07:00
|
|
|
Logger.debug(
|
|
|
|
"#{__MODULE__} terminating websocket connection for user #{
|
|
|
|
(state.user || %{id: "anonymous"}).id
|
|
|
|
}, topic #{state.topic || "?"}: #{inspect(reason)}"
|
|
|
|
)
|
|
|
|
|
2019-09-16 01:44:03 -06:00
|
|
|
Streamer.remove_socket(state.topic, streamer_socket(state))
|
2018-12-17 06:39:59 -07:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# Public streams without authentication.
|
2019-07-06 12:26:08 -06:00
|
|
|
defp allow_request(stream, [nil, nil]) when stream in @anonymous_streams do
|
2018-12-17 06:39:59 -07:00
|
|
|
{:ok, nil}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticated streams.
|
2019-07-06 12:26:08 -06: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 01:20:43 -06:00
|
|
|
user = %User{} <- User.get_cached_by_id(user_id) do
|
2018-12-17 06:39:59 -07: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
|
|
|
|
|
|
|
|
defp streamer_socket(state) do
|
|
|
|
%{transport_pid: self(), assigns: state}
|
|
|
|
end
|
|
|
|
end
|