Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BOSH with dynamic domains #3253

Merged
merged 6 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions big_tests/dynamic_domains.config
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@
{transport, escalus_bosh},
{path, <<"/http-bind">>},
{port, 5280}]},
{carol_s, [
{username, <<"carol_s">>},
{server, <<"domain.example.com">>},
{host, <<"localhost">>},
{password, <<"jinglebells_s">>},
{transport, escalus_bosh},
{ssl, true},
{path, <<"/http-bind">>},
{port, 5285}]},
{kate, [
{username, <<"kate">>},
{server, <<"domain.example.com">>},
Expand All @@ -70,6 +79,14 @@
{server, <<"domain.example.com">>},
{host, <<"localhost">>},
{password, <<"nicniema">>}]},
{geralt, [
{username, <<"geralt">>},
{server, <<"domain.example.com">>},
{host, <<"localhost">>},
{password, <<"witcher">>},
{transport, escalus_ws},
{port, 5280},
{wspath, <<"/ws-xmpp">>}]},
{admin, [
{username, <<"admin">>},
{server, <<"localhost">>},
Expand Down
2 changes: 2 additions & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

{suites, "tests", acc_e2e_SUITE}.

{suites, "tests", bosh_SUITE}.

{suites, "tests", carboncopy_SUITE}.

{suites, "tests", disco_and_caps_SUITE}.
Expand Down
41 changes: 21 additions & 20 deletions big_tests/tests/bosh_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
-import(distributed_helper, [mim/0,
require_rpc_nodes/1,
rpc/4]).
-import(domain_helper, [host_type/0]).

%%--------------------------------------------------------------------
%% Suite configuration
Expand All @@ -44,19 +45,18 @@ all() ->
{group, essential_https},
{group, chat_https},
{group, interleave_requests_statem}
].
].

groups() ->
G = [
{essential, [shuffle], essential_test_cases()},
{essential_https, [shuffle], essential_test_cases()},
{chat, [shuffle], chat_test_cases()},
{chat_https, [shuffle], chat_test_cases()},
{time, [parallel], time_test_cases()},
{acks, [shuffle], acks_test_cases()},
{interleave_requests_statem, [parallel], [interleave_requests_statem]}
],
ct_helper:repeat_all_until_all_ok(G).
[
{essential, [shuffle], essential_test_cases()},
{essential_https, [shuffle], essential_test_cases()},
{chat, [shuffle], chat_test_cases()},
{chat_https, [shuffle], chat_test_cases()},
{time, [parallel], time_test_cases()},
{acks, [shuffle], acks_test_cases()},
{interleave_requests_statem, [parallel], [interleave_requests_statem]}
].

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().
Expand Down Expand Up @@ -186,10 +186,10 @@ do_not_accept_0_hold_value(Config) ->


send_specific_hold(Config, HoldValue) ->
{Server, Path, Client} = get_fusco_connection(Config),
{Domain, Path, Client} = get_fusco_connection(Config),

Rid = rand:uniform(1000000),
Body0 = escalus_bosh:session_creation_body(2, <<"1.0">>, <<"en">>, Rid, Server, nil),
Body0 = escalus_bosh:session_creation_body(2, <<"1.0">>, <<"en">>, Rid, Domain, nil),
#xmlel{attrs = Attrs0} = Body0,
Attrs = lists:keyreplace(<<"hold">>, 1, Attrs0, {<<"hold">>, HoldValue}),
Body = Body0#xmlel{attrs = Attrs},
Expand Down Expand Up @@ -259,10 +259,11 @@ get_fusco_connection(Config) ->
NamedSpecs = escalus_config:get_config(escalus_users, Config),
CarolSpec = proplists:get_value(?config(user, Config), NamedSpecs),
Server = proplists:get_value(server, CarolSpec),
Host = proplists:get_value(host, CarolSpec, Server),
Path = proplists:get_value(path, CarolSpec),
Port = proplists:get_value(port, CarolSpec),
UseSSL = proplists:get_value(ssl, CarolSpec, false),
{ok, Client} = fusco_cp:start_link({binary_to_list(Server), Port, UseSSL}, [], 1),
{ok, Client} = fusco_cp:start_link({binary_to_list(Host), Port, UseSSL}, [], 1),
{Server, Path, Client}.

stream_error(Config) ->
Expand Down Expand Up @@ -848,23 +849,23 @@ inactivity() ->

inactivity(Value) ->
{inactivity,
fun() -> rpc(mim(), mod_bosh, get_inactivity, [domain()]) end,
fun(V) -> rpc(mim(), mod_bosh, set_inactivity, [domain(), V]) end,
fun() -> rpc(mim(), mod_bosh, get_inactivity, [host_type()]) end,
fun(V) -> rpc(mim(), mod_bosh, set_inactivity, [host_type(), V]) end,
Value}.

