2016-11-22 13:17:05 +01:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% File : mod_bosh.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% Purpose : This module acts as a bridge to ejabberd_bosh which implements
|
|
|
|
%%% the real stuff, this is to handle the new pluggable architecture
|
|
|
|
%%% for extending ejabberd's http service.
|
|
|
|
%%% Created : 20 Jul 2011 by Evgeniy Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2019-01-08 22:53:27 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2019 ProcessOne
|
2016-11-22 13:17:05 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
|
|
|
%%%
|
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
%%%
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(mod_bosh).
|
|
|
|
|
|
|
|
-author('steve@zeank.in-berlin.de').
|
|
|
|
|
|
|
|
%%-define(ejabberd_debug, true).
|
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
-export([start_link/0]).
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, process/2, open_session/2,
|
2017-04-14 12:57:52 +02:00
|
|
|
close_session/1, find_session/1, clean_cache/1]).
|
2016-11-22 13:17:05 +01:00
|
|
|
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([depends/2, mod_opt_type/1, mod_options/1]).
|
2016-11-22 13:17:05 +01:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
|
|
|
-include_lib("stdlib/include/ms_transform.hrl").
|
2017-02-18 07:36:27 +01:00
|
|
|
-include("xmpp.hrl").
|
2016-11-22 13:17:05 +01:00
|
|
|
-include("ejabberd_http.hrl").
|
|
|
|
-include("bosh.hrl").
|
|
|
|
|
2017-01-13 10:03:39 +01:00
|
|
|
-callback init() -> any().
|
2017-04-14 12:57:52 +02:00
|
|
|
-callback open_session(binary(), pid()) -> ok | {error, any()}.
|
|
|
|
-callback close_session(binary()) -> ok | {error, any()}.
|
|
|
|
-callback find_session(binary()) -> {ok, pid()} | {error, any()}.
|
|
|
|
-callback use_cache() -> boolean().
|
|
|
|
-callback cache_nodes() -> [node()].
|
|
|
|
|
|
|
|
-optional_callbacks([use_cache/0, cache_nodes/0]).
|
2016-11-22 13:17:05 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
start_link() ->
|
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
process([], #request{method = 'POST', data = <<>>}) ->
|
|
|
|
?DEBUG("Bad Request: no data", []),
|
|
|
|
{400, ?HEADER(?CT_XML),
|
|
|
|
#xmlel{name = <<"h1">>, attrs = [],
|
|
|
|
children = [{xmlcdata, <<"400 Bad Request">>}]}};
|
|
|
|
process([],
|
|
|
|
#request{method = 'POST', data = Data, ip = IP, headers = Hdrs}) ->
|
|
|
|
?DEBUG("Incoming data: ~p", [Data]),
|
|
|
|
Type = get_type(Hdrs),
|
|
|
|
ejabberd_bosh:process_request(Data, IP, Type);
|
|
|
|
process([], #request{method = 'GET', data = <<>>}) ->
|
|
|
|
{200, ?HEADER(?CT_XML), get_human_html_xmlel()};
|
|
|
|
process([], #request{method = 'OPTIONS', data = <<>>}) ->
|
|
|
|
{200, ?OPTIONS_HEADER, []};
|
|
|
|
process(_Path, _Request) ->
|
|
|
|
?DEBUG("Bad Request: ~p", [_Request]),
|
|
|
|
{400, ?HEADER(?CT_XML),
|
|
|
|
#xmlel{name = <<"h1">>, attrs = [],
|
|
|
|
children = [{xmlcdata, <<"400 Bad Request">>}]}}.
|
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec open_session(binary(), pid()) -> ok | {error, any()}.
|
2016-11-22 13:17:05 +01:00
|
|
|
open_session(SID, Pid) ->
|
2017-01-13 10:03:39 +01:00
|
|
|
Mod = gen_mod:ram_db_mod(global, ?MODULE),
|
2017-04-14 12:57:52 +02:00
|
|
|
case Mod:open_session(SID, Pid) of
|
|
|
|
ok ->
|
|
|
|
delete_cache(Mod, SID);
|
|
|
|
{error, _} = Err ->
|
|
|
|
Err
|
|
|
|
end.
|
2016-11-22 13:17:05 +01:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec close_session(binary()) -> ok.
|
2016-11-22 13:17:05 +01:00
|
|
|
close_session(SID) ->
|
2017-01-13 10:03:39 +01:00
|
|
|
Mod = gen_mod:ram_db_mod(global, ?MODULE),
|
2017-04-14 12:57:52 +02:00
|
|
|
Mod:close_session(SID),
|
|
|
|
delete_cache(Mod, SID).
|
2016-11-22 13:17:05 +01:00
|
|
|
|
2017-04-14 12:57:52 +02:00
|
|
|
-spec find_session(binary()) -> {ok, pid()} | error.
|
2016-11-22 13:17:05 +01:00
|
|
|
find_session(SID) ->
|
2017-01-13 10:03:39 +01:00
|
|
|
Mod = gen_mod:ram_db_mod(global, ?MODULE),
|
2017-04-14 12:57:52 +02:00
|
|
|
case use_cache(Mod) of
|
|
|
|
true ->
|
|
|
|
ets_cache:lookup(
|
|
|
|
?BOSH_CACHE, SID,
|
|
|
|
fun() ->
|
|
|
|
case Mod:find_session(SID) of
|
|
|
|
{ok, Pid} -> {ok, Pid};
|
|
|
|
{error, _} -> error
|
|
|
|
end
|
|
|
|
end);
|
|
|
|
false ->
|
|
|
|
case Mod:find_session(SID) of
|
|
|
|
{ok, Pid} -> {ok, Pid};
|
|
|
|
{error, _} -> error
|
|
|
|
end
|
|
|
|
end.
|
2016-11-22 13:17:05 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
start(Host, _Opts) ->
|
|
|
|
Mod = gen_mod:ram_db_mod(Host, ?MODULE),
|
|
|
|
init_cache(Host, Mod),
|
2017-02-13 09:11:41 +01:00
|
|
|
Mod:init(),
|
2017-04-14 12:57:52 +02:00
|
|
|
clean_cache(),
|
2017-02-14 10:39:26 +01:00
|
|
|
TmpSup = gen_mod:get_module_proc(Host, ?MODULE),
|
2016-11-22 13:17:05 +01:00
|
|
|
TmpSupSpec = {TmpSup,
|
|
|
|
{ejabberd_tmp_sup, start_link, [TmpSup, ejabberd_bosh]},
|
|
|
|
permanent, infinity, supervisor, [ejabberd_tmp_sup]},
|
2017-02-24 10:05:47 +01:00
|
|
|
supervisor:start_child(ejabberd_gen_mod_sup, TmpSupSpec).
|
2016-11-22 13:17:05 +01:00
|
|
|
|
|
|
|
stop(Host) ->
|
2017-02-14 10:39:26 +01:00
|
|
|
TmpSup = gen_mod:get_module_proc(Host, ?MODULE),
|
2017-02-24 10:05:47 +01:00
|
|
|
supervisor:terminate_child(ejabberd_gen_mod_sup, TmpSup),
|
|
|
|
supervisor:delete_child(ejabberd_gen_mod_sup, TmpSup).
|
2016-11-22 13:17:05 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
reload(Host, _NewOpts, _OldOpts) ->
|
2017-02-22 17:46:47 +01:00
|
|
|
Mod = gen_mod:ram_db_mod(global, ?MODULE),
|
2019-06-14 11:33:26 +02:00
|
|
|
init_cache(Host, Mod),
|
2017-02-22 17:46:47 +01:00
|
|
|
Mod:init(),
|
|
|
|
ok.
|
|
|
|
|
2016-11-22 13:17:05 +01:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
get_type(Hdrs) ->
|
|
|
|
try
|
|
|
|
{_, S} = lists:keyfind('Content-Type', 1, Hdrs),
|
|
|
|
[T|_] = str:tokens(S, <<";">>),
|
|
|
|
[_, <<"json">>] = str:tokens(T, <<"/">>),
|
|
|
|
json
|
|
|
|
catch _:_ ->
|
|
|
|
xml
|
|
|
|
end.
|
|
|
|
|
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
|
|
|
mod_opt_type(json) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:and_then(
|
|
|
|
econf:bool(),
|
|
|
|
fun(false) -> false;
|
|
|
|
(true) ->
|
|
|
|
ejabberd:start_app(jiffy),
|
|
|
|
true
|
|
|
|
end);
|
2016-11-22 13:17:05 +01:00
|
|
|
mod_opt_type(max_concat) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:pos_int(unlimited);
|
2016-11-22 13:17:05 +01:00
|
|
|
mod_opt_type(max_inactivity) ->
|
2019-07-17 21:15:56 +02:00
|
|
|
econf:timeout(second);
|
2016-11-22 13:17:05 +01:00
|
|
|
mod_opt_type(max_pause) ->
|
2019-07-17 21:15:56 +02:00
|
|
|
econf:timeout(second);
|
2016-11-22 13:17:05 +01:00
|
|
|
mod_opt_type(prebind) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
econf:bool();
|
2017-03-10 13:12:43 +01:00
|
|
|
mod_opt_type(queue_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:queue_type();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(ram_db_type) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:db_type(?MODULE);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(use_cache) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_size) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:pos_int(infinity);
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_missed) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:bool();
|
2019-06-14 11:33:26 +02:00
|
|
|
mod_opt_type(cache_life_time) ->
|
2019-06-15 11:53:16 +02:00
|
|
|
econf:timeout(second, infinity).
|
2018-01-23 08:54:52 +01:00
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec mod_options(binary()) -> [{json, boolean()} |
|
|
|
|
{atom(), term()}].
|
2018-01-23 08:54:52 +01:00
|
|
|
mod_options(Host) ->
|
|
|
|
[{json, false},
|
|
|
|
{max_concat, unlimited},
|
2019-07-17 21:15:56 +02:00
|
|
|
{max_inactivity, timer:seconds(30)},
|
|
|
|
{max_pause, timer:seconds(120)},
|
2018-01-23 08:54:52 +01:00
|
|
|
{prebind, false},
|
|
|
|
{ram_db_type, ejabberd_config:default_ram_db(Host, ?MODULE)},
|
2019-06-14 11:33:26 +02:00
|
|
|
{queue_type, ejabberd_option:queue_type(Host)},
|
|
|
|
{use_cache, ejabberd_option:use_cache(Host)},
|
|
|
|
{cache_size, ejabberd_option:cache_size(Host)},
|
|
|
|
{cache_missed, ejabberd_option:cache_missed(Host)},
|
|
|
|
{cache_life_time, ejabberd_option:cache_life_time(Host)}].
|
2017-04-14 12:57:52 +02:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Cache stuff
|
|
|
|
%%%----------------------------------------------------------------------
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec init_cache(binary(), module()) -> ok.
|
|
|
|
init_cache(Host, Mod) ->
|
|
|
|
case use_cache(Mod, Host) of
|
2017-04-14 12:57:52 +02:00
|
|
|
true ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ets_cache:new(?BOSH_CACHE, cache_opts(Host));
|
2017-04-14 12:57:52 +02:00
|
|
|
false ->
|
|
|
|
ets_cache:delete(?BOSH_CACHE)
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec use_cache(module()) -> boolean().
|
|
|
|
use_cache(Mod) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
use_cache(Mod, global).
|
|
|
|
|
|
|
|
-spec use_cache(module(), global | binary()) -> boolean().
|
|
|
|
use_cache(Mod, Host) ->
|
2017-04-14 12:57:52 +02:00
|
|
|
case erlang:function_exported(Mod, use_cache, 0) of
|
|
|
|
true -> Mod:use_cache();
|
2019-06-14 11:33:26 +02:00
|
|
|
false -> mod_bosh_opt:use_cache(Host)
|
2017-04-14 12:57:52 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
-spec cache_nodes(module()) -> [node()].
|
|
|
|
cache_nodes(Mod) ->
|
|
|
|
case erlang:function_exported(Mod, cache_nodes, 0) of
|
|
|
|
true -> Mod:cache_nodes();
|
|
|
|
false -> ejabberd_cluster:get_nodes()
|
|
|
|
end.
|
|
|
|
|
|
|
|
-spec delete_cache(module(), binary()) -> ok.
|
|
|
|
delete_cache(Mod, SID) ->
|
|
|
|
case use_cache(Mod) of
|
|
|
|
true ->
|
|
|
|
ets_cache:delete(?BOSH_CACHE, SID, cache_nodes(Mod));
|
|
|
|
false ->
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
-spec cache_opts(binary()) -> [proplists:property()].
|
|
|
|
cache_opts(Host) ->
|
|
|
|
MaxSize = mod_bosh_opt:cache_size(Host),
|
|
|
|
CacheMissed = mod_bosh_opt:cache_missed(Host),
|
2019-06-15 11:53:16 +02:00
|
|
|
LifeTime = mod_bosh_opt:cache_life_time(Host),
|
2017-04-14 12:57:52 +02:00
|
|
|
[{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}].
|
|
|
|
|
2018-09-09 08:59:08 +02:00
|
|
|
-spec clean_cache(node()) -> non_neg_integer().
|
2017-04-14 12:57:52 +02:00
|
|
|
clean_cache(Node) ->
|
|
|
|
ets_cache:filter(
|
|
|
|
?BOSH_CACHE,
|
|
|
|
fun(_, error) ->
|
|
|
|
false;
|
|
|
|
(_, {ok, Pid}) ->
|
|
|
|
node(Pid) /= Node
|
|
|
|
end).
|
|
|
|
|
|
|
|
-spec clean_cache() -> ok.
|
|
|
|
clean_cache() ->
|
|
|
|
ejabberd_cluster:eval_everywhere(?MODULE, clean_cache, [node()]).
|
2017-01-02 15:53:25 +01:00
|
|
|
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Help Web Page
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
get_human_html_xmlel() ->
|
|
|
|
Heading = <<"ejabberd ",
|
|
|
|
(iolist_to_binary(atom_to_list(?MODULE)))/binary>>,
|
|
|
|
#xmlel{name = <<"html">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, <<"http://www.w3.org/1999/xhtml">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"head">>,
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"title">>,
|
|
|
|
children = [{xmlcdata, Heading}]},
|
|
|
|
#xmlel{name = <<"style">>,
|
|
|
|
children = [{xmlcdata, get_style_cdata()}]}]},
|
|
|
|
#xmlel{name = <<"body">>,
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"div">>,
|
|
|
|
attrs = [{<<"class">>, <<"container">>}],
|
|
|
|
children = get_container_children(Heading)}]}]}.
|
|
|
|
|
|
|
|
get_container_children(Heading) ->
|
|
|
|
[#xmlel{name = <<"div">>,
|
|
|
|
attrs = [{<<"class">>, <<"section">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"div">>,
|
|
|
|
attrs = [{<<"class">>, <<"block">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"a">>,
|
|
|
|
attrs = [{<<"href">>, <<"https://www.ejabberd.im">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"img">>,
|
|
|
|
attrs = [{<<"height">>, <<"32">>},
|
|
|
|
{<<"src">>, get_image_src()}]}]}]}]},
|
|
|
|
#xmlel{name = <<"div">>,
|
|
|
|
attrs = [{<<"class">>, <<"white section">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"div">>,
|
|
|
|
attrs = [{<<"class">>, <<"block">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"h1">>, children = [{xmlcdata, Heading}]},
|
|
|
|
#xmlel{name = <<"p">>, children =
|
|
|
|
[{xmlcdata, <<"An implementation of ">>},
|
|
|
|
#xmlel{name = <<"a">>,
|
|
|
|
attrs = [{<<"href">>, <<"http://xmpp.org/extensions/xep-0206.html">>}],
|
|
|
|
children = [{xmlcdata, <<"XMPP over BOSH (XEP-0206)">>}]}]},
|
|
|
|
#xmlel{name = <<"p">>, children =
|
|
|
|
[{xmlcdata, <<"This web page is only informative. To "
|
|
|
|
"use HTTP-Bind you need a Jabber/XMPP "
|
|
|
|
"client that supports it.">>}]}]}]},
|
|
|
|
#xmlel{name = <<"div">>,
|
|
|
|
attrs = [{<<"class">>, <<"section">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"div">>,
|
|
|
|
attrs = [{<<"class">>, <<"block">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"a">>,
|
|
|
|
attrs = [{<<"href">>, <<"https://www.ejabberd.im">>},
|
|
|
|
{<<"title">>, <<"ejabberd XMPP server">>}],
|
|
|
|
children = [{xmlcdata, <<"ejabberd">>}]},
|
|
|
|
{xmlcdata, <<" is maintained by ">>},
|
|
|
|
#xmlel{name = <<"a">>,
|
|
|
|
attrs = [{<<"href">>, <<"https://www.process-one.net">>},
|
|
|
|
{<<"title">>, <<"ProcessOne - Leader in Instant Messaging and Push Solutions">>}],
|
|
|
|
children = [{xmlcdata, <<"ProcessOne">>}]} ]}]}
|
|
|
|
].
|
|
|
|
|
|
|
|
get_style_cdata() ->
|
2017-10-09 22:35:42 +02:00
|
|
|
case misc:read_css("bosh.css") of
|
|
|
|
{ok, Data} -> Data;
|
|
|
|
{error, _} -> <<>>
|
|
|
|
end.
|
2017-01-02 15:53:25 +01:00
|
|
|
|
|
|
|
get_image_src() ->
|
2017-10-09 22:35:42 +02:00
|
|
|
case misc:read_img("bosh-logo.png") of
|
|
|
|
{ok, Img} ->
|
|
|
|
B64Img = base64:encode(Img),
|
|
|
|
<<"data:image/png;base64,", B64Img/binary>>;
|
|
|
|
{error, _} ->
|
|
|
|
<<>>
|
|
|
|
end.
|