diff --git a/mod_rest/src/mod_rest.erl b/mod_rest/src/mod_rest.erl index fab4a59..152ee0b 100644 --- a/mod_rest/src/mod_rest.erl +++ b/mod_rest/src/mod_rest.erl @@ -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">>};