chore: ensure the `bind` output style
e.g: - Configured as `1883`, printed as `:1883` - Configured as `0.0.0.0:1883`, printed as `:1883` - Configured as `127.0.0.1:1883`, printed as `127.0.0.1:1883` - Configured as `::1:1883`, printed as `[::1]:1883` - Configured as `[::1]:1883`, printed as `[::1]:1883`
This commit is contained in:
parent
889e25d64e
commit
257e310931
|
@ -54,7 +54,7 @@
|
|||
|
||||
-export([pre_config_update/3, post_config_update/5]).
|
||||
|
||||
-export([format_addr/1]).
|
||||
-export([format_bind/1]).
|
||||
|
||||
-define(CONF_KEY_PATH, [listeners, '?', '?']).
|
||||
-define(TYPES_STRING, ["tcp", "ssl", "ws", "wss", "quic"]).
|
||||
|
@ -201,14 +201,14 @@ start_listener(Type, ListenerName, #{bind := Bind} = Conf) ->
|
|||
?tp(listener_started, #{type => Type, bind => Bind}),
|
||||
console_print(
|
||||
"Listener ~ts on ~ts started.~n",
|
||||
[listener_id(Type, ListenerName), format_addr(Bind)]
|
||||
[listener_id(Type, ListenerName), format_bind(Bind)]
|
||||
),
|
||||
ok;
|
||||
{error, {already_started, Pid}} ->
|
||||
{error, {already_started, Pid}};
|
||||
{error, Reason} ->
|
||||
ListenerId = listener_id(Type, ListenerName),
|
||||
BindStr = format_addr(Bind),
|
||||
BindStr = format_bind(Bind),
|
||||
?ELOG(
|
||||
"Failed to start listener ~ts on ~ts: ~0p.~n",
|
||||
[ListenerId, BindStr, Reason]
|
||||
|
@ -261,19 +261,19 @@ stop_listener(Type, ListenerName, #{bind := Bind} = Conf) ->
|
|||
ok ->
|
||||
console_print(
|
||||
"Listener ~ts on ~ts stopped.~n",
|
||||
[listener_id(Type, ListenerName), format_addr(Bind)]
|
||||
[listener_id(Type, ListenerName), format_bind(Bind)]
|
||||
),
|
||||
ok;
|
||||
{error, not_found} ->
|
||||
?ELOG(
|
||||
"Failed to stop listener ~ts on ~ts: ~0p~n",
|
||||
[listener_id(Type, ListenerName), format_addr(Bind), already_stopped]
|
||||
[listener_id(Type, ListenerName), format_bind(Bind), already_stopped]
|
||||
),
|
||||
ok;
|
||||
{error, Reason} ->
|
||||
?ELOG(
|
||||
"Failed to stop listener ~ts on ~ts: ~0p~n",
|
||||
[listener_id(Type, ListenerName), format_addr(Bind), Reason]
|
||||
[listener_id(Type, ListenerName), format_bind(Bind), Reason]
|
||||
),
|
||||
{error, Reason}
|
||||
end.
|
||||
|
@ -492,17 +492,32 @@ merge_default(Options) ->
|
|||
[{tcp_options, ?MQTT_SOCKOPTS} | Options]
|
||||
end.
|
||||
|
||||
format_addr(Port) when is_integer(Port) ->
|
||||
io_lib:format("~w", [Port]);
|
||||
-spec format_bind(
|
||||
integer() | {tuple(), integer()} | string() | binary()
|
||||
) -> io_lib:chars().
|
||||
format_bind(Port) when is_integer(Port) ->
|
||||
io_lib:format(":~w", [Port]);
|
||||
%% Print only the port number when bound on all interfaces
|
||||
format_addr({{0, 0, 0, 0}, Port}) ->
|
||||
format_addr(Port);
|
||||
format_addr({{0, 0, 0, 0, 0, 0, 0, 0}, Port}) ->
|
||||
format_addr(Port);
|
||||
format_addr({Addr, Port}) when is_list(Addr) ->
|
||||
format_bind({{0, 0, 0, 0}, Port}) ->
|
||||
format_bind(Port);
|
||||
format_bind({{0, 0, 0, 0, 0, 0, 0, 0}, Port}) ->
|
||||
format_bind(Port);
|
||||
format_bind({Addr, Port}) when is_list(Addr) ->
|
||||
io_lib:format("~ts:~w", [Addr, Port]);
|
||||
format_addr({Addr, Port}) when is_tuple(Addr) ->
|
||||
io_lib:format("~ts:~w", [inet:ntoa(Addr), Port]).
|
||||
format_bind({Addr, Port}) when is_tuple(Addr), tuple_size(Addr) == 4 ->
|
||||
io_lib:format("~ts:~w", [inet:ntoa(Addr), Port]);
|
||||
format_bind({Addr, Port}) when is_tuple(Addr), tuple_size(Addr) == 8 ->
|
||||
io_lib:format("[~ts]:~w", [inet:ntoa(Addr), Port]);
|
||||
%% Support string, binary type for Port or IP:Port
|
||||
format_bind(Str) when is_list(Str) ->
|
||||
case emqx_schema:to_ip_port(Str) of
|
||||
{ok, {Ip, Port}} ->
|
||||
format_bind({Ip, Port});
|
||||
{error, _} ->
|
||||
format_bind(list_to_integer(Str))
|
||||
end;
|
||||
format_bind(Bin) when is_binary(Bin) ->
|
||||
format_bind(binary_to_list(Bin)).
|
||||
|
||||
listener_id(Type, ListenerName) ->
|
||||
list_to_atom(lists:append([str(Type), ":", str(ListenerName)])).
|
||||
|
|
|
@ -92,7 +92,7 @@ start_listeners(Listeners) ->
|
|||
case minirest:start(Name, RanchOptions, Minirest) of
|
||||
{ok, _} ->
|
||||
?ULOG("Listener ~ts on ~ts started.~n", [
|
||||
Name, emqx_listeners:format_addr(Bind)
|
||||
Name, emqx_listeners:format_bind(Bind)
|
||||
]),
|
||||
Acc;
|
||||
{error, _Reason} ->
|
||||
|
@ -114,7 +114,7 @@ stop_listeners(Listeners) ->
|
|||
case minirest:stop(Name) of
|
||||
ok ->
|
||||
?ULOG("Stop listener ~ts on ~ts successfully.~n", [
|
||||
Name, emqx_listeners:format_addr(Port)
|
||||
Name, emqx_listeners:format_bind(Port)
|
||||
]);
|
||||
{error, not_found} ->
|
||||
?SLOG(warning, #{msg => "stop_listener_failed", name => Name, port => Port})
|
||||
|
|
|
@ -345,9 +345,9 @@ aggregate_running(R, undefined) -> R;
|
|||
aggregate_running(_, _) -> inconsistent.
|
||||
|
||||
bind2str(Listener = #{bind := Bind}) ->
|
||||
Listener#{bind := iolist_to_binary(emqx_gateway_utils:format_listenon(Bind))};
|
||||
Listener#{bind := iolist_to_binary(emqx_listeners:format_bind(Bind))};
|
||||
bind2str(Listener = #{<<"bind">> := Bind}) ->
|
||||
Listener#{<<"bind">> := iolist_to_binary(emqx_gateway_utils:format_listenon(Bind))}.
|
||||
Listener#{<<"bind">> := iolist_to_binary(emqx_listeners:format_bind(Bind))}.
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Swagger defines
|
||||
|
|
|
@ -37,7 +37,6 @@
|
|||
|
||||
-export([
|
||||
apply/2,
|
||||
format_listenon/1,
|
||||
parse_listenon/1,
|
||||
unix_ts_to_rfc3339/1,
|
||||
unix_ts_to_rfc3339/2,
|
||||
|
@ -165,7 +164,7 @@ start_listener(
|
|||
{Type, LisName, ListenOn, SocketOpts, Cfg},
|
||||
ModCfg
|
||||
) ->
|
||||
ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
|
||||
ListenOnStr = emqx_listeners:format_bind(ListenOn),
|
||||
ListenerId = emqx_gateway_utils:listener_id(GwName, Type, LisName),
|
||||
|
||||
NCfg = maps:merge(Cfg, ModCfg),
|
||||
|
@ -243,7 +242,7 @@ stop_listeners(GwName, Listeners) ->
|
|||
-spec stop_listener(GwName :: atom(), Listener :: tuple()) -> ok.
|
||||
stop_listener(GwName, {Type, LisName, ListenOn, SocketOpts, Cfg}) ->
|
||||
StopRet = stop_listener(GwName, Type, LisName, ListenOn, SocketOpts, Cfg),
|
||||
ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
|
||||
ListenOnStr = emqx_listeners:format_bind(ListenOn),
|
||||
case StopRet of
|
||||
ok ->
|
||||
console_print(
|
||||
|
@ -287,9 +286,6 @@ apply(F, A2) when
|
|||
->
|
||||
erlang:apply(F, A2).
|
||||
|
||||
format_listenon(Term) ->
|
||||
emqx_mgmt_util:format_listen_on(Term).
|
||||
|
||||
parse_listenon(Port) when is_integer(Port) ->
|
||||
Port;
|
||||
parse_listenon(IpPort) when is_tuple(IpPort) ->
|
||||
|
|
|
@ -167,7 +167,7 @@ start_grpc_server(GwName, Options = #{bind := ListenOn}) ->
|
|||
)}
|
||||
]
|
||||
end,
|
||||
ListenOnStr = emqx_listeners:format_addr(ListenOn),
|
||||
ListenOnStr = emqx_listeners:format_bind(ListenOn),
|
||||
case grpc:start_server(GwName, ListenOn, Services, SvrOptions) of
|
||||
{ok, _SvrPid} ->
|
||||
console_print(
|
||||
|
|
|
@ -543,7 +543,7 @@ format_status(Key, Node, Listener, Acc) ->
|
|||
enable => Enabled,
|
||||
ids => [Id],
|
||||
acceptors => Acceptors,
|
||||
bind => format_raw_bind(Bind),
|
||||
bind => iolist_to_binary(emqx_listeners:format_bind(Bind)),
|
||||
status => #{
|
||||
running => Running,
|
||||
max_connections => MaxConnections,
|
||||
|
@ -605,12 +605,6 @@ max_conn(_Int1, <<"infinity">>) -> <<"infinity">>;
|
|||
max_conn(<<"infinity">>, _Int) -> <<"infinity">>;
|
||||
max_conn(Int1, Int2) -> Int1 + Int2.
|
||||
|
||||
%% @doc returning a uniform format (ip_port string) is more
|
||||
%% helpful to users
|
||||
format_raw_bind(Bind) when is_integer(Bind) ->
|
||||
<<"0.0.0.0:", (integer_to_binary(Bind))/binary>>;
|
||||
format_raw_bind(Bind) when is_binary(Bind) -> Bind.
|
||||
|
||||
update(Path, Conf) ->
|
||||
wrap(emqx_conf:update(Path, {update, Conf}, ?OPTS(cluster))).
|
||||
|
||||
|
|
|
@ -582,7 +582,7 @@ listeners([]) ->
|
|||
end,
|
||||
Info =
|
||||
[
|
||||
{listen_on, {string, emqx_mgmt_util:format_listen_on(Bind)}},
|
||||
{listen_on, {string, emqx_listeners:format_bind(Bind)}},
|
||||
{acceptors, Acceptors},
|
||||
{proxy_protocol, ProxyProtocol},
|
||||
{running, Running}
|
||||
|
|
|
@ -43,10 +43,7 @@
|
|||
batch_schema/1
|
||||
]).
|
||||
|
||||
-export([
|
||||
urldecode/1,
|
||||
format_listen_on/1
|
||||
]).
|
||||
-export([urldecode/1]).
|
||||
|
||||
-define(KB, 1024).
|
||||
-define(MB, (1024 * 1024)).
|
||||
|
@ -89,25 +86,6 @@ merge_maps(Default, New) ->
|
|||
urldecode(S) ->
|
||||
emqx_http_lib:uri_decode(S).
|
||||
|
||||
-spec format_listen_on(
|
||||
integer() | {tuple(), integer()} | string() | binary()
|
||||
) -> io_lib:chars().
|
||||
format_listen_on(Port) when is_integer(Port) ->
|
||||
io_lib:format("0.0.0.0:~w", [Port]);
|
||||
format_listen_on({Addr, Port}) when is_list(Addr) ->
|
||||
io_lib:format("~ts:~w", [Addr, Port]);
|
||||
format_listen_on({Addr, Port}) when is_tuple(Addr) ->
|
||||
io_lib:format("~ts:~w", [inet:ntoa(Addr), Port]);
|
||||
format_listen_on(Str) when is_list(Str) ->
|
||||
case emqx_schema:to_ip_port(Str) of
|
||||
{ok, {Ip, Port}} ->
|
||||
format_listen_on({Ip, Port});
|
||||
{error, _} ->
|
||||
format_listen_on(list_to_integer(Str))
|
||||
end;
|
||||
format_listen_on(Bin) when is_binary(Bin) ->
|
||||
format_listen_on(binary_to_list(Bin)).
|
||||
|
||||
%%%==============================================================================================
|
||||
%% schema util
|
||||
schema(Ref) when is_atom(Ref) ->
|
||||
|
|
Loading…
Reference in New Issue