-
Notifications
You must be signed in to change notification settings - Fork 428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spam control #1123
Open
bartekgorny
wants to merge
8
commits into
master
Choose a base branch
from
spam-control
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Spam control #1123
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8165bb8
starting
bartekgorny d82a366
sample config
bartekgorny 50f403c
cleanup and improvements
bartekgorny ce3bc19
bugfix - oftentimes there is no decision, e.g. because the stanza is …
bartekgorny 1d26c6f
Fixed return value from handler (sometimes c2s would stop and we need…
bartekgorny dfdcf16
just to make life easier
bartekgorny e975808
docs, renaming, cleanup
bartekgorny 3d0cc0c
a cleaner way to terminate connection; and, we make sure a stream tra…
bartekgorny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
%%%------------------------------------------------------------------- | ||
%%% @author bartek | ||
%%% @copyright (C) 2016, <COMPANY> | ||
%%% @doc | ||
%%% | ||
%%% @end | ||
%%% Created : 23. Oct 2016 11:19 | ||
%%%------------------------------------------------------------------- | ||
-module(mod_spamctl). | ||
-author("bartek"). | ||
-behaviour(gen_mod). | ||
|
||
%% API | ||
-export([start/2, stop/1, initialise/1, control/3]). | ||
-export([cutoff/5, notify_offender/5, notify_admin/5]). | ||
|
||
-include("ejabberd.hrl"). | ||
-include("jlib.hrl"). | ||
|
||
-define(DEFAULT_POOL_NAME, http_pool). | ||
-define(DEFAULT_PATH, ""). | ||
|
||
-type state() :: map(). % whatever you want it to be | ||
-type decision() :: atom(). | ||
|
||
start(Host, _Opts) -> | ||
ejabberd_hooks:add(spamctl_initialise, Host, | ||
?MODULE, initialise, 50), | ||
ejabberd_hooks:add(spamctl_control, Host, | ||
?MODULE, control, 50), | ||
ejabberd_hooks:add(spamctl_react, Host, | ||
?MODULE, notify_offender, 30), | ||
ejabberd_hooks:add(spamctl_react, Host, | ||
?MODULE, notify_admin, 40), | ||
ejabberd_hooks:add(spamctl_react, Host, | ||
?MODULE, cutoff, 50), | ||
ok. | ||
|
||
stop(Host) -> | ||
ejabberd_hooks:delete(spamctl_initialise, Host, | ||
?MODULE, initialise, 50), | ||
ejabberd_hooks:delete(spamctl_control, Host, | ||
?MODULE, control, 50), | ||
ejabberd_hooks:delete(spamctl_react, Host, | ||
?MODULE, notify_offender, 30), | ||
ejabberd_hooks:delete(spamctl_react, Host, | ||
?MODULE, notify_admin, 40), | ||
ejabberd_hooks:delete(spamctl_react, Host, | ||
?MODULE, cutoff, 50), | ||
ok. | ||
|
||
%% @doc Triggered by `spamctl_initialise` when stream is started, returns initial spamcontrol state. | ||
-spec initialise(map()) -> state(). | ||
initialise(#{host := Host} = State) -> | ||
MaxRate = gen_mod:get_module_opt(Host, ?MODULE, maxrate, 10), | ||
Span = gen_mod:get_module_opt(Host, ?MODULE, span, 2), | ||
ModOpt = #{maxrate => MaxRate, span => Span}, | ||
% merge config options into accumulator | ||
NState = maps:merge(State, ModOpt), | ||
% add working values used by this module | ||
ModState = #{rate => 0, | ||
decision => ok, | ||
lasttime => usec:from_now(os:timestamp())}, | ||
maps:merge(NState, ModState). | ||
|
||
%% @doc Triggered by `spamctl_control` every time the user sends a stanza; returns a | ||
%% modified spamcontrol state. The state MUST contain key `decision` which normally is | ||
%% `ok` - anything else means that the user violated spamcontrol rules and tells c2s | ||
%% to run `spamctl_react` hook. | ||
-spec control(state(), binary(), xmlel()) -> state(). | ||
control(State, Name, M) -> | ||
NState = check_msg(Name, M, State), | ||
case NState of | ||
#{decision := excess} -> | ||
{stop, NState}; | ||
_ -> | ||
NState | ||
end. | ||
|
||
check_msg(<<"message">>, M, State) -> | ||
Now = usec:from_now(os:timestamp()), | ||
Span = maps:get(span, State) * 1000000, | ||
Lasttime = maps:get(lasttime, State), | ||
Cycled = Now - Lasttime > Span, | ||
check_msg(M, State, Now, Cycled); | ||
check_msg(_, _, State) -> | ||
State. | ||
|
||
check_msg(_M, State, Now, true) -> | ||
set_decision(ok, Now, State); | ||
check_msg(M, State, Now, false) -> | ||
NRate = maps:get(rate, State) + 1, | ||
check_msg_rate(M, Now, State#{rate => NRate}). | ||
|
||
check_msg_rate(_M, Now, #{maxrate := Max, rate := Rate} = State) when Rate > Max -> | ||
set_decision(excess, Now, State); | ||
check_msg_rate(_M, _Now, State) -> | ||
State. | ||
|
||
set_decision(Dec, Now, State) -> | ||
State#{decision => Dec, lasttime => Now, rate => 0}. | ||
|
||
%% @doc if a msg is determined to be spam, for any reason, we call this | ||
%% to terminate the user connection | ||
-spec cutoff(state(), decision(), jid(), jid(), xmlel()) -> state(). | ||
cutoff(State, excess, _From, _To, _Msg) -> | ||
p1_fsm_old:send_event(self(), stop), | ||
State; | ||
cutoff(State, _, _, _, _) -> | ||
State. | ||
|
||
%% @doc if a msg is determined to be spam, for any reason, we call this to notify | ||
%% the user he is not welcome | ||
-spec notify_offender(state(), decision(), jid(), jid(), xmlel()) -> state(). | ||
notify_offender(State, excess, From, To, Msg) -> | ||
send_back_error(?ERR_NOT_ACCEPTABLE, From, To, Msg), | ||
State; | ||
notify_offender(State, _, _, _, _) -> | ||
State. | ||
|
||
send_back_error(Etype, From, To, Packet) -> | ||
Err = jlib:make_error_reply(Packet, Etype), | ||
ejabberd_router:route(To, From, Err). | ||
|
||
%% @doc if a msg is determined to be spam, for any reason, we call this to notify | ||
%% some external service about what happened | ||
-spec notify_admin(state(), decision(), jid(), jid(), xmlel()) -> state(). | ||
notify_admin(State, excess, From, _To, _Msg) -> | ||
C = jid:to_binary({From#jid.user, From#jid.server}), | ||
send_http_notification(maps:get(host, State), C, <<"exceeded message limit">>), | ||
State. | ||
|
||
send_http_notification(Host, Culprit, Body) -> | ||
Path = fix_path(list_to_binary(gen_mod:get_module_opt(Host, ?MODULE, path, ?DEFAULT_PATH))), | ||
PoolName = gen_mod:get_module_opt(Host, ?MODULE, pool_name, ?DEFAULT_POOL_NAME), | ||
Pool = mongoose_http_client:get_pool(PoolName), | ||
Query = <<"culprit=", Culprit/binary, "&message=", Body/binary>>, | ||
?INFO_MSG("Making request '~p' for user ~s@~s...", [Path, Culprit, Host]), | ||
Headers = [{<<"Content-Type">>, <<"application/x-www-form-urlencoded">>}], | ||
case mongoose_http_client:post(Pool, Path, Headers, Query) of | ||
{ok, _} -> | ||
ok; | ||
{error, E} -> | ||
?ERROR_MSG("Failed to record spam policy violation (~p):~nOffender: ~p, message: ~p~n", | ||
[E, Culprit, Body]) | ||
end. | ||
|
||
|
||
fix_path(<<"/", R/binary>>) -> | ||
R; | ||
fix_path(R) -> | ||
R. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
-module(spamctl_SUITE). | ||
-compile([export_all]). | ||
|
||
-include_lib("exml/include/exml.hrl"). | ||
-include_lib("eunit/include/eunit.hrl"). | ||
|
||
|
||
all() -> | ||
[ burst_ctl ]. | ||
|
||
init_per_suite(C) -> | ||
application:ensure_all_started(lager), | ||
C. | ||
|
||
end_per_suite(_C) -> | ||
ok. | ||
|
||
burst_ctl(_C) -> | ||
% initialise with 10 msgs over 2 seconds | ||
State = #{maxrate => 10, | ||
span => 2, | ||
rate => 0, | ||
decision => ok, | ||
lasttime => now_to_usec()}, | ||
State1 = proc_msgs(State, 8), | ||
State2 = proc_msgs(State1, 1), | ||
#{decision := ok} = State2, | ||
State3 = proc_msgs(State2, 1), | ||
#{decision := ok} = State3, | ||
State4 = proc_msgs(State3, 1), | ||
#{decision := excess} = State4, | ||
State5 = proc_msgs(State4, 1), | ||
#{decision := excess} = State5, | ||
timer:sleep(2100), | ||
State7 = proc_msgs(State5, 8), | ||
#{decision := ok} = State7, | ||
ok. | ||
|
||
proc_msgs(State, 0) -> | ||
State; | ||
proc_msgs(State, Y) -> | ||
M = #xmlel{name = <<"message">>, | ||
attrs = [{<<"type">>, <<"chat">>}, {<<"to">>, <<"bob37.76184@localhost">>}], | ||
children = [#xmlel{name = <<"body">>, | ||
attrs = [], | ||
children = [{xmlcdata, <<Y/integer>>}]}]}, | ||
NState = case mod_spamctl:control(State, <<"message">>, [M]) of | ||
{stop, S} -> S; | ||
S -> S | ||
end, | ||
proc_msgs(NState, Y - 1). | ||
|
||
|
||
now_to_usec() -> | ||
{MSec, Sec, USec} = os:timestamp(), | ||
(MSec * 1000000 + Sec) * 1000000 + USec. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These
spamctl_*
set of hooks are quite powerful. How about passing whole c2s state as an arg to theinitialise
hook handler so the implementation may choose what to do with it? Other solution would be passing the state to all the hooks. I can imagine that some clever logic (rewording or stopping the user) may need some of the fields which are in c2s state.