Add 'listener' to `protocol_error` metric

parent e28e21a7
...@@ -135,14 +135,15 @@ handle_cast({proxy_ans, Down, Data}, #state{down = Down, srv_error_filter = off} ...@@ -135,14 +135,15 @@ handle_cast({proxy_ans, Down, Data}, #state{down = Down, srv_error_filter = off}
{ok, S1} = up_send(Data, S), {ok, S1} = up_send(Data, S),
ok = mtp_down_conn:ack(Down, 1, iolist_size(Data)), ok = mtp_down_conn:ack(Down, 1, iolist_size(Data)),
maybe_check_health(bump_timer(S1)); maybe_check_health(bump_timer(S1));
handle_cast({proxy_ans, Down, ?SRV_ERROR = Data}, #state{down = Down, srv_error_filter = Filter, handle_cast({proxy_ans, Down, ?SRV_ERROR = Data},
addr = {Ip, _}} = S) when Filter =/= off -> #state{down = Down, srv_error_filter = Filter, listener = Listener,
addr = {Ip, _}} = S) when Filter =/= off ->
%% telegram server -> proxy %% telegram server -> proxy
%% Server replied with server error; it might be another kind of replay attack; %% 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 %% Don't send this packet to client so proxy won't be fingerprinted
ok = mtp_down_conn:ack(Down, 1, iolist_size(Data)), ok = mtp_down_conn:ack(Down, 1, iolist_size(Data)),
?log(warning, "~s: protocol_error srv_error_filtered", [inet:ntoa(Ip)]), ?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, {noreply,
case Filter of case Filter of
first -> S#state{srv_error_filter = off}; first -> S#state{srv_error_filter = off};
...@@ -182,7 +183,7 @@ handle_info({tcp, Sock, Data}, #state{sock = Sock, transport = Transport, ...@@ -182,7 +183,7 @@ handle_info({tcp, Sock, Data}, #state{sock = Sock, transport = Transport,
%% Consider checking health here as well %% Consider checking health here as well
{noreply, bump_timer(S1)} {noreply, bump_timer(S1)}
catch error:{protocol_error, Type, Extra} -> 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]), ?log(warning, "~s: protocol_error ~p ~p", [inet:ntoa(Ip), Type, Extra]),
{stop, normal, maybe_close_down(S)} {stop, normal, maybe_close_down(S)}
end; end;
...@@ -333,7 +334,7 @@ parse_upstream_data(<<Header:64/binary, Rest/binary>>, ...@@ -333,7 +334,7 @@ parse_upstream_data(<<Header:64/binary, Rest/binary>>,
stage = tunnel}, stage = tunnel},
hibernate)); hibernate));
{error, Reason} when is_atom(Reason) -> {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}) error({protocol_error, Reason, Header})
end; end;
parse_upstream_data(Bin, #state{stage = Stage, codec = Codec0} = S) when Stage =/= tunnel -> parse_upstream_data(Bin, #state{stage = Stage, codec = Codec0} = S) when Stage =/= tunnel ->
......
...@@ -158,7 +158,7 @@ active_metrics() -> ...@@ -158,7 +158,7 @@ active_metrics() ->
{count, [?APP, protocol_error, total], {count, [?APP, protocol_error, total],
"Proxy protocol errors", "Proxy protocol errors",
#{labels => [reason]}}, #{labels => [listener, reason]}},
{count, [?APP, protocol_ok, total], {count, [?APP, protocol_ok, total],
"Proxy upstream protocol type", "Proxy upstream protocol type",
#{labels => [listener, protocol]}}, #{labels => [listener, protocol]}},
......
...@@ -128,7 +128,7 @@ packet_too_large_case(Cfg) when is_list(Cfg) -> ...@@ -128,7 +128,7 @@ packet_too_large_case(Cfg) when is_list(Cfg) ->
Port = ?config(mtp_port, Cfg), Port = ?config(mtp_port, Cfg),
Secret = ?config(mtp_secret, Cfg), Secret = ?config(mtp_secret, Cfg),
ErrCount = fun(Tag) -> 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, end,
OkPacket = binary:copy(<<0>>, 64), OkPacket = binary:copy(<<0>>, 64),
BigPacket = binary:copy(<<0>>, 1024 * 1024 + 1024), BigPacket = binary:copy(<<0>>, 1024 * 1024 + 1024),
...@@ -333,7 +333,7 @@ replay_attack_case(Cfg) when is_list(Cfg) -> ...@@ -333,7 +333,7 @@ replay_attack_case(Cfg) when is_list(Cfg) ->
Seed = crypto:strong_rand_bytes(58), Seed = crypto:strong_rand_bytes(58),
ErrCount = fun() -> ErrCount = fun() ->
mtp_test_metric:get_tags( mtp_test_metric:get_tags(
count, [?APP, protocol_error, total], [replay_session_detected]) count, [?APP, protocol_error, total], [?FUNCTION_NAME, replay_session_detected])
end, end,
?assertEqual(not_found, ErrCount()), ?assertEqual(not_found, ErrCount()),
Cli1 = mtp_test_client:connect(Host, Port, Seed, Secret, DcId, mtp_secure), Cli1 = mtp_test_client:connect(Host, Port, Seed, Secret, DcId, mtp_secure),
...@@ -342,7 +342,7 @@ replay_attack_case(Cfg) when is_list(Cfg) -> ...@@ -342,7 +342,7 @@ replay_attack_case(Cfg) when is_list(Cfg) ->
Cli2 = mtp_test_client:connect(Host, Port, Seed, Secret, DcId, mtp_secure), Cli2 = mtp_test_client:connect(Host, Port, Seed, Secret, DcId, mtp_secure),
?assertEqual( ?assertEqual(
ok, mtp_test_metric:wait_for_value( 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(), {mtp_session_storage:status(),
sys:get_state(mtp_test_metric)}), sys:get_state(mtp_test_metric)}),
?assertEqual(1, ErrCount()), ?assertEqual(1, ErrCount()),
...@@ -361,7 +361,7 @@ replay_attack_server_error_case(Cfg) when is_list(Cfg) -> ...@@ -361,7 +361,7 @@ replay_attack_server_error_case(Cfg) when is_list(Cfg) ->
Secret = ?config(mtp_secret, Cfg), Secret = ?config(mtp_secret, Cfg),
ErrCount = fun() -> ErrCount = fun() ->
mtp_test_metric:get_tags( mtp_test_metric:get_tags(
count, [?APP, protocol_error, total], [srv_error_filtered]) count, [?APP, protocol_error, total], [?FUNCTION_NAME, srv_error_filtered])
end, end,
?assertEqual(not_found, ErrCount()), ?assertEqual(not_found, ErrCount()),
Cli1 = mtp_test_client:connect(Host, Port, Secret, DcId, mtp_secure), 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) -> ...@@ -369,7 +369,7 @@ replay_attack_server_error_case(Cfg) when is_list(Cfg) ->
_Cli2 = mtp_test_client:send(<<108, 254, 255, 255>>, Cli1), _Cli2 = mtp_test_client:send(<<108, 254, 255, 255>>, Cli1),
?assertEqual( ?assertEqual(
ok, mtp_test_metric:wait_for_value( 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(), {mtp_session_storage:status(),
sys:get_state(mtp_test_metric)}), sys:get_state(mtp_test_metric)}),
?assertEqual(1, ErrCount()). ?assertEqual(1, ErrCount()).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment