2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 22:44:49 +00:00
|
|
|
# Copyright © 2017-2020 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
|
|
|
|
2019-09-16 10:03:37 +00:00
|
|
|
# Handled by periodic keepalive in Pleroma.Web.Streamer.Ping.
|
2019-02-28 16:23:24 +00:00
|
|
|
@timeout :infinity
|
|
|
|
|
|
|
|
def init(%{qs: qs} = req, state) do
|
2020-05-12 19:06:23 +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 19:06:23 +00:00
|
|
|
access_token <- Map.get(params, "access_token"),
|
|
|
|
{:ok, user} <- authenticate_request(access_token, sec_websocket),
|
|
|
|
{:ok, topic} <- Streamer.get_topic(Map.get(params, "stream"), user, 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
|
|
|
|
|
2019-02-28 16:23:24 +00:00
|
|
|
{:cowboy_websocket, req, %{user: user, topic: topic}, %{idle_timeout: @timeout}}
|
2018-12-17 13:39:59 +00:00
|
|
|
else
|
2020-05-12 19:06:23 +00:00
|
|
|
{:error, :bad_topic} ->
|
|
|
|
Logger.debug("#{__MODULE__} bad topic #{inspect(req)}")
|
|
|
|
{:ok, 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 19:06:23 +00:00
|
|
|
{:error, :unauthorized} ->
|
|
|
|
Logger.debug("#{__MODULE__} authentication error: #{inspect(req)}")
|
|
|
|
{:ok, 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
|
|
|
|
send(self(), :subscribe)
|
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
|
2018-12-17 13:39:59 +00:00
|
|
|
# We never receive messages.
|
2019-02-28 15:17:01 +00:00
|
|
|
def websocket_handle(_frame, state) do
|
|
|
|
{:ok, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2019-02-28 15:17:01 +00:00
|
|
|
def websocket_info(:subscribe, state) do
|
2018-12-17 13:39:59 +00:00
|
|
|
Logger.debug(
|
|
|
|
"#{__MODULE__} accepted websocket connection for user #{
|
|
|
|
(state.user || %{id: "anonymous"}).id
|
|
|
|
}, topic #{state.topic}"
|
|
|
|
)
|
|
|
|
|
2019-09-16 10:03:37 +00:00
|
|
|
Streamer.add_socket(state.topic, streamer_socket(state))
|
2019-02-28 15:17:01 +00:00
|
|
|
{:ok, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2019-02-28 15:17:01 +00:00
|
|
|
def websocket_info({:text, message}, state) do
|
|
|
|
{:reply, {:text, message}, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2019-02-28 15:17:01 +00:00
|
|
|
def terminate(reason, _req, state) do
|
2018-12-17 13:39:59 +00:00
|
|
|
Logger.debug(
|
|
|
|
"#{__MODULE__} terminating websocket connection for user #{
|
|
|
|
(state.user || %{id: "anonymous"}).id
|
|
|
|
}, topic #{state.topic || "?"}: #{inspect(reason)}"
|
|
|
|
)
|
|
|
|
|
2019-09-16 10:03:37 +00:00
|
|
|
Streamer.remove_socket(state.topic, streamer_socket(state))
|
2018-12-17 13:39:59 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# Public streams without authentication.
|
2020-05-12 19:06:23 +00:00
|
|
|
defp authenticate_request(nil, nil) do
|
2018-12-17 13:39:59 +00:00
|
|
|
{:ok, nil}
|
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticated streams.
|
2020-05-12 19:06:23 +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),
|
|
|
|
%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
|
2018-12-17 13:39:59 +00:00
|
|
|
{:ok, user}
|
|
|
|
else
|
2020-05-12 19:06:23 +00:00
|
|
|
_ -> {:error, :unauthorized}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp streamer_socket(state) do
|
|
|
|
%{transport_pid: self(), assigns: state}
|
|
|
|
end
|
|
|
|
end
|