2015-02-25 10:42:59 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_websocket.erl
|
|
|
|
%%% Author : Eric Cestari <ecestari@process-one.net>
|
|
|
|
%%% Purpose : XMPP Websocket support
|
|
|
|
%%% Created : 09-10-2010 by Eric Cestari <ecestari@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%% Some code lifted from MISULTIN - WebSocket misultin_websocket.erl - >-|-|-(°>
|
|
|
|
%%% (http://github.com/ostinelli/misultin/blob/master/src/misultin_websocket.erl)
|
|
|
|
%%% Copyright (C) 2010, Roberto Ostinelli <roberto@ostinelli.net>, Joe Armstrong.
|
|
|
|
%%% All rights reserved.
|
|
|
|
%%%
|
|
|
|
%%% Code portions from Joe Armstrong have been originally taken under MIT license at the address:
|
|
|
|
%%% <http://armstrongonsoftware.blogspot.com/2009/12/comet-is-dead-long-live-websockets.html>
|
|
|
|
%%%
|
|
|
|
%%% BSD License
|
|
|
|
%%%
|
|
|
|
%%% Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
|
|
|
%%% that the following conditions are met:
|
|
|
|
%%%
|
|
|
|
%%% * Redistributions of source code must retain the above copyright notice, this list of conditions and the
|
|
|
|
%%% following disclaimer.
|
|
|
|
%%% * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
|
|
|
%%% the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
|
|
%%% * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
|
|
|
|
%%% products derived from this software without specific prior written permission.
|
|
|
|
%%%
|
|
|
|
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
|
|
|
%%% WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
%%% PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
%%% ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
|
|
%%% TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
%%% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
%%% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
%%% POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
%%% ==========================================================================================================
|
2023-01-09 17:09:06 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2023 ProcessOne
|
2015-02-25 10:42:59 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_websocket).
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({rfc, 6455}).
|
2022-11-04 11:34:45 +01:00
|
|
|
-protocol({rfc, 7395}).
|
2015-05-21 17:02:36 +02:00
|
|
|
|
2015-02-25 10:42:59 +01:00
|
|
|
-author('ecestari@process-one.net').
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-export([socket_handoff/5]).
|
2015-02-25 10:42:59 +01:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
|
|
|
|
2020-09-03 13:45:57 +02:00
|
|
|
-include_lib("xmpp/include/xmpp.hrl").
|
2015-02-25 10:42:59 +01:00
|
|
|
|
|
|
|
-include("ejabberd_http.hrl").
|
|
|
|
|
|
|
|
-define(CT_XML, {<<"Content-Type">>, <<"text/xml; charset=utf-8">>}).
|
|
|
|
-define(CT_PLAIN, {<<"Content-Type">>, <<"text/plain">>}).
|
|
|
|
|
|
|
|
-define(AC_ALLOW_ORIGIN, {<<"Access-Control-Allow-Origin">>, <<"*">>}).
|
|
|
|
-define(AC_ALLOW_METHODS, {<<"Access-Control-Allow-Methods">>, <<"GET, OPTIONS">>}).
|
|
|
|
-define(AC_ALLOW_HEADERS, {<<"Access-Control-Allow-Headers">>, <<"Content-Type">>}).
|
|
|
|
-define(AC_MAX_AGE, {<<"Access-Control-Max-Age">>, <<"86400">>}).
|
|
|
|
|
|
|
|
-define(OPTIONS_HEADER, [?CT_PLAIN, ?AC_ALLOW_ORIGIN, ?AC_ALLOW_METHODS,
|
|
|
|
?AC_ALLOW_HEADERS, ?AC_MAX_AGE]).
|
|
|
|
-define(HEADER, [?CT_XML, ?AC_ALLOW_ORIGIN, ?AC_ALLOW_HEADERS]).
|
|
|
|
|
2019-04-26 15:29:43 +02:00
|
|
|
is_valid_websocket_upgrade(_Path, Headers) ->
|
|
|
|
HeadersToValidate = [{'Upgrade', <<"websocket">>},
|
|
|
|
{'Connection', ignore},
|
|
|
|
{'Host', ignore},
|
|
|
|
{<<"Sec-Websocket-Key">>, ignore},
|
|
|
|
{<<"Sec-Websocket-Version">>, <<"13">>}],
|
|
|
|
Res = lists:all(
|
|
|
|
fun({Tag, Val}) ->
|
|
|
|
case lists:keyfind(Tag, 1, Headers) of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
{_, _} when Val == ignore ->
|
|
|
|
true;
|
|
|
|
{_, HVal} ->
|
|
|
|
str:to_lower(HVal) == Val
|
|
|
|
end
|
|
|
|
end, HeadersToValidate),
|
|
|
|
|
|
|
|
case {Res, lists:keyfind(<<"Origin">>, 1, Headers), get_origin()} of
|
|
|
|
{false, _, _} ->
|
|
|
|
false;
|
|
|
|
{true, _, []} ->
|
|
|
|
true;
|
|
|
|
{true, {_, HVal}, Origins} ->
|
|
|
|
HValLow = str:to_lower(HVal),
|
|
|
|
case lists:any(fun(V) -> V == HValLow end, Origins) of
|
|
|
|
true ->
|
|
|
|
true;
|
|
|
|
_ ->
|
|
|
|
invalid_origin
|
|
|
|
end;
|
|
|
|
{true, false, _} ->
|
|
|
|
true
|
2015-02-25 10:42:59 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
socket_handoff(LocalPath, #request{method = 'GET', ip = IP, q = Q, path = Path,
|
2015-09-04 12:42:53 +02:00
|
|
|
headers = Headers, host = Host, port = Port,
|
2018-05-14 18:30:21 +02:00
|
|
|
socket = Socket, sockmod = SockMod,
|
|
|
|
data = Buf, opts = HOpts},
|
|
|
|
_Opts, HandlerModule, InfoMsgFun) ->
|
2019-04-26 15:29:43 +02:00
|
|
|
case is_valid_websocket_upgrade(LocalPath, Headers) of
|
2015-02-25 10:42:59 +01:00
|
|
|
true ->
|
|
|
|
WS = #ws{socket = Socket,
|
|
|
|
sockmod = SockMod,
|
|
|
|
ip = IP,
|
|
|
|
q = Q,
|
|
|
|
host = Host,
|
|
|
|
port = Port,
|
|
|
|
path = Path,
|
|
|
|
headers = Headers,
|
|
|
|
local_path = LocalPath,
|
2015-09-04 12:42:53 +02:00
|
|
|
buf = Buf,
|
|
|
|
http_opts = HOpts},
|
2015-02-25 10:42:59 +01:00
|
|
|
|
|
|
|
connect(WS, HandlerModule);
|
2019-04-26 15:29:43 +02:00
|
|
|
false ->
|
|
|
|
{200, ?HEADER, InfoMsgFun()};
|
|
|
|
invalid_origin ->
|
|
|
|
{403, ?HEADER, #xmlel{name = <<"h1">>,
|
|
|
|
children = [{xmlcdata, <<"403 Bad Request - Invalid origin">>}]}}
|
2015-02-25 10:42:59 +01:00
|
|
|
end;
|
2018-05-14 18:30:21 +02:00
|
|
|
socket_handoff(_, #request{method = 'OPTIONS'}, _, _, _) ->
|
2015-02-25 10:42:59 +01:00
|
|
|
{200, ?OPTIONS_HEADER, []};
|
2018-05-14 18:30:21 +02:00
|
|
|
socket_handoff(_, #request{method = 'HEAD'}, _, _, _) ->
|
2015-02-25 10:42:59 +01:00
|
|
|
{200, ?HEADER, []};
|
2018-05-14 18:30:21 +02:00
|
|
|
socket_handoff(_, _, _, _, _) ->
|
2015-02-25 10:42:59 +01:00
|
|
|
{400, ?HEADER, #xmlel{name = <<"h1">>,
|
|
|
|
children = [{xmlcdata, <<"400 Bad Request">>}]}}.
|
|
|
|
|
|
|
|
connect(#ws{socket = Socket, sockmod = SockMod} = Ws, WsLoop) ->
|
|
|
|
{NewWs, HandshakeResponse} = handshake(Ws),
|
|
|
|
SockMod:send(Socket, HandshakeResponse),
|
|
|
|
|
|
|
|
?DEBUG("Sent handshake response : ~p",
|
|
|
|
[HandshakeResponse]),
|
|
|
|
Ws0 = {Ws, self()},
|
|
|
|
{ok, WsHandleLoopPid} = WsLoop:start_link(Ws0),
|
|
|
|
erlang:monitor(process, WsHandleLoopPid),
|
|
|
|
|
|
|
|
case NewWs#ws.buf of
|
|
|
|
<<>> ->
|
|
|
|
ok;
|
|
|
|
Data ->
|
|
|
|
self() ! {raw, Socket, Data}
|
|
|
|
end,
|
|
|
|
|
|
|
|
% set opts
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp ->
|
|
|
|
inet:setopts(Socket, [{packet, 0}, {active, true}]);
|
|
|
|
_ ->
|
|
|
|
SockMod:setopts(Socket, [{packet, 0}, {active, true}])
|
|
|
|
end,
|
2023-01-13 19:40:53 +01:00
|
|
|
ws_loop(ejabberd_websocket_codec:new_server(), Socket, WsHandleLoopPid, SockMod, none).
|
2015-02-25 10:42:59 +01:00
|
|
|
|
|
|
|
handshake(#ws{headers = Headers} = State) ->
|
|
|
|
{_, Key} = lists:keyfind(<<"Sec-Websocket-Key">>, 1,
|
|
|
|
Headers),
|
|
|
|
SubProtocolHeader = case find_subprotocol(Headers) of
|
|
|
|
false ->
|
|
|
|
[];
|
|
|
|
V ->
|
|
|
|
[<<"Sec-Websocket-Protocol:">>, V, <<"\r\n">>]
|
|
|
|
end,
|
2017-05-23 09:43:26 +02:00
|
|
|
Hash = base64:encode(
|
2017-02-13 16:35:57 +01:00
|
|
|
crypto:hash(sha, <<Key/binary, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11">>)),
|
2015-02-25 10:42:59 +01:00
|
|
|
{State, [<<"HTTP/1.1 101 Switching Protocols\r\n">>,
|
|
|
|
<<"Upgrade: websocket\r\n">>,
|
|
|
|
<<"Connection: Upgrade\r\n">>,
|
|
|
|
SubProtocolHeader,
|
|
|
|
<<"Sec-WebSocket-Accept: ">>, Hash, <<"\r\n\r\n">>]}.
|
|
|
|
|
|
|
|
find_subprotocol(Headers) ->
|
|
|
|
case lists:keysearch(<<"Sec-Websocket-Protocol">>, 1, Headers) of
|
|
|
|
false ->
|
|
|
|
case lists:keysearch(<<"Websocket-Protocol">>, 1, Headers) of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
{value, {_, Protocol2}} ->
|
|
|
|
Protocol2
|
|
|
|
end;
|
|
|
|
{value, {_, Protocol}} ->
|
|
|
|
Protocol
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2023-01-13 19:40:53 +01:00
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid, SockMod, Shaper) ->
|
2015-02-25 10:42:59 +01:00
|
|
|
receive
|
|
|
|
{DataType, _Socket, Data} when DataType =:= tcp orelse DataType =:= raw ->
|
2023-01-13 19:40:53 +01:00
|
|
|
case handle_data(DataType, Codec, Data, Socket, WsHandleLoopPid, SockMod, Shaper) of
|
|
|
|
{error, tls, Error} ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("TLS decode error ~p", [Error]),
|
2023-01-13 19:40:53 +01:00
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid, SockMod, 1002); % protocol error
|
|
|
|
{error, protocol, Error} ->
|
|
|
|
?DEBUG("Websocket decode error ~p", [Error]),
|
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid, SockMod, 1002); % protocol error
|
|
|
|
{NewCodec, ToSend, NewShaper} ->
|
2021-06-12 17:57:30 +02:00
|
|
|
lists:foreach(fun(Pkt) -> SockMod:send(Socket, Pkt)
|
2015-02-25 10:42:59 +01:00
|
|
|
end, ToSend),
|
2023-01-13 19:40:53 +01:00
|
|
|
ws_loop(NewCodec, Socket, WsHandleLoopPid, SockMod, NewShaper)
|
2015-02-25 10:42:59 +01:00
|
|
|
end;
|
2019-01-30 09:57:17 +01:00
|
|
|
{new_shaper, NewShaper} ->
|
|
|
|
NewShaper = case NewShaper of
|
|
|
|
none when Shaper /= none ->
|
2021-06-12 17:57:30 +02:00
|
|
|
activate(Socket, SockMod, true), none;
|
2019-01-30 09:57:17 +01:00
|
|
|
_ ->
|
|
|
|
NewShaper
|
|
|
|
end,
|
2023-01-13 19:40:53 +01:00
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid, SockMod, NewShaper);
|
2015-02-25 10:42:59 +01:00
|
|
|
{tcp_closed, _Socket} ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("TCP connection was closed, exit", []),
|
2023-01-13 19:40:53 +01:00
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid, SockMod, 0);
|
2019-05-09 21:06:23 +02:00
|
|
|
{tcp_error, Socket, Reason} ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("TCP connection error: ~ts", [inet:format_error(Reason)]),
|
2023-01-13 19:40:53 +01:00
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid, SockMod, 0);
|
2015-02-25 10:42:59 +01:00
|
|
|
{'DOWN', Ref, process, WsHandleLoopPid, Reason} ->
|
|
|
|
Code = case Reason of
|
|
|
|
normal ->
|
|
|
|
1000; % normal close
|
|
|
|
_ ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?ERROR_MSG("Linked websocket controlling loop crashed "
|
2015-02-25 10:42:59 +01:00
|
|
|
"with reason: ~p",
|
|
|
|
[Reason]),
|
|
|
|
1011 % internal error
|
|
|
|
end,
|
|
|
|
erlang:demonitor(Ref),
|
2023-01-13 19:40:53 +01:00
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid, SockMod, Code);
|
2020-10-13 14:57:33 +02:00
|
|
|
{text_with_reply, Data, Sender} ->
|
2023-01-13 19:40:53 +01:00
|
|
|
SockMod:send(Socket, ejabberd_websocket_codec:encode(Codec, 1, Data)),
|
2020-10-13 14:57:33 +02:00
|
|
|
Sender ! {text_reply, self()},
|
2023-01-13 19:40:53 +01:00
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod, Shaper);
|
2020-10-13 14:57:33 +02:00
|
|
|
{data_with_reply, Data, Sender} ->
|
2023-01-13 19:40:53 +01:00
|
|
|
SockMod:send(Socket, ejabberd_websocket_codec:encode(Codec, 2, Data)),
|
2020-10-13 14:57:33 +02:00
|
|
|
Sender ! {data_reply, self()},
|
2023-01-13 19:40:53 +01:00
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod, Shaper);
|
2019-04-25 13:30:42 +02:00
|
|
|
{text, Data} ->
|
2023-01-13 19:40:53 +01:00
|
|
|
SockMod:send(Socket, ejabberd_websocket_codec:encode(Codec, 1, Data)),
|
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod, Shaper);
|
2019-04-25 13:30:42 +02:00
|
|
|
{data, Data} ->
|
2023-01-13 19:40:53 +01:00
|
|
|
SockMod:send(Socket, ejabberd_websocket_codec:encode(Codec, 2, Data)),
|
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod, Shaper);
|
2015-02-25 10:42:59 +01:00
|
|
|
{ping, Data} ->
|
2023-01-13 19:40:53 +01:00
|
|
|
SockMod:send(Socket, ejabberd_websocket_codec:encode(Codec, 9, Data)),
|
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod, Shaper);
|
2015-02-25 10:42:59 +01:00
|
|
|
shutdown ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?DEBUG("Shutdown request received, closing websocket "
|
2015-02-25 10:42:59 +01:00
|
|
|
"with pid ~p",
|
|
|
|
[self()]),
|
2023-01-13 19:40:53 +01:00
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid, SockMod, 1001); % going away
|
2015-02-25 10:42:59 +01:00
|
|
|
_Ignored ->
|
2019-06-24 19:32:34 +02:00
|
|
|
?WARNING_MSG("Received unexpected message, ignoring: ~p",
|
2015-02-25 10:42:59 +01:00
|
|
|
[_Ignored]),
|
2023-01-13 19:40:53 +01:00
|
|
|
ws_loop(Codec, Socket, WsHandleLoopPid,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod, Shaper)
|
2015-02-25 10:42:59 +01:00
|
|
|
end.
|
|
|
|
|
2023-01-13 19:40:53 +01:00
|
|
|
handle_data(tcp, Codec, Data, Socket, WsHandleLoopPid, fast_tls, Shaper) ->
|
2016-02-03 16:13:16 +01:00
|
|
|
case fast_tls:recv_data(Socket, Data) of
|
2015-02-25 10:42:59 +01:00
|
|
|
{ok, NewData} ->
|
2023-01-13 19:40:53 +01:00
|
|
|
handle_data_int(Codec, NewData, Socket, WsHandleLoopPid, fast_tls, Shaper);
|
2015-02-25 10:42:59 +01:00
|
|
|
{error, Error} ->
|
2023-01-13 19:40:53 +01:00
|
|
|
{error, tls, Error}
|
2015-02-25 10:42:59 +01:00
|
|
|
end;
|
2023-01-13 19:40:53 +01:00
|
|
|
handle_data(_, Codec, Data, Socket, WsHandleLoopPid, SockMod, Shaper) ->
|
|
|
|
handle_data_int(Codec, Data, Socket, WsHandleLoopPid, SockMod, Shaper).
|
|
|
|
|
|
|
|
handle_data_int(Codec, Data, Socket, WsHandleLoopPid, SockMod, Shaper) ->
|
|
|
|
{Type, NewCodec, Recv} = ejabberd_websocket_codec:decode(Codec, Data),
|
|
|
|
Send =
|
|
|
|
lists:filtermap(
|
|
|
|
fun({Op, Payload}) when Op == 1; Op == 2 ->
|
|
|
|
WsHandleLoopPid ! {received, Payload},
|
|
|
|
false;
|
|
|
|
({8, Payload}) ->
|
|
|
|
CloseCode =
|
|
|
|
case Payload of
|
|
|
|
<<Code:16/integer-big, Message/binary>> ->
|
|
|
|
?DEBUG("WebSocket close op: ~p ~ts",
|
|
|
|
[Code, Message]),
|
|
|
|
Code;
|
|
|
|
<<Code:16/integer-big>> ->
|
|
|
|
?DEBUG("WebSocket close op: ~p", [Code]),
|
|
|
|
Code;
|
|
|
|
_ ->
|
|
|
|
?DEBUG("WebSocket close op unknown: ~p", [Payload]),
|
|
|
|
1000
|
|
|
|
end,
|
|
|
|
Frame = ejabberd_websocket_codec:encode(Codec, 8, <<CloseCode:16/integer-big>>),
|
|
|
|
{true, Frame};
|
|
|
|
({9, Payload}) ->
|
|
|
|
WsHandleLoopPid ! ping,
|
|
|
|
Frame = ejabberd_websocket_codec:encode(Codec, 10, Payload),
|
|
|
|
{true, Frame};
|
|
|
|
({10, _Payload}) ->
|
|
|
|
WsHandleLoopPid ! pong,
|
|
|
|
false
|
|
|
|
end, Recv),
|
|
|
|
case Type of
|
|
|
|
error ->
|
|
|
|
{error, protocol, NewCodec};
|
|
|
|
_ ->
|
|
|
|
{NewCodec, Send, handle_shaping(Data, Socket, SockMod, Shaper)}
|
|
|
|
end.
|
2015-02-25 10:42:59 +01:00
|
|
|
|
2023-01-13 19:40:53 +01:00
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod, CloseCode) when CloseCode > 0 ->
|
2023-01-13 19:40:53 +01:00
|
|
|
Frame = ejabberd_websocket_codec:encode(Codec, 8, <<CloseCode:16/integer-big>>),
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod:send(Socket, Frame),
|
2023-01-13 19:40:53 +01:00
|
|
|
websocket_close(Codec, Socket, WsHandleLoopPid, SockMod, 0);
|
|
|
|
websocket_close(_Codec, Socket, WsHandleLoopPid, SockMod, _CloseCode) ->
|
2015-02-25 10:42:59 +01:00
|
|
|
WsHandleLoopPid ! closed,
|
2021-06-12 17:57:30 +02:00
|
|
|
SockMod:close(Socket).
|
2019-03-15 12:19:14 +01:00
|
|
|
|
|
|
|
get_origin() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ejabberd_option:websocket_origin().
|
2019-01-30 09:57:17 +01:00
|
|
|
|
2021-06-12 17:57:30 +02:00
|
|
|
handle_shaping(_Data, _Socket, _SockMod, none) ->
|
2019-01-30 09:57:17 +01:00
|
|
|
none;
|
2021-06-12 17:57:30 +02:00
|
|
|
handle_shaping(Data, Socket, SockMod, Shaper) ->
|
2019-01-30 09:57:17 +01:00
|
|
|
{NewShaper, Pause} = ejabberd_shaper:update(Shaper, byte_size(Data)),
|
|
|
|
if Pause > 0 ->
|
|
|
|
activate_after(Socket, self(), Pause);
|
2021-06-12 17:57:30 +02:00
|
|
|
true -> activate(Socket, SockMod, once)
|
2019-01-30 09:57:17 +01:00
|
|
|
end,
|
|
|
|
NewShaper.
|
|
|
|
|
|
|
|
activate(Socket, SockMod, ActiveState) ->
|
|
|
|
case SockMod of
|
|
|
|
gen_tcp -> inet:setopts(Socket, [{active, ActiveState}]);
|
|
|
|
_ -> SockMod:setopts(Socket, [{active, ActiveState}])
|
|
|
|
end.
|
|
|
|
|
|
|
|
activate_after(Socket, Pid, Pause) ->
|
|
|
|
if Pause > 0 ->
|
|
|
|
erlang:send_after(Pause, Pid, {tcp, Socket, <<>>});
|
|
|
|
true ->
|
|
|
|
Pid ! {tcp, Socket, <<>>}
|
|
|
|
end,
|
|
|
|
ok.
|