fix(mgmt_api): client ip and port as two fields when clients query

This commit is contained in:
JimMoen 2021-10-12 09:32:59 +08:00 committed by Rory Z
parent bd53c488d1
commit 6fb1d22e0c
2 changed files with 18 additions and 17 deletions

View File

@ -280,9 +280,9 @@ pick_params_to_qs([], _, Acc1, Acc2) ->
NAcc2 = [E || E <- Acc2, not lists:keymember(element(1, E), 1, Acc1)], NAcc2 = [E || E <- Acc2, not lists:keymember(element(1, E), 1, Acc1)],
{lists:reverse(Acc1), lists:reverse(NAcc2)}; {lists:reverse(Acc1), lists:reverse(NAcc2)};
pick_params_to_qs([{Key, Value}|Params], QsKits, Acc1, Acc2) -> pick_params_to_qs([{Key, Value} | Params], QsSchema, Acc1, Acc2) ->
case proplists:get_value(Key, QsKits) of case proplists:get_value(Key, QsSchema) of
undefined -> pick_params_to_qs(Params, QsKits, Acc1, Acc2); undefined -> pick_params_to_qs(Params, QsSchema, Acc1, Acc2);
Type -> Type ->
case Key of case Key of
<<Prefix:4/binary, NKey/binary>> <<Prefix:4/binary, NKey/binary>>
@ -294,16 +294,16 @@ pick_params_to_qs([{Key, Value}|Params], QsKits, Acc1, Acc2) ->
end, end,
case lists:keytake(OpposeKey, 1, Params) of case lists:keytake(OpposeKey, 1, Params) of
false -> false ->
pick_params_to_qs(Params, QsKits, [qs(Key, Value, Type) | Acc1], Acc2); pick_params_to_qs(Params, QsSchema, [qs(Key, Value, Type) | Acc1], Acc2);
{value, {K2, V2}, NParams} -> {value, {K2, V2}, NParams} ->
pick_params_to_qs(NParams, QsKits, [qs(Key, Value, K2, V2, Type) | Acc1], Acc2) pick_params_to_qs(NParams, QsSchema, [qs(Key, Value, K2, V2, Type) | Acc1], Acc2)
end; end;
_ -> _ ->
case is_fuzzy_key(Key) of case is_fuzzy_key(Key) of
true -> true ->
pick_params_to_qs(Params, QsKits, Acc1, [qs(Key, Value, Type) | Acc2]); pick_params_to_qs(Params, QsSchema, Acc1, [qs(Key, Value, Type) | Acc2]);
_ -> _ ->
pick_params_to_qs(Params, QsKits, [qs(Key, Value, Type) | Acc1], Acc2) pick_params_to_qs(Params, QsSchema, [qs(Key, Value, Type) | Acc1], Acc2)
end end
end end

View File

