2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_proxy65.erl
|
|
|
|
%%% Author : Evgeniy Khramtsov <xram@jabber.ru>
|
|
|
|
%%% Purpose : Main supervisor.
|
|
|
|
%%% Created : 12 Oct 2006 by Evgeniy Khramtsov <xram@jabber.ru>
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2017-01-02 21:41:53 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2017 ProcessOne
|
2007-12-24 14:57:53 +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.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% 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.
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
2006-10-28 04:04:55 +02:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_proxy65).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-author('xram@jabber.ru').
|
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 65, '1.8'}).
|
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-behaviour(gen_mod).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
-behaviour(supervisor).
|
|
|
|
|
|
|
|
%% gen_mod callbacks.
|
2017-02-22 17:46:47 +01:00
|
|
|
-export([start/2, stop/1, reload/3, transform_module_options/1]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
%% supervisor callbacks.
|
|
|
|
-export([init/1]).
|
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
-export([start_link/2, mod_opt_type/1, depends/2]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
-define(PROCNAME, ejabberd_mod_proxy65).
|
|
|
|
|
2017-01-16 13:28:11 +01:00
|
|
|
-callback init() -> any().
|
|
|
|
-callback register_stream(binary(), pid()) -> ok | {error, any()}.
|
|
|
|
-callback unregister_stream(binary()) -> ok | {error, any()}.
|
|
|
|
-callback activate_stream(binary(), binary(), pos_integer() | infinity, node()) ->
|
|
|
|
ok | {error, limit | conflict | notfound | term()}.
|
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
start(Host, Opts) ->
|
2017-04-30 18:01:47 +02:00
|
|
|
{ListenOpts, ModOpts} = lists:partition(
|
|
|
|
fun({auth_type, _}) -> true;
|
|
|
|
({recbuf, _}) -> true;
|
|
|
|
({sndbuf, _}) -> true;
|
|
|
|
({shaper, _}) -> true;
|
|
|
|
(_) -> false
|
|
|
|
end, Opts),
|
|
|
|
case mod_proxy65_service:add_listener(Host, ListenOpts) of
|
2017-02-13 09:11:41 +01:00
|
|
|
{error, _} = Err ->
|
|
|
|
Err;
|
|
|
|
_ ->
|
|
|
|
Mod = gen_mod:ram_db_mod(global, ?MODULE),
|
|
|
|
Mod:init(),
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2017-04-30 18:01:47 +02:00
|
|
|
ChildSpec = {Proc, {?MODULE, start_link, [Host, ModOpts]},
|
2017-02-13 09:11:41 +01:00
|
|
|
transient, infinity, supervisor, [?MODULE]},
|
2017-02-24 10:05:47 +01:00
|
|
|
supervisor:start_child(ejabberd_gen_mod_sup, ChildSpec)
|
2010-11-17 10:13:19 +01:00
|
|
|
end.
|
2006-10-28 04:04:55 +02:00
|
|
|
|
|
|
|
stop(Host) ->
|
2017-02-24 14:31:39 +01:00
|
|
|
case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of
|
|
|
|
false ->
|
|
|
|
mod_proxy65_service:delete_listener(Host);
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end,
|
2006-10-28 04:04:55 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2017-02-24 10:05:47 +01:00
|
|
|
supervisor:terminate_child(ejabberd_gen_mod_sup, Proc),
|
|
|
|
supervisor:delete_child(ejabberd_gen_mod_sup, Proc).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2017-02-22 17:46:47 +01:00
|
|
|
reload(Host, NewOpts, OldOpts) ->
|
|
|
|
Mod = gen_mod:ram_db_mod(global, ?MODULE),
|
|
|
|
Mod:init(),
|
|
|
|
mod_proxy65_service:reload(Host, NewOpts, OldOpts).
|
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
start_link(Host, Opts) ->
|
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
2013-03-14 10:33:02 +01:00
|
|
|
supervisor:start_link({local, Proc}, ?MODULE,
|
|
|
|
[Host, Opts]).
|
2006-10-28 04:04:55 +02:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_module_options(Opts) ->
|
|
|
|
mod_proxy65_service:transform_module_options(Opts).
|
|
|
|
|
2006-10-28 04:04:55 +02:00
|
|
|
init([Host, Opts]) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Service = {mod_proxy65_service,
|
|
|
|
{mod_proxy65_service, start_link, [Host, Opts]},
|
|
|
|
transient, 5000, worker, [mod_proxy65_service]},
|
|
|
|
StreamSupervisor = {ejabberd_mod_proxy65_sup,
|
|
|
|
{ejabberd_tmp_sup, start_link,
|
|
|
|
[gen_mod:get_module_proc(Host,
|
|
|
|
ejabberd_mod_proxy65_sup),
|
|
|
|
mod_proxy65_stream]},
|
|
|
|
transient, infinity, supervisor, [ejabberd_tmp_sup]},
|
|
|
|
{ok,
|
|
|
|
{{one_for_one, 10, 1},
|
2017-01-16 13:28:11 +01:00
|
|
|
[StreamSupervisor, Service]}}.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2016-11-15 18:14:21 +01:00
|
|
|
mod_opt_type(access) -> fun acl:access_rules_validator/1;
|
2015-06-03 15:05:17 +02:00
|
|
|
mod_opt_type(host) -> fun iolist_to_binary/1;
|
2017-08-08 16:46:26 +02:00
|
|
|
mod_opt_type(hosts) ->
|
|
|
|
fun(L) -> lists:map(fun iolist_to_binary/1, L) end;
|
2015-06-03 15:05:17 +02:00
|
|
|
mod_opt_type(hostname) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(ip) ->
|
|
|
|
fun (S) ->
|
|
|
|
{ok, Addr} =
|
|
|
|
inet_parse:address(binary_to_list(iolist_to_binary(S))),
|
|
|
|
Addr
|
|
|
|
end;
|
|
|
|
mod_opt_type(name) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(port) ->
|
|
|
|
fun (P) when is_integer(P), P > 0, P < 65536 -> P end;
|
|
|
|
mod_opt_type(max_connections) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I;
|
|
|
|
(infinity) -> infinity
|
|
|
|
end;
|
2017-01-16 13:28:11 +01:00
|
|
|
mod_opt_type(ram_db_type) ->
|
|
|
|
fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2017-04-30 18:01:47 +02:00
|
|
|
mod_opt_type(Opt) ->
|
|
|
|
case mod_proxy65_stream:listen_opt_type(Opt) of
|
|
|
|
Opts when is_list(Opts) ->
|
2017-08-08 16:46:26 +02:00
|
|
|
[access, host, hosts, hostname, ip, name, port,
|
2017-04-30 18:01:47 +02:00
|
|
|
max_connections, ram_db_type] ++ Opts;
|
|
|
|
Fun ->
|
|
|
|
Fun
|
|
|
|
end.
|