feat(frame): better log for proxy_protocol config disabled

This commit is contained in:
JimMoen 2022-01-07 14:32:58 +08:00
parent 93ec2ef995
commit 9969fd0d18
3 changed files with 27 additions and 4 deletions

View File

@ -589,6 +589,11 @@ parse_incoming(Data, Packets, State = #state{parse_state = ParseState}) ->
NState = State#state{parse_state = NParseState}, NState = State#state{parse_state = NParseState},
parse_incoming(Rest, [Packet|Packets], NState) parse_incoming(Rest, [Packet|Packets], NState)
catch catch
error:proxy_protocol_config_disabled ->
?LOG(error,
"~nMalformed packet, "
"please check proxy_protocol config for specific listeners and zones~n"),
{[{frame_error, proxy_protocol_config_disabled} | Packets], State};
error:Reason:Stk -> error:Reason:Stk ->
?LOG(error, "~nParse failed for ~0p~n~0p~nFrame data:~0p", ?LOG(error, "~nParse failed for ~0p~n~0p~nFrame data:~0p",
[Reason, Stk, Data]), [Reason, Stk, Data]),
@ -812,4 +817,3 @@ stop(Reason, Reply, State) ->
set_field(Name, Value, State) -> set_field(Name, Value, State) ->
Pos = emqx_misc:index_of(Name, record_info(fields, state)), Pos = emqx_misc:index_of(Name, record_info(fields, state)),
setelement(Pos+1, State, Value). setelement(Pos+1, State, Value).

View File

@ -69,6 +69,13 @@
-define(MULTIPLIER_MAX, 16#200000). -define(MULTIPLIER_MAX, 16#200000).
%% proxy_protocol v1 header human readable
-define(PPV1_PROXY, "PROXY ").
-define(PPV1_PROXY_UNKNOWN, "PROXY UNKNOWN").
%% proxy_protocol v2 header signature:
%% 16#0D,16#0A, 16#0D,16#0A,16#00,16#0D,16#0A,16#51,16#55,16#49,16#54,16#0A
-define(PPV2_HEADER_SIG, "\r\n\r\n\0\r\nQUIT\n").
-dialyzer({no_match, [serialize_utf8_string/2]}). -dialyzer({no_match, [serialize_utf8_string/2]}).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
@ -98,6 +105,13 @@ parse(Bin) ->
-spec(parse(binary(), parse_state()) -> parse_result()). -spec(parse(binary(), parse_state()) -> parse_result()).
parse(<<>>, {none, Options}) -> parse(<<>>, {none, Options}) ->
{more, {none, Options}}; {more, {none, Options}};
parse(<<?PPV1_PROXY, IPVer:5/binary, _Rest/binary>>, {none, _Options})
when IPVer =:= <<"TCP4 ">> orelse IPVer =:= <<"TCP6 ">> ->
error(proxy_protocol_config_disabled);
parse(<<?PPV1_PROXY_UNKNOWN, _Rest/binary>>, {none, _Options}) ->
error(proxy_protocol_config_disabled);
parse(<<?PPV2_HEADER_SIG, _Rest/binary>>, {none, _Options}) ->
error(proxy_protocol_config_disabled);
parse(<<Type:4, Dup:1, QoS:2, Retain:1, Rest/binary>>, parse(<<Type:4, Dup:1, QoS:2, Retain:1, Rest/binary>>,
{none, Options = #{strict_mode := StrictMode}}) -> {none, Options = #{strict_mode := StrictMode}}) ->
%% Validate header if strict mode. %% Validate header if strict mode.

View File

@ -63,7 +63,7 @@
%% Simulate the active_n opt %% Simulate the active_n opt
active_n :: pos_integer(), active_n :: pos_integer(),
%% MQTT Piggyback %% MQTT Piggyback
mqtt_piggyback :: single | multiple, mqtt_piggyback :: single | multiple,
%% Limiter %% Limiter
limiter :: maybe(emqx_limiter:limiter()), limiter :: maybe(emqx_limiter:limiter()),
%% Limit Timer %% Limit Timer
@ -486,6 +486,12 @@ parse_incoming(Data, State = #state{parse_state = ParseState}) ->
NState = State#state{parse_state = NParseState}, NState = State#state{parse_state = NParseState},
parse_incoming(Rest, postpone({incoming, Packet}, NState)) parse_incoming(Rest, postpone({incoming, Packet}, NState))
catch catch
error:proxy_protocol_config_disabled ->
?LOG(error,
"~nMalformed packet, "
"please check proxy_protocol config for specific listeners and zones~n"),
FrameError = {frame_error, proxy_protocol_config_disabled},
postpone({incoming, FrameError} ,State);
error:Reason:Stk -> error:Reason:Stk ->
?LOG(error, "~nParse failed for ~0p~n~0p~nFrame data: ~0p", ?LOG(error, "~nParse failed for ~0p~n~0p~nFrame data: ~0p",
[Reason, Stk, Data]), [Reason, Stk, Data]),
@ -544,7 +550,7 @@ handle_outgoing(Packets, State = #state{active_n = ActiveN, mqtt_piggyback = MQT
postpone({check_gc, Stats}, State); postpone({check_gc, Stats}, State);
false -> State false -> State
end, end,
{case MQTTPiggyback of {case MQTTPiggyback of
single -> [{binary, IoData}]; single -> [{binary, IoData}];
multiple -> lists:map(fun(Bin) -> {binary, Bin} end, IoData) multiple -> lists:map(fun(Bin) -> {binary, Bin} end, IoData)
@ -689,4 +695,3 @@ trigger(Event) -> erlang:send(self(), Event).
set_field(Name, Value, State) -> set_field(Name, Value, State) ->
Pos = emqx_misc:index_of(Name, record_info(fields, state)), Pos = emqx_misc:index_of(Name, record_info(fields, state)),
setelement(Pos+1, State, Value). setelement(Pos+1, State, Value).