@ -55,7 +55,7 @@
[ {<<"node">>, atom} [ {<<"node">>, atom}
, {<<"username">>, binary} , {<<"username">>, binary}
, {<<"zone">>, atom} , {<<"zone">>, atom}
, {<<"ip_address">>, ip_port} , {<<"ip_address">>, ip}
, {<<"conn_state">>, atom} , {<<"conn_state">>, atom}
, {<<"clean_start">>, atom} , {<<"clean_start">>, atom}
, {<<"proto_name">>, binary} , {<<"proto_name">>, binary}
@ -114,6 +114,7 @@ properties(client) ->
{inflight_cnt, integer, <<"Current length of inflight">>}, {inflight_cnt, integer, <<"Current length of inflight">>},
{inflight_max, integer, <<"v4 api name [max_inflight]. Maximum length of inflight">>}, {inflight_max, integer, <<"v4 api name [max_inflight]. Maximum length of inflight">>},
{ip_address, string , <<"Client's IP address">>}, {ip_address, string , <<"Client's IP address">>},
{port, integer, <<"Client's port">>},
{is_bridge, boolean, <<"Indicates whether the client is connectedvia bridge">>}, {is_bridge, boolean, <<"Indicates whether the client is connectedvia bridge">>},
{keepalive, integer, <<"keepalive time, with the unit of second">>}, {keepalive, integer, <<"keepalive time, with the unit of second">>},
{mailbox_len, integer, <<"Process mailbox size">>}, {mailbox_len, integer, <<"Process mailbox size">>},
@ -189,7 +190,7 @@ clients_api() ->
name => ip_address, name => ip_address,
in => query, in => query,
required => false, required => false,
description => <<"IP address">>, description => <<"Client's IP address">>,
schema => #{type => string} schema => #{type => string}
}, },
#{ #{
@ -602,7 +603,7 @@ ms(zone, X) ->
ms(conn_state, X) -> ms(conn_state, X) ->
#{conn_state => X}; #{conn_state => X};
ms(ip_address, X) -> ms(ip_address, X) ->
#{conninfo => #{peername => X}}; #{conninfo => #{peername => {X, '_'}}};
ms(clean_start, X) -> ms(clean_start, X) ->
#{conninfo => #{clean_start => X}}; #{conninfo => #{clean_start => X}};
ms(proto_name, X) -> ms(proto_name, X) ->
@ -643,12 +644,13 @@ format_channel_info({_, ClientInfo, ClientStats}) ->
StatsMap = maps:without([memory, next_pkt_id, total_heap_size], StatsMap = maps:without([memory, next_pkt_id, total_heap_size],
maps:from_list(ClientStats)), maps:from_list(ClientStats)),
ClientInfoMap0 = maps:fold(fun take_maps_from_inner/3, #{}, ClientInfo), ClientInfoMap0 = maps:fold(fun take_maps_from_inner/3, #{}, ClientInfo),
IpAddress = peer_to_binary(maps:get(peername, ClientInfoMap0)), {IpAddress, Port} = peername_dispart(maps:get(peername, ClientInfoMap0)),
Connected = maps:get(conn_state, ClientInfoMap0) =:= connected, Connected = maps:get(conn_state, ClientInfoMap0) =:= connected,
ClientInfoMap1 = maps:merge(StatsMap, ClientInfoMap0), ClientInfoMap1 = maps:merge(StatsMap, ClientInfoMap0),
ClientInfoMap2 = maps:put(node, node(), ClientInfoMap1), ClientInfoMap2 = maps:put(node, node(), ClientInfoMap1),
ClientInfoMap3 = maps:put(ip_address, IpAddress, ClientInfoMap2), ClientInfoMap3 = maps:put(ip_address, IpAddress, ClientInfoMap2),
ClientInfoMap = maps:put(connected, Connected, ClientInfoMap3), ClientInfoMap4 = maps:put(port, Port, ClientInfoMap3),
ClientInfoMap = maps:put(connected, Connected, ClientInfoMap4),
RemoveList = RemoveList =
[ auth_result [ auth_result
, peername , peername
@ -692,12 +694,11 @@ result_format_time_fun(Key, NClientInfoMap) ->
#{} -> NClientInfoMap #{} -> NClientInfoMap
end. end.
peer_to_binary({Addr, Port}) -> -spec(peername_dispart(emqx_types:peername()) -> {binary(), inet:port_number()}).
peername_dispart({Addr, Port}) ->
AddrBinary = list_to_binary(inet:ntoa(Addr)), AddrBinary = list_to_binary(inet:ntoa(Addr)),
PortBinary = integer_to_binary(Port), %% PortBinary = integer_to_binary(Port),
<<AddrBinary/binary, ":", PortBinary/binary>>; {<<AddrBinary/binary>>, Port}.
peer_to_binary(Addr) ->
list_to_binary(inet:ntoa(Addr)).
format_authz_cache({{PubSub, Topic}, {AuthzResult, Timestamp}}) -> format_authz_cache({{PubSub, Topic}, {AuthzResult, Timestamp}}) ->
#{ access => PubSub, #{ access => PubSub,