max_wait() ->
max_wait(?MAX_WAIT).

max_wait(Value) ->
{max_wait,
fun() -> rpc(mim(), mod_bosh, get_max_wait, [domain()]) end,
fun(V) -> rpc(mim(), mod_bosh, set_max_wait, [domain(), V]) end,
fun() -> rpc(mim(), mod_bosh, get_max_wait, [host_type()]) end,
fun(V) -> rpc(mim(), mod_bosh, set_max_wait, [host_type(), V]) end,
Value}.

server_acks_opt() ->
{server_acks,
fun() -> rpc(mim(), mod_bosh, get_server_acks, [domain()]) end,
fun(V) -> rpc(mim(), mod_bosh, set_server_acks, [domain(), V]) end,
fun() -> rpc(mim(), mod_bosh, get_server_acks, [host_type()]) end,
fun(V) -> rpc(mim(), mod_bosh, set_server_acks, [host_type(), V]) end,
true}.

is_session_alive(Sid) ->
Expand Down
2 changes: 2 additions & 0 deletions rel/mim1.vars-toml.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
[[host_config]]
host_type = \"test type\"

[host_config.modules.mod_bosh]

[host_config.modules.mod_cache_users]

[host_config.modules.mod_carboncopy]
Expand Down
97 changes: 51 additions & 46 deletions src/mod_bosh.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
%% gen_mod callbacks
-export([start/2,
stop/1,
config_spec/0]).
config_spec/0,
supported_features/0]).

%% cowboy_loop_handler callbacks
-export([init/2,
Expand Down Expand Up @@ -106,47 +107,47 @@
%% API
%%--------------------------------------------------------------------

-spec get_inactivity(jid:lserver()) -> pos_integer() | infinity.
get_inactivity(LServer) ->
gen_mod:get_module_opt(LServer, ?MODULE, inactivity, ?DEFAULT_INACTIVITY).
-spec get_inactivity(mongooseim:host_type()) -> pos_integer() | infinity.
get_inactivity(HostType) ->
gen_mod:get_module_opt(HostType, ?MODULE, inactivity, ?DEFAULT_INACTIVITY).


%% @doc Return true if succeeded, false otherwise.
-spec set_inactivity(jid:lserver(), Seconds :: pos_integer() | infinity) -> boolean().
set_inactivity(LServer, infinity) ->
gen_mod:set_module_opt(LServer, ?MODULE, inactivity, infinity);
set_inactivity(LServer, Seconds) when is_integer(Seconds), Seconds > 0 ->
gen_mod:set_module_opt(LServer, ?MODULE, inactivity, Seconds).
-spec set_inactivity(mongooseim:host_type(), Seconds :: pos_integer() | infinity) -> boolean().
set_inactivity(HostType, infinity) ->
gen_mod:set_module_opt(HostType, ?MODULE, inactivity, infinity);
set_inactivity(HostType, Seconds) when is_integer(Seconds), Seconds > 0 ->
gen_mod:set_module_opt(HostType, ?MODULE, inactivity, Seconds).


-spec get_max_wait(jid:lserver()) -> pos_integer() | infinity.
get_max_wait(LServer) ->
gen_mod:get_module_opt(LServer, ?MODULE, max_wait, ?DEFAULT_MAX_WAIT).
-spec get_max_wait(mongooseim:host_type()) -> pos_integer() | infinity.
get_max_wait(HostType) ->
gen_mod:get_module_opt(HostType, ?MODULE, max_wait, ?DEFAULT_MAX_WAIT).


%% @doc Return true if succeeded, false otherwise.
-spec set_max_wait(jid:lserver(), Seconds :: pos_integer() | infinity) -> boolean().
set_max_wait(LServer, infinity) ->
gen_mod:set_module_opt(LServer, ?MODULE, max_wait, infinity);
set_max_wait(LServer, Seconds) when is_integer(Seconds), Seconds > 0 ->
gen_mod:set_module_opt(LServer, ?MODULE, max_wait, Seconds).
-spec set_max_wait(mongooseim:host_type(), Seconds :: pos_integer() | infinity) -> boolean().
set_max_wait(HostType, infinity) ->
gen_mod:set_module_opt(HostType, ?MODULE, max_wait, infinity);
set_max_wait(HostType, Seconds) when is_integer(Seconds), Seconds > 0 ->
gen_mod:set_module_opt(HostType, ?MODULE, max_wait, Seconds).


-spec get_server_acks(jid:lserver()) -> boolean().
get_server_acks(LServer) ->
gen_mod:get_module_opt(LServer, ?MODULE, server_acks, ?DEFAULT_SERVER_ACKS).
-spec get_server_acks(mongooseim:host_type()) -> boolean().
get_server_acks(HostType) ->
gen_mod:get_module_opt(HostType, ?MODULE, server_acks, ?DEFAULT_SERVER_ACKS).


-spec set_server_acks(jid:lserver(), EnableServerAcks :: boolean()) -> boolean().
set_server_acks(LServer, EnableServerAcks) ->
gen_mod:set_module_opt(LServer, ?MODULE, server_acks, EnableServerAcks).
-spec set_server_acks(mongooseim:host_type(), EnableServerAcks :: boolean()) -> boolean().
set_server_acks(HostType, EnableServerAcks) ->
gen_mod:set_module_opt(HostType, ?MODULE, server_acks, EnableServerAcks).

%%--------------------------------------------------------------------
%% gen_mod callbacks
%%--------------------------------------------------------------------

-spec start(jid:server(), [option()]) -> any().
start(_Host, Opts) ->
-spec start(mongooseim:host_type(), [option()]) -> any().
start(_HostType, Opts) ->
try
start_backend(Opts),
{ok, _Pid} = mod_bosh_socket:start_supervisor(),
Expand All @@ -156,7 +157,7 @@ start(_Host, Opts) ->
ErrorReason
end.

stop(_Host) ->
stop(_HostType) ->
ok.

-spec config_spec() -> mongoose_config_spec:config_section().
Expand All @@ -173,6 +174,10 @@ config_spec() ->
}
}.

