diff --git a/src/mtp_handler.erl b/src/mtp_handler.erl index 32452a3..d58aeb3 100644 --- a/src/mtp_handler.erl +++ b/src/mtp_handler.erl @@ -135,14 +135,15 @@ handle_cast({proxy_ans, Down, Data}, #state{down = Down, srv_error_filter = off} {ok, S1} = up_send(Data, S), ok = mtp_down_conn:ack(Down, 1, iolist_size(Data)), maybe_check_health(bump_timer(S1)); -handle_cast({proxy_ans, Down, ?SRV_ERROR = Data}, #state{down = Down, srv_error_filter = Filter, - addr = {Ip, _}} = S) when Filter =/= off -> +handle_cast({proxy_ans, Down, ?SRV_ERROR = Data}, + #state{down = Down, srv_error_filter = Filter, listener = Listener, + addr = {Ip, _}} = S) when Filter =/= off -> %% telegram server -> proxy %% Server replied with server error; it might be another kind of replay attack; %% Don't send this packet to client so proxy won't be fingerprinted ok = mtp_down_conn:ack(Down, 1, iolist_size(Data)), ?log(warning, "~s: protocol_error srv_error_filtered", [inet:ntoa(Ip)]), - mtp_metric:count_inc([?APP, protocol_error, total], 1, #{labels => [srv_error_filtered]}), + mtp_metric:count_inc([?APP, protocol_error, total], 1, #{labels => [Listener, srv_error_filtered]}), {noreply, case Filter of first -> S#state{srv_error_filter = off}; @@ -182,7 +183,7 @@ handle_info({tcp, Sock, Data}, #state{sock = Sock, transport = Transport, %% Consider checking health here as well {noreply, bump_timer(S1)} catch error:{protocol_error, Type, Extra} -> - mtp_metric:count_inc([?APP, protocol_error, total], 1, #{labels => [Type]}), + mtp_metric:count_inc([?APP, protocol_error, total], 1, #{labels => [Listener, Type]}), ?log(warning, "~s: protocol_error ~p ~p", [inet:ntoa(Ip), Type, Extra]), {stop, normal, maybe_close_down(S)} end; @@ -333,7 +334,7 @@ parse_upstream_data(<>, stage = tunnel}, hibernate)); {error, Reason} when is_atom(Reason) -> - mtp_metric:count_inc([?APP, protocol_error, total], 1, #{labels => [Reason]}), + mtp_metric:count_inc([?APP, protocol_error, total], 1, #{labels => [Listener, Reason]}), error({protocol_error, Reason, Header}) end; parse_upstream_data(Bin, #state{stage = Stage, codec = Codec0} = S) when Stage =/= tunnel -> diff --git a/src/mtp_metric.erl b/src/mtp_metric.erl index c2a5717..5fec7cb 100644 --- a/src/mtp_metric.erl +++ b/src/mtp_metric.erl @@ -158,7 +158,7 @@ active_metrics() -> {count, [?APP, protocol_error, total], "Proxy protocol errors", - #{labels => [reason]}}, + #{labels => [listener, reason]}}, {count, [?APP, protocol_ok, total], "Proxy upstream protocol type", #{labels => [listener, protocol]}}, diff --git a/test/single_dc_SUITE.erl b/test/single_dc_SUITE.erl index 1784d6a..05a3579 100644 --- a/test/single_dc_SUITE.erl +++ b/test/single_dc_SUITE.erl @@ -128,7 +128,7 @@ packet_too_large_case(Cfg) when is_list(Cfg) -> Port = ?config(mtp_port, Cfg), Secret = ?config(mtp_secret, Cfg), ErrCount = fun(Tag) -> - mtp_test_metric:get_tags(count, [?APP, protocol_error, total], [Tag]) + mtp_test_metric:get_tags(count, [?APP, protocol_error, total], [?FUNCTION_NAME, Tag]) end, OkPacket = binary:copy(<<0>>, 64), BigPacket = binary:copy(<<0>>, 1024 * 1024 + 1024), @@ -333,7 +333,7 @@ replay_attack_case(Cfg) when is_list(Cfg) -> Seed = crypto:strong_rand_bytes(58), ErrCount = fun() -> mtp_test_metric:get_tags( - count, [?APP, protocol_error, total], [replay_session_detected]) + count, [?APP, protocol_error, total], [?FUNCTION_NAME, replay_session_detected]) end, ?assertEqual(not_found, ErrCount()), Cli1 = mtp_test_client:connect(Host, Port, Seed, Secret, DcId, mtp_secure), @@ -342,7 +342,7 @@ replay_attack_case(Cfg) when is_list(Cfg) -> Cli2 = mtp_test_client:connect(Host, Port, Seed, Secret, DcId, mtp_secure), ?assertEqual( ok, mtp_test_metric:wait_for_value( - count, [?APP, protocol_error, total], [replay_session_detected], 1, 5000), + count, [?APP, protocol_error, total], [?FUNCTION_NAME, replay_session_detected], 1, 5000), {mtp_session_storage:status(), sys:get_state(mtp_test_metric)}), ?assertEqual(1, ErrCount()), @@ -361,7 +361,7 @@ replay_attack_server_error_case(Cfg) when is_list(Cfg) -> Secret = ?config(mtp_secret, Cfg), ErrCount = fun() -> mtp_test_metric:get_tags( - count, [?APP, protocol_error, total], [srv_error_filtered]) + count, [?APP, protocol_error, total], [?FUNCTION_NAME, srv_error_filtered]) end, ?assertEqual(not_found, ErrCount()), Cli1 = mtp_test_client:connect(Host, Port, Secret, DcId, mtp_secure), @@ -369,7 +369,7 @@ replay_attack_server_error_case(Cfg) when is_list(Cfg) -> _Cli2 = mtp_test_client:send(<<108, 254, 255, 255>>, Cli1), ?assertEqual( ok, mtp_test_metric:wait_for_value( - count, [?APP, protocol_error, total], [srv_error_filtered], 1, 5000), + count, [?APP, protocol_error, total], [?FUNCTION_NAME, srv_error_filtered], 1, 5000), {mtp_session_storage:status(), sys:get_state(mtp_test_metric)}), ?assertEqual(1, ErrCount()).