From 81e57f21481704aa6dfd6e734d81492db9166cbc Mon Sep 17 00:00:00 2001 From: firest Date: Sat, 7 May 2022 16:39:59 +0800 Subject: [PATCH] fix(gateway): make it safer to get the value of bind --- .../src/emqx_gateway_api_listeners.erl | 123 +++++++++--------- apps/emqx_gateway/src/emqx_gateway_conf.erl | 11 ++ 2 files changed, 74 insertions(+), 60 deletions(-) diff --git a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl index 343351311..4064af05b 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl @@ -83,38 +83,7 @@ paths() -> listeners(get, #{bindings := #{name := Name0}}) -> with_gateway(Name0, fun(GwName, _) -> - Listeners = emqx_gateway_conf:listeners(GwName), - ListenOns = lists:map( - fun(#{id := Id, <<"bind">> := BinListenOn}) -> - {Id, erlang:binary_to_integer(BinListenOn)} - end, - Listeners - ), - - ClusterStatus = listeners_cluster_status(ListenOns), - - Result = lists:map( - fun(#{id := Id} = Listener) -> - NodeStatus = lists:foldl( - fun(Info, Acc) -> - Status = maps:get(Id, Info), - [Status | Acc] - end, - [], - ClusterStatus - ), - - {MaxCons, CurrCons} = emqx_gateway_http:sum_cluster_connections(NodeStatus), - - Listener#{ - max_connections => MaxCons, - current_connections => CurrCons, - node_status => NodeStatus - } - end, - Listeners - ), - + Result = get_cluster_listeners_info(GwName), {200, Result} end); listeners(post, #{bindings := #{name := Name0}, body := LConf}) -> @@ -300,6 +269,68 @@ import_users(post, #{ page_params(Qs) -> maps:with([<<"page">>, <<"limit">>], Qs). +get_cluster_listeners_info(GwName) -> + Listeners = emqx_gateway_conf:listeners(GwName), + ListenOns = lists:map( + fun(#{id := Id} = Conf) -> + ListenOn = emqx_gateway_conf:get_bind(Conf), + {Id, ListenOn} + end, + Listeners + ), + + ClusterStatus = listeners_cluster_status(ListenOns), + + lists:map( + fun(#{id := Id} = Listener) -> + NodeStatus = lists:foldl( + fun(Info, Acc) -> + Status = maps:get(Id, Info), + [Status | Acc] + end, + [], + ClusterStatus + ), + + {MaxCons, CurrCons} = emqx_gateway_http:sum_cluster_connections(NodeStatus), + + Listener#{ + max_connections => MaxCons, + current_connections => CurrCons, + node_status => NodeStatus + } + end, + Listeners + ). + +listeners_cluster_status(Listeners) -> + Nodes = mria_mnesia:running_nodes(), + case emqx_gateway_api_listeners_proto_v1:listeners_cluster_status(Nodes, Listeners) of + {Results, []} -> + Results; + {_, _BadNodes} -> + error(badrpc) + end. + +do_listeners_cluster_status(Listeners) -> + Node = node(), + lists:foldl( + fun({Id, ListenOn}, Acc) -> + BinId = erlang:atom_to_binary(Id), + {ok, #{<<"max_connections">> := Max}} = emqx_gateway_conf:listener(BinId), + Curr = esockd:get_current_connections({Id, ListenOn}), + Acc#{ + Id => #{ + node => Node, + current_connections => Curr, + max_connections => Max + } + } + end, + #{}, + Listeners + ). + %%-------------------------------------------------------------------- %% Swagger defines %%-------------------------------------------------------------------- @@ -790,31 +821,3 @@ examples_listener() -> } } }. - -listeners_cluster_status(Listeners) -> - Nodes = mria_mnesia:running_nodes(), - case emqx_gateway_api_listeners_proto_v1:listeners_cluster_status(Nodes, Listeners) of - {Results, []} -> - Results; - {_, _BadNodes} -> - error(badrpc) - end. - -do_listeners_cluster_status(Listeners) -> - Node = node(), - lists:foldl( - fun({Id, ListenOn}, Acc) -> - BinId = erlang:atom_to_binary(Id), - {ok, #{<<"max_connections">> := Max}} = emqx_gateway_conf:listener(BinId), - Curr = esockd:get_current_connections({Id, ListenOn}), - Acc#{ - Id => #{ - node => Node, - current_connections => Curr, - max_connections => Max - } - } - end, - #{}, - Listeners - ). diff --git a/apps/emqx_gateway/src/emqx_gateway_conf.erl b/apps/emqx_gateway/src/emqx_gateway_conf.erl index a61754d12..2aba5bff0 100644 --- a/apps/emqx_gateway/src/emqx_gateway_conf.erl +++ b/apps/emqx_gateway/src/emqx_gateway_conf.erl @@ -50,6 +50,8 @@ remove_authn/2 ]). +-export([get_bind/1]). + %% internal exports -export([ unconvert_listeners/1, @@ -196,6 +198,15 @@ bind2str(LConf = #{bind := Bind}) when is_binary(Bind) -> bind2str(LConf = #{<<"bind">> := Bind}) when is_binary(Bind) -> LConf. +get_bind(#{bind := Bind}) when is_integer(Bind) -> + Bind; +get_bind(#{<<"bind">> := Bind}) when is_integer(Bind) -> + Bind; +get_bind(#{bind := Bind}) when is_binary(Bind) -> + erlang:binary_to_integer(Bind); +get_bind(#{<<"bind">> := Bind}) when is_binary(Bind) -> + erlang:binary_to_integer(Bind). + -spec listeners(atom_or_bin()) -> [map()]. listeners(GwName0) -> GwName = bin(GwName0),