Merge pull request #20 from scrogson/master

Fix mod_rest allowed_ips option to work with new yaml config.
This commit is contained in:
badlop 2014-03-24 19:04:41 +01:00
commit c8dfced84c
1 changed files with 21 additions and 6 deletions

View File

@ -46,11 +46,7 @@ start(_Host, _Opts) ->
stop(_Host) ->
ok.
process([], #request{method = 'POST',
data = Data,
host = Host,
ip = ClientIp
}) ->
process([], #request{method = 'POST', data = Data, host = Host, ip = ClientIp}) ->
try
{ClientAddress, _PortNumber} = ClientIp,
check_member_option(Host, ClientAddress, allowed_ips),
@ -111,7 +107,7 @@ try_get_option(Host, OptionName, DefaultValue) ->
true -> ok;
_ -> throw({module_must_be_started_in_vhost, ?MODULE, Host})
end,
gen_mod:get_module_opt(Host, ?MODULE, OptionName, fun(I) when I -> I end, DefaultValue).
gen_mod:get_module_opt(Host, ?MODULE, OptionName, fun(I) -> I end, DefaultValue).
get_option_access(Host) ->
try_get_option(Host, access_commands, []).
@ -123,12 +119,31 @@ check_stanza(Stanza, _From, To, Host) ->
check_member_option(Host, StanzaType, allowed_stanza_types),
allowed.
check_member_option(Host, ClientIp, allowed_ips) ->
true = case try_get_option(Host, allowed_ips, all) of
all -> true;
AllowedValues ->
case lists:all(fun(El) -> is_binary(El) end, AllowedValues) of
true ->
AllowedIps = lists:map(fun(El) ->
binary_to_ip_tuple(El)
end,
AllowedValues),
lists:member(ClientIp, AllowedIps);
false ->
lists:member(ClientIp, AllowedValues)
end
end;
check_member_option(Host, Element, Option) ->
true = case try_get_option(Host, Option, all) of
all -> true;
AllowedValues -> lists:member(Element, AllowedValues)
end.
binary_to_ip_tuple(IpAddress) when is_binary(IpAddress) ->
{ok, IpTuple} = inet_parse:address(binary_to_list(IpAddress)),
IpTuple.
post_request(Stanza, From, To) ->
case ejabberd_router:route(From, To, Stanza) of
ok -> {200, [], <<"Ok">>};