1
0
Fork 0
mirror of https://github.com/processone/ejabberd synced 2025-10-03 09:49:18 +02:00

Add options that allow to configure proxy used by rest module

This commit is contained in:
Paweł Chmielowski 2025-05-21 15:37:49 +02:00
parent 18e7805ef5
commit c38b2bfc21
4 changed files with 77 additions and 3 deletions

View file

@ -119,6 +119,10 @@
-export([redis_server/0]). -export([redis_server/0]).
-export([registration_timeout/0]). -export([registration_timeout/0]).
-export([resource_conflict/0, resource_conflict/1]). -export([resource_conflict/0, resource_conflict/1]).
-export([rest_proxy/0, rest_proxy/1]).
-export([rest_proxy_password/0, rest_proxy_password/1]).
-export([rest_proxy_port/0, rest_proxy_port/1]).
-export([rest_proxy_username/0, rest_proxy_username/1]).
-export([router_cache_life_time/0]). -export([router_cache_life_time/0]).
-export([router_cache_missed/0]). -export([router_cache_missed/0]).
-export([router_cache_size/0]). -export([router_cache_size/0]).
@ -833,6 +837,34 @@ resource_conflict() ->
resource_conflict(Host) -> resource_conflict(Host) ->
ejabberd_config:get_option({resource_conflict, Host}). ejabberd_config:get_option({resource_conflict, Host}).
-spec rest_proxy() -> binary().
rest_proxy() ->
rest_proxy(global).
-spec rest_proxy(global | binary()) -> binary().
rest_proxy(Host) ->
ejabberd_config:get_option({rest_proxy, Host}).
-spec rest_proxy_password() -> string().
rest_proxy_password() ->
rest_proxy_password(global).
-spec rest_proxy_password(global | binary()) -> string().
rest_proxy_password(Host) ->
ejabberd_config:get_option({rest_proxy_password, Host}).
-spec rest_proxy_port() -> char().
rest_proxy_port() ->
rest_proxy_port(global).
-spec rest_proxy_port(global | binary()) -> char().
rest_proxy_port(Host) ->
ejabberd_config:get_option({rest_proxy_port, Host}).
-spec rest_proxy_username() -> string().
rest_proxy_username() ->
rest_proxy_username(global).
-spec rest_proxy_username(global | binary()) -> string().
rest_proxy_username(Host) ->
ejabberd_config:get_option({rest_proxy_username, Host}).
-spec router_cache_life_time() -> 'infinity' | pos_integer(). -spec router_cache_life_time() -> 'infinity' | pos_integer().
router_cache_life_time() -> router_cache_life_time() ->
ejabberd_config:get_option({router_cache_life_time, global}). ejabberd_config:get_option({router_cache_life_time, global}).

View file

@ -333,6 +333,14 @@ opt_type(registration_timeout) ->
econf:timeout(second, infinity); econf:timeout(second, infinity);
opt_type(resource_conflict) -> opt_type(resource_conflict) ->
econf:enum([setresource, closeold, closenew, acceptnew]); econf:enum([setresource, closeold, closenew, acceptnew]);
opt_type(rest_proxy) ->
econf:domain();
opt_type(rest_proxy_port) ->
econf:port();
opt_type(rest_proxy_username) ->
econf:string();
opt_type(rest_proxy_password) ->
econf:string();
opt_type(router_cache_life_time) -> opt_type(router_cache_life_time) ->
econf:timeout(second, infinity); econf:timeout(second, infinity);
opt_type(router_cache_missed) -> opt_type(router_cache_missed) ->
@ -652,6 +660,10 @@ options() ->
{redis_server, "localhost"}, {redis_server, "localhost"},
{registration_timeout, timer:seconds(600)}, {registration_timeout, timer:seconds(600)},
{resource_conflict, acceptnew}, {resource_conflict, acceptnew},
{rest_proxy, <<>>},
{rest_proxy_port, 0},
{rest_proxy_username, ""},
{rest_proxy_password, ""},
{router_cache_life_time, {router_cache_life_time,
fun(Host) -> ejabberd_config:get_option({cache_life_time, Host}) end}, fun(Host) -> ejabberd_config:get_option({cache_life_time, Host}) end},
{router_cache_missed, {router_cache_missed,

View file

@ -1194,6 +1194,22 @@ doc() ->
"uses old Jabber Non-SASL authentication (XEP-0078), " "uses old Jabber Non-SASL authentication (XEP-0078), "
"then this option is not respected, and the action performed " "then this option is not respected, and the action performed "
"is 'closeold'.")}}, "is 'closeold'.")}},
{rest_proxy,
#{value => "Host",
desc => ?T("Address of a HTTP Connect proxy used by modules issuing rest calls "
"(like ejabberd_oauth_rest)")}},
{rest_proxy_port,
#{value => "1..65535",
desc => ?T("Port of a HTTP Connect proxy used by modules issuing rest calls "
"(like ejabberd_oauth_rest)")}},
{rest_proxy_username,
#{value => "string()",
desc => ?T("Username used to authenticate to HTTP Connect proxy used by modules issuing rest calls "
"(like ejabberd_oauth_rest)")}},
{rest_proxy_password,
#{value => "string()",
desc => ?T("Password used to authenticate to HTTP Connect proxy used by modules issuing rest calls "
"(like ejabberd_oauth_rest)")}},
{router_cache_life_time, {router_cache_life_time,
#{value => "timeout()", #{value => "timeout()",
desc => desc =>

View file

@ -38,7 +38,14 @@
start(Host) -> start(Host) ->
application:start(inets), application:start(inets),
Size = ejabberd_option:ext_api_http_pool_size(Host), Size = ejabberd_option:ext_api_http_pool_size(Host),
httpc:set_options([{max_sessions, Size}]). Proxy = case {ejabberd_option:rest_proxy(Host),
ejabberd_option:rest_proxy_port(Host)} of
{<<>>, _, _} ->
[];
{Host, Port} ->
[{proxy, {{binary_to_list(Host), Port}, []}}]
end,
httpc:set_options([{max_sessions, Size}] ++ Proxy).
stop(_Host) -> stop(_Host) ->
ok. ok.
@ -87,8 +94,15 @@ request(Server, Method, Path, Params, Mime, Data) ->
_ -> {Params, []} _ -> {Params, []}
end, end,
URI = to_list(url(Server, Path, Query)), URI = to_list(url(Server, Path, Query)),
HttpOpts = [{connect_timeout, ?CONNECT_TIMEOUT}, HttpOpts = case {ejabberd_option:rest_proxy_username(Server),
{timeout, ?HTTP_TIMEOUT}], ejabberd_option:rest_proxy_password(Server)} of
{"", _} -> [{connect_timeout, ?CONNECT_TIMEOUT},
{timeout, ?HTTP_TIMEOUT}];
{User, Pass} ->
[{connect_timeout, ?CONNECT_TIMEOUT},
{timeout, ?HTTP_TIMEOUT},
{proxy_auth, {User, Pass}}]
end,
Hdrs = [{"connection", "keep-alive"}, Hdrs = [{"connection", "keep-alive"},
{"Accept", "application/json"}, {"Accept", "application/json"},
{"User-Agent", "ejabberd"}] {"User-Agent", "ejabberd"}]