-spec supported_features() -> [atom()].
supported_features() ->
[dynamic_domains].

%%--------------------------------------------------------------------
%% Hooks handlers
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -368,18 +373,20 @@ get_session_socket(Sid) ->
-spec maybe_start_session(req(), exml:element()) ->
{SessionStarted :: boolean(), req()}.
maybe_start_session(Req, Body) ->
Host = exml_query:attr(Body, <<"to">>),
case is_known_host(Host) of
true ->
maybe_start_session_on_known_host(Host, Req, Body);
false ->
Domain = exml_query:attr(Body, <<"to">>),
case mongoose_domain_api:get_domain_host_type(Domain) of
{ok, HostType} ->
maybe_start_session_on_known_host(HostType, Req, Body);
{error, not_found} ->
Req1 = terminal_condition(<<"host-unknown">>, Req),
{false, Req1}
end.

maybe_start_session_on_known_host(Host, Req, Body) ->
-spec maybe_start_session_on_known_host(mongooseim:host_type(), req(), exml:element()) ->
{SessionStarted :: boolean(), req()}.
maybe_start_session_on_known_host(HostType, Req, Body) ->
try
maybe_start_session_on_known_host_unsafe(Host, Req, Body)
maybe_start_session_on_known_host_unsafe(HostType, Req, Body)
catch
error:Reason ->
%% It's here because something catch-y was here before
Expand All @@ -389,25 +396,22 @@ maybe_start_session_on_known_host(Host, Req, Body) ->
{false, Req1}
end.

maybe_start_session_on_known_host_unsafe(Host, Req, Body) ->
-spec maybe_start_session_on_known_host_unsafe(mongooseim:host_type(), req(), exml:element()) ->
{SessionStarted :: boolean(), req()}.
maybe_start_session_on_known_host_unsafe(HostType, Req, Body) ->
%% Version isn't checked as it would be meaningless when supporting
%% only a subset of the specification.
{ok, NewBody} = set_max_hold(Body),
Peer = cowboy_req:peer(Req),
PeerCert = cowboy_req:cert(Req),
start_session(Host, Peer, PeerCert, NewBody),
start_session(HostType, Peer, PeerCert, NewBody),
{true, Req}.

%% @doc Is the argument locally served host?
is_known_host(Host) ->
Hosts = ejabberd_config:get_global_option(hosts),
lists:member(Host, Hosts).

