mirror of
https://github.com/processone/ejabberd.git
synced 2025-01-01 17:53:00 +01:00
Speed up ejabberd_s2s:is_service/2, allow_host/2 (thanks to Andreas Köhler)(EJAB-1319)
Iterating through the list of possible parent domains of a given domain and comparing with the list of hosts or routes is almost always faster than doing it the other way around. It naturally returns the shortest or longest parent domain satisfying a predicate, whereas the possibly long list compared with would need to be sorted by length first.
This commit is contained in:
parent
95ce77f80d
commit
a5230c46c2
@ -411,19 +411,18 @@ is_service(From, To) ->
|
||||
s2s -> % bypass RFC 3920 10.3
|
||||
false;
|
||||
_ ->
|
||||
LDstDomain = To#jid.lserver,
|
||||
P = fun(Domain) -> is_subdomain(LDstDomain, Domain) end,
|
||||
lists:any(P, ?MYHOSTS)
|
||||
Hosts = ?MYHOSTS,
|
||||
P = fun(ParentDomain) -> lists:member(ParentDomain, Hosts) end,
|
||||
lists:any(P, parent_domains(To#jid.lserver))
|
||||
end.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Function: is_subdomain(Domain1, Domain2) -> true | false
|
||||
%% Description: Return true if Domain1 (a string representing an
|
||||
%% internet domain name) is a subdomain (or the same domain) of
|
||||
%% Domain2
|
||||
%% --------------------------------------------------------------------
|
||||
is_subdomain(Domain1, Domain2) ->
|
||||
lists:suffix(string:tokens(Domain2, "."), string:tokens(Domain1, ".")).
|
||||
parent_domains(Domain) ->
|
||||
lists:foldl(
|
||||
fun(Label, []) ->
|
||||
[Label];
|
||||
(Label, [Head | Tail]) ->
|
||||
[Label ++ "." ++ Head, Head | Tail]
|
||||
end, [], lists:reverse(string:tokens(Domain, "."))).
|
||||
|
||||
send_element(Pid, El) ->
|
||||
Pid ! {send_element, El}.
|
||||
@ -486,10 +485,11 @@ update_tables() ->
|
||||
|
||||
%% Check if host is in blacklist or white list
|
||||
allow_host(MyServer, S2SHost) ->
|
||||
case lists:filter(
|
||||
fun(Host) ->
|
||||
is_subdomain(MyServer, Host)
|
||||
end, ?MYHOSTS) of
|
||||
Hosts = ?MYHOSTS,
|
||||
case lists:dropwhile(
|
||||
fun(ParentDomain) ->
|
||||
not lists:member(ParentDomain, Hosts)
|
||||
end, parent_domains(MyServer)) of
|
||||
[MyHost|_] ->
|
||||
allow_host1(MyHost, S2SHost);
|
||||
[] ->
|
||||
|
Loading…
Reference in New Issue
Block a user