-spec start_session(jid:lserver(), mongoose_transport:peer(),
-spec start_session(mongooseim:host_type(), mongoose_transport:peer(),
binary() | undefined, exml:element()) -> any().
start_session(LServer, Peer, PeerCert, Body) ->
start_session(HostType, Peer, PeerCert, Body) ->
Sid = make_sid(),
{ok, Socket} = mod_bosh_socket:start(LServer, Sid, Peer, PeerCert),
{ok, Socket} = mod_bosh_socket:start(HostType, Sid, Peer, PeerCert),
store_session(Sid, Socket),
handle_request(Socket, streamstart, Body),
?LOG_DEBUG(#{what => bosh_start_session, sid => Sid}).
Expand Down Expand Up @@ -513,6 +517,7 @@ maybe_set_max_hold(_, _) ->
cowboy_reply(Code, Headers, Body, Req) when is_list(Headers) ->
cowboy_req:reply(Code, maps:from_list(Headers), Body, Req).

config_metrics(Host) ->
OptsToReport = [{backend, mnesia}], %list of tuples {option, defualt_value}
mongoose_module_metrics:opts_for_module(Host, ?MODULE, OptsToReport).
-spec config_metrics(mongooseim:host_type()) -> [option()].
config_metrics(HostType) ->
OptsToReport = [{backend, mnesia}], %list of tuples {option, default_value}
mongoose_module_metrics:opts_for_module(HostType, ?MODULE, OptsToReport).
28 changes: 15 additions & 13 deletions src/mod_bosh_socket.erl
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,18 @@
%% API
%%--------------------------------------------------------------------

-spec start(jid:lserver(), mod_bosh:sid(), mongoose_transport:peer(), binary() | undefined) ->
-spec start(mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(),
binary() | undefined) ->
{'error', _} | {'ok', 'undefined' | pid()} | {'ok', 'undefined' | pid(), _}.
start(LServer, Sid, Peer, PeerCert) ->
supervisor:start_child(?BOSH_SOCKET_SUP, [LServer, Sid, Peer, PeerCert]).
start(HostType, Sid, Peer, PeerCert) ->
supervisor:start_child(?BOSH_SOCKET_SUP, [HostType, Sid, Peer, PeerCert]).


-spec start_link(jid:lserver(), mod_bosh:sid(), mongoose_transport:peer(), binary() | undefined) ->
-spec start_link(mongooseim:host_type(), mod_bosh:sid(), mongoose_transport:peer(),
binary() | undefined) ->
'ignore' | {'error', _} | {'ok', pid()}.
start_link(LServer, Sid, Peer, PeerCert) ->
gen_fsm_compat:start_link(?MODULE, [LServer, Sid, Peer, PeerCert], []).
start_link(HostType, Sid, Peer, PeerCert) ->
gen_fsm_compat:start_link(?MODULE, [HostType, Sid, Peer, PeerCert], []).

-spec start_supervisor() -> {ok, pid()} | {error, any()}.
start_supervisor() ->
Expand Down Expand Up @@ -185,23 +187,23 @@ get_cached_responses(Pid) ->
%% {stop, StopReason}
%% @end
%%--------------------------------------------------------------------
init([LServer, Sid, Peer, PeerCert]) ->
init([HostType, Sid, Peer, PeerCert]) ->
BoshSocket = #bosh_socket{sid = Sid, pid = self(), peer = Peer, peercert = PeerCert},
C2SOpts = [{xml_socket, true}],
{ok, C2SPid} = ejabberd_c2s:start({mod_bosh_socket, BoshSocket}, C2SOpts),
State = #state{sid = Sid,
c2s_pid = C2SPid,
inactivity = mod_bosh:get_inactivity(LServer),
maxpause = get_maxpause(LServer),
max_wait = mod_bosh:get_max_wait(LServer),
server_acks = mod_bosh:get_server_acks(LServer)},
inactivity = mod_bosh:get_inactivity(HostType),
maxpause = get_maxpause(HostType),
max_wait = mod_bosh:get_max_wait(HostType),
server_acks = mod_bosh:get_server_acks(HostType)},
?LOG_DEBUG(ls(#{what => bosh_socket_init}, State)),
{ok, accumulate, State}.


%% TODO: maybe make maxpause runtime configurable like inactivity?
get_maxpause(LServer) ->
case gen_mod:get_module_opt(LServer, mod_bosh, maxpause, undefined) of
get_maxpause(HostType) ->
case gen_mod:get_module_opt(HostType, mod_bosh, maxpause, undefined) of
undefined -> ?DEFAULT_MAXPAUSE;
MP -> MP
end.
Expand Down
9 changes: 5 additions & 4 deletions src/system_metrics/mongoose_module_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

-ignore_xref([behaviour_info/1]).

-callback config_metrics(string()) -> any().
-callback config_metrics(mongooseim:host_type()) -> any().

-optional_callbacks([config_metrics/1]).

opts_for_module(Host, Module, OptsToReport) ->
-spec opts_for_module(mongooseim:host_type(), module(), list()) -> list() | tuple().
opts_for_module(HostType, Module, OptsToReport) ->
try
Opts = gen_mod:get_module_opts(Host, Module),
Opts = gen_mod:get_module_opts(HostType, Module),
lists:map(
fun({OptToReport, DefaultValue}) ->
Value = proplists:get_value(OptToReport, Opts, DefaultValue),
{OptToReport, Value}
end,OptsToReport)
end, OptsToReport)
catch
_:_ -> {none, none}
end.