Merge pull request #7442 from HJianBo/fmt-gw
chore(gw): format gateway application
This commit is contained in:
commit
4a359124f5
|
|
@ -16,8 +16,12 @@
|
||||||
-module(emqx_bpapi).
|
-module(emqx_bpapi).
|
||||||
|
|
||||||
%% API:
|
%% API:
|
||||||
-export([start/0, announce/1, supported_version/1, supported_version/2,
|
-export([
|
||||||
versions_file/1]).
|
start/0,
|
||||||
|
announce/1,
|
||||||
|
supported_version/1, supported_version/2,
|
||||||
|
versions_file/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([api/0, api_version/0, var_name/0, call/0, rpc/0, bpapi_meta/0]).
|
-export_type([api/0, api_version/0, var_name/0, call/0, rpc/0, bpapi_meta/0]).
|
||||||
|
|
||||||
|
|
@ -31,11 +35,12 @@
|
||||||
-type rpc() :: {_From :: call(), _To :: call()}.
|
-type rpc() :: {_From :: call(), _To :: call()}.
|
||||||
|
|
||||||
-type bpapi_meta() ::
|
-type bpapi_meta() ::
|
||||||
#{ api := api()
|
#{
|
||||||
, version := api_version()
|
api := api(),
|
||||||
, calls := [rpc()]
|
version := api_version(),
|
||||||
, casts := [rpc()]
|
calls := [rpc()],
|
||||||
}.
|
casts := [rpc()]
|
||||||
|
}.
|
||||||
|
|
||||||
-include("emqx_bpapi.hrl").
|
-include("emqx_bpapi.hrl").
|
||||||
|
|
||||||
|
|
@ -49,11 +54,12 @@
|
||||||
|
|
||||||
-spec start() -> ok.
|
-spec start() -> ok.
|
||||||
start() ->
|
start() ->
|
||||||
ok = mria:create_table(?TAB, [ {type, set}
|
ok = mria:create_table(?TAB, [
|
||||||
, {storage, ram_copies}
|
{type, set},
|
||||||
, {attributes, record_info(fields, ?TAB)}
|
{storage, ram_copies},
|
||||||
, {rlog_shard, ?COMMON_SHARD}
|
{attributes, record_info(fields, ?TAB)},
|
||||||
]),
|
{rlog_shard, ?COMMON_SHARD}
|
||||||
|
]),
|
||||||
ok = mria:wait_for_tables([?TAB]),
|
ok = mria:wait_for_tables([?TAB]),
|
||||||
announce(emqx).
|
announce(emqx).
|
||||||
|
|
||||||
|
|
@ -86,25 +92,34 @@ versions_file(App) ->
|
||||||
announce_fun(Data) ->
|
announce_fun(Data) ->
|
||||||
%% Delete old records, if present:
|
%% Delete old records, if present:
|
||||||
MS = ets:fun2ms(fun(#?TAB{key = {node(), API}}) ->
|
MS = ets:fun2ms(fun(#?TAB{key = {node(), API}}) ->
|
||||||
{node(), API}
|
{node(), API}
|
||||||
end),
|
end),
|
||||||
OldKeys = mnesia:select(?TAB, MS, write),
|
OldKeys = mnesia:select(?TAB, MS, write),
|
||||||
_ = [mnesia:delete({?TAB, Key})
|
_ = [
|
||||||
|| Key <- OldKeys],
|
mnesia:delete({?TAB, Key})
|
||||||
|
|| Key <- OldKeys
|
||||||
|
],
|
||||||
%% Insert new records:
|
%% Insert new records:
|
||||||
_ = [mnesia:write(#?TAB{key = {node(), API}, version = Version})
|
_ = [
|
||||||
|| {API, Version} <- Data],
|
mnesia:write(#?TAB{key = {node(), API}, version = Version})
|
||||||
|
|| {API, Version} <- Data
|
||||||
|
],
|
||||||
%% Update maximum supported version:
|
%% Update maximum supported version:
|
||||||
[update_minimum(API) || {API, _} <- Data],
|
[update_minimum(API) || {API, _} <- Data],
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-spec update_minimum(api()) -> ok.
|
-spec update_minimum(api()) -> ok.
|
||||||
update_minimum(API) ->
|
update_minimum(API) ->
|
||||||
MS = ets:fun2ms(fun(#?TAB{ key = {N, A}
|
MS = ets:fun2ms(fun(
|
||||||
, version = Value
|
#?TAB{
|
||||||
}) when N =/= ?multicall,
|
key = {N, A},
|
||||||
A =:= API ->
|
version = Value
|
||||||
Value
|
}
|
||||||
end),
|
) when
|
||||||
|
N =/= ?multicall,
|
||||||
|
A =:= API
|
||||||
|
->
|
||||||
|
Value
|
||||||
|
end),
|
||||||
MinVersion = lists:min(mnesia:select(?TAB, MS)),
|
MinVersion = lists:min(mnesia:select(?TAB, MS)),
|
||||||
mnesia:write(#?TAB{key = {?multicall, API}, version = MinVersion}).
|
mnesia:write(#?TAB{key = {?multicall, API}, version = MinVersion}).
|
||||||
|
|
|
||||||
|
|
@ -20,9 +20,9 @@
|
||||||
|
|
||||||
-define(multicall, multicall).
|
-define(multicall, multicall).
|
||||||
|
|
||||||
-record(?TAB,
|
-record(?TAB, {
|
||||||
{ key :: {node() | ?multicall, emqx_bpapi:api()}
|
key :: {node() | ?multicall, emqx_bpapi:api()},
|
||||||
, version :: emqx_bpapi:api_version()
|
version :: emqx_bpapi:api_version()
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
|
||||||
|
|
@ -26,22 +26,24 @@
|
||||||
|
|
||||||
-type semantics() :: call | cast.
|
-type semantics() :: call | cast.
|
||||||
|
|
||||||
-record(s,
|
-record(s, {
|
||||||
{ api :: emqx_bpapi:api()
|
api :: emqx_bpapi:api(),
|
||||||
, module :: module()
|
module :: module(),
|
||||||
, version :: emqx_bpapi:api_version() | undefined
|
version :: emqx_bpapi:api_version() | undefined,
|
||||||
, targets = [] :: [{semantics(), emqx_bpapi:call(), emqx_bpapi:call()}]
|
targets = [] :: [{semantics(), emqx_bpapi:call(), emqx_bpapi:call()}],
|
||||||
, errors = [] :: list()
|
errors = [] :: list(),
|
||||||
, file
|
file
|
||||||
}).
|
}).
|
||||||
|
|
||||||
format_error(invalid_name) ->
|
format_error(invalid_name) ->
|
||||||
"BPAPI module name should follow <API>_proto_v<number> pattern";
|
"BPAPI module name should follow <API>_proto_v<number> pattern";
|
||||||
format_error({invalid_fun, Name, Arity}) ->
|
format_error({invalid_fun, Name, Arity}) ->
|
||||||
io_lib:format("malformed function ~p/~p. "
|
io_lib:format(
|
||||||
"BPAPI functions should have exactly one clause "
|
"malformed function ~p/~p. "
|
||||||
"and call (emqx_|e)rpc at the top level",
|
"BPAPI functions should have exactly one clause "
|
||||||
[Name, Arity]).
|
"and call (emqx_|e)rpc at the top level",
|
||||||
|
[Name, Arity]
|
||||||
|
).
|
||||||
|
|
||||||
parse_transform(Forms, _Options) ->
|
parse_transform(Forms, _Options) ->
|
||||||
log("Original:~n~p", [Forms]),
|
log("Original:~n~p", [Forms]),
|
||||||
|
|
@ -60,11 +62,11 @@ go({attribute, _, file, {File, _}}, S) ->
|
||||||
go({attribute, Line, module, Mod}, S) ->
|
go({attribute, Line, module, Mod}, S) ->
|
||||||
case api_and_version(Mod) of
|
case api_and_version(Mod) of
|
||||||
{ok, API, Vsn} -> S#s{api = API, version = Vsn, module = Mod};
|
{ok, API, Vsn} -> S#s{api = API, version = Vsn, module = Mod};
|
||||||
error -> push_err(Line, invalid_name, S)
|
error -> push_err(Line, invalid_name, S)
|
||||||
end;
|
end;
|
||||||
go({function, _Line, introduced_in, 0, _}, S) ->
|
go({function, _Line, introduced_in, 0, _}, S) ->
|
||||||
S;
|
S;
|
||||||
go({function, _Line, deprecated_since, 0, _}, S) ->
|
go({function, _Line, deprecated_since, 0, _}, S) ->
|
||||||
S;
|
S;
|
||||||
go({function, Line, Name, Arity, Clauses}, S) ->
|
go({function, Line, Name, Arity, Clauses}, S) ->
|
||||||
analyze_fun(Line, Name, Arity, Clauses, S);
|
analyze_fun(Line, Name, Arity, Clauses, S);
|
||||||
|
|
@ -79,26 +81,25 @@ finalize(Forms, S) ->
|
||||||
{Attrs, Funcs} = lists:splitwith(fun is_attribute/1, Forms),
|
{Attrs, Funcs} = lists:splitwith(fun is_attribute/1, Forms),
|
||||||
AST = mk_meta_fun(S),
|
AST = mk_meta_fun(S),
|
||||||
log("Meta fun:~n~p", [AST]),
|
log("Meta fun:~n~p", [AST]),
|
||||||
Attrs ++ [mk_export()] ++ [AST|Funcs].
|
Attrs ++ [mk_export()] ++ [AST | Funcs].
|
||||||
|
|
||||||
mk_meta_fun(#s{api = API, version = Vsn, targets = Targets}) ->
|
mk_meta_fun(#s{api = API, version = Vsn, targets = Targets}) ->
|
||||||
Line = 0,
|
Line = 0,
|
||||||
Calls = [{From, To} || {call, From, To} <- Targets],
|
Calls = [{From, To} || {call, From, To} <- Targets],
|
||||||
Casts = [{From, To} || {cast, From, To} <- Targets],
|
Casts = [{From, To} || {cast, From, To} <- Targets],
|
||||||
Ret = typerefl_quote:const(Line, #{ api => API
|
Ret = typerefl_quote:const(Line, #{
|
||||||
, version => Vsn
|
api => API,
|
||||||
, calls => Calls
|
version => Vsn,
|
||||||
, casts => Casts
|
calls => Calls,
|
||||||
}),
|
casts => Casts
|
||||||
{function, Line, ?META_FUN, _Arity = 0,
|
}),
|
||||||
[{clause, Line, _Args = [], _Guards = [],
|
{function, Line, ?META_FUN, _Arity = 0, [{clause, Line, _Args = [], _Guards = [], [Ret]}]}.
|
||||||
[Ret]}]}.
|
|
||||||
|
|
||||||
mk_export() ->
|
mk_export() ->
|
||||||
{attribute, 0, export, [{?META_FUN, 0}]}.
|
{attribute, 0, export, [{?META_FUN, 0}]}.
|
||||||
|
|
||||||
is_attribute({attribute, _Line, _Attr, _Val}) -> true;
|
is_attribute({attribute, _Line, _Attr, _Val}) -> true;
|
||||||
is_attribute(_) -> false.
|
is_attribute(_) -> false.
|
||||||
|
|
||||||
%% Extract the target function of the RPC call
|
%% Extract the target function of the RPC call
|
||||||
analyze_fun(Line, Name, Arity, [{clause, Line, Head, _Guards, Exprs}], S) ->
|
analyze_fun(Line, Name, Arity, [{clause, Line, Head, _Guards, Exprs}], S) ->
|
||||||
|
|
@ -122,14 +123,17 @@ analyze_exprs(Line, Name, Arity, Head, Exprs, S) ->
|
||||||
|
|
||||||
-spec extract_outer_args([erl_parse:abstract_form()]) -> [atom()].
|
-spec extract_outer_args([erl_parse:abstract_form()]) -> [atom()].
|
||||||
extract_outer_args(Abs) ->
|
extract_outer_args(Abs) ->
|
||||||
lists:map(fun({var, _, Var}) ->
|
lists:map(
|
||||||
Var;
|
fun
|
||||||
({match, _, {var, _, Var}, _}) ->
|
({var, _, Var}) ->
|
||||||
Var;
|
Var;
|
||||||
({match, _, _, {var, _, Var}}) ->
|
({match, _, {var, _, Var}, _}) ->
|
||||||
Var
|
Var;
|
||||||
end,
|
({match, _, _, {var, _, Var}}) ->
|
||||||
Abs).
|
Var
|
||||||
|
end,
|
||||||
|
Abs
|
||||||
|
).
|
||||||
|
|
||||||
-spec extract_target_call(_AST, [_AST]) -> {semantics(), emqx_bpapi:call()}.
|
-spec extract_target_call(_AST, [_AST]) -> {semantics(), emqx_bpapi:call()}.
|
||||||
extract_target_call(RPCBackend, OuterArgs) ->
|
extract_target_call(RPCBackend, OuterArgs) ->
|
||||||
|
|
@ -166,13 +170,13 @@ extract_mfa(?BACKEND(emqx_cluster_rpc, multicall), [M, F, A, _RequiredNum, _Time
|
||||||
extract_mfa(_, _) ->
|
extract_mfa(_, _) ->
|
||||||
error("unrecognized RPC call").
|
error("unrecognized RPC call").
|
||||||
|
|
||||||
call_or_cast(cast) -> cast;
|
call_or_cast(cast) -> cast;
|
||||||
call_or_cast(multicast) -> cast;
|
call_or_cast(multicast) -> cast;
|
||||||
call_or_cast(multicall) -> call;
|
call_or_cast(multicall) -> call;
|
||||||
call_or_cast(call) -> call.
|
call_or_cast(call) -> call.
|
||||||
|
|
||||||
list_to_args({cons, _, {var, _, A}, T}) ->
|
list_to_args({cons, _, {var, _, A}, T}) ->
|
||||||
[A|list_to_args(T)];
|
[A | list_to_args(T)];
|
||||||
list_to_args({nil, _}) ->
|
list_to_args({nil, _}) ->
|
||||||
[].
|
[].
|
||||||
|
|
||||||
|
|
@ -180,10 +184,10 @@ invalid_fun(Line, Name, Arity, S) ->
|
||||||
push_err(Line, {invalid_fun, Name, Arity}, S).
|
push_err(Line, {invalid_fun, Name, Arity}, S).
|
||||||
|
|
||||||
push_err(Line, Err, S = #s{errors = Errs}) ->
|
push_err(Line, Err, S = #s{errors = Errs}) ->
|
||||||
S#s{errors = [{Line, Err}|Errs]}.
|
S#s{errors = [{Line, Err} | Errs]}.
|
||||||
|
|
||||||
push_target(Target, S = #s{targets = Targets}) ->
|
push_target(Target, S = #s{targets = Targets}) ->
|
||||||
S#s{targets = [Target|Targets]}.
|
S#s{targets = [Target | Targets]}.
|
||||||
|
|
||||||
-spec api_and_version(module()) -> {ok, emqx_bpapi:api(), emqx_bpapi:version()} | error.
|
-spec api_and_version(module()) -> {ok, emqx_bpapi:api(), emqx_bpapi:version()} | error.
|
||||||
api_and_version(Module) ->
|
api_and_version(Module) ->
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,16 @@ post_config_update(_ConfPath, _Req, _NewConf, _OldConf, _AppEnvs) ->
|
||||||
|
|
||||||
update_log_handlers(NewHandlers) ->
|
update_log_handlers(NewHandlers) ->
|
||||||
OldHandlers = application:get_env(kernel, logger, []),
|
OldHandlers = application:get_env(kernel, logger, []),
|
||||||
lists:foreach(fun({handler, HandlerId, _Mod, _Conf}) ->
|
lists:foreach(
|
||||||
logger:remove_handler(HandlerId)
|
fun({handler, HandlerId, _Mod, _Conf}) ->
|
||||||
end, OldHandlers -- NewHandlers),
|
logger:remove_handler(HandlerId)
|
||||||
lists:foreach(fun({handler, HandlerId, Mod, Conf}) ->
|
end,
|
||||||
logger:add_handler(HandlerId, Mod, Conf)
|
OldHandlers -- NewHandlers
|
||||||
end, NewHandlers -- OldHandlers),
|
),
|
||||||
|
lists:foreach(
|
||||||
|
fun({handler, HandlerId, Mod, Conf}) ->
|
||||||
|
logger:add_handler(HandlerId, Mod, Conf)
|
||||||
|
end,
|
||||||
|
NewHandlers -- OldHandlers
|
||||||
|
),
|
||||||
application:set_env(kernel, logger, NewHandlers).
|
application:set_env(kernel, logger, NewHandlers).
|
||||||
|
|
|
||||||
|
|
@ -21,17 +21,20 @@
|
||||||
%% API
|
%% API
|
||||||
-export([new_create_options/2, create/1, delete/1, consume/2]).
|
-export([new_create_options/2, create/1, delete/1, consume/2]).
|
||||||
|
|
||||||
-type create_options() :: #{ module := ?MODULE
|
-type create_options() :: #{
|
||||||
, type := emqx_limiter_schema:limiter_type()
|
module := ?MODULE,
|
||||||
, bucket := emqx_limiter_schema:bucket_name()
|
type := emqx_limiter_schema:limiter_type(),
|
||||||
}.
|
bucket := emqx_limiter_schema:bucket_name()
|
||||||
|
}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% API
|
%% API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec new_create_options(emqx_limiter_schema:limiter_type(),
|
-spec new_create_options(
|
||||||
emqx_limiter_schema:bucket_name()) -> create_options().
|
emqx_limiter_schema:limiter_type(),
|
||||||
|
emqx_limiter_schema:bucket_name()
|
||||||
|
) -> create_options().
|
||||||
new_create_options(Type, BucketName) ->
|
new_create_options(Type, BucketName) ->
|
||||||
#{module => ?MODULE, type => Type, bucket => BucketName}.
|
#{module => ?MODULE, type => Type, bucket => BucketName}.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,52 +21,71 @@
|
||||||
%% @end
|
%% @end
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ make_token_bucket_limiter/2, make_ref_limiter/2, check/2
|
-export([
|
||||||
, consume/2, set_retry/2, retry/1, make_infinity_limiter/0
|
make_token_bucket_limiter/2,
|
||||||
, make_future/1, available/1
|
make_ref_limiter/2,
|
||||||
]).
|
check/2,
|
||||||
|
consume/2,
|
||||||
|
set_retry/2,
|
||||||
|
retry/1,
|
||||||
|
make_infinity_limiter/0,
|
||||||
|
make_future/1,
|
||||||
|
available/1
|
||||||
|
]).
|
||||||
-export_type([token_bucket_limiter/0]).
|
-export_type([token_bucket_limiter/0]).
|
||||||
|
|
||||||
%% a token bucket limiter with a limiter server's bucket reference
|
%% a token bucket limiter with a limiter server's bucket reference
|
||||||
-type token_bucket_limiter() :: #{ %% the number of tokens currently available
|
|
||||||
tokens := non_neg_integer()
|
|
||||||
, rate := decimal()
|
|
||||||
, capacity := decimal()
|
|
||||||
, lasttime := millisecond()
|
|
||||||
%% @see emqx_limiter_schema
|
|
||||||
, max_retry_time := non_neg_integer()
|
|
||||||
%% @see emqx_limiter_schema
|
|
||||||
, failure_strategy := failure_strategy()
|
|
||||||
, divisible := boolean() %% @see emqx_limiter_schema
|
|
||||||
, low_water_mark := non_neg_integer() %% @see emqx_limiter_schema
|
|
||||||
, bucket := bucket() %% the limiter server's bucket
|
|
||||||
|
|
||||||
%% retry contenxt
|
%% the number of tokens currently available
|
||||||
%% undefined meaning no retry context or no need to retry
|
-type token_bucket_limiter() :: #{
|
||||||
, retry_ctx => undefined
|
tokens := non_neg_integer(),
|
||||||
| retry_context(token_bucket_limiter()) %% the retry context
|
rate := decimal(),
|
||||||
, atom => any() %% allow to add other keys
|
capacity := decimal(),
|
||||||
}.
|
lasttime := millisecond(),
|
||||||
|
%% @see emqx_limiter_schema
|
||||||
|
max_retry_time := non_neg_integer(),
|
||||||
|
%% @see emqx_limiter_schema
|
||||||
|
failure_strategy := failure_strategy(),
|
||||||
|
%% @see emqx_limiter_schema
|
||||||
|
divisible := boolean(),
|
||||||
|
%% @see emqx_limiter_schema
|
||||||
|
low_water_mark := non_neg_integer(),
|
||||||
|
%% the limiter server's bucket
|
||||||
|
bucket := bucket(),
|
||||||
|
|
||||||
|
%% retry contenxt
|
||||||
|
%% undefined meaning no retry context or no need to retry
|
||||||
|
retry_ctx =>
|
||||||
|
undefined
|
||||||
|
%% the retry context
|
||||||
|
| retry_context(token_bucket_limiter()),
|
||||||
|
%% allow to add other keys
|
||||||
|
atom => any()
|
||||||
|
}.
|
||||||
|
|
||||||
%% a limiter server's bucket reference
|
%% a limiter server's bucket reference
|
||||||
-type ref_limiter() :: #{ max_retry_time := non_neg_integer()
|
-type ref_limiter() :: #{
|
||||||
, failure_strategy := failure_strategy()
|
max_retry_time := non_neg_integer(),
|
||||||
, divisible := boolean()
|
failure_strategy := failure_strategy(),
|
||||||
, low_water_mark := non_neg_integer()
|
divisible := boolean(),
|
||||||
, bucket := bucket()
|
low_water_mark := non_neg_integer(),
|
||||||
|
bucket := bucket(),
|
||||||
|
|
||||||
, retry_ctx => undefined | retry_context(ref_limiter())
|
retry_ctx => undefined | retry_context(ref_limiter()),
|
||||||
, atom => any() %% allow to add other keys
|
%% allow to add other keys
|
||||||
}.
|
atom => any()
|
||||||
|
}.
|
||||||
|
|
||||||
-type retry_fun(Limiter) :: fun((pos_integer(), Limiter) -> inner_check_result(Limiter)).
|
-type retry_fun(Limiter) :: fun((pos_integer(), Limiter) -> inner_check_result(Limiter)).
|
||||||
-type acquire_type(Limiter) :: integer() | retry_context(Limiter).
|
-type acquire_type(Limiter) :: integer() | retry_context(Limiter).
|
||||||
-type retry_context(Limiter) :: #{ continuation := undefined | retry_fun(Limiter)
|
-type retry_context(Limiter) :: #{
|
||||||
, diff := non_neg_integer() %% how many tokens are left to obtain
|
continuation := undefined | retry_fun(Limiter),
|
||||||
|
%% how many tokens are left to obtain
|
||||||
|
diff := non_neg_integer(),
|
||||||
|
|
||||||
, need => pos_integer()
|
need => pos_integer(),
|
||||||
, start => millisecond()
|
start => millisecond()
|
||||||
}.
|
}.
|
||||||
|
|
||||||
-type bucket() :: emqx_limiter_bucket_ref:bucket_ref().
|
-type bucket() :: emqx_limiter_bucket_ref:bucket_ref().
|
||||||
-type limiter() :: token_bucket_limiter() | ref_limiter() | infinity.
|
-type limiter() :: token_bucket_limiter() | ref_limiter() | infinity.
|
||||||
|
|
@ -77,27 +96,31 @@
|
||||||
-type check_result_pause(Limiter) :: {pause_type(), millisecond(), retry_context(Limiter), Limiter}.
|
-type check_result_pause(Limiter) :: {pause_type(), millisecond(), retry_context(Limiter), Limiter}.
|
||||||
-type result_drop(Limiter) :: {drop, Limiter}.
|
-type result_drop(Limiter) :: {drop, Limiter}.
|
||||||
|
|
||||||
-type check_result(Limiter) :: check_result_ok(Limiter)
|
-type check_result(Limiter) ::
|
||||||
| check_result_pause(Limiter)
|
check_result_ok(Limiter)
|
||||||
| result_drop(Limiter).
|
| check_result_pause(Limiter)
|
||||||
|
| result_drop(Limiter).
|
||||||
|
|
||||||
-type inner_check_result(Limiter) :: check_result_ok(Limiter)
|
-type inner_check_result(Limiter) ::
|
||||||
| check_result_pause(Limiter).
|
check_result_ok(Limiter)
|
||||||
|
| check_result_pause(Limiter).
|
||||||
|
|
||||||
-type consume_result(Limiter) :: check_result_ok(Limiter)
|
-type consume_result(Limiter) ::
|
||||||
| result_drop(Limiter).
|
check_result_ok(Limiter)
|
||||||
|
| result_drop(Limiter).
|
||||||
|
|
||||||
-type decimal() :: emqx_limiter_decimal:decimal().
|
-type decimal() :: emqx_limiter_decimal:decimal().
|
||||||
-type failure_strategy() :: emqx_limiter_schema:failure_strategy().
|
-type failure_strategy() :: emqx_limiter_schema:failure_strategy().
|
||||||
|
|
||||||
-type limiter_bucket_cfg() :: #{ rate := decimal()
|
-type limiter_bucket_cfg() :: #{
|
||||||
, initial := non_neg_integer()
|
rate := decimal(),
|
||||||
, low_water_mark := non_neg_integer()
|
initial := non_neg_integer(),
|
||||||
, capacity := decimal()
|
low_water_mark := non_neg_integer(),
|
||||||
, divisible := boolean()
|
capacity := decimal(),
|
||||||
, max_retry_time := non_neg_integer()
|
divisible := boolean(),
|
||||||
, failure_strategy := failure_strategy()
|
max_retry_time := non_neg_integer(),
|
||||||
}.
|
failure_strategy := failure_strategy()
|
||||||
|
}.
|
||||||
|
|
||||||
-type future() :: pos_integer().
|
-type future() :: pos_integer().
|
||||||
|
|
||||||
|
|
@ -113,10 +136,11 @@
|
||||||
%%@doc create a limiter
|
%%@doc create a limiter
|
||||||
-spec make_token_bucket_limiter(limiter_bucket_cfg(), bucket()) -> _.
|
-spec make_token_bucket_limiter(limiter_bucket_cfg(), bucket()) -> _.
|
||||||
make_token_bucket_limiter(Cfg, Bucket) ->
|
make_token_bucket_limiter(Cfg, Bucket) ->
|
||||||
Cfg#{ tokens => emqx_limiter_server:get_initial_val(Cfg)
|
Cfg#{
|
||||||
, lasttime => ?NOW
|
tokens => emqx_limiter_server:get_initial_val(Cfg),
|
||||||
, bucket => Bucket
|
lasttime => ?NOW,
|
||||||
}.
|
bucket => Bucket
|
||||||
|
}.
|
||||||
|
|
||||||
%%@doc create a limiter server's reference
|
%%@doc create a limiter server's reference
|
||||||
-spec make_ref_limiter(limiter_bucket_cfg(), bucket()) -> ref_limiter().
|
-spec make_ref_limiter(limiter_bucket_cfg(), bucket()) -> ref_limiter().
|
||||||
|
|
@ -130,38 +154,39 @@ make_infinity_limiter() ->
|
||||||
%% @doc request some tokens
|
%% @doc request some tokens
|
||||||
%% it will automatically retry when failed until the maximum retry time is reached
|
%% it will automatically retry when failed until the maximum retry time is reached
|
||||||
%% @end
|
%% @end
|
||||||
-spec consume(integer(), Limiter) -> consume_result(Limiter)
|
-spec consume(integer(), Limiter) -> consume_result(Limiter) when
|
||||||
when Limiter :: limiter().
|
Limiter :: limiter().
|
||||||
consume(Need, #{max_retry_time := RetryTime} = Limiter) when Need > 0 ->
|
consume(Need, #{max_retry_time := RetryTime} = Limiter) when Need > 0 ->
|
||||||
try_consume(RetryTime, Need, Limiter);
|
try_consume(RetryTime, Need, Limiter);
|
||||||
|
|
||||||
consume(_, Limiter) ->
|
consume(_, Limiter) ->
|
||||||
{ok, Limiter}.
|
{ok, Limiter}.
|
||||||
|
|
||||||
%% @doc try to request the token and return the result without automatically retrying
|
%% @doc try to request the token and return the result without automatically retrying
|
||||||
-spec check(acquire_type(Limiter), Limiter) -> check_result(Limiter)
|
-spec check(acquire_type(Limiter), Limiter) -> check_result(Limiter) when
|
||||||
when Limiter :: limiter().
|
Limiter :: limiter().
|
||||||
check(_, infinity) ->
|
check(_, infinity) ->
|
||||||
{ok, infinity};
|
{ok, infinity};
|
||||||
|
|
||||||
check(Need, Limiter) when is_integer(Need), Need > 0 ->
|
check(Need, Limiter) when is_integer(Need), Need > 0 ->
|
||||||
case do_check(Need, Limiter) of
|
case do_check(Need, Limiter) of
|
||||||
{ok, _} = Done ->
|
{ok, _} = Done ->
|
||||||
Done;
|
Done;
|
||||||
{PauseType, Pause, Ctx, Limiter2} ->
|
{PauseType, Pause, Ctx, Limiter2} ->
|
||||||
{PauseType,
|
{PauseType, Pause, Ctx#{start => ?NOW, need => Need}, Limiter2}
|
||||||
Pause,
|
|
||||||
Ctx#{start => ?NOW, need => Need}, Limiter2}
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% check with retry context.
|
%% check with retry context.
|
||||||
%% when continuation = undefined, the diff will be 0
|
%% when continuation = undefined, the diff will be 0
|
||||||
%% so there is no need to check continuation here
|
%% so there is no need to check continuation here
|
||||||
check(#{continuation := Cont,
|
check(
|
||||||
|
#{
|
||||||
|
continuation := Cont,
|
||||||
diff := Diff,
|
diff := Diff,
|
||||||
start := Start} = Retry,
|
start := Start
|
||||||
#{failure_strategy := Failure,
|
} = Retry,
|
||||||
max_retry_time := RetryTime} = Limiter) when Diff > 0 ->
|
#{
|
||||||
|
failure_strategy := Failure,
|
||||||
|
max_retry_time := RetryTime
|
||||||
|
} = Limiter
|
||||||
|
) when Diff > 0 ->
|
||||||
case Cont(Diff, Limiter) of
|
case Cont(Diff, Limiter) of
|
||||||
{ok, _} = Done ->
|
{ok, _} = Done ->
|
||||||
Done;
|
Done;
|
||||||
|
|
@ -175,13 +200,12 @@ check(#{continuation := Cont,
|
||||||
on_failure(Failure, try_restore(Retry2, Limiter2))
|
on_failure(Failure, try_restore(Retry2, Limiter2))
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
check(_, Limiter) ->
|
check(_, Limiter) ->
|
||||||
{ok, Limiter}.
|
{ok, Limiter}.
|
||||||
|
|
||||||
%% @doc pack the retry context into the limiter data
|
%% @doc pack the retry context into the limiter data
|
||||||
-spec set_retry(retry_context(Limiter), Limiter) -> Limiter
|
-spec set_retry(retry_context(Limiter), Limiter) -> Limiter when
|
||||||
when Limiter :: limiter().
|
Limiter :: limiter().
|
||||||
set_retry(Retry, Limiter) ->
|
set_retry(Retry, Limiter) ->
|
||||||
Limiter#{retry_ctx => Retry}.
|
Limiter#{retry_ctx => Retry}.
|
||||||
|
|
||||||
|
|
@ -189,7 +213,6 @@ set_retry(Retry, Limiter) ->
|
||||||
-spec retry(Limiter) -> check_result(Limiter) when Limiter :: limiter().
|
-spec retry(Limiter) -> check_result(Limiter) when Limiter :: limiter().
|
||||||
retry(#{retry_ctx := Retry} = Limiter) when is_map(Retry) ->
|
retry(#{retry_ctx := Retry} = Limiter) when is_map(Retry) ->
|
||||||
check(Retry, Limiter#{retry_ctx := undefined});
|
check(Retry, Limiter#{retry_ctx := undefined});
|
||||||
|
|
||||||
retry(Limiter) ->
|
retry(Limiter) ->
|
||||||
{ok, Limiter}.
|
{ok, Limiter}.
|
||||||
|
|
||||||
|
|
@ -202,30 +225,32 @@ make_future(Need) ->
|
||||||
|
|
||||||
%% @doc get the number of tokens currently available
|
%% @doc get the number of tokens currently available
|
||||||
-spec available(limiter()) -> decimal().
|
-spec available(limiter()) -> decimal().
|
||||||
available(#{tokens := Tokens,
|
available(#{
|
||||||
rate := Rate,
|
tokens := Tokens,
|
||||||
lasttime := LastTime,
|
rate := Rate,
|
||||||
capacity := Capacity,
|
lasttime := LastTime,
|
||||||
bucket := Bucket}) ->
|
capacity := Capacity,
|
||||||
|
bucket := Bucket
|
||||||
|
}) ->
|
||||||
Tokens2 = apply_elapsed_time(Rate, ?NOW - LastTime, Tokens, Capacity),
|
Tokens2 = apply_elapsed_time(Rate, ?NOW - LastTime, Tokens, Capacity),
|
||||||
erlang:min(Tokens2, emqx_limiter_bucket_ref:available(Bucket));
|
erlang:min(Tokens2, emqx_limiter_bucket_ref:available(Bucket));
|
||||||
|
|
||||||
available(#{bucket := Bucket}) ->
|
available(#{bucket := Bucket}) ->
|
||||||
emqx_limiter_bucket_ref:available(Bucket);
|
emqx_limiter_bucket_ref:available(Bucket);
|
||||||
|
|
||||||
available(infinity) ->
|
available(infinity) ->
|
||||||
infinity.
|
infinity.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec try_consume(millisecond(),
|
-spec try_consume(
|
||||||
acquire_type(Limiter),
|
millisecond(),
|
||||||
Limiter) -> consume_result(Limiter) when Limiter :: limiter().
|
acquire_type(Limiter),
|
||||||
try_consume(LeftTime, Retry, #{failure_strategy := Failure} = Limiter)
|
Limiter
|
||||||
when LeftTime =< 0, is_map(Retry) ->
|
) -> consume_result(Limiter) when Limiter :: limiter().
|
||||||
|
try_consume(LeftTime, Retry, #{failure_strategy := Failure} = Limiter) when
|
||||||
|
LeftTime =< 0, is_map(Retry)
|
||||||
|
->
|
||||||
on_failure(Failure, try_restore(Retry, Limiter));
|
on_failure(Failure, try_restore(Retry, Limiter));
|
||||||
|
|
||||||
try_consume(LeftTime, Need, Limiter) when is_integer(Need) ->
|
try_consume(LeftTime, Need, Limiter) when is_integer(Need) ->
|
||||||
case do_check(Need, Limiter) of
|
case do_check(Need, Limiter) of
|
||||||
{ok, _} = Done ->
|
{ok, _} = Done ->
|
||||||
|
|
@ -234,10 +259,14 @@ try_consume(LeftTime, Need, Limiter) when is_integer(Need) ->
|
||||||
timer:sleep(erlang:min(LeftTime, Pause)),
|
timer:sleep(erlang:min(LeftTime, Pause)),
|
||||||
try_consume(LeftTime - Pause, Ctx#{need => Need}, Limiter2)
|
try_consume(LeftTime - Pause, Ctx#{need => Need}, Limiter2)
|
||||||
end;
|
end;
|
||||||
|
try_consume(
|
||||||
try_consume(LeftTime,
|
LeftTime,
|
||||||
#{continuation := Cont,
|
#{
|
||||||
diff := Diff} = Retry, Limiter) when Diff > 0 ->
|
continuation := Cont,
|
||||||
|
diff := Diff
|
||||||
|
} = Retry,
|
||||||
|
Limiter
|
||||||
|
) when Diff > 0 ->
|
||||||
case Cont(Diff, Limiter) of
|
case Cont(Diff, Limiter) of
|
||||||
{ok, _} = Done ->
|
{ok, _} = Done ->
|
||||||
Done;
|
Done;
|
||||||
|
|
@ -245,64 +274,78 @@ try_consume(LeftTime,
|
||||||
timer:sleep(erlang:min(LeftTime, Pause)),
|
timer:sleep(erlang:min(LeftTime, Pause)),
|
||||||
try_consume(LeftTime - Pause, maps:merge(Retry, Ctx), Limiter2)
|
try_consume(LeftTime - Pause, maps:merge(Retry, Ctx), Limiter2)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
try_consume(_, _, Limiter) ->
|
try_consume(_, _, Limiter) ->
|
||||||
{ok, Limiter}.
|
{ok, Limiter}.
|
||||||
|
|
||||||
-spec do_check(acquire_type(Limiter), Limiter) -> inner_check_result(Limiter)
|
-spec do_check(acquire_type(Limiter), Limiter) -> inner_check_result(Limiter) when
|
||||||
when Limiter :: limiter().
|
Limiter :: limiter().
|
||||||
do_check(Need, #{tokens := Tokens} = Limiter) when Need =< Tokens ->
|
do_check(Need, #{tokens := Tokens} = Limiter) when Need =< Tokens ->
|
||||||
do_check_with_parent_limiter(Need, Limiter);
|
do_check_with_parent_limiter(Need, Limiter);
|
||||||
|
|
||||||
do_check(Need, #{tokens := _} = Limiter) ->
|
do_check(Need, #{tokens := _} = Limiter) ->
|
||||||
do_reset(Need, Limiter);
|
do_reset(Need, Limiter);
|
||||||
|
do_check(
|
||||||
do_check(Need, #{divisible := Divisible,
|
Need,
|
||||||
bucket := Bucket} = Ref) ->
|
#{
|
||||||
|
divisible := Divisible,
|
||||||
|
bucket := Bucket
|
||||||
|
} = Ref
|
||||||
|
) ->
|
||||||
case emqx_limiter_bucket_ref:check(Need, Bucket, Divisible) of
|
case emqx_limiter_bucket_ref:check(Need, Bucket, Divisible) of
|
||||||
{ok, Tokens} ->
|
{ok, Tokens} ->
|
||||||
may_return_or_pause(Tokens, Ref);
|
may_return_or_pause(Tokens, Ref);
|
||||||
{PauseType, Rate, Obtained} ->
|
{PauseType, Rate, Obtained} ->
|
||||||
return_pause(Rate,
|
return_pause(
|
||||||
PauseType,
|
Rate,
|
||||||
fun ?FUNCTION_NAME/2, Need - Obtained, Ref)
|
PauseType,
|
||||||
|
fun ?FUNCTION_NAME/2,
|
||||||
|
Need - Obtained,
|
||||||
|
Ref
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
on_failure(force, Limiter) ->
|
on_failure(force, Limiter) ->
|
||||||
{ok, Limiter};
|
{ok, Limiter};
|
||||||
|
|
||||||
on_failure(drop, Limiter) ->
|
on_failure(drop, Limiter) ->
|
||||||
{drop, Limiter};
|
{drop, Limiter};
|
||||||
|
|
||||||
on_failure(throw, Limiter) ->
|
on_failure(throw, Limiter) ->
|
||||||
Message = io_lib:format("limiter consume failed, limiter:~p~n", [Limiter]),
|
Message = io_lib:format("limiter consume failed, limiter:~p~n", [Limiter]),
|
||||||
erlang:throw({rate_check_fail, Message}).
|
erlang:throw({rate_check_fail, Message}).
|
||||||
|
|
||||||
-spec do_check_with_parent_limiter(pos_integer(), token_bucket_limiter()) ->
|
-spec do_check_with_parent_limiter(pos_integer(), token_bucket_limiter()) ->
|
||||||
inner_check_result(token_bucket_limiter()).
|
inner_check_result(token_bucket_limiter()).
|
||||||
do_check_with_parent_limiter(Need,
|
do_check_with_parent_limiter(
|
||||||
#{tokens := Tokens,
|
Need,
|
||||||
divisible := Divisible,
|
#{
|
||||||
bucket := Bucket} = Limiter) ->
|
tokens := Tokens,
|
||||||
|
divisible := Divisible,
|
||||||
|
bucket := Bucket
|
||||||
|
} = Limiter
|
||||||
|
) ->
|
||||||
case emqx_limiter_bucket_ref:check(Need, Bucket, Divisible) of
|
case emqx_limiter_bucket_ref:check(Need, Bucket, Divisible) of
|
||||||
{ok, RefLeft} ->
|
{ok, RefLeft} ->
|
||||||
Left = sub(Tokens, Need),
|
Left = sub(Tokens, Need),
|
||||||
may_return_or_pause(erlang:min(RefLeft, Left), Limiter#{tokens := Left});
|
may_return_or_pause(erlang:min(RefLeft, Left), Limiter#{tokens := Left});
|
||||||
{PauseType, Rate, Obtained} ->
|
{PauseType, Rate, Obtained} ->
|
||||||
return_pause(Rate,
|
return_pause(
|
||||||
PauseType,
|
Rate,
|
||||||
fun ?FUNCTION_NAME/2,
|
PauseType,
|
||||||
Need - Obtained,
|
fun ?FUNCTION_NAME/2,
|
||||||
Limiter#{tokens := sub(Tokens, Obtained)})
|
Need - Obtained,
|
||||||
|
Limiter#{tokens := sub(Tokens, Obtained)}
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec do_reset(pos_integer(), token_bucket_limiter()) -> inner_check_result(token_bucket_limiter()).
|
-spec do_reset(pos_integer(), token_bucket_limiter()) -> inner_check_result(token_bucket_limiter()).
|
||||||
do_reset(Need,
|
do_reset(
|
||||||
#{tokens := Tokens,
|
Need,
|
||||||
rate := Rate,
|
#{
|
||||||
lasttime := LastTime,
|
tokens := Tokens,
|
||||||
divisible := Divisible,
|
rate := Rate,
|
||||||
capacity := Capacity} = Limiter) ->
|
lasttime := LastTime,
|
||||||
|
divisible := Divisible,
|
||||||
|
capacity := Capacity
|
||||||
|
} = Limiter
|
||||||
|
) ->
|
||||||
Now = ?NOW,
|
Now = ?NOW,
|
||||||
Tokens2 = apply_elapsed_time(Rate, Now - LastTime, Tokens, Capacity),
|
Tokens2 = apply_elapsed_time(Rate, Now - LastTime, Tokens, Capacity),
|
||||||
|
|
||||||
|
|
@ -312,49 +355,54 @@ do_reset(Need,
|
||||||
do_check_with_parent_limiter(Need, Limiter2);
|
do_check_with_parent_limiter(Need, Limiter2);
|
||||||
Available when Divisible andalso Available > 0 ->
|
Available when Divisible andalso Available > 0 ->
|
||||||
%% must be allocated here, because may be Need > Capacity
|
%% must be allocated here, because may be Need > Capacity
|
||||||
return_pause(Rate,
|
return_pause(
|
||||||
partial,
|
Rate,
|
||||||
fun do_reset/2,
|
partial,
|
||||||
Need - Available,
|
fun do_reset/2,
|
||||||
Limiter#{tokens := 0, lasttime := Now});
|
Need - Available,
|
||||||
|
Limiter#{tokens := 0, lasttime := Now}
|
||||||
|
);
|
||||||
_ ->
|
_ ->
|
||||||
return_pause(Rate, pause, fun do_reset/2, Need, Limiter)
|
return_pause(Rate, pause, fun do_reset/2, Need, Limiter)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec return_pause(decimal(), pause_type(), retry_fun(Limiter), pos_integer(), Limiter)
|
-spec return_pause(decimal(), pause_type(), retry_fun(Limiter), pos_integer(), Limiter) ->
|
||||||
-> check_result_pause(Limiter) when Limiter :: limiter().
|
check_result_pause(Limiter)
|
||||||
|
when
|
||||||
|
Limiter :: limiter().
|
||||||
return_pause(infinity, PauseType, Fun, Diff, Limiter) ->
|
return_pause(infinity, PauseType, Fun, Diff, Limiter) ->
|
||||||
%% workaround when emqx_limiter_server's rate is infinity
|
%% workaround when emqx_limiter_server's rate is infinity
|
||||||
{PauseType, ?MINIMUM_PAUSE, make_retry_context(Fun, Diff), Limiter};
|
{PauseType, ?MINIMUM_PAUSE, make_retry_context(Fun, Diff), Limiter};
|
||||||
|
|
||||||
return_pause(Rate, PauseType, Fun, Diff, Limiter) ->
|
return_pause(Rate, PauseType, Fun, Diff, Limiter) ->
|
||||||
Val = erlang:round(Diff * emqx_limiter_schema:minimum_period() / Rate),
|
Val = erlang:round(Diff * emqx_limiter_schema:minimum_period() / Rate),
|
||||||
Pause = emqx_misc:clamp(Val, ?MINIMUM_PAUSE, ?MAXIMUM_PAUSE),
|
Pause = emqx_misc:clamp(Val, ?MINIMUM_PAUSE, ?MAXIMUM_PAUSE),
|
||||||
{PauseType, Pause, make_retry_context(Fun, Diff), Limiter}.
|
{PauseType, Pause, make_retry_context(Fun, Diff), Limiter}.
|
||||||
|
|
||||||
-spec make_retry_context(undefined | retry_fun(Limiter), non_neg_integer()) ->
|
-spec make_retry_context(undefined | retry_fun(Limiter), non_neg_integer()) ->
|
||||||
retry_context(Limiter) when Limiter :: limiter().
|
retry_context(Limiter)
|
||||||
|
when
|
||||||
|
Limiter :: limiter().
|
||||||
make_retry_context(Fun, Diff) ->
|
make_retry_context(Fun, Diff) ->
|
||||||
#{continuation => Fun, diff => Diff}.
|
#{continuation => Fun, diff => Diff}.
|
||||||
|
|
||||||
-spec try_restore(retry_context(Limiter), Limiter) -> Limiter
|
-spec try_restore(retry_context(Limiter), Limiter) -> Limiter when
|
||||||
when Limiter :: limiter().
|
Limiter :: limiter().
|
||||||
try_restore(#{need := Need, diff := Diff},
|
try_restore(
|
||||||
#{tokens := Tokens, capacity := Capacity, bucket := Bucket} = Limiter) ->
|
#{need := Need, diff := Diff},
|
||||||
|
#{tokens := Tokens, capacity := Capacity, bucket := Bucket} = Limiter
|
||||||
|
) ->
|
||||||
Back = Need - Diff,
|
Back = Need - Diff,
|
||||||
Tokens2 = erlang:min(Capacity, Back + Tokens),
|
Tokens2 = erlang:min(Capacity, Back + Tokens),
|
||||||
emqx_limiter_bucket_ref:try_restore(Back, Bucket),
|
emqx_limiter_bucket_ref:try_restore(Back, Bucket),
|
||||||
Limiter#{tokens := Tokens2};
|
Limiter#{tokens := Tokens2};
|
||||||
|
|
||||||
try_restore(#{need := Need, diff := Diff}, #{bucket := Bucket} = Limiter) ->
|
try_restore(#{need := Need, diff := Diff}, #{bucket := Bucket} = Limiter) ->
|
||||||
emqx_limiter_bucket_ref:try_restore(Need - Diff, Bucket),
|
emqx_limiter_bucket_ref:try_restore(Need - Diff, Bucket),
|
||||||
Limiter.
|
Limiter.
|
||||||
|
|
||||||
-spec may_return_or_pause(non_neg_integer(), Limiter) -> check_result(Limiter)
|
-spec may_return_or_pause(non_neg_integer(), Limiter) -> check_result(Limiter) when
|
||||||
when Limiter :: limiter().
|
Limiter :: limiter().
|
||||||
may_return_or_pause(Left, #{low_water_mark := Mark} = Limiter) when Left >= Mark ->
|
may_return_or_pause(Left, #{low_water_mark := Mark} = Limiter) when Left >= Mark ->
|
||||||
{ok, Limiter};
|
{ok, Limiter};
|
||||||
|
|
||||||
may_return_or_pause(_, Limiter) ->
|
may_return_or_pause(_, Limiter) ->
|
||||||
{pause, ?MINIMUM_PAUSE, make_retry_context(undefined, 0), Limiter}.
|
{pause, ?MINIMUM_PAUSE, make_retry_context(undefined, 0), Limiter}.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
%% -*- mode: erlang -*-
|
%% -*- mode: erlang -*-
|
||||||
{application, emqx_limiter,
|
{application, emqx_limiter, [
|
||||||
[{description, "EMQX Hierarchical Limiter"},
|
{description, "EMQX Hierarchical Limiter"},
|
||||||
{vsn, "1.0.0"}, % strict semver, bump manually!
|
% strict semver, bump manually!
|
||||||
{modules, []},
|
{vsn, "1.0.0"},
|
||||||
{registered, [emqx_limiter_sup]},
|
{modules, []},
|
||||||
{applications, [kernel,stdlib,emqx]},
|
{registered, [emqx_limiter_sup]},
|
||||||
{mod, {emqx_limiter_app,[]}},
|
{applications, [kernel, stdlib, emqx]},
|
||||||
{env, []},
|
{mod, {emqx_limiter_app, []}},
|
||||||
{licenses, ["Apache-2.0"]},
|
{env, []},
|
||||||
{maintainers, ["EMQX Team <contact@emqx.io>"]},
|
{licenses, ["Apache-2.0"]},
|
||||||
{links, []}
|
{maintainers, ["EMQX Team <contact@emqx.io>"]},
|
||||||
]}.
|
{links, []}
|
||||||
|
]}.
|
||||||
|
|
|
||||||
|
|
@ -31,13 +31,16 @@
|
||||||
%% top supervisor of the tree.
|
%% top supervisor of the tree.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec start(StartType :: normal |
|
-spec start(
|
||||||
{takeover, Node :: node()} |
|
StartType ::
|
||||||
{failover, Node :: node()},
|
normal
|
||||||
StartArgs :: term()) ->
|
| {takeover, Node :: node()}
|
||||||
{ok, Pid :: pid()} |
|
| {failover, Node :: node()},
|
||||||
{ok, Pid :: pid(), State :: term()} |
|
StartArgs :: term()
|
||||||
{error, Reason :: term()}.
|
) ->
|
||||||
|
{ok, Pid :: pid()}
|
||||||
|
| {ok, Pid :: pid(), State :: term()}
|
||||||
|
| {error, Reason :: term()}.
|
||||||
start(_StartType, _StartArgs) ->
|
start(_StartType, _StartArgs) ->
|
||||||
{ok, _} = emqx_limiter_sup:start_link().
|
{ok, _} = emqx_limiter_sup:start_link().
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,57 +21,74 @@
|
||||||
%% @end
|
%% @end
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ new/3, check/3, try_restore/2
|
-export([
|
||||||
, available/1]).
|
new/3,
|
||||||
|
check/3,
|
||||||
|
try_restore/2,
|
||||||
|
available/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([bucket_ref/0]).
|
-export_type([bucket_ref/0]).
|
||||||
|
|
||||||
-type infinity_bucket_ref() :: infinity.
|
-type infinity_bucket_ref() :: infinity.
|
||||||
-type finite_bucket_ref() :: #{ counter := counters:counters_ref()
|
-type finite_bucket_ref() :: #{
|
||||||
, index := index()
|
counter := counters:counters_ref(),
|
||||||
, rate := rate()}.
|
index := index(),
|
||||||
|
rate := rate()
|
||||||
|
}.
|
||||||
|
|
||||||
-type bucket_ref() :: infinity_bucket_ref()
|
-type bucket_ref() ::
|
||||||
| finite_bucket_ref().
|
infinity_bucket_ref()
|
||||||
|
| finite_bucket_ref().
|
||||||
|
|
||||||
-type index() :: emqx_limiter_server:index().
|
-type index() :: emqx_limiter_server:index().
|
||||||
-type rate() :: emqx_limiter_decimal:decimal().
|
-type rate() :: emqx_limiter_decimal:decimal().
|
||||||
-type check_failure_type() :: partial | pause.
|
-type check_failure_type() :: partial | pause.
|
||||||
|
|
||||||
|
-elvis([{elvis_style, no_if_expression, disable}]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% API
|
%% API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec new(undefined | counters:countres_ref(),
|
-spec new(
|
||||||
undefined | index(),
|
undefined | counters:countres_ref(),
|
||||||
rate()) -> bucket_ref().
|
undefined | index(),
|
||||||
|
rate()
|
||||||
|
) -> bucket_ref().
|
||||||
new(undefined, _, _) ->
|
new(undefined, _, _) ->
|
||||||
infinity;
|
infinity;
|
||||||
|
|
||||||
new(Counter, Index, Rate) ->
|
new(Counter, Index, Rate) ->
|
||||||
#{counter => Counter,
|
#{
|
||||||
index => Index,
|
counter => Counter,
|
||||||
rate => Rate}.
|
index => Index,
|
||||||
|
rate => Rate
|
||||||
|
}.
|
||||||
|
|
||||||
%% @doc check tokens
|
%% @doc check tokens
|
||||||
-spec check(pos_integer(), bucket_ref(), Disivisble :: boolean()) ->
|
-spec check(pos_integer(), bucket_ref(), Disivisble :: boolean()) ->
|
||||||
HasToken :: {ok, emqx_limiter_decimal:decimal()}
|
HasToken ::
|
||||||
| {check_failure_type(), rate(), pos_integer()}.
|
{ok, emqx_limiter_decimal:decimal()}
|
||||||
|
| {check_failure_type(), rate(), pos_integer()}.
|
||||||
check(_, infinity, _) ->
|
check(_, infinity, _) ->
|
||||||
{ok, infinity};
|
{ok, infinity};
|
||||||
|
check(
|
||||||
check(Need,
|
Need,
|
||||||
#{counter := Counter,
|
#{
|
||||||
|
counter := Counter,
|
||||||
index := Index,
|
index := Index,
|
||||||
rate := Rate},
|
rate := Rate
|
||||||
Divisible)->
|
},
|
||||||
|
Divisible
|
||||||
|
) ->
|
||||||
RefToken = counters:get(Counter, Index),
|
RefToken = counters:get(Counter, Index),
|
||||||
if RefToken >= Need ->
|
if
|
||||||
|
RefToken >= Need ->
|
||||||
counters:sub(Counter, Index, Need),
|
counters:sub(Counter, Index, Need),
|
||||||
{ok, RefToken - Need};
|
{ok, RefToken - Need};
|
||||||
Divisible andalso RefToken > 0 ->
|
Divisible andalso RefToken > 0 ->
|
||||||
counters:sub(Counter, Index, RefToken),
|
counters:sub(Counter, Index, RefToken),
|
||||||
{partial, Rate, RefToken};
|
{partial, Rate, RefToken};
|
||||||
true ->
|
true ->
|
||||||
{pause, Rate, 0}
|
{pause, Rate, 0}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -93,7 +110,6 @@ try_restore(Inc, #{counter := Counter, index := Index}) ->
|
||||||
-spec available(bucket_ref()) -> emqx_limiter_decimal:decimal().
|
-spec available(bucket_ref()) -> emqx_limiter_decimal:decimal().
|
||||||
available(#{counter := Counter, index := Index}) ->
|
available(#{counter := Counter, index := Index}) ->
|
||||||
counters:get(Counter, Index);
|
counters:get(Counter, Index);
|
||||||
|
|
||||||
available(infinity) ->
|
available(infinity) ->
|
||||||
infinity.
|
infinity.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,21 +21,31 @@
|
||||||
%% @end
|
%% @end
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ new/0, new/1, new/2, get_limiter_by_names/2
|
-export([
|
||||||
, add_new/3, update_by_name/3, set_retry_context/2
|
new/0, new/1, new/2,
|
||||||
, check/3, retry/2, get_retry_context/1
|
get_limiter_by_names/2,
|
||||||
, check_list/2, retry_list/2
|
add_new/3,
|
||||||
]).
|
update_by_name/3,
|
||||||
|
set_retry_context/2,
|
||||||
|
check/3,
|
||||||
|
retry/2,
|
||||||
|
get_retry_context/1,
|
||||||
|
check_list/2,
|
||||||
|
retry_list/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([container/0, check_result/0]).
|
-export_type([container/0, check_result/0]).
|
||||||
|
|
||||||
-type container() :: #{ limiter_type() => undefined | limiter()
|
-type container() :: #{
|
||||||
%% the retry context of the limiter
|
limiter_type() => undefined | limiter(),
|
||||||
, retry_key() => undefined
|
%% the retry context of the limiter
|
||||||
| retry_context()
|
retry_key() =>
|
||||||
| future()
|
undefined
|
||||||
, retry_ctx := undefined | any() %% the retry context of the container
|
| retry_context()
|
||||||
}.
|
| future(),
|
||||||
|
%% the retry context of the container
|
||||||
|
retry_ctx := undefined | any()
|
||||||
|
}.
|
||||||
|
|
||||||
-type future() :: pos_integer().
|
-type future() :: pos_integer().
|
||||||
-type limiter_type() :: emqx_limiter_schema:limiter_type().
|
-type limiter_type() :: emqx_limiter_schema:limiter_type().
|
||||||
|
|
@ -43,9 +53,10 @@
|
||||||
-type retry_context() :: emqx_htb_limiter:retry_context().
|
-type retry_context() :: emqx_htb_limiter:retry_context().
|
||||||
-type bucket_name() :: emqx_limiter_schema:bucket_name().
|
-type bucket_name() :: emqx_limiter_schema:bucket_name().
|
||||||
-type millisecond() :: non_neg_integer().
|
-type millisecond() :: non_neg_integer().
|
||||||
-type check_result() :: {ok, container()}
|
-type check_result() ::
|
||||||
| {drop, container()}
|
{ok, container()}
|
||||||
| {pause, millisecond(), container()}.
|
| {drop, container()}
|
||||||
|
| {pause, millisecond(), container()}.
|
||||||
|
|
||||||
-define(RETRY_KEY(Type), {retry, Type}).
|
-define(RETRY_KEY(Type), {retry, Type}).
|
||||||
-type retry_key() :: ?RETRY_KEY(limiter_type()).
|
-type retry_key() :: ?RETRY_KEY(limiter_type()).
|
||||||
|
|
@ -62,36 +73,43 @@ new() ->
|
||||||
new(Types) ->
|
new(Types) ->
|
||||||
new(Types, #{}).
|
new(Types, #{}).
|
||||||
|
|
||||||
-spec new(list(limiter_type()),
|
-spec new(
|
||||||
#{limiter_type() => emqx_limiter_schema:bucket_name()}) -> container().
|
list(limiter_type()),
|
||||||
|
#{limiter_type() => emqx_limiter_schema:bucket_name()}
|
||||||
|
) -> container().
|
||||||
new(Types, Names) ->
|
new(Types, Names) ->
|
||||||
get_limiter_by_names(Types, Names).
|
get_limiter_by_names(Types, Names).
|
||||||
|
|
||||||
%% @doc generate a container
|
%% @doc generate a container
|
||||||
%% according to the type of limiter and the bucket name configuration of the limiter
|
%% according to the type of limiter and the bucket name configuration of the limiter
|
||||||
%% @end
|
%% @end
|
||||||
-spec get_limiter_by_names(list(limiter_type()),
|
-spec get_limiter_by_names(
|
||||||
#{limiter_type() => emqx_limiter_schema:bucket_name()}) -> container().
|
list(limiter_type()),
|
||||||
|
#{limiter_type() => emqx_limiter_schema:bucket_name()}
|
||||||
|
) -> container().
|
||||||
get_limiter_by_names(Types, BucketNames) ->
|
get_limiter_by_names(Types, BucketNames) ->
|
||||||
Init = fun(Type, Acc) ->
|
Init = fun(Type, Acc) ->
|
||||||
Limiter = emqx_limiter_server:connect(Type, BucketNames),
|
Limiter = emqx_limiter_server:connect(Type, BucketNames),
|
||||||
add_new(Type, Limiter, Acc)
|
add_new(Type, Limiter, Acc)
|
||||||
end,
|
end,
|
||||||
lists:foldl(Init, #{retry_ctx => undefined}, Types).
|
lists:foldl(Init, #{retry_ctx => undefined}, Types).
|
||||||
|
|
||||||
%% @doc add the specified type of limiter to the container
|
%% @doc add the specified type of limiter to the container
|
||||||
-spec update_by_name(limiter_type(),
|
-spec update_by_name(
|
||||||
bucket_name() | #{limiter_type() => bucket_name()},
|
limiter_type(),
|
||||||
container()) -> container().
|
bucket_name() | #{limiter_type() => bucket_name()},
|
||||||
|
container()
|
||||||
|
) -> container().
|
||||||
update_by_name(Type, Buckets, Container) ->
|
update_by_name(Type, Buckets, Container) ->
|
||||||
Limiter = emqx_limiter_server:connect(Type, Buckets),
|
Limiter = emqx_limiter_server:connect(Type, Buckets),
|
||||||
add_new(Type, Limiter, Container).
|
add_new(Type, Limiter, Container).
|
||||||
|
|
||||||
-spec add_new(limiter_type(), limiter(), container()) -> container().
|
-spec add_new(limiter_type(), limiter(), container()) -> container().
|
||||||
add_new(Type, Limiter, Container) ->
|
add_new(Type, Limiter, Container) ->
|
||||||
Container#{ Type => Limiter
|
Container#{
|
||||||
, ?RETRY_KEY(Type) => undefined
|
Type => Limiter,
|
||||||
}.
|
?RETRY_KEY(Type) => undefined
|
||||||
|
}.
|
||||||
|
|
||||||
%% @doc check the specified limiter
|
%% @doc check the specified limiter
|
||||||
-spec check(pos_integer(), limiter_type(), container()) -> check_result().
|
-spec check(pos_integer(), limiter_type(), container()) -> check_result().
|
||||||
|
|
@ -107,18 +125,21 @@ check_list([{Need, Type} | T], Container) ->
|
||||||
check_list(T, Container#{Type := Limiter2});
|
check_list(T, Container#{Type := Limiter2});
|
||||||
{_, PauseMs, Ctx, Limiter2} ->
|
{_, PauseMs, Ctx, Limiter2} ->
|
||||||
Fun = fun({FN, FT}, Acc) ->
|
Fun = fun({FN, FT}, Acc) ->
|
||||||
Future = emqx_htb_limiter:make_future(FN),
|
Future = emqx_htb_limiter:make_future(FN),
|
||||||
Acc#{?RETRY_KEY(FT) := Future}
|
Acc#{?RETRY_KEY(FT) := Future}
|
||||||
end,
|
end,
|
||||||
C2 = lists:foldl(Fun,
|
C2 = lists:foldl(
|
||||||
Container#{Type := Limiter2,
|
Fun,
|
||||||
?RETRY_KEY(Type) := Ctx},
|
Container#{
|
||||||
T),
|
Type := Limiter2,
|
||||||
|
?RETRY_KEY(Type) := Ctx
|
||||||
|
},
|
||||||
|
T
|
||||||
|
),
|
||||||
{pause, PauseMs, C2};
|
{pause, PauseMs, C2};
|
||||||
{drop, Limiter2} ->
|
{drop, Limiter2} ->
|
||||||
{drop, Container#{Type := Limiter2}}
|
{drop, Container#{Type := Limiter2}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
check_list([], Container) ->
|
check_list([], Container) ->
|
||||||
{ok, Container}.
|
{ok, Container}.
|
||||||
|
|
||||||
|
|
@ -132,24 +153,23 @@ retry(Type, Container) ->
|
||||||
retry_list([Type | T], Container) ->
|
retry_list([Type | T], Container) ->
|
||||||
Key = ?RETRY_KEY(Type),
|
Key = ?RETRY_KEY(Type),
|
||||||
case Container of
|
case Container of
|
||||||
#{Type := Limiter,
|
#{
|
||||||
Key := Retry} when Retry =/= undefined ->
|
Type := Limiter,
|
||||||
|
Key := Retry
|
||||||
|
} when Retry =/= undefined ->
|
||||||
case emqx_htb_limiter:check(Retry, Limiter) of
|
case emqx_htb_limiter:check(Retry, Limiter) of
|
||||||
{ok, Limiter2} ->
|
{ok, Limiter2} ->
|
||||||
%% undefined meaning there is no retry context or there is no need to retry
|
%% undefined meaning there is no retry context or there is no need to retry
|
||||||
%% when a limiter has a undefined retry context, the check will always success
|
%% when a limiter has a undefined retry context, the check will always success
|
||||||
retry_list(T, Container#{Type := Limiter2, Key := undefined});
|
retry_list(T, Container#{Type := Limiter2, Key := undefined});
|
||||||
{_, PauseMs, Ctx, Limiter2} ->
|
{_, PauseMs, Ctx, Limiter2} ->
|
||||||
{pause,
|
{pause, PauseMs, Container#{Type := Limiter2, Key := Ctx}};
|
||||||
PauseMs,
|
|
||||||
Container#{Type := Limiter2, Key := Ctx}};
|
|
||||||
{drop, Limiter2} ->
|
{drop, Limiter2} ->
|
||||||
{drop, Container#{Type := Limiter2}}
|
{drop, Container#{Type := Limiter2}}
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
retry_list(T, Container)
|
retry_list(T, Container)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
retry_list([], Container) ->
|
retry_list([], Container) ->
|
||||||
{ok, Container}.
|
{ok, Container}.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,12 @@
|
||||||
-module(emqx_limiter_correction).
|
-module(emqx_limiter_correction).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ add/2 ]).
|
-export([add/2]).
|
||||||
|
|
||||||
-type correction_value() :: #{ correction := emqx_limiter_decimal:zero_or_float()
|
-type correction_value() :: #{
|
||||||
, any() => any()
|
correction := emqx_limiter_decimal:zero_or_float(),
|
||||||
}.
|
any() => any()
|
||||||
|
}.
|
||||||
|
|
||||||
-export_type([correction_value/0]).
|
-export_type([correction_value/0]).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,8 +19,13 @@
|
||||||
-module(emqx_limiter_decimal).
|
-module(emqx_limiter_decimal).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ add/2, sub/2, mul/2
|
-export([
|
||||||
, put_to_counter/3, floor_div/2]).
|
add/2,
|
||||||
|
sub/2,
|
||||||
|
mul/2,
|
||||||
|
put_to_counter/3,
|
||||||
|
floor_div/2
|
||||||
|
]).
|
||||||
-export_type([decimal/0, zero_or_float/0]).
|
-export_type([decimal/0, zero_or_float/0]).
|
||||||
|
|
||||||
-type decimal() :: infinity | number().
|
-type decimal() :: infinity | number().
|
||||||
|
|
@ -30,33 +35,35 @@
|
||||||
%%% API
|
%%% API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec add(decimal(), decimal()) -> decimal().
|
-spec add(decimal(), decimal()) -> decimal().
|
||||||
add(A, B) when A =:= infinity
|
add(A, B) when
|
||||||
orelse B =:= infinity ->
|
A =:= infinity orelse
|
||||||
|
B =:= infinity
|
||||||
|
->
|
||||||
infinity;
|
infinity;
|
||||||
|
|
||||||
add(A, B) ->
|
add(A, B) ->
|
||||||
A + B.
|
A + B.
|
||||||
|
|
||||||
-spec sub(decimal(), decimal()) -> decimal().
|
-spec sub(decimal(), decimal()) -> decimal().
|
||||||
sub(A, B) when A =:= infinity
|
sub(A, B) when
|
||||||
orelse B =:= infinity ->
|
A =:= infinity orelse
|
||||||
|
B =:= infinity
|
||||||
|
->
|
||||||
infinity;
|
infinity;
|
||||||
|
|
||||||
sub(A, B) ->
|
sub(A, B) ->
|
||||||
A - B.
|
A - B.
|
||||||
|
|
||||||
-spec mul(decimal(), decimal()) -> decimal().
|
-spec mul(decimal(), decimal()) -> decimal().
|
||||||
mul(A, B) when A =:= infinity
|
mul(A, B) when
|
||||||
orelse B =:= infinity ->
|
A =:= infinity orelse
|
||||||
|
B =:= infinity
|
||||||
|
->
|
||||||
infinity;
|
infinity;
|
||||||
|
|
||||||
mul(A, B) ->
|
mul(A, B) ->
|
||||||
A * B.
|
A * B.
|
||||||
|
|
||||||
-spec floor_div(decimal(), number()) -> decimal().
|
-spec floor_div(decimal(), number()) -> decimal().
|
||||||
floor_div(infinity, _) ->
|
floor_div(infinity, _) ->
|
||||||
infinity;
|
infinity;
|
||||||
|
|
||||||
floor_div(A, B) ->
|
floor_div(A, B) ->
|
||||||
erlang:floor(A / B).
|
erlang:floor(A / B).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,14 +22,26 @@
|
||||||
-include_lib("stdlib/include/ms_transform.hrl").
|
-include_lib("stdlib/include/ms_transform.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ start_link/0, start_server/1, find_bucket/1
|
-export([
|
||||||
, find_bucket/2, insert_bucket/2, insert_bucket/3
|
start_link/0,
|
||||||
, make_path/2, restart_server/1
|
start_server/1,
|
||||||
]).
|
find_bucket/1,
|
||||||
|
find_bucket/2,
|
||||||
|
insert_bucket/2, insert_bucket/3,
|
||||||
|
make_path/2,
|
||||||
|
restart_server/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
-export([
|
||||||
terminate/2, code_change/3, format_status/2]).
|
init/1,
|
||||||
|
handle_call/3,
|
||||||
|
handle_cast/2,
|
||||||
|
handle_info/2,
|
||||||
|
terminate/2,
|
||||||
|
code_change/3,
|
||||||
|
format_status/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([path/0]).
|
-export_type([path/0]).
|
||||||
|
|
||||||
|
|
@ -38,9 +50,10 @@
|
||||||
-type bucket_name() :: emqx_limiter_schema:bucket_name().
|
-type bucket_name() :: emqx_limiter_schema:bucket_name().
|
||||||
|
|
||||||
%% counter record in ets table
|
%% counter record in ets table
|
||||||
-record(bucket, { path :: path()
|
-record(bucket, {
|
||||||
, bucket :: bucket_ref()
|
path :: path(),
|
||||||
}).
|
bucket :: bucket_ref()
|
||||||
|
}).
|
||||||
|
|
||||||
-type bucket_ref() :: emqx_limiter_bucket_ref:bucket_ref().
|
-type bucket_ref() :: emqx_limiter_bucket_ref:bucket_ref().
|
||||||
|
|
||||||
|
|
@ -58,7 +71,7 @@ restart_server(Type) ->
|
||||||
emqx_limiter_server_sup:restart(Type).
|
emqx_limiter_server_sup:restart(Type).
|
||||||
|
|
||||||
-spec find_bucket(limiter_type(), bucket_name()) ->
|
-spec find_bucket(limiter_type(), bucket_name()) ->
|
||||||
{ok, bucket_ref()} | undefined.
|
{ok, bucket_ref()} | undefined.
|
||||||
find_bucket(Type, BucketName) ->
|
find_bucket(Type, BucketName) ->
|
||||||
find_bucket(make_path(Type, BucketName)).
|
find_bucket(make_path(Type, BucketName)).
|
||||||
|
|
||||||
|
|
@ -71,13 +84,14 @@ find_bucket(Path) ->
|
||||||
undefined
|
undefined
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec insert_bucket(limiter_type(),
|
-spec insert_bucket(
|
||||||
bucket_name(),
|
limiter_type(),
|
||||||
bucket_ref()) -> boolean().
|
bucket_name(),
|
||||||
|
bucket_ref()
|
||||||
|
) -> boolean().
|
||||||
insert_bucket(Type, BucketName, Bucket) ->
|
insert_bucket(Type, BucketName, Bucket) ->
|
||||||
inner_insert_bucket(make_path(Type, BucketName), Bucket).
|
inner_insert_bucket(make_path(Type, BucketName), Bucket).
|
||||||
|
|
||||||
|
|
||||||
-spec insert_bucket(path(), bucket_ref()) -> true.
|
-spec insert_bucket(path(), bucket_ref()) -> true.
|
||||||
insert_bucket(Path, Bucket) ->
|
insert_bucket(Path, Bucket) ->
|
||||||
inner_insert_bucket(Path, Bucket).
|
inner_insert_bucket(Path, Bucket).
|
||||||
|
|
@ -91,10 +105,11 @@ make_path(Type, BucketName) ->
|
||||||
%% Starts the server
|
%% Starts the server
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec start_link() -> {ok, Pid :: pid()} |
|
-spec start_link() ->
|
||||||
{error, Error :: {already_started, pid()}} |
|
{ok, Pid :: pid()}
|
||||||
{error, Error :: term()} |
|
| {error, Error :: {already_started, pid()}}
|
||||||
ignore.
|
| {error, Error :: term()}
|
||||||
|
| ignore.
|
||||||
start_link() ->
|
start_link() ->
|
||||||
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
||||||
|
|
||||||
|
|
@ -108,16 +123,22 @@ start_link() ->
|
||||||
%% Initializes the server
|
%% Initializes the server
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec init(Args :: term()) -> {ok, State :: term()} |
|
-spec init(Args :: term()) ->
|
||||||
{ok, State :: term(), Timeout :: timeout()} |
|
{ok, State :: term()}
|
||||||
{ok, State :: term(), hibernate} |
|
| {ok, State :: term(), Timeout :: timeout()}
|
||||||
{stop, Reason :: term()} |
|
| {ok, State :: term(), hibernate}
|
||||||
ignore.
|
| {stop, Reason :: term()}
|
||||||
|
| ignore.
|
||||||
init([]) ->
|
init([]) ->
|
||||||
_ = ets:new(?TAB, [ set, public, named_table, {keypos, #bucket.path}
|
_ = ets:new(?TAB, [
|
||||||
, {write_concurrency, true}, {read_concurrency, true}
|
set,
|
||||||
, {heir, erlang:whereis(emqx_limiter_sup), none}
|
public,
|
||||||
]),
|
named_table,
|
||||||
|
{keypos, #bucket.path},
|
||||||
|
{write_concurrency, true},
|
||||||
|
{read_concurrency, true},
|
||||||
|
{heir, erlang:whereis(emqx_limiter_sup), none}
|
||||||
|
]),
|
||||||
{ok, #{}}.
|
{ok, #{}}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -127,14 +148,14 @@ init([]) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec handle_call(Request :: term(), From :: {pid(), term()}, State :: term()) ->
|
-spec handle_call(Request :: term(), From :: {pid(), term()}, State :: term()) ->
|
||||||
{reply, Reply :: term(), NewState :: term()} |
|
{reply, Reply :: term(), NewState :: term()}
|
||||||
{reply, Reply :: term(), NewState :: term(), Timeout :: timeout()} |
|
| {reply, Reply :: term(), NewState :: term(), Timeout :: timeout()}
|
||||||
{reply, Reply :: term(), NewState :: term(), hibernate} |
|
| {reply, Reply :: term(), NewState :: term(), hibernate}
|
||||||
{noreply, NewState :: term()} |
|
| {noreply, NewState :: term()}
|
||||||
{noreply, NewState :: term(), Timeout :: timeout()} |
|
| {noreply, NewState :: term(), Timeout :: timeout()}
|
||||||
{noreply, NewState :: term(), hibernate} |
|
| {noreply, NewState :: term(), hibernate}
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: term()} |
|
| {stop, Reason :: term(), Reply :: term(), NewState :: term()}
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
| {stop, Reason :: term(), NewState :: term()}.
|
||||||
handle_call(Req, _From, State) ->
|
handle_call(Req, _From, State) ->
|
||||||
?SLOG(error, #{msg => "unexpected_call", call => Req}),
|
?SLOG(error, #{msg => "unexpected_call", call => Req}),
|
||||||
{reply, ignore, State}.
|
{reply, ignore, State}.
|
||||||
|
|
@ -146,10 +167,10 @@ handle_call(Req, _From, State) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec handle_cast(Request :: term(), State :: term()) ->
|
-spec handle_cast(Request :: term(), State :: term()) ->
|
||||||
{noreply, NewState :: term()} |
|
{noreply, NewState :: term()}
|
||||||
{noreply, NewState :: term(), Timeout :: timeout()} |
|
| {noreply, NewState :: term(), Timeout :: timeout()}
|
||||||
{noreply, NewState :: term(), hibernate} |
|
| {noreply, NewState :: term(), hibernate}
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
| {stop, Reason :: term(), NewState :: term()}.
|
||||||
handle_cast(Req, State) ->
|
handle_cast(Req, State) ->
|
||||||
?SLOG(error, #{msg => "unexpected_cast", cast => Req}),
|
?SLOG(error, #{msg => "unexpected_cast", cast => Req}),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
@ -161,10 +182,10 @@ handle_cast(Req, State) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec handle_info(Info :: timeout() | term(), State :: term()) ->
|
-spec handle_info(Info :: timeout() | term(), State :: term()) ->
|
||||||
{noreply, NewState :: term()} |
|
{noreply, NewState :: term()}
|
||||||
{noreply, NewState :: term(), Timeout :: timeout()} |
|
| {noreply, NewState :: term(), Timeout :: timeout()}
|
||||||
{noreply, NewState :: term(), hibernate} |
|
| {noreply, NewState :: term(), hibernate}
|
||||||
{stop, Reason :: normal | term(), NewState :: term()}.
|
| {stop, Reason :: normal | term(), NewState :: term()}.
|
||||||
handle_info(Info, State) ->
|
handle_info(Info, State) ->
|
||||||
?SLOG(error, #{msg => "unexpected_info", info => Info}),
|
?SLOG(error, #{msg => "unexpected_info", info => Info}),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
@ -178,8 +199,10 @@ handle_info(Info, State) ->
|
||||||
%% with Reason. The return value is ignored.
|
%% with Reason. The return value is ignored.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec terminate(Reason :: normal | shutdown | {shutdown, term()} | term(),
|
-spec terminate(
|
||||||
State :: term()) -> any().
|
Reason :: normal | shutdown | {shutdown, term()} | term(),
|
||||||
|
State :: term()
|
||||||
|
) -> any().
|
||||||
terminate(_Reason, _State) ->
|
terminate(_Reason, _State) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
@ -189,10 +212,13 @@ terminate(_Reason, _State) ->
|
||||||
%% Convert process state when code is changed
|
%% Convert process state when code is changed
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec code_change(OldVsn :: term() | {down, term()},
|
-spec code_change(
|
||||||
State :: term(),
|
OldVsn :: term() | {down, term()},
|
||||||
Extra :: term()) -> {ok, NewState :: term()} |
|
State :: term(),
|
||||||
{error, Reason :: term()}.
|
Extra :: term()
|
||||||
|
) ->
|
||||||
|
{ok, NewState :: term()}
|
||||||
|
| {error, Reason :: term()}.
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
|
|
@ -204,8 +230,10 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%% or when it appears in termination error logs.
|
%% or when it appears in termination error logs.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec format_status(Opt :: normal | terminate,
|
-spec format_status(
|
||||||
Status :: list()) -> Status :: term().
|
Opt :: normal | terminate,
|
||||||
|
Status :: list()
|
||||||
|
) -> Status :: term().
|
||||||
format_status(_Opt, Status) ->
|
format_status(_Opt, Status) ->
|
||||||
Status.
|
Status.
|
||||||
|
|
||||||
|
|
@ -214,5 +242,7 @@ format_status(_Opt, Status) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec inner_insert_bucket(path(), bucket_ref()) -> true.
|
-spec inner_insert_bucket(path(), bucket_ref()) -> true.
|
||||||
inner_insert_bucket(Path, Bucket) ->
|
inner_insert_bucket(Path, Bucket) ->
|
||||||
ets:insert(?TAB,
|
ets:insert(
|
||||||
#bucket{path = Path, bucket = Bucket}).
|
?TAB,
|
||||||
|
#bucket{path = Path, bucket = Bucket}
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -18,43 +18,60 @@
|
||||||
|
|
||||||
-include_lib("typerefl/include/types.hrl").
|
-include_lib("typerefl/include/types.hrl").
|
||||||
|
|
||||||
-export([ roots/0, fields/1, to_rate/1, to_capacity/1
|
-export([
|
||||||
, minimum_period/0, to_burst_rate/1, to_initial/1
|
roots/0,
|
||||||
, namespace/0, get_bucket_cfg_path/2, desc/1
|
fields/1,
|
||||||
]).
|
to_rate/1,
|
||||||
|
to_capacity/1,
|
||||||
|
minimum_period/0,
|
||||||
|
to_burst_rate/1,
|
||||||
|
to_initial/1,
|
||||||
|
namespace/0,
|
||||||
|
get_bucket_cfg_path/2,
|
||||||
|
desc/1
|
||||||
|
]).
|
||||||
|
|
||||||
-define(KILOBYTE, 1024).
|
-define(KILOBYTE, 1024).
|
||||||
|
|
||||||
-type limiter_type() :: bytes_in
|
-type limiter_type() ::
|
||||||
| message_in
|
bytes_in
|
||||||
| connection
|
| message_in
|
||||||
| message_routing
|
| connection
|
||||||
| batch.
|
| message_routing
|
||||||
|
| batch.
|
||||||
|
|
||||||
-type bucket_name() :: atom().
|
-type bucket_name() :: atom().
|
||||||
-type rate() :: infinity | float().
|
-type rate() :: infinity | float().
|
||||||
-type burst_rate() :: 0 | float().
|
-type burst_rate() :: 0 | float().
|
||||||
-type capacity() :: infinity | number(). %% the capacity of the token bucket
|
%% the capacity of the token bucket
|
||||||
-type initial() :: non_neg_integer(). %% initial capacity of the token bucket
|
-type capacity() :: infinity | number().
|
||||||
|
%% initial capacity of the token bucket
|
||||||
|
-type initial() :: non_neg_integer().
|
||||||
-type bucket_path() :: list(atom()).
|
-type bucket_path() :: list(atom()).
|
||||||
|
|
||||||
%% the processing strategy after the failure of the token request
|
%% the processing strategy after the failure of the token request
|
||||||
-type failure_strategy() :: force %% Forced to pass
|
|
||||||
| drop %% discard the current request
|
%% Forced to pass
|
||||||
| throw. %% throw an exception
|
-type failure_strategy() ::
|
||||||
|
force
|
||||||
|
%% discard the current request
|
||||||
|
| drop
|
||||||
|
%% throw an exception
|
||||||
|
| throw.
|
||||||
|
|
||||||
-typerefl_from_string({rate/0, ?MODULE, to_rate}).
|
-typerefl_from_string({rate/0, ?MODULE, to_rate}).
|
||||||
-typerefl_from_string({burst_rate/0, ?MODULE, to_burst_rate}).
|
-typerefl_from_string({burst_rate/0, ?MODULE, to_burst_rate}).
|
||||||
-typerefl_from_string({capacity/0, ?MODULE, to_capacity}).
|
-typerefl_from_string({capacity/0, ?MODULE, to_capacity}).
|
||||||
-typerefl_from_string({initial/0, ?MODULE, to_initial}).
|
-typerefl_from_string({initial/0, ?MODULE, to_initial}).
|
||||||
|
|
||||||
-reflect_type([ rate/0
|
-reflect_type([
|
||||||
, burst_rate/0
|
rate/0,
|
||||||
, capacity/0
|
burst_rate/0,
|
||||||
, initial/0
|
capacity/0,
|
||||||
, failure_strategy/0
|
initial/0,
|
||||||
, bucket_name/0
|
failure_strategy/0,
|
||||||
]).
|
bucket_name/0
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([limiter_type/0, bucket_path/0]).
|
-export_type([limiter_type/0, bucket_path/0]).
|
||||||
|
|
||||||
|
|
@ -66,87 +83,154 @@ namespace() -> limiter.
|
||||||
roots() -> [limiter].
|
roots() -> [limiter].
|
||||||
|
|
||||||
fields(limiter) ->
|
fields(limiter) ->
|
||||||
[ {bytes_in, sc(ref(limiter_opts),
|
[
|
||||||
#{description =>
|
{bytes_in,
|
||||||
<<"The bytes_in limiter.<br>"
|
sc(
|
||||||
|
ref(limiter_opts),
|
||||||
|
#{
|
||||||
|
description =>
|
||||||
|
<<
|
||||||
|
"The bytes_in limiter.<br>"
|
||||||
"It is used to limit the inbound bytes rate for this EMQX node."
|
"It is used to limit the inbound bytes rate for this EMQX node."
|
||||||
"If the this limiter limit is reached,"
|
"If the this limiter limit is reached,"
|
||||||
"the restricted client will be slow down even be hung for a while.">>
|
"the restricted client will be slow down even be hung for a while."
|
||||||
})}
|
>>
|
||||||
, {message_in, sc(ref(limiter_opts),
|
}
|
||||||
#{description =>
|
)},
|
||||||
<<"The message_in limiter.<br>"
|
{message_in,
|
||||||
"This is used to limit the inbound message numbers for this EMQX node"
|
sc(
|
||||||
"If the this limiter limit is reached,"
|
ref(limiter_opts),
|
||||||
"the restricted client will be slow down even be hung for a while.">>
|
#{
|
||||||
})}
|
description =>
|
||||||
, {connection, sc(ref(limiter_opts),
|
<<
|
||||||
#{description =>
|
"The message_in limiter.<br>"
|
||||||
<<"The connection limiter.<br>"
|
"This is used to limit the inbound message numbers for this EMQX node"
|
||||||
"This is used to limit the connection rate for this EMQX node"
|
"If the this limiter limit is reached,"
|
||||||
"If the this limiter limit is reached,"
|
"the restricted client will be slow down even be hung for a while."
|
||||||
"New connections will be refused"
|
>>
|
||||||
>>})}
|
}
|
||||||
, {message_routing, sc(ref(limiter_opts),
|
)},
|
||||||
#{description =>
|
{connection,
|
||||||
<<"The message_routing limiter.<br>"
|
sc(
|
||||||
"This is used to limite the deliver rate for this EMQX node"
|
ref(limiter_opts),
|
||||||
"If the this limiter limit is reached,"
|
#{
|
||||||
"New publish will be refused"
|
description =>
|
||||||
>>
|
<<
|
||||||
})}
|
"The connection limiter.<br>"
|
||||||
, {batch, sc(ref(limiter_opts),
|
"This is used to limit the connection rate for this EMQX node"
|
||||||
#{description => <<"The batch limiter.<br>"
|
"If the this limiter limit is reached,"
|
||||||
"This is used for EMQX internal batch operation"
|
"New connections will be refused"
|
||||||
"e.g. limite the retainer's deliver rate"
|
>>
|
||||||
>>
|
}
|
||||||
})}
|
)},
|
||||||
|
{message_routing,
|
||||||
|
sc(
|
||||||
|
ref(limiter_opts),
|
||||||
|
#{
|
||||||
|
description =>
|
||||||
|
<<
|
||||||
|
"The message_routing limiter.<br>"
|
||||||
|
"This is used to limite the deliver rate for this EMQX node"
|
||||||
|
"If the this limiter limit is reached,"
|
||||||
|
"New publish will be refused"
|
||||||
|
>>
|
||||||
|
}
|
||||||
|
)},
|
||||||
|
{batch,
|
||||||
|
sc(
|
||||||
|
ref(limiter_opts),
|
||||||
|
#{
|
||||||
|
description => <<
|
||||||
|
"The batch limiter.<br>"
|
||||||
|
"This is used for EMQX internal batch operation"
|
||||||
|
"e.g. limite the retainer's deliver rate"
|
||||||
|
>>
|
||||||
|
}
|
||||||
|
)}
|
||||||
];
|
];
|
||||||
|
|
||||||
fields(limiter_opts) ->
|
fields(limiter_opts) ->
|
||||||
[ {rate, sc(rate(), #{default => "infinity", desc => "The rate"})}
|
[
|
||||||
, {burst, sc(burst_rate(),
|
{rate, sc(rate(), #{default => "infinity", desc => "The rate"})},
|
||||||
#{default => "0/0s",
|
{burst,
|
||||||
desc => "The burst, This value is based on rate.<br/>
|
sc(
|
||||||
This value + rate = the maximum limit that can be achieved when limiter burst."
|
burst_rate(),
|
||||||
})}
|
#{
|
||||||
, {bucket, sc(map("bucket name", ref(bucket_opts)), #{desc => "Buckets config"})}
|
default => "0/0s",
|
||||||
|
desc =>
|
||||||
|
"The burst, This value is based on rate.<br/>\n"
|
||||||
|
" This value + rate = the maximum limit that can be achieved when limiter burst."
|
||||||
|
}
|
||||||
|
)},
|
||||||
|
{bucket, sc(map("bucket name", ref(bucket_opts)), #{desc => "Buckets config"})}
|
||||||
];
|
];
|
||||||
|
|
||||||
fields(bucket_opts) ->
|
fields(bucket_opts) ->
|
||||||
[ {rate, sc(rate(), #{desc => "Rate for this bucket."})}
|
[
|
||||||
, {capacity, sc(capacity(), #{desc => "The maximum number of tokens for this bucket."})}
|
{rate, sc(rate(), #{desc => "Rate for this bucket."})},
|
||||||
, {initial, sc(initial(), #{default => "0",
|
{capacity, sc(capacity(), #{desc => "The maximum number of tokens for this bucket."})},
|
||||||
desc => "The initial number of tokens for this bucket."})}
|
{initial,
|
||||||
, {per_client, sc(ref(client_bucket),
|
sc(initial(), #{
|
||||||
#{default => #{},
|
default => "0",
|
||||||
desc => "The rate limit for each user of the bucket,"
|
desc => "The initial number of tokens for this bucket."
|
||||||
|
})},
|
||||||
|
{per_client,
|
||||||
|
sc(
|
||||||
|
ref(client_bucket),
|
||||||
|
#{
|
||||||
|
default => #{},
|
||||||
|
desc =>
|
||||||
|
"The rate limit for each user of the bucket,"
|
||||||
" this field is not required"
|
" this field is not required"
|
||||||
})}
|
}
|
||||||
|
)}
|
||||||
];
|
];
|
||||||
|
|
||||||
fields(client_bucket) ->
|
fields(client_bucket) ->
|
||||||
[ {rate, sc(rate(), #{default => "infinity", desc => "Rate for this bucket."})}
|
[
|
||||||
, {initial, sc(initial(), #{default => "0", desc => "The initial number of tokens for this bucket."})}
|
{rate, sc(rate(), #{default => "infinity", desc => "Rate for this bucket."})},
|
||||||
%% low_water_mark add for emqx_channel and emqx_session
|
{initial,
|
||||||
%% both modules consume first and then check
|
sc(initial(), #{default => "0", desc => "The initial number of tokens for this bucket."})},
|
||||||
%% so we need to use this value to prevent excessive consumption
|
%% low_water_mark add for emqx_channel and emqx_session
|
||||||
%% (e.g, consumption from an empty bucket)
|
%% both modules consume first and then check
|
||||||
, {low_water_mark, sc(initial(),
|
%% so we need to use this value to prevent excessive consumption
|
||||||
#{desc => "If the remaining tokens are lower than this value,
|
%% (e.g, consumption from an empty bucket)
|
||||||
the check/consume will succeed, but it will be forced to wait for a short period of time.",
|
{low_water_mark,
|
||||||
default => "0"})}
|
sc(
|
||||||
, {capacity, sc(capacity(), #{desc => "The capacity of the token bucket.",
|
initial(),
|
||||||
default => "infinity"})}
|
#{
|
||||||
, {divisible, sc(boolean(),
|
desc =>
|
||||||
#{desc => "Is it possible to split the number of requested tokens?",
|
"If the remaining tokens are lower than this value,\n"
|
||||||
default => false})}
|
"the check/consume will succeed, but it will be forced to wait for a short period of time.",
|
||||||
, {max_retry_time, sc(emqx_schema:duration(),
|
default => "0"
|
||||||
#{ desc => "The maximum retry time when acquire failed."
|
}
|
||||||
, default => "10s"})}
|
)},
|
||||||
, {failure_strategy, sc(failure_strategy(),
|
{capacity,
|
||||||
#{ desc => "The strategy when all the retries failed."
|
sc(capacity(), #{
|
||||||
, default => force})}
|
desc => "The capacity of the token bucket.",
|
||||||
|
default => "infinity"
|
||||||
|
})},
|
||||||
|
{divisible,
|
||||||
|
sc(
|
||||||
|
boolean(),
|
||||||
|
#{
|
||||||
|
desc => "Is it possible to split the number of requested tokens?",
|
||||||
|
default => false
|
||||||
|
}
|
||||||
|
)},
|
||||||
|
{max_retry_time,
|
||||||
|
sc(
|
||||||
|
emqx_schema:duration(),
|
||||||
|
#{
|
||||||
|
desc => "The maximum retry time when acquire failed.",
|
||||||
|
default => "10s"
|
||||||
|
}
|
||||||
|
)},
|
||||||
|
{failure_strategy,
|
||||||
|
sc(
|
||||||
|
failure_strategy(),
|
||||||
|
#{
|
||||||
|
desc => "The strategy when all the retries failed.",
|
||||||
|
default => force
|
||||||
|
}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
desc(limiter) ->
|
desc(limiter) ->
|
||||||
|
|
@ -184,33 +268,40 @@ to_rate(Str, CanInfinity, CanZero) ->
|
||||||
case Tokens of
|
case Tokens of
|
||||||
["infinity"] when CanInfinity ->
|
["infinity"] when CanInfinity ->
|
||||||
{ok, infinity};
|
{ok, infinity};
|
||||||
[QuotaStr] -> %% if time unit is 1s, it can be omitted
|
%% if time unit is 1s, it can be omitted
|
||||||
|
[QuotaStr] ->
|
||||||
{ok, Val} = to_capacity(QuotaStr),
|
{ok, Val} = to_capacity(QuotaStr),
|
||||||
check_capacity(Str, Val, CanZero,
|
check_capacity(
|
||||||
fun(Quota) ->
|
Str,
|
||||||
{ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
|
Val,
|
||||||
end);
|
CanZero,
|
||||||
|
fun(Quota) ->
|
||||||
|
{ok, Quota * minimum_period() / ?UNIT_TIME_IN_MS}
|
||||||
|
end
|
||||||
|
);
|
||||||
[QuotaStr, Interval] ->
|
[QuotaStr, Interval] ->
|
||||||
{ok, Val} = to_capacity(QuotaStr),
|
{ok, Val} = to_capacity(QuotaStr),
|
||||||
check_capacity(Str, Val, CanZero,
|
check_capacity(
|
||||||
fun(Quota) ->
|
Str,
|
||||||
case emqx_schema:to_duration_ms(Interval) of
|
Val,
|
||||||
{ok, Ms} when Ms > 0 ->
|
CanZero,
|
||||||
{ok, Quota * minimum_period() / Ms};
|
fun(Quota) ->
|
||||||
_ ->
|
case emqx_schema:to_duration_ms(Interval) of
|
||||||
{error, Str}
|
{ok, Ms} when Ms > 0 ->
|
||||||
end
|
{ok, Quota * minimum_period() / Ms};
|
||||||
end);
|
_ ->
|
||||||
|
{error, Str}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
);
|
||||||
_ ->
|
_ ->
|
||||||
{error, Str}
|
{error, Str}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
check_capacity(_Str, 0, true, _Cont) ->
|
check_capacity(_Str, 0, true, _Cont) ->
|
||||||
{ok, 0};
|
{ok, 0};
|
||||||
|
|
||||||
check_capacity(Str, 0, false, _Cont) ->
|
check_capacity(Str, 0, false, _Cont) ->
|
||||||
{error, Str};
|
{error, Str};
|
||||||
|
|
||||||
check_capacity(_Str, Quota, _CanZero, Cont) ->
|
check_capacity(_Str, Quota, _CanZero, Cont) ->
|
||||||
Cont(Quota).
|
Cont(Quota).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,34 +30,53 @@
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
-export([
|
||||||
terminate/2, code_change/3, format_status/2]).
|
init/1,
|
||||||
|
handle_call/3,
|
||||||
|
handle_cast/2,
|
||||||
|
handle_info/2,
|
||||||
|
terminate/2,
|
||||||
|
code_change/3,
|
||||||
|
format_status/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ start_link/1, connect/2, info/1
|
-export([
|
||||||
, name/1, get_initial_val/1, update_config/1
|
start_link/1,
|
||||||
]).
|
connect/2,
|
||||||
|
info/1,
|
||||||
|
name/1,
|
||||||
|
get_initial_val/1,
|
||||||
|
update_config/1
|
||||||
|
]).
|
||||||
|
|
||||||
-type root() :: #{ rate := rate() %% number of tokens generated per period
|
%% number of tokens generated per period
|
||||||
, burst := rate()
|
-type root() :: #{
|
||||||
, period := pos_integer() %% token generation interval(second)
|
rate := rate(),
|
||||||
, consumed := non_neg_integer()
|
burst := rate(),
|
||||||
}.
|
%% token generation interval(second)
|
||||||
|
period := pos_integer(),
|
||||||
|
consumed := non_neg_integer()
|
||||||
|
}.
|
||||||
|
|
||||||
-type bucket() :: #{ name := bucket_name()
|
-type bucket() :: #{
|
||||||
, rate := rate()
|
name := bucket_name(),
|
||||||
, obtained := non_neg_integer()
|
rate := rate(),
|
||||||
, correction := emqx_limiter_decimal:zero_or_float() %% token correction value
|
obtained := non_neg_integer(),
|
||||||
, capacity := capacity()
|
%% token correction value
|
||||||
, counter := undefined | counters:counters_ref()
|
correction := emqx_limiter_decimal:zero_or_float(),
|
||||||
, index := undefined | index()
|
capacity := capacity(),
|
||||||
}.
|
counter := undefined | counters:counters_ref(),
|
||||||
|
index := undefined | index()
|
||||||
|
}.
|
||||||
|
|
||||||
-type state() :: #{ type := limiter_type()
|
-type state() :: #{
|
||||||
, root := undefined | root()
|
type := limiter_type(),
|
||||||
, buckets := buckets()
|
root := undefined | root(),
|
||||||
, counter := undefined | counters:counters_ref() %% current counter to alloc
|
buckets := buckets(),
|
||||||
, index := index()
|
%% current counter to alloc
|
||||||
}.
|
counter := undefined | counters:counters_ref(),
|
||||||
|
index := index()
|
||||||
|
}.
|
||||||
|
|
||||||
-type buckets() :: #{bucket_name() => bucket()}.
|
-type buckets() :: #{bucket_name() => bucket()}.
|
||||||
-type limiter_type() :: emqx_limiter_schema:limiter_type().
|
-type limiter_type() :: emqx_limiter_schema:limiter_type().
|
||||||
|
|
@ -69,7 +88,8 @@
|
||||||
-type index() :: pos_integer().
|
-type index() :: pos_integer().
|
||||||
|
|
||||||
-define(CALL(Type), gen_server:call(name(Type), ?FUNCTION_NAME)).
|
-define(CALL(Type), gen_server:call(name(Type), ?FUNCTION_NAME)).
|
||||||
-define(OVERLOAD_MIN_ALLOC, 0.3). %% minimum coefficient for overloaded limiter
|
%% minimum coefficient for overloaded limiter
|
||||||
|
-define(OVERLOAD_MIN_ALLOC, 0.3).
|
||||||
-define(CURRYING(X, F2), fun(Y) -> F2(X, Y) end).
|
-define(CURRYING(X, F2), fun(Y) -> F2(X, Y) end).
|
||||||
|
|
||||||
-export_type([index/0]).
|
-export_type([index/0]).
|
||||||
|
|
@ -80,29 +100,33 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% API
|
%% API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec connect(limiter_type(),
|
-spec connect(
|
||||||
bucket_name() | #{limiter_type() => bucket_name() | undefined}) ->
|
limiter_type(),
|
||||||
emqx_htb_limiter:limiter().
|
bucket_name() | #{limiter_type() => bucket_name() | undefined}
|
||||||
|
) ->
|
||||||
|
emqx_htb_limiter:limiter().
|
||||||
%% If no bucket path is set in config, there will be no limit
|
%% If no bucket path is set in config, there will be no limit
|
||||||
connect(_Type, undefined) ->
|
connect(_Type, undefined) ->
|
||||||
emqx_htb_limiter:make_infinity_limiter();
|
emqx_htb_limiter:make_infinity_limiter();
|
||||||
|
|
||||||
connect(Type, BucketName) when is_atom(BucketName) ->
|
connect(Type, BucketName) when is_atom(BucketName) ->
|
||||||
CfgPath = emqx_limiter_schema:get_bucket_cfg_path(Type, BucketName),
|
CfgPath = emqx_limiter_schema:get_bucket_cfg_path(Type, BucketName),
|
||||||
case emqx:get_config(CfgPath, undefined) of
|
case emqx:get_config(CfgPath, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
?SLOG(error, #{msg => "bucket_config_not_found", path => CfgPath}),
|
?SLOG(error, #{msg => "bucket_config_not_found", path => CfgPath}),
|
||||||
throw("bucket's config not found");
|
throw("bucket's config not found");
|
||||||
#{rate := AggrRate,
|
#{
|
||||||
capacity := AggrSize,
|
rate := AggrRate,
|
||||||
per_client := #{rate := CliRate, capacity := CliSize} = Cfg} ->
|
capacity := AggrSize,
|
||||||
|
per_client := #{rate := CliRate, capacity := CliSize} = Cfg
|
||||||
|
} ->
|
||||||
case emqx_limiter_manager:find_bucket(Type, BucketName) of
|
case emqx_limiter_manager:find_bucket(Type, BucketName) of
|
||||||
{ok, Bucket} ->
|
{ok, Bucket} ->
|
||||||
if CliRate < AggrRate orelse CliSize < AggrSize ->
|
if
|
||||||
|
CliRate < AggrRate orelse CliSize < AggrSize ->
|
||||||
emqx_htb_limiter:make_token_bucket_limiter(Cfg, Bucket);
|
emqx_htb_limiter:make_token_bucket_limiter(Cfg, Bucket);
|
||||||
Bucket =:= infinity ->
|
Bucket =:= infinity ->
|
||||||
emqx_htb_limiter:make_infinity_limiter();
|
emqx_htb_limiter:make_infinity_limiter();
|
||||||
true ->
|
true ->
|
||||||
emqx_htb_limiter:make_ref_limiter(Cfg, Bucket)
|
emqx_htb_limiter:make_ref_limiter(Cfg, Bucket)
|
||||||
end;
|
end;
|
||||||
undefined ->
|
undefined ->
|
||||||
|
|
@ -110,7 +134,6 @@ connect(Type, BucketName) when is_atom(BucketName) ->
|
||||||
throw("invalid bucket")
|
throw("invalid bucket")
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
connect(Type, Paths) ->
|
connect(Type, Paths) ->
|
||||||
connect(Type, maps:get(Type, Paths, undefined)).
|
connect(Type, maps:get(Type, Paths, undefined)).
|
||||||
|
|
||||||
|
|
@ -135,7 +158,6 @@ update_config(Type) ->
|
||||||
start_link(Type) ->
|
start_link(Type) ->
|
||||||
gen_server:start_link({local, name(Type)}, ?MODULE, [Type], []).
|
gen_server:start_link({local, name(Type)}, ?MODULE, [Type], []).
|
||||||
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%%% gen_server callbacks
|
%%% gen_server callbacks
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -146,11 +168,12 @@ start_link(Type) ->
|
||||||
%% Initializes the server
|
%% Initializes the server
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec init(Args :: term()) -> {ok, State :: term()} |
|
-spec init(Args :: term()) ->
|
||||||
{ok, State :: term(), Timeout :: timeout()} |
|
{ok, State :: term()}
|
||||||
{ok, State :: term(), hibernate} |
|
| {ok, State :: term(), Timeout :: timeout()}
|
||||||
{stop, Reason :: term()} |
|
| {ok, State :: term(), hibernate}
|
||||||
ignore.
|
| {stop, Reason :: term()}
|
||||||
|
| ignore.
|
||||||
init([Type]) ->
|
init([Type]) ->
|
||||||
State = init_tree(Type),
|
State = init_tree(Type),
|
||||||
#{root := #{period := Perido}} = State,
|
#{root := #{period := Perido}} = State,
|
||||||
|
|
@ -164,21 +187,19 @@ init([Type]) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec handle_call(Request :: term(), From :: {pid(), term()}, State :: term()) ->
|
-spec handle_call(Request :: term(), From :: {pid(), term()}, State :: term()) ->
|
||||||
{reply, Reply :: term(), NewState :: term()} |
|
{reply, Reply :: term(), NewState :: term()}
|
||||||
{reply, Reply :: term(), NewState :: term(), Timeout :: timeout()} |
|
| {reply, Reply :: term(), NewState :: term(), Timeout :: timeout()}
|
||||||
{reply, Reply :: term(), NewState :: term(), hibernate} |
|
| {reply, Reply :: term(), NewState :: term(), hibernate}
|
||||||
{noreply, NewState :: term()} |
|
| {noreply, NewState :: term()}
|
||||||
{noreply, NewState :: term(), Timeout :: timeout()} |
|
| {noreply, NewState :: term(), Timeout :: timeout()}
|
||||||
{noreply, NewState :: term(), hibernate} |
|
| {noreply, NewState :: term(), hibernate}
|
||||||
{stop, Reason :: term(), Reply :: term(), NewState :: term()} |
|
| {stop, Reason :: term(), Reply :: term(), NewState :: term()}
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
| {stop, Reason :: term(), NewState :: term()}.
|
||||||
handle_call(info, _From, State) ->
|
handle_call(info, _From, State) ->
|
||||||
{reply, State, State};
|
{reply, State, State};
|
||||||
|
|
||||||
handle_call(update_config, _From, #{type := Type}) ->
|
handle_call(update_config, _From, #{type := Type}) ->
|
||||||
NewState = init_tree(Type),
|
NewState = init_tree(Type),
|
||||||
{reply, ok, NewState};
|
{reply, ok, NewState};
|
||||||
|
|
||||||
handle_call(Req, _From, State) ->
|
handle_call(Req, _From, State) ->
|
||||||
?SLOG(error, #{msg => "unexpected_call", call => Req}),
|
?SLOG(error, #{msg => "unexpected_call", call => Req}),
|
||||||
{reply, ignored, State}.
|
{reply, ignored, State}.
|
||||||
|
|
@ -190,10 +211,10 @@ handle_call(Req, _From, State) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec handle_cast(Request :: term(), State :: term()) ->
|
-spec handle_cast(Request :: term(), State :: term()) ->
|
||||||
{noreply, NewState :: term()} |
|
{noreply, NewState :: term()}
|
||||||
{noreply, NewState :: term(), Timeout :: timeout()} |
|
| {noreply, NewState :: term(), Timeout :: timeout()}
|
||||||
{noreply, NewState :: term(), hibernate} |
|
| {noreply, NewState :: term(), hibernate}
|
||||||
{stop, Reason :: term(), NewState :: term()}.
|
| {stop, Reason :: term(), NewState :: term()}.
|
||||||
handle_cast(Req, State) ->
|
handle_cast(Req, State) ->
|
||||||
?SLOG(error, #{msg => "unexpected_cast", cast => Req}),
|
?SLOG(error, #{msg => "unexpected_cast", cast => Req}),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
@ -205,13 +226,12 @@ handle_cast(Req, State) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec handle_info(Info :: timeout() | term(), State :: term()) ->
|
-spec handle_info(Info :: timeout() | term(), State :: term()) ->
|
||||||
{noreply, NewState :: term()} |
|
{noreply, NewState :: term()}
|
||||||
{noreply, NewState :: term(), Timeout :: timeout()} |
|
| {noreply, NewState :: term(), Timeout :: timeout()}
|
||||||
{noreply, NewState :: term(), hibernate} |
|
| {noreply, NewState :: term(), hibernate}
|
||||||
{stop, Reason :: normal | term(), NewState :: term()}.
|
| {stop, Reason :: normal | term(), NewState :: term()}.
|
||||||
handle_info(oscillate, State) ->
|
handle_info(oscillate, State) ->
|
||||||
{noreply, oscillation(State)};
|
{noreply, oscillation(State)};
|
||||||
|
|
||||||
handle_info(Info, State) ->
|
handle_info(Info, State) ->
|
||||||
?SLOG(error, #{msg => "unexpected_info", info => Info}),
|
?SLOG(error, #{msg => "unexpected_info", info => Info}),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
@ -225,8 +245,10 @@ handle_info(Info, State) ->
|
||||||
%% with Reason. The return value is ignored.
|
%% with Reason. The return value is ignored.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec terminate(Reason :: normal | shutdown | {shutdown, term()} | term(),
|
-spec terminate(
|
||||||
State :: term()) -> any().
|
Reason :: normal | shutdown | {shutdown, term()} | term(),
|
||||||
|
State :: term()
|
||||||
|
) -> any().
|
||||||
terminate(_Reason, _State) ->
|
terminate(_Reason, _State) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
@ -236,10 +258,13 @@ terminate(_Reason, _State) ->
|
||||||
%% Convert process state when code is changed
|
%% Convert process state when code is changed
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec code_change(OldVsn :: term() | {down, term()},
|
-spec code_change(
|
||||||
State :: term(),
|
OldVsn :: term() | {down, term()},
|
||||||
Extra :: term()) -> {ok, NewState :: term()} |
|
State :: term(),
|
||||||
{error, Reason :: term()}.
|
Extra :: term()
|
||||||
|
) ->
|
||||||
|
{ok, NewState :: term()}
|
||||||
|
| {error, Reason :: term()}.
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
|
|
@ -251,8 +276,10 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%% or when it appears in termination error logs.
|
%% or when it appears in termination error logs.
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec format_status(Opt :: normal | terminate,
|
-spec format_status(
|
||||||
Status :: list()) -> Status :: term().
|
Opt :: normal | terminate,
|
||||||
|
Status :: list()
|
||||||
|
) -> Status :: term().
|
||||||
format_status(_Opt, Status) ->
|
format_status(_Opt, Status) ->
|
||||||
Status.
|
Status.
|
||||||
|
|
||||||
|
|
@ -264,40 +291,54 @@ oscillate(Interval) ->
|
||||||
|
|
||||||
%% @doc generate tokens, and then spread to leaf nodes
|
%% @doc generate tokens, and then spread to leaf nodes
|
||||||
-spec oscillation(state()) -> state().
|
-spec oscillation(state()) -> state().
|
||||||
oscillation(#{root := #{rate := Flow,
|
oscillation(
|
||||||
period := Interval,
|
#{
|
||||||
consumed := Consumed} = Root,
|
root := #{
|
||||||
buckets := Buckets} = State) ->
|
rate := Flow,
|
||||||
|
period := Interval,
|
||||||
|
consumed := Consumed
|
||||||
|
} = Root,
|
||||||
|
buckets := Buckets
|
||||||
|
} = State
|
||||||
|
) ->
|
||||||
oscillate(Interval),
|
oscillate(Interval),
|
||||||
Ordereds = get_ordered_buckets(Buckets),
|
Ordereds = get_ordered_buckets(Buckets),
|
||||||
{Alloced, Buckets2} = transverse(Ordereds, Flow, 0, Buckets),
|
{Alloced, Buckets2} = transverse(Ordereds, Flow, 0, Buckets),
|
||||||
maybe_burst(State#{buckets := Buckets2,
|
maybe_burst(State#{
|
||||||
root := Root#{consumed := Consumed + Alloced}}).
|
buckets := Buckets2,
|
||||||
|
root := Root#{consumed := Consumed + Alloced}
|
||||||
|
}).
|
||||||
|
|
||||||
%% @doc horizontal spread
|
%% @doc horizontal spread
|
||||||
-spec transverse(list(bucket()),
|
-spec transverse(
|
||||||
flow(),
|
list(bucket()),
|
||||||
non_neg_integer(),
|
flow(),
|
||||||
buckets()) -> {non_neg_integer(), buckets()}.
|
non_neg_integer(),
|
||||||
|
buckets()
|
||||||
|
) -> {non_neg_integer(), buckets()}.
|
||||||
transverse([H | T], InFlow, Alloced, Buckets) when InFlow > 0 ->
|
transverse([H | T], InFlow, Alloced, Buckets) when InFlow > 0 ->
|
||||||
{BucketAlloced, Buckets2} = longitudinal(H, InFlow, Buckets),
|
{BucketAlloced, Buckets2} = longitudinal(H, InFlow, Buckets),
|
||||||
InFlow2 = sub(InFlow, BucketAlloced),
|
InFlow2 = sub(InFlow, BucketAlloced),
|
||||||
Alloced2 = Alloced + BucketAlloced,
|
Alloced2 = Alloced + BucketAlloced,
|
||||||
transverse(T, InFlow2, Alloced2, Buckets2);
|
transverse(T, InFlow2, Alloced2, Buckets2);
|
||||||
|
|
||||||
transverse(_, _, Alloced, Buckets) ->
|
transverse(_, _, Alloced, Buckets) ->
|
||||||
{Alloced, Buckets}.
|
{Alloced, Buckets}.
|
||||||
|
|
||||||
%% @doc vertical spread
|
%% @doc vertical spread
|
||||||
-spec longitudinal(bucket(), flow(), buckets()) ->
|
-spec longitudinal(bucket(), flow(), buckets()) ->
|
||||||
{non_neg_integer(), buckets()}.
|
{non_neg_integer(), buckets()}.
|
||||||
longitudinal(#{name := Name,
|
longitudinal(
|
||||||
rate := Rate,
|
#{
|
||||||
capacity := Capacity,
|
name := Name,
|
||||||
counter := Counter,
|
rate := Rate,
|
||||||
index := Index,
|
capacity := Capacity,
|
||||||
obtained := Obtained} = Bucket,
|
counter := Counter,
|
||||||
InFlow, Buckets) when Counter =/= undefined ->
|
index := Index,
|
||||||
|
obtained := Obtained
|
||||||
|
} = Bucket,
|
||||||
|
InFlow,
|
||||||
|
Buckets
|
||||||
|
) when Counter =/= undefined ->
|
||||||
Flow = erlang:min(InFlow, Rate),
|
Flow = erlang:min(InFlow, Rate),
|
||||||
|
|
||||||
ShouldAlloc =
|
ShouldAlloc =
|
||||||
|
|
@ -305,8 +346,10 @@ longitudinal(#{name := Name,
|
||||||
Tokens when Tokens < 0 ->
|
Tokens when Tokens < 0 ->
|
||||||
%% toknes's value mayb be a negative value(stolen from the future)
|
%% toknes's value mayb be a negative value(stolen from the future)
|
||||||
%% because ∃ x. add(Capacity, x) < 0, so here we must compare with minimum value
|
%% because ∃ x. add(Capacity, x) < 0, so here we must compare with minimum value
|
||||||
erlang:max(add(Capacity, Tokens),
|
erlang:max(
|
||||||
mul(Capacity, ?OVERLOAD_MIN_ALLOC));
|
add(Capacity, Tokens),
|
||||||
|
mul(Capacity, ?OVERLOAD_MIN_ALLOC)
|
||||||
|
);
|
||||||
Tokens ->
|
Tokens ->
|
||||||
%% is it possible that Tokens > Capacity ???
|
%% is it possible that Tokens > Capacity ???
|
||||||
erlang:max(sub(Capacity, Tokens), 0)
|
erlang:max(sub(Capacity, Tokens), 0)
|
||||||
|
|
@ -320,12 +363,10 @@ longitudinal(#{name := Name,
|
||||||
{Inc, Bucket2} = emqx_limiter_correction:add(Available, Bucket),
|
{Inc, Bucket2} = emqx_limiter_correction:add(Available, Bucket),
|
||||||
counters:add(Counter, Index, Inc),
|
counters:add(Counter, Index, Inc),
|
||||||
|
|
||||||
{Inc,
|
{Inc, Buckets#{Name := Bucket2#{obtained := Obtained + Inc}}};
|
||||||
Buckets#{Name := Bucket2#{obtained := Obtained + Inc}}};
|
|
||||||
_ ->
|
_ ->
|
||||||
{0, Buckets}
|
{0, Buckets}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
longitudinal(_, _, Buckets) ->
|
longitudinal(_, _, Buckets) ->
|
||||||
{0, Buckets}.
|
{0, Buckets}.
|
||||||
|
|
||||||
|
|
@ -333,95 +374,111 @@ longitudinal(_, _, Buckets) ->
|
||||||
get_ordered_buckets(Buckets) when is_map(Buckets) ->
|
get_ordered_buckets(Buckets) when is_map(Buckets) ->
|
||||||
BucketList = maps:values(Buckets),
|
BucketList = maps:values(Buckets),
|
||||||
get_ordered_buckets(BucketList);
|
get_ordered_buckets(BucketList);
|
||||||
|
|
||||||
get_ordered_buckets(Buckets) ->
|
get_ordered_buckets(Buckets) ->
|
||||||
%% sort by obtained, avoid node goes hungry
|
%% sort by obtained, avoid node goes hungry
|
||||||
lists:sort(fun(#{obtained := A}, #{obtained := B}) ->
|
lists:sort(
|
||||||
A < B
|
fun(#{obtained := A}, #{obtained := B}) ->
|
||||||
end,
|
A < B
|
||||||
Buckets).
|
end,
|
||||||
|
Buckets
|
||||||
|
).
|
||||||
|
|
||||||
-spec maybe_burst(state()) -> state().
|
-spec maybe_burst(state()) -> state().
|
||||||
maybe_burst(#{buckets := Buckets,
|
maybe_burst(
|
||||||
root := #{burst := Burst}} = State) when Burst > 0 ->
|
#{
|
||||||
Fold = fun(_Name, #{counter := Cnt, index := Idx} = Bucket, Acc) when Cnt =/= undefined ->
|
buckets := Buckets,
|
||||||
case counters:get(Cnt, Idx) > 0 of
|
root := #{burst := Burst}
|
||||||
true ->
|
} = State
|
||||||
Acc;
|
) when Burst > 0 ->
|
||||||
false ->
|
Fold = fun
|
||||||
[Bucket | Acc]
|
(_Name, #{counter := Cnt, index := Idx} = Bucket, Acc) when Cnt =/= undefined ->
|
||||||
end;
|
case counters:get(Cnt, Idx) > 0 of
|
||||||
(_Name, _Bucket, Acc) ->
|
true ->
|
||||||
Acc
|
Acc;
|
||||||
end,
|
false ->
|
||||||
|
[Bucket | Acc]
|
||||||
|
end;
|
||||||
|
(_Name, _Bucket, Acc) ->
|
||||||
|
Acc
|
||||||
|
end,
|
||||||
|
|
||||||
Empties = maps:fold(Fold, [], Buckets),
|
Empties = maps:fold(Fold, [], Buckets),
|
||||||
dispatch_burst(Empties, Burst, State);
|
dispatch_burst(Empties, Burst, State);
|
||||||
|
|
||||||
maybe_burst(State) ->
|
maybe_burst(State) ->
|
||||||
State.
|
State.
|
||||||
|
|
||||||
-spec dispatch_burst(list(bucket()), non_neg_integer(), state()) -> state().
|
-spec dispatch_burst(list(bucket()), non_neg_integer(), state()) -> state().
|
||||||
dispatch_burst([], _, State) ->
|
dispatch_burst([], _, State) ->
|
||||||
State;
|
State;
|
||||||
|
dispatch_burst(
|
||||||
dispatch_burst(Empties, InFlow,
|
Empties,
|
||||||
#{root := #{consumed := Consumed} = Root, buckets := Buckets} = State) ->
|
InFlow,
|
||||||
|
#{root := #{consumed := Consumed} = Root, buckets := Buckets} = State
|
||||||
|
) ->
|
||||||
EachFlow = InFlow / erlang:length(Empties),
|
EachFlow = InFlow / erlang:length(Empties),
|
||||||
{Alloced, Buckets2} = dispatch_burst_to_buckets(Empties, EachFlow, 0, Buckets),
|
{Alloced, Buckets2} = dispatch_burst_to_buckets(Empties, EachFlow, 0, Buckets),
|
||||||
State#{root := Root#{consumed := Consumed + Alloced}, buckets := Buckets2}.
|
State#{root := Root#{consumed := Consumed + Alloced}, buckets := Buckets2}.
|
||||||
|
|
||||||
-spec dispatch_burst_to_buckets(list(bucket()),
|
-spec dispatch_burst_to_buckets(
|
||||||
float(),
|
list(bucket()),
|
||||||
non_neg_integer(), buckets()) -> {non_neg_integer(), buckets()}.
|
float(),
|
||||||
|
non_neg_integer(),
|
||||||
|
buckets()
|
||||||
|
) -> {non_neg_integer(), buckets()}.
|
||||||
dispatch_burst_to_buckets([Bucket | T], InFlow, Alloced, Buckets) ->
|
dispatch_burst_to_buckets([Bucket | T], InFlow, Alloced, Buckets) ->
|
||||||
#{name := Name,
|
#{
|
||||||
counter := Counter,
|
name := Name,
|
||||||
index := Index,
|
counter := Counter,
|
||||||
obtained := Obtained} = Bucket,
|
index := Index,
|
||||||
|
obtained := Obtained
|
||||||
|
} = Bucket,
|
||||||
{Inc, Bucket2} = emqx_limiter_correction:add(InFlow, Bucket),
|
{Inc, Bucket2} = emqx_limiter_correction:add(InFlow, Bucket),
|
||||||
|
|
||||||
counters:add(Counter, Index, Inc),
|
counters:add(Counter, Index, Inc),
|
||||||
|
|
||||||
Buckets2 = Buckets#{Name := Bucket2#{obtained := Obtained + Inc}},
|
Buckets2 = Buckets#{Name := Bucket2#{obtained := Obtained + Inc}},
|
||||||
dispatch_burst_to_buckets(T, InFlow, Alloced + Inc, Buckets2);
|
dispatch_burst_to_buckets(T, InFlow, Alloced + Inc, Buckets2);
|
||||||
|
|
||||||
dispatch_burst_to_buckets([], _, Alloced, Buckets) ->
|
dispatch_burst_to_buckets([], _, Alloced, Buckets) ->
|
||||||
{Alloced, Buckets}.
|
{Alloced, Buckets}.
|
||||||
|
|
||||||
-spec init_tree(emqx_limiter_schema:limiter_type()) -> state().
|
-spec init_tree(emqx_limiter_schema:limiter_type()) -> state().
|
||||||
init_tree(Type) ->
|
init_tree(Type) ->
|
||||||
State = #{ type => Type
|
State = #{
|
||||||
, root => undefined
|
type => Type,
|
||||||
, counter => undefined
|
root => undefined,
|
||||||
, index => 1
|
counter => undefined,
|
||||||
, buckets => #{}
|
index => 1,
|
||||||
},
|
buckets => #{}
|
||||||
|
},
|
||||||
|
|
||||||
#{bucket := Buckets} = Cfg = emqx:get_config([limiter, Type]),
|
#{bucket := Buckets} = Cfg = emqx:get_config([limiter, Type]),
|
||||||
{Factor, Root} = make_root(Cfg),
|
{Factor, Root} = make_root(Cfg),
|
||||||
{CounterNum, DelayBuckets} = make_bucket(maps:to_list(Buckets), Type, Cfg, Factor, 1, []),
|
{CounterNum, DelayBuckets} = make_bucket(maps:to_list(Buckets), Type, Cfg, Factor, 1, []),
|
||||||
|
|
||||||
State2 = State#{root := Root,
|
State2 = State#{
|
||||||
counter := counters:new(CounterNum, [write_concurrency])
|
root := Root,
|
||||||
},
|
counter := counters:new(CounterNum, [write_concurrency])
|
||||||
|
},
|
||||||
|
|
||||||
lists:foldl(fun(F, Acc) -> F(Acc) end, State2, DelayBuckets).
|
lists:foldl(fun(F, Acc) -> F(Acc) end, State2, DelayBuckets).
|
||||||
|
|
||||||
-spec make_root(hocons:confg()) -> {number(), root()}.
|
-spec make_root(hocons:confg()) -> {number(), root()}.
|
||||||
make_root(#{rate := Rate, burst := Burst}) when Rate >= 1 ->
|
make_root(#{rate := Rate, burst := Burst}) when Rate >= 1 ->
|
||||||
{1, #{rate => Rate,
|
{1, #{
|
||||||
burst => Burst,
|
rate => Rate,
|
||||||
period => emqx_limiter_schema:minimum_period(),
|
burst => Burst,
|
||||||
consumed => 0}};
|
period => emqx_limiter_schema:minimum_period(),
|
||||||
|
consumed => 0
|
||||||
|
}};
|
||||||
make_root(#{rate := Rate, burst := Burst}) ->
|
make_root(#{rate := Rate, burst := Burst}) ->
|
||||||
MiniPeriod = emqx_limiter_schema:minimum_period(),
|
MiniPeriod = emqx_limiter_schema:minimum_period(),
|
||||||
Factor = 1 / Rate,
|
Factor = 1 / Rate,
|
||||||
{Factor, #{rate => 1,
|
{Factor, #{
|
||||||
burst => Burst * Factor,
|
rate => 1,
|
||||||
period => erlang:floor(Factor * MiniPeriod),
|
burst => Burst * Factor,
|
||||||
consumed => 0}}.
|
period => erlang:floor(Factor * MiniPeriod),
|
||||||
|
consumed => 0
|
||||||
|
}}.
|
||||||
|
|
||||||
make_bucket([{Name, Conf} | T], Type, GlobalCfg, Factor, CounterNum, DelayBuckets) ->
|
make_bucket([{Name, Conf} | T], Type, GlobalCfg, Factor, CounterNum, DelayBuckets) ->
|
||||||
Path = emqx_limiter_manager:make_path(Type, Name),
|
Path = emqx_limiter_manager:make_path(Type, Name),
|
||||||
|
|
@ -433,48 +490,66 @@ make_bucket([{Name, Conf} | T], Type, GlobalCfg, Factor, CounterNum, DelayBucket
|
||||||
emqx_limiter_manager:insert_bucket(Path, Ref),
|
emqx_limiter_manager:insert_bucket(Path, Ref),
|
||||||
CounterNum2 = CounterNum,
|
CounterNum2 = CounterNum,
|
||||||
InitFun = fun(#{name := BucketName} = Bucket, #{buckets := Buckets} = State) ->
|
InitFun = fun(#{name := BucketName} = Bucket, #{buckets := Buckets} = State) ->
|
||||||
State#{buckets := Buckets#{BucketName => Bucket}}
|
State#{buckets := Buckets#{BucketName => Bucket}}
|
||||||
end;
|
end;
|
||||||
RawRate ->
|
RawRate ->
|
||||||
#{capacity := Capacity} = Conf,
|
#{capacity := Capacity} = Conf,
|
||||||
Initial = get_initial_val(Conf),
|
Initial = get_initial_val(Conf),
|
||||||
Rate = mul(RawRate, Factor),
|
Rate = mul(RawRate, Factor),
|
||||||
CounterNum2 = CounterNum + 1,
|
CounterNum2 = CounterNum + 1,
|
||||||
InitFun = fun(#{name := BucketName} = Bucket, #{buckets := Buckets} = State) ->
|
InitFun = fun(#{name := BucketName} = Bucket, #{buckets := Buckets} = State) ->
|
||||||
{Counter, Idx, State2} = alloc_counter(Path, RawRate, Initial, State),
|
{Counter, Idx, State2} = alloc_counter(Path, RawRate, Initial, State),
|
||||||
Bucket2 = Bucket#{counter := Counter, index := Idx},
|
Bucket2 = Bucket#{counter := Counter, index := Idx},
|
||||||
State2#{buckets := Buckets#{BucketName => Bucket2}}
|
State2#{buckets := Buckets#{BucketName => Bucket2}}
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
|
|
||||||
Bucket = #{ name => Name
|
Bucket = #{
|
||||||
, rate => Rate
|
name => Name,
|
||||||
, obtained => 0
|
rate => Rate,
|
||||||
, correction => 0
|
obtained => 0,
|
||||||
, capacity => Capacity
|
correction => 0,
|
||||||
, counter => undefined
|
capacity => Capacity,
|
||||||
, index => undefined},
|
counter => undefined,
|
||||||
|
index => undefined
|
||||||
|
},
|
||||||
|
|
||||||
DelayInit = ?CURRYING(Bucket, InitFun),
|
DelayInit = ?CURRYING(Bucket, InitFun),
|
||||||
|
|
||||||
make_bucket(T,
|
make_bucket(
|
||||||
Type, GlobalCfg, Factor, CounterNum2, [DelayInit | DelayBuckets]);
|
T,
|
||||||
|
Type,
|
||||||
|
GlobalCfg,
|
||||||
|
Factor,
|
||||||
|
CounterNum2,
|
||||||
|
[DelayInit | DelayBuckets]
|
||||||
|
);
|
||||||
make_bucket([], _Type, _Global, _Factor, CounterNum, DelayBuckets) ->
|
make_bucket([], _Type, _Global, _Factor, CounterNum, DelayBuckets) ->
|
||||||
{CounterNum, DelayBuckets}.
|
{CounterNum, DelayBuckets}.
|
||||||
|
|
||||||
-spec alloc_counter(emqx_limiter_manager:path(), rate(), capacity(), state()) ->
|
-spec alloc_counter(emqx_limiter_manager:path(), rate(), capacity(), state()) ->
|
||||||
{counters:counters_ref(), pos_integer(), state()}.
|
{counters:counters_ref(), pos_integer(), state()}.
|
||||||
alloc_counter(Path, Rate, Initial,
|
alloc_counter(
|
||||||
#{counter := Counter, index := Index} = State) ->
|
Path,
|
||||||
|
Rate,
|
||||||
|
Initial,
|
||||||
|
#{counter := Counter, index := Index} = State
|
||||||
|
) ->
|
||||||
case emqx_limiter_manager:find_bucket(Path) of
|
case emqx_limiter_manager:find_bucket(Path) of
|
||||||
{ok, #{counter := ECounter,
|
{ok, #{
|
||||||
index := EIndex}} when ECounter =/= undefined ->
|
counter := ECounter,
|
||||||
|
index := EIndex
|
||||||
|
}} when ECounter =/= undefined ->
|
||||||
init_counter(Path, ECounter, EIndex, Rate, Initial, State);
|
init_counter(Path, ECounter, EIndex, Rate, Initial, State);
|
||||||
_ ->
|
_ ->
|
||||||
init_counter(Path, Counter, Index,
|
init_counter(
|
||||||
Rate, Initial, State#{index := Index + 1})
|
Path,
|
||||||
|
Counter,
|
||||||
|
Index,
|
||||||
|
Rate,
|
||||||
|
Initial,
|
||||||
|
State#{index := Index + 1}
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
init_counter(Path, Counter, Index, Rate, Initial, State) ->
|
init_counter(Path, Counter, Index, Rate, Initial, State) ->
|
||||||
|
|
@ -483,26 +558,29 @@ init_counter(Path, Counter, Index, Rate, Initial, State) ->
|
||||||
emqx_limiter_manager:insert_bucket(Path, Ref),
|
emqx_limiter_manager:insert_bucket(Path, Ref),
|
||||||
{Counter, Index, State}.
|
{Counter, Index, State}.
|
||||||
|
|
||||||
|
|
||||||
%% @doc find first limited node
|
%% @doc find first limited node
|
||||||
get_counter_rate(#{rate := Rate, capacity := Capacity}, _GlobalCfg)
|
get_counter_rate(#{rate := Rate, capacity := Capacity}, _GlobalCfg) when
|
||||||
when Rate =/= infinity orelse Capacity =/= infinity -> %% TODO maybe no need to check capacity
|
%% TODO maybe no need to check capacity
|
||||||
|
Rate =/= infinity orelse Capacity =/= infinity
|
||||||
|
->
|
||||||
Rate;
|
Rate;
|
||||||
|
get_counter_rate(_Cfg, #{rate := Rate}) ->
|
||||||
get_counter_rate(_Cfg, #{rate := Rate}) ->
|
|
||||||
Rate.
|
Rate.
|
||||||
|
|
||||||
-spec get_initial_val(hocons:config()) -> decimal().
|
-spec get_initial_val(hocons:config()) -> decimal().
|
||||||
get_initial_val(#{initial := Initial,
|
get_initial_val(#{
|
||||||
rate := Rate,
|
initial := Initial,
|
||||||
capacity := Capacity}) ->
|
rate := Rate,
|
||||||
|
capacity := Capacity
|
||||||
|
}) ->
|
||||||
%% initial will nevner be infinity(see the emqx_limiter_schema)
|
%% initial will nevner be infinity(see the emqx_limiter_schema)
|
||||||
if Initial > 0 ->
|
if
|
||||||
|
Initial > 0 ->
|
||||||
Initial;
|
Initial;
|
||||||
Rate =/= infinity ->
|
Rate =/= infinity ->
|
||||||
erlang:min(Rate, Capacity);
|
erlang:min(Rate, Capacity);
|
||||||
Capacity =/= infinity ->
|
Capacity =/= infinity ->
|
||||||
Capacity;
|
Capacity;
|
||||||
true ->
|
true ->
|
||||||
0
|
0
|
||||||
end.
|
end.
|
||||||
|
|
|
||||||
|
|
@ -33,11 +33,12 @@
|
||||||
%% Starts the supervisor
|
%% Starts the supervisor
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec start_link() -> {ok, Pid :: pid()} |
|
-spec start_link() ->
|
||||||
{error, {already_started, Pid :: pid()}} |
|
{ok, Pid :: pid()}
|
||||||
{error, {shutdown, term()}} |
|
| {error, {already_started, Pid :: pid()}}
|
||||||
{error, term()} |
|
| {error, {shutdown, term()}}
|
||||||
ignore.
|
| {error, term()}
|
||||||
|
| ignore.
|
||||||
start_link() ->
|
start_link() ->
|
||||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||||
|
|
||||||
|
|
@ -67,13 +68,14 @@ restart(Type) ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec init(Args :: term()) ->
|
-spec init(Args :: term()) ->
|
||||||
{ok, {SupFlags :: supervisor:sup_flags(),
|
{ok, {SupFlags :: supervisor:sup_flags(), [ChildSpec :: supervisor:child_spec()]}}
|
||||||
[ChildSpec :: supervisor:child_spec()]}} |
|
| ignore.
|
||||||
ignore.
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
SupFlags = #{strategy => one_for_one,
|
SupFlags = #{
|
||||||
intensity => 10,
|
strategy => one_for_one,
|
||||||
period => 3600},
|
intensity => 10,
|
||||||
|
period => 3600
|
||||||
|
},
|
||||||
|
|
||||||
{ok, {SupFlags, childs()}}.
|
{ok, {SupFlags, childs()}}.
|
||||||
|
|
||||||
|
|
@ -82,12 +84,14 @@ init([]) ->
|
||||||
%%--==================================================================
|
%%--==================================================================
|
||||||
make_child(Type) ->
|
make_child(Type) ->
|
||||||
Id = emqx_limiter_server:name(Type),
|
Id = emqx_limiter_server:name(Type),
|
||||||
#{id => Id,
|
#{
|
||||||
start => {emqx_limiter_server, start_link, [Type]},
|
id => Id,
|
||||||
restart => transient,
|
start => {emqx_limiter_server, start_link, [Type]},
|
||||||
shutdown => 5000,
|
restart => transient,
|
||||||
type => worker,
|
shutdown => 5000,
|
||||||
modules => [emqx_limiter_server]}.
|
type => worker,
|
||||||
|
modules => [emqx_limiter_server]
|
||||||
|
}.
|
||||||
|
|
||||||
childs() ->
|
childs() ->
|
||||||
Conf = emqx:get_config([limiter]),
|
Conf = emqx:get_config([limiter]),
|
||||||
|
|
|
||||||
|
|
@ -33,11 +33,12 @@
|
||||||
%% Starts the supervisor
|
%% Starts the supervisor
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec start_link() -> {ok, Pid :: pid()} |
|
-spec start_link() ->
|
||||||
{error, {already_started, Pid :: pid()}} |
|
{ok, Pid :: pid()}
|
||||||
{error, {shutdown, term()}} |
|
| {error, {already_started, Pid :: pid()}}
|
||||||
{error, term()} |
|
| {error, {shutdown, term()}}
|
||||||
ignore.
|
| {error, term()}
|
||||||
|
| ignore.
|
||||||
start_link() ->
|
start_link() ->
|
||||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||||
|
|
||||||
|
|
@ -55,22 +56,27 @@ start_link() ->
|
||||||
%% @end
|
%% @end
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec init(Args :: term()) ->
|
-spec init(Args :: term()) ->
|
||||||
{ok, {SupFlags :: supervisor:sup_flags(),
|
{ok, {SupFlags :: supervisor:sup_flags(), [ChildSpec :: supervisor:child_spec()]}}
|
||||||
[ChildSpec :: supervisor:child_spec()]}} |
|
| ignore.
|
||||||
ignore.
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
SupFlags = #{strategy => one_for_one,
|
SupFlags = #{
|
||||||
intensity => 10,
|
strategy => one_for_one,
|
||||||
period => 3600},
|
intensity => 10,
|
||||||
|
period => 3600
|
||||||
|
},
|
||||||
|
|
||||||
Childs = [ make_child(emqx_limiter_manager, worker)
|
Childs = [
|
||||||
, make_child(emqx_limiter_server_sup, supervisor)],
|
make_child(emqx_limiter_manager, worker),
|
||||||
|
make_child(emqx_limiter_server_sup, supervisor)
|
||||||
|
],
|
||||||
|
|
||||||
{ok, {SupFlags, Childs}}.
|
{ok, {SupFlags, Childs}}.
|
||||||
|
|
||||||
make_child(Mod, Type) ->
|
make_child(Mod, Type) ->
|
||||||
#{id => Mod,
|
#{
|
||||||
start => {Mod, start_link, []},
|
id => Mod,
|
||||||
restart => transient,
|
start => {Mod, start_link, []},
|
||||||
type => Type,
|
restart => transient,
|
||||||
modules => [Mod]}.
|
type => Type,
|
||||||
|
modules => [Mod]
|
||||||
|
}.
|
||||||
|
|
|
||||||
|
|
@ -24,30 +24,33 @@
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
-boot_mnesia({mnesia, [boot]}).
|
||||||
-export([mnesia/1]).
|
-export([mnesia/1]).
|
||||||
|
|
||||||
-export([ publish/1
|
-export([
|
||||||
, subscribe/3
|
publish/1,
|
||||||
, unsubscribe/2
|
subscribe/3,
|
||||||
, log/3
|
unsubscribe/2,
|
||||||
]).
|
log/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ start_link/0
|
-export([
|
||||||
, list/0
|
start_link/0,
|
||||||
, list/1
|
list/0,
|
||||||
, get_trace_filename/1
|
list/1,
|
||||||
, create/1
|
get_trace_filename/1,
|
||||||
, delete/1
|
create/1,
|
||||||
, clear/0
|
delete/1,
|
||||||
, update/2
|
clear/0,
|
||||||
, check/0
|
update/2,
|
||||||
]).
|
check/0
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ format/1
|
-export([
|
||||||
, zip_dir/0
|
format/1,
|
||||||
, filename/2
|
zip_dir/0,
|
||||||
, trace_dir/0
|
filename/2,
|
||||||
, trace_file/1
|
trace_dir/0,
|
||||||
, delete_files_after_send/2
|
trace_file/1,
|
||||||
]).
|
delete_files_after_send/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
|
||||||
|
|
||||||
|
|
@ -56,22 +59,23 @@
|
||||||
-define(OWN_KEYS, [level, filters, filter_default, handlers]).
|
-define(OWN_KEYS, [level, filters, filter_default, handlers]).
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
-export([ log_file/2
|
-export([
|
||||||
, find_closest_time/2
|
log_file/2,
|
||||||
]).
|
find_closest_time/2
|
||||||
|
]).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
-export_type([ip_address/0]).
|
-export_type([ip_address/0]).
|
||||||
-type ip_address() :: string().
|
-type ip_address() :: string().
|
||||||
|
|
||||||
-record(?TRACE, {
|
-record(?TRACE, {
|
||||||
name :: binary() | undefined | '_'
|
name :: binary() | undefined | '_',
|
||||||
, type :: clientid | topic | ip_address | undefined | '_'
|
type :: clientid | topic | ip_address | undefined | '_',
|
||||||
, filter :: emqx_types:topic() | emqx_types:clientid() | ip_address() | undefined | '_'
|
filter :: emqx_types:topic() | emqx_types:clientid() | ip_address() | undefined | '_',
|
||||||
, enable = true :: boolean() | '_'
|
enable = true :: boolean() | '_',
|
||||||
, start_at :: integer() | undefined | '_'
|
start_at :: integer() | undefined | '_',
|
||||||
, end_at :: integer() | undefined | '_'
|
end_at :: integer() | undefined | '_'
|
||||||
}).
|
}).
|
||||||
|
|
||||||
mnesia(boot) ->
|
mnesia(boot) ->
|
||||||
ok = mria:create_table(?TRACE, [
|
ok = mria:create_table(?TRACE, [
|
||||||
|
|
@ -79,18 +83,23 @@ mnesia(boot) ->
|
||||||
{rlog_shard, ?COMMON_SHARD},
|
{rlog_shard, ?COMMON_SHARD},
|
||||||
{storage, disc_copies},
|
{storage, disc_copies},
|
||||||
{record_name, ?TRACE},
|
{record_name, ?TRACE},
|
||||||
{attributes, record_info(fields, ?TRACE)}]).
|
{attributes, record_info(fields, ?TRACE)}
|
||||||
|
]).
|
||||||
|
|
||||||
publish(#message{topic = <<"$SYS/", _/binary>>}) -> ignore;
|
publish(#message{topic = <<"$SYS/", _/binary>>}) ->
|
||||||
|
ignore;
|
||||||
publish(#message{from = From, topic = Topic, payload = Payload}) when
|
publish(#message{from = From, topic = Topic, payload = Payload}) when
|
||||||
is_binary(From); is_atom(From) ->
|
is_binary(From); is_atom(From)
|
||||||
|
->
|
||||||
?TRACE("PUBLISH", "publish_to", #{topic => Topic, payload => Payload}).
|
?TRACE("PUBLISH", "publish_to", #{topic => Topic, payload => Payload}).
|
||||||
|
|
||||||
subscribe(<<"$SYS/", _/binary>>, _SubId, _SubOpts) -> ignore;
|
subscribe(<<"$SYS/", _/binary>>, _SubId, _SubOpts) ->
|
||||||
|
ignore;
|
||||||
subscribe(Topic, SubId, SubOpts) ->
|
subscribe(Topic, SubId, SubOpts) ->
|
||||||
?TRACE("SUBSCRIBE", "subscribe", #{topic => Topic, sub_opts => SubOpts, sub_id => SubId}).
|
?TRACE("SUBSCRIBE", "subscribe", #{topic => Topic, sub_opts => SubOpts, sub_id => SubId}).
|
||||||
|
|
||||||
unsubscribe(<<"$SYS/", _/binary>>, _SubOpts) -> ignore;
|
unsubscribe(<<"$SYS/", _/binary>>, _SubOpts) ->
|
||||||
|
ignore;
|
||||||
unsubscribe(Topic, SubOpts) ->
|
unsubscribe(Topic, SubOpts) ->
|
||||||
?TRACE("UNSUBSCRIBE", "unsubscribe", #{topic => Topic, sub_opts => SubOpts}).
|
?TRACE("UNSUBSCRIBE", "unsubscribe", #{topic => Topic, sub_opts => SubOpts}).
|
||||||
|
|
||||||
|
|
@ -103,36 +112,47 @@ log(List, Msg, Meta0) ->
|
||||||
Log = #{level => debug, meta => Meta, msg => Msg},
|
Log = #{level => debug, meta => Meta, msg => Msg},
|
||||||
log_filter(List, Log).
|
log_filter(List, Log).
|
||||||
|
|
||||||
log_filter([], _Log) -> ok;
|
log_filter([], _Log) ->
|
||||||
|
ok;
|
||||||
log_filter([{Id, FilterFun, Filter, Name} | Rest], Log0) ->
|
log_filter([{Id, FilterFun, Filter, Name} | Rest], Log0) ->
|
||||||
case FilterFun(Log0, {Filter, Name}) of
|
case FilterFun(Log0, {Filter, Name}) of
|
||||||
stop -> stop;
|
stop ->
|
||||||
ignore -> ignore;
|
stop;
|
||||||
|
ignore ->
|
||||||
|
ignore;
|
||||||
Log ->
|
Log ->
|
||||||
case logger_config:get(ets:whereis(logger), Id) of
|
case logger_config:get(ets:whereis(logger), Id) of
|
||||||
{ok, #{module := Module} = HandlerConfig0} ->
|
{ok, #{module := Module} = HandlerConfig0} ->
|
||||||
HandlerConfig = maps:without(?OWN_KEYS, HandlerConfig0),
|
HandlerConfig = maps:without(?OWN_KEYS, HandlerConfig0),
|
||||||
try Module:log(Log, HandlerConfig)
|
try
|
||||||
catch C:R:S ->
|
Module:log(Log, HandlerConfig)
|
||||||
case logger:remove_handler(Id) of
|
catch
|
||||||
ok ->
|
C:R:S ->
|
||||||
logger:internal_log(error, {removed_failing_handler, Id, C, R, S});
|
case logger:remove_handler(Id) of
|
||||||
{error,{not_found,_}} ->
|
ok ->
|
||||||
%% Probably already removed by other client
|
logger:internal_log(
|
||||||
%% Don't report again
|
error, {removed_failing_handler, Id, C, R, S}
|
||||||
ok;
|
);
|
||||||
{error,Reason} ->
|
{error, {not_found, _}} ->
|
||||||
logger:internal_log(error,
|
%% Probably already removed by other client
|
||||||
{removed_handler_failed, Id, Reason, C, R, S})
|
%% Don't report again
|
||||||
end
|
ok;
|
||||||
|
{error, Reason} ->
|
||||||
|
logger:internal_log(
|
||||||
|
error,
|
||||||
|
{removed_handler_failed, Id, Reason, C, R, S}
|
||||||
|
)
|
||||||
|
end
|
||||||
end;
|
end;
|
||||||
{error, {not_found, Id}} -> ok;
|
{error, {not_found, Id}} ->
|
||||||
{error, Reason} -> logger:internal_log(error, {find_handle_id_failed, Id, Reason})
|
ok;
|
||||||
|
{error, Reason} ->
|
||||||
|
logger:internal_log(error, {find_handle_id_failed, Id, Reason})
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
log_filter(Rest, Log0).
|
log_filter(Rest, Log0).
|
||||||
|
|
||||||
-spec(start_link() -> emqx_types:startlink_ret()).
|
-spec start_link() -> emqx_types:startlink_ret().
|
||||||
start_link() ->
|
start_link() ->
|
||||||
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
|
||||||
|
|
||||||
|
|
@ -145,7 +165,8 @@ list(Enable) ->
|
||||||
ets:match_object(?TRACE, #?TRACE{enable = Enable, _ = '_'}).
|
ets:match_object(?TRACE, #?TRACE{enable = Enable, _ = '_'}).
|
||||||
|
|
||||||
-spec create([{Key :: binary(), Value :: binary()}] | #{atom() => binary()}) ->
|
-spec create([{Key :: binary(), Value :: binary()}] | #{atom() => binary()}) ->
|
||||||
{ok, #?TRACE{}} | {error, {duplicate_condition, iodata()} | {already_existed, iodata()} | iodata()}.
|
{ok, #?TRACE{}}
|
||||||
|
| {error, {duplicate_condition, iodata()} | {already_existed, iodata()} | iodata()}.
|
||||||
create(Trace) ->
|
create(Trace) ->
|
||||||
case mnesia:table_info(?TRACE, size) < ?MAX_SIZE of
|
case mnesia:table_info(?TRACE, size) < ?MAX_SIZE of
|
||||||
true ->
|
true ->
|
||||||
|
|
@ -154,8 +175,9 @@ create(Trace) ->
|
||||||
{error, Reason} -> {error, Reason}
|
{error, Reason} -> {error, Reason}
|
||||||
end;
|
end;
|
||||||
false ->
|
false ->
|
||||||
{error, "The number of traces created has reache the maximum"
|
{error,
|
||||||
" please delete the useless ones first"}
|
"The number of traces created has reache the maximum"
|
||||||
|
" please delete the useless ones first"}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec delete(Name :: binary()) -> ok | {error, not_found}.
|
-spec delete(Name :: binary()) -> ok | {error, not_found}.
|
||||||
|
|
@ -165,7 +187,7 @@ delete(Name) ->
|
||||||
[_] -> mnesia:delete(?TRACE, Name, write);
|
[_] -> mnesia:delete(?TRACE, Name, write);
|
||||||
[] -> mnesia:abort(not_found)
|
[] -> mnesia:abort(not_found)
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
transaction(Tran).
|
transaction(Tran).
|
||||||
|
|
||||||
-spec clear() -> ok | {error, Reason :: term()}.
|
-spec clear() -> ok | {error, Reason :: term()}.
|
||||||
|
|
@ -180,15 +202,17 @@ clear() ->
|
||||||
update(Name, Enable) ->
|
update(Name, Enable) ->
|
||||||
Tran = fun() ->
|
Tran = fun() ->
|
||||||
case mnesia:read(?TRACE, Name) of
|
case mnesia:read(?TRACE, Name) of
|
||||||
[] -> mnesia:abort(not_found);
|
[] ->
|
||||||
[#?TRACE{enable = Enable}] -> ok;
|
mnesia:abort(not_found);
|
||||||
|
[#?TRACE{enable = Enable}] ->
|
||||||
|
ok;
|
||||||
[Rec] ->
|
[Rec] ->
|
||||||
case erlang:system_time(second) >= Rec#?TRACE.end_at of
|
case erlang:system_time(second) >= Rec#?TRACE.end_at of
|
||||||
false -> mnesia:write(?TRACE, Rec#?TRACE{enable = Enable}, write);
|
false -> mnesia:write(?TRACE, Rec#?TRACE{enable = Enable}, write);
|
||||||
true -> mnesia:abort(finished)
|
true -> mnesia:abort(finished)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
transaction(Tran).
|
transaction(Tran).
|
||||||
|
|
||||||
check() ->
|
check() ->
|
||||||
|
|
@ -201,12 +225,13 @@ get_trace_filename(Name) ->
|
||||||
case mnesia:read(?TRACE, Name, read) of
|
case mnesia:read(?TRACE, Name, read) of
|
||||||
[] -> mnesia:abort(not_found);
|
[] -> mnesia:abort(not_found);
|
||||||
[#?TRACE{start_at = Start}] -> {ok, filename(Name, Start)}
|
[#?TRACE{start_at = Start}] -> {ok, filename(Name, Start)}
|
||||||
end end,
|
end
|
||||||
|
end,
|
||||||
transaction(Tran).
|
transaction(Tran).
|
||||||
|
|
||||||
-spec trace_file(File :: file:filename_all()) ->
|
-spec trace_file(File :: file:filename_all()) ->
|
||||||
{ok, Node :: list(), Binary :: binary()} |
|
{ok, Node :: list(), Binary :: binary()}
|
||||||
{error, Node :: list(), Reason :: term()}.
|
| {error, Node :: list(), Reason :: term()}.
|
||||||
trace_file(File) ->
|
trace_file(File) ->
|
||||||
FileName = filename:join(trace_dir(), File),
|
FileName = filename:join(trace_dir(), File),
|
||||||
Node = atom_to_list(node()),
|
Node = atom_to_list(node()),
|
||||||
|
|
@ -221,10 +246,13 @@ delete_files_after_send(TraceLog, Zips) ->
|
||||||
-spec format(list(#?TRACE{})) -> list(map()).
|
-spec format(list(#?TRACE{})) -> list(map()).
|
||||||
format(Traces) ->
|
format(Traces) ->
|
||||||
Fields = record_info(fields, ?TRACE),
|
Fields = record_info(fields, ?TRACE),
|
||||||
lists:map(fun(Trace0 = #?TRACE{}) ->
|
lists:map(
|
||||||
[_ | Values] = tuple_to_list(Trace0),
|
fun(Trace0 = #?TRACE{}) ->
|
||||||
maps:from_list(lists:zip(Fields, Values))
|
[_ | Values] = tuple_to_list(Trace0),
|
||||||
end, Traces).
|
maps:from_list(lists:zip(Fields, Values))
|
||||||
|
end,
|
||||||
|
Traces
|
||||||
|
).
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
ok = mria:wait_for_tables([?TRACE]),
|
ok = mria:wait_for_tables([?TRACE]),
|
||||||
|
|
@ -253,7 +281,8 @@ handle_cast(Msg, State) ->
|
||||||
|
|
||||||
handle_info({'DOWN', _Ref, process, Pid, _Reason}, State = #{monitors := Monitors}) ->
|
handle_info({'DOWN', _Ref, process, Pid, _Reason}, State = #{monitors := Monitors}) ->
|
||||||
case maps:take(Pid, Monitors) of
|
case maps:take(Pid, Monitors) of
|
||||||
error -> {noreply, State};
|
error ->
|
||||||
|
{noreply, State};
|
||||||
{Files, NewMonitors} ->
|
{Files, NewMonitors} ->
|
||||||
lists:foreach(fun file:delete/1, Files),
|
lists:foreach(fun file:delete/1, Files),
|
||||||
{noreply, State#{monitors => NewMonitors}}
|
{noreply, State#{monitors => NewMonitors}}
|
||||||
|
|
@ -264,11 +293,9 @@ handle_info({timeout, TRef, update_trace}, #{timer := TRef} = State) ->
|
||||||
update_trace_handler(),
|
update_trace_handler(),
|
||||||
?tp(update_trace_done, #{}),
|
?tp(update_trace_done, #{}),
|
||||||
{noreply, State#{timer => NextTRef}};
|
{noreply, State#{timer => NextTRef}};
|
||||||
|
|
||||||
handle_info({mnesia_table_event, _Events}, State = #{timer := TRef}) ->
|
handle_info({mnesia_table_event, _Events}, State = #{timer := TRef}) ->
|
||||||
emqx_misc:cancel_timer(TRef),
|
emqx_misc:cancel_timer(TRef),
|
||||||
handle_info({timeout, TRef, update_trace}, State);
|
handle_info({timeout, TRef, update_trace}, State);
|
||||||
|
|
||||||
handle_info(Info, State) ->
|
handle_info(Info, State) ->
|
||||||
?SLOG(error, #{unexpected_info => Info}),
|
?SLOG(error, #{unexpected_info => Info}),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
@ -294,11 +321,13 @@ insert_new_trace(Trace) ->
|
||||||
[] ->
|
[] ->
|
||||||
ok = mnesia:write(?TRACE, Trace, write),
|
ok = mnesia:write(?TRACE, Trace, write),
|
||||||
{ok, Trace};
|
{ok, Trace};
|
||||||
[#?TRACE{name = Name}] -> mnesia:abort({duplicate_condition, Name})
|
[#?TRACE{name = Name}] ->
|
||||||
|
mnesia:abort({duplicate_condition, Name})
|
||||||
end;
|
end;
|
||||||
[#?TRACE{name = Name}] -> mnesia:abort({already_existed, Name})
|
[#?TRACE{name = Name}] ->
|
||||||
|
mnesia:abort({already_existed, Name})
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
transaction(Tran).
|
transaction(Tran).
|
||||||
|
|
||||||
update_trace(Traces) ->
|
update_trace(Traces) ->
|
||||||
|
|
@ -314,67 +343,91 @@ update_trace(Traces) ->
|
||||||
emqx_misc:start_timer(NextTime, update_trace).
|
emqx_misc:start_timer(NextTime, update_trace).
|
||||||
|
|
||||||
stop_all_trace_handler() ->
|
stop_all_trace_handler() ->
|
||||||
lists:foreach(fun(#{id := Id}) -> emqx_trace_handler:uninstall(Id) end,
|
lists:foreach(
|
||||||
emqx_trace_handler:running()).
|
fun(#{id := Id}) -> emqx_trace_handler:uninstall(Id) end,
|
||||||
|
emqx_trace_handler:running()
|
||||||
|
).
|
||||||
get_enable_trace() ->
|
get_enable_trace() ->
|
||||||
transaction(fun() ->
|
transaction(fun() ->
|
||||||
mnesia:match_object(?TRACE, #?TRACE{enable = true, _ = '_'}, read)
|
mnesia:match_object(?TRACE, #?TRACE{enable = true, _ = '_'}, read)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
find_closest_time(Traces, Now) ->
|
find_closest_time(Traces, Now) ->
|
||||||
Sec =
|
Sec =
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun(#?TRACE{start_at = Start, end_at = End, enable = true}, Closest) ->
|
fun
|
||||||
min(closest(End, Now, Closest), closest(Start, Now, Closest));
|
(#?TRACE{start_at = Start, end_at = End, enable = true}, Closest) ->
|
||||||
(_, Closest) -> Closest
|
min(closest(End, Now, Closest), closest(Start, Now, Closest));
|
||||||
end, 60 * 15, Traces),
|
(_, Closest) ->
|
||||||
|
Closest
|
||||||
|
end,
|
||||||
|
60 * 15,
|
||||||
|
Traces
|
||||||
|
),
|
||||||
timer:seconds(Sec).
|
timer:seconds(Sec).
|
||||||
|
|
||||||
closest(Time, Now, Closest) when Now >= Time -> Closest;
|
closest(Time, Now, Closest) when Now >= Time -> Closest;
|
||||||
closest(Time, Now, Closest) -> min(Time - Now, Closest).
|
closest(Time, Now, Closest) -> min(Time - Now, Closest).
|
||||||
|
|
||||||
disable_finished([]) -> ok;
|
disable_finished([]) ->
|
||||||
|
ok;
|
||||||
disable_finished(Traces) ->
|
disable_finished(Traces) ->
|
||||||
transaction(fun() ->
|
transaction(fun() ->
|
||||||
lists:map(fun(#?TRACE{name = Name}) ->
|
lists:map(
|
||||||
case mnesia:read(?TRACE, Name, write) of
|
fun(#?TRACE{name = Name}) ->
|
||||||
[] -> ok;
|
case mnesia:read(?TRACE, Name, write) of
|
||||||
[Trace] -> mnesia:write(?TRACE, Trace#?TRACE{enable = false}, write)
|
[] -> ok;
|
||||||
end end, Traces)
|
[Trace] -> mnesia:write(?TRACE, Trace#?TRACE{enable = false}, write)
|
||||||
end).
|
end
|
||||||
|
end,
|
||||||
|
Traces
|
||||||
|
)
|
||||||
|
end).
|
||||||
|
|
||||||
start_trace(Traces, Started0) ->
|
start_trace(Traces, Started0) ->
|
||||||
Started = lists:map(fun(#{name := Name}) -> Name end, Started0),
|
Started = lists:map(fun(#{name := Name}) -> Name end, Started0),
|
||||||
lists:foldl(fun(#?TRACE{name = Name} = Trace,
|
lists:foldl(
|
||||||
{Running, StartedAcc}) ->
|
fun(
|
||||||
case lists:member(Name, StartedAcc) of
|
#?TRACE{name = Name} = Trace,
|
||||||
true -> {[Name | Running], StartedAcc};
|
{Running, StartedAcc}
|
||||||
false ->
|
) ->
|
||||||
case start_trace(Trace) of
|
case lists:member(Name, StartedAcc) of
|
||||||
ok -> {[Name | Running], [Name | StartedAcc]};
|
true ->
|
||||||
{error, _Reason} -> {[Name | Running], StartedAcc}
|
{[Name | Running], StartedAcc};
|
||||||
end
|
false ->
|
||||||
end
|
case start_trace(Trace) of
|
||||||
end, {[], Started}, Traces).
|
ok -> {[Name | Running], [Name | StartedAcc]};
|
||||||
|
{error, _Reason} -> {[Name | Running], StartedAcc}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
{[], Started},
|
||||||
|
Traces
|
||||||
|
).
|
||||||
|
|
||||||
start_trace(Trace) ->
|
start_trace(Trace) ->
|
||||||
#?TRACE{name = Name
|
#?TRACE{
|
||||||
, type = Type
|
name = Name,
|
||||||
, filter = Filter
|
type = Type,
|
||||||
, start_at = Start
|
filter = Filter,
|
||||||
|
start_at = Start
|
||||||
} = Trace,
|
} = Trace,
|
||||||
Who = #{name => Name, type => Type, filter => Filter},
|
Who = #{name => Name, type => Type, filter => Filter},
|
||||||
emqx_trace_handler:install(Who, debug, log_file(Name, Start)).
|
emqx_trace_handler:install(Who, debug, log_file(Name, Start)).
|
||||||
|
|
||||||
stop_trace(Finished, Started) ->
|
stop_trace(Finished, Started) ->
|
||||||
lists:foreach(fun(#{name := Name, type := Type, filter := Filter}) ->
|
lists:foreach(
|
||||||
case lists:member(Name, Finished) of
|
fun(#{name := Name, type := Type, filter := Filter}) ->
|
||||||
true ->
|
case lists:member(Name, Finished) of
|
||||||
?TRACE("API", "trace_stopping", #{Type => Filter}),
|
true ->
|
||||||
emqx_trace_handler:uninstall(Type, Name);
|
?TRACE("API", "trace_stopping", #{Type => Filter}),
|
||||||
false -> ok
|
emqx_trace_handler:uninstall(Type, Name);
|
||||||
end
|
false ->
|
||||||
end, Started).
|
ok
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
Started
|
||||||
|
).
|
||||||
|
|
||||||
clean_stale_trace_files() ->
|
clean_stale_trace_files() ->
|
||||||
TraceDir = trace_dir(),
|
TraceDir = trace_dir(),
|
||||||
|
|
@ -383,30 +436,44 @@ clean_stale_trace_files() ->
|
||||||
FileFun = fun(#?TRACE{name = Name, start_at = StartAt}) -> filename(Name, StartAt) end,
|
FileFun = fun(#?TRACE{name = Name, start_at = StartAt}) -> filename(Name, StartAt) end,
|
||||||
KeepFiles = lists:map(FileFun, list()),
|
KeepFiles = lists:map(FileFun, list()),
|
||||||
case AllFiles -- ["zip" | KeepFiles] of
|
case AllFiles -- ["zip" | KeepFiles] of
|
||||||
[] -> ok;
|
[] ->
|
||||||
|
ok;
|
||||||
DeleteFiles ->
|
DeleteFiles ->
|
||||||
DelFun = fun(F) -> file:delete(filename:join(TraceDir, F)) end,
|
DelFun = fun(F) -> file:delete(filename:join(TraceDir, F)) end,
|
||||||
lists:foreach(DelFun, DeleteFiles)
|
lists:foreach(DelFun, DeleteFiles)
|
||||||
end;
|
end;
|
||||||
_ -> ok
|
_ ->
|
||||||
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
classify_by_time(Traces, Now) ->
|
classify_by_time(Traces, Now) ->
|
||||||
classify_by_time(Traces, Now, [], [], []).
|
classify_by_time(Traces, Now, [], [], []).
|
||||||
|
|
||||||
classify_by_time([], _Now, Wait, Run, Finish) -> {Wait, Run, Finish};
|
classify_by_time([], _Now, Wait, Run, Finish) ->
|
||||||
classify_by_time([Trace = #?TRACE{start_at = Start} | Traces],
|
{Wait, Run, Finish};
|
||||||
Now, Wait, Run, Finish) when Start > Now ->
|
classify_by_time(
|
||||||
|
[Trace = #?TRACE{start_at = Start} | Traces],
|
||||||
|
Now,
|
||||||
|
Wait,
|
||||||
|
Run,
|
||||||
|
Finish
|
||||||
|
) when Start > Now ->
|
||||||
classify_by_time(Traces, Now, [Trace | Wait], Run, Finish);
|
classify_by_time(Traces, Now, [Trace | Wait], Run, Finish);
|
||||||
classify_by_time([Trace = #?TRACE{end_at = End} | Traces],
|
classify_by_time(
|
||||||
Now, Wait, Run, Finish) when End =< Now ->
|
[Trace = #?TRACE{end_at = End} | Traces],
|
||||||
|
Now,
|
||||||
|
Wait,
|
||||||
|
Run,
|
||||||
|
Finish
|
||||||
|
) when End =< Now ->
|
||||||
classify_by_time(Traces, Now, Wait, Run, [Trace | Finish]);
|
classify_by_time(Traces, Now, Wait, Run, [Trace | Finish]);
|
||||||
classify_by_time([Trace | Traces], Now, Wait, Run, Finish) ->
|
classify_by_time([Trace | Traces], Now, Wait, Run, Finish) ->
|
||||||
classify_by_time(Traces, Now, Wait, [Trace | Run], Finish).
|
classify_by_time(Traces, Now, Wait, [Trace | Run], Finish).
|
||||||
|
|
||||||
to_trace(TraceParam) ->
|
to_trace(TraceParam) ->
|
||||||
case to_trace(ensure_map(TraceParam), #?TRACE{}) of
|
case to_trace(ensure_map(TraceParam), #?TRACE{}) of
|
||||||
{error, Reason} -> {error, Reason};
|
{error, Reason} ->
|
||||||
|
{error, Reason};
|
||||||
{ok, #?TRACE{name = undefined}} ->
|
{ok, #?TRACE{name = undefined}} ->
|
||||||
{error, "name required"};
|
{error, "name required"};
|
||||||
{ok, #?TRACE{type = undefined}} ->
|
{ok, #?TRACE{type = undefined}} ->
|
||||||
|
|
@ -421,21 +488,31 @@ to_trace(TraceParam) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
ensure_map(#{} = Trace) ->
|
ensure_map(#{} = Trace) ->
|
||||||
maps:fold(fun(K, V, Acc) when is_binary(K) -> Acc#{binary_to_existing_atom(K) => V};
|
maps:fold(
|
||||||
(K, V, Acc) when is_atom(K) -> Acc#{K => V}
|
fun
|
||||||
end, #{}, Trace);
|
(K, V, Acc) when is_binary(K) -> Acc#{binary_to_existing_atom(K) => V};
|
||||||
|
(K, V, Acc) when is_atom(K) -> Acc#{K => V}
|
||||||
|
end,
|
||||||
|
#{},
|
||||||
|
Trace
|
||||||
|
);
|
||||||
ensure_map(Trace) when is_list(Trace) ->
|
ensure_map(Trace) when is_list(Trace) ->
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun({K, V}, Acc) when is_binary(K) -> Acc#{binary_to_existing_atom(K) => V};
|
fun
|
||||||
|
({K, V}, Acc) when is_binary(K) -> Acc#{binary_to_existing_atom(K) => V};
|
||||||
({K, V}, Acc) when is_atom(K) -> Acc#{K => V};
|
({K, V}, Acc) when is_atom(K) -> Acc#{K => V};
|
||||||
(_, Acc) -> Acc
|
(_, Acc) -> Acc
|
||||||
end, #{}, Trace).
|
end,
|
||||||
|
#{},
|
||||||
|
Trace
|
||||||
|
).
|
||||||
|
|
||||||
fill_default(Trace = #?TRACE{start_at = undefined}) ->
|
fill_default(Trace = #?TRACE{start_at = undefined}) ->
|
||||||
fill_default(Trace#?TRACE{start_at = erlang:system_time(second)});
|
fill_default(Trace#?TRACE{start_at = erlang:system_time(second)});
|
||||||
fill_default(Trace = #?TRACE{end_at = undefined, start_at = StartAt}) ->
|
fill_default(Trace = #?TRACE{end_at = undefined, start_at = StartAt}) ->
|
||||||
fill_default(Trace#?TRACE{end_at = StartAt + 10 * 60});
|
fill_default(Trace#?TRACE{end_at = StartAt + 10 * 60});
|
||||||
fill_default(Trace) -> Trace.
|
fill_default(Trace) ->
|
||||||
|
Trace.
|
||||||
|
|
||||||
-define(NAME_RE, "^[A-Za-z]+[A-Za-z0-9-_]*$").
|
-define(NAME_RE, "^[A-Za-z]+[A-Za-z0-9-_]*$").
|
||||||
|
|
||||||
|
|
@ -452,16 +529,19 @@ to_trace(#{type := topic, topic := Filter} = Trace, Rec) ->
|
||||||
ok ->
|
ok ->
|
||||||
Trace0 = maps:without([type, topic], Trace),
|
Trace0 = maps:without([type, topic], Trace),
|
||||||
to_trace(Trace0, Rec#?TRACE{type = topic, filter = Filter});
|
to_trace(Trace0, Rec#?TRACE{type = topic, filter = Filter});
|
||||||
Error -> Error
|
Error ->
|
||||||
|
Error
|
||||||
end;
|
end;
|
||||||
to_trace(#{type := ip_address, ip_address := Filter} = Trace, Rec) ->
|
to_trace(#{type := ip_address, ip_address := Filter} = Trace, Rec) ->
|
||||||
case validate_ip_address(Filter) of
|
case validate_ip_address(Filter) of
|
||||||
ok ->
|
ok ->
|
||||||
Trace0 = maps:without([type, ip_address], Trace),
|
Trace0 = maps:without([type, ip_address], Trace),
|
||||||
to_trace(Trace0, Rec#?TRACE{type = ip_address, filter = binary_to_list(Filter)});
|
to_trace(Trace0, Rec#?TRACE{type = ip_address, filter = binary_to_list(Filter)});
|
||||||
Error -> Error
|
Error ->
|
||||||
|
Error
|
||||||
end;
|
end;
|
||||||
to_trace(#{type := Type}, _Rec) -> {error, io_lib:format("required ~s field", [Type])};
|
to_trace(#{type := Type}, _Rec) ->
|
||||||
|
{error, io_lib:format("required ~s field", [Type])};
|
||||||
to_trace(#{start_at := StartAt} = Trace, Rec) ->
|
to_trace(#{start_at := StartAt} = Trace, Rec) ->
|
||||||
{ok, Sec} = to_system_second(StartAt),
|
{ok, Sec} = to_system_second(StartAt),
|
||||||
to_trace(maps:remove(start_at, Trace), Rec#?TRACE{start_at = Sec});
|
to_trace(maps:remove(start_at, Trace), Rec#?TRACE{start_at = Sec});
|
||||||
|
|
@ -473,7 +553,8 @@ to_trace(#{end_at := EndAt} = Trace, Rec) ->
|
||||||
{ok, _Sec} ->
|
{ok, _Sec} ->
|
||||||
{error, "end_at time has already passed"}
|
{error, "end_at time has already passed"}
|
||||||
end;
|
end;
|
||||||
to_trace(_, Rec) -> {ok, Rec}.
|
to_trace(_, Rec) ->
|
||||||
|
{ok, Rec}.
|
||||||
|
|
||||||
validate_topic(TopicName) ->
|
validate_topic(TopicName) ->
|
||||||
try emqx_topic:validate(filter, TopicName) of
|
try emqx_topic:validate(filter, TopicName) of
|
||||||
|
|
@ -513,11 +594,22 @@ transaction(Tran) ->
|
||||||
|
|
||||||
update_trace_handler() ->
|
update_trace_handler() ->
|
||||||
case emqx_trace_handler:running() of
|
case emqx_trace_handler:running() of
|
||||||
[] -> persistent_term:erase(?TRACE_FILTER);
|
[] ->
|
||||||
|
persistent_term:erase(?TRACE_FILTER);
|
||||||
Running ->
|
Running ->
|
||||||
List = lists:map(fun(#{id := Id, filter_fun := FilterFun,
|
List = lists:map(
|
||||||
filter := Filter, name := Name}) ->
|
fun(
|
||||||
{Id, FilterFun, Filter, Name} end, Running),
|
#{
|
||||||
|
id := Id,
|
||||||
|
filter_fun := FilterFun,
|
||||||
|
filter := Filter,
|
||||||
|
name := Name
|
||||||
|
}
|
||||||
|
) ->
|
||||||
|
{Id, FilterFun, Filter, Name}
|
||||||
|
end,
|
||||||
|
Running
|
||||||
|
),
|
||||||
case List =/= persistent_term:get(?TRACE_FILTER, undefined) of
|
case List =/= persistent_term:get(?TRACE_FILTER, undefined) of
|
||||||
true -> persistent_term:put(?TRACE_FILTER, List);
|
true -> persistent_term:put(?TRACE_FILTER, List);
|
||||||
false -> ok
|
false -> ok
|
||||||
|
|
@ -525,6 +617,9 @@ update_trace_handler() ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
filter_cli_handler(Names) ->
|
filter_cli_handler(Names) ->
|
||||||
lists:filter(fun(Name) ->
|
lists:filter(
|
||||||
nomatch =:= re:run(Name, "^CLI-+.", [])
|
fun(Name) ->
|
||||||
end, Names).
|
nomatch =:= re:run(Name, "^CLI-+.", [])
|
||||||
|
end,
|
||||||
|
Names
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -23,14 +23,15 @@
|
||||||
-spec format(LogEvent, Config) -> unicode:chardata() when
|
-spec format(LogEvent, Config) -> unicode:chardata() when
|
||||||
LogEvent :: logger:log_event(),
|
LogEvent :: logger:log_event(),
|
||||||
Config :: logger:config().
|
Config :: logger:config().
|
||||||
format(#{level := debug, meta := Meta = #{trace_tag := Tag}, msg := Msg},
|
format(
|
||||||
#{payload_encode := PEncode}) ->
|
#{level := debug, meta := Meta = #{trace_tag := Tag}, msg := Msg},
|
||||||
|
#{payload_encode := PEncode}
|
||||||
|
) ->
|
||||||
Time = calendar:system_time_to_rfc3339(erlang:system_time(second)),
|
Time = calendar:system_time_to_rfc3339(erlang:system_time(second)),
|
||||||
ClientId = to_iolist(maps:get(clientid, Meta, "")),
|
ClientId = to_iolist(maps:get(clientid, Meta, "")),
|
||||||
Peername = maps:get(peername, Meta, ""),
|
Peername = maps:get(peername, Meta, ""),
|
||||||
MetaBin = format_meta(Meta, PEncode),
|
MetaBin = format_meta(Meta, PEncode),
|
||||||
[Time, " [", Tag, "] ", ClientId, "@", Peername, " msg: ", Msg, MetaBin, "\n"];
|
[Time, " [", Tag, "] ", ClientId, "@", Peername, " msg: ", Msg, MetaBin, "\n"];
|
||||||
|
|
||||||
format(Event, Config) ->
|
format(Event, Config) ->
|
||||||
emqx_logger_textfmt:format(Event, Config).
|
emqx_logger_textfmt:format(Event, Config).
|
||||||
|
|
||||||
|
|
@ -72,6 +73,10 @@ to_iolist(SubMap) when is_map(SubMap) -> ["[", map_to_iolist(SubMap), "]"];
|
||||||
to_iolist(Char) -> emqx_logger_textfmt:try_format_unicode(Char).
|
to_iolist(Char) -> emqx_logger_textfmt:try_format_unicode(Char).
|
||||||
|
|
||||||
map_to_iolist(Map) ->
|
map_to_iolist(Map) ->
|
||||||
lists:join(",",
|
lists:join(
|
||||||
lists:map(fun({K, V}) -> [to_iolist(K), ": ", to_iolist(V)] end,
|
",",
|
||||||
maps:to_list(Map))).
|
lists:map(
|
||||||
|
fun({K, V}) -> [to_iolist(K), ": ", to_iolist(V)] end,
|
||||||
|
maps:to_list(Map)
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -22,28 +22,30 @@
|
||||||
-logger_header("[Tracer]").
|
-logger_header("[Tracer]").
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([ running/0
|
-export([
|
||||||
, install/3
|
running/0,
|
||||||
, install/4
|
install/3,
|
||||||
, install/5
|
install/4,
|
||||||
, uninstall/1
|
install/5,
|
||||||
, uninstall/2
|
uninstall/1,
|
||||||
]).
|
uninstall/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% For logger handler filters callbacks
|
%% For logger handler filters callbacks
|
||||||
-export([ filter_clientid/2
|
-export([
|
||||||
, filter_topic/2
|
filter_clientid/2,
|
||||||
, filter_ip_address/2
|
filter_topic/2,
|
||||||
]).
|
filter_ip_address/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([handler_id/2]).
|
-export([handler_id/2]).
|
||||||
-export([payload_encode/0]).
|
-export([payload_encode/0]).
|
||||||
|
|
||||||
-type tracer() :: #{
|
-type tracer() :: #{
|
||||||
name := binary(),
|
name := binary(),
|
||||||
type := clientid | topic | ip_address,
|
type := clientid | topic | ip_address,
|
||||||
filter := emqx_types:clientid() | emqx_types:topic() | emqx_trace:ip_address()
|
filter := emqx_types:clientid() | emqx_types:topic() | emqx_trace:ip_address()
|
||||||
}.
|
}.
|
||||||
|
|
||||||
-define(CONFIG(_LogFile_), #{
|
-define(CONFIG(_LogFile_), #{
|
||||||
type => halt,
|
type => halt,
|
||||||
|
|
@ -60,19 +62,23 @@
|
||||||
%% APIs
|
%% APIs
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
-spec install(Name :: binary() | list(),
|
-spec install(
|
||||||
|
Name :: binary() | list(),
|
||||||
Type :: clientid | topic | ip_address,
|
Type :: clientid | topic | ip_address,
|
||||||
Filter :: emqx_types:clientid() | emqx_types:topic() | string(),
|
Filter :: emqx_types:clientid() | emqx_types:topic() | string(),
|
||||||
Level :: logger:level() | all,
|
Level :: logger:level() | all,
|
||||||
LogFilePath :: string()) -> ok | {error, term()}.
|
LogFilePath :: string()
|
||||||
|
) -> ok | {error, term()}.
|
||||||
install(Name, Type, Filter, Level, LogFile) ->
|
install(Name, Type, Filter, Level, LogFile) ->
|
||||||
Who = #{type => Type, filter => ensure_bin(Filter), name => ensure_bin(Name)},
|
Who = #{type => Type, filter => ensure_bin(Filter), name => ensure_bin(Name)},
|
||||||
install(Who, Level, LogFile).
|
install(Who, Level, LogFile).
|
||||||
|
|
||||||
-spec install(Type :: clientid | topic | ip_address,
|
-spec install(
|
||||||
|
Type :: clientid | topic | ip_address,
|
||||||
Filter :: emqx_types:clientid() | emqx_types:topic() | string(),
|
Filter :: emqx_types:clientid() | emqx_types:topic() | string(),
|
||||||
Level :: logger:level() | all,
|
Level :: logger:level() | all,
|
||||||
LogFilePath :: string()) -> ok | {error, term()}.
|
LogFilePath :: string()
|
||||||
|
) -> ok | {error, term()}.
|
||||||
install(Type, Filter, Level, LogFile) ->
|
install(Type, Filter, Level, LogFile) ->
|
||||||
install(Filter, Type, Filter, Level, LogFile).
|
install(Filter, Type, Filter, Level, LogFile).
|
||||||
|
|
||||||
|
|
@ -92,8 +98,10 @@ install(Who = #{name := Name, type := Type}, Level, LogFile) ->
|
||||||
show_prompts(Res, Who, "start_trace"),
|
show_prompts(Res, Who, "start_trace"),
|
||||||
Res.
|
Res.
|
||||||
|
|
||||||
-spec uninstall(Type :: clientid | topic | ip_address,
|
-spec uninstall(
|
||||||
Name :: binary() | list()) -> ok | {error, term()}.
|
Type :: clientid | topic | ip_address,
|
||||||
|
Name :: binary() | list()
|
||||||
|
) -> ok | {error, term()}.
|
||||||
uninstall(Type, Name) ->
|
uninstall(Type, Name) ->
|
||||||
HandlerId = handler_id(ensure_bin(Name), Type),
|
HandlerId = handler_id(ensure_bin(Name), Type),
|
||||||
uninstall(HandlerId).
|
uninstall(HandlerId).
|
||||||
|
|
@ -108,12 +116,12 @@ uninstall(HandlerId) ->
|
||||||
-spec running() ->
|
-spec running() ->
|
||||||
[
|
[
|
||||||
#{
|
#{
|
||||||
name => binary(),
|
name => binary(),
|
||||||
type => topic | clientid | ip_address,
|
type => topic | clientid | ip_address,
|
||||||
id => atom(),
|
id => atom(),
|
||||||
filter => emqx_types:topic() | emqx_types:clienetid() | emqx_trace:ip_address(),
|
filter => emqx_types:topic() | emqx_types:clienetid() | emqx_trace:ip_address(),
|
||||||
level => logger:level(),
|
level => logger:level(),
|
||||||
dst => file:filename() | console | unknown
|
dst => file:filename() | console | unknown
|
||||||
}
|
}
|
||||||
].
|
].
|
||||||
running() ->
|
running() ->
|
||||||
|
|
@ -122,17 +130,20 @@ running() ->
|
||||||
-spec filter_clientid(logger:log_event(), {binary(), atom()}) -> logger:log_event() | stop.
|
-spec filter_clientid(logger:log_event(), {binary(), atom()}) -> logger:log_event() | stop.
|
||||||
filter_clientid(#{meta := Meta = #{clientid := ClientId}} = Log, {MatchId, _Name}) ->
|
filter_clientid(#{meta := Meta = #{clientid := ClientId}} = Log, {MatchId, _Name}) ->
|
||||||
filter_ret(ClientId =:= MatchId andalso is_trace(Meta), Log);
|
filter_ret(ClientId =:= MatchId andalso is_trace(Meta), Log);
|
||||||
filter_clientid(_Log, _ExpectId) -> stop.
|
filter_clientid(_Log, _ExpectId) ->
|
||||||
|
stop.
|
||||||
|
|
||||||
-spec filter_topic(logger:log_event(), {binary(), atom()}) -> logger:log_event() | stop.
|
-spec filter_topic(logger:log_event(), {binary(), atom()}) -> logger:log_event() | stop.
|
||||||
filter_topic(#{meta := Meta = #{topic := Topic}} = Log, {TopicFilter, _Name}) ->
|
filter_topic(#{meta := Meta = #{topic := Topic}} = Log, {TopicFilter, _Name}) ->
|
||||||
filter_ret(is_trace(Meta) andalso emqx_topic:match(Topic, TopicFilter), Log);
|
filter_ret(is_trace(Meta) andalso emqx_topic:match(Topic, TopicFilter), Log);
|
||||||
filter_topic(_Log, _ExpectId) -> stop.
|
filter_topic(_Log, _ExpectId) ->
|
||||||
|
stop.
|
||||||
|
|
||||||
-spec filter_ip_address(logger:log_event(), {string(), atom()}) -> logger:log_event() | stop.
|
-spec filter_ip_address(logger:log_event(), {string(), atom()}) -> logger:log_event() | stop.
|
||||||
filter_ip_address(#{meta := Meta = #{peername := Peername}} = Log, {IP, _Name}) ->
|
filter_ip_address(#{meta := Meta = #{peername := Peername}} = Log, {IP, _Name}) ->
|
||||||
filter_ret(is_trace(Meta) andalso lists:prefix(IP, Peername), Log);
|
filter_ret(is_trace(Meta) andalso lists:prefix(IP, Peername), Log);
|
||||||
filter_ip_address(_Log, _ExpectId) -> stop.
|
filter_ip_address(_Log, _ExpectId) ->
|
||||||
|
stop.
|
||||||
|
|
||||||
-compile({inline, [is_trace/1, filter_ret/2]}).
|
-compile({inline, [is_trace/1, filter_ret/2]}).
|
||||||
%% TRUE when is_trace is missing.
|
%% TRUE when is_trace is missing.
|
||||||
|
|
@ -150,16 +161,14 @@ filters(#{type := ip_address, filter := Filter, name := Name}) ->
|
||||||
[{ip_address, {fun ?MODULE:filter_ip_address/2, {ensure_list(Filter), Name}}}].
|
[{ip_address, {fun ?MODULE:filter_ip_address/2, {ensure_list(Filter), Name}}}].
|
||||||
|
|
||||||
formatter(#{type := _Type}) ->
|
formatter(#{type := _Type}) ->
|
||||||
{emqx_trace_formatter,
|
{emqx_trace_formatter, #{
|
||||||
#{
|
%% template is for ?SLOG message not ?TRACE.
|
||||||
%% template is for ?SLOG message not ?TRACE.
|
template => [time, " [", level, "] ", msg, "\n"],
|
||||||
template => [time," [",level,"] ", msg,"\n"],
|
single_line => true,
|
||||||
single_line => true,
|
max_size => unlimited,
|
||||||
max_size => unlimited,
|
depth => unlimited,
|
||||||
depth => unlimited,
|
payload_encode => payload_encode()
|
||||||
payload_encode => payload_encode()
|
}}.
|
||||||
}
|
|
||||||
}.
|
|
||||||
|
|
||||||
filter_traces(#{id := Id, level := Level, dst := Dst, filters := Filters}, Acc) ->
|
filter_traces(#{id := Id, level := Level, dst := Dst, filters := Filters}, Acc) ->
|
||||||
Init = #{id => Id, level => Level, dst => Dst},
|
Init = #{id => Id, level => Level, dst => Dst},
|
||||||
|
|
@ -167,7 +176,8 @@ filter_traces(#{id := Id, level := Level, dst := Dst, filters := Filters}, Acc)
|
||||||
[{Type, {FilterFun, {Filter, Name}}}] when
|
[{Type, {FilterFun, {Filter, Name}}}] when
|
||||||
Type =:= topic orelse
|
Type =:= topic orelse
|
||||||
Type =:= clientid orelse
|
Type =:= clientid orelse
|
||||||
Type =:= ip_address ->
|
Type =:= ip_address
|
||||||
|
->
|
||||||
[Init#{type => Type, filter => Filter, name => Name, filter_fun => FilterFun} | Acc];
|
[Init#{type => Type, filter => Filter, name => Name, filter_fun => FilterFun} | Acc];
|
||||||
_ ->
|
_ ->
|
||||||
Acc
|
Acc
|
||||||
|
|
@ -179,7 +189,7 @@ handler_id(Name, Type) ->
|
||||||
try
|
try
|
||||||
do_handler_id(Name, Type)
|
do_handler_id(Name, Type)
|
||||||
catch
|
catch
|
||||||
_ : _ ->
|
_:_ ->
|
||||||
Hash = emqx_misc:bin2hexstr_a_f(crypto:hash(md5, Name)),
|
Hash = emqx_misc:bin2hexstr_a_f(crypto:hash(md5, Name)),
|
||||||
do_handler_id(Hash, Type)
|
do_handler_id(Hash, Type)
|
||||||
end.
|
end.
|
||||||
|
|
|
||||||
|
|
@ -18,17 +18,18 @@
|
||||||
|
|
||||||
-behaviour(emqx_bpapi).
|
-behaviour(emqx_bpapi).
|
||||||
|
|
||||||
-export([ introduced_in/0
|
-export([
|
||||||
|
introduced_in/0,
|
||||||
|
|
||||||
, forward/3
|
forward/3,
|
||||||
, forward_async/3
|
forward_async/3,
|
||||||
, list_client_subscriptions/2
|
list_client_subscriptions/2,
|
||||||
, list_subscriptions_via_topic/2
|
list_subscriptions_via_topic/2,
|
||||||
|
|
||||||
, start_listener/2
|
start_listener/2,
|
||||||
, stop_listener/2
|
stop_listener/2,
|
||||||
, restart_listener/2
|
restart_listener/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-include("bpapi.hrl").
|
-include("bpapi.hrl").
|
||||||
-include("emqx.hrl").
|
-include("emqx.hrl").
|
||||||
|
|
@ -36,8 +37,9 @@
|
||||||
introduced_in() ->
|
introduced_in() ->
|
||||||
"5.0.0".
|
"5.0.0".
|
||||||
|
|
||||||
-spec forward(node(), emqx_types:topic(), emqx_types:delivery()) -> emqx_types:deliver_result()
|
-spec forward(node(), emqx_types:topic(), emqx_types:delivery()) ->
|
||||||
| emqx_rpc:badrpc().
|
emqx_types:deliver_result()
|
||||||
|
| emqx_rpc:badrpc().
|
||||||
forward(Node, Topic, Delivery = #delivery{}) when is_binary(Topic) ->
|
forward(Node, Topic, Delivery = #delivery{}) when is_binary(Topic) ->
|
||||||
emqx_rpc:call(Topic, Node, emqx_broker, dispatch, [Topic, Delivery]).
|
emqx_rpc:call(Topic, Node, emqx_broker, dispatch, [Topic, Delivery]).
|
||||||
|
|
||||||
|
|
@ -46,8 +48,8 @@ forward_async(Node, Topic, Delivery = #delivery{}) when is_binary(Topic) ->
|
||||||
emqx_rpc:cast(Topic, Node, emqx_broker, dispatch, [Topic, Delivery]).
|
emqx_rpc:cast(Topic, Node, emqx_broker, dispatch, [Topic, Delivery]).
|
||||||
|
|
||||||
-spec list_client_subscriptions(node(), emqx_types:clientid()) ->
|
-spec list_client_subscriptions(node(), emqx_types:clientid()) ->
|
||||||
[{emqx_types:topic(), emqx_types:subopts()}]
|
[{emqx_types:topic(), emqx_types:subopts()}]
|
||||||
| emqx_rpc:badrpc().
|
| emqx_rpc:badrpc().
|
||||||
list_client_subscriptions(Node, ClientId) ->
|
list_client_subscriptions(Node, ClientId) ->
|
||||||
rpc:call(Node, emqx_broker, subscriptions, [ClientId]).
|
rpc:call(Node, emqx_broker, subscriptions, [ClientId]).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,18 +18,19 @@
|
||||||
|
|
||||||
-behaviour(emqx_bpapi).
|
-behaviour(emqx_bpapi).
|
||||||
|
|
||||||
-export([ introduced_in/0
|
-export([
|
||||||
|
introduced_in/0,
|
||||||
|
|
||||||
, lookup_client/2
|
lookup_client/2,
|
||||||
, kickout_client/2
|
kickout_client/2,
|
||||||
|
|
||||||
, get_chan_stats/2
|
get_chan_stats/2,
|
||||||
, get_chan_info/2
|
get_chan_info/2,
|
||||||
, get_chann_conn_mod/2
|
get_chann_conn_mod/2,
|
||||||
|
|
||||||
, takeover_session/2
|
takeover_session/2,
|
||||||
, kick_session/3
|
kick_session/3
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-include("bpapi.hrl").
|
-include("bpapi.hrl").
|
||||||
-include("src/emqx_cm.hrl").
|
-include("src/emqx_cm.hrl").
|
||||||
|
|
@ -42,7 +43,7 @@ kickout_client(Node, ClientId) ->
|
||||||
rpc:call(Node, emqx_cm, kick_session, [ClientId]).
|
rpc:call(Node, emqx_cm, kick_session, [ClientId]).
|
||||||
|
|
||||||
-spec lookup_client(node(), {clientid, emqx_types:clientid()} | {username, emqx_types:username()}) ->
|
-spec lookup_client(node(), {clientid, emqx_types:clientid()} | {username, emqx_types:username()}) ->
|
||||||
[emqx_cm:channel_info()] | {badrpc, _}.
|
[emqx_cm:channel_info()] | {badrpc, _}.
|
||||||
lookup_client(Node, Key) ->
|
lookup_client(Node, Key) ->
|
||||||
rpc:call(Node, emqx_cm, lookup_client, [Key]).
|
rpc:call(Node, emqx_cm, lookup_client, [Key]).
|
||||||
|
|
||||||
|
|
@ -54,15 +55,16 @@ get_chan_stats(ClientId, ChanPid) ->
|
||||||
get_chan_info(ClientId, ChanPid) ->
|
get_chan_info(ClientId, ChanPid) ->
|
||||||
rpc:call(node(ChanPid), emqx_cm, do_get_chan_info, [ClientId, ChanPid], ?T_GET_INFO * 2).
|
rpc:call(node(ChanPid), emqx_cm, do_get_chan_info, [ClientId, ChanPid], ?T_GET_INFO * 2).
|
||||||
|
|
||||||
-spec get_chann_conn_mod(emqx_types:clientid(), emqx_cm:chan_pid()) -> module() | undefined | {badrpc, _}.
|
-spec get_chann_conn_mod(emqx_types:clientid(), emqx_cm:chan_pid()) ->
|
||||||
|
module() | undefined | {badrpc, _}.
|
||||||
get_chann_conn_mod(ClientId, ChanPid) ->
|
get_chann_conn_mod(ClientId, ChanPid) ->
|
||||||
rpc:call(node(ChanPid), emqx_cm, do_get_chann_conn_mod, [ClientId, ChanPid], ?T_GET_INFO * 2).
|
rpc:call(node(ChanPid), emqx_cm, do_get_chann_conn_mod, [ClientId, ChanPid], ?T_GET_INFO * 2).
|
||||||
|
|
||||||
-spec takeover_session(emqx_types:clientid(), emqx_cm:chan_pid()) ->
|
-spec takeover_session(emqx_types:clientid(), emqx_cm:chan_pid()) ->
|
||||||
none
|
none
|
||||||
| {expired | persistent, emqx_session:session()}
|
| {expired | persistent, emqx_session:session()}
|
||||||
| {living, _ConnMod :: atom(), emqx_cm:chan_pid(), emqx_session:session()}
|
| {living, _ConnMod :: atom(), emqx_cm:chan_pid(), emqx_session:session()}
|
||||||
| {badrpc, _}.
|
| {badrpc, _}.
|
||||||
takeover_session(ClientId, ChanPid) ->
|
takeover_session(ClientId, ChanPid) ->
|
||||||
rpc:call(node(ChanPid), emqx_cm, takeover_session, [ClientId, ChanPid], ?T_TAKEOVER * 2).
|
rpc:call(node(ChanPid), emqx_cm, takeover_session, [ClientId, ChanPid], ?T_TAKEOVER * 2).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,10 +18,11 @@
|
||||||
|
|
||||||
-behaviour(emqx_bpapi).
|
-behaviour(emqx_bpapi).
|
||||||
|
|
||||||
-export([ introduced_in/0
|
-export([
|
||||||
, resume_begin/3
|
introduced_in/0,
|
||||||
, resume_end/3
|
resume_begin/3,
|
||||||
]).
|
resume_end/3
|
||||||
|
]).
|
||||||
|
|
||||||
-include("bpapi.hrl").
|
-include("bpapi.hrl").
|
||||||
-include("emqx.hrl").
|
-include("emqx.hrl").
|
||||||
|
|
@ -30,11 +31,11 @@ introduced_in() ->
|
||||||
"5.0.0".
|
"5.0.0".
|
||||||
|
|
||||||
-spec resume_begin([node()], pid(), binary()) ->
|
-spec resume_begin([node()], pid(), binary()) ->
|
||||||
emqx_rpc:erpc_multicall([{node(), emqx_guid:guid()}]).
|
emqx_rpc:erpc_multicall([{node(), emqx_guid:guid()}]).
|
||||||
resume_begin(Nodes, Pid, SessionID) when is_pid(Pid), is_binary(SessionID) ->
|
resume_begin(Nodes, Pid, SessionID) when is_pid(Pid), is_binary(SessionID) ->
|
||||||
erpc:multicall(Nodes, emqx_session_router, resume_begin, [Pid, SessionID]).
|
erpc:multicall(Nodes, emqx_session_router, resume_begin, [Pid, SessionID]).
|
||||||
|
|
||||||
-spec resume_end([node()], pid(), binary()) ->
|
-spec resume_end([node()], pid(), binary()) ->
|
||||||
emqx_rpc:erpc_multicall({'ok', [emqx_types:message()]} | {'error', term()}).
|
emqx_rpc:erpc_multicall({'ok', [emqx_types:message()]} | {'error', term()}).
|
||||||
resume_end(Nodes, Pid, SessionID) when is_pid(Pid), is_binary(SessionID) ->
|
resume_end(Nodes, Pid, SessionID) when is_pid(Pid), is_binary(SessionID) ->
|
||||||
erpc:multicall(Nodes, emqx_session_router, resume_end, [Pid, SessionID]).
|
erpc:multicall(Nodes, emqx_session_router, resume_end, [Pid, SessionID]).
|
||||||
|
|
|
||||||
|
|
@ -20,20 +20,21 @@
|
||||||
|
|
||||||
-include("bpapi.hrl").
|
-include("bpapi.hrl").
|
||||||
|
|
||||||
-export([ introduced_in/0
|
-export([
|
||||||
|
introduced_in/0,
|
||||||
|
|
||||||
, is_running/1
|
is_running/1,
|
||||||
|
|
||||||
, get_alarms/2
|
get_alarms/2,
|
||||||
, get_stats/1
|
get_stats/1,
|
||||||
, get_metrics/1
|
get_metrics/1,
|
||||||
|
|
||||||
, deactivate_alarm/2
|
deactivate_alarm/2,
|
||||||
, delete_all_deactivated_alarms/1
|
delete_all_deactivated_alarms/1,
|
||||||
|
|
||||||
, clean_authz_cache/1
|
clean_authz_cache/1,
|
||||||
, clean_authz_cache/2
|
clean_authz_cache/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
introduced_in() ->
|
introduced_in() ->
|
||||||
"5.0.0".
|
"5.0.0".
|
||||||
|
|
@ -55,9 +56,9 @@ get_metrics(Node) ->
|
||||||
rpc:call(Node, emqx_metrics, all, []).
|
rpc:call(Node, emqx_metrics, all, []).
|
||||||
|
|
||||||
-spec clean_authz_cache(node(), emqx_types:clientid()) ->
|
-spec clean_authz_cache(node(), emqx_types:clientid()) ->
|
||||||
ok
|
ok
|
||||||
| {error, not_found}
|
| {error, not_found}
|
||||||
| {badrpc, _}.
|
| {badrpc, _}.
|
||||||
clean_authz_cache(Node, ClientId) ->
|
clean_authz_cache(Node, ClientId) ->
|
||||||
rpc:call(Node, emqx_authz_cache, drain_cache, [ClientId]).
|
rpc:call(Node, emqx_authz_cache, drain_cache, [ClientId]).
|
||||||
|
|
||||||
|
|
@ -66,7 +67,7 @@ clean_authz_cache(Node) ->
|
||||||
rpc:call(Node, emqx_authz_cache, drain_cache, []).
|
rpc:call(Node, emqx_authz_cache, drain_cache, []).
|
||||||
|
|
||||||
-spec deactivate_alarm(node(), binary() | atom()) ->
|
-spec deactivate_alarm(node(), binary() | atom()) ->
|
||||||
ok | {error, not_found} | {badrpc, _}.
|
ok | {error, not_found} | {badrpc, _}.
|
||||||
deactivate_alarm(Node, Name) ->
|
deactivate_alarm(Node, Name) ->
|
||||||
rpc:call(Node, emqx_alarm, deactivate, [Name]).
|
rpc:call(Node, emqx_alarm, deactivate, [Name]).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,18 +23,24 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
prop_symmetric() ->
|
prop_symmetric() ->
|
||||||
?FORALL(Data, raw_data(),
|
?FORALL(
|
||||||
|
Data,
|
||||||
|
raw_data(),
|
||||||
begin
|
begin
|
||||||
Encoded = emqx_base62:encode(Data),
|
Encoded = emqx_base62:encode(Data),
|
||||||
to_binary(Data) =:= emqx_base62:decode(Encoded)
|
to_binary(Data) =:= emqx_base62:decode(Encoded)
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_size() ->
|
prop_size() ->
|
||||||
?FORALL(Data, binary(),
|
?FORALL(
|
||||||
begin
|
Data,
|
||||||
Encoded = emqx_base62:encode(Data),
|
binary(),
|
||||||
base62_size(Data, Encoded)
|
begin
|
||||||
end).
|
Encoded = emqx_base62:encode(Data),
|
||||||
|
base62_size(Data, Encoded)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Helpers
|
%% Helpers
|
||||||
|
|
@ -59,7 +65,7 @@ base62_size(Data, Encoded) ->
|
||||||
RangeEnd = DataSize div 3 * 8,
|
RangeEnd = DataSize div 3 * 8,
|
||||||
EncodedSize >= RangeStart andalso EncodedSize =< RangeEnd;
|
EncodedSize >= RangeStart andalso EncodedSize =< RangeEnd;
|
||||||
_Rem ->
|
_Rem ->
|
||||||
RangeStart = DataSize * 8 div 6 + 1,
|
RangeStart = DataSize * 8 div 6 + 1,
|
||||||
RangeEnd = DataSize * 8 div 6 * 2 + 1,
|
RangeEnd = DataSize * 8 div 6 * 2 + 1,
|
||||||
EncodedSize >= RangeStart andalso EncodedSize =< RangeEnd
|
EncodedSize >= RangeStart andalso EncodedSize =< RangeEnd
|
||||||
end.
|
end.
|
||||||
|
|
|
||||||
|
|
@ -24,24 +24,27 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
prop_serialize_parse_connect() ->
|
prop_serialize_parse_connect() ->
|
||||||
?FORALL(Opts = #{version := ProtoVer}, parse_opts(),
|
?FORALL(
|
||||||
begin
|
Opts = #{version := ProtoVer},
|
||||||
ProtoName = proplists:get_value(ProtoVer, ?PROTOCOL_NAMES),
|
parse_opts(),
|
||||||
Packet = ?CONNECT_PACKET(#mqtt_packet_connect{
|
begin
|
||||||
proto_name = ProtoName,
|
ProtoName = proplists:get_value(ProtoVer, ?PROTOCOL_NAMES),
|
||||||
proto_ver = ProtoVer,
|
Packet = ?CONNECT_PACKET(#mqtt_packet_connect{
|
||||||
clientid = <<"clientId">>,
|
proto_name = ProtoName,
|
||||||
will_qos = ?QOS_1,
|
proto_ver = ProtoVer,
|
||||||
will_flag = true,
|
clientid = <<"clientId">>,
|
||||||
will_retain = true,
|
will_qos = ?QOS_1,
|
||||||
will_topic = <<"will">>,
|
will_flag = true,
|
||||||
will_props = #{},
|
will_retain = true,
|
||||||
will_payload = <<"bye">>,
|
will_topic = <<"will">>,
|
||||||
clean_start = true,
|
will_props = #{},
|
||||||
properties = #{}
|
will_payload = <<"bye">>,
|
||||||
}),
|
clean_start = true,
|
||||||
Packet =:= parse_serialize(Packet, Opts)
|
properties = #{}
|
||||||
end).
|
}),
|
||||||
|
Packet =:= parse_serialize(Packet, Opts)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Helpers
|
%% Helpers
|
||||||
|
|
@ -59,4 +62,4 @@ parse_serialize(Packet, Opts) when is_map(Opts) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
parse_opts() ->
|
parse_opts() ->
|
||||||
?LET(PropList, [{strict_mode, boolean()}, {version, range(4,5)}], maps:from_list(PropList)).
|
?LET(PropList, [{strict_mode, boolean()}, {version, range(4, 5)}], maps:from_list(PropList)).
|
||||||
|
|
|
||||||
|
|
@ -16,14 +16,17 @@
|
||||||
|
|
||||||
-module(prop_emqx_json).
|
-module(prop_emqx_json).
|
||||||
|
|
||||||
-import(emqx_json,
|
-import(
|
||||||
[ decode/1
|
emqx_json,
|
||||||
, decode/2
|
[
|
||||||
, encode/1
|
decode/1,
|
||||||
, safe_decode/1
|
decode/2,
|
||||||
, safe_decode/2
|
encode/1,
|
||||||
, safe_encode/1
|
safe_decode/1,
|
||||||
]).
|
safe_decode/2,
|
||||||
|
safe_encode/1
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
||||||
-include_lib("proper/include/proper.hrl").
|
-include_lib("proper/include/proper.hrl").
|
||||||
|
|
||||||
|
|
@ -32,99 +35,133 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
prop_json_basic() ->
|
prop_json_basic() ->
|
||||||
?FORALL(T, json_basic(),
|
?FORALL(
|
||||||
begin
|
T,
|
||||||
{ok, J} = safe_encode(T),
|
json_basic(),
|
||||||
{ok, T} = safe_decode(J),
|
begin
|
||||||
T = decode(encode(T)),
|
{ok, J} = safe_encode(T),
|
||||||
true
|
{ok, T} = safe_decode(J),
|
||||||
end).
|
T = decode(encode(T)),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_json_basic_atom() ->
|
prop_json_basic_atom() ->
|
||||||
?FORALL(T0, latin_atom(),
|
?FORALL(
|
||||||
begin
|
T0,
|
||||||
T = atom_to_binary(T0, utf8),
|
latin_atom(),
|
||||||
{ok, J} = safe_encode(T0),
|
begin
|
||||||
{ok, T} = safe_decode(J),
|
T = atom_to_binary(T0, utf8),
|
||||||
T = decode(encode(T0)),
|
{ok, J} = safe_encode(T0),
|
||||||
true
|
{ok, T} = safe_decode(J),
|
||||||
end).
|
T = decode(encode(T0)),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_object_proplist_to_proplist() ->
|
prop_object_proplist_to_proplist() ->
|
||||||
?FORALL(T, json_object(),
|
?FORALL(
|
||||||
begin
|
T,
|
||||||
{ok, J} = safe_encode(T),
|
json_object(),
|
||||||
{ok, T} = safe_decode(J),
|
begin
|
||||||
T = decode(encode(T)),
|
{ok, J} = safe_encode(T),
|
||||||
true
|
{ok, T} = safe_decode(J),
|
||||||
end).
|
T = decode(encode(T)),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_object_map_to_map() ->
|
prop_object_map_to_map() ->
|
||||||
?FORALL(T, json_object_map(),
|
?FORALL(
|
||||||
begin
|
T,
|
||||||
{ok, J} = safe_encode(T),
|
json_object_map(),
|
||||||
{ok, T} = safe_decode(J, [return_maps]),
|
begin
|
||||||
T = decode(encode(T), [return_maps]),
|
{ok, J} = safe_encode(T),
|
||||||
true
|
{ok, T} = safe_decode(J, [return_maps]),
|
||||||
end).
|
T = decode(encode(T), [return_maps]),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%% The duplicated key will be overridden
|
%% The duplicated key will be overridden
|
||||||
prop_object_proplist_to_map() ->
|
prop_object_proplist_to_map() ->
|
||||||
?FORALL(T0, json_object(),
|
?FORALL(
|
||||||
begin
|
T0,
|
||||||
T = to_map(T0),
|
json_object(),
|
||||||
{ok, J} = safe_encode(T0),
|
begin
|
||||||
{ok, T} = safe_decode(J, [return_maps]),
|
T = to_map(T0),
|
||||||
T = decode(encode(T0), [return_maps]),
|
{ok, J} = safe_encode(T0),
|
||||||
true
|
{ok, T} = safe_decode(J, [return_maps]),
|
||||||
end).
|
T = decode(encode(T0), [return_maps]),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_object_map_to_proplist() ->
|
prop_object_map_to_proplist() ->
|
||||||
?FORALL(T0, json_object_map(),
|
?FORALL(
|
||||||
begin
|
T0,
|
||||||
%% jiffy encode a map with descending order, that is,
|
json_object_map(),
|
||||||
%% it is opposite with maps traversal sequence
|
begin
|
||||||
%% see: the `to_list` implementation
|
%% jiffy encode a map with descending order, that is,
|
||||||
T = to_list(T0),
|
%% it is opposite with maps traversal sequence
|
||||||
{ok, J} = safe_encode(T0),
|
%% see: the `to_list` implementation
|
||||||
{ok, T} = safe_decode(J),
|
T = to_list(T0),
|
||||||
T = decode(encode(T0)),
|
{ok, J} = safe_encode(T0),
|
||||||
true
|
{ok, T} = safe_decode(J),
|
||||||
end).
|
T = decode(encode(T0)),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_safe_encode() ->
|
prop_safe_encode() ->
|
||||||
?FORALL(T, invalid_json_term(),
|
?FORALL(
|
||||||
begin
|
T,
|
||||||
{error, _} = safe_encode(T), true
|
invalid_json_term(),
|
||||||
end).
|
begin
|
||||||
|
{error, _} = safe_encode(T),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_safe_decode() ->
|
prop_safe_decode() ->
|
||||||
?FORALL(T, invalid_json_str(),
|
?FORALL(
|
||||||
begin
|
T,
|
||||||
{error, _} = safe_decode(T), true
|
invalid_json_str(),
|
||||||
end).
|
begin
|
||||||
|
{error, _} = safe_decode(T),
|
||||||
|
true
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Helpers
|
%% Helpers
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
to_map([{_, _}|_] = L) ->
|
to_map([{_, _} | _] = L) ->
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun({Name, Value}, Acc) ->
|
fun({Name, Value}, Acc) ->
|
||||||
Acc#{Name => to_map(Value)}
|
Acc#{Name => to_map(Value)}
|
||||||
end, #{}, L);
|
end,
|
||||||
|
#{},
|
||||||
|
L
|
||||||
|
);
|
||||||
to_map(L) when is_list(L) ->
|
to_map(L) when is_list(L) ->
|
||||||
[to_map(E) || E <- L];
|
[to_map(E) || E <- L];
|
||||||
to_map(T) -> T.
|
to_map(T) ->
|
||||||
|
T.
|
||||||
|
|
||||||
to_list(L) when is_list(L) ->
|
to_list(L) when is_list(L) ->
|
||||||
[to_list(E) || E <- L];
|
[to_list(E) || E <- L];
|
||||||
to_list(M) when is_map(M) ->
|
to_list(M) when is_map(M) ->
|
||||||
maps:fold(
|
maps:fold(
|
||||||
fun(K, V, Acc) ->
|
fun(K, V, Acc) ->
|
||||||
[{K, to_list(V)}|Acc]
|
[{K, to_list(V)} | Acc]
|
||||||
end, [], M);
|
end,
|
||||||
to_list(T) -> T.
|
[],
|
||||||
|
M
|
||||||
|
);
|
||||||
|
to_list(T) ->
|
||||||
|
T.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Generators (https://tools.ietf.org/html/rfc8259)
|
%% Generators (https://tools.ietf.org/html/rfc8259)
|
||||||
|
|
@ -140,8 +177,14 @@ latin_atom() ->
|
||||||
json_string() -> utf8().
|
json_string() -> utf8().
|
||||||
|
|
||||||
json_object() ->
|
json_object() ->
|
||||||
oneof([json_array_1(), json_object_1(), json_array_object_1(),
|
oneof([
|
||||||
json_array_2(), json_object_2(), json_array_object_2()]).
|
json_array_1(),
|
||||||
|
json_object_1(),
|
||||||
|
json_array_object_1(),
|
||||||
|
json_array_2(),
|
||||||
|
json_object_2(),
|
||||||
|
json_array_object_2()
|
||||||
|
]).
|
||||||
|
|
||||||
json_object_map() ->
|
json_object_map() ->
|
||||||
?LET(L, json_object(), to_map(L)).
|
?LET(L, json_object(), to_map(L)).
|
||||||
|
|
@ -156,9 +199,14 @@ json_object_1() ->
|
||||||
list({json_key(), json_basic()}).
|
list({json_key(), json_basic()}).
|
||||||
|
|
||||||
json_object_2() ->
|
json_object_2() ->
|
||||||
list({json_key(), oneof([json_basic(),
|
list({
|
||||||
json_array_1(),
|
json_key(),
|
||||||
json_object_1()])}).
|
oneof([
|
||||||
|
json_basic(),
|
||||||
|
json_array_1(),
|
||||||
|
json_object_1()
|
||||||
|
])
|
||||||
|
}).
|
||||||
|
|
||||||
json_array_object_1() ->
|
json_array_object_1() ->
|
||||||
list(json_object_1()).
|
list(json_object_1()).
|
||||||
|
|
@ -186,5 +234,4 @@ chaos(S, 0, _) ->
|
||||||
chaos(S, N, T) ->
|
chaos(S, N, T) ->
|
||||||
I = rand:uniform(length(S)),
|
I = rand:uniform(length(S)),
|
||||||
{L1, L2} = lists:split(I, S),
|
{L1, L2} = lists:split(I, S),
|
||||||
chaos(lists:flatten([L1, lists:nth(rand:uniform(length(T)), T), L2]), N-1, T).
|
chaos(lists:flatten([L1, lists:nth(rand:uniform(length(T)), T), L2]), N - 1, T).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,31 +14,36 @@
|
||||||
%% limitations under the License.
|
%% limitations under the License.
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
-module(prop_emqx_psk).
|
-module(prop_emqx_psk).
|
||||||
|
|
||||||
-include_lib("proper/include/proper.hrl").
|
-include_lib("proper/include/proper.hrl").
|
||||||
|
|
||||||
-define(ALL(Vars, Types, Exprs),
|
-define(ALL(Vars, Types, Exprs),
|
||||||
?SETUP(fun() ->
|
?SETUP(
|
||||||
|
fun() ->
|
||||||
State = do_setup(),
|
State = do_setup(),
|
||||||
fun() -> do_teardown(State) end
|
fun() -> do_teardown(State) end
|
||||||
end, ?FORALL(Vars, Types, Exprs))).
|
end,
|
||||||
|
?FORALL(Vars, Types, Exprs)
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Properties
|
%% Properties
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
prop_lookup() ->
|
prop_lookup() ->
|
||||||
?ALL({ClientPSKID, UserState},
|
?ALL(
|
||||||
{client_pskid(), user_state()},
|
{ClientPSKID, UserState},
|
||||||
begin
|
{client_pskid(), user_state()},
|
||||||
case emqx_tls_psk:lookup(psk, ClientPSKID, UserState) of
|
begin
|
||||||
{ok, _Result} -> true;
|
case emqx_tls_psk:lookup(psk, ClientPSKID, UserState) of
|
||||||
error -> true;
|
{ok, _Result} -> true;
|
||||||
_Other -> false
|
error -> true;
|
||||||
end
|
_Other -> false
|
||||||
end).
|
end
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Helper
|
%% Helper
|
||||||
|
|
@ -47,10 +52,13 @@ prop_lookup() ->
|
||||||
do_setup() ->
|
do_setup() ->
|
||||||
ok = emqx_logger:set_log_level(emergency),
|
ok = emqx_logger:set_log_level(emergency),
|
||||||
ok = meck:new(emqx_hooks, [passthrough, no_history]),
|
ok = meck:new(emqx_hooks, [passthrough, no_history]),
|
||||||
ok = meck:expect(emqx_hooks, run_fold,
|
ok = meck:expect(
|
||||||
fun('tls_handshake.psk_lookup', [ClientPSKID], not_found) ->
|
emqx_hooks,
|
||||||
unicode:characters_to_binary(ClientPSKID)
|
run_fold,
|
||||||
end).
|
fun('tls_handshake.psk_lookup', [ClientPSKID], not_found) ->
|
||||||
|
unicode:characters_to_binary(ClientPSKID)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
do_teardown(_) ->
|
do_teardown(_) ->
|
||||||
ok = emqx_logger:set_log_level(error),
|
ok = emqx_logger:set_log_level(error),
|
||||||
|
|
@ -63,4 +71,3 @@ do_teardown(_) ->
|
||||||
client_pskid() -> oneof([string(), integer(), [1, [-1]]]).
|
client_pskid() -> oneof([string(), integer(), [1, [-1]]]).
|
||||||
|
|
||||||
user_state() -> term().
|
user_state() -> term().
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,20 +24,29 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
prop_name_text() ->
|
prop_name_text() ->
|
||||||
?FORALL(UnionArgs, union_args(),
|
?FORALL(
|
||||||
is_atom(apply_fun(name, UnionArgs)) andalso
|
UnionArgs,
|
||||||
is_binary(apply_fun(text, UnionArgs))).
|
union_args(),
|
||||||
|
is_atom(apply_fun(name, UnionArgs)) andalso
|
||||||
|
is_binary(apply_fun(text, UnionArgs))
|
||||||
|
).
|
||||||
|
|
||||||
prop_compat() ->
|
prop_compat() ->
|
||||||
?FORALL(CompatArgs, compat_args(),
|
?FORALL(
|
||||||
begin
|
CompatArgs,
|
||||||
Result = apply_fun(compat, CompatArgs),
|
compat_args(),
|
||||||
is_number(Result) orelse Result =:= undefined
|
begin
|
||||||
end).
|
Result = apply_fun(compat, CompatArgs),
|
||||||
|
is_number(Result) orelse Result =:= undefined
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_connack_error() ->
|
prop_connack_error() ->
|
||||||
?FORALL(CONNACK_ERROR_ARGS, connack_error_args(),
|
?FORALL(
|
||||||
is_integer(apply_fun(connack_error, CONNACK_ERROR_ARGS))).
|
CONNACK_ERROR_ARGS,
|
||||||
|
connack_error_args(),
|
||||||
|
is_integer(apply_fun(connack_error, CONNACK_ERROR_ARGS))
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Helper
|
%% Helper
|
||||||
|
|
@ -51,45 +60,60 @@ apply_fun(Fun, Args) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
union_args() ->
|
union_args() ->
|
||||||
frequency([{6, [real_mqttv3_rc(), mqttv3_version()]},
|
frequency([
|
||||||
{43, [real_mqttv5_rc(), mqttv5_version()]}]).
|
{6, [real_mqttv3_rc(), mqttv3_version()]},
|
||||||
|
{43, [real_mqttv5_rc(), mqttv5_version()]}
|
||||||
|
]).
|
||||||
|
|
||||||
compat_args() ->
|
compat_args() ->
|
||||||
frequency([{18, [connack, compat_rc()]},
|
frequency([
|
||||||
{2, [suback, compat_rc()]},
|
{18, [connack, compat_rc()]},
|
||||||
{1, [unsuback, compat_rc()]}]).
|
{2, [suback, compat_rc()]},
|
||||||
|
{1, [unsuback, compat_rc()]}
|
||||||
|
]).
|
||||||
|
|
||||||
connack_error_args() ->
|
connack_error_args() ->
|
||||||
[frequency([{10, connack_error()},
|
[
|
||||||
{1, unexpected_connack_error()}])].
|
frequency([
|
||||||
|
{10, connack_error()},
|
||||||
|
{1, unexpected_connack_error()}
|
||||||
|
])
|
||||||
|
].
|
||||||
|
|
||||||
connack_error() ->
|
connack_error() ->
|
||||||
oneof([client_identifier_not_valid,
|
oneof([
|
||||||
bad_username_or_password,
|
client_identifier_not_valid,
|
||||||
bad_clientid_or_password,
|
bad_username_or_password,
|
||||||
username_or_password_undefined,
|
bad_clientid_or_password,
|
||||||
password_error,
|
username_or_password_undefined,
|
||||||
not_authorized,
|
password_error,
|
||||||
server_unavailable,
|
not_authorized,
|
||||||
server_busy,
|
server_unavailable,
|
||||||
banned,
|
server_busy,
|
||||||
bad_authentication_method]).
|
banned,
|
||||||
|
bad_authentication_method
|
||||||
|
]).
|
||||||
|
|
||||||
unexpected_connack_error() ->
|
unexpected_connack_error() ->
|
||||||
oneof([who_knows]).
|
oneof([who_knows]).
|
||||||
|
|
||||||
|
|
||||||
real_mqttv3_rc() ->
|
real_mqttv3_rc() ->
|
||||||
frequency([{6, mqttv3_rc()},
|
frequency([
|
||||||
{1, unexpected_rc()}]).
|
{6, mqttv3_rc()},
|
||||||
|
{1, unexpected_rc()}
|
||||||
|
]).
|
||||||
|
|
||||||
real_mqttv5_rc() ->
|
real_mqttv5_rc() ->
|
||||||
frequency([{43, mqttv5_rc()},
|
frequency([
|
||||||
{2, unexpected_rc()}]).
|
{43, mqttv5_rc()},
|
||||||
|
{2, unexpected_rc()}
|
||||||
|
]).
|
||||||
|
|
||||||
compat_rc() ->
|
compat_rc() ->
|
||||||
frequency([{95, ?SUCHTHAT(RC , mqttv5_rc(), RC >= 16#80 orelse RC =< 2)},
|
frequency([
|
||||||
{5, unexpected_rc()}]).
|
{95, ?SUCHTHAT(RC, mqttv5_rc(), RC >= 16#80 orelse RC =< 2)},
|
||||||
|
{5, unexpected_rc()}
|
||||||
|
]).
|
||||||
|
|
||||||
mqttv3_rc() ->
|
mqttv3_rc() ->
|
||||||
oneof(mqttv3_rcs()).
|
oneof(mqttv3_rcs()).
|
||||||
|
|
@ -104,12 +128,51 @@ mqttv3_rcs() ->
|
||||||
[0, 1, 2, 3, 4, 5].
|
[0, 1, 2, 3, 4, 5].
|
||||||
|
|
||||||
mqttv5_rcs() ->
|
mqttv5_rcs() ->
|
||||||
[16#00, 16#01, 16#02, 16#04, 16#10, 16#11, 16#18, 16#19,
|
[
|
||||||
16#80, 16#81, 16#82, 16#83, 16#84, 16#85, 16#86, 16#87,
|
16#00,
|
||||||
16#88, 16#89, 16#8A, 16#8B, 16#8C, 16#8D, 16#8E, 16#8F,
|
16#01,
|
||||||
16#90, 16#91, 16#92, 16#93, 16#94, 16#95, 16#96, 16#97,
|
16#02,
|
||||||
16#98, 16#99, 16#9A, 16#9B, 16#9C, 16#9D, 16#9E, 16#9F,
|
16#04,
|
||||||
16#A0, 16#A1, 16#A2].
|
16#10,
|
||||||
|
16#11,
|
||||||
|
16#18,
|
||||||
|
16#19,
|
||||||
|
16#80,
|
||||||
|
16#81,
|
||||||
|
16#82,
|
||||||
|
16#83,
|
||||||
|
16#84,
|
||||||
|
16#85,
|
||||||
|
16#86,
|
||||||
|
16#87,
|
||||||
|
16#88,
|
||||||
|
16#89,
|
||||||
|
16#8A,
|
||||||
|
16#8B,
|
||||||
|
16#8C,
|
||||||
|
16#8D,
|
||||||
|
16#8E,
|
||||||
|
16#8F,
|
||||||
|
16#90,
|
||||||
|
16#91,
|
||||||
|
16#92,
|
||||||
|
16#93,
|
||||||
|
16#94,
|
||||||
|
16#95,
|
||||||
|
16#96,
|
||||||
|
16#97,
|
||||||
|
16#98,
|
||||||
|
16#99,
|
||||||
|
16#9A,
|
||||||
|
16#9B,
|
||||||
|
16#9C,
|
||||||
|
16#9D,
|
||||||
|
16#9E,
|
||||||
|
16#9F,
|
||||||
|
16#A0,
|
||||||
|
16#A1,
|
||||||
|
16#A2
|
||||||
|
].
|
||||||
|
|
||||||
unexpected_rcs() ->
|
unexpected_rcs() ->
|
||||||
ReasonCodes = mqttv3_rcs() ++ mqttv5_rcs(),
|
ReasonCodes = mqttv3_rcs() ++ mqttv5_rcs(),
|
||||||
|
|
|
||||||
|
|
@ -22,64 +22,84 @@
|
||||||
-define(NODENAME, 'test@127.0.0.1').
|
-define(NODENAME, 'test@127.0.0.1').
|
||||||
|
|
||||||
-define(ALL(Vars, Types, Exprs),
|
-define(ALL(Vars, Types, Exprs),
|
||||||
?SETUP(fun() ->
|
?SETUP(
|
||||||
|
fun() ->
|
||||||
State = do_setup(),
|
State = do_setup(),
|
||||||
fun() -> do_teardown(State) end
|
fun() -> do_teardown(State) end
|
||||||
end, ?FORALL(Vars, Types, Exprs))).
|
end,
|
||||||
|
?FORALL(Vars, Types, Exprs)
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Properties
|
%% Properties
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
prop_node() ->
|
prop_node() ->
|
||||||
?ALL(Node0, nodename(),
|
?ALL(
|
||||||
begin
|
Node0,
|
||||||
Node = punch(Node0),
|
nodename(),
|
||||||
?assert(emqx_rpc:cast(Node, erlang, system_time, [])),
|
begin
|
||||||
case emqx_rpc:call(Node, erlang, system_time, []) of
|
Node = punch(Node0),
|
||||||
{badrpc, _Reason} -> true;
|
?assert(emqx_rpc:cast(Node, erlang, system_time, [])),
|
||||||
Delivery when is_integer(Delivery) -> true;
|
case emqx_rpc:call(Node, erlang, system_time, []) of
|
||||||
_Other -> false
|
{badrpc, _Reason} -> true;
|
||||||
end
|
Delivery when is_integer(Delivery) -> true;
|
||||||
end).
|
_Other -> false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_node_with_key() ->
|
prop_node_with_key() ->
|
||||||
?ALL({Node0, Key}, nodename_with_key(),
|
?ALL(
|
||||||
begin
|
{Node0, Key},
|
||||||
Node = punch(Node0),
|
nodename_with_key(),
|
||||||
?assert(emqx_rpc:cast(Key, Node, erlang, system_time, [])),
|
begin
|
||||||
case emqx_rpc:call(Key, Node, erlang, system_time, []) of
|
Node = punch(Node0),
|
||||||
{badrpc, _Reason} -> true;
|
?assert(emqx_rpc:cast(Key, Node, erlang, system_time, [])),
|
||||||
Delivery when is_integer(Delivery) -> true;
|
case emqx_rpc:call(Key, Node, erlang, system_time, []) of
|
||||||
_Other -> false
|
{badrpc, _Reason} -> true;
|
||||||
end
|
Delivery when is_integer(Delivery) -> true;
|
||||||
end).
|
_Other -> false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_nodes() ->
|
prop_nodes() ->
|
||||||
?ALL(Nodes0, nodesname(),
|
?ALL(
|
||||||
begin
|
Nodes0,
|
||||||
Nodes = punch(Nodes0),
|
nodesname(),
|
||||||
case emqx_rpc:multicall(Nodes, erlang, system_time, []) of
|
begin
|
||||||
{RealResults, RealBadNodes}
|
Nodes = punch(Nodes0),
|
||||||
when is_list(RealResults);
|
case emqx_rpc:multicall(Nodes, erlang, system_time, []) of
|
||||||
is_list(RealBadNodes) ->
|
{RealResults, RealBadNodes} when
|
||||||
true;
|
is_list(RealResults);
|
||||||
_Other -> false
|
is_list(RealBadNodes)
|
||||||
end
|
->
|
||||||
end).
|
true;
|
||||||
|
_Other ->
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
prop_nodes_with_key() ->
|
prop_nodes_with_key() ->
|
||||||
?ALL({Nodes0, Key}, nodesname_with_key(),
|
?ALL(
|
||||||
begin
|
{Nodes0, Key},
|
||||||
Nodes = punch(Nodes0),
|
nodesname_with_key(),
|
||||||
case emqx_rpc:multicall(Key, Nodes, erlang, system_time, []) of
|
begin
|
||||||
{RealResults, RealBadNodes}
|
Nodes = punch(Nodes0),
|
||||||
when is_list(RealResults);
|
case emqx_rpc:multicall(Key, Nodes, erlang, system_time, []) of
|
||||||
is_list(RealBadNodes) ->
|
{RealResults, RealBadNodes} when
|
||||||
true;
|
is_list(RealResults);
|
||||||
_Other -> false
|
is_list(RealBadNodes)
|
||||||
end
|
->
|
||||||
end).
|
true;
|
||||||
|
_Other ->
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Helper
|
%% Helper
|
||||||
|
|
@ -91,10 +111,13 @@ do_setup() ->
|
||||||
{ok, _Apps} = application:ensure_all_started(gen_rpc),
|
{ok, _Apps} = application:ensure_all_started(gen_rpc),
|
||||||
ok = application:set_env(gen_rpc, call_receive_timeout, 100),
|
ok = application:set_env(gen_rpc, call_receive_timeout, 100),
|
||||||
ok = meck:new(gen_rpc, [passthrough, no_history]),
|
ok = meck:new(gen_rpc, [passthrough, no_history]),
|
||||||
ok = meck:expect(gen_rpc, multicall,
|
ok = meck:expect(
|
||||||
fun(Nodes, Mod, Fun, Args) ->
|
gen_rpc,
|
||||||
gen_rpc:multicall(Nodes, Mod, Fun, Args, 100)
|
multicall,
|
||||||
end).
|
fun(Nodes, Mod, Fun, Args) ->
|
||||||
|
gen_rpc:multicall(Nodes, Mod, Fun, Args, 100)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
do_teardown(_) ->
|
do_teardown(_) ->
|
||||||
ok = net_kernel:stop(),
|
ok = net_kernel:stop(),
|
||||||
|
|
@ -121,22 +144,25 @@ ensure_distributed_nodename() ->
|
||||||
%% Generator
|
%% Generator
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
nodename() ->
|
nodename() ->
|
||||||
?LET({NodePrefix, HostName},
|
?LET(
|
||||||
{node_prefix(), hostname()},
|
{NodePrefix, HostName},
|
||||||
begin
|
{node_prefix(), hostname()},
|
||||||
Node = NodePrefix ++ "@" ++ HostName,
|
begin
|
||||||
list_to_atom(Node)
|
Node = NodePrefix ++ "@" ++ HostName,
|
||||||
end).
|
list_to_atom(Node)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
nodename_with_key() ->
|
nodename_with_key() ->
|
||||||
?LET({NodePrefix, HostName, Key},
|
?LET(
|
||||||
{node_prefix(), hostname(), choose(0, 10)},
|
{NodePrefix, HostName, Key},
|
||||||
begin
|
{node_prefix(), hostname(), choose(0, 10)},
|
||||||
Node = NodePrefix ++ "@" ++ HostName,
|
begin
|
||||||
{list_to_atom(Node), Key}
|
Node = NodePrefix ++ "@" ++ HostName,
|
||||||
end).
|
{list_to_atom(Node), Key}
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
nodesname() ->
|
nodesname() ->
|
||||||
oneof([list(nodename()), [node()]]).
|
oneof([list(nodename()), [node()]]).
|
||||||
|
|
@ -163,6 +189,7 @@ hostname() ->
|
||||||
punch(Nodes) when is_list(Nodes) ->
|
punch(Nodes) when is_list(Nodes) ->
|
||||||
lists:map(fun punch/1, Nodes);
|
lists:map(fun punch/1, Nodes);
|
||||||
punch('nonode@nohost') ->
|
punch('nonode@nohost') ->
|
||||||
node(); %% Equal to ?NODENAME
|
%% Equal to ?NODENAME
|
||||||
|
node();
|
||||||
punch(GoodBoy) ->
|
punch(GoodBoy) ->
|
||||||
GoodBoy.
|
GoodBoy.
|
||||||
|
|
|
||||||
|
|
@ -18,41 +18,53 @@
|
||||||
|
|
||||||
-include_lib("proper/include/proper.hrl").
|
-include_lib("proper/include/proper.hrl").
|
||||||
|
|
||||||
-export([ initial_state/0
|
-export([
|
||||||
, command/1
|
initial_state/0,
|
||||||
, precondition/2
|
command/1,
|
||||||
, postcondition/3
|
precondition/2,
|
||||||
, next_state/3
|
postcondition/3,
|
||||||
]).
|
next_state/3
|
||||||
|
]).
|
||||||
|
|
||||||
-define(mock_modules,
|
-define(mock_modules, [
|
||||||
[ emqx_metrics
|
emqx_metrics,
|
||||||
, emqx_stats
|
emqx_stats,
|
||||||
, emqx_broker
|
emqx_broker,
|
||||||
, mria_mnesia
|
mria_mnesia,
|
||||||
, emqx_hooks
|
emqx_hooks
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-define(ALL(Vars, Types, Exprs),
|
-define(ALL(Vars, Types, Exprs),
|
||||||
?SETUP(fun() ->
|
?SETUP(
|
||||||
|
fun() ->
|
||||||
State = do_setup(),
|
State = do_setup(),
|
||||||
fun() -> do_teardown(State) end
|
fun() -> do_teardown(State) end
|
||||||
end, ?FORALL(Vars, Types, Exprs))).
|
end,
|
||||||
|
?FORALL(Vars, Types, Exprs)
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Properties
|
%% Properties
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
prop_sys() ->
|
prop_sys() ->
|
||||||
?ALL(Cmds, commands(?MODULE),
|
?ALL(
|
||||||
|
Cmds,
|
||||||
|
commands(?MODULE),
|
||||||
begin
|
begin
|
||||||
{ok, _Pid} = emqx_sys:start_link(),
|
{ok, _Pid} = emqx_sys:start_link(),
|
||||||
{History, State, Result} = run_commands(?MODULE, Cmds),
|
{History, State, Result} = run_commands(?MODULE, Cmds),
|
||||||
ok = emqx_sys:stop(),
|
ok = emqx_sys:stop(),
|
||||||
?WHENFAIL(io:format("History: ~p\nState: ~p\nResult: ~p\n",
|
?WHENFAIL(
|
||||||
[History,State,Result]),
|
io:format(
|
||||||
aggregate(command_names(Cmds), Result =:= ok))
|
"History: ~p\nState: ~p\nResult: ~p\n",
|
||||||
end).
|
[History, State, Result]
|
||||||
|
),
|
||||||
|
aggregate(command_names(Cmds), Result =:= ok)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Helpers
|
%% Helpers
|
||||||
|
|
@ -62,9 +74,15 @@ do_setup() ->
|
||||||
ok = emqx_logger:set_log_level(emergency),
|
ok = emqx_logger:set_log_level(emergency),
|
||||||
emqx_config:put([sys_topics, sys_msg_interval], 60000),
|
emqx_config:put([sys_topics, sys_msg_interval], 60000),
|
||||||
emqx_config:put([sys_topics, sys_heartbeat_interval], 30000),
|
emqx_config:put([sys_topics, sys_heartbeat_interval], 30000),
|
||||||
emqx_config:put([sys_topics, sys_event_messages],
|
emqx_config:put(
|
||||||
#{client_connected => true, client_disconnected => true,
|
[sys_topics, sys_event_messages],
|
||||||
client_subscribed => true, client_unsubscribed => true}),
|
#{
|
||||||
|
client_connected => true,
|
||||||
|
client_disconnected => true,
|
||||||
|
client_subscribed => true,
|
||||||
|
client_unsubscribed => true
|
||||||
|
}
|
||||||
|
),
|
||||||
[mock(Mod) || Mod <- ?mock_modules],
|
[mock(Mod) || Mod <- ?mock_modules],
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
|
@ -78,10 +96,16 @@ mock(Module) ->
|
||||||
do_mock(Module).
|
do_mock(Module).
|
||||||
|
|
||||||
do_mock(emqx_broker) ->
|
do_mock(emqx_broker) ->
|
||||||
meck:expect(emqx_broker, publish,
|
meck:expect(
|
||||||
fun(Msg) -> {node(), <<"test">>, Msg} end),
|
emqx_broker,
|
||||||
meck:expect(emqx_broker, safe_publish,
|
publish,
|
||||||
fun(Msg) -> {node(), <<"test">>, Msg} end);
|
fun(Msg) -> {node(), <<"test">>, Msg} end
|
||||||
|
),
|
||||||
|
meck:expect(
|
||||||
|
emqx_broker,
|
||||||
|
safe_publish,
|
||||||
|
fun(Msg) -> {node(), <<"test">>, Msg} end
|
||||||
|
);
|
||||||
do_mock(emqx_stats) ->
|
do_mock(emqx_stats) ->
|
||||||
meck:expect(emqx_stats, getstats, fun() -> [0] end);
|
meck:expect(emqx_stats, getstats, fun() -> [0] end);
|
||||||
do_mock(mria_mnesia) ->
|
do_mock(mria_mnesia) ->
|
||||||
|
|
@ -102,16 +126,17 @@ initial_state() ->
|
||||||
|
|
||||||
%% @doc List of possible commands to run against the system
|
%% @doc List of possible commands to run against the system
|
||||||
command(_State) ->
|
command(_State) ->
|
||||||
oneof([{call, emqx_sys, info, []},
|
oneof([
|
||||||
{call, emqx_sys, version, []},
|
{call, emqx_sys, info, []},
|
||||||
{call, emqx_sys, uptime, []},
|
{call, emqx_sys, version, []},
|
||||||
{call, emqx_sys, datetime, []},
|
{call, emqx_sys, uptime, []},
|
||||||
{call, emqx_sys, sysdescr, []},
|
{call, emqx_sys, datetime, []},
|
||||||
%------------ unexpected message ----------------------%
|
{call, emqx_sys, sysdescr, []},
|
||||||
{call, emqx_sys, handle_call, [emqx_sys, other, state]},
|
%------------ unexpected message ----------------------%
|
||||||
{call, emqx_sys, handle_cast, [emqx_sys, other]},
|
{call, emqx_sys, handle_call, [emqx_sys, other, state]},
|
||||||
{call, emqx_sys, handle_info, [info, state]}
|
{call, emqx_sys, handle_cast, [emqx_sys, other]},
|
||||||
]).
|
{call, emqx_sys, handle_info, [info, state]}
|
||||||
|
]).
|
||||||
|
|
||||||
precondition(_State, {call, _Mod, _Fun, _Args}) ->
|
precondition(_State, {call, _Mod, _Fun, _Args}) ->
|
||||||
true.
|
true.
|
||||||
|
|
|
||||||
|
|
@ -23,47 +23,54 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
]).
|
fields/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ add_user/2
|
-export([
|
||||||
, delete_user/2
|
add_user/2,
|
||||||
, update_user/3
|
delete_user/2,
|
||||||
, lookup_user/2
|
update_user/3,
|
||||||
, list_users/2
|
lookup_user/2,
|
||||||
]).
|
list_users/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ query/4
|
-export([
|
||||||
, format_user_info/1
|
query/4,
|
||||||
, group_match_spec/1]).
|
format_user_info/1,
|
||||||
|
group_match_spec/1
|
||||||
|
]).
|
||||||
|
|
||||||
-define(TAB, ?MODULE).
|
-define(TAB, ?MODULE).
|
||||||
-define(AUTHN_QSCHEMA, [ {<<"like_username">>, binary}
|
-define(AUTHN_QSCHEMA, [
|
||||||
, {<<"user_group">>, binary}]).
|
{<<"like_username">>, binary},
|
||||||
|
{<<"user_group">>, binary}
|
||||||
|
]).
|
||||||
-define(QUERY_FUN, {?MODULE, query}).
|
-define(QUERY_FUN, {?MODULE, query}).
|
||||||
|
|
||||||
-type(user_group() :: binary()).
|
-type user_group() :: binary().
|
||||||
|
|
||||||
-export([mnesia/1]).
|
-export([mnesia/1]).
|
||||||
|
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
-boot_mnesia({mnesia, [boot]}).
|
||||||
|
|
||||||
-record(user_info,
|
-record(user_info, {
|
||||||
{ user_id
|
user_id,
|
||||||
, stored_key
|
stored_key,
|
||||||
, server_key
|
server_key,
|
||||||
, salt
|
salt,
|
||||||
, is_superuser
|
is_superuser
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-reflect_type([user_group/0]).
|
-reflect_type([user_group/0]).
|
||||||
|
|
||||||
|
|
@ -72,14 +79,15 @@
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
%% @doc Create or replicate tables.
|
%% @doc Create or replicate tables.
|
||||||
-spec(mnesia(boot | copy) -> ok).
|
-spec mnesia(boot | copy) -> ok.
|
||||||
mnesia(boot) ->
|
mnesia(boot) ->
|
||||||
ok = mria:create_table(?TAB, [
|
ok = mria:create_table(?TAB, [
|
||||||
{rlog_shard, ?AUTH_SHARD},
|
{rlog_shard, ?AUTH_SHARD},
|
||||||
{storage, disc_copies},
|
{storage, disc_copies},
|
||||||
{record_name, user_info},
|
{record_name, user_info},
|
||||||
{attributes, record_info(fields, user_info)},
|
{attributes, record_info(fields, user_info)},
|
||||||
{storage_properties, [{ets, [{read_concurrency, true}]}]}]).
|
{storage_properties, [{ets, [{read_concurrency, true}]}]}
|
||||||
|
]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Hocon Schema
|
%% Hocon Schema
|
||||||
|
|
@ -90,10 +98,11 @@ namespace() -> "authn-scram-builtin_db".
|
||||||
roots() -> [?CONF_NS].
|
roots() -> [?CONF_NS].
|
||||||
|
|
||||||
fields(?CONF_NS) ->
|
fields(?CONF_NS) ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('scram')}
|
[
|
||||||
, {backend, emqx_authn_schema:backend('built_in_database')}
|
{mechanism, emqx_authn_schema:mechanism('scram')},
|
||||||
, {algorithm, fun algorithm/1}
|
{backend, emqx_authn_schema:backend('built_in_database')},
|
||||||
, {iteration_count, fun iteration_count/1}
|
{algorithm, fun algorithm/1},
|
||||||
|
{iteration_count, fun iteration_count/1}
|
||||||
] ++ emqx_authn_schema:common_fields().
|
] ++ emqx_authn_schema:common_fields().
|
||||||
|
|
||||||
algorithm(type) -> hoconsc:enum([sha256, sha512]);
|
algorithm(type) -> hoconsc:enum([sha256, sha512]);
|
||||||
|
|
@ -109,23 +118,33 @@ iteration_count(_) -> undefined.
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
refs() ->
|
refs() ->
|
||||||
[hoconsc:ref(?MODULE, ?CONF_NS)].
|
[hoconsc:ref(?MODULE, ?CONF_NS)].
|
||||||
|
|
||||||
create(AuthenticatorID,
|
create(
|
||||||
#{algorithm := Algorithm,
|
AuthenticatorID,
|
||||||
iteration_count := IterationCount}) ->
|
#{
|
||||||
State = #{user_group => AuthenticatorID,
|
algorithm := Algorithm,
|
||||||
algorithm => Algorithm,
|
iteration_count := IterationCount
|
||||||
iteration_count => IterationCount},
|
}
|
||||||
|
) ->
|
||||||
|
State = #{
|
||||||
|
user_group => AuthenticatorID,
|
||||||
|
algorithm => Algorithm,
|
||||||
|
iteration_count => IterationCount
|
||||||
|
},
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
|
|
||||||
update(Config, #{user_group := ID}) ->
|
update(Config, #{user_group := ID}) ->
|
||||||
create(ID, Config).
|
create(ID, Config).
|
||||||
|
|
||||||
authenticate(#{auth_method := AuthMethod,
|
authenticate(
|
||||||
auth_data := AuthData,
|
#{
|
||||||
auth_cache := AuthCache}, State) ->
|
auth_method := AuthMethod,
|
||||||
|
auth_data := AuthData,
|
||||||
|
auth_cache := AuthCache
|
||||||
|
},
|
||||||
|
State
|
||||||
|
) ->
|
||||||
case ensure_auth_method(AuthMethod, State) of
|
case ensure_auth_method(AuthMethod, State) of
|
||||||
true ->
|
true ->
|
||||||
case AuthCache of
|
case AuthCache of
|
||||||
|
|
@ -144,13 +163,22 @@ destroy(#{user_group := UserGroup}) ->
|
||||||
MatchSpec = group_match_spec(UserGroup),
|
MatchSpec = group_match_spec(UserGroup),
|
||||||
trans(
|
trans(
|
||||||
fun() ->
|
fun() ->
|
||||||
ok = lists:foreach(fun(UserInfo) ->
|
ok = lists:foreach(
|
||||||
mnesia:delete_object(?TAB, UserInfo, write)
|
fun(UserInfo) ->
|
||||||
end, mnesia:select(?TAB, MatchSpec, write))
|
mnesia:delete_object(?TAB, UserInfo, write)
|
||||||
end).
|
end,
|
||||||
|
mnesia:select(?TAB, MatchSpec, write)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
add_user(#{user_id := UserID,
|
add_user(
|
||||||
password := Password} = UserInfo, #{user_group := UserGroup} = State) ->
|
#{
|
||||||
|
user_id := UserID,
|
||||||
|
password := Password
|
||||||
|
} = UserInfo,
|
||||||
|
#{user_group := UserGroup} = State
|
||||||
|
) ->
|
||||||
trans(
|
trans(
|
||||||
fun() ->
|
fun() ->
|
||||||
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
||||||
|
|
@ -161,7 +189,8 @@ add_user(#{user_id := UserID,
|
||||||
[_] ->
|
[_] ->
|
||||||
{error, already_exist}
|
{error, already_exist}
|
||||||
end
|
end
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
delete_user(UserID, #{user_group := UserGroup}) ->
|
delete_user(UserID, #{user_group := UserGroup}) ->
|
||||||
trans(
|
trans(
|
||||||
|
|
@ -172,30 +201,42 @@ delete_user(UserID, #{user_group := UserGroup}) ->
|
||||||
[_] ->
|
[_] ->
|
||||||
mnesia:delete(?TAB, {UserGroup, UserID}, write)
|
mnesia:delete(?TAB, {UserGroup, UserID}, write)
|
||||||
end
|
end
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
update_user(UserID, User,
|
update_user(
|
||||||
#{user_group := UserGroup} = State) ->
|
UserID,
|
||||||
|
User,
|
||||||
|
#{user_group := UserGroup} = State
|
||||||
|
) ->
|
||||||
trans(
|
trans(
|
||||||
fun() ->
|
fun() ->
|
||||||
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
||||||
[] ->
|
[] ->
|
||||||
{error, not_found};
|
{error, not_found};
|
||||||
[#user_info{is_superuser = IsSuperuser} = UserInfo] ->
|
[#user_info{is_superuser = IsSuperuser} = UserInfo] ->
|
||||||
UserInfo1 = UserInfo#user_info{is_superuser = maps:get(is_superuser, User, IsSuperuser)},
|
UserInfo1 = UserInfo#user_info{
|
||||||
UserInfo2 = case maps:get(password, User, undefined) of
|
is_superuser = maps:get(is_superuser, User, IsSuperuser)
|
||||||
undefined ->
|
},
|
||||||
UserInfo1;
|
UserInfo2 =
|
||||||
Password ->
|
case maps:get(password, User, undefined) of
|
||||||
{StoredKey, ServerKey, Salt} = esasl_scram:generate_authentication_info(Password, State),
|
undefined ->
|
||||||
UserInfo1#user_info{stored_key = StoredKey,
|
UserInfo1;
|
||||||
server_key = ServerKey,
|
Password ->
|
||||||
salt = Salt}
|
{StoredKey, ServerKey, Salt} = esasl_scram:generate_authentication_info(
|
||||||
end,
|
Password, State
|
||||||
|
),
|
||||||
|
UserInfo1#user_info{
|
||||||
|
stored_key = StoredKey,
|
||||||
|
server_key = ServerKey,
|
||||||
|
salt = Salt
|
||||||
|
}
|
||||||
|
end,
|
||||||
mnesia:write(?TAB, UserInfo2, write),
|
mnesia:write(?TAB, UserInfo2, write),
|
||||||
{ok, format_user_info(UserInfo2)}
|
{ok, format_user_info(UserInfo2)}
|
||||||
end
|
end
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
lookup_user(UserID, #{user_group := UserGroup}) ->
|
lookup_user(UserID, #{user_group := UserGroup}) ->
|
||||||
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
||||||
|
|
@ -214,14 +255,23 @@ list_users(QueryString, #{user_group := UserGroup}) ->
|
||||||
|
|
||||||
query(Tab, {QString, []}, Continuation, Limit) ->
|
query(Tab, {QString, []}, Continuation, Limit) ->
|
||||||
Ms = ms_from_qstring(QString),
|
Ms = ms_from_qstring(QString),
|
||||||
emqx_mgmt_api:select_table_with_count(Tab, Ms, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(
|
||||||
fun format_user_info/1);
|
Tab,
|
||||||
|
Ms,
|
||||||
|
Continuation,
|
||||||
|
Limit,
|
||||||
|
fun format_user_info/1
|
||||||
|
);
|
||||||
query(Tab, {QString, FuzzyQString}, Continuation, Limit) ->
|
query(Tab, {QString, FuzzyQString}, Continuation, Limit) ->
|
||||||
Ms = ms_from_qstring(QString),
|
Ms = ms_from_qstring(QString),
|
||||||
FuzzyFilterFun = fuzzy_filter_fun(FuzzyQString),
|
FuzzyFilterFun = fuzzy_filter_fun(FuzzyQString),
|
||||||
emqx_mgmt_api:select_table_with_count(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(
|
||||||
fun format_user_info/1).
|
Tab,
|
||||||
|
{Ms, FuzzyFilterFun},
|
||||||
|
Continuation,
|
||||||
|
Limit,
|
||||||
|
fun format_user_info/1
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Match funcs
|
%% Match funcs
|
||||||
|
|
@ -229,14 +279,18 @@ query(Tab, {QString, FuzzyQString}, Continuation, Limit) ->
|
||||||
%% Fuzzy username funcs
|
%% Fuzzy username funcs
|
||||||
fuzzy_filter_fun(Fuzzy) ->
|
fuzzy_filter_fun(Fuzzy) ->
|
||||||
fun(MsRaws) when is_list(MsRaws) ->
|
fun(MsRaws) when is_list(MsRaws) ->
|
||||||
lists:filter( fun(E) -> run_fuzzy_filter(E, Fuzzy) end
|
lists:filter(
|
||||||
, MsRaws)
|
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||||
|
MsRaws
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
run_fuzzy_filter(_, []) ->
|
run_fuzzy_filter(_, []) ->
|
||||||
true;
|
true;
|
||||||
run_fuzzy_filter( E = #user_info{user_id = {_, UserID}}
|
run_fuzzy_filter(
|
||||||
, [{username, like, UsernameSubStr} | Fuzzy]) ->
|
E = #user_info{user_id = {_, UserID}},
|
||||||
|
[{username, like, UsernameSubStr} | Fuzzy]
|
||||||
|
) ->
|
||||||
binary:match(UserID, UsernameSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
|
binary:match(UserID, UsernameSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
@ -252,13 +306,17 @@ ensure_auth_method(_, _) ->
|
||||||
|
|
||||||
check_client_first_message(Bin, _Cache, #{iteration_count := IterationCount} = State) ->
|
check_client_first_message(Bin, _Cache, #{iteration_count := IterationCount} = State) ->
|
||||||
RetrieveFun = fun(Username) ->
|
RetrieveFun = fun(Username) ->
|
||||||
retrieve(Username, State)
|
retrieve(Username, State)
|
||||||
end,
|
end,
|
||||||
case esasl_scram:check_client_first_message(
|
case
|
||||||
Bin,
|
esasl_scram:check_client_first_message(
|
||||||
#{iteration_count => IterationCount,
|
Bin,
|
||||||
retrieve => RetrieveFun}
|
#{
|
||||||
) of
|
iteration_count => IterationCount,
|
||||||
|
retrieve => RetrieveFun
|
||||||
|
}
|
||||||
|
)
|
||||||
|
of
|
||||||
{continue, ServerFirstMessage, Cache} ->
|
{continue, ServerFirstMessage, Cache} ->
|
||||||
{continue, ServerFirstMessage, Cache};
|
{continue, ServerFirstMessage, Cache};
|
||||||
ignore ->
|
ignore ->
|
||||||
|
|
@ -268,10 +326,12 @@ check_client_first_message(Bin, _Cache, #{iteration_count := IterationCount} = S
|
||||||
end.
|
end.
|
||||||
|
|
||||||
check_client_final_message(Bin, #{is_superuser := IsSuperuser} = Cache, #{algorithm := Alg}) ->
|
check_client_final_message(Bin, #{is_superuser := IsSuperuser} = Cache, #{algorithm := Alg}) ->
|
||||||
case esasl_scram:check_client_final_message(
|
case
|
||||||
Bin,
|
esasl_scram:check_client_final_message(
|
||||||
Cache#{algorithm => Alg}
|
Bin,
|
||||||
) of
|
Cache#{algorithm => Alg}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, ServerFinalMessage} ->
|
{ok, ServerFinalMessage} ->
|
||||||
{ok, #{is_superuser => IsSuperuser}, ServerFinalMessage};
|
{ok, #{is_superuser => IsSuperuser}, ServerFinalMessage};
|
||||||
{error, _Reason} ->
|
{error, _Reason} ->
|
||||||
|
|
@ -280,23 +340,31 @@ check_client_final_message(Bin, #{is_superuser := IsSuperuser} = Cache, #{algori
|
||||||
|
|
||||||
add_user(UserGroup, UserID, Password, IsSuperuser, State) ->
|
add_user(UserGroup, UserID, Password, IsSuperuser, State) ->
|
||||||
{StoredKey, ServerKey, Salt} = esasl_scram:generate_authentication_info(Password, State),
|
{StoredKey, ServerKey, Salt} = esasl_scram:generate_authentication_info(Password, State),
|
||||||
UserInfo = #user_info{user_id = {UserGroup, UserID},
|
UserInfo = #user_info{
|
||||||
stored_key = StoredKey,
|
user_id = {UserGroup, UserID},
|
||||||
server_key = ServerKey,
|
stored_key = StoredKey,
|
||||||
salt = Salt,
|
server_key = ServerKey,
|
||||||
is_superuser = IsSuperuser},
|
salt = Salt,
|
||||||
|
is_superuser = IsSuperuser
|
||||||
|
},
|
||||||
mnesia:write(?TAB, UserInfo, write).
|
mnesia:write(?TAB, UserInfo, write).
|
||||||
|
|
||||||
retrieve(UserID, #{user_group := UserGroup}) ->
|
retrieve(UserID, #{user_group := UserGroup}) ->
|
||||||
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
||||||
[#user_info{stored_key = StoredKey,
|
[
|
||||||
server_key = ServerKey,
|
#user_info{
|
||||||
salt = Salt,
|
stored_key = StoredKey,
|
||||||
is_superuser = IsSuperuser}] ->
|
server_key = ServerKey,
|
||||||
{ok, #{stored_key => StoredKey,
|
salt = Salt,
|
||||||
server_key => ServerKey,
|
is_superuser = IsSuperuser
|
||||||
salt => Salt,
|
}
|
||||||
is_superuser => IsSuperuser}};
|
] ->
|
||||||
|
{ok, #{
|
||||||
|
stored_key => StoredKey,
|
||||||
|
server_key => ServerKey,
|
||||||
|
salt => Salt,
|
||||||
|
is_superuser => IsSuperuser
|
||||||
|
}};
|
||||||
[] ->
|
[] ->
|
||||||
{error, not_found}
|
{error, not_found}
|
||||||
end.
|
end.
|
||||||
|
|
@ -315,15 +383,21 @@ format_user_info(#user_info{user_id = {_, UserID}, is_superuser = IsSuperuser})
|
||||||
#{user_id => UserID, is_superuser => IsSuperuser}.
|
#{user_id => UserID, is_superuser => IsSuperuser}.
|
||||||
|
|
||||||
ms_from_qstring(QString) ->
|
ms_from_qstring(QString) ->
|
||||||
[Ms] = lists:foldl(fun({user_group, '=:=', UserGroup}, AccIn) ->
|
[Ms] = lists:foldl(
|
||||||
[group_match_spec(UserGroup) | AccIn];
|
fun
|
||||||
(_, AccIn) ->
|
({user_group, '=:=', UserGroup}, AccIn) ->
|
||||||
AccIn
|
[group_match_spec(UserGroup) | AccIn];
|
||||||
end, [], QString),
|
(_, AccIn) ->
|
||||||
|
AccIn
|
||||||
|
end,
|
||||||
|
[],
|
||||||
|
QString
|
||||||
|
),
|
||||||
Ms.
|
Ms.
|
||||||
|
|
||||||
group_match_spec(UserGroup) ->
|
group_match_spec(UserGroup) ->
|
||||||
ets:fun2ms(
|
ets:fun2ms(
|
||||||
fun(#user_info{user_id = {Group, _}} = User) when Group =:= UserGroup ->
|
fun(#user_info{user_id = {Group, _}} = User) when Group =:= UserGroup ->
|
||||||
User
|
User
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,10 @@
|
||||||
|
|
||||||
-behaviour(emqx_bpapi).
|
-behaviour(emqx_bpapi).
|
||||||
|
|
||||||
-export([ introduced_in/0
|
-export([
|
||||||
, lookup_from_all_nodes/3
|
introduced_in/0,
|
||||||
]).
|
lookup_from_all_nodes/3
|
||||||
|
]).
|
||||||
|
|
||||||
-include_lib("emqx/include/bpapi.hrl").
|
-include_lib("emqx/include/bpapi.hrl").
|
||||||
|
|
||||||
|
|
@ -30,6 +31,8 @@ introduced_in() ->
|
||||||
"5.0.0".
|
"5.0.0".
|
||||||
|
|
||||||
-spec lookup_from_all_nodes([node()], atom(), binary()) ->
|
-spec lookup_from_all_nodes([node()], atom(), binary()) ->
|
||||||
emqx_rpc:erpc_multicall().
|
emqx_rpc:erpc_multicall().
|
||||||
lookup_from_all_nodes(Nodes, ChainName, AuthenticatorID) ->
|
lookup_from_all_nodes(Nodes, ChainName, AuthenticatorID) ->
|
||||||
erpc:multicall(Nodes, emqx_authn_api, lookup_from_local_node, [ChainName, AuthenticatorID], ?TIMEOUT).
|
erpc:multicall(
|
||||||
|
Nodes, emqx_authn_api, lookup_from_local_node, [ChainName, AuthenticatorID], ?TIMEOUT
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -24,18 +24,20 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
, validations/0
|
fields/1,
|
||||||
]).
|
validations/0
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Hocon Schema
|
%% Hocon Schema
|
||||||
|
|
@ -44,35 +46,47 @@
|
||||||
namespace() -> "authn-http".
|
namespace() -> "authn-http".
|
||||||
|
|
||||||
roots() ->
|
roots() ->
|
||||||
[ {?CONF_NS,
|
[
|
||||||
hoconsc:mk(hoconsc:union(refs()),
|
{?CONF_NS,
|
||||||
#{})}
|
hoconsc:mk(
|
||||||
|
hoconsc:union(refs()),
|
||||||
|
#{}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
fields(get) ->
|
fields(get) ->
|
||||||
[ {method, #{type => get, default => post}}
|
[
|
||||||
, {headers, fun headers_no_content_type/1}
|
{method, #{type => get, default => post}},
|
||||||
|
{headers, fun headers_no_content_type/1}
|
||||||
] ++ common_fields();
|
] ++ common_fields();
|
||||||
|
|
||||||
fields(post) ->
|
fields(post) ->
|
||||||
[ {method, #{type => post, default => post}}
|
[
|
||||||
, {headers, fun headers/1}
|
{method, #{type => post, default => post}},
|
||||||
|
{headers, fun headers/1}
|
||||||
] ++ common_fields().
|
] ++ common_fields().
|
||||||
|
|
||||||
common_fields() ->
|
common_fields() ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('password_based')}
|
[
|
||||||
, {backend, emqx_authn_schema:backend(http)}
|
{mechanism, emqx_authn_schema:mechanism('password_based')},
|
||||||
, {url, fun url/1}
|
{backend, emqx_authn_schema:backend(http)},
|
||||||
, {body, map([{fuzzy, term(), binary()}])}
|
{url, fun url/1},
|
||||||
, {request_timeout, fun request_timeout/1}
|
{body, map([{fuzzy, term(), binary()}])},
|
||||||
] ++ emqx_authn_schema:common_fields()
|
{request_timeout, fun request_timeout/1}
|
||||||
++ maps:to_list(maps:without([ base_url
|
] ++ emqx_authn_schema:common_fields() ++
|
||||||
, pool_type],
|
maps:to_list(
|
||||||
maps:from_list(emqx_connector_http:fields(config)))).
|
maps:without(
|
||||||
|
[
|
||||||
|
base_url,
|
||||||
|
pool_type
|
||||||
|
],
|
||||||
|
maps:from_list(emqx_connector_http:fields(config))
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
validations() ->
|
validations() ->
|
||||||
[ {check_ssl_opts, fun check_ssl_opts/1}
|
[
|
||||||
, {check_headers, fun check_headers/1}
|
{check_ssl_opts, fun check_ssl_opts/1},
|
||||||
|
{check_headers, fun check_headers/1}
|
||||||
].
|
].
|
||||||
|
|
||||||
url(type) -> binary();
|
url(type) -> binary();
|
||||||
|
|
@ -80,21 +94,27 @@ url(validator) -> [?NOT_EMPTY("the value of the field 'url' cannot be empty")];
|
||||||
url(required) -> true;
|
url(required) -> true;
|
||||||
url(_) -> undefined.
|
url(_) -> undefined.
|
||||||
|
|
||||||
headers(type) -> map();
|
headers(type) ->
|
||||||
|
map();
|
||||||
headers(converter) ->
|
headers(converter) ->
|
||||||
fun(Headers) ->
|
fun(Headers) ->
|
||||||
maps:merge(default_headers(), transform_header_name(Headers))
|
maps:merge(default_headers(), transform_header_name(Headers))
|
||||||
end;
|
end;
|
||||||
headers(default) -> default_headers();
|
headers(default) ->
|
||||||
headers(_) -> undefined.
|
default_headers();
|
||||||
|
headers(_) ->
|
||||||
|
undefined.
|
||||||
|
|
||||||
headers_no_content_type(type) -> map();
|
headers_no_content_type(type) ->
|
||||||
|
map();
|
||||||
headers_no_content_type(converter) ->
|
headers_no_content_type(converter) ->
|
||||||
fun(Headers) ->
|
fun(Headers) ->
|
||||||
maps:merge(default_headers_no_content_type(), transform_header_name(Headers))
|
maps:merge(default_headers_no_content_type(), transform_header_name(Headers))
|
||||||
end;
|
end;
|
||||||
headers_no_content_type(default) -> default_headers_no_content_type();
|
headers_no_content_type(default) ->
|
||||||
headers_no_content_type(_) -> undefined.
|
default_headers_no_content_type();
|
||||||
|
headers_no_content_type(_) ->
|
||||||
|
undefined.
|
||||||
|
|
||||||
request_timeout(type) -> emqx_schema:duration_ms();
|
request_timeout(type) -> emqx_schema:duration_ms();
|
||||||
request_timeout(default) -> <<"5s">>;
|
request_timeout(default) -> <<"5s">>;
|
||||||
|
|
@ -105,36 +125,51 @@ request_timeout(_) -> undefined.
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
refs() ->
|
refs() ->
|
||||||
[ hoconsc:ref(?MODULE, get)
|
[
|
||||||
, hoconsc:ref(?MODULE, post)
|
hoconsc:ref(?MODULE, get),
|
||||||
|
hoconsc:ref(?MODULE, post)
|
||||||
].
|
].
|
||||||
|
|
||||||
create(_AuthenticatorID, Config) ->
|
create(_AuthenticatorID, Config) ->
|
||||||
create(Config).
|
create(Config).
|
||||||
|
|
||||||
create(#{method := Method,
|
create(
|
||||||
url := RawURL,
|
#{
|
||||||
headers := Headers,
|
method := Method,
|
||||||
body := Body,
|
url := RawURL,
|
||||||
request_timeout := RequestTimeout} = Config) ->
|
headers := Headers,
|
||||||
|
body := Body,
|
||||||
|
request_timeout := RequestTimeout
|
||||||
|
} = Config
|
||||||
|
) ->
|
||||||
{BsaeUrlWithPath, Query} = parse_fullpath(RawURL),
|
{BsaeUrlWithPath, Query} = parse_fullpath(RawURL),
|
||||||
URIMap = parse_url(BsaeUrlWithPath),
|
URIMap = parse_url(BsaeUrlWithPath),
|
||||||
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
||||||
State = #{method => Method,
|
State = #{
|
||||||
path => maps:get(path, URIMap),
|
method => Method,
|
||||||
base_query_template => emqx_authn_utils:parse_deep(
|
path => maps:get(path, URIMap),
|
||||||
cow_qs:parse_qs(to_bin(Query))),
|
base_query_template => emqx_authn_utils:parse_deep(
|
||||||
headers => maps:to_list(Headers),
|
cow_qs:parse_qs(to_bin(Query))
|
||||||
body_template => emqx_authn_utils:parse_deep(
|
),
|
||||||
maps:to_list(Body)),
|
headers => maps:to_list(Headers),
|
||||||
request_timeout => RequestTimeout,
|
body_template => emqx_authn_utils:parse_deep(
|
||||||
resource_id => ResourceId},
|
maps:to_list(Body)
|
||||||
case emqx_resource:create_local(ResourceId,
|
),
|
||||||
?RESOURCE_GROUP,
|
request_timeout => RequestTimeout,
|
||||||
emqx_connector_http,
|
resource_id => ResourceId
|
||||||
Config#{base_url => maps:remove(query, URIMap),
|
},
|
||||||
pool_type => random},
|
case
|
||||||
#{}) of
|
emqx_resource:create_local(
|
||||||
|
ResourceId,
|
||||||
|
?RESOURCE_GROUP,
|
||||||
|
emqx_connector_http,
|
||||||
|
Config#{
|
||||||
|
base_url => maps:remove(query, URIMap),
|
||||||
|
pool_type => random
|
||||||
|
},
|
||||||
|
#{}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, already_created} ->
|
{ok, already_created} ->
|
||||||
{ok, State};
|
{ok, State};
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
|
|
@ -154,13 +189,20 @@ update(Config, State) ->
|
||||||
|
|
||||||
authenticate(#{auth_method := _}, _) ->
|
authenticate(#{auth_method := _}, _) ->
|
||||||
ignore;
|
ignore;
|
||||||
authenticate(Credential, #{resource_id := ResourceId,
|
authenticate(
|
||||||
method := Method,
|
Credential,
|
||||||
request_timeout := RequestTimeout} = State) ->
|
#{
|
||||||
|
resource_id := ResourceId,
|
||||||
|
method := Method,
|
||||||
|
request_timeout := RequestTimeout
|
||||||
|
} = State
|
||||||
|
) ->
|
||||||
Request = generate_request(Credential, State),
|
Request = generate_request(Credential, State),
|
||||||
case emqx_resource:query(ResourceId, {Method, Request, RequestTimeout}) of
|
case emqx_resource:query(ResourceId, {Method, Request, RequestTimeout}) of
|
||||||
{ok, 204, _Headers} -> {ok, #{is_superuser => false}};
|
{ok, 204, _Headers} ->
|
||||||
{ok, 200, _Headers} -> {ok, #{is_superuser => false}};
|
{ok, #{is_superuser => false}};
|
||||||
|
{ok, 200, _Headers} ->
|
||||||
|
{ok, #{is_superuser => false}};
|
||||||
{ok, 200, Headers, Body} ->
|
{ok, 200, Headers, Body} ->
|
||||||
ContentType = proplists:get_value(<<"content-type">>, Headers, <<"application/json">>),
|
ContentType = proplists:get_value(<<"content-type">>, Headers, <<"application/json">>),
|
||||||
case safely_parse_body(ContentType, Body) of
|
case safely_parse_body(ContentType, Body) of
|
||||||
|
|
@ -173,24 +215,32 @@ authenticate(Credential, #{resource_id := ResourceId,
|
||||||
{ok, #{is_superuser => false}}
|
{ok, #{is_superuser => false}}
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{msg => "http_server_query_failed",
|
?SLOG(error, #{
|
||||||
resource => ResourceId,
|
msg => "http_server_query_failed",
|
||||||
reason => Reason}),
|
resource => ResourceId,
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
ignore;
|
ignore;
|
||||||
Other ->
|
Other ->
|
||||||
Output = may_append_body(#{resource => ResourceId}, Other),
|
Output = may_append_body(#{resource => ResourceId}, Other),
|
||||||
case erlang:element(2, Other) of
|
case erlang:element(2, Other) of
|
||||||
Code5xx when Code5xx >= 500 andalso Code5xx < 600 ->
|
Code5xx when Code5xx >= 500 andalso Code5xx < 600 ->
|
||||||
?SLOG(error, Output#{msg => "http_server_error",
|
?SLOG(error, Output#{
|
||||||
code => Code5xx}),
|
msg => "http_server_error",
|
||||||
|
code => Code5xx
|
||||||
|
}),
|
||||||
ignore;
|
ignore;
|
||||||
Code4xx when Code4xx >= 400 andalso Code4xx < 500 ->
|
Code4xx when Code4xx >= 400 andalso Code4xx < 500 ->
|
||||||
?SLOG(warning, Output#{msg => "refused_by_http_server",
|
?SLOG(warning, Output#{
|
||||||
code => Code4xx}),
|
msg => "refused_by_http_server",
|
||||||
|
code => Code4xx
|
||||||
|
}),
|
||||||
{error, not_authorized};
|
{error, not_authorized};
|
||||||
OtherCode ->
|
OtherCode ->
|
||||||
?SLOG(error, Output#{msg => "undesired_response_code",
|
?SLOG(error, Output#{
|
||||||
code => OtherCode}),
|
msg => "undesired_response_code",
|
||||||
|
code => OtherCode
|
||||||
|
}),
|
||||||
ignore
|
ignore
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
@ -207,22 +257,29 @@ parse_fullpath(RawURL) ->
|
||||||
cow_http:parse_fullpath(to_bin(RawURL)).
|
cow_http:parse_fullpath(to_bin(RawURL)).
|
||||||
|
|
||||||
default_headers() ->
|
default_headers() ->
|
||||||
maps:put(<<"content-type">>,
|
maps:put(
|
||||||
<<"application/json">>,
|
<<"content-type">>,
|
||||||
default_headers_no_content_type()).
|
<<"application/json">>,
|
||||||
|
default_headers_no_content_type()
|
||||||
|
).
|
||||||
|
|
||||||
default_headers_no_content_type() ->
|
default_headers_no_content_type() ->
|
||||||
#{ <<"accept">> => <<"application/json">>
|
#{
|
||||||
, <<"cache-control">> => <<"no-cache">>
|
<<"accept">> => <<"application/json">>,
|
||||||
, <<"connection">> => <<"keep-alive">>
|
<<"cache-control">> => <<"no-cache">>,
|
||||||
, <<"keep-alive">> => <<"timeout=30, max=1000">>
|
<<"connection">> => <<"keep-alive">>,
|
||||||
}.
|
<<"keep-alive">> => <<"timeout=30, max=1000">>
|
||||||
|
}.
|
||||||
|
|
||||||
transform_header_name(Headers) ->
|
transform_header_name(Headers) ->
|
||||||
maps:fold(fun(K0, V, Acc) ->
|
maps:fold(
|
||||||
K = list_to_binary(string:to_lower(to_list(K0))),
|
fun(K0, V, Acc) ->
|
||||||
maps:put(K, V, Acc)
|
K = list_to_binary(string:to_lower(to_list(K0))),
|
||||||
end, #{}, Headers).
|
maps:put(K, V, Acc)
|
||||||
|
end,
|
||||||
|
#{},
|
||||||
|
Headers
|
||||||
|
).
|
||||||
|
|
||||||
check_ssl_opts(Conf) ->
|
check_ssl_opts(Conf) ->
|
||||||
{BaseUrlWithPath, _Query} = parse_fullpath(get_conf_val("url", Conf)),
|
{BaseUrlWithPath, _Query} = parse_fullpath(get_conf_val("url", Conf)),
|
||||||
|
|
@ -250,11 +307,13 @@ parse_url(URL) ->
|
||||||
URIMap
|
URIMap
|
||||||
end.
|
end.
|
||||||
|
|
||||||
generate_request(Credential, #{method := Method,
|
generate_request(Credential, #{
|
||||||
path := Path,
|
method := Method,
|
||||||
base_query_template := BaseQueryTemplate,
|
path := Path,
|
||||||
headers := Headers,
|
base_query_template := BaseQueryTemplate,
|
||||||
body_template := BodyTemplate}) ->
|
headers := Headers,
|
||||||
|
body_template := BodyTemplate
|
||||||
|
}) ->
|
||||||
Body = emqx_authn_utils:render_deep(BodyTemplate, Credential),
|
Body = emqx_authn_utils:render_deep(BodyTemplate, Credential),
|
||||||
NBaseQuery = emqx_authn_utils:render_deep(BaseQueryTemplate, Credential),
|
NBaseQuery = emqx_authn_utils:render_deep(BaseQueryTemplate, Credential),
|
||||||
case Method of
|
case Method of
|
||||||
|
|
|
||||||
|
|
@ -22,23 +22,25 @@
|
||||||
-include_lib("jose/include/jose_jwk.hrl").
|
-include_lib("jose/include/jose_jwk.hrl").
|
||||||
-include_lib("snabbkaffe/include/snabbkaffe.hrl").
|
-include_lib("snabbkaffe/include/snabbkaffe.hrl").
|
||||||
|
|
||||||
|
-export([
|
||||||
|
start_link/1,
|
||||||
|
stop/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ start_link/1
|
-export([
|
||||||
, stop/1
|
get_jwks/1,
|
||||||
]).
|
update/2
|
||||||
|
]).
|
||||||
-export([ get_jwks/1
|
|
||||||
, update/2
|
|
||||||
]).
|
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% APIs
|
%% APIs
|
||||||
|
|
@ -67,11 +69,9 @@ init([Opts]) ->
|
||||||
|
|
||||||
handle_call(get_cached_jwks, _From, #{jwks := Jwks} = State) ->
|
handle_call(get_cached_jwks, _From, #{jwks := Jwks} = State) ->
|
||||||
{reply, {ok, Jwks}, State};
|
{reply, {ok, Jwks}, State};
|
||||||
|
|
||||||
handle_call({update, Opts}, _From, _State) ->
|
handle_call({update, Opts}, _From, _State) ->
|
||||||
NewState = handle_options(Opts),
|
NewState = handle_options(Opts),
|
||||||
{reply, ok, refresh_jwks(NewState)};
|
{reply, ok, refresh_jwks(NewState)};
|
||||||
|
|
||||||
handle_call(_Req, _From, State) ->
|
handle_call(_Req, _From, State) ->
|
||||||
{reply, ok, State}.
|
{reply, ok, State}.
|
||||||
|
|
||||||
|
|
@ -80,47 +80,53 @@ handle_cast(_Msg, State) ->
|
||||||
|
|
||||||
handle_info({refresh_jwks, _TRef, refresh}, #{request_id := RequestID} = State) ->
|
handle_info({refresh_jwks, _TRef, refresh}, #{request_id := RequestID} = State) ->
|
||||||
case RequestID of
|
case RequestID of
|
||||||
undefined -> ok;
|
undefined ->
|
||||||
|
ok;
|
||||||
_ ->
|
_ ->
|
||||||
ok = httpc:cancel_request(RequestID),
|
ok = httpc:cancel_request(RequestID),
|
||||||
receive
|
receive
|
||||||
{http, _} -> ok
|
{http, _} -> ok
|
||||||
after 0 ->
|
after 0 ->
|
||||||
ok
|
ok
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
{noreply, refresh_jwks(State)};
|
{noreply, refresh_jwks(State)};
|
||||||
|
handle_info(
|
||||||
handle_info({http, {RequestID, Result}},
|
{http, {RequestID, Result}},
|
||||||
#{request_id := RequestID, endpoint := Endpoint} = State0) ->
|
#{request_id := RequestID, endpoint := Endpoint} = State0
|
||||||
|
) ->
|
||||||
?tp(debug, jwks_endpoint_response, #{request_id => RequestID}),
|
?tp(debug, jwks_endpoint_response, #{request_id => RequestID}),
|
||||||
State1 = State0#{request_id := undefined},
|
State1 = State0#{request_id := undefined},
|
||||||
NewState = case Result of
|
NewState =
|
||||||
{error, Reason} ->
|
case Result of
|
||||||
?SLOG(warning, #{msg => "failed_to_request_jwks_endpoint",
|
{error, Reason} ->
|
||||||
endpoint => Endpoint,
|
?SLOG(warning, #{
|
||||||
reason => Reason}),
|
msg => "failed_to_request_jwks_endpoint",
|
||||||
State1;
|
endpoint => Endpoint,
|
||||||
{StatusLine, Headers, Body} ->
|
reason => Reason
|
||||||
try
|
}),
|
||||||
JWKS = jose_jwk:from(emqx_json:decode(Body, [return_maps])),
|
State1;
|
||||||
{_, JWKs} = JWKS#jose_jwk.keys,
|
{StatusLine, Headers, Body} ->
|
||||||
State1#{jwks := JWKs}
|
try
|
||||||
catch _:_ ->
|
JWKS = jose_jwk:from(emqx_json:decode(Body, [return_maps])),
|
||||||
?SLOG(warning, #{msg => "invalid_jwks_returned",
|
{_, JWKs} = JWKS#jose_jwk.keys,
|
||||||
endpoint => Endpoint,
|
State1#{jwks := JWKs}
|
||||||
status => StatusLine,
|
catch
|
||||||
headers => Headers,
|
_:_ ->
|
||||||
body => Body}),
|
?SLOG(warning, #{
|
||||||
State1
|
msg => "invalid_jwks_returned",
|
||||||
end
|
endpoint => Endpoint,
|
||||||
end,
|
status => StatusLine,
|
||||||
|
headers => Headers,
|
||||||
|
body => Body
|
||||||
|
}),
|
||||||
|
State1
|
||||||
|
end
|
||||||
|
end,
|
||||||
{noreply, NewState};
|
{noreply, NewState};
|
||||||
|
|
||||||
handle_info({http, {_, _}}, State) ->
|
handle_info({http, {_, _}}, State) ->
|
||||||
%% ignore
|
%% ignore
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info(_Info, State) ->
|
handle_info(_Info, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
@ -135,32 +141,50 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
handle_options(#{endpoint := Endpoint,
|
handle_options(#{
|
||||||
refresh_interval := RefreshInterval0,
|
endpoint := Endpoint,
|
||||||
ssl_opts := SSLOpts}) ->
|
refresh_interval := RefreshInterval0,
|
||||||
#{endpoint => Endpoint,
|
ssl_opts := SSLOpts
|
||||||
refresh_interval => limit_refresh_interval(RefreshInterval0),
|
}) ->
|
||||||
ssl_opts => maps:to_list(SSLOpts),
|
#{
|
||||||
jwks => [],
|
endpoint => Endpoint,
|
||||||
request_id => undefined}.
|
refresh_interval => limit_refresh_interval(RefreshInterval0),
|
||||||
|
ssl_opts => maps:to_list(SSLOpts),
|
||||||
|
jwks => [],
|
||||||
|
request_id => undefined
|
||||||
|
}.
|
||||||
|
|
||||||
refresh_jwks(#{endpoint := Endpoint,
|
refresh_jwks(
|
||||||
ssl_opts := SSLOpts} = State) ->
|
#{
|
||||||
HTTPOpts = [ {timeout, 5000}
|
endpoint := Endpoint,
|
||||||
, {connect_timeout, 5000}
|
ssl_opts := SSLOpts
|
||||||
, {ssl, SSLOpts}
|
} = State
|
||||||
],
|
) ->
|
||||||
NState = case httpc:request(get, {Endpoint, [{"Accept", "application/json"}]}, HTTPOpts,
|
HTTPOpts = [
|
||||||
[{body_format, binary}, {sync, false}, {receiver, self()}]) of
|
{timeout, 5000},
|
||||||
{error, Reason} ->
|
{connect_timeout, 5000},
|
||||||
?tp(warning, jwks_endpoint_request_fail, #{endpoint => Endpoint,
|
{ssl, SSLOpts}
|
||||||
http_opts => HTTPOpts,
|
],
|
||||||
reason => Reason}),
|
NState =
|
||||||
State;
|
case
|
||||||
{ok, RequestID} ->
|
httpc:request(
|
||||||
?tp(debug, jwks_endpoint_request_ok, #{request_id => RequestID}),
|
get,
|
||||||
State#{request_id := RequestID}
|
{Endpoint, [{"Accept", "application/json"}]},
|
||||||
end,
|
HTTPOpts,
|
||||||
|
[{body_format, binary}, {sync, false}, {receiver, self()}]
|
||||||
|
)
|
||||||
|
of
|
||||||
|
{error, Reason} ->
|
||||||
|
?tp(warning, jwks_endpoint_request_fail, #{
|
||||||
|
endpoint => Endpoint,
|
||||||
|
http_opts => HTTPOpts,
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
|
State;
|
||||||
|
{ok, RequestID} ->
|
||||||
|
?tp(debug, jwks_endpoint_request_ok, #{request_id => RequestID}),
|
||||||
|
State#{request_id := RequestID}
|
||||||
|
end,
|
||||||
ensure_expiry_timer(NState).
|
ensure_expiry_timer(NState).
|
||||||
|
|
||||||
ensure_expiry_timer(State = #{refresh_interval := Interval}) ->
|
ensure_expiry_timer(State = #{refresh_interval := Interval}) ->
|
||||||
|
|
|
||||||
|
|
@ -23,17 +23,19 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
]).
|
fields/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Hocon Schema
|
%% Hocon Schema
|
||||||
|
|
@ -42,49 +44,56 @@
|
||||||
namespace() -> "authn-jwt".
|
namespace() -> "authn-jwt".
|
||||||
|
|
||||||
roots() ->
|
roots() ->
|
||||||
[ {?CONF_NS,
|
[
|
||||||
hoconsc:mk(hoconsc:union(refs()),
|
{?CONF_NS,
|
||||||
#{})}
|
hoconsc:mk(
|
||||||
|
hoconsc:union(refs()),
|
||||||
|
#{}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
fields('hmac-based') ->
|
fields('hmac-based') ->
|
||||||
[ {use_jwks, {enum, [false]}}
|
[
|
||||||
, {algorithm, {enum, ['hmac-based']}}
|
{use_jwks, {enum, [false]}},
|
||||||
, {secret, fun secret/1}
|
{algorithm, {enum, ['hmac-based']}},
|
||||||
, {secret_base64_encoded, fun secret_base64_encoded/1}
|
{secret, fun secret/1},
|
||||||
|
{secret_base64_encoded, fun secret_base64_encoded/1}
|
||||||
] ++ common_fields();
|
] ++ common_fields();
|
||||||
|
|
||||||
fields('public-key') ->
|
fields('public-key') ->
|
||||||
[ {use_jwks, {enum, [false]}}
|
[
|
||||||
, {algorithm, {enum, ['public-key']}}
|
{use_jwks, {enum, [false]}},
|
||||||
, {certificate, fun certificate/1}
|
{algorithm, {enum, ['public-key']}},
|
||||||
|
{certificate, fun certificate/1}
|
||||||
] ++ common_fields();
|
] ++ common_fields();
|
||||||
|
|
||||||
fields('jwks') ->
|
fields('jwks') ->
|
||||||
[ {use_jwks, {enum, [true]}}
|
[
|
||||||
, {endpoint, fun endpoint/1}
|
{use_jwks, {enum, [true]}},
|
||||||
, {refresh_interval, fun refresh_interval/1}
|
{endpoint, fun endpoint/1},
|
||||||
, {ssl, #{type => hoconsc:union([ hoconsc:ref(?MODULE, ssl_enable)
|
{refresh_interval, fun refresh_interval/1},
|
||||||
, hoconsc:ref(?MODULE, ssl_disable)
|
{ssl, #{
|
||||||
]),
|
type => hoconsc:union([
|
||||||
default => #{<<"enable">> => false}}}
|
hoconsc:ref(?MODULE, ssl_enable),
|
||||||
|
hoconsc:ref(?MODULE, ssl_disable)
|
||||||
|
]),
|
||||||
|
default => #{<<"enable">> => false}
|
||||||
|
}}
|
||||||
] ++ common_fields();
|
] ++ common_fields();
|
||||||
|
|
||||||
fields(ssl_enable) ->
|
fields(ssl_enable) ->
|
||||||
[ {enable, #{type => true}}
|
[
|
||||||
, {cacertfile, fun cacertfile/1}
|
{enable, #{type => true}},
|
||||||
, {certfile, fun certfile/1}
|
{cacertfile, fun cacertfile/1},
|
||||||
, {keyfile, fun keyfile/1}
|
{certfile, fun certfile/1},
|
||||||
, {verify, fun verify/1}
|
{keyfile, fun keyfile/1},
|
||||||
, {server_name_indication, fun server_name_indication/1}
|
{verify, fun verify/1},
|
||||||
|
{server_name_indication, fun server_name_indication/1}
|
||||||
];
|
];
|
||||||
|
|
||||||
fields(ssl_disable) ->
|
fields(ssl_disable) ->
|
||||||
[ {enable, #{type => false}} ].
|
[{enable, #{type => false}}].
|
||||||
|
|
||||||
common_fields() ->
|
common_fields() ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('jwt')}
|
[
|
||||||
, {verify_claims, fun verify_claims/1}
|
{mechanism, emqx_authn_schema:mechanism('jwt')},
|
||||||
|
{verify_claims, fun verify_claims/1}
|
||||||
] ++ emqx_authn_schema:common_fields().
|
] ++ emqx_authn_schema:common_fields().
|
||||||
|
|
||||||
secret(type) -> binary();
|
secret(type) -> binary();
|
||||||
|
|
@ -121,24 +130,29 @@ verify(_) -> undefined.
|
||||||
server_name_indication(type) -> string();
|
server_name_indication(type) -> string();
|
||||||
server_name_indication(_) -> undefined.
|
server_name_indication(_) -> undefined.
|
||||||
|
|
||||||
verify_claims(type) -> list();
|
verify_claims(type) ->
|
||||||
verify_claims(default) -> #{};
|
list();
|
||||||
verify_claims(validator) -> [fun do_check_verify_claims/1];
|
verify_claims(default) ->
|
||||||
|
#{};
|
||||||
|
verify_claims(validator) ->
|
||||||
|
[fun do_check_verify_claims/1];
|
||||||
verify_claims(converter) ->
|
verify_claims(converter) ->
|
||||||
fun(VerifyClaims) ->
|
fun(VerifyClaims) ->
|
||||||
[{to_binary(K), V} || {K, V} <- maps:to_list(VerifyClaims)]
|
[{to_binary(K), V} || {K, V} <- maps:to_list(VerifyClaims)]
|
||||||
end;
|
end;
|
||||||
verify_claims(_) -> undefined.
|
verify_claims(_) ->
|
||||||
|
undefined.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% APIs
|
%% APIs
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
refs() ->
|
refs() ->
|
||||||
[ hoconsc:ref(?MODULE, 'hmac-based')
|
[
|
||||||
, hoconsc:ref(?MODULE, 'public-key')
|
hoconsc:ref(?MODULE, 'hmac-based'),
|
||||||
, hoconsc:ref(?MODULE, 'jwks')
|
hoconsc:ref(?MODULE, 'public-key'),
|
||||||
].
|
hoconsc:ref(?MODULE, 'jwks')
|
||||||
|
].
|
||||||
|
|
||||||
create(_AuthenticatorID, Config) ->
|
create(_AuthenticatorID, Config) ->
|
||||||
create(Config).
|
create(Config).
|
||||||
|
|
@ -146,18 +160,22 @@ create(_AuthenticatorID, Config) ->
|
||||||
create(#{verify_claims := VerifyClaims} = Config) ->
|
create(#{verify_claims := VerifyClaims} = Config) ->
|
||||||
create2(Config#{verify_claims => handle_verify_claims(VerifyClaims)}).
|
create2(Config#{verify_claims => handle_verify_claims(VerifyClaims)}).
|
||||||
|
|
||||||
update(#{use_jwks := false} = Config,
|
update(
|
||||||
#{jwk := Connector})
|
#{use_jwks := false} = Config,
|
||||||
when is_pid(Connector) ->
|
#{jwk := Connector}
|
||||||
|
) when
|
||||||
|
is_pid(Connector)
|
||||||
|
->
|
||||||
_ = emqx_authn_jwks_connector:stop(Connector),
|
_ = emqx_authn_jwks_connector:stop(Connector),
|
||||||
create(Config);
|
create(Config);
|
||||||
|
|
||||||
update(#{use_jwks := false} = Config, _State) ->
|
update(#{use_jwks := false} = Config, _State) ->
|
||||||
create(Config);
|
create(Config);
|
||||||
|
update(
|
||||||
update(#{use_jwks := true} = Config,
|
#{use_jwks := true} = Config,
|
||||||
#{jwk := Connector} = State)
|
#{jwk := Connector} = State
|
||||||
when is_pid(Connector) ->
|
) when
|
||||||
|
is_pid(Connector)
|
||||||
|
->
|
||||||
ok = emqx_authn_jwks_connector:update(Connector, connector_opts(Config)),
|
ok = emqx_authn_jwks_connector:update(Connector, connector_opts(Config)),
|
||||||
case maps:get(verify_cliams, Config, undefined) of
|
case maps:get(verify_cliams, Config, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
|
|
@ -165,21 +183,23 @@ update(#{use_jwks := true} = Config,
|
||||||
VerifyClaims ->
|
VerifyClaims ->
|
||||||
{ok, State#{verify_claims => handle_verify_claims(VerifyClaims)}}
|
{ok, State#{verify_claims => handle_verify_claims(VerifyClaims)}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
update(#{use_jwks := true} = Config, _State) ->
|
update(#{use_jwks := true} = Config, _State) ->
|
||||||
create(Config).
|
create(Config).
|
||||||
|
|
||||||
authenticate(#{auth_method := _}, _) ->
|
authenticate(#{auth_method := _}, _) ->
|
||||||
ignore;
|
ignore;
|
||||||
authenticate(Credential = #{password := JWT}, #{jwk := JWK,
|
authenticate(Credential = #{password := JWT}, #{
|
||||||
verify_claims := VerifyClaims0}) ->
|
jwk := JWK,
|
||||||
JWKs = case erlang:is_pid(JWK) of
|
verify_claims := VerifyClaims0
|
||||||
false ->
|
}) ->
|
||||||
[JWK];
|
JWKs =
|
||||||
true ->
|
case erlang:is_pid(JWK) of
|
||||||
{ok, JWKs0} = emqx_authn_jwks_connector:get_jwks(JWK),
|
false ->
|
||||||
JWKs0
|
[JWK];
|
||||||
end,
|
true ->
|
||||||
|
{ok, JWKs0} = emqx_authn_jwks_connector:get_jwks(JWK),
|
||||||
|
JWKs0
|
||||||
|
end,
|
||||||
VerifyClaims = replace_placeholder(VerifyClaims0, Credential),
|
VerifyClaims = replace_placeholder(VerifyClaims0, Credential),
|
||||||
case verify(JWT, JWKs, VerifyClaims) of
|
case verify(JWT, JWKs, VerifyClaims) of
|
||||||
{ok, Extra} -> {ok, Extra};
|
{ok, Extra} -> {ok, Extra};
|
||||||
|
|
@ -197,41 +217,54 @@ destroy(_) ->
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
create2(#{use_jwks := false,
|
create2(#{
|
||||||
algorithm := 'hmac-based',
|
use_jwks := false,
|
||||||
secret := Secret0,
|
algorithm := 'hmac-based',
|
||||||
secret_base64_encoded := Base64Encoded,
|
secret := Secret0,
|
||||||
verify_claims := VerifyClaims}) ->
|
secret_base64_encoded := Base64Encoded,
|
||||||
|
verify_claims := VerifyClaims
|
||||||
|
}) ->
|
||||||
case may_decode_secret(Base64Encoded, Secret0) of
|
case may_decode_secret(Base64Encoded, Secret0) of
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason};
|
{error, Reason};
|
||||||
Secret ->
|
Secret ->
|
||||||
JWK = jose_jwk:from_oct(Secret),
|
JWK = jose_jwk:from_oct(Secret),
|
||||||
{ok, #{jwk => JWK,
|
{ok, #{
|
||||||
verify_claims => VerifyClaims}}
|
jwk => JWK,
|
||||||
|
verify_claims => VerifyClaims
|
||||||
|
}}
|
||||||
end;
|
end;
|
||||||
|
create2(#{
|
||||||
create2(#{use_jwks := false,
|
use_jwks := false,
|
||||||
algorithm := 'public-key',
|
algorithm := 'public-key',
|
||||||
certificate := Certificate,
|
certificate := Certificate,
|
||||||
verify_claims := VerifyClaims}) ->
|
verify_claims := VerifyClaims
|
||||||
|
}) ->
|
||||||
JWK = create_jwk_from_pem_or_file(Certificate),
|
JWK = create_jwk_from_pem_or_file(Certificate),
|
||||||
{ok, #{jwk => JWK,
|
{ok, #{
|
||||||
verify_claims => VerifyClaims}};
|
jwk => JWK,
|
||||||
|
verify_claims => VerifyClaims
|
||||||
create2(#{use_jwks := true,
|
}};
|
||||||
verify_claims := VerifyClaims} = Config) ->
|
create2(
|
||||||
|
#{
|
||||||
|
use_jwks := true,
|
||||||
|
verify_claims := VerifyClaims
|
||||||
|
} = Config
|
||||||
|
) ->
|
||||||
case emqx_authn_jwks_connector:start_link(connector_opts(Config)) of
|
case emqx_authn_jwks_connector:start_link(connector_opts(Config)) of
|
||||||
{ok, Connector} ->
|
{ok, Connector} ->
|
||||||
{ok, #{jwk => Connector,
|
{ok, #{
|
||||||
verify_claims => VerifyClaims}};
|
jwk => Connector,
|
||||||
|
verify_claims => VerifyClaims
|
||||||
|
}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
create_jwk_from_pem_or_file(CertfileOrFilePath)
|
create_jwk_from_pem_or_file(CertfileOrFilePath) when
|
||||||
when is_binary(CertfileOrFilePath);
|
is_binary(CertfileOrFilePath);
|
||||||
is_list(CertfileOrFilePath) ->
|
is_list(CertfileOrFilePath)
|
||||||
|
->
|
||||||
case filelib:is_file(CertfileOrFilePath) of
|
case filelib:is_file(CertfileOrFilePath) of
|
||||||
true ->
|
true ->
|
||||||
jose_jwk:from_pem_file(CertfileOrFilePath);
|
jose_jwk:from_pem_file(CertfileOrFilePath);
|
||||||
|
|
@ -240,18 +273,20 @@ create_jwk_from_pem_or_file(CertfileOrFilePath)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
connector_opts(#{ssl := #{enable := Enable} = SSL} = Config) ->
|
connector_opts(#{ssl := #{enable := Enable} = SSL} = Config) ->
|
||||||
SSLOpts = case Enable of
|
SSLOpts =
|
||||||
true -> maps:without([enable], SSL);
|
case Enable of
|
||||||
false -> #{}
|
true -> maps:without([enable], SSL);
|
||||||
end,
|
false -> #{}
|
||||||
|
end,
|
||||||
Config#{ssl_opts => SSLOpts}.
|
Config#{ssl_opts => SSLOpts}.
|
||||||
|
|
||||||
|
may_decode_secret(false, Secret) ->
|
||||||
may_decode_secret(false, Secret) -> Secret;
|
Secret;
|
||||||
may_decode_secret(true, Secret) ->
|
may_decode_secret(true, Secret) ->
|
||||||
try base64:decode(Secret)
|
try
|
||||||
|
base64:decode(Secret)
|
||||||
catch
|
catch
|
||||||
error : _ ->
|
error:_ ->
|
||||||
{error, {invalid_parameter, secret}}
|
{error, {invalid_parameter, secret}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -288,15 +323,18 @@ verify(JWS, [JWK | More], VerifyClaims) ->
|
||||||
|
|
||||||
verify_claims(Claims, VerifyClaims0) ->
|
verify_claims(Claims, VerifyClaims0) ->
|
||||||
Now = os:system_time(seconds),
|
Now = os:system_time(seconds),
|
||||||
VerifyClaims = [{<<"exp">>, fun(ExpireTime) ->
|
VerifyClaims =
|
||||||
Now < ExpireTime
|
[
|
||||||
end},
|
{<<"exp">>, fun(ExpireTime) ->
|
||||||
{<<"iat">>, fun(IssueAt) ->
|
Now < ExpireTime
|
||||||
IssueAt =< Now
|
end},
|
||||||
end},
|
{<<"iat">>, fun(IssueAt) ->
|
||||||
{<<"nbf">>, fun(NotBefore) ->
|
IssueAt =< Now
|
||||||
NotBefore =< Now
|
end},
|
||||||
end}] ++ VerifyClaims0,
|
{<<"nbf">>, fun(NotBefore) ->
|
||||||
|
NotBefore =< Now
|
||||||
|
end}
|
||||||
|
] ++ VerifyClaims0,
|
||||||
do_verify_claims(Claims, VerifyClaims).
|
do_verify_claims(Claims, VerifyClaims).
|
||||||
|
|
||||||
do_verify_claims(_Claims, []) ->
|
do_verify_claims(_Claims, []) ->
|
||||||
|
|
@ -327,8 +365,8 @@ do_check_verify_claims([]) ->
|
||||||
true;
|
true;
|
||||||
do_check_verify_claims([{Name, Expected} | More]) ->
|
do_check_verify_claims([{Name, Expected} | More]) ->
|
||||||
check_claim_name(Name) andalso
|
check_claim_name(Name) andalso
|
||||||
check_claim_expected(Expected) andalso
|
check_claim_expected(Expected) andalso
|
||||||
do_check_verify_claims(More).
|
do_check_verify_claims(More).
|
||||||
|
|
||||||
check_claim_name(exp) ->
|
check_claim_name(exp) ->
|
||||||
false;
|
false;
|
||||||
|
|
|
||||||
|
|
@ -23,40 +23,45 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
]).
|
fields/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ import_users/2
|
-export([
|
||||||
, add_user/2
|
import_users/2,
|
||||||
, delete_user/2
|
add_user/2,
|
||||||
, update_user/3
|
delete_user/2,
|
||||||
, lookup_user/2
|
update_user/3,
|
||||||
, list_users/2
|
lookup_user/2,
|
||||||
]).
|
list_users/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ query/4
|
-export([
|
||||||
, format_user_info/1
|
query/4,
|
||||||
, group_match_spec/1]).
|
format_user_info/1,
|
||||||
|
group_match_spec/1
|
||||||
|
]).
|
||||||
|
|
||||||
-type user_id_type() :: clientid | username.
|
-type user_id_type() :: clientid | username.
|
||||||
-type user_group() :: binary().
|
-type user_group() :: binary().
|
||||||
-type user_id() :: binary().
|
-type user_id() :: binary().
|
||||||
|
|
||||||
-record(user_info,
|
-record(user_info, {
|
||||||
{ user_id :: {user_group(), user_id()}
|
user_id :: {user_group(), user_id()},
|
||||||
, password_hash :: binary()
|
password_hash :: binary(),
|
||||||
, salt :: binary()
|
salt :: binary(),
|
||||||
, is_superuser :: boolean()
|
is_superuser :: boolean()
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-reflect_type([user_id_type/0]).
|
-reflect_type([user_id_type/0]).
|
||||||
|
|
||||||
|
|
@ -65,9 +70,11 @@
|
||||||
-boot_mnesia({mnesia, [boot]}).
|
-boot_mnesia({mnesia, [boot]}).
|
||||||
|
|
||||||
-define(TAB, ?MODULE).
|
-define(TAB, ?MODULE).
|
||||||
-define(AUTHN_QSCHEMA, [ {<<"like_username">>, binary}
|
-define(AUTHN_QSCHEMA, [
|
||||||
, {<<"like_clientid">>, binary}
|
{<<"like_username">>, binary},
|
||||||
, {<<"user_group">>, binary}]).
|
{<<"like_clientid">>, binary},
|
||||||
|
{<<"user_group">>, binary}
|
||||||
|
]).
|
||||||
-define(QUERY_FUN, {?MODULE, query}).
|
-define(QUERY_FUN, {?MODULE, query}).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
@ -75,14 +82,15 @@
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
%% @doc Create or replicate tables.
|
%% @doc Create or replicate tables.
|
||||||
-spec(mnesia(boot | copy) -> ok).
|
-spec mnesia(boot | copy) -> ok.
|
||||||
mnesia(boot) ->
|
mnesia(boot) ->
|
||||||
ok = mria:create_table(?TAB, [
|
ok = mria:create_table(?TAB, [
|
||||||
{rlog_shard, ?AUTH_SHARD},
|
{rlog_shard, ?AUTH_SHARD},
|
||||||
{storage, disc_copies},
|
{storage, disc_copies},
|
||||||
{record_name, user_info},
|
{record_name, user_info},
|
||||||
{attributes, record_info(fields, user_info)},
|
{attributes, record_info(fields, user_info)},
|
||||||
{storage_properties, [{ets, [{read_concurrency, true}]}]}]).
|
{storage_properties, [{ets, [{read_concurrency, true}]}]}
|
||||||
|
]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Hocon Schema
|
%% Hocon Schema
|
||||||
|
|
@ -93,10 +101,11 @@ namespace() -> "authn-builtin_db".
|
||||||
roots() -> [?CONF_NS].
|
roots() -> [?CONF_NS].
|
||||||
|
|
||||||
fields(?CONF_NS) ->
|
fields(?CONF_NS) ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('password_based')}
|
[
|
||||||
, {backend, emqx_authn_schema:backend('built_in_database')}
|
{mechanism, emqx_authn_schema:mechanism('password_based')},
|
||||||
, {user_id_type, fun user_id_type/1}
|
{backend, emqx_authn_schema:backend('built_in_database')},
|
||||||
, {password_hash_algorithm, fun emqx_authn_password_hashing:type_rw/1}
|
{user_id_type, fun user_id_type/1},
|
||||||
|
{password_hash_algorithm, fun emqx_authn_password_hashing:type_rw/1}
|
||||||
] ++ emqx_authn_schema:common_fields().
|
] ++ emqx_authn_schema:common_fields().
|
||||||
|
|
||||||
user_id_type(type) -> user_id_type();
|
user_id_type(type) -> user_id_type();
|
||||||
|
|
@ -108,15 +117,21 @@ user_id_type(_) -> undefined.
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
refs() ->
|
refs() ->
|
||||||
[hoconsc:ref(?MODULE, ?CONF_NS)].
|
[hoconsc:ref(?MODULE, ?CONF_NS)].
|
||||||
|
|
||||||
create(AuthenticatorID,
|
create(
|
||||||
#{user_id_type := Type,
|
AuthenticatorID,
|
||||||
password_hash_algorithm := Algorithm}) ->
|
#{
|
||||||
|
user_id_type := Type,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
}
|
||||||
|
) ->
|
||||||
ok = emqx_authn_password_hashing:init(Algorithm),
|
ok = emqx_authn_password_hashing:init(Algorithm),
|
||||||
State = #{user_group => AuthenticatorID,
|
State = #{
|
||||||
user_id_type => Type,
|
user_group => AuthenticatorID,
|
||||||
password_hash_algorithm => Algorithm},
|
user_id_type => Type,
|
||||||
|
password_hash_algorithm => Algorithm
|
||||||
|
},
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
update(Config, #{user_group := ID}) ->
|
update(Config, #{user_group := ID}) ->
|
||||||
|
|
@ -124,17 +139,24 @@ update(Config, #{user_group := ID}) ->
|
||||||
|
|
||||||
authenticate(#{auth_method := _}, _) ->
|
authenticate(#{auth_method := _}, _) ->
|
||||||
ignore;
|
ignore;
|
||||||
authenticate(#{password := Password} = Credential,
|
authenticate(
|
||||||
#{user_group := UserGroup,
|
#{password := Password} = Credential,
|
||||||
user_id_type := Type,
|
#{
|
||||||
password_hash_algorithm := Algorithm}) ->
|
user_group := UserGroup,
|
||||||
|
user_id_type := Type,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
}
|
||||||
|
) ->
|
||||||
UserID = get_user_identity(Credential, Type),
|
UserID = get_user_identity(Credential, Type),
|
||||||
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
||||||
[] ->
|
[] ->
|
||||||
ignore;
|
ignore;
|
||||||
[#user_info{password_hash = PasswordHash, salt = Salt, is_superuser = IsSuperuser}] ->
|
[#user_info{password_hash = PasswordHash, salt = Salt, is_superuser = IsSuperuser}] ->
|
||||||
case emqx_authn_password_hashing:check_password(
|
case
|
||||||
Algorithm, Salt, PasswordHash, Password) of
|
emqx_authn_password_hashing:check_password(
|
||||||
|
Algorithm, Salt, PasswordHash, Password
|
||||||
|
)
|
||||||
|
of
|
||||||
true -> {ok, #{is_superuser => IsSuperuser}};
|
true -> {ok, #{is_superuser => IsSuperuser}};
|
||||||
false -> {error, bad_username_or_password}
|
false -> {error, bad_username_or_password}
|
||||||
end
|
end
|
||||||
|
|
@ -142,14 +164,15 @@ authenticate(#{password := Password} = Credential,
|
||||||
|
|
||||||
destroy(#{user_group := UserGroup}) ->
|
destroy(#{user_group := UserGroup}) ->
|
||||||
trans(
|
trans(
|
||||||
fun() ->
|
fun() ->
|
||||||
ok = lists:foreach(
|
ok = lists:foreach(
|
||||||
fun(User) ->
|
fun(User) ->
|
||||||
mnesia:delete_object(?TAB, User, write)
|
mnesia:delete_object(?TAB, User, write)
|
||||||
end,
|
end,
|
||||||
mnesia:select(?TAB, group_match_spec(UserGroup), write))
|
mnesia:select(?TAB, group_match_spec(UserGroup), write)
|
||||||
end).
|
)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
import_users(Filename0, State) ->
|
import_users(Filename0, State) ->
|
||||||
Filename = to_binary(Filename0),
|
Filename = to_binary(Filename0),
|
||||||
|
|
@ -164,10 +187,16 @@ import_users(Filename0, State) ->
|
||||||
{error, {unsupported_file_format, Extension}}
|
{error, {unsupported_file_format, Extension}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
add_user(#{user_id := UserID,
|
add_user(
|
||||||
password := Password} = UserInfo,
|
#{
|
||||||
#{user_group := UserGroup,
|
user_id := UserID,
|
||||||
password_hash_algorithm := Algorithm}) ->
|
password := Password
|
||||||
|
} = UserInfo,
|
||||||
|
#{
|
||||||
|
user_group := UserGroup,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
}
|
||||||
|
) ->
|
||||||
trans(
|
trans(
|
||||||
fun() ->
|
fun() ->
|
||||||
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
||||||
|
|
@ -179,7 +208,8 @@ add_user(#{user_id := UserID,
|
||||||
[_] ->
|
[_] ->
|
||||||
{error, already_exist}
|
{error, already_exist}
|
||||||
end
|
end
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
delete_user(UserID, #{user_group := UserGroup}) ->
|
delete_user(UserID, #{user_group := UserGroup}) ->
|
||||||
trans(
|
trans(
|
||||||
|
|
@ -190,31 +220,44 @@ delete_user(UserID, #{user_group := UserGroup}) ->
|
||||||
[_] ->
|
[_] ->
|
||||||
mnesia:delete(?TAB, {UserGroup, UserID}, write)
|
mnesia:delete(?TAB, {UserGroup, UserID}, write)
|
||||||
end
|
end
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
update_user(UserID, UserInfo,
|
update_user(
|
||||||
#{user_group := UserGroup,
|
UserID,
|
||||||
password_hash_algorithm := Algorithm}) ->
|
UserInfo,
|
||||||
|
#{
|
||||||
|
user_group := UserGroup,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
}
|
||||||
|
) ->
|
||||||
trans(
|
trans(
|
||||||
fun() ->
|
fun() ->
|
||||||
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
case mnesia:read(?TAB, {UserGroup, UserID}, write) of
|
||||||
[] ->
|
[] ->
|
||||||
{error, not_found};
|
{error, not_found};
|
||||||
[#user_info{ password_hash = PasswordHash
|
[
|
||||||
, salt = Salt
|
#user_info{
|
||||||
, is_superuser = IsSuperuser}] ->
|
password_hash = PasswordHash,
|
||||||
|
salt = Salt,
|
||||||
|
is_superuser = IsSuperuser
|
||||||
|
}
|
||||||
|
] ->
|
||||||
NSuperuser = maps:get(is_superuser, UserInfo, IsSuperuser),
|
NSuperuser = maps:get(is_superuser, UserInfo, IsSuperuser),
|
||||||
{NPasswordHash, NSalt} = case UserInfo of
|
{NPasswordHash, NSalt} =
|
||||||
#{password := Password} ->
|
case UserInfo of
|
||||||
emqx_authn_password_hashing:hash(
|
#{password := Password} ->
|
||||||
Algorithm, Password);
|
emqx_authn_password_hashing:hash(
|
||||||
#{} ->
|
Algorithm, Password
|
||||||
{PasswordHash, Salt}
|
);
|
||||||
end,
|
#{} ->
|
||||||
|
{PasswordHash, Salt}
|
||||||
|
end,
|
||||||
insert_user(UserGroup, UserID, NPasswordHash, NSalt, NSuperuser),
|
insert_user(UserGroup, UserID, NPasswordHash, NSalt, NSuperuser),
|
||||||
{ok, #{user_id => UserID, is_superuser => NSuperuser}}
|
{ok, #{user_id => UserID, is_superuser => NSuperuser}}
|
||||||
end
|
end
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
lookup_user(UserID, #{user_group := UserGroup}) ->
|
lookup_user(UserID, #{user_group := UserGroup}) ->
|
||||||
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
case mnesia:dirty_read(?TAB, {UserGroup, UserID}) of
|
||||||
|
|
@ -233,14 +276,23 @@ list_users(QueryString, #{user_group := UserGroup}) ->
|
||||||
|
|
||||||
query(Tab, {QString, []}, Continuation, Limit) ->
|
query(Tab, {QString, []}, Continuation, Limit) ->
|
||||||
Ms = ms_from_qstring(QString),
|
Ms = ms_from_qstring(QString),
|
||||||
emqx_mgmt_api:select_table_with_count(Tab, Ms, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(
|
||||||
fun format_user_info/1);
|
Tab,
|
||||||
|
Ms,
|
||||||
|
Continuation,
|
||||||
|
Limit,
|
||||||
|
fun format_user_info/1
|
||||||
|
);
|
||||||
query(Tab, {QString, FuzzyQString}, Continuation, Limit) ->
|
query(Tab, {QString, FuzzyQString}, Continuation, Limit) ->
|
||||||
Ms = ms_from_qstring(QString),
|
Ms = ms_from_qstring(QString),
|
||||||
FuzzyFilterFun = fuzzy_filter_fun(FuzzyQString),
|
FuzzyFilterFun = fuzzy_filter_fun(FuzzyQString),
|
||||||
emqx_mgmt_api:select_table_with_count(Tab, {Ms, FuzzyFilterFun}, Continuation, Limit,
|
emqx_mgmt_api:select_table_with_count(
|
||||||
fun format_user_info/1).
|
Tab,
|
||||||
|
{Ms, FuzzyFilterFun},
|
||||||
|
Continuation,
|
||||||
|
Limit,
|
||||||
|
fun format_user_info/1
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Match funcs
|
%% Match funcs
|
||||||
|
|
@ -248,17 +300,23 @@ query(Tab, {QString, FuzzyQString}, Continuation, Limit) ->
|
||||||
%% Fuzzy username funcs
|
%% Fuzzy username funcs
|
||||||
fuzzy_filter_fun(Fuzzy) ->
|
fuzzy_filter_fun(Fuzzy) ->
|
||||||
fun(MsRaws) when is_list(MsRaws) ->
|
fun(MsRaws) when is_list(MsRaws) ->
|
||||||
lists:filter( fun(E) -> run_fuzzy_filter(E, Fuzzy) end
|
lists:filter(
|
||||||
, MsRaws)
|
fun(E) -> run_fuzzy_filter(E, Fuzzy) end,
|
||||||
|
MsRaws
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
run_fuzzy_filter(_, []) ->
|
run_fuzzy_filter(_, []) ->
|
||||||
true;
|
true;
|
||||||
run_fuzzy_filter( E = #user_info{user_id = {_, UserID}}
|
run_fuzzy_filter(
|
||||||
, [{username, like, UsernameSubStr} | Fuzzy]) ->
|
E = #user_info{user_id = {_, UserID}},
|
||||||
|
[{username, like, UsernameSubStr} | Fuzzy]
|
||||||
|
) ->
|
||||||
binary:match(UserID, UsernameSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy);
|
binary:match(UserID, UsernameSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy);
|
||||||
run_fuzzy_filter( E = #user_info{user_id = {_, UserID}}
|
run_fuzzy_filter(
|
||||||
, [{clientid, like, ClientIDSubStr} | Fuzzy]) ->
|
E = #user_info{user_id = {_, UserID}},
|
||||||
|
[{clientid, like, ClientIDSubStr} | Fuzzy]
|
||||||
|
) ->
|
||||||
binary:match(UserID, ClientIDSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
|
binary:match(UserID, ClientIDSubStr) /= nomatch andalso run_fuzzy_filter(E, Fuzzy).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
@ -297,9 +355,15 @@ import_users_from_csv(Filename, #{user_group := UserGroup}) ->
|
||||||
|
|
||||||
import(_UserGroup, []) ->
|
import(_UserGroup, []) ->
|
||||||
ok;
|
ok;
|
||||||
import(UserGroup, [#{<<"user_id">> := UserID,
|
import(UserGroup, [
|
||||||
<<"password_hash">> := PasswordHash} = UserInfo | More])
|
#{
|
||||||
when is_binary(UserID) andalso is_binary(PasswordHash) ->
|
<<"user_id">> := UserID,
|
||||||
|
<<"password_hash">> := PasswordHash
|
||||||
|
} = UserInfo
|
||||||
|
| More
|
||||||
|
]) when
|
||||||
|
is_binary(UserID) andalso is_binary(PasswordHash)
|
||||||
|
->
|
||||||
Salt = maps:get(<<"salt">>, UserInfo, <<>>),
|
Salt = maps:get(<<"salt">>, UserInfo, <<>>),
|
||||||
IsSuperuser = maps:get(<<"is_superuser">>, UserInfo, false),
|
IsSuperuser = maps:get(<<"is_superuser">>, UserInfo, false),
|
||||||
insert_user(UserGroup, UserID, PasswordHash, Salt, IsSuperuser),
|
insert_user(UserGroup, UserID, PasswordHash, Salt, IsSuperuser),
|
||||||
|
|
@ -313,8 +377,11 @@ import(UserGroup, File, Seq) ->
|
||||||
{ok, Line} ->
|
{ok, Line} ->
|
||||||
Fields = binary:split(Line, [<<",">>, <<" ">>, <<"\n">>], [global, trim_all]),
|
Fields = binary:split(Line, [<<",">>, <<" ">>, <<"\n">>], [global, trim_all]),
|
||||||
case get_user_info_by_seq(Fields, Seq) of
|
case get_user_info_by_seq(Fields, Seq) of
|
||||||
{ok, #{user_id := UserID,
|
{ok,
|
||||||
password_hash := PasswordHash} = UserInfo} ->
|
#{
|
||||||
|
user_id := UserID,
|
||||||
|
password_hash := PasswordHash
|
||||||
|
} = UserInfo} ->
|
||||||
Salt = maps:get(salt, UserInfo, <<>>),
|
Salt = maps:get(salt, UserInfo, <<>>),
|
||||||
IsSuperuser = maps:get(is_superuser, UserInfo, false),
|
IsSuperuser = maps:get(is_superuser, UserInfo, false),
|
||||||
insert_user(UserGroup, UserID, PasswordHash, Salt, IsSuperuser),
|
insert_user(UserGroup, UserID, PasswordHash, Salt, IsSuperuser),
|
||||||
|
|
@ -360,10 +427,12 @@ get_user_info_by_seq(_, _, _) ->
|
||||||
{error, bad_format}.
|
{error, bad_format}.
|
||||||
|
|
||||||
insert_user(UserGroup, UserID, PasswordHash, Salt, IsSuperuser) ->
|
insert_user(UserGroup, UserID, PasswordHash, Salt, IsSuperuser) ->
|
||||||
UserInfo = #user_info{user_id = {UserGroup, UserID},
|
UserInfo = #user_info{
|
||||||
password_hash = PasswordHash,
|
user_id = {UserGroup, UserID},
|
||||||
salt = Salt,
|
password_hash = PasswordHash,
|
||||||
is_superuser = IsSuperuser},
|
salt = Salt,
|
||||||
|
is_superuser = IsSuperuser
|
||||||
|
},
|
||||||
mnesia:write(?TAB, UserInfo, write).
|
mnesia:write(?TAB, UserInfo, write).
|
||||||
|
|
||||||
%% TODO: Support other type
|
%% TODO: Support other type
|
||||||
|
|
@ -392,15 +461,21 @@ format_user_info(#user_info{user_id = {_, UserID}, is_superuser = IsSuperuser})
|
||||||
#{user_id => UserID, is_superuser => IsSuperuser}.
|
#{user_id => UserID, is_superuser => IsSuperuser}.
|
||||||
|
|
||||||
ms_from_qstring(QString) ->
|
ms_from_qstring(QString) ->
|
||||||
[Ms] = lists:foldl(fun({user_group, '=:=', UserGroup}, AccIn) ->
|
[Ms] = lists:foldl(
|
||||||
[group_match_spec(UserGroup) | AccIn];
|
fun
|
||||||
(_, AccIn) ->
|
({user_group, '=:=', UserGroup}, AccIn) ->
|
||||||
AccIn
|
[group_match_spec(UserGroup) | AccIn];
|
||||||
end, [], QString),
|
(_, AccIn) ->
|
||||||
|
AccIn
|
||||||
|
end,
|
||||||
|
[],
|
||||||
|
QString
|
||||||
|
),
|
||||||
Ms.
|
Ms.
|
||||||
|
|
||||||
group_match_spec(UserGroup) ->
|
group_match_spec(UserGroup) ->
|
||||||
ets:fun2ms(
|
ets:fun2ms(
|
||||||
fun(#user_info{user_id = {Group, _}} = User) when Group =:= UserGroup ->
|
fun(#user_info{user_id = {Group, _}} = User) when Group =:= UserGroup ->
|
||||||
User
|
User
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -23,18 +23,20 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
, desc/1
|
fields/1,
|
||||||
]).
|
desc/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Hocon Schema
|
%% Hocon Schema
|
||||||
|
|
@ -43,16 +45,18 @@
|
||||||
namespace() -> "authn-mongodb".
|
namespace() -> "authn-mongodb".
|
||||||
|
|
||||||
roots() ->
|
roots() ->
|
||||||
[ {?CONF_NS, hoconsc:mk(hoconsc:union(refs()),
|
[
|
||||||
#{})}
|
{?CONF_NS,
|
||||||
|
hoconsc:mk(
|
||||||
|
hoconsc:union(refs()),
|
||||||
|
#{}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
fields(standalone) ->
|
fields(standalone) ->
|
||||||
common_fields() ++ emqx_connector_mongo:fields(single);
|
common_fields() ++ emqx_connector_mongo:fields(single);
|
||||||
|
|
||||||
fields('replica-set') ->
|
fields('replica-set') ->
|
||||||
common_fields() ++ emqx_connector_mongo:fields(rs);
|
common_fields() ++ emqx_connector_mongo:fields(rs);
|
||||||
|
|
||||||
fields('sharded-cluster') ->
|
fields('sharded-cluster') ->
|
||||||
common_fields() ++ emqx_connector_mongo:fields(sharded).
|
common_fields() ++ emqx_connector_mongo:fields(sharded).
|
||||||
|
|
||||||
|
|
@ -66,26 +70,30 @@ desc(_) ->
|
||||||
undefined.
|
undefined.
|
||||||
|
|
||||||
common_fields() ->
|
common_fields() ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('password_based')}
|
[
|
||||||
, {backend, emqx_authn_schema:backend(mongodb)}
|
{mechanism, emqx_authn_schema:mechanism('password_based')},
|
||||||
, {collection, fun collection/1}
|
{backend, emqx_authn_schema:backend(mongodb)},
|
||||||
, {selector, fun selector/1}
|
{collection, fun collection/1},
|
||||||
, {password_hash_field, fun password_hash_field/1}
|
{selector, fun selector/1},
|
||||||
, {salt_field, fun salt_field/1}
|
{password_hash_field, fun password_hash_field/1},
|
||||||
, {is_superuser_field, fun is_superuser_field/1}
|
{salt_field, fun salt_field/1},
|
||||||
, {password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1}
|
{is_superuser_field, fun is_superuser_field/1},
|
||||||
|
{password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1}
|
||||||
] ++ emqx_authn_schema:common_fields().
|
] ++ emqx_authn_schema:common_fields().
|
||||||
|
|
||||||
collection(type) -> binary();
|
collection(type) -> binary();
|
||||||
collection(desc) -> "Collection used to store authentication data.";
|
collection(desc) -> "Collection used to store authentication data.";
|
||||||
collection(_) -> undefined.
|
collection(_) -> undefined.
|
||||||
|
|
||||||
selector(type) -> map();
|
selector(type) ->
|
||||||
selector(desc) -> "Statement that is executed during the authentication process. "
|
map();
|
||||||
"Commands can support following wildcards:\n"
|
selector(desc) ->
|
||||||
" - `${username}`: substituted with client's username\n"
|
"Statement that is executed during the authentication process. "
|
||||||
" - `${clientid}`: substituted with the clientid";
|
"Commands can support following wildcards:\n"
|
||||||
selector(_) -> undefined.
|
" - `${username}`: substituted with client's username\n"
|
||||||
|
" - `${clientid}`: substituted with the clientid";
|
||||||
|
selector(_) ->
|
||||||
|
undefined.
|
||||||
|
|
||||||
password_hash_field(type) -> binary();
|
password_hash_field(type) -> binary();
|
||||||
password_hash_field(desc) -> "Document field that contains password hash.";
|
password_hash_field(desc) -> "Document field that contains password hash.";
|
||||||
|
|
@ -106,9 +114,10 @@ is_superuser_field(_) -> undefined.
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
refs() ->
|
refs() ->
|
||||||
[ hoconsc:ref(?MODULE, standalone)
|
[
|
||||||
, hoconsc:ref(?MODULE, 'replica-set')
|
hoconsc:ref(?MODULE, standalone),
|
||||||
, hoconsc:ref(?MODULE, 'sharded-cluster')
|
hoconsc:ref(?MODULE, 'replica-set'),
|
||||||
|
hoconsc:ref(?MODULE, 'sharded-cluster')
|
||||||
].
|
].
|
||||||
|
|
||||||
create(_AuthenticatorID, Config) ->
|
create(_AuthenticatorID, Config) ->
|
||||||
|
|
@ -117,24 +126,32 @@ create(_AuthenticatorID, Config) ->
|
||||||
create(#{selector := Selector} = Config) ->
|
create(#{selector := Selector} = Config) ->
|
||||||
SelectorTemplate = emqx_authn_utils:parse_deep(Selector),
|
SelectorTemplate = emqx_authn_utils:parse_deep(Selector),
|
||||||
State = maps:with(
|
State = maps:with(
|
||||||
[collection,
|
[
|
||||||
password_hash_field,
|
collection,
|
||||||
salt_field,
|
password_hash_field,
|
||||||
is_superuser_field,
|
salt_field,
|
||||||
password_hash_algorithm,
|
is_superuser_field,
|
||||||
salt_position],
|
password_hash_algorithm,
|
||||||
Config),
|
salt_position
|
||||||
|
],
|
||||||
|
Config
|
||||||
|
),
|
||||||
#{password_hash_algorithm := Algorithm} = State,
|
#{password_hash_algorithm := Algorithm} = State,
|
||||||
ok = emqx_authn_password_hashing:init(Algorithm),
|
ok = emqx_authn_password_hashing:init(Algorithm),
|
||||||
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
||||||
NState = State#{
|
NState = State#{
|
||||||
selector_template => SelectorTemplate,
|
selector_template => SelectorTemplate,
|
||||||
resource_id => ResourceId},
|
resource_id => ResourceId
|
||||||
case emqx_resource:create_local(ResourceId,
|
},
|
||||||
?RESOURCE_GROUP,
|
case
|
||||||
emqx_connector_mongo,
|
emqx_resource:create_local(
|
||||||
Config,
|
ResourceId,
|
||||||
#{}) of
|
?RESOURCE_GROUP,
|
||||||
|
emqx_connector_mongo,
|
||||||
|
Config,
|
||||||
|
#{}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, already_created} ->
|
{ok, already_created} ->
|
||||||
{ok, NState};
|
{ok, NState};
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
|
|
@ -154,30 +171,39 @@ update(Config, State) ->
|
||||||
|
|
||||||
authenticate(#{auth_method := _}, _) ->
|
authenticate(#{auth_method := _}, _) ->
|
||||||
ignore;
|
ignore;
|
||||||
authenticate(#{password := Password} = Credential,
|
authenticate(
|
||||||
#{collection := Collection,
|
#{password := Password} = Credential,
|
||||||
selector_template := SelectorTemplate,
|
#{
|
||||||
resource_id := ResourceId} = State) ->
|
collection := Collection,
|
||||||
|
selector_template := SelectorTemplate,
|
||||||
|
resource_id := ResourceId
|
||||||
|
} = State
|
||||||
|
) ->
|
||||||
Selector = emqx_authn_utils:render_deep(SelectorTemplate, Credential),
|
Selector = emqx_authn_utils:render_deep(SelectorTemplate, Credential),
|
||||||
case emqx_resource:query(ResourceId, {find_one, Collection, Selector, #{}}) of
|
case emqx_resource:query(ResourceId, {find_one, Collection, Selector, #{}}) of
|
||||||
undefined -> ignore;
|
undefined ->
|
||||||
|
ignore;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{msg => "mongodb_query_failed",
|
?SLOG(error, #{
|
||||||
resource => ResourceId,
|
msg => "mongodb_query_failed",
|
||||||
collection => Collection,
|
resource => ResourceId,
|
||||||
selector => Selector,
|
collection => Collection,
|
||||||
reason => Reason}),
|
selector => Selector,
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
ignore;
|
ignore;
|
||||||
Doc ->
|
Doc ->
|
||||||
case check_password(Password, Doc, State) of
|
case check_password(Password, Doc, State) of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, is_superuser(Doc, State)};
|
{ok, is_superuser(Doc, State)};
|
||||||
{error, {cannot_find_password_hash_field, PasswordHashField}} ->
|
{error, {cannot_find_password_hash_field, PasswordHashField}} ->
|
||||||
?SLOG(error, #{msg => "cannot_find_password_hash_field",
|
?SLOG(error, #{
|
||||||
resource => ResourceId,
|
msg => "cannot_find_password_hash_field",
|
||||||
collection => Collection,
|
resource => ResourceId,
|
||||||
selector => Selector,
|
collection => Collection,
|
||||||
password_hash_field => PasswordHashField}),
|
selector => Selector,
|
||||||
|
password_hash_field => PasswordHashField
|
||||||
|
}),
|
||||||
ignore;
|
ignore;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
|
|
@ -194,18 +220,23 @@ destroy(#{resource_id := ResourceId}) ->
|
||||||
|
|
||||||
check_password(undefined, _Selected, _State) ->
|
check_password(undefined, _Selected, _State) ->
|
||||||
{error, bad_username_or_password};
|
{error, bad_username_or_password};
|
||||||
check_password(Password,
|
check_password(
|
||||||
Doc,
|
Password,
|
||||||
#{password_hash_algorithm := Algorithm,
|
Doc,
|
||||||
password_hash_field := PasswordHashField} = State) ->
|
#{
|
||||||
|
password_hash_algorithm := Algorithm,
|
||||||
|
password_hash_field := PasswordHashField
|
||||||
|
} = State
|
||||||
|
) ->
|
||||||
case maps:get(PasswordHashField, Doc, undefined) of
|
case maps:get(PasswordHashField, Doc, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
{error, {cannot_find_password_hash_field, PasswordHashField}};
|
{error, {cannot_find_password_hash_field, PasswordHashField}};
|
||||||
Hash ->
|
Hash ->
|
||||||
Salt = case maps:get(salt_field, State, undefined) of
|
Salt =
|
||||||
undefined -> <<>>;
|
case maps:get(salt_field, State, undefined) of
|
||||||
SaltField -> maps:get(SaltField, Doc, <<>>)
|
undefined -> <<>>;
|
||||||
end,
|
SaltField -> maps:get(SaltField, Doc, <<>>)
|
||||||
|
end,
|
||||||
case emqx_authn_password_hashing:check_password(Algorithm, Salt, Hash, Password) of
|
case emqx_authn_password_hashing:check_password(Algorithm, Salt, Hash, Password) of
|
||||||
true -> ok;
|
true -> ok;
|
||||||
false -> {error, bad_username_or_password}
|
false -> {error, bad_username_or_password}
|
||||||
|
|
|
||||||
|
|
@ -23,17 +23,19 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
]).
|
fields/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Hocon Schema
|
%% Hocon Schema
|
||||||
|
|
@ -44,13 +46,14 @@ namespace() -> "authn-mysql".
|
||||||
roots() -> [?CONF_NS].
|
roots() -> [?CONF_NS].
|
||||||
|
|
||||||
fields(?CONF_NS) ->
|
fields(?CONF_NS) ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('password_based')}
|
[
|
||||||
, {backend, emqx_authn_schema:backend(mysql)}
|
{mechanism, emqx_authn_schema:mechanism('password_based')},
|
||||||
, {password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1}
|
{backend, emqx_authn_schema:backend(mysql)},
|
||||||
, {query, fun query/1}
|
{password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1},
|
||||||
, {query_timeout, fun query_timeout/1}
|
{query, fun query/1},
|
||||||
] ++ emqx_authn_schema:common_fields()
|
{query_timeout, fun query_timeout/1}
|
||||||
++ emqx_connector_mysql:fields(config).
|
] ++ emqx_authn_schema:common_fields() ++
|
||||||
|
emqx_connector_mysql:fields(config).
|
||||||
|
|
||||||
query(type) -> string();
|
query(type) -> string();
|
||||||
query(_) -> undefined.
|
query(_) -> undefined.
|
||||||
|
|
@ -64,28 +67,37 @@ query_timeout(_) -> undefined.
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
refs() ->
|
refs() ->
|
||||||
[hoconsc:ref(?MODULE, ?CONF_NS)].
|
[hoconsc:ref(?MODULE, ?CONF_NS)].
|
||||||
|
|
||||||
create(_AuthenticatorID, Config) ->
|
create(_AuthenticatorID, Config) ->
|
||||||
create(Config).
|
create(Config).
|
||||||
|
|
||||||
create(#{password_hash_algorithm := Algorithm,
|
create(
|
||||||
query := Query0,
|
#{
|
||||||
query_timeout := QueryTimeout
|
password_hash_algorithm := Algorithm,
|
||||||
} = Config) ->
|
query := Query0,
|
||||||
|
query_timeout := QueryTimeout
|
||||||
|
} = Config
|
||||||
|
) ->
|
||||||
ok = emqx_authn_password_hashing:init(Algorithm),
|
ok = emqx_authn_password_hashing:init(Algorithm),
|
||||||
{Query, PlaceHolders} = emqx_authn_utils:parse_sql(Query0, '?'),
|
{Query, PlaceHolders} = emqx_authn_utils:parse_sql(Query0, '?'),
|
||||||
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
||||||
State = #{password_hash_algorithm => Algorithm,
|
State = #{
|
||||||
query => Query,
|
password_hash_algorithm => Algorithm,
|
||||||
placeholders => PlaceHolders,
|
query => Query,
|
||||||
query_timeout => QueryTimeout,
|
placeholders => PlaceHolders,
|
||||||
resource_id => ResourceId},
|
query_timeout => QueryTimeout,
|
||||||
case emqx_resource:create_local(ResourceId,
|
resource_id => ResourceId
|
||||||
?RESOURCE_GROUP,
|
},
|
||||||
emqx_connector_mysql,
|
case
|
||||||
Config,
|
emqx_resource:create_local(
|
||||||
#{}) of
|
ResourceId,
|
||||||
|
?RESOURCE_GROUP,
|
||||||
|
emqx_connector_mysql,
|
||||||
|
Config,
|
||||||
|
#{}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, already_created} ->
|
{ok, already_created} ->
|
||||||
{ok, State};
|
{ok, State};
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
|
|
@ -105,31 +117,41 @@ update(Config, State) ->
|
||||||
|
|
||||||
authenticate(#{auth_method := _}, _) ->
|
authenticate(#{auth_method := _}, _) ->
|
||||||
ignore;
|
ignore;
|
||||||
authenticate(#{password := Password} = Credential,
|
authenticate(
|
||||||
#{placeholders := PlaceHolders,
|
#{password := Password} = Credential,
|
||||||
query := Query,
|
#{
|
||||||
query_timeout := Timeout,
|
placeholders := PlaceHolders,
|
||||||
resource_id := ResourceId,
|
query := Query,
|
||||||
password_hash_algorithm := Algorithm}) ->
|
query_timeout := Timeout,
|
||||||
|
resource_id := ResourceId,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
}
|
||||||
|
) ->
|
||||||
Params = emqx_authn_utils:render_sql_params(PlaceHolders, Credential),
|
Params = emqx_authn_utils:render_sql_params(PlaceHolders, Credential),
|
||||||
case emqx_resource:query(ResourceId, {sql, Query, Params, Timeout}) of
|
case emqx_resource:query(ResourceId, {sql, Query, Params, Timeout}) of
|
||||||
{ok, _Columns, []} -> ignore;
|
{ok, _Columns, []} ->
|
||||||
|
ignore;
|
||||||
{ok, Columns, [Row | _]} ->
|
{ok, Columns, [Row | _]} ->
|
||||||
Selected = maps:from_list(lists:zip(Columns, Row)),
|
Selected = maps:from_list(lists:zip(Columns, Row)),
|
||||||
case emqx_authn_utils:check_password_from_selected_map(
|
case
|
||||||
Algorithm, Selected, Password) of
|
emqx_authn_utils:check_password_from_selected_map(
|
||||||
|
Algorithm, Selected, Password
|
||||||
|
)
|
||||||
|
of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, emqx_authn_utils:is_superuser(Selected)};
|
{ok, emqx_authn_utils:is_superuser(Selected)};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{msg => "mysql_query_failed",
|
?SLOG(error, #{
|
||||||
resource => ResourceId,
|
msg => "mysql_query_failed",
|
||||||
query => Query,
|
resource => ResourceId,
|
||||||
params => Params,
|
query => Query,
|
||||||
timeout => Timeout,
|
params => Params,
|
||||||
reason => Reason}),
|
timeout => Timeout,
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
ignore
|
ignore
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,17 +24,19 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
]).
|
fields/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
-compile(export_all).
|
-compile(export_all).
|
||||||
|
|
@ -50,13 +52,14 @@ namespace() -> "authn-postgresql".
|
||||||
roots() -> [?CONF_NS].
|
roots() -> [?CONF_NS].
|
||||||
|
|
||||||
fields(?CONF_NS) ->
|
fields(?CONF_NS) ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('password_based')}
|
[
|
||||||
, {backend, emqx_authn_schema:backend(postgresql)}
|
{mechanism, emqx_authn_schema:mechanism('password_based')},
|
||||||
, {password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1}
|
{backend, emqx_authn_schema:backend(postgresql)},
|
||||||
, {query, fun query/1}
|
{password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1},
|
||||||
|
{query, fun query/1}
|
||||||
] ++
|
] ++
|
||||||
emqx_authn_schema:common_fields() ++
|
emqx_authn_schema:common_fields() ++
|
||||||
proplists:delete(named_queries, emqx_connector_pgsql:fields(config)).
|
proplists:delete(named_queries, emqx_connector_pgsql:fields(config)).
|
||||||
|
|
||||||
query(type) -> string();
|
query(type) -> string();
|
||||||
query(_) -> undefined.
|
query(_) -> undefined.
|
||||||
|
|
@ -71,17 +74,29 @@ refs() ->
|
||||||
create(_AuthenticatorID, Config) ->
|
create(_AuthenticatorID, Config) ->
|
||||||
create(Config).
|
create(Config).
|
||||||
|
|
||||||
create(#{query := Query0,
|
create(
|
||||||
password_hash_algorithm := Algorithm} = Config) ->
|
#{
|
||||||
|
query := Query0,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
} = Config
|
||||||
|
) ->
|
||||||
ok = emqx_authn_password_hashing:init(Algorithm),
|
ok = emqx_authn_password_hashing:init(Algorithm),
|
||||||
{Query, PlaceHolders} = emqx_authn_utils:parse_sql(Query0, '$n'),
|
{Query, PlaceHolders} = emqx_authn_utils:parse_sql(Query0, '$n'),
|
||||||
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
||||||
State = #{placeholders => PlaceHolders,
|
State = #{
|
||||||
password_hash_algorithm => Algorithm,
|
placeholders => PlaceHolders,
|
||||||
resource_id => ResourceId},
|
password_hash_algorithm => Algorithm,
|
||||||
case emqx_resource:create_local(ResourceId, ?RESOURCE_GROUP, emqx_connector_pgsql,
|
resource_id => ResourceId
|
||||||
Config#{named_queries => #{ResourceId => Query}},
|
},
|
||||||
#{}) of
|
case
|
||||||
|
emqx_resource:create_local(
|
||||||
|
ResourceId,
|
||||||
|
?RESOURCE_GROUP,
|
||||||
|
emqx_connector_pgsql,
|
||||||
|
Config#{named_queries => #{ResourceId => Query}},
|
||||||
|
#{}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, already_created} ->
|
{ok, already_created} ->
|
||||||
{ok, State};
|
{ok, State};
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
|
|
@ -101,28 +116,38 @@ update(Config, State) ->
|
||||||
|
|
||||||
authenticate(#{auth_method := _}, _) ->
|
authenticate(#{auth_method := _}, _) ->
|
||||||
ignore;
|
ignore;
|
||||||
authenticate(#{password := Password} = Credential,
|
authenticate(
|
||||||
#{placeholders := PlaceHolders,
|
#{password := Password} = Credential,
|
||||||
resource_id := ResourceId,
|
#{
|
||||||
password_hash_algorithm := Algorithm}) ->
|
placeholders := PlaceHolders,
|
||||||
|
resource_id := ResourceId,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
}
|
||||||
|
) ->
|
||||||
Params = emqx_authn_utils:render_sql_params(PlaceHolders, Credential),
|
Params = emqx_authn_utils:render_sql_params(PlaceHolders, Credential),
|
||||||
case emqx_resource:query(ResourceId, {prepared_query, ResourceId, Params}) of
|
case emqx_resource:query(ResourceId, {prepared_query, ResourceId, Params}) of
|
||||||
{ok, _Columns, []} -> ignore;
|
{ok, _Columns, []} ->
|
||||||
|
ignore;
|
||||||
{ok, Columns, [Row | _]} ->
|
{ok, Columns, [Row | _]} ->
|
||||||
NColumns = [Name || #column{name = Name} <- Columns],
|
NColumns = [Name || #column{name = Name} <- Columns],
|
||||||
Selected = maps:from_list(lists:zip(NColumns, erlang:tuple_to_list(Row))),
|
Selected = maps:from_list(lists:zip(NColumns, erlang:tuple_to_list(Row))),
|
||||||
case emqx_authn_utils:check_password_from_selected_map(
|
case
|
||||||
Algorithm, Selected, Password) of
|
emqx_authn_utils:check_password_from_selected_map(
|
||||||
|
Algorithm, Selected, Password
|
||||||
|
)
|
||||||
|
of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, emqx_authn_utils:is_superuser(Selected)};
|
{ok, emqx_authn_utils:is_superuser(Selected)};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{msg => "postgresql_query_failed",
|
?SLOG(error, #{
|
||||||
resource => ResourceId,
|
msg => "postgresql_query_failed",
|
||||||
params => Params,
|
resource => ResourceId,
|
||||||
reason => Reason}),
|
params => Params,
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
ignore
|
ignore
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,17 +23,19 @@
|
||||||
-behaviour(hocon_schema).
|
-behaviour(hocon_schema).
|
||||||
-behaviour(emqx_authentication).
|
-behaviour(emqx_authentication).
|
||||||
|
|
||||||
-export([ namespace/0
|
-export([
|
||||||
, roots/0
|
namespace/0,
|
||||||
, fields/1
|
roots/0,
|
||||||
]).
|
fields/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ refs/0
|
-export([
|
||||||
, create/2
|
refs/0,
|
||||||
, update/2
|
create/2,
|
||||||
, authenticate/2
|
update/2,
|
||||||
, destroy/1
|
authenticate/2,
|
||||||
]).
|
destroy/1
|
||||||
|
]).
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Hocon Schema
|
%% Hocon Schema
|
||||||
|
|
@ -42,24 +44,27 @@
|
||||||
namespace() -> "authn-redis".
|
namespace() -> "authn-redis".
|
||||||
|
|
||||||
roots() ->
|
roots() ->
|
||||||
[ {?CONF_NS, hoconsc:mk(hoconsc:union(refs()),
|
[
|
||||||
#{})}
|
{?CONF_NS,
|
||||||
|
hoconsc:mk(
|
||||||
|
hoconsc:union(refs()),
|
||||||
|
#{}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
fields(standalone) ->
|
fields(standalone) ->
|
||||||
common_fields() ++ emqx_connector_redis:fields(single);
|
common_fields() ++ emqx_connector_redis:fields(single);
|
||||||
|
|
||||||
fields(cluster) ->
|
fields(cluster) ->
|
||||||
common_fields() ++ emqx_connector_redis:fields(cluster);
|
common_fields() ++ emqx_connector_redis:fields(cluster);
|
||||||
|
|
||||||
fields(sentinel) ->
|
fields(sentinel) ->
|
||||||
common_fields() ++ emqx_connector_redis:fields(sentinel).
|
common_fields() ++ emqx_connector_redis:fields(sentinel).
|
||||||
|
|
||||||
common_fields() ->
|
common_fields() ->
|
||||||
[ {mechanism, emqx_authn_schema:mechanism('password_based')}
|
[
|
||||||
, {backend, emqx_authn_schema:backend(redis)}
|
{mechanism, emqx_authn_schema:mechanism('password_based')},
|
||||||
, {cmd, fun cmd/1}
|
{backend, emqx_authn_schema:backend(redis)},
|
||||||
, {password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1}
|
{cmd, fun cmd/1},
|
||||||
|
{password_hash_algorithm, fun emqx_authn_password_hashing:type_ro/1}
|
||||||
] ++ emqx_authn_schema:common_fields().
|
] ++ emqx_authn_schema:common_fields().
|
||||||
|
|
||||||
cmd(type) -> string();
|
cmd(type) -> string();
|
||||||
|
|
@ -70,30 +75,43 @@ cmd(_) -> undefined.
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
||||||
refs() ->
|
refs() ->
|
||||||
[ hoconsc:ref(?MODULE, standalone)
|
[
|
||||||
, hoconsc:ref(?MODULE, cluster)
|
hoconsc:ref(?MODULE, standalone),
|
||||||
, hoconsc:ref(?MODULE, sentinel)
|
hoconsc:ref(?MODULE, cluster),
|
||||||
|
hoconsc:ref(?MODULE, sentinel)
|
||||||
].
|
].
|
||||||
|
|
||||||
create(_AuthenticatorID, Config) ->
|
create(_AuthenticatorID, Config) ->
|
||||||
create(Config).
|
create(Config).
|
||||||
|
|
||||||
create(#{cmd := Cmd,
|
create(
|
||||||
password_hash_algorithm := Algorithm} = Config) ->
|
#{
|
||||||
|
cmd := Cmd,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
} = Config
|
||||||
|
) ->
|
||||||
ok = emqx_authn_password_hashing:init(Algorithm),
|
ok = emqx_authn_password_hashing:init(Algorithm),
|
||||||
try
|
try
|
||||||
NCmd = parse_cmd(Cmd),
|
NCmd = parse_cmd(Cmd),
|
||||||
ok = emqx_authn_utils:ensure_apps_started(Algorithm),
|
ok = emqx_authn_utils:ensure_apps_started(Algorithm),
|
||||||
State = maps:with(
|
State = maps:with(
|
||||||
[password_hash_algorithm, salt_position],
|
[password_hash_algorithm, salt_position],
|
||||||
Config),
|
Config
|
||||||
|
),
|
||||||
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
|
||||||
NState = State#{
|
NState = State#{
|
||||||
cmd => NCmd,
|
cmd => NCmd,
|
||||||
resource_id => ResourceId},
|
resource_id => ResourceId
|
||||||
case emqx_resource:create_local(ResourceId, ?RESOURCE_GROUP,
|
},
|
||||||
emqx_connector_redis, Config,
|
case
|
||||||
#{}) of
|
emqx_resource:create_local(
|
||||||
|
ResourceId,
|
||||||
|
?RESOURCE_GROUP,
|
||||||
|
emqx_connector_redis,
|
||||||
|
Config,
|
||||||
|
#{}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, already_created} ->
|
{ok, already_created} ->
|
||||||
{ok, NState};
|
{ok, NState};
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
|
|
@ -121,38 +139,50 @@ update(Config, State) ->
|
||||||
|
|
||||||
authenticate(#{auth_method := _}, _) ->
|
authenticate(#{auth_method := _}, _) ->
|
||||||
ignore;
|
ignore;
|
||||||
authenticate(#{password := Password} = Credential,
|
authenticate(
|
||||||
#{cmd := {Command, KeyTemplate, Fields},
|
#{password := Password} = Credential,
|
||||||
resource_id := ResourceId,
|
#{
|
||||||
password_hash_algorithm := Algorithm}) ->
|
cmd := {Command, KeyTemplate, Fields},
|
||||||
|
resource_id := ResourceId,
|
||||||
|
password_hash_algorithm := Algorithm
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NKey = emqx_authn_utils:render_str(KeyTemplate, Credential),
|
NKey = emqx_authn_utils:render_str(KeyTemplate, Credential),
|
||||||
case emqx_resource:query(ResourceId, {cmd, [Command, NKey | Fields]}) of
|
case emqx_resource:query(ResourceId, {cmd, [Command, NKey | Fields]}) of
|
||||||
{ok, []} -> ignore;
|
{ok, []} ->
|
||||||
|
ignore;
|
||||||
{ok, Values} ->
|
{ok, Values} ->
|
||||||
case merge(Fields, Values) of
|
case merge(Fields, Values) of
|
||||||
#{<<"password_hash">> := _} = Selected ->
|
#{<<"password_hash">> := _} = Selected ->
|
||||||
case emqx_authn_utils:check_password_from_selected_map(
|
case
|
||||||
Algorithm, Selected, Password) of
|
emqx_authn_utils:check_password_from_selected_map(
|
||||||
|
Algorithm, Selected, Password
|
||||||
|
)
|
||||||
|
of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, emqx_authn_utils:is_superuser(Selected)};
|
{ok, emqx_authn_utils:is_superuser(Selected)};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
?SLOG(error, #{msg => "cannot_find_password_hash_field",
|
?SLOG(error, #{
|
||||||
cmd => Command,
|
msg => "cannot_find_password_hash_field",
|
||||||
keys => NKey,
|
cmd => Command,
|
||||||
fields => Fields,
|
keys => NKey,
|
||||||
resource => ResourceId}),
|
fields => Fields,
|
||||||
|
resource => ResourceId
|
||||||
|
}),
|
||||||
ignore
|
ignore
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{msg => "redis_query_failed",
|
?SLOG(error, #{
|
||||||
resource => ResourceId,
|
msg => "redis_query_failed",
|
||||||
cmd => Command,
|
resource => ResourceId,
|
||||||
keys => NKey,
|
cmd => Command,
|
||||||
fields => Fields,
|
keys => NKey,
|
||||||
reason => Reason}),
|
fields => Fields,
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
ignore
|
ignore
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -191,5 +221,8 @@ merge(Fields, Value) when not is_list(Value) ->
|
||||||
merge(Fields, [Value]);
|
merge(Fields, [Value]);
|
||||||
merge(Fields, Values) ->
|
merge(Fields, Values) ->
|
||||||
maps:from_list(
|
maps:from_list(
|
||||||
[{list_to_binary(K), V}
|
[
|
||||||
|| {K, V} <- lists:zip(Fields, Values), V =/= undefined]).
|
{list_to_binary(K), V}
|
||||||
|
|| {K, V} <- lists:zip(Fields, Values), V =/= undefined
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,10 @@
|
||||||
|
|
||||||
-behaviour(emqx_bpapi).
|
-behaviour(emqx_bpapi).
|
||||||
|
|
||||||
-export([ introduced_in/0
|
-export([
|
||||||
, lookup_from_all_nodes/2
|
introduced_in/0,
|
||||||
]).
|
lookup_from_all_nodes/2
|
||||||
|
]).
|
||||||
|
|
||||||
-include_lib("emqx/include/bpapi.hrl").
|
-include_lib("emqx/include/bpapi.hrl").
|
||||||
|
|
||||||
|
|
@ -30,6 +31,6 @@ introduced_in() ->
|
||||||
"5.0.0".
|
"5.0.0".
|
||||||
|
|
||||||
-spec lookup_from_all_nodes([node()], binary()) ->
|
-spec lookup_from_all_nodes([node()], binary()) ->
|
||||||
emqx_rpc:erpc_multicall().
|
emqx_rpc:erpc_multicall().
|
||||||
lookup_from_all_nodes(Nodes, ResourceId) ->
|
lookup_from_all_nodes(Nodes, ResourceId) ->
|
||||||
erpc:multicall(Nodes, emqx_authz_api_sources, lookup_from_local_node, [ResourceId], ?TIMEOUT).
|
erpc:multicall(Nodes, emqx_authz_api_sources, lookup_from_local_node, [ResourceId], ?TIMEOUT).
|
||||||
|
|
|
||||||
|
|
@ -21,19 +21,20 @@
|
||||||
|
|
||||||
%% @doc The Gateway definition
|
%% @doc The Gateway definition
|
||||||
-type gateway() ::
|
-type gateway() ::
|
||||||
#{ name := gateway_name()
|
#{
|
||||||
%% Description
|
name := gateway_name(),
|
||||||
, descr => binary() | undefined
|
%% Description
|
||||||
%% Appears only in getting gateway info
|
descr => binary() | undefined,
|
||||||
, status => stopped | running | unloaded
|
%% Appears only in getting gateway info
|
||||||
%% Timestamp in millisecond
|
status => stopped | running | unloaded,
|
||||||
, created_at => integer()
|
%% Timestamp in millisecond
|
||||||
%% Timestamp in millisecond
|
created_at => integer(),
|
||||||
, started_at => integer()
|
%% Timestamp in millisecond
|
||||||
%% Timestamp in millisecond
|
started_at => integer(),
|
||||||
, stopped_at => integer()
|
%% Timestamp in millisecond
|
||||||
%% Appears only in getting gateway info
|
stopped_at => integer(),
|
||||||
, config => emqx_config:config()
|
%% Appears only in getting gateway info
|
||||||
}.
|
config => emqx_config:config()
|
||||||
|
}.
|
||||||
|
|
||||||
-endif.
|
-endif.
|
||||||
|
|
|
||||||
|
|
@ -19,9 +19,11 @@
|
||||||
-define(RESOURCE_NOT_FOUND, 'RESOURCE_NOT_FOUND').
|
-define(RESOURCE_NOT_FOUND, 'RESOURCE_NOT_FOUND').
|
||||||
-define(INTERNAL_ERROR, 'INTERNAL_SERVER_ERROR').
|
-define(INTERNAL_ERROR, 'INTERNAL_SERVER_ERROR').
|
||||||
|
|
||||||
-define(STANDARD_RESP(R),
|
-define(STANDARD_RESP(R), R#{
|
||||||
R#{ 400 => emqx_dashboard_swagger:error_codes(
|
400 => emqx_dashboard_swagger:error_codes(
|
||||||
[?BAD_REQUEST], <<"Bad request">>)
|
[?BAD_REQUEST], <<"Bad request">>
|
||||||
, 404 => emqx_dashboard_swagger:error_codes(
|
),
|
||||||
[?NOT_FOUND, ?RESOURCE_NOT_FOUND], <<"Not Found">>)
|
404 => emqx_dashboard_swagger:error_codes(
|
||||||
}).
|
[?NOT_FOUND, ?RESOURCE_NOT_FOUND], <<"Not Found">>
|
||||||
|
)
|
||||||
|
}).
|
||||||
|
|
|
||||||
|
|
@ -2,30 +2,38 @@
|
||||||
|
|
||||||
{erl_opts, [debug_info]}.
|
{erl_opts, [debug_info]}.
|
||||||
{deps, [
|
{deps, [
|
||||||
{emqx, {path, "../emqx"}},
|
{emqx, {path, "../emqx"}},
|
||||||
{grpc, {git, "https://github.com/emqx/grpc-erl", {tag, "0.6.4"}}}
|
{grpc, {git, "https://github.com/emqx/grpc-erl", {tag, "0.6.4"}}}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{plugins, [
|
{plugins, [
|
||||||
{grpc_plugin, {git, "https://github.com/HJianBo/grpc_plugin", {tag, "v0.10.2"}}}
|
{grpc_plugin, {git, "https://github.com/HJianBo/grpc_plugin", {tag, "v0.10.2"}}}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{grpc,
|
{grpc, [
|
||||||
[{protos, ["src/exproto/protos"]},
|
{protos, ["src/exproto/protos"]},
|
||||||
{out_dir, "src/exproto/"},
|
{out_dir, "src/exproto/"},
|
||||||
{gpb_opts, [{module_name_prefix, "emqx_"},
|
{gpb_opts, [
|
||||||
{module_name_suffix, "_pb"}]}
|
{module_name_prefix, "emqx_"},
|
||||||
|
{module_name_suffix, "_pb"}
|
||||||
|
]}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{provider_hooks,
|
{provider_hooks, [
|
||||||
[{pre, [{compile, {grpc, gen}},
|
{pre, [
|
||||||
{clean, {grpc, clean}}]}
|
{compile, {grpc, gen}},
|
||||||
|
{clean, {grpc, clean}}
|
||||||
|
]}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
{xref_ignores, [emqx_exproto_pb]}.
|
{xref_ignores, [emqx_exproto_pb]}.
|
||||||
|
|
||||||
{cover_excl_mods, [emqx_exproto_pb,
|
{cover_excl_mods, [
|
||||||
emqx_exproto_v_1_connection_adapter_client,
|
emqx_exproto_pb,
|
||||||
emqx_exproto_v_1_connection_adapter_bhvr,
|
emqx_exproto_v_1_connection_adapter_client,
|
||||||
emqx_exproto_v_1_connection_handler_client,
|
emqx_exproto_v_1_connection_adapter_bhvr,
|
||||||
emqx_exproto_v_1_connection_handler_bhvr]}.
|
emqx_exproto_v_1_connection_handler_client,
|
||||||
|
emqx_exproto_v_1_connection_handler_bhvr
|
||||||
|
]}.
|
||||||
|
|
||||||
|
{project_plugins, [erlfmt]}.
|
||||||
|
|
|
||||||
|
|
@ -46,56 +46,57 @@
|
||||||
|
|
||||||
-type gen_server_from() :: {pid(), Tag :: term()}.
|
-type gen_server_from() :: {pid(), Tag :: term()}.
|
||||||
|
|
||||||
-type reply() :: {outgoing, emqx_gateway_frame:packet()}
|
-type reply() ::
|
||||||
| {outgoing, [emqx_gateway_frame:packet()]}
|
{outgoing, emqx_gateway_frame:packet()}
|
||||||
| {event, conn_state() | updated}
|
| {outgoing, [emqx_gateway_frame:packet()]}
|
||||||
| {close, Reason :: atom()}.
|
| {event, conn_state() | updated}
|
||||||
|
| {close, Reason :: atom()}.
|
||||||
|
|
||||||
-type replies() :: reply() | [reply()].
|
-type replies() :: reply() | [reply()].
|
||||||
|
|
||||||
%% @doc Handle the incoming frame
|
%% @doc Handle the incoming frame
|
||||||
-callback handle_in(emqx_gateway_frame:frame() | {frame_error, any()},
|
-callback handle_in(
|
||||||
channel())
|
emqx_gateway_frame:frame() | {frame_error, any()},
|
||||||
-> {ok, channel()}
|
channel()
|
||||||
| {ok, replies(), channel()}
|
) ->
|
||||||
| {shutdown, Reason :: any(), channel()}
|
{ok, channel()}
|
||||||
| {shutdown, Reason :: any(), replies(), channel()}.
|
| {ok, replies(), channel()}
|
||||||
|
| {shutdown, Reason :: any(), channel()}
|
||||||
|
| {shutdown, Reason :: any(), replies(), channel()}.
|
||||||
|
|
||||||
%% @doc Handle the outgoing messages dispatched from PUB/SUB system
|
%% @doc Handle the outgoing messages dispatched from PUB/SUB system
|
||||||
-callback handle_deliver(list(emqx_types:deliver()), channel())
|
-callback handle_deliver(list(emqx_types:deliver()), channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {ok, replies(), channel()}.
|
| {ok, replies(), channel()}.
|
||||||
|
|
||||||
%% @doc Handle the timeout event
|
%% @doc Handle the timeout event
|
||||||
-callback handle_timeout(reference(), Msg :: any(), channel())
|
-callback handle_timeout(reference(), Msg :: any(), channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {ok, replies(), channel()}
|
| {ok, replies(), channel()}
|
||||||
| {shutdown, Reason :: any(), channel()}.
|
| {shutdown, Reason :: any(), channel()}.
|
||||||
|
|
||||||
%% @doc Handle the custom gen_server:call/2 for its connection process
|
%% @doc Handle the custom gen_server:call/2 for its connection process
|
||||||
-callback handle_call(Req :: any(), From :: gen_server_from(), channel())
|
-callback handle_call(Req :: any(), From :: gen_server_from(), channel()) ->
|
||||||
-> {reply, Reply :: any(), channel()}
|
{reply, Reply :: any(), channel()}
|
||||||
%% Reply to caller and trigger an event(s)
|
%% Reply to caller and trigger an event(s)
|
||||||
| {reply, Reply :: any(),
|
| {reply, Reply :: any(), EventOrEvents :: tuple() | list(tuple()), channel()}
|
||||||
EventOrEvents :: tuple() | list(tuple()), channel()}
|
| {noreply, channel()}
|
||||||
| {noreply, channel()}
|
| {noreply, EventOrEvents :: tuple() | list(tuple()), channel()}
|
||||||
| {noreply, EventOrEvents :: tuple() | list(tuple()), channel()}
|
| {shutdown, Reason :: any(), Reply :: any(), channel()}
|
||||||
| {shutdown, Reason :: any(), Reply :: any(), channel()}
|
%% Shutdown the process, reply to caller and write a packet to client
|
||||||
%% Shutdown the process, reply to caller and write a packet to client
|
| {shutdown, Reason :: any(), Reply :: any(), emqx_gateway_frame:frame(), channel()}.
|
||||||
| {shutdown, Reason :: any(), Reply :: any(),
|
|
||||||
emqx_gateway_frame:frame(), channel()}.
|
|
||||||
|
|
||||||
%% @doc Handle the custom gen_server:cast/2 for its connection process
|
%% @doc Handle the custom gen_server:cast/2 for its connection process
|
||||||
-callback handle_cast(Req :: any(), channel())
|
-callback handle_cast(Req :: any(), channel()) ->
|
||||||
-> ok
|
ok
|
||||||
| {ok, channel()}
|
| {ok, channel()}
|
||||||
| {shutdown, Reason :: any(), channel()}.
|
| {shutdown, Reason :: any(), channel()}.
|
||||||
|
|
||||||
%% @doc Handle the custom process messages for its connection process
|
%% @doc Handle the custom process messages for its connection process
|
||||||
-callback handle_info(Info :: any(), channel())
|
-callback handle_info(Info :: any(), channel()) ->
|
||||||
-> ok
|
ok
|
||||||
| {ok, channel()}
|
| {ok, channel()}
|
||||||
| {shutdown, Reason :: any(), channel()}.
|
| {shutdown, Reason :: any(), channel()}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Terminate
|
%% Terminate
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -26,17 +26,18 @@
|
||||||
|
|
||||||
-type frame() :: any().
|
-type frame() :: any().
|
||||||
|
|
||||||
-type parse_result() :: {ok, frame(),
|
-type parse_result() ::
|
||||||
Rest :: binary(), NewState :: parse_state()}
|
{ok, frame(), Rest :: binary(), NewState :: parse_state()}
|
||||||
| {more, NewState :: parse_state()}.
|
| {more, NewState :: parse_state()}.
|
||||||
|
|
||||||
-type serialize_options() :: map().
|
-type serialize_options() :: map().
|
||||||
|
|
||||||
-export_type([ parse_state/0
|
-export_type([
|
||||||
, parse_result/0
|
parse_state/0,
|
||||||
, serialize_options/0
|
parse_result/0,
|
||||||
, frame/0
|
serialize_options/0,
|
||||||
]).
|
frame/0
|
||||||
|
]).
|
||||||
|
|
||||||
%% Callbacks
|
%% Callbacks
|
||||||
|
|
||||||
|
|
@ -60,4 +61,3 @@
|
||||||
|
|
||||||
%% @doc
|
%% @doc
|
||||||
-callback is_message(Frame :: any()) -> boolean().
|
-callback is_message(Frame :: any()) -> boolean().
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,21 +22,25 @@
|
||||||
-type reason() :: any().
|
-type reason() :: any().
|
||||||
|
|
||||||
%% @doc
|
%% @doc
|
||||||
-callback on_gateway_load(Gateway :: gateway(),
|
-callback on_gateway_load(
|
||||||
Ctx :: emqx_gateway_ctx:context())
|
Gateway :: gateway(),
|
||||||
-> {error, reason()}
|
Ctx :: emqx_gateway_ctx:context()
|
||||||
| {ok, [ChildPid :: pid()], GwState :: state()}
|
) ->
|
||||||
%% TODO: v0.2 The child spec is better for restarting child process
|
{error, reason()}
|
||||||
| {ok, [Childspec :: supervisor:child_spec()], GwState :: state()}.
|
| {ok, [ChildPid :: pid()], GwState :: state()}
|
||||||
|
%% TODO: v0.2 The child spec is better for restarting child process
|
||||||
|
| {ok, [Childspec :: supervisor:child_spec()], GwState :: state()}.
|
||||||
|
|
||||||
%% @doc
|
%% @doc
|
||||||
-callback on_gateway_update(Config :: emqx_config:config(),
|
-callback on_gateway_update(
|
||||||
Gateway :: gateway(),
|
Config :: emqx_config:config(),
|
||||||
GwState :: state())
|
Gateway :: gateway(),
|
||||||
-> ok
|
GwState :: state()
|
||||||
| {ok, [ChildPid :: pid()], NGwState :: state()}
|
) ->
|
||||||
| {ok, [Childspec :: supervisor:child_spec()], NGwState :: state()}
|
ok
|
||||||
| {error, reason()}.
|
| {ok, [ChildPid :: pid()], NGwState :: state()}
|
||||||
|
| {ok, [Childspec :: supervisor:child_spec()], NGwState :: state()}
|
||||||
|
| {error, reason()}.
|
||||||
|
|
||||||
%% @doc
|
%% @doc
|
||||||
-callback on_gateway_unload(Gateway :: gateway(), GwState :: state()) -> ok.
|
-callback on_gateway_unload(Gateway :: gateway(), GwState :: state()) -> ok.
|
||||||
|
|
|
||||||
|
|
@ -44,17 +44,22 @@ paths() ->
|
||||||
[?PREFIX ++ "/request"].
|
[?PREFIX ++ "/request"].
|
||||||
|
|
||||||
schema(?PREFIX ++ "/request") ->
|
schema(?PREFIX ++ "/request") ->
|
||||||
#{operationId => request,
|
#{
|
||||||
post => #{ tags => [<<"gateway|coap">>]
|
operationId => request,
|
||||||
, desc => <<"Send a CoAP request message to the client">>
|
post => #{
|
||||||
, parameters => request_parameters()
|
tags => [<<"gateway|coap">>],
|
||||||
, requestBody => request_body()
|
desc => <<"Send a CoAP request message to the client">>,
|
||||||
, responses => #{200 => coap_message(),
|
parameters => request_parameters(),
|
||||||
404 => error_codes(['CLIENT_NOT_FOUND'], <<"Client not found error">>),
|
requestBody => request_body(),
|
||||||
504 => error_codes(['CLIENT_NOT_RESPONSE'], <<"Waiting for client response timeout">>)}
|
responses => #{
|
||||||
}
|
200 => coap_message(),
|
||||||
}.
|
404 => error_codes(['CLIENT_NOT_FOUND'], <<"Client not found error">>),
|
||||||
|
504 => error_codes(
|
||||||
|
['CLIENT_NOT_RESPONSE'], <<"Waiting for client response timeout">>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.
|
||||||
|
|
||||||
request(post, #{body := Body, bindings := Bindings}) ->
|
request(post, #{body := Body, bindings := Bindings}) ->
|
||||||
ClientId = maps:get(clientid, Bindings, undefined),
|
ClientId = maps:get(clientid, Bindings, undefined),
|
||||||
|
|
@ -66,8 +71,12 @@ request(post, #{body := Body, bindings := Bindings}) ->
|
||||||
CT = erlang:atom_to_binary(AtomCT),
|
CT = erlang:atom_to_binary(AtomCT),
|
||||||
Payload2 = parse_payload(CT, Payload),
|
Payload2 = parse_payload(CT, Payload),
|
||||||
|
|
||||||
Msg = emqx_coap_message:request(con,
|
Msg = emqx_coap_message:request(
|
||||||
Method, Payload2, #{content_format => CT}),
|
con,
|
||||||
|
Method,
|
||||||
|
Payload2,
|
||||||
|
#{content_format => CT}
|
||||||
|
),
|
||||||
|
|
||||||
Msg2 = Msg#coap_message{token = Token},
|
Msg2 = Msg#coap_message{token = Token},
|
||||||
|
|
||||||
|
|
@ -78,7 +87,7 @@ request(post, #{body := Body, bindings := Bindings}) ->
|
||||||
{404, #{code => 'CLIENT_NOT_FOUND'}};
|
{404, #{code => 'CLIENT_NOT_FOUND'}};
|
||||||
Response ->
|
Response ->
|
||||||
{200, format_to_response(CT, Response)}
|
{200, format_to_response(CT, Response)}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
|
|
@ -87,42 +96,49 @@ request_parameters() ->
|
||||||
[{clientid, mk(binary(), #{in => path, required => true})}].
|
[{clientid, mk(binary(), #{in => path, required => true})}].
|
||||||
|
|
||||||
request_body() ->
|
request_body() ->
|
||||||
[ {token, mk(binary(), #{desc => "message token, can be empty"})}
|
[
|
||||||
, {method, mk(enum([get, put, post, delete]), #{desc => "request method type"})}
|
{token, mk(binary(), #{desc => "message token, can be empty"})},
|
||||||
, {timeout, mk(emqx_schema:duration_ms(), #{desc => "timespan for response"})}
|
{method, mk(enum([get, put, post, delete]), #{desc => "request method type"})},
|
||||||
, {content_type, mk(enum(['text/plain', 'application/json', 'application/octet-stream']),
|
{timeout, mk(emqx_schema:duration_ms(), #{desc => "timespan for response"})},
|
||||||
#{desc => "payload type"})}
|
{content_type,
|
||||||
, {payload, mk(binary(), #{desc => "the content of the payload"})}
|
mk(
|
||||||
|
enum(['text/plain', 'application/json', 'application/octet-stream']),
|
||||||
|
#{desc => "payload type"}
|
||||||
|
)},
|
||||||
|
{payload, mk(binary(), #{desc => "the content of the payload"})}
|
||||||
].
|
].
|
||||||
|
|
||||||
coap_message() ->
|
coap_message() ->
|
||||||
[ {id, mk(integer(), #{desc => "message id"})}
|
[
|
||||||
, {token, mk(string(), #{desc => "message token, can be empty"})}
|
{id, mk(integer(), #{desc => "message id"})},
|
||||||
, {method, mk(string(), #{desc => "response code"})}
|
{token, mk(string(), #{desc => "message token, can be empty"})},
|
||||||
, {payload, mk(string(), #{desc => "payload"})}
|
{method, mk(string(), #{desc => "response code"})},
|
||||||
|
{payload, mk(string(), #{desc => "payload"})}
|
||||||
].
|
].
|
||||||
|
|
||||||
format_to_response(ContentType, #coap_message{id = Id,
|
format_to_response(ContentType, #coap_message{
|
||||||
token = Token,
|
id = Id,
|
||||||
method = Method,
|
token = Token,
|
||||||
payload = Payload}) ->
|
method = Method,
|
||||||
#{id => Id,
|
payload = Payload
|
||||||
token => Token,
|
}) ->
|
||||||
method => format_to_binary(Method),
|
#{
|
||||||
payload => format_payload(ContentType, Payload)}.
|
id => Id,
|
||||||
|
token => Token,
|
||||||
|
method => format_to_binary(Method),
|
||||||
|
payload => format_payload(ContentType, Payload)
|
||||||
|
}.
|
||||||
|
|
||||||
format_to_binary(Obj) ->
|
format_to_binary(Obj) ->
|
||||||
erlang:list_to_binary(io_lib:format("~p", [Obj])).
|
erlang:list_to_binary(io_lib:format("~p", [Obj])).
|
||||||
|
|
||||||
format_payload(<<"application/octet-stream">>, Payload) ->
|
format_payload(<<"application/octet-stream">>, Payload) ->
|
||||||
base64:encode(Payload);
|
base64:encode(Payload);
|
||||||
|
|
||||||
format_payload(_, Payload) ->
|
format_payload(_, Payload) ->
|
||||||
Payload.
|
Payload.
|
||||||
|
|
||||||
parse_payload(<<"application/octet-stream">>, Body) ->
|
parse_payload(<<"application/octet-stream">>, Body) ->
|
||||||
base64:decode(Body);
|
base64:decode(Body);
|
||||||
|
|
||||||
parse_payload(_, Body) ->
|
parse_payload(_, Body) ->
|
||||||
Body.
|
Body.
|
||||||
|
|
||||||
|
|
@ -140,10 +156,13 @@ call_client(ClientId, Msg, Timeout) ->
|
||||||
_ ->
|
_ ->
|
||||||
not_found
|
not_found
|
||||||
end
|
end
|
||||||
catch _:Error:Trace ->
|
catch
|
||||||
?SLOG(warning, #{msg => "coap_client_call_exception",
|
_:Error:Trace ->
|
||||||
clientid => ClientId,
|
?SLOG(warning, #{
|
||||||
error => Error,
|
msg => "coap_client_call_exception",
|
||||||
stacktrace => Trace}),
|
clientid => ClientId,
|
||||||
|
error => Error,
|
||||||
|
stacktrace => Trace
|
||||||
|
}),
|
||||||
not_found
|
not_found
|
||||||
end.
|
end.
|
||||||
|
|
|
||||||
|
|
@ -19,26 +19,29 @@
|
||||||
-behaviour(emqx_gateway_channel).
|
-behaviour(emqx_gateway_channel).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ info/1
|
-export([
|
||||||
, info/2
|
info/1,
|
||||||
, stats/1
|
info/2,
|
||||||
, validator/4
|
stats/1,
|
||||||
, metrics_inc/2
|
validator/4,
|
||||||
, run_hooks/3
|
metrics_inc/2,
|
||||||
, send_request/2
|
run_hooks/3,
|
||||||
]).
|
send_request/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ init/2
|
-export([
|
||||||
, handle_in/2
|
init/2,
|
||||||
, handle_deliver/2
|
handle_in/2,
|
||||||
, handle_timeout/3
|
handle_deliver/2,
|
||||||
, terminate/2
|
handle_timeout/3,
|
||||||
]).
|
terminate/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ handle_call/3
|
-export([
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
]).
|
handle_info/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([channel/0]).
|
-export_type([channel/0]).
|
||||||
|
|
||||||
|
|
@ -49,34 +52,35 @@
|
||||||
-define(AUTHN, ?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME_ATOM).
|
-define(AUTHN, ?EMQX_AUTHENTICATION_CONFIG_ROOT_NAME_ATOM).
|
||||||
|
|
||||||
-record(channel, {
|
-record(channel, {
|
||||||
%% Context
|
%% Context
|
||||||
ctx :: emqx_gateway_ctx:context(),
|
ctx :: emqx_gateway_ctx:context(),
|
||||||
%% Connection Info
|
%% Connection Info
|
||||||
conninfo :: emqx_types:conninfo(),
|
conninfo :: emqx_types:conninfo(),
|
||||||
%% Client Info
|
%% Client Info
|
||||||
clientinfo :: emqx_types:clientinfo(),
|
clientinfo :: emqx_types:clientinfo(),
|
||||||
%% Session
|
%% Session
|
||||||
session :: emqx_coap_session:session() | undefined,
|
session :: emqx_coap_session:session() | undefined,
|
||||||
%% Keepalive
|
%% Keepalive
|
||||||
keepalive :: emqx_keepalive:keepalive() | undefined,
|
keepalive :: emqx_keepalive:keepalive() | undefined,
|
||||||
%% Timer
|
%% Timer
|
||||||
timers :: #{atom() => disable | undefined | reference()},
|
timers :: #{atom() => disable | undefined | reference()},
|
||||||
%% Connection mode
|
%% Connection mode
|
||||||
connection_required :: boolean(),
|
connection_required :: boolean(),
|
||||||
%% Connection State
|
%% Connection State
|
||||||
conn_state :: conn_state(),
|
conn_state :: conn_state(),
|
||||||
%% Session token to identity this connection
|
%% Session token to identity this connection
|
||||||
token :: binary() | undefined
|
token :: binary() | undefined
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-type channel() :: #channel{}.
|
-type channel() :: #channel{}.
|
||||||
|
|
||||||
-type conn_state() :: idle | connecting | connected | disconnected.
|
-type conn_state() :: idle | connecting | connected | disconnected.
|
||||||
|
|
||||||
-type reply() :: {outgoing, coap_message()}
|
-type reply() ::
|
||||||
| {outgoing, [coap_message()]}
|
{outgoing, coap_message()}
|
||||||
| {event, conn_state()|updated}
|
| {outgoing, [coap_message()]}
|
||||||
| {close, Reason :: atom()}.
|
| {event, conn_state() | updated}
|
||||||
|
| {close, Reason :: atom()}.
|
||||||
|
|
||||||
-type replies() :: reply() | [reply()].
|
-type replies() :: reply() | [reply()].
|
||||||
|
|
||||||
|
|
@ -97,10 +101,9 @@
|
||||||
info(Channel) ->
|
info(Channel) ->
|
||||||
maps:from_list(info(?INFO_KEYS, Channel)).
|
maps:from_list(info(?INFO_KEYS, Channel)).
|
||||||
|
|
||||||
-spec info(list(atom())|atom(), channel()) -> term().
|
-spec info(list(atom()) | atom(), channel()) -> term().
|
||||||
info(Keys, Channel) when is_list(Keys) ->
|
info(Keys, Channel) when is_list(Keys) ->
|
||||||
[{Key, info(Key, Channel)} || Key <- Keys];
|
[{Key, info(Key, Channel)} || Key <- Keys];
|
||||||
|
|
||||||
info(conninfo, #channel{conninfo = ConnInfo}) ->
|
info(conninfo, #channel{conninfo = ConnInfo}) ->
|
||||||
ConnInfo;
|
ConnInfo;
|
||||||
info(conn_state, #channel{conn_state = ConnState}) ->
|
info(conn_state, #channel{conn_state = ConnState}) ->
|
||||||
|
|
@ -119,41 +122,47 @@ stats(_) ->
|
||||||
[].
|
[].
|
||||||
|
|
||||||
-spec init(map(), map()) -> channel().
|
-spec init(map(), map()) -> channel().
|
||||||
init(ConnInfo = #{peername := {PeerHost, _},
|
init(
|
||||||
sockname := {_, SockPort}},
|
ConnInfo = #{
|
||||||
#{ctx := Ctx} = Config) ->
|
peername := {PeerHost, _},
|
||||||
|
sockname := {_, SockPort}
|
||||||
|
},
|
||||||
|
#{ctx := Ctx} = Config
|
||||||
|
) ->
|
||||||
Peercert = maps:get(peercert, ConnInfo, undefined),
|
Peercert = maps:get(peercert, ConnInfo, undefined),
|
||||||
Mountpoint = maps:get(mountpoint, Config, <<>>),
|
Mountpoint = maps:get(mountpoint, Config, <<>>),
|
||||||
ListenerId = case maps:get(listener, Config, undefined) of
|
ListenerId =
|
||||||
undefined -> undefined;
|
case maps:get(listener, Config, undefined) of
|
||||||
{GwName, Type, LisName} ->
|
undefined -> undefined;
|
||||||
emqx_gateway_utils:listener_id(GwName, Type, LisName)
|
{GwName, Type, LisName} -> emqx_gateway_utils:listener_id(GwName, Type, LisName)
|
||||||
end,
|
end,
|
||||||
ClientInfo = set_peercert_infos(
|
ClientInfo = set_peercert_infos(
|
||||||
Peercert,
|
Peercert,
|
||||||
#{ zone => default
|
#{
|
||||||
, listener => ListenerId
|
zone => default,
|
||||||
, protocol => 'coap'
|
listener => ListenerId,
|
||||||
, peerhost => PeerHost
|
protocol => 'coap',
|
||||||
, sockport => SockPort
|
peerhost => PeerHost,
|
||||||
, clientid => emqx_guid:to_base62(emqx_guid:gen())
|
sockport => SockPort,
|
||||||
, username => undefined
|
clientid => emqx_guid:to_base62(emqx_guid:gen()),
|
||||||
, is_bridge => false
|
username => undefined,
|
||||||
, is_superuser => false
|
is_bridge => false,
|
||||||
, mountpoint => Mountpoint
|
is_superuser => false,
|
||||||
}
|
mountpoint => Mountpoint
|
||||||
),
|
}
|
||||||
|
),
|
||||||
|
|
||||||
Heartbeat = ?GET_IDLE_TIME(Config),
|
Heartbeat = ?GET_IDLE_TIME(Config),
|
||||||
#channel{ ctx = Ctx
|
#channel{
|
||||||
, conninfo = ConnInfo
|
ctx = Ctx,
|
||||||
, clientinfo = ClientInfo
|
conninfo = ConnInfo,
|
||||||
, timers = #{}
|
clientinfo = ClientInfo,
|
||||||
, session = emqx_coap_session:new()
|
timers = #{},
|
||||||
, keepalive = emqx_keepalive:init(Heartbeat)
|
session = emqx_coap_session:new(),
|
||||||
, connection_required = maps:get(connection_required, Config, false)
|
keepalive = emqx_keepalive:init(Heartbeat),
|
||||||
, conn_state = idle
|
connection_required = maps:get(connection_required, Config, false),
|
||||||
}.
|
conn_state = idle
|
||||||
|
}.
|
||||||
|
|
||||||
validator(Type, Topic, Ctx, ClientInfo) ->
|
validator(Type, Topic, Ctx, ClientInfo) ->
|
||||||
emqx_gateway_ctx:authorize(Ctx, ClientInfo, Type, Topic).
|
emqx_gateway_ctx:authorize(Ctx, ClientInfo, Type, Topic).
|
||||||
|
|
@ -166,11 +175,11 @@ send_request(Channel, Request) ->
|
||||||
%% Handle incoming packet
|
%% Handle incoming packet
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec handle_in(coap_message() | {frame_error, any()}, channel())
|
-spec handle_in(coap_message() | {frame_error, any()}, channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {ok, replies(), channel()}
|
| {ok, replies(), channel()}
|
||||||
| {shutdown, Reason :: term(), channel()}
|
| {shutdown, Reason :: term(), channel()}
|
||||||
| {shutdown, Reason :: term(), replies(), channel()}.
|
| {shutdown, Reason :: term(), replies(), channel()}.
|
||||||
handle_in(Msg, ChannleT) ->
|
handle_in(Msg, ChannleT) ->
|
||||||
Channel = ensure_keepalive_timer(ChannleT),
|
Channel = ensure_keepalive_timer(ChannleT),
|
||||||
case emqx_coap_message:is_request(Msg) of
|
case emqx_coap_message:is_request(Msg) of
|
||||||
|
|
@ -183,8 +192,13 @@ handle_in(Msg, ChannleT) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Handle Delivers from broker to client
|
%% Handle Delivers from broker to client
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
handle_deliver(Delivers, #channel{session = Session,
|
handle_deliver(
|
||||||
ctx = Ctx} = Channel) ->
|
Delivers,
|
||||||
|
#channel{
|
||||||
|
session = Session,
|
||||||
|
ctx = Ctx
|
||||||
|
} = Channel
|
||||||
|
) ->
|
||||||
handle_result(emqx_coap_session:deliver(Delivers, Ctx, Session), Channel).
|
handle_result(emqx_coap_session:deliver(Delivers, Ctx, Session), Channel).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -199,13 +213,10 @@ handle_timeout(_, {keepalive, NewVal}, #channel{keepalive = KeepAlive} = Channel
|
||||||
{error, timeout} ->
|
{error, timeout} ->
|
||||||
{shutdown, timeout, ensure_disconnected(keepalive_timeout, Channel)}
|
{shutdown, timeout, ensure_disconnected(keepalive_timeout, Channel)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_timeout(_, {transport, Msg}, Channel) ->
|
handle_timeout(_, {transport, Msg}, Channel) ->
|
||||||
call_session(timeout, Msg, Channel);
|
call_session(timeout, Msg, Channel);
|
||||||
|
|
||||||
handle_timeout(_, disconnect, Channel) ->
|
handle_timeout(_, disconnect, Channel) ->
|
||||||
{shutdown, normal, Channel};
|
{shutdown, normal, Channel};
|
||||||
|
|
||||||
handle_timeout(_, _, Channel) ->
|
handle_timeout(_, _, Channel) ->
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
|
|
@ -213,72 +224,89 @@ handle_timeout(_, _, Channel) ->
|
||||||
%% Handle call
|
%% Handle call
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec(handle_call(Req :: term(), From :: term(), channel())
|
-spec handle_call(Req :: term(), From :: term(), channel()) ->
|
||||||
-> {reply, Reply :: term(), channel()}
|
{reply, Reply :: term(), channel()}
|
||||||
| {reply, Reply :: term(), replies(), channel()}
|
| {reply, Reply :: term(), replies(), channel()}
|
||||||
| {shutdown, Reason :: term(), Reply :: term(), channel()}
|
| {shutdown, Reason :: term(), Reply :: term(), channel()}
|
||||||
| {shutdown, Reason :: term(), Reply :: term(), coap_message(), channel()}).
|
| {shutdown, Reason :: term(), Reply :: term(), coap_message(), channel()}.
|
||||||
handle_call({send_request, Msg}, From, Channel) ->
|
handle_call({send_request, Msg}, From, Channel) ->
|
||||||
Result = call_session(handle_out, {{send_request, From}, Msg}, Channel),
|
Result = call_session(handle_out, {{send_request, From}, Msg}, Channel),
|
||||||
erlang:setelement(1, Result, noreply);
|
erlang:setelement(1, Result, noreply);
|
||||||
|
handle_call(
|
||||||
handle_call({subscribe, Topic, SubOpts}, _From,
|
{subscribe, Topic, SubOpts},
|
||||||
Channel = #channel{
|
_From,
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo
|
ctx = Ctx,
|
||||||
= #{clientid := ClientId,
|
clientinfo =
|
||||||
mountpoint := Mountpoint},
|
ClientInfo =
|
||||||
session = Session}) ->
|
#{
|
||||||
Token = maps:get(token,
|
clientid := ClientId,
|
||||||
maps:get(sub_props, SubOpts, #{}),
|
mountpoint := Mountpoint
|
||||||
<<>>),
|
},
|
||||||
|
session = Session
|
||||||
|
}
|
||||||
|
) ->
|
||||||
|
Token = maps:get(
|
||||||
|
token,
|
||||||
|
maps:get(sub_props, SubOpts, #{}),
|
||||||
|
<<>>
|
||||||
|
),
|
||||||
NSubOpts = maps:merge(
|
NSubOpts = maps:merge(
|
||||||
emqx_gateway_utils:default_subopts(),
|
emqx_gateway_utils:default_subopts(),
|
||||||
SubOpts),
|
SubOpts
|
||||||
|
),
|
||||||
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
||||||
_ = emqx_broker:subscribe(MountedTopic, ClientId, NSubOpts),
|
_ = emqx_broker:subscribe(MountedTopic, ClientId, NSubOpts),
|
||||||
|
|
||||||
_ = run_hooks(Ctx, 'session.subscribed',
|
_ = run_hooks(
|
||||||
[ClientInfo, MountedTopic, NSubOpts]),
|
Ctx,
|
||||||
|
'session.subscribed',
|
||||||
|
[ClientInfo, MountedTopic, NSubOpts]
|
||||||
|
),
|
||||||
%% modify session state
|
%% modify session state
|
||||||
SubReq = {Topic, Token},
|
SubReq = {Topic, Token},
|
||||||
TempMsg = #coap_message{type = non},
|
TempMsg = #coap_message{type = non},
|
||||||
%% FIXME: The subopts is not used for emqx_coap_session
|
%% FIXME: The subopts is not used for emqx_coap_session
|
||||||
Result = emqx_coap_session:process_subscribe(
|
Result = emqx_coap_session:process_subscribe(
|
||||||
SubReq, TempMsg, #{}, Session),
|
SubReq, TempMsg, #{}, Session
|
||||||
|
),
|
||||||
NSession = maps:get(session, Result),
|
NSession = maps:get(session, Result),
|
||||||
{reply, {ok, {MountedTopic, NSubOpts}}, Channel#channel{session = NSession}};
|
{reply, {ok, {MountedTopic, NSubOpts}}, Channel#channel{session = NSession}};
|
||||||
|
handle_call(
|
||||||
handle_call({unsubscribe, Topic}, _From,
|
{unsubscribe, Topic},
|
||||||
Channel = #channel{
|
_From,
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo
|
ctx = Ctx,
|
||||||
= #{mountpoint := Mountpoint},
|
clientinfo =
|
||||||
session = Session}) ->
|
ClientInfo =
|
||||||
|
#{mountpoint := Mountpoint},
|
||||||
|
session = Session
|
||||||
|
}
|
||||||
|
) ->
|
||||||
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
||||||
ok = emqx_broker:unsubscribe(MountedTopic),
|
ok = emqx_broker:unsubscribe(MountedTopic),
|
||||||
_ = run_hooks(Ctx, 'session.unsubscribe',
|
_ = run_hooks(
|
||||||
[ClientInfo, MountedTopic, #{}]),
|
Ctx,
|
||||||
|
'session.unsubscribe',
|
||||||
|
[ClientInfo, MountedTopic, #{}]
|
||||||
|
),
|
||||||
|
|
||||||
%% modify session state
|
%% modify session state
|
||||||
UnSubReq = Topic,
|
UnSubReq = Topic,
|
||||||
TempMsg = #coap_message{type = non},
|
TempMsg = #coap_message{type = non},
|
||||||
Result = emqx_coap_session:process_subscribe(
|
Result = emqx_coap_session:process_subscribe(
|
||||||
UnSubReq, TempMsg, #{}, Session),
|
UnSubReq, TempMsg, #{}, Session
|
||||||
|
),
|
||||||
NSession = maps:get(session, Result),
|
NSession = maps:get(session, Result),
|
||||||
{reply, ok, Channel#channel{session = NSession}};
|
{reply, ok, Channel#channel{session = NSession}};
|
||||||
|
|
||||||
handle_call(subscriptions, _From, Channel = #channel{session = Session}) ->
|
handle_call(subscriptions, _From, Channel = #channel{session = Session}) ->
|
||||||
Subs = emqx_coap_session:info(subscriptions, Session),
|
Subs = emqx_coap_session:info(subscriptions, Session),
|
||||||
{reply, {ok, maps:to_list(Subs)}, Channel};
|
{reply, {ok, maps:to_list(Subs)}, Channel};
|
||||||
|
|
||||||
handle_call(kick, _From, Channel) ->
|
handle_call(kick, _From, Channel) ->
|
||||||
NChannel = ensure_disconnected(kicked, Channel),
|
NChannel = ensure_disconnected(kicked, Channel),
|
||||||
shutdown_and_reply(kicked, ok, NChannel);
|
shutdown_and_reply(kicked, ok, NChannel);
|
||||||
|
|
||||||
handle_call(discard, _From, Channel) ->
|
handle_call(discard, _From, Channel) ->
|
||||||
shutdown_and_reply(discarded, ok, Channel);
|
shutdown_and_reply(discarded, ok, Channel);
|
||||||
|
|
||||||
handle_call(Req, _From, Channel) ->
|
handle_call(Req, _From, Channel) ->
|
||||||
?SLOG(error, #{msg => "unexpected_call", call => Req}),
|
?SLOG(error, #{msg => "unexpected_call", call => Req}),
|
||||||
{reply, ignored, Channel}.
|
{reply, ignored, Channel}.
|
||||||
|
|
@ -287,8 +315,8 @@ handle_call(Req, _From, Channel) ->
|
||||||
%% Handle Cast
|
%% Handle Cast
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec handle_cast(Req :: term(), channel())
|
-spec handle_cast(Req :: term(), channel()) ->
|
||||||
-> ok | {ok, channel()} | {shutdown, Reason :: term(), channel()}.
|
ok | {ok, channel()} | {shutdown, Reason :: term(), channel()}.
|
||||||
handle_cast(Req, Channel) ->
|
handle_cast(Req, Channel) ->
|
||||||
?SLOG(error, #{msg => "unexpected_cast", cast => Req}),
|
?SLOG(error, #{msg => "unexpected_cast", cast => Req}),
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
@ -297,11 +325,10 @@ handle_cast(Req, Channel) ->
|
||||||
%% Handle Info
|
%% Handle Info
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec(handle_info(Info :: term(), channel())
|
-spec handle_info(Info :: term(), channel()) ->
|
||||||
-> ok | {ok, channel()} | {shutdown, Reason :: term(), channel()}).
|
ok | {ok, channel()} | {shutdown, Reason :: term(), channel()}.
|
||||||
handle_info({subscribe, _AutoSubs}, Channel) ->
|
handle_info({subscribe, _AutoSubs}, Channel) ->
|
||||||
{ok, Channel};
|
{ok, Channel};
|
||||||
|
|
||||||
handle_info(Info, Channel) ->
|
handle_info(Info, Channel) ->
|
||||||
?SLOG(warning, #{msg => "unexpected_info", info => Info}),
|
?SLOG(warning, #{msg => "unexpected_info", info => Info}),
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
@ -309,21 +336,23 @@ handle_info(Info, Channel) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Terminate
|
%% Terminate
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
terminate(Reason, #channel{clientinfo = ClientInfo,
|
terminate(Reason, #channel{
|
||||||
ctx = Ctx,
|
clientinfo = ClientInfo,
|
||||||
session = Session}) ->
|
ctx = Ctx,
|
||||||
|
session = Session
|
||||||
|
}) ->
|
||||||
run_hooks(Ctx, 'session.terminated', [ClientInfo, Reason, Session]).
|
run_hooks(Ctx, 'session.terminated', [ClientInfo, Reason, Session]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal functions
|
%% Internal functions
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
set_peercert_infos(NoSSL, ClientInfo)
|
set_peercert_infos(NoSSL, ClientInfo) when
|
||||||
when NoSSL =:= nossl;
|
NoSSL =:= nossl;
|
||||||
NoSSL =:= undefined ->
|
NoSSL =:= undefined
|
||||||
|
->
|
||||||
ClientInfo;
|
ClientInfo;
|
||||||
set_peercert_infos(Peercert, ClientInfo) ->
|
set_peercert_infos(Peercert, ClientInfo) ->
|
||||||
{DN, CN} = {esockd_peercert:subject(Peercert),
|
{DN, CN} = {esockd_peercert:subject(Peercert), esockd_peercert:common_name(Peercert)},
|
||||||
esockd_peercert:common_name(Peercert)},
|
|
||||||
ClientInfo#{dn => DN, cn => CN}.
|
ClientInfo#{dn => DN, cn => CN}.
|
||||||
|
|
||||||
ensure_timer(Name, Time, Msg, #channel{timers = Timers} = Channel) ->
|
ensure_timer(Name, Time, Msg, #channel{timers = Timers} = Channel) ->
|
||||||
|
|
@ -348,15 +377,21 @@ ensure_keepalive_timer(Fun, #channel{keepalive = KeepAlive} = Channel) ->
|
||||||
check_auth_state(Msg, #channel{connection_required = Required} = Channel) ->
|
check_auth_state(Msg, #channel{connection_required = Required} = Channel) ->
|
||||||
check_token(Required, Msg, Channel).
|
check_token(Required, Msg, Channel).
|
||||||
|
|
||||||
check_token(true,
|
check_token(
|
||||||
Msg,
|
true,
|
||||||
#channel{token = Token,
|
Msg,
|
||||||
clientinfo = ClientInfo,
|
#channel{
|
||||||
conn_state = CState} = Channel) ->
|
token = Token,
|
||||||
|
clientinfo = ClientInfo,
|
||||||
|
conn_state = CState
|
||||||
|
} = Channel
|
||||||
|
) ->
|
||||||
#{clientid := ClientId} = ClientInfo,
|
#{clientid := ClientId} = ClientInfo,
|
||||||
case emqx_coap_message:get_option(uri_query, Msg) of
|
case emqx_coap_message:get_option(uri_query, Msg) of
|
||||||
#{<<"clientid">> := ClientId,
|
#{
|
||||||
<<"token">> := Token} ->
|
<<"clientid">> := ClientId,
|
||||||
|
<<"token">> := Token
|
||||||
|
} ->
|
||||||
call_session(handle_request, Msg, Channel);
|
call_session(handle_request, Msg, Channel);
|
||||||
#{<<"clientid">> := DesireId} ->
|
#{<<"clientid">> := DesireId} ->
|
||||||
try_takeover(CState, DesireId, Msg, Channel);
|
try_takeover(CState, DesireId, Msg, Channel);
|
||||||
|
|
@ -364,7 +399,6 @@ check_token(true,
|
||||||
Reply = emqx_coap_message:piggyback({error, unauthorized}, Msg),
|
Reply = emqx_coap_message:piggyback({error, unauthorized}, Msg),
|
||||||
{ok, {outgoing, Reply}, Channel}
|
{ok, {outgoing, Reply}, Channel}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
check_token(false, Msg, Channel) ->
|
check_token(false, Msg, Channel) ->
|
||||||
call_session(handle_request, Msg, Channel).
|
call_session(handle_request, Msg, Channel).
|
||||||
|
|
||||||
|
|
@ -383,7 +417,6 @@ try_takeover(idle, DesireId, Msg, Channel) ->
|
||||||
do_takeover(DesireId, Msg, Channel)
|
do_takeover(DesireId, Msg, Channel)
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
try_takeover(_, DesireId, Msg, Channel) ->
|
try_takeover(_, DesireId, Msg, Channel) ->
|
||||||
do_takeover(DesireId, Msg, Channel).
|
do_takeover(DesireId, Msg, Channel).
|
||||||
|
|
||||||
|
|
@ -392,43 +425,57 @@ do_takeover(_DesireId, Msg, Channel) ->
|
||||||
Reset = emqx_coap_message:reset(Msg),
|
Reset = emqx_coap_message:reset(Msg),
|
||||||
{ok, {outgoing, Reset}, Channel}.
|
{ok, {outgoing, Reset}, Channel}.
|
||||||
|
|
||||||
run_conn_hooks(Input, Channel = #channel{ctx = Ctx,
|
run_conn_hooks(
|
||||||
conninfo = ConnInfo}) ->
|
Input,
|
||||||
|
Channel = #channel{
|
||||||
|
ctx = Ctx,
|
||||||
|
conninfo = ConnInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
ConnProps = #{},
|
ConnProps = #{},
|
||||||
case run_hooks(Ctx, 'client.connect', [ConnInfo], ConnProps) of
|
case run_hooks(Ctx, 'client.connect', [ConnInfo], ConnProps) of
|
||||||
Error = {error, _Reason} -> Error;
|
Error = {error, _Reason} -> Error;
|
||||||
_NConnProps ->
|
_NConnProps -> {ok, Input, Channel}
|
||||||
{ok, Input, Channel}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
enrich_conninfo({Queries, _Msg},
|
enrich_conninfo(
|
||||||
Channel = #channel{
|
{Queries, _Msg},
|
||||||
keepalive = KeepAlive,
|
Channel = #channel{
|
||||||
conninfo = ConnInfo}) ->
|
keepalive = KeepAlive,
|
||||||
|
conninfo = ConnInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
case Queries of
|
case Queries of
|
||||||
#{<<"clientid">> := ClientId} ->
|
#{<<"clientid">> := ClientId} ->
|
||||||
Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)),
|
Interval = maps:get(interval, emqx_keepalive:info(KeepAlive)),
|
||||||
NConnInfo = ConnInfo#{ clientid => ClientId
|
NConnInfo = ConnInfo#{
|
||||||
, proto_name => <<"CoAP">>
|
clientid => ClientId,
|
||||||
, proto_ver => <<"1">>
|
proto_name => <<"CoAP">>,
|
||||||
, clean_start => true
|
proto_ver => <<"1">>,
|
||||||
, keepalive => Interval
|
clean_start => true,
|
||||||
, expiry_interval => 0
|
keepalive => Interval,
|
||||||
},
|
expiry_interval => 0
|
||||||
|
},
|
||||||
{ok, Channel#channel{conninfo = NConnInfo}};
|
{ok, Channel#channel{conninfo = NConnInfo}};
|
||||||
_ ->
|
_ ->
|
||||||
{error, "invalid queries", Channel}
|
{error, "invalid queries", Channel}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
enrich_clientinfo({Queries, Msg},
|
enrich_clientinfo(
|
||||||
Channel = #channel{clientinfo = ClientInfo0}) ->
|
{Queries, Msg},
|
||||||
|
Channel = #channel{clientinfo = ClientInfo0}
|
||||||
|
) ->
|
||||||
case Queries of
|
case Queries of
|
||||||
#{<<"username">> := UserName,
|
#{
|
||||||
<<"password">> := Password,
|
<<"username">> := UserName,
|
||||||
<<"clientid">> := ClientId} ->
|
<<"password">> := Password,
|
||||||
ClientInfo = ClientInfo0#{username => UserName,
|
<<"clientid">> := ClientId
|
||||||
password => Password,
|
} ->
|
||||||
clientid => ClientId},
|
ClientInfo = ClientInfo0#{
|
||||||
|
username => UserName,
|
||||||
|
password => Password,
|
||||||
|
clientid => ClientId
|
||||||
|
},
|
||||||
{ok, NClientInfo} = fix_mountpoint(Msg, ClientInfo),
|
{ok, NClientInfo} = fix_mountpoint(Msg, ClientInfo),
|
||||||
{ok, Channel#channel{clientinfo = NClientInfo}};
|
{ok, Channel#channel{clientinfo = NClientInfo}};
|
||||||
_ ->
|
_ ->
|
||||||
|
|
@ -439,19 +486,27 @@ set_log_meta(_Input, #channel{clientinfo = #{clientid := ClientId}}) ->
|
||||||
emqx_logger:set_metadata_clientid(ClientId),
|
emqx_logger:set_metadata_clientid(ClientId),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
auth_connect(_Input, Channel = #channel{ctx = Ctx,
|
auth_connect(
|
||||||
clientinfo = ClientInfo}) ->
|
_Input,
|
||||||
#{clientid := ClientId,
|
Channel = #channel{
|
||||||
username := Username} = ClientInfo,
|
ctx = Ctx,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
|
#{
|
||||||
|
clientid := ClientId,
|
||||||
|
username := Username
|
||||||
|
} = ClientInfo,
|
||||||
case emqx_gateway_ctx:authenticate(Ctx, ClientInfo) of
|
case emqx_gateway_ctx:authenticate(Ctx, ClientInfo) of
|
||||||
{ok, NClientInfo} ->
|
{ok, NClientInfo} ->
|
||||||
{ok, Channel#channel{clientinfo = NClientInfo}};
|
{ok, Channel#channel{clientinfo = NClientInfo}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(warning, #{ msg => "client_login_failed"
|
?SLOG(warning, #{
|
||||||
, username => Username
|
msg => "client_login_failed",
|
||||||
, clientid => ClientId
|
username => Username,
|
||||||
, reason => Reason
|
clientid => ClientId,
|
||||||
}),
|
reason => Reason
|
||||||
|
}),
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -461,33 +516,44 @@ fix_mountpoint(_Packet, ClientInfo = #{mountpoint := Mountpoint}) ->
|
||||||
Mountpoint1 = emqx_mountpoint:replvar(Mountpoint, ClientInfo),
|
Mountpoint1 = emqx_mountpoint:replvar(Mountpoint, ClientInfo),
|
||||||
{ok, ClientInfo#{mountpoint := Mountpoint1}}.
|
{ok, ClientInfo#{mountpoint := Mountpoint1}}.
|
||||||
|
|
||||||
process_connect(#channel{ctx = Ctx,
|
process_connect(
|
||||||
session = Session,
|
#channel{
|
||||||
conninfo = ConnInfo,
|
ctx = Ctx,
|
||||||
clientinfo = ClientInfo} = Channel,
|
session = Session,
|
||||||
Msg, Result, Iter) ->
|
conninfo = ConnInfo,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
} = Channel,
|
||||||
|
Msg,
|
||||||
|
Result,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
%% inherit the old session
|
%% inherit the old session
|
||||||
SessFun = fun(_,_) -> Session end,
|
SessFun = fun(_, _) -> Session end,
|
||||||
case emqx_gateway_ctx:open_session(
|
case
|
||||||
Ctx,
|
emqx_gateway_ctx:open_session(
|
||||||
true,
|
Ctx,
|
||||||
ClientInfo,
|
true,
|
||||||
ConnInfo,
|
ClientInfo,
|
||||||
SessFun,
|
ConnInfo,
|
||||||
emqx_coap_session
|
SessFun,
|
||||||
) of
|
emqx_coap_session
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, _Sess} ->
|
{ok, _Sess} ->
|
||||||
RandVal = rand:uniform(?TOKEN_MAXIMUM),
|
RandVal = rand:uniform(?TOKEN_MAXIMUM),
|
||||||
Token = erlang:list_to_binary(erlang:integer_to_list(RandVal)),
|
Token = erlang:list_to_binary(erlang:integer_to_list(RandVal)),
|
||||||
NResult = Result#{events => [{event, connected}]},
|
NResult = Result#{events => [{event, connected}]},
|
||||||
iter(Iter,
|
iter(
|
||||||
reply({ok, created}, Token, Msg, NResult),
|
Iter,
|
||||||
Channel#channel{token = Token});
|
reply({ok, created}, Token, Msg, NResult),
|
||||||
|
Channel#channel{token = Token}
|
||||||
|
);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{ msg => "failed_open_session"
|
?SLOG(error, #{
|
||||||
, clientid => maps:get(clientid, ClientInfo)
|
msg => "failed_open_session",
|
||||||
, reason => Reason
|
clientid => maps:get(clientid, ClientInfo),
|
||||||
}),
|
reason => Reason
|
||||||
|
}),
|
||||||
iter(Iter, reply({error, bad_request}, Msg, Result), Channel)
|
iter(Iter, reply({error, bad_request}, Msg, Result), Channel)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -505,11 +571,14 @@ metrics_inc(Name, Ctx) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Ensure connected
|
%% Ensure connected
|
||||||
|
|
||||||
ensure_connected(Channel = #channel{ctx = Ctx,
|
ensure_connected(
|
||||||
conninfo = ConnInfo,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo}) ->
|
ctx = Ctx,
|
||||||
NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond)
|
conninfo = ConnInfo,
|
||||||
},
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
|
NConnInfo = ConnInfo#{connected_at => erlang:system_time(millisecond)},
|
||||||
_ = run_hooks(Ctx, 'client.connack', [NConnInfo, connection_accepted, []]),
|
_ = run_hooks(Ctx, 'client.connack', [NConnInfo, connection_accepted, []]),
|
||||||
ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
|
ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
|
||||||
Channel#channel{conninfo = NConnInfo, conn_state = connected}.
|
Channel#channel{conninfo = NConnInfo, conn_state = connected}.
|
||||||
|
|
@ -517,10 +586,14 @@ ensure_connected(Channel = #channel{ctx = Ctx,
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Ensure disconnected
|
%% Ensure disconnected
|
||||||
|
|
||||||
ensure_disconnected(Reason, Channel = #channel{
|
ensure_disconnected(
|
||||||
ctx = Ctx,
|
Reason,
|
||||||
conninfo = ConnInfo,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo}) ->
|
ctx = Ctx,
|
||||||
|
conninfo = ConnInfo,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
||||||
ok = run_hooks(Ctx, 'client.disconnected', [ClientInfo, Reason, NConnInfo]),
|
ok = run_hooks(Ctx, 'client.disconnected', [ClientInfo, Reason, NConnInfo]),
|
||||||
Channel#channel{conninfo = NConnInfo, conn_state = disconnected}.
|
Channel#channel{conninfo = NConnInfo, conn_state = disconnected}.
|
||||||
|
|
@ -540,18 +613,32 @@ call_session(Fun, Msg, #channel{session = Session} = Channel) ->
|
||||||
handle_result(Result, Channel).
|
handle_result(Result, Channel).
|
||||||
|
|
||||||
handle_result(Result, Channel) ->
|
handle_result(Result, Channel) ->
|
||||||
iter([ session, fun process_session/4
|
iter(
|
||||||
, proto, fun process_protocol/4
|
[
|
||||||
, reply, fun process_reply/4
|
session,
|
||||||
, out, fun process_out/4
|
fun process_session/4,
|
||||||
, fun process_nothing/3
|
proto,
|
||||||
],
|
fun process_protocol/4,
|
||||||
Result,
|
reply,
|
||||||
Channel).
|
fun process_reply/4,
|
||||||
|
out,
|
||||||
|
fun process_out/4,
|
||||||
|
fun process_nothing/3
|
||||||
|
],
|
||||||
|
Result,
|
||||||
|
Channel
|
||||||
|
).
|
||||||
|
|
||||||
call_handler(request, Msg, Result,
|
call_handler(
|
||||||
#channel{ctx = Ctx,
|
request,
|
||||||
clientinfo = ClientInfo} = Channel, Iter) ->
|
Msg,
|
||||||
|
Result,
|
||||||
|
#channel{
|
||||||
|
ctx = Ctx,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
} = Channel,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
HandlerResult =
|
HandlerResult =
|
||||||
case emqx_coap_message:get_option(uri_path, Msg) of
|
case emqx_coap_message:get_option(uri_path, Msg) of
|
||||||
[<<"ps">> | RestPath] ->
|
[<<"ps">> | RestPath] ->
|
||||||
|
|
@ -561,15 +648,20 @@ call_handler(request, Msg, Result,
|
||||||
_ ->
|
_ ->
|
||||||
reply({error, bad_request}, Msg)
|
reply({error, bad_request}, Msg)
|
||||||
end,
|
end,
|
||||||
iter([ connection, fun process_connection/4
|
iter(
|
||||||
, subscribe, fun process_subscribe/4 | Iter],
|
[
|
||||||
maps:merge(Result, HandlerResult),
|
connection,
|
||||||
Channel);
|
fun process_connection/4,
|
||||||
|
subscribe,
|
||||||
|
fun process_subscribe/4
|
||||||
|
| Iter
|
||||||
|
],
|
||||||
|
maps:merge(Result, HandlerResult),
|
||||||
|
Channel
|
||||||
|
);
|
||||||
call_handler(response, {{send_request, From}, Response}, Result, Channel, Iter) ->
|
call_handler(response, {{send_request, From}, Response}, Result, Channel, Iter) ->
|
||||||
gen_server:reply(From, Response),
|
gen_server:reply(From, Response),
|
||||||
iter(Iter, Result, Channel);
|
iter(Iter, Result, Channel);
|
||||||
|
|
||||||
call_handler(_, _, Result, Channel, Iter) ->
|
call_handler(_, _, Result, Channel, Iter) ->
|
||||||
iter(Iter, Result, Channel).
|
iter(Iter, Result, Channel).
|
||||||
|
|
||||||
|
|
@ -582,12 +674,13 @@ process_protocol({Type, Msg}, Result, Channel, Iter) ->
|
||||||
%% leaf node
|
%% leaf node
|
||||||
process_out(Outs, Result, Channel, _) ->
|
process_out(Outs, Result, Channel, _) ->
|
||||||
Outs2 = lists:reverse(Outs),
|
Outs2 = lists:reverse(Outs),
|
||||||
Outs3 = case maps:get(reply, Result, undefined) of
|
Outs3 =
|
||||||
undefined ->
|
case maps:get(reply, Result, undefined) of
|
||||||
Outs2;
|
undefined ->
|
||||||
Reply ->
|
Outs2;
|
||||||
[Reply | Outs2]
|
Reply ->
|
||||||
end,
|
[Reply | Outs2]
|
||||||
|
end,
|
||||||
Events = maps:get(events, Result, []),
|
Events = maps:get(events, Result, []),
|
||||||
{ok, [{outgoing, Outs3}] ++ Events, Channel}.
|
{ok, [{outgoing, Outs3}] ++ Events, Channel}.
|
||||||
|
|
||||||
|
|
@ -595,32 +688,48 @@ process_out(Outs, Result, Channel, _) ->
|
||||||
process_nothing(_, _, Channel) ->
|
process_nothing(_, _, Channel) ->
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
process_connection({open, Req}, Result,
|
process_connection(
|
||||||
Channel = #channel{conn_state = idle}, Iter) ->
|
{open, Req},
|
||||||
|
Result,
|
||||||
|
Channel = #channel{conn_state = idle},
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
Queries = emqx_coap_message:get_option(uri_query, Req),
|
Queries = emqx_coap_message:get_option(uri_query, Req),
|
||||||
case emqx_misc:pipeline(
|
case
|
||||||
[ fun enrich_conninfo/2
|
emqx_misc:pipeline(
|
||||||
, fun run_conn_hooks/2
|
[
|
||||||
, fun enrich_clientinfo/2
|
fun enrich_conninfo/2,
|
||||||
, fun set_log_meta/2
|
fun run_conn_hooks/2,
|
||||||
, fun auth_connect/2
|
fun enrich_clientinfo/2,
|
||||||
],
|
fun set_log_meta/2,
|
||||||
{Queries, Req},
|
fun auth_connect/2
|
||||||
Channel) of
|
],
|
||||||
|
{Queries, Req},
|
||||||
|
Channel
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, _Input, NChannel} ->
|
{ok, _Input, NChannel} ->
|
||||||
process_connect(ensure_connected(NChannel), Req, Result, Iter);
|
process_connect(ensure_connected(NChannel), Req, Result, Iter);
|
||||||
{error, ReasonCode, NChannel} ->
|
{error, ReasonCode, NChannel} ->
|
||||||
ErrMsg = io_lib:format("Login Failed: ~ts", [ReasonCode]),
|
ErrMsg = io_lib:format("Login Failed: ~ts", [ReasonCode]),
|
||||||
Payload = iolist_to_binary(ErrMsg),
|
Payload = iolist_to_binary(ErrMsg),
|
||||||
iter(Iter,
|
iter(
|
||||||
reply({error, bad_request}, Payload, Req, Result),
|
Iter,
|
||||||
NChannel)
|
reply({error, bad_request}, Payload, Req, Result),
|
||||||
|
NChannel
|
||||||
|
)
|
||||||
end;
|
end;
|
||||||
process_connection({open, Req}, Result,
|
process_connection(
|
||||||
Channel = #channel{
|
{open, Req},
|
||||||
conn_state = ConnState,
|
Result,
|
||||||
clientinfo = #{clientid := ClientId}}, Iter)
|
Channel = #channel{
|
||||||
when ConnState == connected ->
|
conn_state = ConnState,
|
||||||
|
clientinfo = #{clientid := ClientId}
|
||||||
|
},
|
||||||
|
Iter
|
||||||
|
) when
|
||||||
|
ConnState == connected
|
||||||
|
->
|
||||||
Queries = emqx_coap_message:get_option(uri_query, Req),
|
Queries = emqx_coap_message:get_option(uri_query, Req),
|
||||||
ErrMsg0 =
|
ErrMsg0 =
|
||||||
case Queries of
|
case Queries of
|
||||||
|
|
@ -633,9 +742,11 @@ process_connection({open, Req}, Result,
|
||||||
end,
|
end,
|
||||||
ErrMsg = io_lib:format("Bad Request: ~ts", [ErrMsg0]),
|
ErrMsg = io_lib:format("Bad Request: ~ts", [ErrMsg0]),
|
||||||
Payload = iolist_to_binary(ErrMsg),
|
Payload = iolist_to_binary(ErrMsg),
|
||||||
iter(Iter,
|
iter(
|
||||||
reply({error, bad_request}, Payload, Req, Result),
|
Iter,
|
||||||
Channel);
|
reply({error, bad_request}, Payload, Req, Result),
|
||||||
|
Channel
|
||||||
|
);
|
||||||
process_connection({close, Msg}, _, Channel, _) ->
|
process_connection({close, Msg}, _, Channel, _) ->
|
||||||
Reply = emqx_coap_message:piggyback({ok, deleted}, Msg),
|
Reply = emqx_coap_message:piggyback({ok, deleted}, Msg),
|
||||||
NChannel = ensure_disconnected(normal, Channel),
|
NChannel = ensure_disconnected(normal, Channel),
|
||||||
|
|
@ -651,5 +762,4 @@ process_reply(Reply, Result, #channel{session = Session} = Channel, _) ->
|
||||||
Outs = maps:get(out, Result, []),
|
Outs = maps:get(out, Result, []),
|
||||||
Outs2 = lists:reverse(Outs),
|
Outs2 = lists:reverse(Outs),
|
||||||
Events = maps:get(events, Result, []),
|
Events = maps:get(events, Result, []),
|
||||||
{ok, [{outgoing, [Reply | Outs2]}] ++ Events,
|
{ok, [{outgoing, [Reply | Outs2]}] ++ Events, Channel#channel{session = Session2}}.
|
||||||
Channel#channel{session = Session2}}.
|
|
||||||
|
|
|
||||||
|
|
@ -19,14 +19,15 @@
|
||||||
-behaviour(emqx_gateway_frame).
|
-behaviour(emqx_gateway_frame).
|
||||||
|
|
||||||
%% emqx_gateway_frame callbacks
|
%% emqx_gateway_frame callbacks
|
||||||
-export([ initial_parse_state/1
|
-export([
|
||||||
, serialize_opts/0
|
initial_parse_state/1,
|
||||||
, serialize_pkt/2
|
serialize_opts/0,
|
||||||
, parse/2
|
serialize_pkt/2,
|
||||||
, format/1
|
parse/2,
|
||||||
, type/1
|
format/1,
|
||||||
, is_message/1
|
type/1,
|
||||||
]).
|
is_message/1
|
||||||
|
]).
|
||||||
|
|
||||||
-include("src/coap/include/emqx_coap.hrl").
|
-include("src/coap/include/emqx_coap.hrl").
|
||||||
-include_lib("emqx/include/types.hrl").
|
-include_lib("emqx/include/types.hrl").
|
||||||
|
|
@ -37,7 +38,8 @@
|
||||||
-define(OPTION_URI_HOST, 3).
|
-define(OPTION_URI_HOST, 3).
|
||||||
-define(OPTION_ETAG, 4).
|
-define(OPTION_ETAG, 4).
|
||||||
-define(OPTION_IF_NONE_MATCH, 5).
|
-define(OPTION_IF_NONE_MATCH, 5).
|
||||||
-define(OPTION_OBSERVE, 6). % draft-ietf-core-observe-16
|
% draft-ietf-core-observe-16
|
||||||
|
-define(OPTION_OBSERVE, 6).
|
||||||
-define(OPTION_URI_PORT, 7).
|
-define(OPTION_URI_PORT, 7).
|
||||||
-define(OPTION_LOCATION_PATH, 8).
|
-define(OPTION_LOCATION_PATH, 8).
|
||||||
-define(OPTION_URI_PATH, 11).
|
-define(OPTION_URI_PATH, 11).
|
||||||
|
|
@ -46,7 +48,8 @@
|
||||||
-define(OPTION_URI_QUERY, 15).
|
-define(OPTION_URI_QUERY, 15).
|
||||||
-define(OPTION_ACCEPT, 17).
|
-define(OPTION_ACCEPT, 17).
|
||||||
-define(OPTION_LOCATION_QUERY, 20).
|
-define(OPTION_LOCATION_QUERY, 20).
|
||||||
-define(OPTION_BLOCK2, 23). % draft-ietf-core-block-17
|
% draft-ietf-core-block-17
|
||||||
|
-define(OPTION_BLOCK2, 23).
|
||||||
-define(OPTION_BLOCK1, 27).
|
-define(OPTION_BLOCK1, 27).
|
||||||
-define(OPTION_PROXY_URI, 35).
|
-define(OPTION_PROXY_URI, 35).
|
||||||
-define(OPTION_PROXY_SCHEME, 39).
|
-define(OPTION_PROXY_SCHEME, 39).
|
||||||
|
|
@ -70,22 +73,25 @@ serialize_opts() ->
|
||||||
%% empty message
|
%% empty message
|
||||||
serialize_pkt(#coap_message{type = Type, method = undefined, id = MsgId}, _Opts) ->
|
serialize_pkt(#coap_message{type = Type, method = undefined, id = MsgId}, _Opts) ->
|
||||||
<<?VERSION:2, (encode_type(Type)):2, 0:4, 0:3, 0:5, MsgId:16>>;
|
<<?VERSION:2, (encode_type(Type)):2, 0:4, 0:3, 0:5, MsgId:16>>;
|
||||||
|
serialize_pkt(
|
||||||
serialize_pkt(#coap_message{ type = Type
|
#coap_message{
|
||||||
, method = Method
|
type = Type,
|
||||||
, id = MsgId
|
method = Method,
|
||||||
, token = Token
|
id = MsgId,
|
||||||
, options = Options
|
token = Token,
|
||||||
, payload = Payload
|
options = Options,
|
||||||
},
|
payload = Payload
|
||||||
_Opts) ->
|
},
|
||||||
|
_Opts
|
||||||
|
) ->
|
||||||
TKL = byte_size(Token),
|
TKL = byte_size(Token),
|
||||||
{Class, Code} = method_to_class_code(Method),
|
{Class, Code} = method_to_class_code(Method),
|
||||||
Head = <<?VERSION:2, (encode_type(Type)):2, TKL:4, Class:3, Code:5, MsgId:16, Token:TKL/binary>>,
|
Head =
|
||||||
|
<<?VERSION:2, (encode_type(Type)):2, TKL:4, Class:3, Code:5, MsgId:16, Token:TKL/binary>>,
|
||||||
FlatOpts = flatten_options(Options),
|
FlatOpts = flatten_options(Options),
|
||||||
encode_option_list(FlatOpts, 0, Head, Payload).
|
encode_option_list(FlatOpts, 0, Head, Payload).
|
||||||
|
|
||||||
-spec encode_type(message_type()) -> 0 .. 3.
|
-spec encode_type(message_type()) -> 0..3.
|
||||||
encode_type(con) -> 0;
|
encode_type(con) -> 0;
|
||||||
encode_type(non) -> 1;
|
encode_type(non) -> 1;
|
||||||
encode_type(ack) -> 2;
|
encode_type(ack) -> 2;
|
||||||
|
|
@ -96,91 +102,121 @@ flatten_options(Opts) ->
|
||||||
|
|
||||||
flatten_options([{_OptId, undefined} | T], Acc) ->
|
flatten_options([{_OptId, undefined} | T], Acc) ->
|
||||||
flatten_options(T, Acc);
|
flatten_options(T, Acc);
|
||||||
|
|
||||||
flatten_options([{OptId, OptVal} | T], Acc) ->
|
flatten_options([{OptId, OptVal} | T], Acc) ->
|
||||||
flatten_options(T,
|
flatten_options(
|
||||||
case is_repeatable_option(OptId) of
|
T,
|
||||||
false ->
|
case is_repeatable_option(OptId) of
|
||||||
[encode_option(OptId, OptVal) | Acc];
|
false ->
|
||||||
_ ->
|
[encode_option(OptId, OptVal) | Acc];
|
||||||
try_encode_repeatable(OptId, OptVal) ++ Acc
|
_ ->
|
||||||
end);
|
try_encode_repeatable(OptId, OptVal) ++ Acc
|
||||||
|
end
|
||||||
|
);
|
||||||
flatten_options([], Acc) ->
|
flatten_options([], Acc) ->
|
||||||
%% sort by option id for calculate the deltas
|
%% sort by option id for calculate the deltas
|
||||||
lists:keysort(1, Acc).
|
lists:keysort(1, Acc).
|
||||||
|
|
||||||
encode_option_list([{OptNum, OptVal} | OptionList], LastNum, Acc, Payload) ->
|
encode_option_list([{OptNum, OptVal} | OptionList], LastNum, Acc, Payload) ->
|
||||||
NumDiff = OptNum - LastNum,
|
NumDiff = OptNum - LastNum,
|
||||||
{Delta, ExtNum} = if
|
{Delta, ExtNum} =
|
||||||
NumDiff >= 269 ->
|
if
|
||||||
{14, <<(NumDiff - 269):16>>};
|
NumDiff >= 269 ->
|
||||||
OptNum - LastNum >= 13 ->
|
{14, <<(NumDiff - 269):16>>};
|
||||||
{13, <<(NumDiff - 13)>>};
|
OptNum - LastNum >= 13 ->
|
||||||
true ->
|
{13, <<(NumDiff - 13)>>};
|
||||||
{NumDiff, <<>>}
|
true ->
|
||||||
end,
|
{NumDiff, <<>>}
|
||||||
|
end,
|
||||||
Binaryize = byte_size(OptVal),
|
Binaryize = byte_size(OptVal),
|
||||||
{Len, ExtLen} = if
|
{Len, ExtLen} =
|
||||||
Binaryize >= 269 ->
|
if
|
||||||
{14, <<(Binaryize - 269):16>>};
|
Binaryize >= 269 ->
|
||||||
Binaryize >= 13 ->
|
{14, <<(Binaryize - 269):16>>};
|
||||||
{13, <<(Binaryize - 13)>>};
|
Binaryize >= 13 ->
|
||||||
true ->
|
{13, <<(Binaryize - 13)>>};
|
||||||
{Binaryize, <<>>}
|
true ->
|
||||||
end,
|
{Binaryize, <<>>}
|
||||||
|
end,
|
||||||
Acc2 = <<Acc/binary, Delta:4, Len:4, ExtNum/binary, ExtLen/binary, OptVal/binary>>,
|
Acc2 = <<Acc/binary, Delta:4, Len:4, ExtNum/binary, ExtLen/binary, OptVal/binary>>,
|
||||||
encode_option_list(OptionList, OptNum, Acc2, Payload);
|
encode_option_list(OptionList, OptNum, Acc2, Payload);
|
||||||
|
|
||||||
encode_option_list([], _LastNum, Acc, <<>>) ->
|
encode_option_list([], _LastNum, Acc, <<>>) ->
|
||||||
Acc;
|
Acc;
|
||||||
encode_option_list([], _, Acc, Payload) ->
|
encode_option_list([], _, Acc, Payload) ->
|
||||||
<<Acc/binary, 16#FF, Payload/binary>>.
|
<<Acc/binary, 16#FF, Payload/binary>>.
|
||||||
|
|
||||||
try_encode_repeatable(uri_query, Val) when is_map(Val) ->
|
try_encode_repeatable(uri_query, Val) when is_map(Val) ->
|
||||||
maps:fold(fun(K, V, Acc) ->
|
maps:fold(
|
||||||
[encode_option(uri_query, <<K/binary, $=, V/binary>>) | Acc]
|
fun(K, V, Acc) ->
|
||||||
end,
|
[encode_option(uri_query, <<K/binary, $=, V/binary>>) | Acc]
|
||||||
[], Val);
|
end,
|
||||||
|
[],
|
||||||
|
Val
|
||||||
|
);
|
||||||
try_encode_repeatable(K, Val) ->
|
try_encode_repeatable(K, Val) ->
|
||||||
lists:foldr(fun(undefined, Acc) ->
|
lists:foldr(
|
||||||
Acc;
|
fun
|
||||||
(E, Acc) ->
|
(undefined, Acc) ->
|
||||||
[encode_option(K, E) | Acc]
|
Acc;
|
||||||
end, [], Val).
|
(E, Acc) ->
|
||||||
|
[encode_option(K, E) | Acc]
|
||||||
|
end,
|
||||||
|
[],
|
||||||
|
Val
|
||||||
|
).
|
||||||
|
|
||||||
%% RFC 7252
|
%% RFC 7252
|
||||||
encode_option(if_match, OptVal) -> {?OPTION_IF_MATCH, OptVal};
|
encode_option(if_match, OptVal) ->
|
||||||
encode_option(uri_host, OptVal) -> {?OPTION_URI_HOST, OptVal};
|
{?OPTION_IF_MATCH, OptVal};
|
||||||
encode_option(etag, OptVal) -> {?OPTION_ETAG, OptVal};
|
encode_option(uri_host, OptVal) ->
|
||||||
encode_option(if_none_match, true) -> {?OPTION_IF_NONE_MATCH, <<>>};
|
{?OPTION_URI_HOST, OptVal};
|
||||||
encode_option(uri_port, OptVal) -> {?OPTION_URI_PORT, binary:encode_unsigned(OptVal)};
|
encode_option(etag, OptVal) ->
|
||||||
encode_option(location_path, OptVal) -> {?OPTION_LOCATION_PATH, OptVal};
|
{?OPTION_ETAG, OptVal};
|
||||||
encode_option(uri_path, OptVal) -> {?OPTION_URI_PATH, OptVal};
|
encode_option(if_none_match, true) ->
|
||||||
|
{?OPTION_IF_NONE_MATCH, <<>>};
|
||||||
|
encode_option(uri_port, OptVal) ->
|
||||||
|
{?OPTION_URI_PORT, binary:encode_unsigned(OptVal)};
|
||||||
|
encode_option(location_path, OptVal) ->
|
||||||
|
{?OPTION_LOCATION_PATH, OptVal};
|
||||||
|
encode_option(uri_path, OptVal) ->
|
||||||
|
{?OPTION_URI_PATH, OptVal};
|
||||||
encode_option(content_format, OptVal) when is_integer(OptVal) ->
|
encode_option(content_format, OptVal) when is_integer(OptVal) ->
|
||||||
{?OPTION_CONTENT_FORMAT, binary:encode_unsigned(OptVal)};
|
{?OPTION_CONTENT_FORMAT, binary:encode_unsigned(OptVal)};
|
||||||
encode_option(content_format, OptVal) ->
|
encode_option(content_format, OptVal) ->
|
||||||
Num = content_format_to_code(OptVal),
|
Num = content_format_to_code(OptVal),
|
||||||
{?OPTION_CONTENT_FORMAT, binary:encode_unsigned(Num)};
|
{?OPTION_CONTENT_FORMAT, binary:encode_unsigned(Num)};
|
||||||
encode_option(max_age, OptVal) -> {?OPTION_MAX_AGE, binary:encode_unsigned(OptVal)};
|
encode_option(max_age, OptVal) ->
|
||||||
encode_option(uri_query, OptVal) -> {?OPTION_URI_QUERY, OptVal};
|
{?OPTION_MAX_AGE, binary:encode_unsigned(OptVal)};
|
||||||
encode_option('accept', OptVal) -> {?OPTION_ACCEPT, binary:encode_unsigned(OptVal)};
|
encode_option(uri_query, OptVal) ->
|
||||||
encode_option(location_query, OptVal) -> {?OPTION_LOCATION_QUERY, OptVal};
|
{?OPTION_URI_QUERY, OptVal};
|
||||||
encode_option(proxy_uri, OptVal) -> {?OPTION_PROXY_URI, OptVal};
|
encode_option('accept', OptVal) ->
|
||||||
encode_option(proxy_scheme, OptVal) -> {?OPTION_PROXY_SCHEME, OptVal};
|
{?OPTION_ACCEPT, binary:encode_unsigned(OptVal)};
|
||||||
encode_option(size1, OptVal) -> {?OPTION_SIZE1, binary:encode_unsigned(OptVal)};
|
encode_option(location_query, OptVal) ->
|
||||||
encode_option(observe, OptVal) -> {?OPTION_OBSERVE, binary:encode_unsigned(OptVal)};
|
{?OPTION_LOCATION_QUERY, OptVal};
|
||||||
encode_option(block2, OptVal) -> {?OPTION_BLOCK2, encode_block(OptVal)};
|
encode_option(proxy_uri, OptVal) ->
|
||||||
encode_option(block1, OptVal) -> {?OPTION_BLOCK1, encode_block(OptVal)};
|
{?OPTION_PROXY_URI, OptVal};
|
||||||
|
encode_option(proxy_scheme, OptVal) ->
|
||||||
|
{?OPTION_PROXY_SCHEME, OptVal};
|
||||||
|
encode_option(size1, OptVal) ->
|
||||||
|
{?OPTION_SIZE1, binary:encode_unsigned(OptVal)};
|
||||||
|
encode_option(observe, OptVal) ->
|
||||||
|
{?OPTION_OBSERVE, binary:encode_unsigned(OptVal)};
|
||||||
|
encode_option(block2, OptVal) ->
|
||||||
|
{?OPTION_BLOCK2, encode_block(OptVal)};
|
||||||
|
encode_option(block1, OptVal) ->
|
||||||
|
{?OPTION_BLOCK1, encode_block(OptVal)};
|
||||||
%% unknown opton
|
%% unknown opton
|
||||||
encode_option(Option, Value) ->
|
encode_option(Option, Value) ->
|
||||||
erlang:throw({bad_option, Option, Value}).
|
erlang:throw({bad_option, Option, Value}).
|
||||||
|
|
||||||
encode_block({Num, More, Size}) ->
|
encode_block({Num, More, Size}) ->
|
||||||
encode_block1(Num,
|
encode_block1(
|
||||||
if More -> 1; true -> 0 end,
|
Num,
|
||||||
trunc(math:log2(Size))-4).
|
if
|
||||||
|
More -> 1;
|
||||||
|
true -> 0
|
||||||
|
end,
|
||||||
|
trunc(math:log2(Size)) - 4
|
||||||
|
).
|
||||||
|
|
||||||
encode_block1(Num, M, SizEx) when Num < 16 ->
|
encode_block1(Num, M, SizEx) when Num < 16 ->
|
||||||
<<Num:4, M:1, SizEx:3>>;
|
<<Num:4, M:1, SizEx:3>>;
|
||||||
|
|
@ -192,14 +228,15 @@ encode_block1(Num, M, SizEx) ->
|
||||||
-spec content_format_to_code(binary()) -> non_neg_integer().
|
-spec content_format_to_code(binary()) -> non_neg_integer().
|
||||||
content_format_to_code(<<"text/plain">>) -> 0;
|
content_format_to_code(<<"text/plain">>) -> 0;
|
||||||
content_format_to_code(<<"application/link-format">>) -> 40;
|
content_format_to_code(<<"application/link-format">>) -> 40;
|
||||||
content_format_to_code(<<"application/xml">>) ->41;
|
content_format_to_code(<<"application/xml">>) -> 41;
|
||||||
content_format_to_code(<<"application/octet-stream">>) -> 42;
|
content_format_to_code(<<"application/octet-stream">>) -> 42;
|
||||||
content_format_to_code(<<"application/exi">>) -> 47;
|
content_format_to_code(<<"application/exi">>) -> 47;
|
||||||
content_format_to_code(<<"application/json">>) -> 50;
|
content_format_to_code(<<"application/json">>) -> 50;
|
||||||
content_format_to_code(<<"application/cbor">>) -> 60;
|
content_format_to_code(<<"application/cbor">>) -> 60;
|
||||||
content_format_to_code(<<"application/vnd.oma.lwm2m+tlv">>) -> 11542;
|
content_format_to_code(<<"application/vnd.oma.lwm2m+tlv">>) -> 11542;
|
||||||
content_format_to_code(<<"application/vnd.oma.lwm2m+json">>) -> 11543;
|
content_format_to_code(<<"application/vnd.oma.lwm2m+json">>) -> 11543;
|
||||||
content_format_to_code(_) -> 42. %% use octet-stream as default
|
%% use octet-stream as default
|
||||||
|
content_format_to_code(_) -> 42.
|
||||||
|
|
||||||
method_to_class_code(get) -> {0, 01};
|
method_to_class_code(get) -> {0, 01};
|
||||||
method_to_class_code(post) -> {0, 02};
|
method_to_class_code(post) -> {0, 02};
|
||||||
|
|
@ -229,54 +266,58 @@ method_to_class_code({error, bad_gateway}) -> {5, 02};
|
||||||
method_to_class_code({error, service_unavailable}) -> {5, 03};
|
method_to_class_code({error, service_unavailable}) -> {5, 03};
|
||||||
method_to_class_code({error, gateway_timeout}) -> {5, 04};
|
method_to_class_code({error, gateway_timeout}) -> {5, 04};
|
||||||
method_to_class_code({error, proxying_not_supported}) -> {5, 05};
|
method_to_class_code({error, proxying_not_supported}) -> {5, 05};
|
||||||
method_to_class_code(Method) ->
|
method_to_class_code(Method) -> erlang:throw({bad_method, Method}).
|
||||||
erlang:throw({bad_method, Method}).
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% parse
|
%% parse
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec parse(binary(), emqx_gateway_frame:parse_state())
|
-spec parse(binary(), emqx_gateway_frame:parse_state()) ->
|
||||||
-> emqx_gateway_frame:parse_result().
|
emqx_gateway_frame:parse_result().
|
||||||
parse(<<?VERSION:2, Type:2, 0:4, 0:3, 0:5, MsgId:16>>, ParseState) ->
|
parse(<<?VERSION:2, Type:2, 0:4, 0:3, 0:5, MsgId:16>>, ParseState) ->
|
||||||
{ok,
|
{ok,
|
||||||
#coap_message{ type = decode_type(Type)
|
#coap_message{
|
||||||
, id = MsgId},
|
type = decode_type(Type),
|
||||||
<<>>,
|
id = MsgId
|
||||||
ParseState};
|
},
|
||||||
|
<<>>, ParseState};
|
||||||
parse(<<?VERSION:2, Type:2, TKL:4, Class:3, Code:5, MsgId:16, Token:TKL/binary, Tail/binary>>,
|
parse(
|
||||||
ParseState) ->
|
<<?VERSION:2, Type:2, TKL:4, Class:3, Code:5, MsgId:16, Token:TKL/binary, Tail/binary>>,
|
||||||
|
ParseState
|
||||||
|
) ->
|
||||||
{Options, Payload} = decode_option_list(Tail),
|
{Options, Payload} = decode_option_list(Tail),
|
||||||
Options2 = maps:fold(fun(K, V, Acc) ->
|
Options2 = maps:fold(
|
||||||
Acc#{K => get_option_val(K, V)}
|
fun(K, V, Acc) ->
|
||||||
end,
|
Acc#{K => get_option_val(K, V)}
|
||||||
#{},
|
end,
|
||||||
Options),
|
#{},
|
||||||
|
Options
|
||||||
|
),
|
||||||
{ok,
|
{ok,
|
||||||
#coap_message{ type = decode_type(Type)
|
#coap_message{
|
||||||
, method = class_code_to_method({Class, Code})
|
type = decode_type(Type),
|
||||||
, id = MsgId
|
method = class_code_to_method({Class, Code}),
|
||||||
, token = Token
|
id = MsgId,
|
||||||
, options = Options2
|
token = Token,
|
||||||
, payload = Payload
|
options = Options2,
|
||||||
},
|
payload = Payload
|
||||||
<<>>,
|
},
|
||||||
ParseState}.
|
<<>>, ParseState}.
|
||||||
|
|
||||||
get_option_val(uri_query, V) ->
|
get_option_val(uri_query, V) ->
|
||||||
KVList = lists:foldl(fun(E, Acc) ->
|
KVList = lists:foldl(
|
||||||
case re:split(E, "=") of
|
fun(E, Acc) ->
|
||||||
[Key, Val] ->
|
case re:split(E, "=") of
|
||||||
[{Key, Val} | Acc];
|
[Key, Val] ->
|
||||||
_ ->
|
[{Key, Val} | Acc];
|
||||||
Acc
|
_ ->
|
||||||
end
|
Acc
|
||||||
end,
|
end
|
||||||
[],
|
end,
|
||||||
V),
|
[],
|
||||||
|
V
|
||||||
|
),
|
||||||
maps:from_list(KVList);
|
maps:from_list(KVList);
|
||||||
|
|
||||||
get_option_val(K, V) ->
|
get_option_val(K, V) ->
|
||||||
case is_repeatable_option(K) of
|
case is_repeatable_option(K) of
|
||||||
true ->
|
true ->
|
||||||
|
|
@ -285,8 +326,8 @@ get_option_val(K, V) ->
|
||||||
V
|
V
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec decode_type(X) -> message_type()
|
-spec decode_type(X) -> message_type() when
|
||||||
when X :: 0 .. 3.
|
X :: 0..3.
|
||||||
decode_type(0) -> con;
|
decode_type(0) -> con;
|
||||||
decode_type(1) -> non;
|
decode_type(1) -> non;
|
||||||
decode_type(2) -> ack;
|
decode_type(2) -> ack;
|
||||||
|
|
@ -298,10 +339,8 @@ decode_option_list(Bin) ->
|
||||||
|
|
||||||
decode_option_list(<<>>, _OptNum, OptMap) ->
|
decode_option_list(<<>>, _OptNum, OptMap) ->
|
||||||
{OptMap, <<>>};
|
{OptMap, <<>>};
|
||||||
|
|
||||||
decode_option_list(<<16#FF, Payload/binary>>, _OptNum, OptMap) ->
|
decode_option_list(<<16#FF, Payload/binary>>, _OptNum, OptMap) ->
|
||||||
{OptMap, Payload};
|
{OptMap, Payload};
|
||||||
|
|
||||||
decode_option_list(<<Delta:4, Len:4, Bin/binary>>, OptNum, OptMap) ->
|
decode_option_list(<<Delta:4, Len:4, Bin/binary>>, OptNum, OptMap) ->
|
||||||
case Delta of
|
case Delta of
|
||||||
Any when Any < 13 ->
|
Any when Any < 13 ->
|
||||||
|
|
@ -349,30 +388,48 @@ append_option(OptNum, RawOptVal, OptMap) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% RFC 7252
|
%% RFC 7252
|
||||||
decode_option(?OPTION_IF_MATCH, OptVal) -> {if_match, OptVal};
|
decode_option(?OPTION_IF_MATCH, OptVal) ->
|
||||||
decode_option(?OPTION_URI_HOST, OptVal) -> {uri_host, OptVal};
|
{if_match, OptVal};
|
||||||
decode_option(?OPTION_ETAG, OptVal) -> {etag, OptVal};
|
decode_option(?OPTION_URI_HOST, OptVal) ->
|
||||||
decode_option(?OPTION_IF_NONE_MATCH, <<>>) -> {if_none_match, true};
|
{uri_host, OptVal};
|
||||||
decode_option(?OPTION_URI_PORT, OptVal) -> {uri_port, binary:decode_unsigned(OptVal)};
|
decode_option(?OPTION_ETAG, OptVal) ->
|
||||||
decode_option(?OPTION_LOCATION_PATH, OptVal) -> {location_path, OptVal};
|
{etag, OptVal};
|
||||||
decode_option(?OPTION_URI_PATH, OptVal) -> {uri_path, OptVal};
|
decode_option(?OPTION_IF_NONE_MATCH, <<>>) ->
|
||||||
|
{if_none_match, true};
|
||||||
|
decode_option(?OPTION_URI_PORT, OptVal) ->
|
||||||
|
{uri_port, binary:decode_unsigned(OptVal)};
|
||||||
|
decode_option(?OPTION_LOCATION_PATH, OptVal) ->
|
||||||
|
{location_path, OptVal};
|
||||||
|
decode_option(?OPTION_URI_PATH, OptVal) ->
|
||||||
|
{uri_path, OptVal};
|
||||||
decode_option(?OPTION_CONTENT_FORMAT, OptVal) ->
|
decode_option(?OPTION_CONTENT_FORMAT, OptVal) ->
|
||||||
Num = binary:decode_unsigned(OptVal),
|
Num = binary:decode_unsigned(OptVal),
|
||||||
{content_format, content_code_to_format(Num)};
|
{content_format, content_code_to_format(Num)};
|
||||||
decode_option(?OPTION_MAX_AGE, OptVal) -> {max_age, binary:decode_unsigned(OptVal)};
|
decode_option(?OPTION_MAX_AGE, OptVal) ->
|
||||||
decode_option(?OPTION_URI_QUERY, OptVal) -> {uri_query, OptVal};
|
{max_age, binary:decode_unsigned(OptVal)};
|
||||||
decode_option(?OPTION_ACCEPT, OptVal) -> {'accept', binary:decode_unsigned(OptVal)};
|
decode_option(?OPTION_URI_QUERY, OptVal) ->
|
||||||
decode_option(?OPTION_LOCATION_QUERY, OptVal) -> {location_query, OptVal};
|
{uri_query, OptVal};
|
||||||
decode_option(?OPTION_PROXY_URI, OptVal) -> {proxy_uri, OptVal};
|
decode_option(?OPTION_ACCEPT, OptVal) ->
|
||||||
decode_option(?OPTION_PROXY_SCHEME, OptVal) -> {proxy_scheme, OptVal};
|
{'accept', binary:decode_unsigned(OptVal)};
|
||||||
decode_option(?OPTION_SIZE1, OptVal) -> {size1, binary:decode_unsigned(OptVal)};
|
decode_option(?OPTION_LOCATION_QUERY, OptVal) ->
|
||||||
|
{location_query, OptVal};
|
||||||
|
decode_option(?OPTION_PROXY_URI, OptVal) ->
|
||||||
|
{proxy_uri, OptVal};
|
||||||
|
decode_option(?OPTION_PROXY_SCHEME, OptVal) ->
|
||||||
|
{proxy_scheme, OptVal};
|
||||||
|
decode_option(?OPTION_SIZE1, OptVal) ->
|
||||||
|
{size1, binary:decode_unsigned(OptVal)};
|
||||||
%% draft-ietf-core-observe-16
|
%% draft-ietf-core-observe-16
|
||||||
decode_option(?OPTION_OBSERVE, OptVal) -> {observe, binary:decode_unsigned(OptVal)};
|
decode_option(?OPTION_OBSERVE, OptVal) ->
|
||||||
|
{observe, binary:decode_unsigned(OptVal)};
|
||||||
%% draft-ietf-core-block-17
|
%% draft-ietf-core-block-17
|
||||||
decode_option(?OPTION_BLOCK2, OptVal) -> {block2, decode_block(OptVal)};
|
decode_option(?OPTION_BLOCK2, OptVal) ->
|
||||||
decode_option(?OPTION_BLOCK1, OptVal) -> {block1, decode_block(OptVal)};
|
{block2, decode_block(OptVal)};
|
||||||
|
decode_option(?OPTION_BLOCK1, OptVal) ->
|
||||||
|
{block1, decode_block(OptVal)};
|
||||||
%% unknown option
|
%% unknown option
|
||||||
decode_option(OptNum, OptVal) -> {OptNum, OptVal}.
|
decode_option(OptNum, OptVal) ->
|
||||||
|
{OptNum, OptVal}.
|
||||||
|
|
||||||
decode_block(<<Num:4, M:1, SizEx:3>>) -> decode_block1(Num, M, SizEx);
|
decode_block(<<Num:4, M:1, SizEx:3>>) -> decode_block1(Num, M, SizEx);
|
||||||
decode_block(<<Num:12, M:1, SizEx:3>>) -> decode_block1(Num, M, SizEx);
|
decode_block(<<Num:12, M:1, SizEx:3>>) -> decode_block1(Num, M, SizEx);
|
||||||
|
|
@ -391,7 +448,8 @@ content_code_to_format(50) -> <<"application/json">>;
|
||||||
content_code_to_format(60) -> <<"application/cbor">>;
|
content_code_to_format(60) -> <<"application/cbor">>;
|
||||||
content_code_to_format(11542) -> <<"application/vnd.oma.lwm2m+tlv">>;
|
content_code_to_format(11542) -> <<"application/vnd.oma.lwm2m+tlv">>;
|
||||||
content_code_to_format(11543) -> <<"application/vnd.oma.lwm2m+json">>;
|
content_code_to_format(11543) -> <<"application/vnd.oma.lwm2m+json">>;
|
||||||
content_code_to_format(_) -> <<"application/octet-stream">>. %% use octet as default
|
%% use octet as default
|
||||||
|
content_code_to_format(_) -> <<"application/octet-stream">>.
|
||||||
|
|
||||||
%% RFC 7252
|
%% RFC 7252
|
||||||
%% atom indicate a request
|
%% atom indicate a request
|
||||||
|
|
@ -399,7 +457,6 @@ class_code_to_method({0, 01}) -> get;
|
||||||
class_code_to_method({0, 02}) -> post;
|
class_code_to_method({0, 02}) -> post;
|
||||||
class_code_to_method({0, 03}) -> put;
|
class_code_to_method({0, 03}) -> put;
|
||||||
class_code_to_method({0, 04}) -> delete;
|
class_code_to_method({0, 04}) -> delete;
|
||||||
|
|
||||||
%% success is a tuple {ok, ...}
|
%% success is a tuple {ok, ...}
|
||||||
class_code_to_method({2, 01}) -> {ok, created};
|
class_code_to_method({2, 01}) -> {ok, created};
|
||||||
class_code_to_method({2, 02}) -> {ok, deleted};
|
class_code_to_method({2, 02}) -> {ok, deleted};
|
||||||
|
|
@ -407,8 +464,8 @@ class_code_to_method({2, 03}) -> {ok, valid};
|
||||||
class_code_to_method({2, 04}) -> {ok, changed};
|
class_code_to_method({2, 04}) -> {ok, changed};
|
||||||
class_code_to_method({2, 05}) -> {ok, content};
|
class_code_to_method({2, 05}) -> {ok, content};
|
||||||
class_code_to_method({2, 07}) -> {ok, nocontent};
|
class_code_to_method({2, 07}) -> {ok, nocontent};
|
||||||
class_code_to_method({2, 31}) -> {ok, continue}; % block
|
% block
|
||||||
|
class_code_to_method({2, 31}) -> {ok, continue};
|
||||||
%% error is a tuple {error, ...}
|
%% error is a tuple {error, ...}
|
||||||
class_code_to_method({4, 00}) -> {error, bad_request};
|
class_code_to_method({4, 00}) -> {error, bad_request};
|
||||||
class_code_to_method({4, 01}) -> {error, unauthorized};
|
class_code_to_method({4, 01}) -> {error, unauthorized};
|
||||||
|
|
@ -417,7 +474,8 @@ class_code_to_method({4, 03}) -> {error, forbidden};
|
||||||
class_code_to_method({4, 04}) -> {error, not_found};
|
class_code_to_method({4, 04}) -> {error, not_found};
|
||||||
class_code_to_method({4, 05}) -> {error, method_not_allowed};
|
class_code_to_method({4, 05}) -> {error, method_not_allowed};
|
||||||
class_code_to_method({4, 06}) -> {error, not_acceptable};
|
class_code_to_method({4, 06}) -> {error, not_acceptable};
|
||||||
class_code_to_method({4, 08}) -> {error, request_entity_incomplete}; % block
|
% block
|
||||||
|
class_code_to_method({4, 08}) -> {error, request_entity_incomplete};
|
||||||
class_code_to_method({4, 12}) -> {error, precondition_failed};
|
class_code_to_method({4, 12}) -> {error, precondition_failed};
|
||||||
class_code_to_method({4, 13}) -> {error, request_entity_too_large};
|
class_code_to_method({4, 13}) -> {error, request_entity_too_large};
|
||||||
class_code_to_method({4, 15}) -> {error, unsupported_content_format};
|
class_code_to_method({4, 15}) -> {error, unsupported_content_format};
|
||||||
|
|
|
||||||
|
|
@ -21,29 +21,33 @@
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
-include_lib("emqx_gateway/include/emqx_gateway.hrl").
|
-include_lib("emqx_gateway/include/emqx_gateway.hrl").
|
||||||
|
|
||||||
-import(emqx_gateway_utils,
|
-import(
|
||||||
[ normalize_config/1
|
emqx_gateway_utils,
|
||||||
, start_listeners/4
|
[
|
||||||
, stop_listeners/2
|
normalize_config/1,
|
||||||
]).
|
start_listeners/4,
|
||||||
|
stop_listeners/2
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([ reg/0
|
-export([
|
||||||
, unreg/0
|
reg/0,
|
||||||
]).
|
unreg/0
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ on_gateway_load/2
|
-export([
|
||||||
, on_gateway_update/3
|
on_gateway_load/2,
|
||||||
, on_gateway_unload/2
|
on_gateway_update/3,
|
||||||
]).
|
on_gateway_unload/2
|
||||||
|
]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% APIs
|
%% APIs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
reg() ->
|
reg() ->
|
||||||
RegistryOptions = [ {cbkmod, ?MODULE}
|
RegistryOptions = [{cbkmod, ?MODULE}],
|
||||||
],
|
|
||||||
emqx_gateway_registry:reg(coap, RegistryOptions).
|
emqx_gateway_registry:reg(coap, RegistryOptions).
|
||||||
|
|
||||||
unreg() ->
|
unreg() ->
|
||||||
|
|
@ -53,22 +57,33 @@ unreg() ->
|
||||||
%% emqx_gateway_registry callbacks
|
%% emqx_gateway_registry callbacks
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
on_gateway_load(_Gateway = #{name := GwName,
|
on_gateway_load(
|
||||||
config := Config
|
_Gateway = #{
|
||||||
}, Ctx) ->
|
name := GwName,
|
||||||
|
config := Config
|
||||||
|
},
|
||||||
|
Ctx
|
||||||
|
) ->
|
||||||
Listeners = normalize_config(Config),
|
Listeners = normalize_config(Config),
|
||||||
ModCfg = #{frame_mod => emqx_coap_frame,
|
ModCfg = #{
|
||||||
chann_mod => emqx_coap_channel
|
frame_mod => emqx_coap_frame,
|
||||||
},
|
chann_mod => emqx_coap_channel
|
||||||
case start_listeners(
|
},
|
||||||
Listeners, GwName, Ctx, ModCfg) of
|
case
|
||||||
|
start_listeners(
|
||||||
|
Listeners, GwName, Ctx, ModCfg
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, ListenerPids} ->
|
{ok, ListenerPids} ->
|
||||||
{ok, ListenerPids, #{ctx => Ctx}};
|
{ok, ListenerPids, #{ctx => Ctx}};
|
||||||
{error, {Reason, Listener}} ->
|
{error, {Reason, Listener}} ->
|
||||||
throw({badconf, #{ key => listeners
|
throw(
|
||||||
, vallue => Listener
|
{badconf, #{
|
||||||
, reason => Reason
|
key => listeners,
|
||||||
}})
|
vallue => Listener,
|
||||||
|
reason => Reason
|
||||||
|
}}
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
||||||
|
|
@ -79,15 +94,21 @@ on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
||||||
on_gateway_unload(Gateway, GwState),
|
on_gateway_unload(Gateway, GwState),
|
||||||
on_gateway_load(Gateway#{config => Config}, Ctx)
|
on_gateway_load(Gateway#{config => Config}, Ctx)
|
||||||
catch
|
catch
|
||||||
Class : Reason : Stk ->
|
Class:Reason:Stk ->
|
||||||
logger:error("Failed to update ~ts; "
|
logger:error(
|
||||||
"reason: {~0p, ~0p} stacktrace: ~0p",
|
"Failed to update ~ts; "
|
||||||
[GwName, Class, Reason, Stk]),
|
"reason: {~0p, ~0p} stacktrace: ~0p",
|
||||||
|
[GwName, Class, Reason, Stk]
|
||||||
|
),
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
on_gateway_unload(_Gateway = #{ name := GwName,
|
on_gateway_unload(
|
||||||
config := Config
|
_Gateway = #{
|
||||||
}, _GwState) ->
|
name := GwName,
|
||||||
|
config := Config
|
||||||
|
},
|
||||||
|
_GwState
|
||||||
|
) ->
|
||||||
Listeners = normalize_config(Config),
|
Listeners = normalize_config(Config),
|
||||||
stop_listeners(GwName, Listeners).
|
stop_listeners(GwName, Listeners).
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,15 @@
|
||||||
-include("src/coap/include/emqx_coap.hrl").
|
-include("src/coap/include/emqx_coap.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ empty/0, reset/1, reset/2
|
-export([
|
||||||
, out/1, out/2, proto_out/1
|
empty/0,
|
||||||
, proto_out/2, iter/3, iter/4
|
reset/1, reset/2,
|
||||||
, reply/2, reply/3, reply/4]).
|
out/1, out/2,
|
||||||
|
proto_out/1,
|
||||||
|
proto_out/2,
|
||||||
|
iter/3, iter/4,
|
||||||
|
reply/2, reply/3, reply/4
|
||||||
|
]).
|
||||||
|
|
||||||
%%-type result() :: map() | empty.
|
%%-type result() :: map() | empty.
|
||||||
|
|
||||||
|
|
@ -46,7 +51,6 @@ out(Msg) ->
|
||||||
|
|
||||||
out(Msg, #{out := Outs} = Result) ->
|
out(Msg, #{out := Outs} = Result) ->
|
||||||
Result#{out := [Msg | Outs]};
|
Result#{out := [Msg | Outs]};
|
||||||
|
|
||||||
out(Msg, Result) ->
|
out(Msg, Result) ->
|
||||||
Result#{out => [Msg]}.
|
Result#{out => [Msg]}.
|
||||||
|
|
||||||
|
|
@ -58,13 +62,11 @@ proto_out(Proto, Result) ->
|
||||||
|
|
||||||
reply(Method, Req) when not is_record(Method, coap_message) ->
|
reply(Method, Req) when not is_record(Method, coap_message) ->
|
||||||
reply(Method, <<>>, Req);
|
reply(Method, <<>>, Req);
|
||||||
|
|
||||||
reply(Reply, Result) ->
|
reply(Reply, Result) ->
|
||||||
Result#{reply => Reply}.
|
Result#{reply => Reply}.
|
||||||
|
|
||||||
reply(Method, Req, Result) when is_record(Req, coap_message) ->
|
reply(Method, Req, Result) when is_record(Req, coap_message) ->
|
||||||
reply(Method, <<>>, Req, Result);
|
reply(Method, <<>>, Req, Result);
|
||||||
|
|
||||||
reply(Method, Payload, Req) ->
|
reply(Method, Payload, Req) ->
|
||||||
reply(Method, Payload, Req, #{}).
|
reply(Method, Payload, Req, #{}).
|
||||||
|
|
||||||
|
|
@ -78,16 +80,15 @@ iter([Key, Fun | T], Input, State) ->
|
||||||
iter(T, Input, State);
|
iter(T, Input, State);
|
||||||
Val ->
|
Val ->
|
||||||
Fun(Val, maps:remove(Key, Input), State, T)
|
Fun(Val, maps:remove(Key, Input), State, T)
|
||||||
%% reserved
|
%% reserved
|
||||||
%% if is_function(Fun) ->
|
%% if is_function(Fun) ->
|
||||||
%% Fun(Val, maps:remove(Key, Input), State, T);
|
%% Fun(Val, maps:remove(Key, Input), State, T);
|
||||||
%% true ->
|
%% true ->
|
||||||
%% %% switch to sub branch
|
%% %% switch to sub branch
|
||||||
%% [FunH | FunT] = Fun,
|
%% [FunH | FunT] = Fun,
|
||||||
%% FunH(Val, maps:remove(Key, Input), State, FunT)
|
%% FunH(Val, maps:remove(Key, Input), State, FunT)
|
||||||
%% end
|
%% end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
%% terminal node
|
%% terminal node
|
||||||
iter([Fun], Input, State) ->
|
iter([Fun], Input, State) ->
|
||||||
Fun(undefined, Input, State).
|
Fun(undefined, Input, State).
|
||||||
|
|
@ -100,7 +101,6 @@ iter([Key, Fun | T], Input, Arg, State) ->
|
||||||
Val ->
|
Val ->
|
||||||
Fun(Val, maps:remove(Key, Input), Arg, State, T)
|
Fun(Val, maps:remove(Key, Input), Arg, State, T)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
iter([Fun], Input, Arg, State) ->
|
iter([Fun], Input, Arg, State) ->
|
||||||
Fun(undefined, Input, Arg, State).
|
Fun(undefined, Input, Arg, State).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,15 +24,24 @@
|
||||||
%% convenience functions for message construction
|
%% convenience functions for message construction
|
||||||
-module(emqx_coap_message).
|
-module(emqx_coap_message).
|
||||||
|
|
||||||
-export([ request/2, request/3, request/4
|
-export([
|
||||||
, ack/1, response/1, response/2
|
request/2, request/3, request/4,
|
||||||
, reset/1, piggyback/2, piggyback/3
|
ack/1,
|
||||||
, response/3]).
|
response/1, response/2,
|
||||||
|
reset/1,
|
||||||
|
piggyback/2, piggyback/3,
|
||||||
|
response/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export([is_request/1]).
|
-export([is_request/1]).
|
||||||
|
|
||||||
-export([ set/3, set_payload/2, get_option/2
|
-export([
|
||||||
, get_option/3, set_payload_block/3, set_payload_block/4]).
|
set/3,
|
||||||
|
set_payload/2,
|
||||||
|
get_option/2,
|
||||||
|
get_option/3,
|
||||||
|
set_payload_block/3, set_payload_block/4
|
||||||
|
]).
|
||||||
|
|
||||||
-include("src/coap/include/emqx_coap.hrl").
|
-include("src/coap/include/emqx_coap.hrl").
|
||||||
|
|
||||||
|
|
@ -43,10 +52,12 @@ request(Type, Method, Payload) ->
|
||||||
request(Type, Method, Payload, []).
|
request(Type, Method, Payload, []).
|
||||||
|
|
||||||
request(Type, Method, Payload, Options) when is_binary(Payload) ->
|
request(Type, Method, Payload, Options) when is_binary(Payload) ->
|
||||||
#coap_message{type = Type,
|
#coap_message{
|
||||||
method = Method,
|
type = Type,
|
||||||
payload = Payload,
|
method = Method,
|
||||||
options = to_options(Options)}.
|
payload = Payload,
|
||||||
|
options = to_options(Options)
|
||||||
|
}.
|
||||||
|
|
||||||
ack(#coap_message{id = Id}) ->
|
ack(#coap_message{id = Id}) ->
|
||||||
#coap_message{type = ack, id = Id}.
|
#coap_message{type = ack, id = Id}.
|
||||||
|
|
@ -61,14 +72,18 @@ response(Request) ->
|
||||||
response(Method, Request) ->
|
response(Method, Request) ->
|
||||||
response(Method, <<>>, Request).
|
response(Method, <<>>, Request).
|
||||||
|
|
||||||
response(Method, Payload, #coap_message{type = Type,
|
response(Method, Payload, #coap_message{
|
||||||
id = Id,
|
type = Type,
|
||||||
token = Token}) ->
|
id = Id,
|
||||||
#coap_message{type = Type,
|
token = Token
|
||||||
id = Id,
|
}) ->
|
||||||
token = Token,
|
#coap_message{
|
||||||
method = Method,
|
type = Type,
|
||||||
payload = Payload}.
|
id = Id,
|
||||||
|
token = Token,
|
||||||
|
method = Method,
|
||||||
|
payload = Payload
|
||||||
|
}.
|
||||||
|
|
||||||
%% make a response which maybe is a piggyback ack
|
%% make a response which maybe is a piggyback ack
|
||||||
piggyback(Method, Request) ->
|
piggyback(Method, Request) ->
|
||||||
|
|
@ -84,8 +99,8 @@ piggyback(Method, Payload, Request) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% omit option for its default value
|
%% omit option for its default value
|
||||||
set(max_age, ?DEFAULT_MAX_AGE, Msg) -> Msg;
|
set(max_age, ?DEFAULT_MAX_AGE, Msg) ->
|
||||||
|
Msg;
|
||||||
%% set non-default value
|
%% set non-default value
|
||||||
set(Option, Value, Msg = #coap_message{options = Options}) ->
|
set(Option, Value, Msg = #coap_message{options = Options}) ->
|
||||||
Msg#coap_message{options = Options#{Option => Value}}.
|
Msg#coap_message{options = Options#{Option => Value}}.
|
||||||
|
|
@ -98,13 +113,11 @@ get_option(Option, #coap_message{options = Options}, Def) ->
|
||||||
|
|
||||||
set_payload(Payload, Msg) when is_binary(Payload) ->
|
set_payload(Payload, Msg) when is_binary(Payload) ->
|
||||||
Msg#coap_message{payload = Payload};
|
Msg#coap_message{payload = Payload};
|
||||||
|
|
||||||
set_payload(Payload, Msg) when is_list(Payload) ->
|
set_payload(Payload, Msg) when is_list(Payload) ->
|
||||||
Msg#coap_message{payload = list_to_binary(Payload)}.
|
Msg#coap_message{payload = list_to_binary(Payload)}.
|
||||||
|
|
||||||
set_payload_block(Content, Block, Msg = #coap_message{method = Method}) when is_atom(Method) ->
|
set_payload_block(Content, Block, Msg = #coap_message{method = Method}) when is_atom(Method) ->
|
||||||
set_payload_block(Content, block1, Block, Msg);
|
set_payload_block(Content, block1, Block, Msg);
|
||||||
|
|
||||||
set_payload_block(Content, Block, Msg = #coap_message{}) ->
|
set_payload_block(Content, Block, Msg = #coap_message{}) ->
|
||||||
set_payload_block(Content, block2, Block, Msg).
|
set_payload_block(Content, block2, Block, Msg).
|
||||||
|
|
||||||
|
|
@ -114,16 +127,21 @@ set_payload_block(Content, BlockId, {Num, _, Size}, Msg) ->
|
||||||
OffsetEnd = OffsetBegin + Size,
|
OffsetEnd = OffsetBegin + Size,
|
||||||
case ContentSize > OffsetEnd of
|
case ContentSize > OffsetEnd of
|
||||||
true ->
|
true ->
|
||||||
set(BlockId, {Num, true, Size},
|
set(
|
||||||
set_payload(binary:part(Content, OffsetBegin, Size), Msg));
|
BlockId,
|
||||||
|
{Num, true, Size},
|
||||||
|
set_payload(binary:part(Content, OffsetBegin, Size), Msg)
|
||||||
|
);
|
||||||
_ ->
|
_ ->
|
||||||
set(BlockId, {Num, false, Size},
|
set(
|
||||||
set_payload(binary:part(Content, OffsetBegin, ContentSize - OffsetBegin), Msg))
|
BlockId,
|
||||||
|
{Num, false, Size},
|
||||||
|
set_payload(binary:part(Content, OffsetBegin, ContentSize - OffsetBegin), Msg)
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
is_request(#coap_message{method = Method}) when is_atom(Method) ->
|
is_request(#coap_message{method = Method}) when is_atom(Method) ->
|
||||||
Method =/= undefined;
|
Method =/= undefined;
|
||||||
|
|
||||||
is_request(_) ->
|
is_request(_) ->
|
||||||
false.
|
false.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,18 +17,25 @@
|
||||||
-module(emqx_coap_observe_res).
|
-module(emqx_coap_observe_res).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ new_manager/0, insert/3, remove/2
|
-export([
|
||||||
, res_changed/2, foreach/2, subscriptions/1]).
|
new_manager/0,
|
||||||
|
insert/3,
|
||||||
|
remove/2,
|
||||||
|
res_changed/2,
|
||||||
|
foreach/2,
|
||||||
|
subscriptions/1
|
||||||
|
]).
|
||||||
-export_type([manager/0]).
|
-export_type([manager/0]).
|
||||||
|
|
||||||
-define(MAX_SEQ_ID, 16777215).
|
-define(MAX_SEQ_ID, 16777215).
|
||||||
|
|
||||||
-type token() :: binary().
|
-type token() :: binary().
|
||||||
-type seq_id() :: 0 .. ?MAX_SEQ_ID.
|
-type seq_id() :: 0..?MAX_SEQ_ID.
|
||||||
|
|
||||||
-type res() :: #{ token := token()
|
-type res() :: #{
|
||||||
, seq_id := seq_id()
|
token := token(),
|
||||||
}.
|
seq_id := seq_id()
|
||||||
|
}.
|
||||||
|
|
||||||
-type manager() :: #{emqx_types:topic() => res()}.
|
-type manager() :: #{emqx_types:topic() => res()}.
|
||||||
|
|
||||||
|
|
@ -41,12 +48,13 @@ new_manager() ->
|
||||||
|
|
||||||
-spec insert(emqx_types:topic(), token(), manager()) -> {seq_id(), manager()}.
|
-spec insert(emqx_types:topic(), token(), manager()) -> {seq_id(), manager()}.
|
||||||
insert(Topic, Token, Manager) ->
|
insert(Topic, Token, Manager) ->
|
||||||
Res = case maps:get(Topic, Manager, undefined) of
|
Res =
|
||||||
undefined ->
|
case maps:get(Topic, Manager, undefined) of
|
||||||
new_res(Token);
|
undefined ->
|
||||||
Any ->
|
new_res(Token);
|
||||||
Any
|
Any ->
|
||||||
end,
|
Any
|
||||||
|
end,
|
||||||
{maps:get(seq_id, Res), Manager#{Topic => Res}}.
|
{maps:get(seq_id, Res), Manager#{Topic => Res}}.
|
||||||
|
|
||||||
-spec remove(emqx_types:topic(), manager()) -> manager().
|
-spec remove(emqx_types:topic(), manager()) -> manager().
|
||||||
|
|
@ -59,17 +67,21 @@ res_changed(Topic, Manager) ->
|
||||||
undefined ->
|
undefined ->
|
||||||
undefined;
|
undefined;
|
||||||
Res ->
|
Res ->
|
||||||
#{token := Token,
|
#{
|
||||||
seq_id := SeqId} = Res2 = res_changed(Res),
|
token := Token,
|
||||||
|
seq_id := SeqId
|
||||||
|
} = Res2 = res_changed(Res),
|
||||||
{Token, SeqId, Manager#{Topic := Res2}}
|
{Token, SeqId, Manager#{Topic := Res2}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
foreach(F, Manager) ->
|
foreach(F, Manager) ->
|
||||||
maps:fold(fun(K, V, _) ->
|
maps:fold(
|
||||||
F(K, V)
|
fun(K, V, _) ->
|
||||||
end,
|
F(K, V)
|
||||||
ok,
|
end,
|
||||||
Manager),
|
ok,
|
||||||
|
Manager
|
||||||
|
),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
-spec subscriptions(manager()) -> [emqx_types:topic()].
|
-spec subscriptions(manager()) -> [emqx_types:topic()].
|
||||||
|
|
@ -81,8 +93,10 @@ subscriptions(Manager) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec new_res(token()) -> res().
|
-spec new_res(token()) -> res().
|
||||||
new_res(Token) ->
|
new_res(Token) ->
|
||||||
#{token => Token,
|
#{
|
||||||
seq_id => 0}.
|
token => Token,
|
||||||
|
seq_id => 0
|
||||||
|
}.
|
||||||
|
|
||||||
-spec res_changed(res()) -> res().
|
-spec res_changed(res()) -> res().
|
||||||
res_changed(#{seq_id := SeqId} = Res) ->
|
res_changed(#{seq_id := SeqId} = Res) ->
|
||||||
|
|
|
||||||
|
|
@ -21,50 +21,57 @@
|
||||||
-include("src/coap/include/emqx_coap.hrl").
|
-include("src/coap/include/emqx_coap.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ new/0
|
-export([
|
||||||
, process_subscribe/4
|
new/0,
|
||||||
]).
|
process_subscribe/4
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ info/1
|
-export([
|
||||||
, info/2
|
info/1,
|
||||||
, stats/1
|
info/2,
|
||||||
]).
|
stats/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ handle_request/2
|
-export([
|
||||||
, handle_response/2
|
handle_request/2,
|
||||||
, handle_out/2
|
handle_response/2,
|
||||||
, set_reply/2
|
handle_out/2,
|
||||||
, deliver/3
|
set_reply/2,
|
||||||
, timeout/2]).
|
deliver/3,
|
||||||
|
timeout/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([session/0]).
|
-export_type([session/0]).
|
||||||
|
|
||||||
-record(session, { transport_manager :: emqx_coap_tm:manager()
|
-record(session, {
|
||||||
, observe_manager :: emqx_coap_observe_res:manager()
|
transport_manager :: emqx_coap_tm:manager(),
|
||||||
, created_at :: pos_integer()
|
observe_manager :: emqx_coap_observe_res:manager(),
|
||||||
}).
|
created_at :: pos_integer()
|
||||||
|
}).
|
||||||
|
|
||||||
-type session() :: #session{}.
|
-type session() :: #session{}.
|
||||||
|
|
||||||
%% steal from emqx_session
|
%% steal from emqx_session
|
||||||
-define(INFO_KEYS, [subscriptions,
|
-define(INFO_KEYS, [
|
||||||
upgrade_qos,
|
subscriptions,
|
||||||
retry_interval,
|
upgrade_qos,
|
||||||
await_rel_timeout,
|
retry_interval,
|
||||||
created_at
|
await_rel_timeout,
|
||||||
]).
|
created_at
|
||||||
|
]).
|
||||||
|
|
||||||
-define(STATS_KEYS, [subscriptions_cnt,
|
-define(STATS_KEYS, [
|
||||||
subscriptions_max,
|
subscriptions_cnt,
|
||||||
inflight_cnt,
|
subscriptions_max,
|
||||||
inflight_max,
|
inflight_cnt,
|
||||||
mqueue_len,
|
inflight_max,
|
||||||
mqueue_max,
|
mqueue_len,
|
||||||
mqueue_dropped,
|
mqueue_max,
|
||||||
next_pkt_id,
|
mqueue_dropped,
|
||||||
awaiting_rel_cnt,
|
next_pkt_id,
|
||||||
awaiting_rel_max
|
awaiting_rel_cnt,
|
||||||
]).
|
awaiting_rel_max
|
||||||
|
]).
|
||||||
|
|
||||||
-import(emqx_coap_medium, [iter/3]).
|
-import(emqx_coap_medium, [iter/3]).
|
||||||
-import(emqx_coap_channel, [metrics_inc/2]).
|
-import(emqx_coap_channel, [metrics_inc/2]).
|
||||||
|
|
@ -75,16 +82,18 @@
|
||||||
-spec new() -> session().
|
-spec new() -> session().
|
||||||
new() ->
|
new() ->
|
||||||
_ = emqx_misc:rand_seed(),
|
_ = emqx_misc:rand_seed(),
|
||||||
#session{ transport_manager = emqx_coap_tm:new()
|
#session{
|
||||||
, observe_manager = emqx_coap_observe_res:new_manager()
|
transport_manager = emqx_coap_tm:new(),
|
||||||
, created_at = erlang:system_time(millisecond)}.
|
observe_manager = emqx_coap_observe_res:new_manager(),
|
||||||
|
created_at = erlang:system_time(millisecond)
|
||||||
|
}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Info, Stats
|
%% Info, Stats
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% @doc Compatible with emqx_session
|
%% @doc Compatible with emqx_session
|
||||||
%% do we need use inflight and mqueue in here?
|
%% do we need use inflight and mqueue in here?
|
||||||
-spec(info(session()) -> emqx_types:infos()).
|
-spec info(session()) -> emqx_types:infos().
|
||||||
info(Session) ->
|
info(Session) ->
|
||||||
maps:from_list(info(?INFO_KEYS, Session)).
|
maps:from_list(info(?INFO_KEYS, Session)).
|
||||||
|
|
||||||
|
|
@ -93,8 +102,10 @@ info(Keys, Session) when is_list(Keys) ->
|
||||||
info(subscriptions, #session{observe_manager = OM}) ->
|
info(subscriptions, #session{observe_manager = OM}) ->
|
||||||
Topics = emqx_coap_observe_res:subscriptions(OM),
|
Topics = emqx_coap_observe_res:subscriptions(OM),
|
||||||
lists:foldl(
|
lists:foldl(
|
||||||
fun(T, Acc) -> Acc#{T => emqx_gateway_utils:default_subopts()} end,
|
fun(T, Acc) -> Acc#{T => emqx_gateway_utils:default_subopts()} end,
|
||||||
#{}, Topics);
|
#{},
|
||||||
|
Topics
|
||||||
|
);
|
||||||
info(subscriptions_cnt, #session{observe_manager = OM}) ->
|
info(subscriptions_cnt, #session{observe_manager = OM}) ->
|
||||||
erlang:length(emqx_coap_observe_res:subscriptions(OM));
|
erlang:length(emqx_coap_observe_res:subscriptions(OM));
|
||||||
info(subscriptions_max, _) ->
|
info(subscriptions_max, _) ->
|
||||||
|
|
@ -131,16 +142,18 @@ info(created_at, #session{created_at = CreatedAt}) ->
|
||||||
CreatedAt.
|
CreatedAt.
|
||||||
|
|
||||||
%% @doc Get stats of the session.
|
%% @doc Get stats of the session.
|
||||||
-spec(stats(session()) -> emqx_types:stats()).
|
-spec stats(session()) -> emqx_types:stats().
|
||||||
stats(Session) -> info(?STATS_KEYS, Session).
|
stats(Session) -> info(?STATS_KEYS, Session).
|
||||||
|
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
%%% Process Message
|
%%% Process Message
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
handle_request(Msg, Session) ->
|
handle_request(Msg, Session) ->
|
||||||
call_transport_manager(?FUNCTION_NAME,
|
call_transport_manager(
|
||||||
Msg,
|
?FUNCTION_NAME,
|
||||||
Session).
|
Msg,
|
||||||
|
Session
|
||||||
|
).
|
||||||
|
|
||||||
handle_response(Msg, Session) ->
|
handle_response(Msg, Session) ->
|
||||||
call_transport_manager(?FUNCTION_NAME, Msg, Session).
|
call_transport_manager(?FUNCTION_NAME, Msg, Session).
|
||||||
|
|
@ -152,26 +165,36 @@ set_reply(Msg, #session{transport_manager = TM} = Session) ->
|
||||||
TM2 = emqx_coap_tm:set_reply(Msg, TM),
|
TM2 = emqx_coap_tm:set_reply(Msg, TM),
|
||||||
Session#session{transport_manager = TM2}.
|
Session#session{transport_manager = TM2}.
|
||||||
|
|
||||||
deliver(Delivers, Ctx, #session{observe_manager = OM,
|
deliver(
|
||||||
transport_manager = TM} = Session) ->
|
Delivers,
|
||||||
|
Ctx,
|
||||||
|
#session{
|
||||||
|
observe_manager = OM,
|
||||||
|
transport_manager = TM
|
||||||
|
} = Session
|
||||||
|
) ->
|
||||||
Fun = fun({_, Topic, Message}, {OutAcc, OMAcc, TMAcc} = Acc) ->
|
Fun = fun({_, Topic, Message}, {OutAcc, OMAcc, TMAcc} = Acc) ->
|
||||||
case emqx_coap_observe_res:res_changed(Topic, OMAcc) of
|
case emqx_coap_observe_res:res_changed(Topic, OMAcc) of
|
||||||
undefined ->
|
undefined ->
|
||||||
metrics_inc('delivery.dropped', Ctx),
|
metrics_inc('delivery.dropped', Ctx),
|
||||||
metrics_inc('delivery.dropped.no_subid', Ctx),
|
metrics_inc('delivery.dropped.no_subid', Ctx),
|
||||||
Acc;
|
Acc;
|
||||||
{Token, SeqId, OM2} ->
|
{Token, SeqId, OM2} ->
|
||||||
metrics_inc('messages.delivered', Ctx),
|
metrics_inc('messages.delivered', Ctx),
|
||||||
Msg = mqtt_to_coap(Message, Token, SeqId),
|
Msg = mqtt_to_coap(Message, Token, SeqId),
|
||||||
#{out := Out, tm := TM2} = emqx_coap_tm:handle_out(Msg, TMAcc),
|
#{out := Out, tm := TM2} = emqx_coap_tm:handle_out(Msg, TMAcc),
|
||||||
{Out ++ OutAcc, OM2, TM2}
|
{Out ++ OutAcc, OM2, TM2}
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
{Outs, OM2, TM2} = lists:foldl(Fun, {[], OM, TM}, lists:reverse(Delivers)),
|
{Outs, OM2, TM2} = lists:foldl(Fun, {[], OM, TM}, lists:reverse(Delivers)),
|
||||||
|
|
||||||
#{out => lists:reverse(Outs),
|
#{
|
||||||
session => Session#session{observe_manager = OM2,
|
out => lists:reverse(Outs),
|
||||||
transport_manager = TM2}}.
|
session => Session#session{
|
||||||
|
observe_manager = OM2,
|
||||||
|
transport_manager = TM2
|
||||||
|
}
|
||||||
|
}.
|
||||||
|
|
||||||
timeout(Timer, Session) ->
|
timeout(Timer, Session) ->
|
||||||
call_transport_manager(?FUNCTION_NAME, Timer, Session).
|
call_transport_manager(?FUNCTION_NAME, Timer, Session).
|
||||||
|
|
@ -179,13 +202,17 @@ timeout(Timer, Session) ->
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
%%% Internal functions
|
%%% Internal functions
|
||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
call_transport_manager(Fun,
|
call_transport_manager(
|
||||||
Msg,
|
Fun,
|
||||||
#session{transport_manager = TM} = Session) ->
|
Msg,
|
||||||
|
#session{transport_manager = TM} = Session
|
||||||
|
) ->
|
||||||
Result = emqx_coap_tm:Fun(Msg, TM),
|
Result = emqx_coap_tm:Fun(Msg, TM),
|
||||||
iter([tm, fun process_tm/4, fun process_session/3],
|
iter(
|
||||||
Result,
|
[tm, fun process_tm/4, fun process_session/3],
|
||||||
Session).
|
Result,
|
||||||
|
Session
|
||||||
|
).
|
||||||
|
|
||||||
process_tm(TM, Result, Session, Cursor) ->
|
process_tm(TM, Result, Session, Cursor) ->
|
||||||
iter(Cursor, Result, Session#session{transport_manager = TM}).
|
iter(Cursor, Result, Session#session{transport_manager = TM}).
|
||||||
|
|
@ -193,8 +220,12 @@ process_tm(TM, Result, Session, Cursor) ->
|
||||||
process_session(_, Result, Session) ->
|
process_session(_, Result, Session) ->
|
||||||
Result#{session => Session}.
|
Result#{session => Session}.
|
||||||
|
|
||||||
process_subscribe(Sub, Msg, Result,
|
process_subscribe(
|
||||||
#session{observe_manager = OM} = Session) ->
|
Sub,
|
||||||
|
Msg,
|
||||||
|
Result,
|
||||||
|
#session{observe_manager = OM} = Session
|
||||||
|
) ->
|
||||||
case Sub of
|
case Sub of
|
||||||
undefined ->
|
undefined ->
|
||||||
Result;
|
Result;
|
||||||
|
|
@ -202,22 +233,28 @@ process_subscribe(Sub, Msg, Result,
|
||||||
{SeqId, OM2} = emqx_coap_observe_res:insert(Topic, Token, OM),
|
{SeqId, OM2} = emqx_coap_observe_res:insert(Topic, Token, OM),
|
||||||
Replay = emqx_coap_message:piggyback({ok, content}, Msg),
|
Replay = emqx_coap_message:piggyback({ok, content}, Msg),
|
||||||
Replay2 = Replay#coap_message{options = #{observe => SeqId}},
|
Replay2 = Replay#coap_message{options = #{observe => SeqId}},
|
||||||
Result#{reply => Replay2,
|
Result#{
|
||||||
session => Session#session{observe_manager = OM2}};
|
reply => Replay2,
|
||||||
|
session => Session#session{observe_manager = OM2}
|
||||||
|
};
|
||||||
Topic ->
|
Topic ->
|
||||||
OM2 = emqx_coap_observe_res:remove(Topic, OM),
|
OM2 = emqx_coap_observe_res:remove(Topic, OM),
|
||||||
Replay = emqx_coap_message:piggyback({ok, nocontent}, Msg),
|
Replay = emqx_coap_message:piggyback({ok, nocontent}, Msg),
|
||||||
Result#{reply => Replay,
|
Result#{
|
||||||
session => Session#session{observe_manager = OM2}}
|
reply => Replay,
|
||||||
|
session => Session#session{observe_manager = OM2}
|
||||||
|
}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
mqtt_to_coap(MQTT, Token, SeqId) ->
|
mqtt_to_coap(MQTT, Token, SeqId) ->
|
||||||
#message{payload = Payload} = MQTT,
|
#message{payload = Payload} = MQTT,
|
||||||
#coap_message{type = get_notify_type(MQTT),
|
#coap_message{
|
||||||
method = {ok, content},
|
type = get_notify_type(MQTT),
|
||||||
token = Token,
|
method = {ok, content},
|
||||||
payload = Payload,
|
token = Token,
|
||||||
options = #{observe => SeqId}}.
|
payload = Payload,
|
||||||
|
options = #{observe => SeqId}
|
||||||
|
}.
|
||||||
|
|
||||||
get_notify_type(#message{qos = Qos}) ->
|
get_notify_type(#message{qos = Qos}) ->
|
||||||
case emqx_conf:get([gateway, coap, notify_qos], non) of
|
case emqx_conf:get([gateway, coap, notify_qos], non) of
|
||||||
|
|
|
||||||
|
|
@ -17,13 +17,15 @@
|
||||||
%% the transport state machine manager
|
%% the transport state machine manager
|
||||||
-module(emqx_coap_tm).
|
-module(emqx_coap_tm).
|
||||||
|
|
||||||
-export([ new/0
|
-export([
|
||||||
, handle_request/2
|
new/0,
|
||||||
, handle_response/2
|
handle_request/2,
|
||||||
, handle_out/2
|
handle_response/2,
|
||||||
, handle_out/3
|
handle_out/2,
|
||||||
, set_reply/2
|
handle_out/3,
|
||||||
, timeout/2]).
|
set_reply/2,
|
||||||
|
timeout/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([manager/0, event_result/1]).
|
-export_type([manager/0, event_result/1]).
|
||||||
|
|
||||||
|
|
@ -32,41 +34,47 @@
|
||||||
|
|
||||||
-type direction() :: in | out.
|
-type direction() :: in | out.
|
||||||
|
|
||||||
-record(state_machine, { seq_id :: seq_id()
|
-record(state_machine, {
|
||||||
, id :: state_machine_key()
|
seq_id :: seq_id(),
|
||||||
, token :: token() | undefined
|
id :: state_machine_key(),
|
||||||
, observe :: 0 | 1 | undefined | observed
|
token :: token() | undefined,
|
||||||
, state :: atom()
|
observe :: 0 | 1 | undefined | observed,
|
||||||
, timers :: maps:map()
|
state :: atom(),
|
||||||
, transport :: emqx_coap_transport:transport()}).
|
timers :: maps:map(),
|
||||||
|
transport :: emqx_coap_transport:transport()
|
||||||
|
}).
|
||||||
-type state_machine() :: #state_machine{}.
|
-type state_machine() :: #state_machine{}.
|
||||||
|
|
||||||
-type message_id() :: 0 .. ?MAX_MESSAGE_ID.
|
-type message_id() :: 0..?MAX_MESSAGE_ID.
|
||||||
-type token_key() :: {token, token()}.
|
-type token_key() :: {token, token()}.
|
||||||
-type state_machine_key() :: {direction(), message_id()}.
|
-type state_machine_key() :: {direction(), message_id()}.
|
||||||
-type seq_id() :: pos_integer().
|
-type seq_id() :: pos_integer().
|
||||||
-type manager_key() :: token_key() | state_machine_key() | seq_id().
|
-type manager_key() :: token_key() | state_machine_key() | seq_id().
|
||||||
|
|
||||||
-type manager() :: #{ seq_id => seq_id()
|
-type manager() :: #{
|
||||||
, next_msg_id => coap_message_id()
|
seq_id => seq_id(),
|
||||||
, token_key() => seq_id()
|
next_msg_id => coap_message_id(),
|
||||||
, state_machine_key() => seq_id()
|
token_key() => seq_id(),
|
||||||
, seq_id() => state_machine()
|
state_machine_key() => seq_id(),
|
||||||
}.
|
seq_id() => state_machine()
|
||||||
|
}.
|
||||||
|
|
||||||
-type ttimeout() :: {state_timeout, pos_integer(), any()}
|
-type ttimeout() ::
|
||||||
| {stop_timeout, pos_integer()}.
|
{state_timeout, pos_integer(), any()}
|
||||||
|
| {stop_timeout, pos_integer()}.
|
||||||
|
|
||||||
-type topic() :: binary().
|
-type topic() :: binary().
|
||||||
-type token() :: binary().
|
-type token() :: binary().
|
||||||
-type sub_register() :: {topic(), token()} | topic().
|
-type sub_register() :: {topic(), token()} | topic().
|
||||||
|
|
||||||
-type event_result(State) ::
|
-type event_result(State) ::
|
||||||
#{next => State,
|
#{
|
||||||
outgoing => coap_message(),
|
next => State,
|
||||||
timeouts => list(ttimeout()),
|
outgoing => coap_message(),
|
||||||
has_sub => undefined | sub_register(),
|
timeouts => list(ttimeout()),
|
||||||
transport => emqx_coap_transport:transport()}.
|
has_sub => undefined | sub_register(),
|
||||||
|
transport => emqx_coap_transport:transport()
|
||||||
|
}.
|
||||||
|
|
||||||
-define(TOKEN_ID(T), {token, T}).
|
-define(TOKEN_ID(T), {token, T}).
|
||||||
|
|
||||||
|
|
@ -78,9 +86,10 @@
|
||||||
|
|
||||||
-spec new() -> manager().
|
-spec new() -> manager().
|
||||||
new() ->
|
new() ->
|
||||||
#{ seq_id => 1
|
#{
|
||||||
, next_msg_id => rand:uniform(?MAX_MESSAGE_ID)
|
seq_id => 1,
|
||||||
}.
|
next_msg_id => rand:uniform(?MAX_MESSAGE_ID)
|
||||||
|
}.
|
||||||
|
|
||||||
handle_request(#coap_message{id = MsgId} = Msg, TM) ->
|
handle_request(#coap_message{id = MsgId} = Msg, TM) ->
|
||||||
Id = {in, MsgId},
|
Id = {in, MsgId},
|
||||||
|
|
@ -111,7 +120,6 @@ handle_response(#coap_message{type = Type, id = MsgId, token = Token} = Msg, TM)
|
||||||
%% send to a client, msg can be request/piggyback/separate/notify
|
%% send to a client, msg can be request/piggyback/separate/notify
|
||||||
handle_out({Ctx, Msg}, TM) ->
|
handle_out({Ctx, Msg}, TM) ->
|
||||||
handle_out(Msg, Ctx, TM);
|
handle_out(Msg, Ctx, TM);
|
||||||
|
|
||||||
handle_out(Msg, TM) ->
|
handle_out(Msg, TM) ->
|
||||||
handle_out(Msg, undefined, TM).
|
handle_out(Msg, undefined, TM).
|
||||||
|
|
||||||
|
|
@ -135,8 +143,10 @@ set_reply(#coap_message{id = MsgId} = Msg, TM) ->
|
||||||
case find_machine(Id, TM) of
|
case find_machine(Id, TM) of
|
||||||
undefined ->
|
undefined ->
|
||||||
TM;
|
TM;
|
||||||
#state_machine{transport = Transport,
|
#state_machine{
|
||||||
seq_id = SeqId} = Machine ->
|
transport = Transport,
|
||||||
|
seq_id = SeqId
|
||||||
|
} = Machine ->
|
||||||
Transport2 = emqx_coap_transport:set_cache(Msg, Transport),
|
Transport2 = emqx_coap_transport:set_cache(Msg, Transport),
|
||||||
Machine2 = Machine#state_machine{transport = Transport2},
|
Machine2 = Machine#state_machine{transport = Transport2},
|
||||||
TM#{SeqId => Machine2}
|
TM#{SeqId => Machine2}
|
||||||
|
|
@ -161,35 +171,52 @@ timeout({SeqId, Type, Msg}, TM) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
process_event(stop_timeout, _, TM, Machine) ->
|
process_event(stop_timeout, _, TM, Machine) ->
|
||||||
process_manager(stop, #{}, Machine, TM);
|
process_manager(stop, #{}, Machine, TM);
|
||||||
|
process_event(
|
||||||
process_event(Event, Msg, TM, #state_machine{state = State,
|
Event,
|
||||||
transport = Transport} = Machine) ->
|
Msg,
|
||||||
|
TM,
|
||||||
|
#state_machine{
|
||||||
|
state = State,
|
||||||
|
transport = Transport
|
||||||
|
} = Machine
|
||||||
|
) ->
|
||||||
Result = emqx_coap_transport:State(Event, Msg, Transport),
|
Result = emqx_coap_transport:State(Event, Msg, Transport),
|
||||||
iter([ proto, fun process_observe_response/5
|
iter(
|
||||||
, next, fun process_state_change/5
|
[
|
||||||
, transport, fun process_transport_change/5
|
proto,
|
||||||
, timeouts, fun process_timeouts/5
|
fun process_observe_response/5,
|
||||||
, fun process_manager/4],
|
next,
|
||||||
Result,
|
fun process_state_change/5,
|
||||||
Machine,
|
transport,
|
||||||
TM).
|
fun process_transport_change/5,
|
||||||
|
timeouts,
|
||||||
|
fun process_timeouts/5,
|
||||||
|
fun process_manager/4
|
||||||
|
],
|
||||||
|
Result,
|
||||||
|
Machine,
|
||||||
|
TM
|
||||||
|
).
|
||||||
|
|
||||||
process_observe_response({response, {_, Msg}} = Response,
|
process_observe_response(
|
||||||
Result,
|
{response, {_, Msg}} = Response,
|
||||||
#state_machine{observe = 0} = Machine,
|
Result,
|
||||||
TM,
|
#state_machine{observe = 0} = Machine,
|
||||||
Iter) ->
|
TM,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
Result2 = proto_out(Response, Result),
|
Result2 = proto_out(Response, Result),
|
||||||
case Msg#coap_message.method of
|
case Msg#coap_message.method of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
iter(Iter,
|
iter(
|
||||||
Result2#{next => observe},
|
Iter,
|
||||||
Machine#state_machine{observe = observed},
|
Result2#{next => observe},
|
||||||
TM);
|
Machine#state_machine{observe = observed},
|
||||||
|
TM
|
||||||
|
);
|
||||||
_ ->
|
_ ->
|
||||||
iter(Iter, Result2, Machine, TM)
|
iter(Iter, Result2, Machine, TM)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
process_observe_response(Proto, Result, Machine, TM, Iter) ->
|
process_observe_response(Proto, Result, Machine, TM, Iter) ->
|
||||||
iter(Iter, proto_out(Proto, Result), Machine, TM).
|
iter(Iter, proto_out(Proto, Result), Machine, TM).
|
||||||
|
|
||||||
|
|
@ -198,10 +225,12 @@ process_state_change(Next, Result, Machine, TM, Iter) ->
|
||||||
stop ->
|
stop ->
|
||||||
process_manager(stop, Result, Machine, TM);
|
process_manager(stop, Result, Machine, TM);
|
||||||
_ ->
|
_ ->
|
||||||
iter(Iter,
|
iter(
|
||||||
Result,
|
Iter,
|
||||||
cancel_state_timer(Machine#state_machine{state = Next}),
|
Result,
|
||||||
TM)
|
cancel_state_timer(Machine#state_machine{state = Next}),
|
||||||
|
TM
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
process_transport_change(Transport, Result, Machine, TM, Iter) ->
|
process_transport_change(Transport, Result, Machine, TM, Iter) ->
|
||||||
|
|
@ -209,22 +238,30 @@ process_transport_change(Transport, Result, Machine, TM, Iter) ->
|
||||||
|
|
||||||
process_timeouts([], Result, Machine, TM, Iter) ->
|
process_timeouts([], Result, Machine, TM, Iter) ->
|
||||||
iter(Iter, Result, Machine, TM);
|
iter(Iter, Result, Machine, TM);
|
||||||
|
process_timeouts(
|
||||||
process_timeouts(Timeouts, Result,
|
Timeouts,
|
||||||
#state_machine{seq_id = SeqId,
|
Result,
|
||||||
timers = Timers} = Machine, TM, Iter) ->
|
#state_machine{
|
||||||
NewTimers = lists:foldl(fun({state_timeout, _, _} = Timer, Acc) ->
|
seq_id = SeqId,
|
||||||
process_timer(SeqId, Timer, Acc);
|
timers = Timers
|
||||||
({stop_timeout, I}, Acc) ->
|
} = Machine,
|
||||||
process_timer(SeqId, {stop_timeout, I, stop}, Acc)
|
TM,
|
||||||
end,
|
Iter
|
||||||
Timers,
|
) ->
|
||||||
Timeouts),
|
NewTimers = lists:foldl(
|
||||||
|
fun
|
||||||
|
({state_timeout, _, _} = Timer, Acc) ->
|
||||||
|
process_timer(SeqId, Timer, Acc);
|
||||||
|
({stop_timeout, I}, Acc) ->
|
||||||
|
process_timer(SeqId, {stop_timeout, I, stop}, Acc)
|
||||||
|
end,
|
||||||
|
Timers,
|
||||||
|
Timeouts
|
||||||
|
),
|
||||||
iter(Iter, Result, Machine#state_machine{timers = NewTimers}, TM).
|
iter(Iter, Result, Machine#state_machine{timers = NewTimers}, TM).
|
||||||
|
|
||||||
process_manager(stop, Result, #state_machine{seq_id = SeqId}, TM) ->
|
process_manager(stop, Result, #state_machine{seq_id = SeqId}, TM) ->
|
||||||
Result#{tm => delete_machine(SeqId, TM)};
|
Result#{tm => delete_machine(SeqId, TM)};
|
||||||
|
|
||||||
process_manager(_, Result, #state_machine{seq_id = SeqId} = Machine2, TM) ->
|
process_manager(_, Result, #state_machine{seq_id = SeqId} = Machine2, TM) ->
|
||||||
Result#{tm => TM#{SeqId => Machine2}}.
|
Result#{tm => TM#{SeqId => Machine2}}.
|
||||||
|
|
||||||
|
|
@ -246,14 +283,18 @@ delete_machine(Id, Manager) ->
|
||||||
case find_machine(Id, Manager) of
|
case find_machine(Id, Manager) of
|
||||||
undefined ->
|
undefined ->
|
||||||
Manager;
|
Manager;
|
||||||
#state_machine{seq_id = SeqId,
|
#state_machine{
|
||||||
id = MachineId,
|
seq_id = SeqId,
|
||||||
token = Token,
|
id = MachineId,
|
||||||
timers = Timers} ->
|
token = Token,
|
||||||
lists:foreach(fun({_, Ref}) ->
|
timers = Timers
|
||||||
emqx_misc:cancel_timer(Ref)
|
} ->
|
||||||
end,
|
lists:foreach(
|
||||||
maps:to_list(Timers)),
|
fun({_, Ref}) ->
|
||||||
|
emqx_misc:cancel_timer(Ref)
|
||||||
|
end,
|
||||||
|
maps:to_list(Timers)
|
||||||
|
),
|
||||||
maps:without([SeqId, MachineId, ?TOKEN_ID(Token)], Manager)
|
maps:without([SeqId, MachineId, ?TOKEN_ID(Token)], Manager)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -264,12 +305,14 @@ find_machine(SeqId, Manager) ->
|
||||||
find_machine_by_seqid(SeqId, Manager).
|
find_machine_by_seqid(SeqId, Manager).
|
||||||
|
|
||||||
-spec find_machine_by_seqid(seq_id() | undefined, manager()) ->
|
-spec find_machine_by_seqid(seq_id() | undefined, manager()) ->
|
||||||
state_machine() | undefined.
|
state_machine() | undefined.
|
||||||
find_machine_by_seqid(SeqId, Manager) ->
|
find_machine_by_seqid(SeqId, Manager) ->
|
||||||
maps:get(SeqId, Manager, undefined).
|
maps:get(SeqId, Manager, undefined).
|
||||||
|
|
||||||
-spec find_machine_by_keys(list(manager_key()),
|
-spec find_machine_by_keys(
|
||||||
manager()) -> state_machine() | undefined.
|
list(manager_key()),
|
||||||
|
manager()
|
||||||
|
) -> state_machine() | undefined.
|
||||||
find_machine_by_keys([H | T], Manager) ->
|
find_machine_by_keys([H | T], Manager) ->
|
||||||
case H of
|
case H of
|
||||||
?TOKEN_ID(<<>>) ->
|
?TOKEN_ID(<<>>) ->
|
||||||
|
|
@ -286,53 +329,63 @@ find_machine_by_keys(_, _) ->
|
||||||
undefined.
|
undefined.
|
||||||
|
|
||||||
-spec new_in_machine(state_machine_key(), manager()) ->
|
-spec new_in_machine(state_machine_key(), manager()) ->
|
||||||
{state_machine(), manager()}.
|
{state_machine(), manager()}.
|
||||||
new_in_machine(MachineId, #{seq_id := SeqId} = Manager) ->
|
new_in_machine(MachineId, #{seq_id := SeqId} = Manager) ->
|
||||||
Machine = #state_machine{ seq_id = SeqId
|
Machine = #state_machine{
|
||||||
, id = MachineId
|
seq_id = SeqId,
|
||||||
, state = idle
|
id = MachineId,
|
||||||
, timers = #{}
|
state = idle,
|
||||||
, transport = emqx_coap_transport:new()},
|
timers = #{},
|
||||||
{Machine, Manager#{seq_id := SeqId + 1,
|
transport = emqx_coap_transport:new()
|
||||||
SeqId => Machine,
|
},
|
||||||
MachineId => SeqId}}.
|
{Machine, Manager#{
|
||||||
|
seq_id := SeqId + 1,
|
||||||
|
SeqId => Machine,
|
||||||
|
MachineId => SeqId
|
||||||
|
}}.
|
||||||
|
|
||||||
-spec new_out_machine(state_machine_key(), any(), coap_message(), manager()) ->
|
-spec new_out_machine(state_machine_key(), any(), coap_message(), manager()) ->
|
||||||
{state_machine(), manager()}.
|
{state_machine(), manager()}.
|
||||||
new_out_machine(MachineId,
|
new_out_machine(
|
||||||
Ctx,
|
MachineId,
|
||||||
#coap_message{type = Type, token = Token, options = Opts},
|
Ctx,
|
||||||
#{seq_id := SeqId} = Manager) ->
|
#coap_message{type = Type, token = Token, options = Opts},
|
||||||
|
#{seq_id := SeqId} = Manager
|
||||||
|
) ->
|
||||||
Observe = maps:get(observe, Opts, undefined),
|
Observe = maps:get(observe, Opts, undefined),
|
||||||
Machine = #state_machine{ seq_id = SeqId
|
Machine = #state_machine{
|
||||||
, id = MachineId
|
seq_id = SeqId,
|
||||||
, token = Token
|
id = MachineId,
|
||||||
, observe = Observe
|
token = Token,
|
||||||
, state = idle
|
observe = Observe,
|
||||||
, timers = #{}
|
state = idle,
|
||||||
, transport = emqx_coap_transport:new(Ctx)},
|
timers = #{},
|
||||||
|
transport = emqx_coap_transport:new(Ctx)
|
||||||
|
},
|
||||||
|
|
||||||
Manager2 = Manager#{seq_id := SeqId + 1,
|
Manager2 = Manager#{
|
||||||
SeqId => Machine,
|
seq_id := SeqId + 1,
|
||||||
MachineId => SeqId},
|
SeqId => Machine,
|
||||||
|
MachineId => SeqId
|
||||||
|
},
|
||||||
{Machine,
|
{Machine,
|
||||||
if Token =:= <<>> ->
|
if
|
||||||
Manager2;
|
Token =:= <<>> ->
|
||||||
Observe =:= 1 ->
|
Manager2;
|
||||||
TokenId = ?TOKEN_ID(Token),
|
Observe =:= 1 ->
|
||||||
delete_machine(TokenId, Manager2);
|
TokenId = ?TOKEN_ID(Token),
|
||||||
Type =:= con orelse Observe =:= 0 ->
|
delete_machine(TokenId, Manager2);
|
||||||
TokenId = ?TOKEN_ID(Token),
|
Type =:= con orelse Observe =:= 0 ->
|
||||||
case maps:get(TokenId, Manager, undefined) of
|
TokenId = ?TOKEN_ID(Token),
|
||||||
undefined ->
|
case maps:get(TokenId, Manager, undefined) of
|
||||||
Manager2#{TokenId => SeqId};
|
undefined ->
|
||||||
_ ->
|
Manager2#{TokenId => SeqId};
|
||||||
throw("token conflict")
|
_ ->
|
||||||
end;
|
throw("token conflict")
|
||||||
true ->
|
end;
|
||||||
Manager2
|
true ->
|
||||||
end
|
Manager2
|
||||||
}.
|
end}.
|
||||||
|
|
||||||
alloc_message_id(#{next_msg_id := MsgId} = TM) ->
|
alloc_message_id(#{next_msg_id := MsgId} = TM) ->
|
||||||
alloc_message_id(MsgId, TM).
|
alloc_message_id(MsgId, TM).
|
||||||
|
|
@ -348,8 +401,9 @@ alloc_message_id(MsgId, TM) ->
|
||||||
|
|
||||||
next_message_id(MsgId) ->
|
next_message_id(MsgId) ->
|
||||||
Next = MsgId + 1,
|
Next = MsgId + 1,
|
||||||
if Next >= ?MAX_MESSAGE_ID ->
|
if
|
||||||
|
Next >= ?MAX_MESSAGE_ID ->
|
||||||
1;
|
1;
|
||||||
true ->
|
true ->
|
||||||
Next
|
Next
|
||||||
end.
|
end.
|
||||||
|
|
|
||||||
|
|
@ -11,71 +11,102 @@
|
||||||
|
|
||||||
-type request_context() :: any().
|
-type request_context() :: any().
|
||||||
|
|
||||||
-record(transport, { cache :: undefined | coap_message()
|
-record(transport, {
|
||||||
, req_context :: request_context()
|
cache :: undefined | coap_message(),
|
||||||
, retry_interval :: non_neg_integer()
|
req_context :: request_context(),
|
||||||
, retry_count :: non_neg_integer()
|
retry_interval :: non_neg_integer(),
|
||||||
, observe :: non_neg_integer() | undefined
|
retry_count :: non_neg_integer(),
|
||||||
}).
|
observe :: non_neg_integer() | undefined
|
||||||
|
}).
|
||||||
|
|
||||||
-type transport() :: #transport{}.
|
-type transport() :: #transport{}.
|
||||||
|
|
||||||
-export([ new/0, new/1, idle/3, maybe_reset/3, set_cache/2
|
-export([
|
||||||
, maybe_resend_4request/3, wait_ack/3, until_stop/3
|
new/0, new/1,
|
||||||
, observe/3, maybe_resend_4response/3]).
|
idle/3,
|
||||||
|
maybe_reset/3,
|
||||||
|
set_cache/2,
|
||||||
|
maybe_resend_4request/3,
|
||||||
|
wait_ack/3,
|
||||||
|
until_stop/3,
|
||||||
|
observe/3,
|
||||||
|
maybe_resend_4response/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([transport/0]).
|
-export_type([transport/0]).
|
||||||
|
|
||||||
-import(emqx_coap_medium, [ empty/0, reset/2, proto_out/2
|
-import(emqx_coap_medium, [
|
||||||
, out/1, out/2, proto_out/1
|
empty/0,
|
||||||
, reply/2]).
|
reset/2,
|
||||||
|
proto_out/2,
|
||||||
|
out/1, out/2,
|
||||||
|
proto_out/1,
|
||||||
|
reply/2
|
||||||
|
]).
|
||||||
|
|
||||||
-spec new() -> transport().
|
-spec new() -> transport().
|
||||||
new() ->
|
new() ->
|
||||||
new(undefined).
|
new(undefined).
|
||||||
|
|
||||||
new(ReqCtx) ->
|
new(ReqCtx) ->
|
||||||
#transport{cache = undefined,
|
#transport{
|
||||||
retry_interval = 0,
|
cache = undefined,
|
||||||
retry_count = 0,
|
retry_interval = 0,
|
||||||
req_context = ReqCtx}.
|
retry_count = 0,
|
||||||
|
req_context = ReqCtx
|
||||||
|
}.
|
||||||
|
|
||||||
idle(in,
|
idle(
|
||||||
#coap_message{type = non, method = Method} = Msg,
|
in,
|
||||||
_) ->
|
#coap_message{type = non, method = Method} = Msg,
|
||||||
|
_
|
||||||
|
) ->
|
||||||
case Method of
|
case Method of
|
||||||
undefined ->
|
undefined ->
|
||||||
reset(Msg, #{next => stop});
|
reset(Msg, #{next => stop});
|
||||||
_ ->
|
_ ->
|
||||||
proto_out({request, Msg},
|
proto_out(
|
||||||
#{next => until_stop,
|
{request, Msg},
|
||||||
timeouts =>
|
#{
|
||||||
[{stop_timeout, ?NON_LIFETIME}]})
|
next => until_stop,
|
||||||
|
timeouts =>
|
||||||
|
[{stop_timeout, ?NON_LIFETIME}]
|
||||||
|
}
|
||||||
|
)
|
||||||
end;
|
end;
|
||||||
|
idle(
|
||||||
idle(in,
|
in,
|
||||||
#coap_message{type = con, method = Method} = Msg,
|
#coap_message{type = con, method = Method} = Msg,
|
||||||
_) ->
|
_
|
||||||
|
) ->
|
||||||
case Method of
|
case Method of
|
||||||
undefined ->
|
undefined ->
|
||||||
reset(Msg, #{next => stop});
|
reset(Msg, #{next => stop});
|
||||||
_ ->
|
_ ->
|
||||||
proto_out({request, Msg},
|
proto_out(
|
||||||
#{next => maybe_resend_4request,
|
{request, Msg},
|
||||||
timeouts =>[{stop_timeout, ?EXCHANGE_LIFETIME}]})
|
#{
|
||||||
|
next => maybe_resend_4request,
|
||||||
|
timeouts => [{stop_timeout, ?EXCHANGE_LIFETIME}]
|
||||||
|
}
|
||||||
|
)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
idle(out, #coap_message{type = non} = Msg, _) ->
|
idle(out, #coap_message{type = non} = Msg, _) ->
|
||||||
out(Msg, #{next => maybe_reset,
|
out(Msg, #{
|
||||||
timeouts => [{stop_timeout, ?NON_LIFETIME}]});
|
next => maybe_reset,
|
||||||
|
timeouts => [{stop_timeout, ?NON_LIFETIME}]
|
||||||
|
});
|
||||||
idle(out, Msg, Transport) ->
|
idle(out, Msg, Transport) ->
|
||||||
_ = emqx_misc:rand_seed(),
|
_ = emqx_misc:rand_seed(),
|
||||||
Timeout = ?ACK_TIMEOUT + rand:uniform(?ACK_RANDOM_FACTOR),
|
Timeout = ?ACK_TIMEOUT + rand:uniform(?ACK_RANDOM_FACTOR),
|
||||||
out(Msg, #{next => wait_ack,
|
out(Msg, #{
|
||||||
transport => Transport#transport{cache = Msg},
|
next => wait_ack,
|
||||||
timeouts => [ {state_timeout, Timeout, ack_timeout}
|
transport => Transport#transport{cache = Msg},
|
||||||
, {stop_timeout, ?EXCHANGE_LIFETIME}]}).
|
timeouts => [
|
||||||
|
{state_timeout, Timeout, ack_timeout},
|
||||||
|
{stop_timeout, ?EXCHANGE_LIFETIME}
|
||||||
|
]
|
||||||
|
}).
|
||||||
|
|
||||||
maybe_resend_4request(in, Msg, Transport) ->
|
maybe_resend_4request(in, Msg, Transport) ->
|
||||||
maybe_resend(Msg, true, Transport).
|
maybe_resend(Msg, true, Transport).
|
||||||
|
|
@ -98,19 +129,26 @@ maybe_resend(Msg, IsExpecteReq, #transport{cache = Cache}) ->
|
||||||
reset(Msg, #{next => stop})
|
reset(Msg, #{next => stop})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
maybe_reset(in, #coap_message{type = Type, method = Method} = Message,
|
maybe_reset(
|
||||||
#transport{req_context = Ctx} = Transport) ->
|
in,
|
||||||
|
#coap_message{type = Type, method = Method} = Message,
|
||||||
|
#transport{req_context = Ctx} = Transport
|
||||||
|
) ->
|
||||||
Ret = #{next => stop},
|
Ret = #{next => stop},
|
||||||
CtxMsg = {Ctx, Message},
|
CtxMsg = {Ctx, Message},
|
||||||
if Type =:= reset ->
|
if
|
||||||
|
Type =:= reset ->
|
||||||
proto_out({reset, CtxMsg}, Ret);
|
proto_out({reset, CtxMsg}, Ret);
|
||||||
is_tuple(Method) ->
|
is_tuple(Method) ->
|
||||||
on_response(Message,
|
on_response(
|
||||||
Transport,
|
Message,
|
||||||
if Type =:= non -> until_stop;
|
Transport,
|
||||||
true -> maybe_resend_4response
|
if
|
||||||
end);
|
Type =:= non -> until_stop;
|
||||||
true ->
|
true -> maybe_resend_4response
|
||||||
|
end
|
||||||
|
);
|
||||||
|
true ->
|
||||||
reset(Message, Ret)
|
reset(Message, Ret)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -131,26 +169,37 @@ wait_ack(in, #coap_message{type = Type, method = Method} = Msg, #transport{req_c
|
||||||
reset(Msg, #{next => stop})
|
reset(Msg, #{next => stop})
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
wait_ack(
|
||||||
wait_ack(state_timeout,
|
state_timeout,
|
||||||
ack_timeout,
|
ack_timeout,
|
||||||
#transport{cache = Msg,
|
#transport{
|
||||||
retry_interval = Timeout,
|
cache = Msg,
|
||||||
retry_count = Count} =Transport) ->
|
retry_interval = Timeout,
|
||||||
|
retry_count = Count
|
||||||
|
} = Transport
|
||||||
|
) ->
|
||||||
case Count < ?MAX_RETRANSMIT of
|
case Count < ?MAX_RETRANSMIT of
|
||||||
true ->
|
true ->
|
||||||
Timeout2 = Timeout * 2,
|
Timeout2 = Timeout * 2,
|
||||||
out(Msg,
|
out(
|
||||||
#{transport => Transport#transport{retry_interval = Timeout2,
|
Msg,
|
||||||
retry_count = Count + 1},
|
#{
|
||||||
timeouts => [{state_timeout, Timeout2, ack_timeout}]});
|
transport => Transport#transport{
|
||||||
|
retry_interval = Timeout2,
|
||||||
|
retry_count = Count + 1
|
||||||
|
},
|
||||||
|
timeouts => [{state_timeout, Timeout2, ack_timeout}]
|
||||||
|
}
|
||||||
|
);
|
||||||
_ ->
|
_ ->
|
||||||
proto_out({ack_failure, Msg}, #{next_state => stop})
|
proto_out({ack_failure, Msg}, #{next_state => stop})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
observe(in,
|
observe(
|
||||||
#coap_message{method = Method} = Message,
|
in,
|
||||||
#transport{observe = Observe} = Transport) ->
|
#coap_message{method = Method} = Message,
|
||||||
|
#transport{observe = Observe} = Transport
|
||||||
|
) ->
|
||||||
case Method of
|
case Method of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
case emqx_coap_message:get_option(observe, Message, Observe) of
|
case emqx_coap_message:get_option(observe, Message, Observe) of
|
||||||
|
|
@ -158,9 +207,11 @@ observe(in,
|
||||||
%% repeatd notify, ignore
|
%% repeatd notify, ignore
|
||||||
empty();
|
empty();
|
||||||
NewObserve ->
|
NewObserve ->
|
||||||
on_response(Message,
|
on_response(
|
||||||
Transport#transport{observe = NewObserve},
|
Message,
|
||||||
?FUNCTION_NAME)
|
Transport#transport{observe = NewObserve},
|
||||||
|
?FUNCTION_NAME
|
||||||
|
)
|
||||||
end;
|
end;
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
#{next => stop};
|
#{next => stop};
|
||||||
|
|
@ -174,17 +225,24 @@ until_stop(_, _, _) ->
|
||||||
set_cache(Cache, Transport) ->
|
set_cache(Cache, Transport) ->
|
||||||
Transport#transport{cache = Cache}.
|
Transport#transport{cache = Cache}.
|
||||||
|
|
||||||
on_response(#coap_message{type = Type} = Message,
|
on_response(
|
||||||
#transport{req_context = Ctx} = Transport,
|
#coap_message{type = Type} = Message,
|
||||||
NextState) ->
|
#transport{req_context = Ctx} = Transport,
|
||||||
|
NextState
|
||||||
|
) ->
|
||||||
CtxMsg = {Ctx, Message},
|
CtxMsg = {Ctx, Message},
|
||||||
if Type =:= non ->
|
if
|
||||||
|
Type =:= non ->
|
||||||
proto_out({response, CtxMsg}, #{next => NextState});
|
proto_out({response, CtxMsg}, #{next => NextState});
|
||||||
Type =:= con ->
|
Type =:= con ->
|
||||||
Ack = emqx_coap_message:ack(Message),
|
Ack = emqx_coap_message:ack(Message),
|
||||||
proto_out({response, CtxMsg},
|
proto_out(
|
||||||
out(Ack, #{next => NextState,
|
{response, CtxMsg},
|
||||||
transport => Transport#transport{cache = Ack}}));
|
out(Ack, #{
|
||||||
true ->
|
next => NextState,
|
||||||
|
transport => Transport#transport{cache = Ack}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
true ->
|
||||||
emqx_coap_message:reset(Message)
|
emqx_coap_message:reset(Message)
|
||||||
end.
|
end.
|
||||||
|
|
|
||||||
|
|
@ -24,18 +24,14 @@
|
||||||
|
|
||||||
handle_request([<<"connection">>], #coap_message{method = Method} = Msg, _Ctx, _CInfo) ->
|
handle_request([<<"connection">>], #coap_message{method = Method} = Msg, _Ctx, _CInfo) ->
|
||||||
handle_method(Method, Msg);
|
handle_method(Method, Msg);
|
||||||
|
|
||||||
handle_request(_, Msg, _, _) ->
|
handle_request(_, Msg, _, _) ->
|
||||||
reply({error, bad_request}, Msg).
|
reply({error, bad_request}, Msg).
|
||||||
|
|
||||||
handle_method(put, Msg) ->
|
handle_method(put, Msg) ->
|
||||||
reply({ok, changed}, Msg);
|
reply({ok, changed}, Msg);
|
||||||
|
|
||||||
handle_method(post, Msg) ->
|
handle_method(post, Msg) ->
|
||||||
#{connection => {open, Msg}};
|
#{connection => {open, Msg}};
|
||||||
|
|
||||||
handle_method(delete, Msg) ->
|
handle_method(delete, Msg) ->
|
||||||
#{connection => {close, Msg}};
|
#{connection => {close, Msg}};
|
||||||
|
|
||||||
handle_method(_, Msg) ->
|
handle_method(_, Msg) ->
|
||||||
reply({error, method_not_allowed}, Msg).
|
reply({error, method_not_allowed}, Msg).
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,6 @@ handle_method(get, Topic, Msg, Ctx, CInfo) ->
|
||||||
_ ->
|
_ ->
|
||||||
reply({error, bad_request}, <<"invalid observe value">>, Msg)
|
reply({error, bad_request}, <<"invalid observe value">>, Msg)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_method(post, Topic, #coap_message{payload = Payload} = Msg, Ctx, CInfo) ->
|
handle_method(post, Topic, #coap_message{payload = Payload} = Msg, Ctx, CInfo) ->
|
||||||
case emqx_coap_channel:validator(publish, Topic, Ctx, CInfo) of
|
case emqx_coap_channel:validator(publish, Topic, Ctx, CInfo) of
|
||||||
allow ->
|
allow ->
|
||||||
|
|
@ -64,22 +63,23 @@ handle_method(post, Topic, #coap_message{payload = Payload} = Msg, Ctx, CInfo) -
|
||||||
_ ->
|
_ ->
|
||||||
reply({error, unauthorized}, Msg)
|
reply({error, unauthorized}, Msg)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_method(_, _, Msg, _, _) ->
|
handle_method(_, _, Msg, _, _) ->
|
||||||
reply({error, method_not_allowed}, Msg).
|
reply({error, method_not_allowed}, Msg).
|
||||||
|
|
||||||
check_topic([]) ->
|
check_topic([]) ->
|
||||||
error;
|
error;
|
||||||
|
|
||||||
check_topic(Path) ->
|
check_topic(Path) ->
|
||||||
Sep = <<"/">>,
|
Sep = <<"/">>,
|
||||||
{ok,
|
{ok,
|
||||||
emqx_http_lib:uri_decode(
|
emqx_http_lib:uri_decode(
|
||||||
lists:foldl(fun(Part, Acc) ->
|
lists:foldl(
|
||||||
<<Acc/binary, Sep/binary, Part/binary>>
|
fun(Part, Acc) ->
|
||||||
end,
|
<<Acc/binary, Sep/binary, Part/binary>>
|
||||||
<<>>,
|
end,
|
||||||
Path))}.
|
<<>>,
|
||||||
|
Path
|
||||||
|
)
|
||||||
|
)}.
|
||||||
|
|
||||||
get_sub_opts(#coap_message{options = Opts} = Msg) ->
|
get_sub_opts(#coap_message{options = Opts} = Msg) ->
|
||||||
SubOpts = maps:fold(fun parse_sub_opts/3, #{}, Opts),
|
SubOpts = maps:fold(fun parse_sub_opts/3, #{}, Opts),
|
||||||
|
|
@ -100,9 +100,12 @@ parse_sub_opts(<<"rh">>, V, Opts) ->
|
||||||
parse_sub_opts(_, _, Opts) ->
|
parse_sub_opts(_, _, Opts) ->
|
||||||
Opts.
|
Opts.
|
||||||
|
|
||||||
type_to_qos(qos0, _) -> ?QOS_0;
|
type_to_qos(qos0, _) ->
|
||||||
type_to_qos(qos1, _) -> ?QOS_1;
|
?QOS_0;
|
||||||
type_to_qos(qos2, _) -> ?QOS_2;
|
type_to_qos(qos1, _) ->
|
||||||
|
?QOS_1;
|
||||||
|
type_to_qos(qos2, _) ->
|
||||||
|
?QOS_2;
|
||||||
type_to_qos(coap, #coap_message{type = Type}) ->
|
type_to_qos(coap, #coap_message{type = Type}) ->
|
||||||
case Type of
|
case Type of
|
||||||
non ->
|
non ->
|
||||||
|
|
@ -121,24 +124,28 @@ get_publish_qos(Msg) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
apply_publish_opts(Msg, MQTTMsg) ->
|
apply_publish_opts(Msg, MQTTMsg) ->
|
||||||
maps:fold(fun(<<"retain">>, V, Acc) ->
|
maps:fold(
|
||||||
Val = erlang:binary_to_atom(V),
|
fun
|
||||||
emqx_message:set_flag(retain, Val, Acc);
|
(<<"retain">>, V, Acc) ->
|
||||||
(<<"expiry">>, V, Acc) ->
|
Val = erlang:binary_to_atom(V),
|
||||||
Val = erlang:binary_to_integer(V),
|
emqx_message:set_flag(retain, Val, Acc);
|
||||||
Props = emqx_message:get_header(properties, Acc),
|
(<<"expiry">>, V, Acc) ->
|
||||||
emqx_message:set_header(properties,
|
Val = erlang:binary_to_integer(V),
|
||||||
Props#{'Message-Expiry-Interval' => Val},
|
Props = emqx_message:get_header(properties, Acc),
|
||||||
Acc);
|
emqx_message:set_header(
|
||||||
(_, _, Acc) ->
|
properties,
|
||||||
Acc
|
Props#{'Message-Expiry-Interval' => Val},
|
||||||
end,
|
Acc
|
||||||
MQTTMsg,
|
);
|
||||||
emqx_coap_message:get_option(uri_query, Msg)).
|
(_, _, Acc) ->
|
||||||
|
Acc
|
||||||
|
end,
|
||||||
|
MQTTMsg,
|
||||||
|
emqx_coap_message:get_option(uri_query, Msg)
|
||||||
|
).
|
||||||
|
|
||||||
subscribe(#coap_message{token = <<>>} = Msg, _, _, _) ->
|
subscribe(#coap_message{token = <<>>} = Msg, _, _, _) ->
|
||||||
reply({error, bad_request}, <<"observe without token">>, Msg);
|
reply({error, bad_request}, <<"observe without token">>, Msg);
|
||||||
|
|
||||||
subscribe(#coap_message{token = Token} = Msg, Topic, Ctx, CInfo) ->
|
subscribe(#coap_message{token = Token} = Msg, Topic, Ctx, CInfo) ->
|
||||||
case emqx_coap_channel:validator(subscribe, Topic, Ctx, CInfo) of
|
case emqx_coap_channel:validator(subscribe, Topic, Ctx, CInfo) of
|
||||||
allow ->
|
allow ->
|
||||||
|
|
|
||||||
|
|
@ -22,55 +22,60 @@
|
||||||
-define(DEFAULT_MAX_AGE, 60).
|
-define(DEFAULT_MAX_AGE, 60).
|
||||||
-define(MAXIMUM_MAX_AGE, 4294967295).
|
-define(MAXIMUM_MAX_AGE, 4294967295).
|
||||||
|
|
||||||
-type coap_message_id() :: 1 .. ?MAX_MESSAGE_ID.
|
-type coap_message_id() :: 1..?MAX_MESSAGE_ID.
|
||||||
-type message_type() :: con | non | ack | reset.
|
-type message_type() :: con | non | ack | reset.
|
||||||
-type max_age() :: 1 .. ?MAXIMUM_MAX_AGE.
|
-type max_age() :: 1..?MAXIMUM_MAX_AGE.
|
||||||
|
|
||||||
-type message_option_name() :: if_match
|
-type message_option_name() ::
|
||||||
| uri_host
|
if_match
|
||||||
| etag
|
| uri_host
|
||||||
| if_none_match
|
| etag
|
||||||
| uri_port
|
| if_none_match
|
||||||
| location_path
|
| uri_port
|
||||||
| uri_path
|
| location_path
|
||||||
| content_format
|
| uri_path
|
||||||
| max_age
|
| content_format
|
||||||
| uri_query
|
| max_age
|
||||||
| 'accept'
|
| uri_query
|
||||||
| location_query
|
| 'accept'
|
||||||
| proxy_uri
|
| location_query
|
||||||
| proxy_scheme
|
| proxy_uri
|
||||||
| size1
|
| proxy_scheme
|
||||||
| observer
|
| size1
|
||||||
| block1
|
| observer
|
||||||
| block2.
|
| block1
|
||||||
|
| block2.
|
||||||
|
|
||||||
-type message_options() :: #{ if_match => list(binary())
|
-type message_options() :: #{
|
||||||
, uri_host => binary()
|
if_match => list(binary()),
|
||||||
, etag => list(binary())
|
uri_host => binary(),
|
||||||
, if_none_match => boolean()
|
etag => list(binary()),
|
||||||
, uri_port => 0 .. 65535
|
if_none_match => boolean(),
|
||||||
, location_path => list(binary())
|
uri_port => 0..65535,
|
||||||
, uri_path => list(binary())
|
location_path => list(binary()),
|
||||||
, content_format => 0 .. 65535
|
uri_path => list(binary()),
|
||||||
, max_age => non_neg_integer()
|
content_format => 0..65535,
|
||||||
, uri_query => list(binary()) | map()
|
max_age => non_neg_integer(),
|
||||||
, 'accept' => 0 .. 65535
|
uri_query => list(binary()) | map(),
|
||||||
, location_query => list(binary())
|
'accept' => 0..65535,
|
||||||
, proxy_uri => binary()
|
location_query => list(binary()),
|
||||||
, proxy_scheme => binary()
|
proxy_uri => binary(),
|
||||||
, size1 => non_neg_integer()
|
proxy_scheme => binary(),
|
||||||
, observer => non_neg_integer()
|
size1 => non_neg_integer(),
|
||||||
, block1 => {non_neg_integer(), boolean(), non_neg_integer()}
|
observer => non_neg_integer(),
|
||||||
, block2 => {non_neg_integer(), boolean(), non_neg_integer()}}.
|
block1 => {non_neg_integer(), boolean(), non_neg_integer()},
|
||||||
|
block2 => {non_neg_integer(), boolean(), non_neg_integer()}
|
||||||
|
}.
|
||||||
|
|
||||||
-record(coap_mqtt_auth, {clientid, username, password}).
|
-record(coap_mqtt_auth, {clientid, username, password}).
|
||||||
|
|
||||||
-record(coap_message, { type :: message_type()
|
-record(coap_message, {
|
||||||
, method
|
type :: message_type(),
|
||||||
, id
|
method,
|
||||||
, token = <<>>
|
id,
|
||||||
, options = #{}
|
token = <<>>,
|
||||||
, payload = <<>>}).
|
options = #{},
|
||||||
|
payload = <<>>
|
||||||
|
}).
|
||||||
|
|
||||||
-type coap_message() :: #coap_message{}.
|
-type coap_message() :: #coap_message{}.
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
%% -*- mode: erlang -*-
|
%% -*- mode: erlang -*-
|
||||||
{application, emqx_gateway,
|
{application, emqx_gateway, [
|
||||||
[{description, "The Gateway management application"},
|
{description, "The Gateway management application"},
|
||||||
{vsn, "0.1.0"},
|
{vsn, "0.1.0"},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{mod, {emqx_gateway_app, []}},
|
{mod, {emqx_gateway_app, []}},
|
||||||
{applications, [kernel, stdlib, grpc, emqx]},
|
{applications, [kernel, stdlib, grpc, emqx]},
|
||||||
{env, []},
|
{env, []},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
{licenses, ["Apache 2.0"]},
|
{licenses, ["Apache 2.0"]},
|
||||||
{links, []}
|
{links, []}
|
||||||
]}.
|
]}.
|
||||||
|
|
|
||||||
|
|
@ -19,15 +19,16 @@
|
||||||
-include("include/emqx_gateway.hrl").
|
-include("include/emqx_gateway.hrl").
|
||||||
|
|
||||||
%% Gateway APIs
|
%% Gateway APIs
|
||||||
-export([ registered_gateway/0
|
-export([
|
||||||
, load/2
|
registered_gateway/0,
|
||||||
, unload/1
|
load/2,
|
||||||
, lookup/1
|
unload/1,
|
||||||
, update/2
|
lookup/1,
|
||||||
, start/1
|
update/2,
|
||||||
, stop/1
|
start/1,
|
||||||
, list/0
|
stop/1,
|
||||||
]).
|
list/0
|
||||||
|
]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% APIs
|
%% APIs
|
||||||
|
|
@ -46,14 +47,15 @@ registered_gateway() ->
|
||||||
list() ->
|
list() ->
|
||||||
emqx_gateway_sup:list_gateway_insta().
|
emqx_gateway_sup:list_gateway_insta().
|
||||||
|
|
||||||
-spec load(gateway_name(), emqx_config:config())
|
-spec load(gateway_name(), emqx_config:config()) ->
|
||||||
-> {ok, pid()}
|
{ok, pid()}
|
||||||
| {error, any()}.
|
| {error, any()}.
|
||||||
load(Name, Config) ->
|
load(Name, Config) ->
|
||||||
Gateway = #{ name => Name
|
Gateway = #{
|
||||||
, descr => undefined
|
name => Name,
|
||||||
, config => Config
|
descr => undefined,
|
||||||
},
|
config => Config
|
||||||
|
},
|
||||||
emqx_gateway_sup:load_gateway(Gateway).
|
emqx_gateway_sup:load_gateway(Gateway).
|
||||||
|
|
||||||
-spec unload(gateway_name()) -> ok | {error, not_found}.
|
-spec unload(gateway_name()) -> ok | {error, not_found}.
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -24,25 +24,30 @@
|
||||||
-import(hoconsc, [mk/2, ref/2]).
|
-import(hoconsc, [mk/2, ref/2]).
|
||||||
-import(emqx_dashboard_swagger, [error_codes/2]).
|
-import(emqx_dashboard_swagger, [error_codes/2]).
|
||||||
|
|
||||||
-import(emqx_gateway_http,
|
-import(
|
||||||
[ return_http_error/2
|
emqx_gateway_http,
|
||||||
, with_gateway/2
|
[
|
||||||
, with_authn/2
|
return_http_error/2,
|
||||||
, checks/2
|
with_gateway/2,
|
||||||
]).
|
with_authn/2,
|
||||||
|
checks/2
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
||||||
%% minirest/dashbaord_swagger behaviour callbacks
|
%% minirest/dashbaord_swagger behaviour callbacks
|
||||||
-export([ api_spec/0
|
-export([
|
||||||
, paths/0
|
api_spec/0,
|
||||||
, schema/1
|
paths/0,
|
||||||
]).
|
schema/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% http handlers
|
%% http handlers
|
||||||
-export([ authn/2
|
-export([
|
||||||
, users/2
|
authn/2,
|
||||||
, users_insta/2
|
users/2,
|
||||||
, import_users/2
|
users_insta/2,
|
||||||
]).
|
import_users/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% internal export for emqx_gateway_api_listeners module
|
%% internal export for emqx_gateway_api_listeners module
|
||||||
-export([schema_authn/0]).
|
-export([schema_authn/0]).
|
||||||
|
|
@ -55,10 +60,11 @@ api_spec() ->
|
||||||
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
||||||
|
|
||||||
paths() ->
|
paths() ->
|
||||||
[ "/gateway/:name/authentication"
|
[
|
||||||
, "/gateway/:name/authentication/users"
|
"/gateway/:name/authentication",
|
||||||
, "/gateway/:name/authentication/users/:uid"
|
"/gateway/:name/authentication/users",
|
||||||
, "/gateway/:name/authentication/import_users"
|
"/gateway/:name/authentication/users/:uid",
|
||||||
|
"/gateway/:name/authentication/import_users"
|
||||||
].
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -66,30 +72,29 @@ paths() ->
|
||||||
|
|
||||||
authn(get, #{bindings := #{name := Name0}}) ->
|
authn(get, #{bindings := #{name := Name0}}) ->
|
||||||
with_gateway(Name0, fun(GwName, _) ->
|
with_gateway(Name0, fun(GwName, _) ->
|
||||||
try
|
try emqx_gateway_http:authn(GwName) of
|
||||||
emqx_gateway_http:authn(GwName)
|
|
||||||
of
|
|
||||||
Authn -> {200, Authn}
|
Authn -> {200, Authn}
|
||||||
catch
|
catch
|
||||||
error : {config_not_found, _} ->
|
error:{config_not_found, _} ->
|
||||||
{204}
|
{204}
|
||||||
end
|
end
|
||||||
end);
|
end);
|
||||||
|
authn(put, #{
|
||||||
authn(put, #{bindings := #{name := Name0},
|
bindings := #{name := Name0},
|
||||||
body := Body}) ->
|
body := Body
|
||||||
|
}) ->
|
||||||
with_gateway(Name0, fun(GwName, _) ->
|
with_gateway(Name0, fun(GwName, _) ->
|
||||||
{ok, Authn} = emqx_gateway_http:update_authn(GwName, Body),
|
{ok, Authn} = emqx_gateway_http:update_authn(GwName, Body),
|
||||||
{200, Authn}
|
{200, Authn}
|
||||||
end);
|
end);
|
||||||
|
authn(post, #{
|
||||||
authn(post, #{bindings := #{name := Name0},
|
bindings := #{name := Name0},
|
||||||
body := Body}) ->
|
body := Body
|
||||||
|
}) ->
|
||||||
with_gateway(Name0, fun(GwName, _) ->
|
with_gateway(Name0, fun(GwName, _) ->
|
||||||
{ok, Authn} = emqx_gateway_http:add_authn(GwName, Body),
|
{ok, Authn} = emqx_gateway_http:add_authn(GwName, Body),
|
||||||
{201, Authn}
|
{201, Authn}
|
||||||
end);
|
end);
|
||||||
|
|
||||||
authn(delete, #{bindings := #{name := Name0}}) ->
|
authn(delete, #{bindings := #{name := Name0}}) ->
|
||||||
with_gateway(Name0, fun(GwName, _) ->
|
with_gateway(Name0, fun(GwName, _) ->
|
||||||
ok = emqx_gateway_http:remove_authn(GwName),
|
ok = emqx_gateway_http:remove_authn(GwName),
|
||||||
|
|
@ -97,47 +102,85 @@ authn(delete, #{bindings := #{name := Name0}}) ->
|
||||||
end).
|
end).
|
||||||
|
|
||||||
users(get, #{bindings := #{name := Name0}, query_string := Qs}) ->
|
users(get, #{bindings := #{name := Name0}, query_string := Qs}) ->
|
||||||
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
with_authn(Name0, fun(
|
||||||
chain_name := ChainName}) ->
|
_GwName,
|
||||||
|
#{
|
||||||
|
id := AuthId,
|
||||||
|
chain_name := ChainName
|
||||||
|
}
|
||||||
|
) ->
|
||||||
emqx_authn_api:list_users(ChainName, AuthId, parse_qstring(Qs))
|
emqx_authn_api:list_users(ChainName, AuthId, parse_qstring(Qs))
|
||||||
end);
|
end);
|
||||||
users(post, #{bindings := #{name := Name0},
|
users(post, #{
|
||||||
body := Body}) ->
|
bindings := #{name := Name0},
|
||||||
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
body := Body
|
||||||
chain_name := ChainName}) ->
|
}) ->
|
||||||
|
with_authn(Name0, fun(
|
||||||
|
_GwName,
|
||||||
|
#{
|
||||||
|
id := AuthId,
|
||||||
|
chain_name := ChainName
|
||||||
|
}
|
||||||
|
) ->
|
||||||
emqx_authn_api:add_user(ChainName, AuthId, Body)
|
emqx_authn_api:add_user(ChainName, AuthId, Body)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
users_insta(get, #{bindings := #{name := Name0, uid := UserId}}) ->
|
users_insta(get, #{bindings := #{name := Name0, uid := UserId}}) ->
|
||||||
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
with_authn(Name0, fun(
|
||||||
chain_name := ChainName}) ->
|
_GwName,
|
||||||
|
#{
|
||||||
|
id := AuthId,
|
||||||
|
chain_name := ChainName
|
||||||
|
}
|
||||||
|
) ->
|
||||||
emqx_authn_api:find_user(ChainName, AuthId, UserId)
|
emqx_authn_api:find_user(ChainName, AuthId, UserId)
|
||||||
end);
|
end);
|
||||||
users_insta(put, #{bindings := #{name := Name0, uid := UserId},
|
users_insta(put, #{
|
||||||
body := Body}) ->
|
bindings := #{name := Name0, uid := UserId},
|
||||||
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
body := Body
|
||||||
chain_name := ChainName}) ->
|
}) ->
|
||||||
|
with_authn(Name0, fun(
|
||||||
|
_GwName,
|
||||||
|
#{
|
||||||
|
id := AuthId,
|
||||||
|
chain_name := ChainName
|
||||||
|
}
|
||||||
|
) ->
|
||||||
emqx_authn_api:update_user(ChainName, AuthId, UserId, Body)
|
emqx_authn_api:update_user(ChainName, AuthId, UserId, Body)
|
||||||
end);
|
end);
|
||||||
users_insta(delete, #{bindings := #{name := Name0, uid := UserId}}) ->
|
users_insta(delete, #{bindings := #{name := Name0, uid := UserId}}) ->
|
||||||
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
with_authn(Name0, fun(
|
||||||
chain_name := ChainName}) ->
|
_GwName,
|
||||||
|
#{
|
||||||
|
id := AuthId,
|
||||||
|
chain_name := ChainName
|
||||||
|
}
|
||||||
|
) ->
|
||||||
emqx_authn_api:delete_user(ChainName, AuthId, UserId)
|
emqx_authn_api:delete_user(ChainName, AuthId, UserId)
|
||||||
end).
|
end).
|
||||||
|
|
||||||
import_users(post, #{bindings := #{name := Name0},
|
import_users(post, #{
|
||||||
body := Body}) ->
|
bindings := #{name := Name0},
|
||||||
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
body := Body
|
||||||
chain_name := ChainName}) ->
|
}) ->
|
||||||
|
with_authn(Name0, fun(
|
||||||
|
_GwName,
|
||||||
|
#{
|
||||||
|
id := AuthId,
|
||||||
|
chain_name := ChainName
|
||||||
|
}
|
||||||
|
) ->
|
||||||
case maps:get(<<"filename">>, Body, undefined) of
|
case maps:get(<<"filename">>, Body, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
emqx_authn_api:serialize_error({missing_parameter, filename});
|
emqx_authn_api:serialize_error({missing_parameter, filename});
|
||||||
Filename ->
|
Filename ->
|
||||||
case emqx_authentication:import_users(
|
case
|
||||||
ChainName, AuthId, Filename) of
|
emqx_authentication:import_users(
|
||||||
|
ChainName, AuthId, Filename
|
||||||
|
)
|
||||||
|
of
|
||||||
ok -> {204};
|
ok -> {204};
|
||||||
{error, Reason} ->
|
{error, Reason} -> emqx_authn_api:serialize_error(Reason)
|
||||||
emqx_authn_api:serialize_error(Reason)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end).
|
end).
|
||||||
|
|
@ -146,178 +189,243 @@ import_users(post, #{bindings := #{name := Name0},
|
||||||
%% Utils
|
%% Utils
|
||||||
|
|
||||||
parse_qstring(Qs) ->
|
parse_qstring(Qs) ->
|
||||||
maps:with([ <<"page">>
|
maps:with(
|
||||||
, <<"limit">>
|
[
|
||||||
, <<"like_username">>
|
<<"page">>,
|
||||||
, <<"like_clientid">>], Qs).
|
<<"limit">>,
|
||||||
|
<<"like_username">>,
|
||||||
|
<<"like_clientid">>
|
||||||
|
],
|
||||||
|
Qs
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Swagger defines
|
%% Swagger defines
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
schema("/gateway/:name/authentication") ->
|
schema("/gateway/:name/authentication") ->
|
||||||
#{ 'operationId' => authn,
|
#{
|
||||||
get =>
|
'operationId' => authn,
|
||||||
#{ desc => <<"Get the gateway authentication">>
|
get =>
|
||||||
, parameters => params_gateway_name_in_path()
|
#{
|
||||||
, responses =>
|
desc => <<"Get the gateway authentication">>,
|
||||||
?STANDARD_RESP(
|
parameters => params_gateway_name_in_path(),
|
||||||
#{ 200 => schema_authn()
|
responses =>
|
||||||
, 204 => <<"Authentication does not initiated">>
|
?STANDARD_RESP(
|
||||||
})
|
#{
|
||||||
},
|
200 => schema_authn(),
|
||||||
put =>
|
204 => <<"Authentication does not initiated">>
|
||||||
#{ desc => <<"Update authentication for the gateway">>
|
}
|
||||||
, parameters => params_gateway_name_in_path()
|
)
|
||||||
, 'requestBody' => schema_authn()
|
},
|
||||||
, responses =>
|
|
||||||
?STANDARD_RESP(#{200 => schema_authn()})
|
|
||||||
},
|
|
||||||
post =>
|
|
||||||
#{ desc => <<"Add authentication for the gateway">>
|
|
||||||
, parameters => params_gateway_name_in_path()
|
|
||||||
, 'requestBody' => schema_authn()
|
|
||||||
, responses =>
|
|
||||||
?STANDARD_RESP(#{201 => schema_authn()})
|
|
||||||
},
|
|
||||||
delete =>
|
|
||||||
#{ desc => <<"Remove the gateway authentication">>
|
|
||||||
, parameters => params_gateway_name_in_path()
|
|
||||||
, responses =>
|
|
||||||
?STANDARD_RESP(#{204 => <<"Deleted">>})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
schema("/gateway/:name/authentication/users") ->
|
|
||||||
#{ 'operationId' => users
|
|
||||||
, get =>
|
|
||||||
#{ desc => <<"Get the users for the authentication">>
|
|
||||||
, parameters => params_gateway_name_in_path() ++
|
|
||||||
params_paging_in_qs() ++
|
|
||||||
params_fuzzy_in_qs()
|
|
||||||
, responses =>
|
|
||||||
?STANDARD_RESP(
|
|
||||||
#{ 200 => emqx_dashboard_swagger:schema_with_example(
|
|
||||||
ref(emqx_authn_api, response_users),
|
|
||||||
emqx_authn_api:response_users_example())
|
|
||||||
})
|
|
||||||
},
|
|
||||||
post =>
|
|
||||||
#{ desc => <<"Add user for the authentication">>
|
|
||||||
, parameters => params_gateway_name_in_path()
|
|
||||||
, 'requestBody' => emqx_dashboard_swagger:schema_with_examples(
|
|
||||||
ref(emqx_authn_api, request_user_create),
|
|
||||||
emqx_authn_api:request_user_create_examples())
|
|
||||||
, responses =>
|
|
||||||
?STANDARD_RESP(
|
|
||||||
#{ 201 => emqx_dashboard_swagger:schema_with_example(
|
|
||||||
ref(emqx_authn_api, response_user),
|
|
||||||
emqx_authn_api:response_user_examples())
|
|
||||||
})
|
|
||||||
}
|
|
||||||
};
|
|
||||||
schema("/gateway/:name/authentication/users/:uid") ->
|
|
||||||
#{ 'operationId' => users_insta
|
|
||||||
, get =>
|
|
||||||
#{ desc => <<"Get user info from the gateway "
|
|
||||||
"authentication">>
|
|
||||||
, parameters => params_gateway_name_in_path() ++
|
|
||||||
params_userid_in_path()
|
|
||||||
, responses =>
|
|
||||||
?STANDARD_RESP(
|
|
||||||
#{ 200 => emqx_dashboard_swagger:schema_with_example(
|
|
||||||
ref(emqx_authn_api, response_user),
|
|
||||||
emqx_authn_api:response_user_examples())
|
|
||||||
})
|
|
||||||
},
|
|
||||||
put =>
|
put =>
|
||||||
#{ desc => <<"Update the user info for the gateway "
|
#{
|
||||||
"authentication">>
|
desc => <<"Update authentication for the gateway">>,
|
||||||
, parameters => params_gateway_name_in_path() ++
|
parameters => params_gateway_name_in_path(),
|
||||||
params_userid_in_path()
|
'requestBody' => schema_authn(),
|
||||||
, 'requestBody' => emqx_dashboard_swagger:schema_with_examples(
|
responses =>
|
||||||
ref(emqx_authn_api, request_user_update),
|
?STANDARD_RESP(#{200 => schema_authn()})
|
||||||
emqx_authn_api:request_user_update_examples())
|
},
|
||||||
, responses =>
|
post =>
|
||||||
?STANDARD_RESP(
|
#{
|
||||||
#{ 200 => emqx_dashboard_swagger:schema_with_example(
|
desc => <<"Add authentication for the gateway">>,
|
||||||
ref(emqx_authn_api, response_user),
|
parameters => params_gateway_name_in_path(),
|
||||||
emqx_authn_api:response_user_examples())
|
'requestBody' => schema_authn(),
|
||||||
})
|
responses =>
|
||||||
},
|
?STANDARD_RESP(#{201 => schema_authn()})
|
||||||
|
},
|
||||||
delete =>
|
delete =>
|
||||||
#{ desc => <<"Delete the user for the gateway "
|
#{
|
||||||
"authentication">>
|
desc => <<"Remove the gateway authentication">>,
|
||||||
, parameters => params_gateway_name_in_path() ++
|
parameters => params_gateway_name_in_path(),
|
||||||
params_userid_in_path()
|
responses =>
|
||||||
, responses =>
|
?STANDARD_RESP(#{204 => <<"Deleted">>})
|
||||||
?STANDARD_RESP(#{204 => <<"User Deleted">>})
|
}
|
||||||
}
|
};
|
||||||
};
|
schema("/gateway/:name/authentication/users") ->
|
||||||
schema("/gateway/:name/authentication/import_users") ->
|
#{
|
||||||
#{ 'operationId' => import_users
|
'operationId' => users,
|
||||||
, post =>
|
get =>
|
||||||
#{ desc => <<"Import users into the gateway authentication">>
|
#{
|
||||||
, parameters => params_gateway_name_in_path()
|
desc => <<"Get the users for the authentication">>,
|
||||||
, 'requestBody' => emqx_dashboard_swagger:schema_with_examples(
|
parameters => params_gateway_name_in_path() ++
|
||||||
ref(emqx_authn_api, request_import_users),
|
params_paging_in_qs() ++
|
||||||
emqx_authn_api:request_import_users_examples()
|
params_fuzzy_in_qs(),
|
||||||
|
responses =>
|
||||||
|
?STANDARD_RESP(
|
||||||
|
#{
|
||||||
|
200 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_users),
|
||||||
|
emqx_authn_api:response_users_example()
|
||||||
)
|
)
|
||||||
, responses =>
|
}
|
||||||
?STANDARD_RESP(#{204 => <<"Imported">>})
|
)
|
||||||
}
|
},
|
||||||
}.
|
post =>
|
||||||
|
#{
|
||||||
|
desc => <<"Add user for the authentication">>,
|
||||||
|
parameters => params_gateway_name_in_path(),
|
||||||
|
'requestBody' => emqx_dashboard_swagger:schema_with_examples(
|
||||||
|
ref(emqx_authn_api, request_user_create),
|
||||||
|
emqx_authn_api:request_user_create_examples()
|
||||||
|
),
|
||||||
|
responses =>
|
||||||
|
?STANDARD_RESP(
|
||||||
|
#{
|
||||||
|
201 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
schema("/gateway/:name/authentication/users/:uid") ->
|
||||||
|
#{
|
||||||
|
'operationId' => users_insta,
|
||||||
|
get =>
|
||||||
|
#{
|
||||||
|
desc => <<
|
||||||
|
"Get user info from the gateway "
|
||||||
|
"authentication"
|
||||||
|
>>,
|
||||||
|
parameters => params_gateway_name_in_path() ++
|
||||||
|
params_userid_in_path(),
|
||||||
|
responses =>
|
||||||
|
?STANDARD_RESP(
|
||||||
|
#{
|
||||||
|
200 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
put =>
|
||||||
|
#{
|
||||||
|
desc => <<
|
||||||
|
"Update the user info for the gateway "
|
||||||
|
"authentication"
|
||||||
|
>>,
|
||||||
|
parameters => params_gateway_name_in_path() ++
|
||||||
|
params_userid_in_path(),
|
||||||
|
'requestBody' => emqx_dashboard_swagger:schema_with_examples(
|
||||||
|
ref(emqx_authn_api, request_user_update),
|
||||||
|
emqx_authn_api:request_user_update_examples()
|
||||||
|
),
|
||||||
|
responses =>
|
||||||
|
?STANDARD_RESP(
|
||||||
|
#{
|
||||||
|
200 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
|
delete =>
|
||||||
|
#{
|
||||||
|
desc => <<
|
||||||
|
"Delete the user for the gateway "
|
||||||
|
"authentication"
|
||||||
|
>>,
|
||||||
|
parameters => params_gateway_name_in_path() ++
|
||||||
|
params_userid_in_path(),
|
||||||
|
responses =>
|
||||||
|
?STANDARD_RESP(#{204 => <<"User Deleted">>})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
schema("/gateway/:name/authentication/import_users") ->
|
||||||
|
#{
|
||||||
|
'operationId' => import_users,
|
||||||
|
post =>
|
||||||
|
#{
|
||||||
|
desc => <<"Import users into the gateway authentication">>,
|
||||||
|
parameters => params_gateway_name_in_path(),
|
||||||
|
'requestBody' => emqx_dashboard_swagger:schema_with_examples(
|
||||||
|
ref(emqx_authn_api, request_import_users),
|
||||||
|
emqx_authn_api:request_import_users_examples()
|
||||||
|
),
|
||||||
|
responses =>
|
||||||
|
?STANDARD_RESP(#{204 => <<"Imported">>})
|
||||||
|
}
|
||||||
|
}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% params defines
|
%% params defines
|
||||||
|
|
||||||
params_gateway_name_in_path() ->
|
params_gateway_name_in_path() ->
|
||||||
[{name,
|
[
|
||||||
mk(binary(),
|
{name,
|
||||||
#{ in => path
|
mk(
|
||||||
, desc => <<"Gateway Name">>
|
binary(),
|
||||||
, example => <<"">>
|
#{
|
||||||
})}
|
in => path,
|
||||||
|
desc => <<"Gateway Name">>,
|
||||||
|
example => <<"">>
|
||||||
|
}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
params_userid_in_path() ->
|
params_userid_in_path() ->
|
||||||
[{uid, mk(binary(),
|
[
|
||||||
#{ in => path
|
{uid,
|
||||||
, desc => <<"User ID">>
|
mk(
|
||||||
, example => <<"">>
|
binary(),
|
||||||
})}
|
#{
|
||||||
|
in => path,
|
||||||
|
desc => <<"User ID">>,
|
||||||
|
example => <<"">>
|
||||||
|
}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
params_paging_in_qs() ->
|
params_paging_in_qs() ->
|
||||||
[{page, mk(integer(),
|
[
|
||||||
#{ in => query
|
{page,
|
||||||
, required => false
|
mk(
|
||||||
, desc => <<"Page Index">>
|
integer(),
|
||||||
, example => 1
|
#{
|
||||||
})},
|
in => query,
|
||||||
{limit, mk(integer(),
|
required => false,
|
||||||
#{ in => query
|
desc => <<"Page Index">>,
|
||||||
, required => false
|
example => 1
|
||||||
, desc => <<"Page Limit">>
|
}
|
||||||
, example => 100
|
)},
|
||||||
})}
|
{limit,
|
||||||
|
mk(
|
||||||
|
integer(),
|
||||||
|
#{
|
||||||
|
in => query,
|
||||||
|
required => false,
|
||||||
|
desc => <<"Page Limit">>,
|
||||||
|
example => 100
|
||||||
|
}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
params_fuzzy_in_qs() ->
|
params_fuzzy_in_qs() ->
|
||||||
[{like_username,
|
[
|
||||||
mk(binary(),
|
{like_username,
|
||||||
#{ in => query
|
mk(
|
||||||
, required => false
|
binary(),
|
||||||
, desc => <<"Fuzzy search by username">>
|
#{
|
||||||
, example => <<"username">>
|
in => query,
|
||||||
})},
|
required => false,
|
||||||
{like_clientid,
|
desc => <<"Fuzzy search by username">>,
|
||||||
mk(binary(),
|
example => <<"username">>
|
||||||
#{ in => query
|
}
|
||||||
, required => false
|
)},
|
||||||
, desc => <<"Fuzzy search by clientid">>
|
{like_clientid,
|
||||||
, example => <<"clientid">>
|
mk(
|
||||||
})}
|
binary(),
|
||||||
|
#{
|
||||||
|
in => query,
|
||||||
|
required => false,
|
||||||
|
desc => <<"Fuzzy search by clientid">>,
|
||||||
|
example => <<"clientid">>
|
||||||
|
}
|
||||||
|
)}
|
||||||
].
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -325,6 +433,6 @@ params_fuzzy_in_qs() ->
|
||||||
|
|
||||||
schema_authn() ->
|
schema_authn() ->
|
||||||
emqx_dashboard_swagger:schema_with_examples(
|
emqx_dashboard_swagger:schema_with_examples(
|
||||||
emqx_authn_schema:authenticator_type(),
|
emqx_authn_schema:authenticator_type(),
|
||||||
emqx_authn_api:authenticator_examples()
|
emqx_authn_api:authenticator_examples()
|
||||||
).
|
).
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
|
@ -22,6 +22,8 @@
|
||||||
|
|
||||||
-export([start/2, stop/1]).
|
-export([start/2, stop/1]).
|
||||||
|
|
||||||
|
-elvis([{elvis_style, invalid_dynamic_call, disable}]).
|
||||||
|
|
||||||
start(_StartType, _StartArgs) ->
|
start(_StartType, _StartArgs) ->
|
||||||
{ok, Sup} = emqx_gateway_sup:start_link(),
|
{ok, Sup} = emqx_gateway_sup:start_link(),
|
||||||
emqx_gateway_cli:load(),
|
emqx_gateway_cli:load(),
|
||||||
|
|
@ -44,22 +46,29 @@ load_default_gateway_applications() ->
|
||||||
|
|
||||||
gateway_type_searching() ->
|
gateway_type_searching() ->
|
||||||
%% FIXME: Hardcoded apps
|
%% FIXME: Hardcoded apps
|
||||||
[emqx_stomp_impl, emqx_sn_impl, emqx_exproto_impl,
|
[
|
||||||
emqx_coap_impl, emqx_lwm2m_impl].
|
emqx_stomp_impl,
|
||||||
|
emqx_sn_impl,
|
||||||
|
emqx_exproto_impl,
|
||||||
|
emqx_coap_impl,
|
||||||
|
emqx_lwm2m_impl
|
||||||
|
].
|
||||||
|
|
||||||
reg(Mod) ->
|
reg(Mod) ->
|
||||||
try
|
try
|
||||||
Mod:reg(),
|
Mod:reg(),
|
||||||
?SLOG(debug, #{ msg => "register_gateway_succeed"
|
?SLOG(debug, #{
|
||||||
, callback_module => Mod
|
msg => "register_gateway_succeed",
|
||||||
})
|
callback_module => Mod
|
||||||
|
})
|
||||||
catch
|
catch
|
||||||
Class : Reason : Stk ->
|
Class:Reason:Stk ->
|
||||||
?SLOG(error, #{ msg => "failed_to_register_gateway"
|
?SLOG(error, #{
|
||||||
, callback_module => Mod
|
msg => "failed_to_register_gateway",
|
||||||
, reason => {Class, Reason}
|
callback_module => Mod,
|
||||||
, stacktrace => Stk
|
reason => {Class, Reason},
|
||||||
})
|
stacktrace => Stk
|
||||||
|
})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
load_gateway_by_default() ->
|
load_gateway_by_default() ->
|
||||||
|
|
@ -67,22 +76,26 @@ load_gateway_by_default() ->
|
||||||
|
|
||||||
load_gateway_by_default([]) ->
|
load_gateway_by_default([]) ->
|
||||||
ok;
|
ok;
|
||||||
load_gateway_by_default([{Type, Confs}|More]) ->
|
load_gateway_by_default([{Type, Confs} | More]) ->
|
||||||
case emqx_gateway_registry:lookup(Type) of
|
case emqx_gateway_registry:lookup(Type) of
|
||||||
undefined ->
|
undefined ->
|
||||||
?SLOG(error, #{ msg => "skip_to_load_gateway"
|
?SLOG(error, #{
|
||||||
, gateway_name => Type
|
msg => "skip_to_load_gateway",
|
||||||
});
|
gateway_name => Type
|
||||||
|
});
|
||||||
_ ->
|
_ ->
|
||||||
case emqx_gateway:load(Type, Confs) of
|
case emqx_gateway:load(Type, Confs) of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
?SLOG(debug, #{ msg => "load_gateway_succeed"
|
?SLOG(debug, #{
|
||||||
, gateway_name => Type
|
msg => "load_gateway_succeed",
|
||||||
});
|
gateway_name => Type
|
||||||
|
});
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{ msg => "load_gateway_failed"
|
?SLOG(error, #{
|
||||||
, gateway_name => Type
|
msg => "load_gateway_failed",
|
||||||
, reason => Reason})
|
gateway_name => Type,
|
||||||
|
reason => Reason
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
load_gateway_by_default(More).
|
load_gateway_by_default(More).
|
||||||
|
|
|
||||||
|
|
@ -17,25 +17,30 @@
|
||||||
%% @doc The Command-Line-Interface module for Gateway Application
|
%% @doc The Command-Line-Interface module for Gateway Application
|
||||||
-module(emqx_gateway_cli).
|
-module(emqx_gateway_cli).
|
||||||
|
|
||||||
-export([ load/0
|
-export([
|
||||||
, unload/0
|
load/0,
|
||||||
]).
|
unload/0
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ gateway/1
|
-export([
|
||||||
, 'gateway-registry'/1
|
gateway/1,
|
||||||
, 'gateway-clients'/1
|
'gateway-registry'/1,
|
||||||
, 'gateway-metrics'/1
|
'gateway-clients'/1,
|
||||||
%, 'gateway-banned'/1
|
'gateway-metrics'/1
|
||||||
]).
|
%, 'gateway-banned'/1
|
||||||
|
]).
|
||||||
|
|
||||||
-elvis([{elvis_style, function_naming_convention, disable}]).
|
-elvis([{elvis_style, function_naming_convention, disable}]).
|
||||||
|
|
||||||
-spec load() -> ok.
|
-spec load() -> ok.
|
||||||
load() ->
|
load() ->
|
||||||
Cmds = [Fun || {Fun, _} <- ?MODULE:module_info(exports), is_cmd(Fun)],
|
Cmds = [Fun || {Fun, _} <- ?MODULE:module_info(exports), is_cmd(Fun)],
|
||||||
lists:foreach(fun(Cmd) ->
|
lists:foreach(
|
||||||
emqx_ctl:register_command(Cmd, {?MODULE, Cmd}, [])
|
fun(Cmd) ->
|
||||||
end, Cmds).
|
emqx_ctl:register_command(Cmd, {?MODULE, Cmd}, [])
|
||||||
|
end,
|
||||||
|
Cmds
|
||||||
|
).
|
||||||
|
|
||||||
-spec unload() -> ok.
|
-spec unload() -> ok.
|
||||||
unload() ->
|
unload() ->
|
||||||
|
|
@ -53,10 +58,11 @@ is_cmd(Fun) ->
|
||||||
|
|
||||||
gateway(["list"]) ->
|
gateway(["list"]) ->
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun (GwSummary) ->
|
fun(GwSummary) ->
|
||||||
print(format_gw_summary(GwSummary))
|
print(format_gw_summary(GwSummary))
|
||||||
end, emqx_gateway_http:gateways(all));
|
end,
|
||||||
|
emqx_gateway_http:gateways(all)
|
||||||
|
);
|
||||||
gateway(["lookup", Name]) ->
|
gateway(["lookup", Name]) ->
|
||||||
case emqx_gateway:lookup(atom(Name)) of
|
case emqx_gateway:lookup(atom(Name)) of
|
||||||
undefined ->
|
undefined ->
|
||||||
|
|
@ -64,18 +70,18 @@ gateway(["lookup", Name]) ->
|
||||||
Gateway ->
|
Gateway ->
|
||||||
print(format_gateway(Gateway))
|
print(format_gateway(Gateway))
|
||||||
end;
|
end;
|
||||||
|
|
||||||
gateway(["load", Name, Conf]) ->
|
gateway(["load", Name, Conf]) ->
|
||||||
case emqx_gateway_conf:load_gateway(
|
case
|
||||||
bin(Name),
|
emqx_gateway_conf:load_gateway(
|
||||||
emqx_json:decode(Conf, [return_maps])
|
bin(Name),
|
||||||
) of
|
emqx_json:decode(Conf, [return_maps])
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
print("ok\n");
|
print("ok\n");
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
print("Error: ~ts\n", [format_error(Reason)])
|
print("Error: ~ts\n", [format_error(Reason)])
|
||||||
end;
|
end;
|
||||||
|
|
||||||
gateway(["unload", Name]) ->
|
gateway(["unload", Name]) ->
|
||||||
case emqx_gateway_conf:unload_gateway(bin(Name)) of
|
case emqx_gateway_conf:unload_gateway(bin(Name)) of
|
||||||
ok ->
|
ok ->
|
||||||
|
|
@ -83,56 +89,51 @@ gateway(["unload", Name]) ->
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
print("Error: ~ts\n", [format_error(Reason)])
|
print("Error: ~ts\n", [format_error(Reason)])
|
||||||
end;
|
end;
|
||||||
|
|
||||||
gateway(["stop", Name]) ->
|
gateway(["stop", Name]) ->
|
||||||
case emqx_gateway_conf:update_gateway(
|
case
|
||||||
bin(Name),
|
emqx_gateway_conf:update_gateway(
|
||||||
#{<<"enable">> => <<"false">>}
|
bin(Name),
|
||||||
) of
|
#{<<"enable">> => <<"false">>}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
print("ok\n");
|
print("ok\n");
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
print("Error: ~ts\n", [format_error(Reason)])
|
print("Error: ~ts\n", [format_error(Reason)])
|
||||||
end;
|
end;
|
||||||
|
|
||||||
gateway(["start", Name]) ->
|
gateway(["start", Name]) ->
|
||||||
case emqx_gateway_conf:update_gateway(
|
case
|
||||||
bin(Name),
|
emqx_gateway_conf:update_gateway(
|
||||||
#{<<"enable">> => <<"true">>}
|
bin(Name),
|
||||||
) of
|
#{<<"enable">> => <<"true">>}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
print("ok\n");
|
print("ok\n");
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
print("Error: ~ts\n", [format_error(Reason)])
|
print("Error: ~ts\n", [format_error(Reason)])
|
||||||
end;
|
end;
|
||||||
|
|
||||||
gateway(_) ->
|
gateway(_) ->
|
||||||
emqx_ctl:usage(
|
emqx_ctl:usage(
|
||||||
[ {"gateway list",
|
[
|
||||||
"List all gateway"}
|
{"gateway list", "List all gateway"},
|
||||||
, {"gateway lookup <Name>",
|
{"gateway lookup <Name>", "Lookup a gateway detailed information"},
|
||||||
"Lookup a gateway detailed information"}
|
{"gateway load <Name> <JsonConf>", "Load a gateway with config"},
|
||||||
, {"gateway load <Name> <JsonConf>",
|
{"gateway unload <Name>", "Unload the gateway"},
|
||||||
"Load a gateway with config"}
|
{"gateway stop <Name>", "Stop the gateway"},
|
||||||
, {"gateway unload <Name>",
|
{"gateway start <Name>", "Start the gateway"}
|
||||||
"Unload the gateway"}
|
]
|
||||||
, {"gateway stop <Name>",
|
).
|
||||||
"Stop the gateway"}
|
|
||||||
, {"gateway start <Name>",
|
|
||||||
"Start the gateway"}
|
|
||||||
]).
|
|
||||||
|
|
||||||
'gateway-registry'(["list"]) ->
|
'gateway-registry'(["list"]) ->
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun({Name, #{cbkmod := CbMod}}) ->
|
fun({Name, #{cbkmod := CbMod}}) ->
|
||||||
print("Registered Name: ~ts, Callback Module: ~ts\n", [Name, CbMod])
|
print("Registered Name: ~ts, Callback Module: ~ts\n", [Name, CbMod])
|
||||||
end,
|
end,
|
||||||
emqx_gateway_registry:list());
|
emqx_gateway_registry:list()
|
||||||
|
);
|
||||||
'gateway-registry'(_) ->
|
'gateway-registry'(_) ->
|
||||||
emqx_ctl:usage([ {"gateway-registry list",
|
emqx_ctl:usage([{"gateway-registry list", "List all registered gateways"}]).
|
||||||
"List all registered gateways"}
|
|
||||||
]).
|
|
||||||
|
|
||||||
'gateway-clients'(["list", Name]) ->
|
'gateway-clients'(["list", Name]) ->
|
||||||
%% XXX: page me?
|
%% XXX: page me?
|
||||||
|
|
@ -143,7 +144,6 @@ gateway(_) ->
|
||||||
_ ->
|
_ ->
|
||||||
dump(InfoTab, client)
|
dump(InfoTab, client)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
'gateway-clients'(["lookup", Name, ClientId]) ->
|
'gateway-clients'(["lookup", Name, ClientId]) ->
|
||||||
ChanTab = emqx_gateway_cm:tabname(chan, Name),
|
ChanTab = emqx_gateway_cm:tabname(chan, Name),
|
||||||
case ets:info(ChanTab) of
|
case ets:info(ChanTab) of
|
||||||
|
|
@ -151,28 +151,25 @@ gateway(_) ->
|
||||||
print("Bad Gateway Name.\n");
|
print("Bad Gateway Name.\n");
|
||||||
_ ->
|
_ ->
|
||||||
case ets:lookup(ChanTab, bin(ClientId)) of
|
case ets:lookup(ChanTab, bin(ClientId)) of
|
||||||
[] -> print("Not Found.\n");
|
[] ->
|
||||||
|
print("Not Found.\n");
|
||||||
[Chann] ->
|
[Chann] ->
|
||||||
InfoTab = emqx_gateway_cm:tabname(info, Name),
|
InfoTab = emqx_gateway_cm:tabname(info, Name),
|
||||||
[ChannInfo] = ets:lookup(InfoTab, Chann),
|
[ChannInfo] = ets:lookup(InfoTab, Chann),
|
||||||
print_record({client, ChannInfo})
|
print_record({client, ChannInfo})
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
|
|
||||||
'gateway-clients'(["kick", Name, ClientId]) ->
|
'gateway-clients'(["kick", Name, ClientId]) ->
|
||||||
case emqx_gateway_cm:kick_session(Name, bin(ClientId)) of
|
case emqx_gateway_cm:kick_session(Name, bin(ClientId)) of
|
||||||
ok -> print("ok\n");
|
ok -> print("ok\n");
|
||||||
_ -> print("Not Found.\n")
|
_ -> print("Not Found.\n")
|
||||||
end;
|
end;
|
||||||
|
|
||||||
'gateway-clients'(_) ->
|
'gateway-clients'(_) ->
|
||||||
emqx_ctl:usage([ {"gateway-clients list <Name>",
|
emqx_ctl:usage([
|
||||||
"List all clients for a gateway"}
|
{"gateway-clients list <Name>", "List all clients for a gateway"},
|
||||||
, {"gateway-clients lookup <Name> <ClientId>",
|
{"gateway-clients lookup <Name> <ClientId>", "Lookup the Client Info for specified client"},
|
||||||
"Lookup the Client Info for specified client"}
|
{"gateway-clients kick <Name> <ClientId>", "Kick out a client"}
|
||||||
, {"gateway-clients kick <Name> <ClientId>",
|
]).
|
||||||
"Kick out a client"}
|
|
||||||
]).
|
|
||||||
|
|
||||||
'gateway-metrics'([Name]) ->
|
'gateway-metrics'([Name]) ->
|
||||||
case emqx_gateway_metrics:lookup(atom(Name)) of
|
case emqx_gateway_metrics:lookup(atom(Name)) of
|
||||||
|
|
@ -180,20 +177,18 @@ gateway(_) ->
|
||||||
print("Bad Gateway Name.\n");
|
print("Bad Gateway Name.\n");
|
||||||
Metrics ->
|
Metrics ->
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun({K, V}) -> print("~-30s: ~w\n", [K, V]) end,
|
fun({K, V}) -> print("~-30s: ~w\n", [K, V]) end,
|
||||||
Metrics)
|
Metrics
|
||||||
|
)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
'gateway-metrics'(_) ->
|
'gateway-metrics'(_) ->
|
||||||
emqx_ctl:usage([ {"gateway-metrics <Name>",
|
emqx_ctl:usage([{"gateway-metrics <Name>", "List all metrics for a gateway"}]).
|
||||||
"List all metrics for a gateway"}
|
|
||||||
]).
|
|
||||||
|
|
||||||
atom(Id) ->
|
atom(Id) ->
|
||||||
try
|
try
|
||||||
list_to_existing_atom(Id)
|
list_to_existing_atom(Id)
|
||||||
catch
|
catch
|
||||||
_ : _ -> undefined
|
_:_ -> undefined
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -207,77 +202,103 @@ dump(Table, Tag) ->
|
||||||
|
|
||||||
dump(_Table, _, '$end_of_table', Result) ->
|
dump(_Table, _, '$end_of_table', Result) ->
|
||||||
lists:reverse(Result);
|
lists:reverse(Result);
|
||||||
|
|
||||||
dump(Table, Tag, Key, Result) ->
|
dump(Table, Tag, Key, Result) ->
|
||||||
PrintValue = [print_record({Tag, Record}) || Record <- ets:lookup(Table, Key)],
|
PrintValue = [print_record({Tag, Record}) || Record <- ets:lookup(Table, Key)],
|
||||||
dump(Table, Tag, ets:next(Table, Key), [PrintValue | Result]).
|
dump(Table, Tag, ets:next(Table, Key), [PrintValue | Result]).
|
||||||
|
|
||||||
print_record({client, {_, Infos, Stats}}) ->
|
print_record({client, {_, Infos, Stats}}) ->
|
||||||
ClientInfo = maps:get(clientinfo, Infos, #{}),
|
ClientInfo = maps:get(clientinfo, Infos, #{}),
|
||||||
ConnInfo = maps:get(conninfo, Infos, #{}),
|
ConnInfo = maps:get(conninfo, Infos, #{}),
|
||||||
_Session = maps:get(session, Infos, #{}),
|
_Session = maps:get(session, Infos, #{}),
|
||||||
SafeGet = fun(K, M) -> maps:get(K, M, undefined) end,
|
SafeGet = fun(K, M) -> maps:get(K, M, undefined) end,
|
||||||
StatsGet = fun(K) -> proplists:get_value(K, Stats, 0) end,
|
StatsGet = fun(K) -> proplists:get_value(K, Stats, 0) end,
|
||||||
|
|
||||||
ConnectedAt = SafeGet(connected_at, ConnInfo),
|
ConnectedAt = SafeGet(connected_at, ConnInfo),
|
||||||
InfoKeys = [clientid, username, peername, clean_start, keepalive,
|
InfoKeys = [
|
||||||
subscriptions_cnt, send_msg, connected, created_at, connected_at],
|
clientid,
|
||||||
Info = #{ clientid => SafeGet(clientid, ClientInfo),
|
username,
|
||||||
username => SafeGet(username, ClientInfo),
|
peername,
|
||||||
peername => SafeGet(peername, ConnInfo),
|
clean_start,
|
||||||
clean_start => SafeGet(clean_start, ConnInfo),
|
keepalive,
|
||||||
keepalive => SafeGet(keepalive, ConnInfo),
|
subscriptions_cnt,
|
||||||
subscriptions_cnt => StatsGet(subscriptions_cnt),
|
send_msg,
|
||||||
send_msg => StatsGet(send_msg),
|
connected,
|
||||||
connected => SafeGet(conn_state, Infos) == connected,
|
created_at,
|
||||||
created_at => ConnectedAt,
|
connected_at
|
||||||
connected_at => ConnectedAt
|
],
|
||||||
},
|
Info = #{
|
||||||
|
clientid => SafeGet(clientid, ClientInfo),
|
||||||
|
username => SafeGet(username, ClientInfo),
|
||||||
|
peername => SafeGet(peername, ConnInfo),
|
||||||
|
clean_start => SafeGet(clean_start, ConnInfo),
|
||||||
|
keepalive => SafeGet(keepalive, ConnInfo),
|
||||||
|
subscriptions_cnt => StatsGet(subscriptions_cnt),
|
||||||
|
send_msg => StatsGet(send_msg),
|
||||||
|
connected => SafeGet(conn_state, Infos) == connected,
|
||||||
|
created_at => ConnectedAt,
|
||||||
|
connected_at => ConnectedAt
|
||||||
|
},
|
||||||
|
|
||||||
print("Client(~ts, username=~ts, peername=~ts, "
|
print(
|
||||||
"clean_start=~ts, keepalive=~w, "
|
"Client(~ts, username=~ts, peername=~ts, "
|
||||||
"subscriptions=~w, delivered_msgs=~w, "
|
"clean_start=~ts, keepalive=~w, "
|
||||||
"connected=~ts, created_at=~w, connected_at=~w)\n",
|
"subscriptions=~w, delivered_msgs=~w, "
|
||||||
[format(K, maps:get(K, Info)) || K <- InfoKeys]).
|
"connected=~ts, created_at=~w, connected_at=~w)\n",
|
||||||
|
[format(K, maps:get(K, Info)) || K <- InfoKeys]
|
||||||
|
).
|
||||||
|
|
||||||
print(S) -> emqx_ctl:print(S).
|
print(S) -> emqx_ctl:print(S).
|
||||||
print(S, A) -> emqx_ctl:print(S, A).
|
print(S, A) -> emqx_ctl:print(S, A).
|
||||||
|
|
||||||
format(_, undefined) ->
|
format(_, undefined) ->
|
||||||
undefined;
|
undefined;
|
||||||
|
|
||||||
format(peername, {IPAddr, Port}) ->
|
format(peername, {IPAddr, Port}) ->
|
||||||
IPStr = emqx_mgmt_util:ntoa(IPAddr),
|
IPStr = emqx_mgmt_util:ntoa(IPAddr),
|
||||||
io_lib:format("~ts:~p", [IPStr, Port]);
|
io_lib:format("~ts:~p", [IPStr, Port]);
|
||||||
|
|
||||||
format(_, Val) ->
|
format(_, Val) ->
|
||||||
Val.
|
Val.
|
||||||
|
|
||||||
format_gw_summary(#{name := Name, status := unloaded}) ->
|
format_gw_summary(#{name := Name, status := unloaded}) ->
|
||||||
io_lib:format("Gateway(name=~ts, status=unloaded)\n", [Name]);
|
io_lib:format("Gateway(name=~ts, status=unloaded)\n", [Name]);
|
||||||
|
format_gw_summary(#{
|
||||||
|
name := Name,
|
||||||
|
status := stopped,
|
||||||
|
stopped_at := StoppedAt
|
||||||
|
}) ->
|
||||||
|
io_lib:format(
|
||||||
|
"Gateway(name=~ts, status=stopped, stopped_at=~ts)\n",
|
||||||
|
[Name, StoppedAt]
|
||||||
|
);
|
||||||
|
format_gw_summary(#{
|
||||||
|
name := Name,
|
||||||
|
status := running,
|
||||||
|
current_connections := ConnCnt,
|
||||||
|
started_at := StartedAt
|
||||||
|
}) ->
|
||||||
|
io_lib:format(
|
||||||
|
"Gateway(name=~ts, status=running, clients=~w, "
|
||||||
|
"started_at=~ts)\n",
|
||||||
|
[Name, ConnCnt, StartedAt]
|
||||||
|
).
|
||||||
|
|
||||||
format_gw_summary(#{name := Name, status := stopped,
|
format_gateway(#{
|
||||||
stopped_at := StoppedAt}) ->
|
name := Name,
|
||||||
io_lib:format("Gateway(name=~ts, status=stopped, stopped_at=~ts)\n",
|
status := unloaded
|
||||||
[Name, StoppedAt]);
|
}) ->
|
||||||
format_gw_summary(#{name := Name, status := running,
|
|
||||||
current_connections := ConnCnt,
|
|
||||||
started_at := StartedAt}) ->
|
|
||||||
io_lib:format("Gateway(name=~ts, status=running, clients=~w, "
|
|
||||||
"started_at=~ts)\n", [Name, ConnCnt, StartedAt]).
|
|
||||||
|
|
||||||
format_gateway(#{name := Name,
|
|
||||||
status := unloaded}) ->
|
|
||||||
io_lib:format(
|
io_lib:format(
|
||||||
"name: ~ts\n"
|
"name: ~ts\n"
|
||||||
"status: unloaded\n", [Name]);
|
"status: unloaded\n",
|
||||||
|
[Name]
|
||||||
format_gateway(Gw =
|
);
|
||||||
#{name := Name,
|
format_gateway(
|
||||||
status := Status,
|
Gw =
|
||||||
created_at := CreatedAt,
|
#{
|
||||||
config := Config
|
name := Name,
|
||||||
}) ->
|
status := Status,
|
||||||
|
created_at := CreatedAt,
|
||||||
|
config := Config
|
||||||
|
}
|
||||||
|
) ->
|
||||||
{StopOrStart, Timestamp} =
|
{StopOrStart, Timestamp} =
|
||||||
case Status of
|
case Status of
|
||||||
stopped -> {stopped_at, maps:get(stopped_at, Gw)};
|
stopped -> {stopped_at, maps:get(stopped_at, Gw)};
|
||||||
|
|
@ -289,10 +310,15 @@ format_gateway(Gw =
|
||||||
"created_at: ~ts\n"
|
"created_at: ~ts\n"
|
||||||
"~ts: ~ts\n"
|
"~ts: ~ts\n"
|
||||||
"config: ~p\n",
|
"config: ~p\n",
|
||||||
[Name, Status,
|
[
|
||||||
emqx_gateway_utils:unix_ts_to_rfc3339(CreatedAt),
|
Name,
|
||||||
StopOrStart, emqx_gateway_utils:unix_ts_to_rfc3339(Timestamp),
|
Status,
|
||||||
Config]).
|
emqx_gateway_utils:unix_ts_to_rfc3339(CreatedAt),
|
||||||
|
StopOrStart,
|
||||||
|
emqx_gateway_utils:unix_ts_to_rfc3339(Timestamp),
|
||||||
|
Config
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
||||||
format_error(Reason) ->
|
format_error(Reason) ->
|
||||||
case emqx_gateway_http:reason2msg(Reason) of
|
case emqx_gateway_http:reason2msg(Reason) of
|
||||||
|
|
|
||||||
|
|
@ -30,70 +30,77 @@
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([start_link/1]).
|
-export([start_link/1]).
|
||||||
|
|
||||||
-export([ open_session/5
|
-export([
|
||||||
, open_session/6
|
open_session/5,
|
||||||
, kick_session/2
|
open_session/6,
|
||||||
, kick_session/3
|
kick_session/2,
|
||||||
, takeover_session/2
|
kick_session/3,
|
||||||
, register_channel/4
|
takeover_session/2,
|
||||||
, unregister_channel/2
|
register_channel/4,
|
||||||
, insert_channel_info/4
|
unregister_channel/2,
|
||||||
, lookup_by_clientid/2
|
insert_channel_info/4,
|
||||||
, set_chan_info/3
|
lookup_by_clientid/2,
|
||||||
, set_chan_info/4
|
set_chan_info/3,
|
||||||
, get_chan_info/2
|
set_chan_info/4,
|
||||||
, get_chan_info/3
|
get_chan_info/2,
|
||||||
, set_chan_stats/3
|
get_chan_info/3,
|
||||||
, set_chan_stats/4
|
set_chan_stats/3,
|
||||||
, get_chan_stats/2
|
set_chan_stats/4,
|
||||||
, get_chan_stats/3
|
get_chan_stats/2,
|
||||||
, connection_closed/2
|
get_chan_stats/3,
|
||||||
]).
|
connection_closed/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ call/3
|
-export([
|
||||||
, call/4
|
call/3,
|
||||||
, cast/3
|
call/4,
|
||||||
]).
|
cast/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ with_channel/3
|
-export([
|
||||||
, lookup_channels/2
|
with_channel/3,
|
||||||
]).
|
lookup_channels/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% Internal funcs for getting tabname by GatewayId
|
%% Internal funcs for getting tabname by GatewayId
|
||||||
-export([cmtabs/1, tabname/2]).
|
-export([cmtabs/1, tabname/2]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
%% RPC targets
|
%% RPC targets
|
||||||
-export([ do_lookup_by_clientid/2
|
-export([
|
||||||
, do_get_chan_info/3
|
do_lookup_by_clientid/2,
|
||||||
, do_set_chan_info/4
|
do_get_chan_info/3,
|
||||||
, do_get_chan_stats/3
|
do_set_chan_info/4,
|
||||||
, do_set_chan_stats/4
|
do_get_chan_stats/3,
|
||||||
, do_kick_session/4
|
do_set_chan_stats/4,
|
||||||
, do_takeover_session/3
|
do_kick_session/4,
|
||||||
, do_get_chann_conn_mod/3
|
do_takeover_session/3,
|
||||||
, do_call/4
|
do_get_chann_conn_mod/3,
|
||||||
, do_call/5
|
do_call/4,
|
||||||
, do_cast/4
|
do_call/5,
|
||||||
]).
|
do_cast/4
|
||||||
|
]).
|
||||||
|
|
||||||
-export_type([ gateway_name/0
|
-export_type([gateway_name/0]).
|
||||||
]).
|
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
gwname :: gateway_name(), %% Gateway Name
|
%% Gateway Name
|
||||||
locker :: pid(), %% ClientId Locker for CM
|
gwname :: gateway_name(),
|
||||||
registry :: pid(), %% ClientId Registry server
|
%% ClientId Locker for CM
|
||||||
chan_pmon :: emqx_pmon:pmon()
|
locker :: pid(),
|
||||||
}).
|
%% ClientId Registry server
|
||||||
|
registry :: pid(),
|
||||||
|
chan_pmon :: emqx_pmon:pmon()
|
||||||
|
}).
|
||||||
|
|
||||||
-type option() :: {gwname, gateway_name()}.
|
-type option() :: {gwname, gateway_name()}.
|
||||||
-type options() :: list(option()).
|
-type options() :: list(option()).
|
||||||
|
|
@ -117,14 +124,16 @@ start_link(Options) ->
|
||||||
procname(GwName) ->
|
procname(GwName) ->
|
||||||
list_to_atom(lists:concat([emqx_gateway_, GwName, '_cm'])).
|
list_to_atom(lists:concat([emqx_gateway_, GwName, '_cm'])).
|
||||||
|
|
||||||
-spec cmtabs(GwName :: gateway_name())
|
-spec cmtabs(GwName :: gateway_name()) ->
|
||||||
-> {ChanTab :: atom(),
|
{ChanTab :: atom(), ConnTab :: atom(), ChannInfoTab :: atom()}.
|
||||||
ConnTab :: atom(),
|
|
||||||
ChannInfoTab :: atom()}.
|
|
||||||
cmtabs(GwName) ->
|
cmtabs(GwName) ->
|
||||||
{ tabname(chan, GwName) %% Record: {ClientId, Pid}
|
%% Record: {ClientId, Pid}
|
||||||
, tabname(conn, GwName) %% Record: {{ClientId, Pid}, ConnMod}
|
{
|
||||||
, tabname(info, GwName) %% Record: {{ClientId, Pid}, Info, Stats}
|
tabname(chan, GwName),
|
||||||
|
%% Record: {{ClientId, Pid}, ConnMod}
|
||||||
|
tabname(conn, GwName),
|
||||||
|
%% Record: {{ClientId, Pid}, Info, Stats}
|
||||||
|
tabname(info, GwName)
|
||||||
}.
|
}.
|
||||||
|
|
||||||
tabname(chan, GwName) ->
|
tabname(chan, GwName) ->
|
||||||
|
|
@ -137,10 +146,12 @@ tabname(info, GwName) ->
|
||||||
lockername(GwName) ->
|
lockername(GwName) ->
|
||||||
list_to_atom(lists:concat([emqx_gateway_, GwName, '_locker'])).
|
list_to_atom(lists:concat([emqx_gateway_, GwName, '_locker'])).
|
||||||
|
|
||||||
-spec register_channel(gateway_name(),
|
-spec register_channel(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
pid(),
|
emqx_types:clientid(),
|
||||||
emqx_types:conninfo()) -> ok.
|
pid(),
|
||||||
|
emqx_types:conninfo()
|
||||||
|
) -> ok.
|
||||||
register_channel(GwName, ClientId, ChanPid, #{conn_mod := ConnMod}) when is_pid(ChanPid) ->
|
register_channel(GwName, ClientId, ChanPid, #{conn_mod := ConnMod}) when is_pid(ChanPid) ->
|
||||||
Chan = {ClientId, ChanPid},
|
Chan = {ClientId, ChanPid},
|
||||||
true = ets:insert(tabname(chan, GwName), Chan),
|
true = ets:insert(tabname(chan, GwName), Chan),
|
||||||
|
|
@ -155,31 +166,36 @@ unregister_channel(GwName, ClientId) when is_binary(ClientId) ->
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% @doc Insert/Update the channel info and stats
|
%% @doc Insert/Update the channel info and stats
|
||||||
-spec insert_channel_info(gateway_name(),
|
-spec insert_channel_info(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
emqx_types:infos(),
|
emqx_types:clientid(),
|
||||||
emqx_types:stats()) -> ok.
|
emqx_types:infos(),
|
||||||
|
emqx_types:stats()
|
||||||
|
) -> ok.
|
||||||
insert_channel_info(GwName, ClientId, Info, Stats) ->
|
insert_channel_info(GwName, ClientId, Info, Stats) ->
|
||||||
Chan = {ClientId, self()},
|
Chan = {ClientId, self()},
|
||||||
true = ets:insert(tabname(info, GwName), {Chan, Info, Stats}),
|
true = ets:insert(tabname(info, GwName), {Chan, Info, Stats}),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
%% @doc Get info of a channel.
|
%% @doc Get info of a channel.
|
||||||
-spec get_chan_info(gateway_name(), emqx_types:clientid())
|
-spec get_chan_info(gateway_name(), emqx_types:clientid()) ->
|
||||||
-> emqx_types:infos() | undefined.
|
emqx_types:infos() | undefined.
|
||||||
get_chan_info(GwName, ClientId) ->
|
get_chan_info(GwName, ClientId) ->
|
||||||
with_channel(GwName, ClientId,
|
with_channel(
|
||||||
|
GwName,
|
||||||
|
ClientId,
|
||||||
fun(ChanPid) ->
|
fun(ChanPid) ->
|
||||||
get_chan_info(GwName, ClientId, ChanPid)
|
get_chan_info(GwName, ClientId, ChanPid)
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
-spec do_lookup_by_clientid(gateway_name(), emqx_types:clientid()) -> [pid()].
|
-spec do_lookup_by_clientid(gateway_name(), emqx_types:clientid()) -> [pid()].
|
||||||
do_lookup_by_clientid(GwName, ClientId) ->
|
do_lookup_by_clientid(GwName, ClientId) ->
|
||||||
ChanTab = emqx_gateway_cm:tabname(chan, GwName),
|
ChanTab = emqx_gateway_cm:tabname(chan, GwName),
|
||||||
[Pid || {_, Pid} <- ets:lookup(ChanTab, ClientId)].
|
[Pid || {_, Pid} <- ets:lookup(ChanTab, ClientId)].
|
||||||
|
|
||||||
-spec do_get_chan_info(gateway_name(), emqx_types:clientid(), pid())
|
-spec do_get_chan_info(gateway_name(), emqx_types:clientid(), pid()) ->
|
||||||
-> emqx_types:infos() | undefined.
|
emqx_types:infos() | undefined.
|
||||||
do_get_chan_info(GwName, ClientId, ChanPid) ->
|
do_get_chan_info(GwName, ClientId, ChanPid) ->
|
||||||
Chan = {ClientId, ChanPid},
|
Chan = {ClientId, ChanPid},
|
||||||
try
|
try
|
||||||
|
|
@ -189,18 +205,21 @@ do_get_chan_info(GwName, ClientId, ChanPid) ->
|
||||||
error:badarg -> undefined
|
error:badarg -> undefined
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec get_chan_info(gateway_name(), emqx_types:clientid(), pid())
|
-spec get_chan_info(gateway_name(), emqx_types:clientid(), pid()) ->
|
||||||
-> emqx_types:infos() | undefined.
|
emqx_types:infos() | undefined.
|
||||||
get_chan_info(GwName, ClientId, ChanPid) ->
|
get_chan_info(GwName, ClientId, ChanPid) ->
|
||||||
wrap_rpc(
|
wrap_rpc(
|
||||||
emqx_gateway_cm_proto_v1:get_chan_info(GwName, ClientId, ChanPid)
|
emqx_gateway_cm_proto_v1:get_chan_info(GwName, ClientId, ChanPid)
|
||||||
).
|
).
|
||||||
|
|
||||||
-spec lookup_by_clientid(gateway_name(), emqx_types:clientid()) -> [pid()].
|
-spec lookup_by_clientid(gateway_name(), emqx_types:clientid()) -> [pid()].
|
||||||
lookup_by_clientid(GwName, ClientId) ->
|
lookup_by_clientid(GwName, ClientId) ->
|
||||||
Nodes = mria_mnesia:running_nodes(),
|
Nodes = mria_mnesia:running_nodes(),
|
||||||
case emqx_gateway_cm_proto_v1:lookup_by_clientid(
|
case
|
||||||
Nodes, GwName, ClientId) of
|
emqx_gateway_cm_proto_v1:lookup_by_clientid(
|
||||||
|
Nodes, GwName, ClientId
|
||||||
|
)
|
||||||
|
of
|
||||||
{Pids, []} ->
|
{Pids, []} ->
|
||||||
lists:append(Pids);
|
lists:append(Pids);
|
||||||
{_, _BadNodes} ->
|
{_, _BadNodes} ->
|
||||||
|
|
@ -208,74 +227,92 @@ lookup_by_clientid(GwName, ClientId) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @doc Update infos of the channel.
|
%% @doc Update infos of the channel.
|
||||||
-spec set_chan_info(gateway_name(),
|
-spec set_chan_info(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
emqx_types:infos()) -> boolean().
|
emqx_types:clientid(),
|
||||||
|
emqx_types:infos()
|
||||||
|
) -> boolean().
|
||||||
set_chan_info(GwName, ClientId, Infos) ->
|
set_chan_info(GwName, ClientId, Infos) ->
|
||||||
set_chan_info(GwName, ClientId, self(), Infos).
|
set_chan_info(GwName, ClientId, self(), Infos).
|
||||||
|
|
||||||
-spec do_set_chan_info(gateway_name(),
|
-spec do_set_chan_info(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
pid(),
|
emqx_types:clientid(),
|
||||||
emqx_types:infos()) -> boolean().
|
pid(),
|
||||||
|
emqx_types:infos()
|
||||||
|
) -> boolean().
|
||||||
do_set_chan_info(GwName, ClientId, ChanPid, Infos) ->
|
do_set_chan_info(GwName, ClientId, ChanPid, Infos) ->
|
||||||
Chan = {ClientId, ChanPid},
|
Chan = {ClientId, ChanPid},
|
||||||
try ets:update_element(tabname(info, GwName), Chan, {2, Infos})
|
try
|
||||||
|
ets:update_element(tabname(info, GwName), Chan, {2, Infos})
|
||||||
catch
|
catch
|
||||||
error:badarg -> false
|
error:badarg -> false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec set_chan_info(gateway_name(),
|
-spec set_chan_info(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
pid(),
|
emqx_types:clientid(),
|
||||||
emqx_types:infos()) -> boolean().
|
pid(),
|
||||||
|
emqx_types:infos()
|
||||||
|
) -> boolean().
|
||||||
set_chan_info(GwName, ClientId, ChanPid, Infos) ->
|
set_chan_info(GwName, ClientId, ChanPid, Infos) ->
|
||||||
wrap_rpc(emqx_gateway_cm_proto_v1:set_chan_info(GwName, ClientId, ChanPid, Infos)).
|
wrap_rpc(emqx_gateway_cm_proto_v1:set_chan_info(GwName, ClientId, ChanPid, Infos)).
|
||||||
|
|
||||||
%% @doc Get channel's stats.
|
%% @doc Get channel's stats.
|
||||||
-spec get_chan_stats(gateway_name(), emqx_types:clientid())
|
-spec get_chan_stats(gateway_name(), emqx_types:clientid()) ->
|
||||||
-> emqx_types:stats() | undefined.
|
emqx_types:stats() | undefined.
|
||||||
get_chan_stats(GwName, ClientId) ->
|
get_chan_stats(GwName, ClientId) ->
|
||||||
with_channel(GwName, ClientId,
|
with_channel(
|
||||||
|
GwName,
|
||||||
|
ClientId,
|
||||||
fun(ChanPid) ->
|
fun(ChanPid) ->
|
||||||
get_chan_stats(GwName, ClientId, ChanPid)
|
get_chan_stats(GwName, ClientId, ChanPid)
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
-spec do_get_chan_stats(gateway_name(), emqx_types:clientid(), pid())
|
-spec do_get_chan_stats(gateway_name(), emqx_types:clientid(), pid()) ->
|
||||||
-> emqx_types:stats() | undefined.
|
emqx_types:stats() | undefined.
|
||||||
do_get_chan_stats(GwName, ClientId, ChanPid) ->
|
do_get_chan_stats(GwName, ClientId, ChanPid) ->
|
||||||
Chan = {ClientId, ChanPid},
|
Chan = {ClientId, ChanPid},
|
||||||
try ets:lookup_element(tabname(info, GwName), Chan, 3)
|
try
|
||||||
|
ets:lookup_element(tabname(info, GwName), Chan, 3)
|
||||||
catch
|
catch
|
||||||
error:badarg -> undefined
|
error:badarg -> undefined
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec get_chan_stats(gateway_name(), emqx_types:clientid(), pid())
|
-spec get_chan_stats(gateway_name(), emqx_types:clientid(), pid()) ->
|
||||||
-> emqx_types:stats() | undefined.
|
emqx_types:stats() | undefined.
|
||||||
get_chan_stats(GwName, ClientId, ChanPid) ->
|
get_chan_stats(GwName, ClientId, ChanPid) ->
|
||||||
wrap_rpc(emqx_gateway_cm_proto_v1:get_chan_stats(GwName, ClientId, ChanPid)).
|
wrap_rpc(emqx_gateway_cm_proto_v1:get_chan_stats(GwName, ClientId, ChanPid)).
|
||||||
|
|
||||||
-spec set_chan_stats(gateway_name(),
|
-spec set_chan_stats(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
emqx_types:stats()) -> boolean().
|
emqx_types:clientid(),
|
||||||
|
emqx_types:stats()
|
||||||
|
) -> boolean().
|
||||||
set_chan_stats(GwName, ClientId, Stats) ->
|
set_chan_stats(GwName, ClientId, Stats) ->
|
||||||
set_chan_stats(GwName, ClientId, self(), Stats).
|
set_chan_stats(GwName, ClientId, self(), Stats).
|
||||||
|
|
||||||
-spec do_set_chan_stats(gateway_name(),
|
-spec do_set_chan_stats(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
pid(),
|
emqx_types:clientid(),
|
||||||
emqx_types:stats()) -> boolean().
|
pid(),
|
||||||
|
emqx_types:stats()
|
||||||
|
) -> boolean().
|
||||||
do_set_chan_stats(GwName, ClientId, ChanPid, Stats) ->
|
do_set_chan_stats(GwName, ClientId, ChanPid, Stats) ->
|
||||||
Chan = {ClientId, ChanPid},
|
Chan = {ClientId, ChanPid},
|
||||||
try ets:update_element(tabname(info, GwName), Chan, {3, Stats})
|
try
|
||||||
|
ets:update_element(tabname(info, GwName), Chan, {3, Stats})
|
||||||
catch
|
catch
|
||||||
error:badarg -> false
|
error:badarg -> false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec set_chan_stats(gateway_name(),
|
-spec set_chan_stats(
|
||||||
emqx_types:clientid(),
|
gateway_name(),
|
||||||
pid(),
|
emqx_types:clientid(),
|
||||||
emqx_types:stats()) -> boolean().
|
pid(),
|
||||||
|
emqx_types:stats()
|
||||||
|
) -> boolean().
|
||||||
set_chan_stats(GwName, ClientId, ChanPid, Stats) ->
|
set_chan_stats(GwName, ClientId, ChanPid, Stats) ->
|
||||||
wrap_rpc(emqx_gateway_cm_proto_v1:set_chan_stats(GwName, ClientId, ChanPid, Stats)).
|
wrap_rpc(emqx_gateway_cm_proto_v1:set_chan_stats(GwName, ClientId, ChanPid, Stats)).
|
||||||
|
|
||||||
|
|
@ -285,18 +322,24 @@ connection_closed(GwName, ClientId) ->
|
||||||
Chan = {ClientId, self()},
|
Chan = {ClientId, self()},
|
||||||
ets:delete_object(tabname(conn, GwName), Chan).
|
ets:delete_object(tabname(conn, GwName), Chan).
|
||||||
|
|
||||||
-spec open_session(GwName :: gateway_name(),
|
-spec open_session(
|
||||||
CleanStart :: boolean(),
|
GwName :: gateway_name(),
|
||||||
ClientInfo :: emqx_types:clientinfo(),
|
CleanStart :: boolean(),
|
||||||
ConnInfo :: emqx_types:conninfo(),
|
ClientInfo :: emqx_types:clientinfo(),
|
||||||
CreateSessionFun :: fun((emqx_types:clientinfo(),
|
ConnInfo :: emqx_types:conninfo(),
|
||||||
emqx_types:conninfo()) -> Session
|
CreateSessionFun :: fun(
|
||||||
))
|
(
|
||||||
-> {ok, #{session := Session,
|
emqx_types:clientinfo(),
|
||||||
present := boolean(),
|
emqx_types:conninfo()
|
||||||
pendings => list()
|
) -> Session
|
||||||
}}
|
)
|
||||||
| {error, any()}.
|
) ->
|
||||||
|
{ok, #{
|
||||||
|
session := Session,
|
||||||
|
present := boolean(),
|
||||||
|
pendings => list()
|
||||||
|
}}
|
||||||
|
| {error, any()}.
|
||||||
|
|
||||||
open_session(GwName, CleanStart, ClientInfo, ConnInfo, CreateSessionFun) ->
|
open_session(GwName, CleanStart, ClientInfo, ConnInfo, CreateSessionFun) ->
|
||||||
open_session(GwName, CleanStart, ClientInfo, ConnInfo, CreateSessionFun, emqx_session).
|
open_session(GwName, CleanStart, ClientInfo, ConnInfo, CreateSessionFun, emqx_session).
|
||||||
|
|
@ -305,21 +348,26 @@ open_session(GwName, true = _CleanStart, ClientInfo, ConnInfo, CreateSessionFun,
|
||||||
Self = self(),
|
Self = self(),
|
||||||
ClientId = maps:get(clientid, ClientInfo),
|
ClientId = maps:get(clientid, ClientInfo),
|
||||||
Fun = fun(_) ->
|
Fun = fun(_) ->
|
||||||
_ = discard_session(GwName, ClientId),
|
_ = discard_session(GwName, ClientId),
|
||||||
Session = create_session(GwName,
|
Session = create_session(
|
||||||
ClientInfo,
|
GwName,
|
||||||
ConnInfo,
|
ClientInfo,
|
||||||
CreateSessionFun,
|
ConnInfo,
|
||||||
SessionMod
|
CreateSessionFun,
|
||||||
),
|
SessionMod
|
||||||
register_channel(GwName, ClientId, Self, ConnInfo),
|
),
|
||||||
{ok, #{session => Session, present => false}}
|
register_channel(GwName, ClientId, Self, ConnInfo),
|
||||||
end,
|
{ok, #{session => Session, present => false}}
|
||||||
|
end,
|
||||||
locker_trans(GwName, ClientId, Fun);
|
locker_trans(GwName, ClientId, Fun);
|
||||||
|
open_session(
|
||||||
open_session(GwName, false = _CleanStart,
|
GwName,
|
||||||
ClientInfo = #{clientid := ClientId},
|
false = _CleanStart,
|
||||||
ConnInfo, CreateSessionFun, SessionMod) ->
|
ClientInfo = #{clientid := ClientId},
|
||||||
|
ConnInfo,
|
||||||
|
CreateSessionFun,
|
||||||
|
SessionMod
|
||||||
|
) ->
|
||||||
Self = self(),
|
Self = self(),
|
||||||
|
|
||||||
ResumeStart =
|
ResumeStart =
|
||||||
|
|
@ -327,79 +375,98 @@ open_session(GwName, false = _CleanStart,
|
||||||
CreateSess =
|
CreateSess =
|
||||||
fun() ->
|
fun() ->
|
||||||
Session = create_session(
|
Session = create_session(
|
||||||
GwName, ClientInfo, ConnInfo,
|
GwName,
|
||||||
CreateSessionFun, SessionMod),
|
ClientInfo,
|
||||||
register_channel(
|
ConnInfo,
|
||||||
GwName, ClientId, Self, ConnInfo),
|
CreateSessionFun,
|
||||||
{ok, #{session => Session, present => false}}
|
SessionMod
|
||||||
|
),
|
||||||
|
register_channel(
|
||||||
|
GwName, ClientId, Self, ConnInfo
|
||||||
|
),
|
||||||
|
{ok, #{session => Session, present => false}}
|
||||||
end,
|
end,
|
||||||
case takeover_session(GwName, ClientId) of
|
case takeover_session(GwName, ClientId) of
|
||||||
{ok, ConnMod, ChanPid, Session} ->
|
{ok, ConnMod, ChanPid, Session} ->
|
||||||
ok = emqx_session:resume(ClientInfo, Session),
|
ok = emqx_session:resume(ClientInfo, Session),
|
||||||
case request_stepdown({takeover, 'end'}, ConnMod, ChanPid) of
|
case request_stepdown({takeover, 'end'}, ConnMod, ChanPid) of
|
||||||
{ok, Pendings} ->
|
{ok, Pendings} ->
|
||||||
register_channel(
|
register_channel(
|
||||||
GwName, ClientId, Self, ConnInfo),
|
GwName, ClientId, Self, ConnInfo
|
||||||
{ok, #{session => Session,
|
),
|
||||||
present => true,
|
{ok, #{
|
||||||
pendings => Pendings}};
|
session => Session,
|
||||||
{error, _} ->
|
present => true,
|
||||||
CreateSess()
|
pendings => Pendings
|
||||||
end;
|
}};
|
||||||
{error, _Reason} -> CreateSess()
|
{error, _} ->
|
||||||
end
|
CreateSess()
|
||||||
end,
|
end;
|
||||||
|
{error, _Reason} ->
|
||||||
|
CreateSess()
|
||||||
|
end
|
||||||
|
end,
|
||||||
locker_trans(GwName, ClientId, ResumeStart).
|
locker_trans(GwName, ClientId, ResumeStart).
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
create_session(GwName, ClientInfo, ConnInfo, CreateSessionFun, SessionMod) ->
|
create_session(GwName, ClientInfo, ConnInfo, CreateSessionFun, SessionMod) ->
|
||||||
try
|
try
|
||||||
Session = emqx_gateway_utils:apply(
|
Session = emqx_gateway_utils:apply(
|
||||||
CreateSessionFun,
|
CreateSessionFun,
|
||||||
[ClientInfo, ConnInfo]
|
[ClientInfo, ConnInfo]
|
||||||
),
|
),
|
||||||
ok = emqx_gateway_metrics:inc(GwName, 'session.created'),
|
ok = emqx_gateway_metrics:inc(GwName, 'session.created'),
|
||||||
SessionInfo = case is_tuple(Session)
|
SessionInfo =
|
||||||
andalso element(1, Session) == session of
|
case
|
||||||
true -> SessionMod:info(Session);
|
is_tuple(Session) andalso
|
||||||
_ ->
|
element(1, Session) == session
|
||||||
case is_map(Session) of
|
of
|
||||||
false ->
|
true ->
|
||||||
throw(session_structure_should_be_map);
|
SessionMod:info(Session);
|
||||||
_ ->
|
_ ->
|
||||||
Session
|
case is_map(Session) of
|
||||||
end
|
false ->
|
||||||
end,
|
throw(session_structure_should_be_map);
|
||||||
|
_ ->
|
||||||
|
Session
|
||||||
|
end
|
||||||
|
end,
|
||||||
ok = emqx_hooks:run('session.created', [ClientInfo, SessionInfo]),
|
ok = emqx_hooks:run('session.created', [ClientInfo, SessionInfo]),
|
||||||
Session
|
Session
|
||||||
catch
|
catch
|
||||||
Class : Reason : Stk ->
|
Class:Reason:Stk ->
|
||||||
?SLOG(error, #{ msg => "failed_create_session"
|
?SLOG(error, #{
|
||||||
, clientid => maps:get(clientid, ClientInfo, undefined)
|
msg => "failed_create_session",
|
||||||
, username => maps:get(username, ClientInfo, undefined)
|
clientid => maps:get(clientid, ClientInfo, undefined),
|
||||||
, reason => {Class, Reason}
|
username => maps:get(username, ClientInfo, undefined),
|
||||||
, stacktrace => Stk
|
reason => {Class, Reason},
|
||||||
}),
|
stacktrace => Stk
|
||||||
throw(Reason)
|
}),
|
||||||
|
throw(Reason)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @doc Try to takeover a session.
|
%% @doc Try to takeover a session.
|
||||||
-spec(takeover_session(gateway_name(), emqx_types:clientid())
|
-spec takeover_session(gateway_name(), emqx_types:clientid()) ->
|
||||||
-> {error, term()}
|
{error, term()}
|
||||||
| {ok, atom(), pid(), emqx_session:session()}).
|
| {ok, atom(), pid(), emqx_session:session()}.
|
||||||
takeover_session(GwName, ClientId) ->
|
takeover_session(GwName, ClientId) ->
|
||||||
case lookup_channels(GwName, ClientId) of
|
case lookup_channels(GwName, ClientId) of
|
||||||
[] -> {error, not_found};
|
[] ->
|
||||||
|
{error, not_found};
|
||||||
[ChanPid] ->
|
[ChanPid] ->
|
||||||
do_takeover_session(GwName, ClientId, ChanPid);
|
do_takeover_session(GwName, ClientId, ChanPid);
|
||||||
ChanPids ->
|
ChanPids ->
|
||||||
[ChanPid | StalePids] = lists:reverse(ChanPids),
|
[ChanPid | StalePids] = lists:reverse(ChanPids),
|
||||||
?SLOG(warning, #{ msg => "more_than_one_channel_found"
|
?SLOG(warning, #{
|
||||||
, chan_pids => ChanPids
|
msg => "more_than_one_channel_found",
|
||||||
}),
|
chan_pids => ChanPids
|
||||||
lists:foreach(fun(StalePid) ->
|
}),
|
||||||
catch discard_session(GwName, ClientId, StalePid)
|
lists:foreach(
|
||||||
end, StalePids),
|
fun(StalePid) ->
|
||||||
|
catch discard_session(GwName, ClientId, StalePid)
|
||||||
|
end,
|
||||||
|
StalePids
|
||||||
|
),
|
||||||
do_takeover_session(GwName, ClientId, ChanPid)
|
do_takeover_session(GwName, ClientId, ChanPid)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -432,17 +499,26 @@ discard_session(GwName, ClientId, ChanPid) ->
|
||||||
-spec kick_session(gateway_name(), emqx_types:clientid()) -> ok | {error, not_found}.
|
-spec kick_session(gateway_name(), emqx_types:clientid()) -> ok | {error, not_found}.
|
||||||
kick_session(GwName, ClientId) ->
|
kick_session(GwName, ClientId) ->
|
||||||
case lookup_channels(GwName, ClientId) of
|
case lookup_channels(GwName, ClientId) of
|
||||||
[] -> {error, not_found};
|
[] ->
|
||||||
|
{error, not_found};
|
||||||
ChanPids ->
|
ChanPids ->
|
||||||
ChanPids > 1 andalso begin
|
ChanPids > 1 andalso
|
||||||
?SLOG(warning, #{ msg => "more_than_one_channel_found"
|
begin
|
||||||
, chan_pids => ChanPids
|
?SLOG(
|
||||||
},
|
warning,
|
||||||
#{clientid => ClientId})
|
#{
|
||||||
end,
|
msg => "more_than_one_channel_found",
|
||||||
lists:foreach(fun(Pid) ->
|
chan_pids => ChanPids
|
||||||
_ = kick_session(GwName, ClientId, Pid)
|
},
|
||||||
end, ChanPids)
|
#{clientid => ClientId}
|
||||||
|
)
|
||||||
|
end,
|
||||||
|
lists:foreach(
|
||||||
|
fun(Pid) ->
|
||||||
|
_ = kick_session(GwName, ClientId, Pid)
|
||||||
|
end,
|
||||||
|
ChanPids
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
kick_session(GwName, ClientId, ChanPid) ->
|
kick_session(GwName, ClientId, ChanPid) ->
|
||||||
|
|
@ -453,27 +529,34 @@ kick_session(GwName, Action, ClientId, ChanPid) ->
|
||||||
try
|
try
|
||||||
wrap_rpc(emqx_gateway_cm_proto_v1:kick_session(GwName, Action, ClientId, ChanPid))
|
wrap_rpc(emqx_gateway_cm_proto_v1:kick_session(GwName, Action, ClientId, ChanPid))
|
||||||
catch
|
catch
|
||||||
Error : Reason ->
|
Error:Reason ->
|
||||||
%% This should mostly be RPC failures.
|
%% This should mostly be RPC failures.
|
||||||
%% However, if the node is still running the old version
|
%% However, if the node is still running the old version
|
||||||
%% code (prior to emqx app 4.3.10) some of the RPC handler
|
%% code (prior to emqx app 4.3.10) some of the RPC handler
|
||||||
%% exceptions may get propagated to a new version node
|
%% exceptions may get propagated to a new version node
|
||||||
?SLOG(error, #{ msg => "failed_to_kick_session_on_remote_node"
|
?SLOG(
|
||||||
, node => node(ChanPid)
|
error,
|
||||||
, action => Action
|
#{
|
||||||
, error => Error
|
msg => "failed_to_kick_session_on_remote_node",
|
||||||
, reason => Reason
|
node => node(ChanPid),
|
||||||
},
|
action => Action,
|
||||||
#{clientid => ClientId})
|
error => Error,
|
||||||
|
reason => Reason
|
||||||
|
},
|
||||||
|
#{clientid => ClientId}
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec do_kick_session(gateway_name(),
|
-spec do_kick_session(
|
||||||
kick | discard,
|
gateway_name(),
|
||||||
emqx_types:clientid(),
|
kick | discard,
|
||||||
pid()) -> ok.
|
emqx_types:clientid(),
|
||||||
|
pid()
|
||||||
|
) -> ok.
|
||||||
do_kick_session(GwName, Action, ClientId, ChanPid) ->
|
do_kick_session(GwName, Action, ClientId, ChanPid) ->
|
||||||
case get_chann_conn_mod(GwName, ClientId, ChanPid) of
|
case get_chann_conn_mod(GwName, ClientId, ChanPid) of
|
||||||
undefined -> ok;
|
undefined ->
|
||||||
|
ok;
|
||||||
ConnMod when is_atom(ConnMod) ->
|
ConnMod when is_atom(ConnMod) ->
|
||||||
ok = request_stepdown(Action, ConnMod, ChanPid)
|
ok = request_stepdown(Action, ConnMod, ChanPid)
|
||||||
end.
|
end.
|
||||||
|
|
@ -482,56 +565,67 @@ do_kick_session(GwName, Action, ClientId, ChanPid) ->
|
||||||
%% If failed to response (e.g. timeout) force a kill.
|
%% If failed to response (e.g. timeout) force a kill.
|
||||||
%% Keeping the stale pid around, or returning error or raise an exception
|
%% Keeping the stale pid around, or returning error or raise an exception
|
||||||
%% benefits nobody.
|
%% benefits nobody.
|
||||||
-spec request_stepdown(Action, module(), pid())
|
-spec request_stepdown(Action, module(), pid()) ->
|
||||||
-> ok
|
ok
|
||||||
| {ok, emqx_session:session() | list(emqx_type:deliver())}
|
| {ok, emqx_session:session() | list(emqx_type:deliver())}
|
||||||
| {error, term()}
|
| {error, term()}
|
||||||
when Action :: kick | discard | {takeover, 'begin'} | {takeover, 'end'}.
|
when
|
||||||
|
Action :: kick | discard | {takeover, 'begin'} | {takeover, 'end'}.
|
||||||
request_stepdown(Action, ConnMod, Pid) ->
|
request_stepdown(Action, ConnMod, Pid) ->
|
||||||
Timeout =
|
Timeout =
|
||||||
case Action == kick orelse Action == discard of
|
case Action == kick orelse Action == discard of
|
||||||
true -> ?T_KICK;
|
true -> ?T_KICK;
|
||||||
_ -> ?T_TAKEOVER
|
_ -> ?T_TAKEOVER
|
||||||
end,
|
end,
|
||||||
Return =
|
Return =
|
||||||
%% this is essentailly a gen_server:call implemented in emqx_connection
|
%% this is essentailly a gen_server:call implemented in emqx_connection
|
||||||
%% and emqx_ws_connection.
|
%% and emqx_ws_connection.
|
||||||
%% the handle_call is implemented in emqx_channel
|
%% the handle_call is implemented in emqx_channel
|
||||||
try apply(ConnMod, call, [Pid, Action, Timeout]) of
|
try apply(ConnMod, call, [Pid, Action, Timeout]) of
|
||||||
ok -> ok;
|
ok -> ok;
|
||||||
Reply -> {ok, Reply}
|
Reply -> {ok, Reply}
|
||||||
catch
|
catch
|
||||||
_ : noproc -> % emqx_ws_connection: call
|
% emqx_ws_connection: call
|
||||||
ok = ?tp(debug, "session_already_gone", #{pid => Pid, action => Action}),
|
_:noproc ->
|
||||||
{error, noproc};
|
ok = ?tp(debug, "session_already_gone", #{pid => Pid, action => Action}),
|
||||||
_ : {noproc, _} -> % emqx_connection: gen_server:call
|
{error, noproc};
|
||||||
ok = ?tp(debug, "session_already_gone", #{pid => Pid, action => Action}),
|
% emqx_connection: gen_server:call
|
||||||
{error, noproc};
|
_:{noproc, _} ->
|
||||||
_ : Reason = {shutdown, _} ->
|
ok = ?tp(debug, "session_already_gone", #{pid => Pid, action => Action}),
|
||||||
ok = ?tp(debug, "session_already_shutdown", #{pid => Pid, action => Action}),
|
{error, noproc};
|
||||||
{error, Reason};
|
_:Reason = {shutdown, _} ->
|
||||||
_ : Reason = {{shutdown, _}, _} ->
|
ok = ?tp(debug, "session_already_shutdown", #{pid => Pid, action => Action}),
|
||||||
ok = ?tp(debug, "session_already_shutdown", #{pid => Pid, action => Action}),
|
{error, Reason};
|
||||||
{error, Reason};
|
_:Reason = {{shutdown, _}, _} ->
|
||||||
_ : {timeout, {gen_server, call, _}} ->
|
ok = ?tp(debug, "session_already_shutdown", #{pid => Pid, action => Action}),
|
||||||
?tp(warning, "session_stepdown_request_timeout",
|
{error, Reason};
|
||||||
#{pid => Pid,
|
_:{timeout, {gen_server, call, _}} ->
|
||||||
action => Action,
|
?tp(
|
||||||
stale_channel => stale_channel_info(Pid)
|
warning,
|
||||||
}),
|
"session_stepdown_request_timeout",
|
||||||
ok = force_kill(Pid),
|
#{
|
||||||
{error, timeout};
|
pid => Pid,
|
||||||
_ : Error : St ->
|
action => Action,
|
||||||
?tp(error, "session_stepdown_request_exception",
|
stale_channel => stale_channel_info(Pid)
|
||||||
#{pid => Pid,
|
}
|
||||||
action => Action,
|
),
|
||||||
reason => Error,
|
ok = force_kill(Pid),
|
||||||
stacktrace => St,
|
{error, timeout};
|
||||||
stale_channel => stale_channel_info(Pid)
|
_:Error:St ->
|
||||||
}),
|
?tp(
|
||||||
ok = force_kill(Pid),
|
error,
|
||||||
{error, Error}
|
"session_stepdown_request_exception",
|
||||||
end,
|
#{
|
||||||
|
pid => Pid,
|
||||||
|
action => Action,
|
||||||
|
reason => Error,
|
||||||
|
stacktrace => St,
|
||||||
|
stale_channel => stale_channel_info(Pid)
|
||||||
|
}
|
||||||
|
),
|
||||||
|
ok = force_kill(Pid),
|
||||||
|
{error, Error}
|
||||||
|
end,
|
||||||
case Action == kick orelse Action == discard of
|
case Action == kick orelse Action == discard of
|
||||||
true -> ok;
|
true -> ok;
|
||||||
_ -> Return
|
_ -> Return
|
||||||
|
|
@ -546,20 +640,22 @@ stale_channel_info(Pid) ->
|
||||||
|
|
||||||
with_channel(GwName, ClientId, Fun) ->
|
with_channel(GwName, ClientId, Fun) ->
|
||||||
case lookup_channels(GwName, ClientId) of
|
case lookup_channels(GwName, ClientId) of
|
||||||
[] -> undefined;
|
[] -> undefined;
|
||||||
[Pid] -> Fun(Pid);
|
[Pid] -> Fun(Pid);
|
||||||
Pids -> Fun(lists:last(Pids))
|
Pids -> Fun(lists:last(Pids))
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @doc Lookup channels.
|
%% @doc Lookup channels.
|
||||||
-spec(lookup_channels(gateway_name(), emqx_types:clientid()) -> list(pid())).
|
-spec lookup_channels(gateway_name(), emqx_types:clientid()) -> list(pid()).
|
||||||
lookup_channels(GwName, ClientId) ->
|
lookup_channels(GwName, ClientId) ->
|
||||||
emqx_gateway_cm_registry:lookup_channels(GwName, ClientId).
|
emqx_gateway_cm_registry:lookup_channels(GwName, ClientId).
|
||||||
|
|
||||||
-spec do_get_chann_conn_mod(gateway_name(), emqx_types:clientid(), pid()) -> atom().
|
-spec do_get_chann_conn_mod(gateway_name(), emqx_types:clientid(), pid()) -> atom().
|
||||||
do_get_chann_conn_mod(GwName, ClientId, ChanPid) ->
|
do_get_chann_conn_mod(GwName, ClientId, ChanPid) ->
|
||||||
Chan = {ClientId, ChanPid},
|
Chan = {ClientId, ChanPid},
|
||||||
try [ConnMod] = ets:lookup_element(tabname(conn, GwName), Chan, 2), ConnMod
|
try
|
||||||
|
[ConnMod] = ets:lookup_element(tabname(conn, GwName), Chan, 2),
|
||||||
|
ConnMod
|
||||||
catch
|
catch
|
||||||
error:badarg -> undefined
|
error:badarg -> undefined
|
||||||
end.
|
end.
|
||||||
|
|
@ -568,28 +664,33 @@ do_get_chann_conn_mod(GwName, ClientId, ChanPid) ->
|
||||||
get_chann_conn_mod(GwName, ClientId, ChanPid) ->
|
get_chann_conn_mod(GwName, ClientId, ChanPid) ->
|
||||||
wrap_rpc(emqx_gateway_cm_proto_v1:get_chann_conn_mod(GwName, ClientId, ChanPid)).
|
wrap_rpc(emqx_gateway_cm_proto_v1:get_chann_conn_mod(GwName, ClientId, ChanPid)).
|
||||||
|
|
||||||
-spec call(gateway_name(), emqx_types:clientid(), term())
|
-spec call(gateway_name(), emqx_types:clientid(), term()) ->
|
||||||
-> undefined | term().
|
undefined | term().
|
||||||
call(GwName, ClientId, Req) ->
|
call(GwName, ClientId, Req) ->
|
||||||
with_channel(
|
with_channel(
|
||||||
GwName, ClientId,
|
GwName,
|
||||||
fun(ChanPid) ->
|
ClientId,
|
||||||
wrap_rpc(
|
fun(ChanPid) ->
|
||||||
emqx_gateway_cm_proto_v1:call(GwName, ClientId, ChanPid, Req)
|
wrap_rpc(
|
||||||
)
|
emqx_gateway_cm_proto_v1:call(GwName, ClientId, ChanPid, Req)
|
||||||
end).
|
)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
-spec call(gateway_name(), emqx_types:clientid(), term(), timeout())
|
-spec call(gateway_name(), emqx_types:clientid(), term(), timeout()) ->
|
||||||
-> undefined | term().
|
undefined | term().
|
||||||
call(GwName, ClientId, Req, Timeout) ->
|
call(GwName, ClientId, Req, Timeout) ->
|
||||||
with_channel(
|
with_channel(
|
||||||
GwName, ClientId,
|
GwName,
|
||||||
fun(ChanPid) ->
|
ClientId,
|
||||||
wrap_rpc(
|
fun(ChanPid) ->
|
||||||
emqx_gateway_cm_proto_v1:call(
|
wrap_rpc(
|
||||||
GwName, ClientId, ChanPid, Req, Timeout)
|
emqx_gateway_cm_proto_v1:call(
|
||||||
)
|
GwName, ClientId, ChanPid, Req, Timeout
|
||||||
end).
|
)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
do_call(GwName, ClientId, ChanPid, Req) ->
|
do_call(GwName, ClientId, ChanPid, Req) ->
|
||||||
case do_get_chann_conn_mod(GwName, ClientId, ChanPid) of
|
case do_get_chann_conn_mod(GwName, ClientId, ChanPid) of
|
||||||
|
|
@ -606,11 +707,14 @@ do_call(GwName, ClientId, ChanPid, Req, Timeout) ->
|
||||||
-spec cast(gateway_name(), emqx_types:clientid(), term()) -> ok.
|
-spec cast(gateway_name(), emqx_types:clientid(), term()) -> ok.
|
||||||
cast(GwName, ClientId, Req) ->
|
cast(GwName, ClientId, Req) ->
|
||||||
with_channel(
|
with_channel(
|
||||||
GwName, ClientId,
|
GwName,
|
||||||
fun(ChanPid) ->
|
ClientId,
|
||||||
wrap_rpc(
|
fun(ChanPid) ->
|
||||||
emqx_gateway_cm_proto_v1:cast(GwName, ClientId, ChanPid, Req))
|
wrap_rpc(
|
||||||
end),
|
emqx_gateway_cm_proto_v1:cast(GwName, ClientId, ChanPid, Req)
|
||||||
|
)
|
||||||
|
end
|
||||||
|
),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
do_cast(GwName, ClientId, ChanPid, Req) ->
|
do_cast(GwName, ClientId, ChanPid, Req) ->
|
||||||
|
|
@ -627,7 +731,11 @@ locker_trans(GwName, ClientId, Fun) ->
|
||||||
Locker = lockername(GwName),
|
Locker = lockername(GwName),
|
||||||
case locker_lock(Locker, ClientId) of
|
case locker_lock(Locker, ClientId) of
|
||||||
{true, Nodes} ->
|
{true, Nodes} ->
|
||||||
try Fun(Nodes) after locker_unlock(Locker, ClientId) end;
|
try
|
||||||
|
Fun(Nodes)
|
||||||
|
after
|
||||||
|
locker_unlock(Locker, ClientId)
|
||||||
|
end;
|
||||||
{false, _Nodes} ->
|
{false, _Nodes} ->
|
||||||
{error, client_id_unavailable}
|
{error, client_id_unavailable}
|
||||||
end.
|
end.
|
||||||
|
|
@ -673,10 +781,12 @@ init(Options) ->
|
||||||
%% TODO: v0.2
|
%% TODO: v0.2
|
||||||
%ok = emqx_stats:update_interval(chan_stats, fun ?MODULE:stats_fun/0),
|
%ok = emqx_stats:update_interval(chan_stats, fun ?MODULE:stats_fun/0),
|
||||||
|
|
||||||
{ok, #state{gwname = GwName,
|
{ok, #state{
|
||||||
locker = Locker,
|
gwname = GwName,
|
||||||
registry = Registry,
|
locker = Locker,
|
||||||
chan_pmon = emqx_pmon:new()}}.
|
registry = Registry,
|
||||||
|
chan_pmon = emqx_pmon:new()
|
||||||
|
}}.
|
||||||
|
|
||||||
handle_call(_Request, _From, State) ->
|
handle_call(_Request, _From, State) ->
|
||||||
Reply = ok,
|
Reply = ok,
|
||||||
|
|
@ -685,19 +795,19 @@ handle_call(_Request, _From, State) ->
|
||||||
handle_cast({registered, {ClientId, ChanPid}}, State = #state{chan_pmon = PMon}) ->
|
handle_cast({registered, {ClientId, ChanPid}}, State = #state{chan_pmon = PMon}) ->
|
||||||
PMon1 = emqx_pmon:monitor(ChanPid, ClientId, PMon),
|
PMon1 = emqx_pmon:monitor(ChanPid, ClientId, PMon),
|
||||||
{noreply, State#state{chan_pmon = PMon1}};
|
{noreply, State#state{chan_pmon = PMon1}};
|
||||||
|
|
||||||
handle_cast(_Msg, State) ->
|
handle_cast(_Msg, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
handle_info({'DOWN', _MRef, process, Pid, _Reason},
|
handle_info(
|
||||||
State = #state{gwname = GwName, chan_pmon = PMon}) ->
|
{'DOWN', _MRef, process, Pid, _Reason},
|
||||||
|
State = #state{gwname = GwName, chan_pmon = PMon}
|
||||||
|
) ->
|
||||||
ChanPids = [Pid | emqx_misc:drain_down(?DEFAULT_BATCH_SIZE)],
|
ChanPids = [Pid | emqx_misc:drain_down(?DEFAULT_BATCH_SIZE)],
|
||||||
{Items, PMon1} = emqx_pmon:erase_all(ChanPids, PMon),
|
{Items, PMon1} = emqx_pmon:erase_all(ChanPids, PMon),
|
||||||
|
|
||||||
CmTabs = cmtabs(GwName),
|
CmTabs = cmtabs(GwName),
|
||||||
ok = emqx_pool:async_submit(fun do_unregister_channel_task/3, [Items, GwName, CmTabs]),
|
ok = emqx_pool:async_submit(fun do_unregister_channel_task/3, [Items, GwName, CmTabs]),
|
||||||
{noreply, State#state{chan_pmon = PMon1}};
|
{noreply, State#state{chan_pmon = PMon1}};
|
||||||
|
|
||||||
handle_info(_Info, State) ->
|
handle_info(_Info, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
@ -711,9 +821,11 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
|
|
||||||
do_unregister_channel_task(Items, GwName, CmTabs) ->
|
do_unregister_channel_task(Items, GwName, CmTabs) ->
|
||||||
lists:foreach(
|
lists:foreach(
|
||||||
fun({ChanPid, ClientId}) ->
|
fun({ChanPid, ClientId}) ->
|
||||||
do_unregister_channel(GwName, {ClientId, ChanPid}, CmTabs)
|
do_unregister_channel(GwName, {ClientId, ChanPid}, CmTabs)
|
||||||
end, Items).
|
end,
|
||||||
|
Items
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal funcs
|
%% Internal funcs
|
||||||
|
|
|
||||||
|
|
@ -23,22 +23,24 @@
|
||||||
|
|
||||||
-export([start_link/1]).
|
-export([start_link/1]).
|
||||||
|
|
||||||
-export([ register_channel/2
|
-export([
|
||||||
, unregister_channel/2
|
register_channel/2,
|
||||||
]).
|
unregister_channel/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([lookup_channels/2]).
|
-export([lookup_channels/2]).
|
||||||
|
|
||||||
-export([tabname/1]).
|
-export([tabname/1]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
-define(CM_SHARD, emqx_gateway_cm_shard).
|
-define(CM_SHARD, emqx_gateway_cm_shard).
|
||||||
-define(LOCK, {?MODULE, cleanup_down}).
|
-define(LOCK, {?MODULE, cleanup_down}).
|
||||||
|
|
@ -46,7 +48,7 @@
|
||||||
-record(channel, {chid, pid}).
|
-record(channel, {chid, pid}).
|
||||||
|
|
||||||
%% @doc Start the global channel registry for the given gateway name.
|
%% @doc Start the global channel registry for the given gateway name.
|
||||||
-spec(start_link(gateway_name()) -> gen_server:startlink_ret()).
|
-spec start_link(gateway_name()) -> gen_server:startlink_ret().
|
||||||
start_link(Name) ->
|
start_link(Name) ->
|
||||||
gen_server:start_link(?MODULE, [Name], []).
|
gen_server:start_link(?MODULE, [Name], []).
|
||||||
|
|
||||||
|
|
@ -63,25 +65,27 @@ tabname(Name) ->
|
||||||
-spec register_channel(gateway_name(), binary() | {binary(), pid()}) -> ok.
|
-spec register_channel(gateway_name(), binary() | {binary(), pid()}) -> ok.
|
||||||
register_channel(Name, ClientId) when is_binary(ClientId) ->
|
register_channel(Name, ClientId) when is_binary(ClientId) ->
|
||||||
register_channel(Name, {ClientId, self()});
|
register_channel(Name, {ClientId, self()});
|
||||||
|
register_channel(Name, {ClientId, ChanPid}) when
|
||||||
register_channel(Name, {ClientId, ChanPid})
|
is_binary(ClientId), is_pid(ChanPid)
|
||||||
when is_binary(ClientId), is_pid(ChanPid) ->
|
->
|
||||||
mria:dirty_write(tabname(Name), record(ClientId, ChanPid)).
|
mria:dirty_write(tabname(Name), record(ClientId, ChanPid)).
|
||||||
|
|
||||||
%% @doc Unregister a global channel.
|
%% @doc Unregister a global channel.
|
||||||
-spec unregister_channel(gateway_name(), binary() | {binary(), pid()}) -> ok.
|
-spec unregister_channel(gateway_name(), binary() | {binary(), pid()}) -> ok.
|
||||||
unregister_channel(Name, ClientId) when is_binary(ClientId) ->
|
unregister_channel(Name, ClientId) when is_binary(ClientId) ->
|
||||||
unregister_channel(Name, {ClientId, self()});
|
unregister_channel(Name, {ClientId, self()});
|
||||||
|
unregister_channel(Name, {ClientId, ChanPid}) when
|
||||||
unregister_channel(Name, {ClientId, ChanPid})
|
is_binary(ClientId), is_pid(ChanPid)
|
||||||
when is_binary(ClientId), is_pid(ChanPid) ->
|
->
|
||||||
mria:dirty_delete_object(tabname(Name), record(ClientId, ChanPid)).
|
mria:dirty_delete_object(tabname(Name), record(ClientId, ChanPid)).
|
||||||
|
|
||||||
%% @doc Lookup the global channels.
|
%% @doc Lookup the global channels.
|
||||||
-spec lookup_channels(gateway_name(), binary()) -> list(pid()).
|
-spec lookup_channels(gateway_name(), binary()) -> list(pid()).
|
||||||
lookup_channels(Name, ClientId) ->
|
lookup_channels(Name, ClientId) ->
|
||||||
[ChanPid
|
[
|
||||||
|| #channel{pid = ChanPid} <- mnesia:dirty_read(tabname(Name), ClientId)].
|
ChanPid
|
||||||
|
|| #channel{pid = ChanPid} <- mnesia:dirty_read(tabname(Name), ClientId)
|
||||||
|
].
|
||||||
|
|
||||||
record(ClientId, ChanPid) ->
|
record(ClientId, ChanPid) ->
|
||||||
#channel{chid = ClientId, pid = ChanPid}.
|
#channel{chid = ClientId, pid = ChanPid}.
|
||||||
|
|
@ -93,13 +97,18 @@ record(ClientId, ChanPid) ->
|
||||||
init([Name]) ->
|
init([Name]) ->
|
||||||
Tab = tabname(Name),
|
Tab = tabname(Name),
|
||||||
ok = mria:create_table(Tab, [
|
ok = mria:create_table(Tab, [
|
||||||
{type, bag},
|
{type, bag},
|
||||||
{rlog_shard, ?CM_SHARD},
|
{rlog_shard, ?CM_SHARD},
|
||||||
{storage, ram_copies},
|
{storage, ram_copies},
|
||||||
{record_name, channel},
|
{record_name, channel},
|
||||||
{attributes, record_info(fields, channel)},
|
{attributes, record_info(fields, channel)},
|
||||||
{storage_properties, [{ets, [{read_concurrency, true},
|
{storage_properties, [
|
||||||
{write_concurrency, true}]}]}]),
|
{ets, [
|
||||||
|
{read_concurrency, true},
|
||||||
|
{write_concurrency, true}
|
||||||
|
]}
|
||||||
|
]}
|
||||||
|
]),
|
||||||
ok = mria:wait_for_tables([Tab]),
|
ok = mria:wait_for_tables([Tab]),
|
||||||
ok = ekka:monitor(membership),
|
ok = ekka:monitor(membership),
|
||||||
{ok, #{name => Name}}.
|
{ok, #{name => Name}}.
|
||||||
|
|
@ -115,14 +124,11 @@ handle_cast(Msg, State) ->
|
||||||
handle_info({membership, {mnesia, down, Node}}, State = #{name := Name}) ->
|
handle_info({membership, {mnesia, down, Node}}, State = #{name := Name}) ->
|
||||||
cleanup_channels(Node, Name),
|
cleanup_channels(Node, Name),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info({membership, {node, down, Node}}, State = #{name := Name}) ->
|
handle_info({membership, {node, down, Node}}, State = #{name := Name}) ->
|
||||||
cleanup_channels(Node, Name),
|
cleanup_channels(Node, Name),
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info({membership, _Event}, State) ->
|
handle_info({membership, _Event}, State) ->
|
||||||
{noreply, State};
|
{noreply, State};
|
||||||
|
|
||||||
handle_info(Info, State) ->
|
handle_info(Info, State) ->
|
||||||
logger:error("Unexpected info: ~p", [Info]),
|
logger:error("Unexpected info: ~p", [Info]),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
@ -140,13 +146,17 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
cleanup_channels(Node, Name) ->
|
cleanup_channels(Node, Name) ->
|
||||||
Tab = tabname(Name),
|
Tab = tabname(Name),
|
||||||
global:trans(
|
global:trans(
|
||||||
{?LOCK, self()},
|
{?LOCK, self()},
|
||||||
fun() ->
|
fun() ->
|
||||||
mria:transaction(?CM_SHARD, fun do_cleanup_channels/2, [Node, Tab])
|
mria:transaction(?CM_SHARD, fun do_cleanup_channels/2, [Node, Tab])
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
do_cleanup_channels(Node, Tab) ->
|
do_cleanup_channels(Node, Tab) ->
|
||||||
Pat = [{#channel{pid = '$1', _ = '_'}, [{'==', {node, '$1'}, Node}], ['$_']}],
|
Pat = [{#channel{pid = '$1', _ = '_'}, [{'==', {node, '$1'}, Node}], ['$_']}],
|
||||||
lists:foreach(fun(Chan) ->
|
lists:foreach(
|
||||||
mnesia:delete_object(Tab, Chan, write)
|
fun(Chan) ->
|
||||||
end, mnesia:select(Tab, Pat, write)).
|
mnesia:delete_object(Tab, Chan, write)
|
||||||
|
end,
|
||||||
|
mnesia:select(Tab, Pat, write)
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -20,41 +20,47 @@
|
||||||
-behaviour(emqx_config_handler).
|
-behaviour(emqx_config_handler).
|
||||||
|
|
||||||
%% Load/Unload
|
%% Load/Unload
|
||||||
-export([ load/0
|
-export([
|
||||||
, unload/0
|
load/0,
|
||||||
]).
|
unload/0
|
||||||
|
]).
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([ gateway/1
|
-export([
|
||||||
, load_gateway/2
|
gateway/1,
|
||||||
, update_gateway/2
|
load_gateway/2,
|
||||||
, unload_gateway/1
|
update_gateway/2,
|
||||||
]).
|
unload_gateway/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ listeners/1
|
-export([
|
||||||
, listener/1
|
listeners/1,
|
||||||
, add_listener/3
|
listener/1,
|
||||||
, update_listener/3
|
add_listener/3,
|
||||||
, remove_listener/2
|
update_listener/3,
|
||||||
]).
|
remove_listener/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ add_authn/2
|
-export([
|
||||||
, add_authn/3
|
add_authn/2,
|
||||||
, update_authn/2
|
add_authn/3,
|
||||||
, update_authn/3
|
update_authn/2,
|
||||||
, remove_authn/1
|
update_authn/3,
|
||||||
, remove_authn/2
|
remove_authn/1,
|
||||||
]).
|
remove_authn/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% internal exports
|
%% internal exports
|
||||||
-export([ unconvert_listeners/1
|
-export([
|
||||||
, convert_listeners/2
|
unconvert_listeners/1,
|
||||||
]).
|
convert_listeners/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% callbacks for emqx_config_handler
|
%% callbacks for emqx_config_handler
|
||||||
-export([ pre_config_update/3
|
-export([
|
||||||
, post_config_update/5
|
pre_config_update/3,
|
||||||
]).
|
post_config_update/5
|
||||||
|
]).
|
||||||
|
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
-include_lib("emqx/include/emqx_authentication.hrl").
|
-include_lib("emqx/include/emqx_authentication.hrl").
|
||||||
|
|
@ -63,8 +69,7 @@
|
||||||
-type atom_or_bin() :: atom() | binary().
|
-type atom_or_bin() :: atom() | binary().
|
||||||
-type ok_or_err() :: ok | {error, term()}.
|
-type ok_or_err() :: ok | {error, term()}.
|
||||||
-type map_or_err() :: {ok, map()} | {error, term()}.
|
-type map_or_err() :: {ok, map()} | {error, term()}.
|
||||||
-type listener_ref() :: {ListenerType :: atom_or_bin(),
|
-type listener_ref() :: {ListenerType :: atom_or_bin(), ListenerName :: atom_or_bin()}.
|
||||||
ListenerName :: atom_or_bin()}.
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Load/Unload
|
%% Load/Unload
|
||||||
|
|
@ -83,21 +88,25 @@ unload() ->
|
||||||
|
|
||||||
-spec load_gateway(atom_or_bin(), map()) -> map_or_err().
|
-spec load_gateway(atom_or_bin(), map()) -> map_or_err().
|
||||||
load_gateway(GwName, Conf) ->
|
load_gateway(GwName, Conf) ->
|
||||||
NConf = case maps:take(<<"listeners">>, Conf) of
|
NConf =
|
||||||
error -> Conf;
|
case maps:take(<<"listeners">>, Conf) of
|
||||||
{Ls, Conf1} ->
|
error -> Conf;
|
||||||
Conf1#{<<"listeners">> => unconvert_listeners(Ls)}
|
{Ls, Conf1} -> Conf1#{<<"listeners">> => unconvert_listeners(Ls)}
|
||||||
end,
|
end,
|
||||||
ret_gw(GwName, update({?FUNCTION_NAME, bin(GwName), NConf})).
|
ret_gw(GwName, update({?FUNCTION_NAME, bin(GwName), NConf})).
|
||||||
|
|
||||||
%% @doc convert listener array to map
|
%% @doc convert listener array to map
|
||||||
unconvert_listeners(Ls) when is_list(Ls) ->
|
unconvert_listeners(Ls) when is_list(Ls) ->
|
||||||
lists:foldl(fun(Lis, Acc) ->
|
lists:foldl(
|
||||||
%% FIXME: params apperence guard?
|
fun(Lis, Acc) ->
|
||||||
{[Type, Name], Lis1} = maps_key_take([<<"type">>, <<"name">>], Lis),
|
%% FIXME: params apperence guard?
|
||||||
NLis1 = maps:without([<<"id">>], Lis1),
|
{[Type, Name], Lis1} = maps_key_take([<<"type">>, <<"name">>], Lis),
|
||||||
emqx_map_lib:deep_merge(Acc, #{Type => #{Name => NLis1}})
|
NLis1 = maps:without([<<"id">>], Lis1),
|
||||||
end, #{}, Ls).
|
emqx_map_lib:deep_merge(Acc, #{Type => #{Name => NLis1}})
|
||||||
|
end,
|
||||||
|
#{},
|
||||||
|
Ls
|
||||||
|
).
|
||||||
|
|
||||||
maps_key_take(Ks, M) ->
|
maps_key_take(Ks, M) ->
|
||||||
maps_key_take(Ks, M, []).
|
maps_key_take(Ks, M, []).
|
||||||
|
|
@ -106,8 +115,7 @@ maps_key_take([], M, Acc) ->
|
||||||
maps_key_take([K | Ks], M, Acc) ->
|
maps_key_take([K | Ks], M, Acc) ->
|
||||||
case maps:take(K, M) of
|
case maps:take(K, M) of
|
||||||
error -> throw(bad_key);
|
error -> throw(bad_key);
|
||||||
{V, M1} ->
|
{V, M1} -> maps_key_take(Ks, M1, [V | Acc])
|
||||||
maps_key_take(Ks, M1, [V | Acc])
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec update_gateway(atom_or_bin(), map()) -> map_or_err().
|
-spec update_gateway(atom_or_bin(), map()) -> map_or_err().
|
||||||
|
|
@ -131,32 +139,38 @@ gateway(GwName0) ->
|
||||||
GwName = bin(GwName0),
|
GwName = bin(GwName0),
|
||||||
Path = [<<"gateway">>, GwName],
|
Path = [<<"gateway">>, GwName],
|
||||||
RawConf = emqx_config:fill_defaults(
|
RawConf = emqx_config:fill_defaults(
|
||||||
emqx_config:get_root_raw(Path)
|
emqx_config:get_root_raw(Path)
|
||||||
),
|
),
|
||||||
Confs = emqx_map_lib:jsonable_map(
|
Confs = emqx_map_lib:jsonable_map(
|
||||||
emqx_map_lib:deep_get(Path, RawConf)),
|
emqx_map_lib:deep_get(Path, RawConf)
|
||||||
|
),
|
||||||
LsConf = maps:get(<<"listeners">>, Confs, #{}),
|
LsConf = maps:get(<<"listeners">>, Confs, #{}),
|
||||||
Confs#{<<"listeners">> => convert_listeners(GwName, LsConf)}.
|
Confs#{<<"listeners">> => convert_listeners(GwName, LsConf)}.
|
||||||
|
|
||||||
%% @doc convert listeners map to array
|
%% @doc convert listeners map to array
|
||||||
convert_listeners(GwName, Ls) when is_map(Ls) ->
|
convert_listeners(GwName, Ls) when is_map(Ls) ->
|
||||||
lists:append([do_convert_listener(GwName, Type, maps:to_list(Conf))
|
lists:append([
|
||||||
|| {Type, Conf} <- maps:to_list(Ls)]).
|
do_convert_listener(GwName, Type, maps:to_list(Conf))
|
||||||
|
|| {Type, Conf} <- maps:to_list(Ls)
|
||||||
|
]).
|
||||||
|
|
||||||
do_convert_listener(GwName, LType, Conf) ->
|
do_convert_listener(GwName, LType, Conf) ->
|
||||||
[ do_convert_listener2(GwName, LType, LName, LConf)
|
[
|
||||||
|| {LName, LConf} <- Conf, is_map(LConf)].
|
do_convert_listener2(GwName, LType, LName, LConf)
|
||||||
|
|| {LName, LConf} <- Conf, is_map(LConf)
|
||||||
|
].
|
||||||
|
|
||||||
do_convert_listener2(GwName, LType, LName, LConf) ->
|
do_convert_listener2(GwName, LType, LName, LConf) ->
|
||||||
ListenerId = emqx_gateway_utils:listener_id(GwName, LType, LName),
|
ListenerId = emqx_gateway_utils:listener_id(GwName, LType, LName),
|
||||||
Running = emqx_gateway_utils:is_running(ListenerId, LConf),
|
Running = emqx_gateway_utils:is_running(ListenerId, LConf),
|
||||||
bind2str(
|
bind2str(
|
||||||
LConf#{
|
LConf#{
|
||||||
id => ListenerId,
|
id => ListenerId,
|
||||||
type => LType,
|
type => LType,
|
||||||
name => LName,
|
name => LName,
|
||||||
running => Running
|
running => Running
|
||||||
}).
|
}
|
||||||
|
).
|
||||||
|
|
||||||
bind2str(LConf = #{bind := Bind}) when is_integer(Bind) ->
|
bind2str(LConf = #{bind := Bind}) when is_integer(Bind) ->
|
||||||
maps:put(bind, integer_to_binary(Bind), LConf);
|
maps:put(bind, integer_to_binary(Bind), LConf);
|
||||||
|
|
@ -169,48 +183,60 @@ bind2str(LConf = #{<<"bind">> := Bind}) when is_binary(Bind) ->
|
||||||
|
|
||||||
-spec listeners(atom_or_bin()) -> [map()].
|
-spec listeners(atom_or_bin()) -> [map()].
|
||||||
listeners(GwName0) ->
|
listeners(GwName0) ->
|
||||||
GwName = bin(GwName0),
|
GwName = bin(GwName0),
|
||||||
RawConf = emqx_config:fill_defaults(
|
RawConf = emqx_config:fill_defaults(
|
||||||
emqx_config:get_root_raw([<<"gateway">>])),
|
emqx_config:get_root_raw([<<"gateway">>])
|
||||||
Listeners = emqx_map_lib:jsonable_map(
|
),
|
||||||
emqx_map_lib:deep_get(
|
Listeners = emqx_map_lib:jsonable_map(
|
||||||
[<<"gateway">>, GwName, <<"listeners">>], RawConf)),
|
emqx_map_lib:deep_get(
|
||||||
convert_listeners(GwName, Listeners).
|
[<<"gateway">>, GwName, <<"listeners">>], RawConf
|
||||||
|
)
|
||||||
|
),
|
||||||
|
convert_listeners(GwName, Listeners).
|
||||||
|
|
||||||
-spec listener(binary()) -> {ok, map()} | {error, not_found} | {error, any()}.
|
-spec listener(binary()) -> {ok, map()} | {error, not_found} | {error, any()}.
|
||||||
listener(ListenerId) ->
|
listener(ListenerId) ->
|
||||||
{GwName, Type, LName} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
{GwName, Type, LName} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
||||||
RootConf = emqx_config:fill_defaults(
|
RootConf = emqx_config:fill_defaults(
|
||||||
emqx_config:get_root_raw([<<"gateway">>])),
|
emqx_config:get_root_raw([<<"gateway">>])
|
||||||
|
),
|
||||||
try
|
try
|
||||||
Path = [<<"gateway">>, GwName, <<"listeners">>, Type, LName],
|
Path = [<<"gateway">>, GwName, <<"listeners">>, Type, LName],
|
||||||
LConf = emqx_map_lib:deep_get(Path, RootConf),
|
LConf = emqx_map_lib:deep_get(Path, RootConf),
|
||||||
Running = emqx_gateway_utils:is_running(
|
Running = emqx_gateway_utils:is_running(
|
||||||
binary_to_existing_atom(ListenerId), LConf),
|
binary_to_existing_atom(ListenerId), LConf
|
||||||
{ok, emqx_map_lib:jsonable_map(
|
),
|
||||||
LConf#{
|
{ok,
|
||||||
id => ListenerId,
|
emqx_map_lib:jsonable_map(
|
||||||
type => Type,
|
LConf#{
|
||||||
name => LName,
|
id => ListenerId,
|
||||||
running => Running})}
|
type => Type,
|
||||||
|
name => LName,
|
||||||
|
running => Running
|
||||||
|
}
|
||||||
|
)}
|
||||||
catch
|
catch
|
||||||
error : {config_not_found, _} ->
|
error:{config_not_found, _} ->
|
||||||
{error, not_found};
|
{error, not_found};
|
||||||
_Class : Reason ->
|
_Class:Reason ->
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec add_listener(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
-spec add_listener(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
||||||
add_listener(GwName, ListenerRef, Conf) ->
|
add_listener(GwName, ListenerRef, Conf) ->
|
||||||
ret_listener_or_err(
|
ret_listener_or_err(
|
||||||
GwName, ListenerRef,
|
GwName,
|
||||||
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})).
|
ListenerRef,
|
||||||
|
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})
|
||||||
|
).
|
||||||
|
|
||||||
-spec update_listener(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
-spec update_listener(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
||||||
update_listener(GwName, ListenerRef, Conf) ->
|
update_listener(GwName, ListenerRef, Conf) ->
|
||||||
ret_listener_or_err(
|
ret_listener_or_err(
|
||||||
GwName, ListenerRef,
|
GwName,
|
||||||
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})).
|
ListenerRef,
|
||||||
|
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})
|
||||||
|
).
|
||||||
|
|
||||||
-spec remove_listener(atom_or_bin(), listener_ref()) -> ok_or_err().
|
-spec remove_listener(atom_or_bin(), listener_ref()) -> ok_or_err().
|
||||||
remove_listener(GwName, ListenerRef) ->
|
remove_listener(GwName, ListenerRef) ->
|
||||||
|
|
@ -223,8 +249,10 @@ add_authn(GwName, Conf) ->
|
||||||
-spec add_authn(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
-spec add_authn(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
||||||
add_authn(GwName, ListenerRef, Conf) ->
|
add_authn(GwName, ListenerRef, Conf) ->
|
||||||
ret_authn(
|
ret_authn(
|
||||||
GwName, ListenerRef,
|
GwName,
|
||||||
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})).
|
ListenerRef,
|
||||||
|
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})
|
||||||
|
).
|
||||||
|
|
||||||
-spec update_authn(atom_or_bin(), map()) -> map_or_err().
|
-spec update_authn(atom_or_bin(), map()) -> map_or_err().
|
||||||
update_authn(GwName, Conf) ->
|
update_authn(GwName, Conf) ->
|
||||||
|
|
@ -233,8 +261,10 @@ update_authn(GwName, Conf) ->
|
||||||
-spec update_authn(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
-spec update_authn(atom_or_bin(), listener_ref(), map()) -> map_or_err().
|
||||||
update_authn(GwName, ListenerRef, Conf) ->
|
update_authn(GwName, ListenerRef, Conf) ->
|
||||||
ret_authn(
|
ret_authn(
|
||||||
GwName, ListenerRef,
|
GwName,
|
||||||
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})).
|
ListenerRef,
|
||||||
|
update({?FUNCTION_NAME, bin(GwName), bin(ListenerRef), Conf})
|
||||||
|
).
|
||||||
|
|
||||||
-spec remove_authn(atom_or_bin()) -> ok_or_err().
|
-spec remove_authn(atom_or_bin()) -> ok_or_err().
|
||||||
remove_authn(GwName) ->
|
remove_authn(GwName) ->
|
||||||
|
|
@ -249,8 +279,8 @@ update(Req) ->
|
||||||
res(emqx_conf:update([gateway], Req, #{override_to => cluster})).
|
res(emqx_conf:update([gateway], Req, #{override_to => cluster})).
|
||||||
|
|
||||||
res({ok, Result}) -> {ok, Result};
|
res({ok, Result}) -> {ok, Result};
|
||||||
res({error, {pre_config_update,?MODULE,Reason}}) -> {error, Reason};
|
res({error, {pre_config_update, ?MODULE, Reason}}) -> {error, Reason};
|
||||||
res({error, {post_config_update,?MODULE,Reason}}) -> {error, Reason};
|
res({error, {post_config_update, ?MODULE, Reason}}) -> {error, Reason};
|
||||||
res({error, Reason}) -> {error, Reason}.
|
res({error, Reason}) -> {error, Reason}.
|
||||||
|
|
||||||
bin({LType, LName}) ->
|
bin({LType, LName}) ->
|
||||||
|
|
@ -266,38 +296,58 @@ ret_ok_err(Err) -> Err.
|
||||||
ret_gw(GwName, {ok, #{raw_config := GwConf}}) ->
|
ret_gw(GwName, {ok, #{raw_config := GwConf}}) ->
|
||||||
GwConf1 = emqx_map_lib:deep_get([bin(GwName)], GwConf),
|
GwConf1 = emqx_map_lib:deep_get([bin(GwName)], GwConf),
|
||||||
LsConf = emqx_map_lib:deep_get(
|
LsConf = emqx_map_lib:deep_get(
|
||||||
[bin(GwName), <<"listeners">>],
|
[bin(GwName), <<"listeners">>],
|
||||||
GwConf, #{}),
|
GwConf,
|
||||||
|
#{}
|
||||||
|
),
|
||||||
NLsConf =
|
NLsConf =
|
||||||
lists:foldl(fun({LType, SubConf}, Acc) ->
|
lists:foldl(
|
||||||
NLConfs =
|
fun({LType, SubConf}, Acc) ->
|
||||||
lists:map(fun({LName, LConf}) ->
|
NLConfs =
|
||||||
do_convert_listener2(GwName, LType, LName, LConf)
|
lists:map(
|
||||||
end, maps:to_list(SubConf)),
|
fun({LName, LConf}) ->
|
||||||
[NLConfs | Acc]
|
do_convert_listener2(GwName, LType, LName, LConf)
|
||||||
end, [], maps:to_list(LsConf)),
|
end,
|
||||||
|
maps:to_list(SubConf)
|
||||||
|
),
|
||||||
|
[NLConfs | Acc]
|
||||||
|
end,
|
||||||
|
[],
|
||||||
|
maps:to_list(LsConf)
|
||||||
|
),
|
||||||
{ok, maps:merge(GwConf1, #{<<"listeners">> => lists:append(NLsConf)})};
|
{ok, maps:merge(GwConf1, #{<<"listeners">> => lists:append(NLsConf)})};
|
||||||
ret_gw(_GwName, Err) -> Err.
|
ret_gw(_GwName, Err) ->
|
||||||
|
Err.
|
||||||
|
|
||||||
ret_authn(GwName, {ok, #{raw_config := GwConf}}) ->
|
ret_authn(GwName, {ok, #{raw_config := GwConf}}) ->
|
||||||
Authn = emqx_map_lib:deep_get(
|
Authn = emqx_map_lib:deep_get(
|
||||||
[bin(GwName), <<"authentication">>],
|
[bin(GwName), <<"authentication">>],
|
||||||
GwConf),
|
GwConf
|
||||||
|
),
|
||||||
{ok, Authn};
|
{ok, Authn};
|
||||||
ret_authn(_GwName, Err) -> Err.
|
ret_authn(_GwName, Err) ->
|
||||||
|
Err.
|
||||||
|
|
||||||
ret_authn(GwName, {LType, LName}, {ok, #{raw_config := GwConf}}) ->
|
ret_authn(GwName, {LType, LName}, {ok, #{raw_config := GwConf}}) ->
|
||||||
Authn = emqx_map_lib:deep_get(
|
Authn = emqx_map_lib:deep_get(
|
||||||
[bin(GwName), <<"listeners">>, bin(LType),
|
[
|
||||||
bin(LName), <<"authentication">>],
|
bin(GwName),
|
||||||
GwConf),
|
<<"listeners">>,
|
||||||
|
bin(LType),
|
||||||
|
bin(LName),
|
||||||
|
<<"authentication">>
|
||||||
|
],
|
||||||
|
GwConf
|
||||||
|
),
|
||||||
{ok, Authn};
|
{ok, Authn};
|
||||||
ret_authn(_, _, Err) -> Err.
|
ret_authn(_, _, Err) ->
|
||||||
|
Err.
|
||||||
|
|
||||||
ret_listener_or_err(GwName, {LType, LName}, {ok, #{raw_config := GwConf}}) ->
|
ret_listener_or_err(GwName, {LType, LName}, {ok, #{raw_config := GwConf}}) ->
|
||||||
LConf = emqx_map_lib:deep_get(
|
LConf = emqx_map_lib:deep_get(
|
||||||
[bin(GwName), <<"listeners">>, bin(LType), bin(LName)],
|
[bin(GwName), <<"listeners">>, bin(LType), bin(LName)],
|
||||||
GwConf),
|
GwConf
|
||||||
|
),
|
||||||
{ok, do_convert_listener2(GwName, LType, LName, LConf)};
|
{ok, do_convert_listener2(GwName, LType, LName, LConf)};
|
||||||
ret_listener_or_err(_, _, Err) ->
|
ret_listener_or_err(_, _, Err) ->
|
||||||
Err.
|
Err.
|
||||||
|
|
@ -306,9 +356,11 @@ ret_listener_or_err(_, _, Err) ->
|
||||||
%% Config Handler
|
%% Config Handler
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec pre_config_update(list(atom()),
|
-spec pre_config_update(
|
||||||
emqx_config:update_request(),
|
list(atom()),
|
||||||
emqx_config:raw_config()) ->
|
emqx_config:update_request(),
|
||||||
|
emqx_config:raw_config()
|
||||||
|
) ->
|
||||||
{ok, emqx_config:update_request()} | {error, term()}.
|
{ok, emqx_config:update_request()} | {error, term()}.
|
||||||
pre_config_update(_, {load_gateway, GwName, Conf}, RawConf) ->
|
pre_config_update(_, {load_gateway, GwName, Conf}, RawConf) ->
|
||||||
case maps:get(GwName, RawConf, undefined) of
|
case maps:get(GwName, RawConf, undefined) of
|
||||||
|
|
@ -327,89 +379,119 @@ pre_config_update(_, {update_gateway, GwName, Conf}, RawConf) ->
|
||||||
{ok, emqx_map_lib:deep_merge(RawConf, #{GwName => NConf})}
|
{ok, emqx_map_lib:deep_merge(RawConf, #{GwName => NConf})}
|
||||||
end;
|
end;
|
||||||
pre_config_update(_, {unload_gateway, GwName}, RawConf) ->
|
pre_config_update(_, {unload_gateway, GwName}, RawConf) ->
|
||||||
_ = tune_gw_certs(fun clear_certs/2,
|
_ = tune_gw_certs(
|
||||||
GwName,
|
fun clear_certs/2,
|
||||||
maps:get(GwName, RawConf, #{})
|
GwName,
|
||||||
),
|
maps:get(GwName, RawConf, #{})
|
||||||
|
),
|
||||||
{ok, maps:remove(GwName, RawConf)};
|
{ok, maps:remove(GwName, RawConf)};
|
||||||
|
|
||||||
pre_config_update(_, {add_listener, GwName, {LType, LName}, Conf}, RawConf) ->
|
pre_config_update(_, {add_listener, GwName, {LType, LName}, Conf}, RawConf) ->
|
||||||
case emqx_map_lib:deep_get(
|
case
|
||||||
[GwName, <<"listeners">>, LType, LName], RawConf, undefined) of
|
emqx_map_lib:deep_get(
|
||||||
|
[GwName, <<"listeners">>, LType, LName], RawConf, undefined
|
||||||
|
)
|
||||||
|
of
|
||||||
undefined ->
|
undefined ->
|
||||||
NConf = convert_certs(certs_dir(GwName), Conf),
|
NConf = convert_certs(certs_dir(GwName), Conf),
|
||||||
NListener = #{LType => #{LName => NConf}},
|
NListener = #{LType => #{LName => NConf}},
|
||||||
{ok, emqx_map_lib:deep_merge(
|
{ok,
|
||||||
RawConf,
|
emqx_map_lib:deep_merge(
|
||||||
#{GwName => #{<<"listeners">> => NListener}})};
|
RawConf,
|
||||||
|
#{GwName => #{<<"listeners">> => NListener}}
|
||||||
|
)};
|
||||||
_ ->
|
_ ->
|
||||||
badres_listener(already_exist, GwName, LType, LName)
|
badres_listener(already_exist, GwName, LType, LName)
|
||||||
end;
|
end;
|
||||||
pre_config_update(_, {update_listener, GwName, {LType, LName}, Conf}, RawConf) ->
|
pre_config_update(_, {update_listener, GwName, {LType, LName}, Conf}, RawConf) ->
|
||||||
case emqx_map_lib:deep_get(
|
case
|
||||||
[GwName, <<"listeners">>, LType, LName], RawConf, undefined) of
|
emqx_map_lib:deep_get(
|
||||||
|
[GwName, <<"listeners">>, LType, LName], RawConf, undefined
|
||||||
|
)
|
||||||
|
of
|
||||||
undefined ->
|
undefined ->
|
||||||
badres_listener(not_found, GwName, LType, LName);
|
badres_listener(not_found, GwName, LType, LName);
|
||||||
OldConf ->
|
OldConf ->
|
||||||
NConf = convert_certs(certs_dir(GwName), Conf, OldConf),
|
NConf = convert_certs(certs_dir(GwName), Conf, OldConf),
|
||||||
NListener = #{LType => #{LName => NConf}},
|
NListener = #{LType => #{LName => NConf}},
|
||||||
{ok, emqx_map_lib:deep_merge(
|
{ok,
|
||||||
RawConf,
|
emqx_map_lib:deep_merge(
|
||||||
#{GwName => #{<<"listeners">> => NListener}})}
|
RawConf,
|
||||||
|
#{GwName => #{<<"listeners">> => NListener}}
|
||||||
|
)}
|
||||||
end;
|
end;
|
||||||
pre_config_update(_, {remove_listener, GwName, {LType, LName}}, RawConf) ->
|
pre_config_update(_, {remove_listener, GwName, {LType, LName}}, RawConf) ->
|
||||||
Path = [GwName, <<"listeners">>, LType, LName],
|
Path = [GwName, <<"listeners">>, LType, LName],
|
||||||
case emqx_map_lib:deep_get(Path, RawConf, undefined) of
|
case emqx_map_lib:deep_get(Path, RawConf, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
{ok, RawConf};
|
{ok, RawConf};
|
||||||
OldConf ->
|
OldConf ->
|
||||||
clear_certs(certs_dir(GwName), OldConf),
|
clear_certs(certs_dir(GwName), OldConf),
|
||||||
{ok, emqx_map_lib:deep_remove(Path, RawConf)}
|
{ok, emqx_map_lib:deep_remove(Path, RawConf)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
pre_config_update(_, {add_authn, GwName, Conf}, RawConf) ->
|
pre_config_update(_, {add_authn, GwName, Conf}, RawConf) ->
|
||||||
case emqx_map_lib:deep_get(
|
case
|
||||||
[GwName, ?AUTHN_BIN], RawConf, undefined) of
|
emqx_map_lib:deep_get(
|
||||||
|
[GwName, ?AUTHN_BIN], RawConf, undefined
|
||||||
|
)
|
||||||
|
of
|
||||||
undefined ->
|
undefined ->
|
||||||
{ok, emqx_map_lib:deep_merge(
|
{ok,
|
||||||
RawConf,
|
emqx_map_lib:deep_merge(
|
||||||
#{GwName => #{?AUTHN_BIN => Conf}})};
|
RawConf,
|
||||||
|
#{GwName => #{?AUTHN_BIN => Conf}}
|
||||||
|
)};
|
||||||
_ ->
|
_ ->
|
||||||
badres_authn(already_exist, GwName)
|
badres_authn(already_exist, GwName)
|
||||||
end;
|
end;
|
||||||
pre_config_update(_, {add_authn, GwName, {LType, LName}, Conf}, RawConf) ->
|
pre_config_update(_, {add_authn, GwName, {LType, LName}, Conf}, RawConf) ->
|
||||||
case emqx_map_lib:deep_get(
|
case
|
||||||
[GwName, <<"listeners">>, LType, LName],
|
emqx_map_lib:deep_get(
|
||||||
RawConf, undefined) of
|
[GwName, <<"listeners">>, LType, LName],
|
||||||
|
RawConf,
|
||||||
|
undefined
|
||||||
|
)
|
||||||
|
of
|
||||||
undefined ->
|
undefined ->
|
||||||
badres_listener(not_found, GwName, LType, LName);
|
badres_listener(not_found, GwName, LType, LName);
|
||||||
Listener ->
|
Listener ->
|
||||||
case maps:get(?AUTHN_BIN, Listener, undefined) of
|
case maps:get(?AUTHN_BIN, Listener, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
NListener = maps:put(?AUTHN_BIN, Conf, Listener),
|
NListener = maps:put(?AUTHN_BIN, Conf, Listener),
|
||||||
NGateway = #{GwName =>
|
NGateway = #{
|
||||||
#{<<"listeners">> =>
|
GwName =>
|
||||||
#{LType => #{LName => NListener}}}},
|
#{
|
||||||
|
<<"listeners">> =>
|
||||||
|
#{LType => #{LName => NListener}}
|
||||||
|
}
|
||||||
|
},
|
||||||
{ok, emqx_map_lib:deep_merge(RawConf, NGateway)};
|
{ok, emqx_map_lib:deep_merge(RawConf, NGateway)};
|
||||||
_ ->
|
_ ->
|
||||||
badres_listener_authn(already_exist, GwName, LType, LName)
|
badres_listener_authn(already_exist, GwName, LType, LName)
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
pre_config_update(_, {update_authn, GwName, Conf}, RawConf) ->
|
pre_config_update(_, {update_authn, GwName, Conf}, RawConf) ->
|
||||||
case emqx_map_lib:deep_get(
|
case
|
||||||
[GwName, ?AUTHN_BIN], RawConf, undefined) of
|
emqx_map_lib:deep_get(
|
||||||
|
[GwName, ?AUTHN_BIN], RawConf, undefined
|
||||||
|
)
|
||||||
|
of
|
||||||
undefined ->
|
undefined ->
|
||||||
badres_authn(not_found, GwName);
|
badres_authn(not_found, GwName);
|
||||||
_ ->
|
_ ->
|
||||||
{ok, emqx_map_lib:deep_merge(
|
{ok,
|
||||||
RawConf,
|
emqx_map_lib:deep_merge(
|
||||||
#{GwName => #{?AUTHN_BIN => Conf}})}
|
RawConf,
|
||||||
|
#{GwName => #{?AUTHN_BIN => Conf}}
|
||||||
|
)}
|
||||||
end;
|
end;
|
||||||
pre_config_update(_, {update_authn, GwName, {LType, LName}, Conf}, RawConf) ->
|
pre_config_update(_, {update_authn, GwName, {LType, LName}, Conf}, RawConf) ->
|
||||||
case emqx_map_lib:deep_get(
|
case
|
||||||
[GwName, <<"listeners">>, LType, LName],
|
emqx_map_lib:deep_get(
|
||||||
RawConf, undefined) of
|
[GwName, <<"listeners">>, LType, LName],
|
||||||
|
RawConf,
|
||||||
|
undefined
|
||||||
|
)
|
||||||
|
of
|
||||||
undefined ->
|
undefined ->
|
||||||
badres_listener(not_found, GwName, LType, LName);
|
badres_listener(not_found, GwName, LType, LName);
|
||||||
Listener ->
|
Listener ->
|
||||||
|
|
@ -418,76 +500,116 @@ pre_config_update(_, {update_authn, GwName, {LType, LName}, Conf}, RawConf) ->
|
||||||
badres_listener_authn(not_found, GwName, LType, LName);
|
badres_listener_authn(not_found, GwName, LType, LName);
|
||||||
Auth ->
|
Auth ->
|
||||||
NListener = maps:put(
|
NListener = maps:put(
|
||||||
?AUTHN_BIN,
|
?AUTHN_BIN,
|
||||||
emqx_map_lib:deep_merge(Auth, Conf),
|
emqx_map_lib:deep_merge(Auth, Conf),
|
||||||
Listener
|
Listener
|
||||||
),
|
),
|
||||||
NGateway = #{GwName =>
|
NGateway = #{
|
||||||
#{<<"listeners">> =>
|
GwName =>
|
||||||
#{LType => #{LName => NListener}}}},
|
#{
|
||||||
|
<<"listeners">> =>
|
||||||
|
#{LType => #{LName => NListener}}
|
||||||
|
}
|
||||||
|
},
|
||||||
{ok, emqx_map_lib:deep_merge(RawConf, NGateway)}
|
{ok, emqx_map_lib:deep_merge(RawConf, NGateway)}
|
||||||
end
|
end
|
||||||
end;
|
end;
|
||||||
pre_config_update(_, {remove_authn, GwName}, RawConf) ->
|
pre_config_update(_, {remove_authn, GwName}, RawConf) ->
|
||||||
{ok, emqx_map_lib:deep_remove(
|
{ok,
|
||||||
[GwName, ?AUTHN_BIN], RawConf)};
|
emqx_map_lib:deep_remove(
|
||||||
|
[GwName, ?AUTHN_BIN], RawConf
|
||||||
|
)};
|
||||||
pre_config_update(_, {remove_authn, GwName, {LType, LName}}, RawConf) ->
|
pre_config_update(_, {remove_authn, GwName, {LType, LName}}, RawConf) ->
|
||||||
Path = [GwName, <<"listeners">>, LType, LName, ?AUTHN_BIN],
|
Path = [GwName, <<"listeners">>, LType, LName, ?AUTHN_BIN],
|
||||||
{ok, emqx_map_lib:deep_remove(Path, RawConf)};
|
{ok, emqx_map_lib:deep_remove(Path, RawConf)};
|
||||||
|
|
||||||
pre_config_update(_, UnknownReq, _RawConf) ->
|
pre_config_update(_, UnknownReq, _RawConf) ->
|
||||||
logger:error("Unknown configuration update request: ~0p", [UnknownReq]),
|
logger:error("Unknown configuration update request: ~0p", [UnknownReq]),
|
||||||
{error, badreq}.
|
{error, badreq}.
|
||||||
|
|
||||||
badres_gateway(not_found, GwName) ->
|
badres_gateway(not_found, GwName) ->
|
||||||
{error, {badres, #{resource => gateway, gateway => GwName,
|
{error,
|
||||||
reason => not_found}}};
|
{badres, #{
|
||||||
|
resource => gateway,
|
||||||
|
gateway => GwName,
|
||||||
|
reason => not_found
|
||||||
|
}}};
|
||||||
badres_gateway(already_exist, GwName) ->
|
badres_gateway(already_exist, GwName) ->
|
||||||
{error, {badres, #{resource => gateway, gateway => GwName,
|
{error,
|
||||||
reason => already_exist}}}.
|
{badres, #{
|
||||||
|
resource => gateway,
|
||||||
|
gateway => GwName,
|
||||||
|
reason => already_exist
|
||||||
|
}}}.
|
||||||
|
|
||||||
badres_listener(not_found, GwName, LType, LName) ->
|
badres_listener(not_found, GwName, LType, LName) ->
|
||||||
{error, {badres, #{resource => listener, gateway => GwName,
|
{error,
|
||||||
listener => {GwName, LType, LName},
|
{badres, #{
|
||||||
reason => not_found}}};
|
resource => listener,
|
||||||
|
gateway => GwName,
|
||||||
|
listener => {GwName, LType, LName},
|
||||||
|
reason => not_found
|
||||||
|
}}};
|
||||||
badres_listener(already_exist, GwName, LType, LName) ->
|
badres_listener(already_exist, GwName, LType, LName) ->
|
||||||
{error, {badres, #{resource => listener, gateway => GwName,
|
{error,
|
||||||
listener => {GwName, LType, LName},
|
{badres, #{
|
||||||
reason => already_exist}}}.
|
resource => listener,
|
||||||
|
gateway => GwName,
|
||||||
|
listener => {GwName, LType, LName},
|
||||||
|
reason => already_exist
|
||||||
|
}}}.
|
||||||
|
|
||||||
badres_authn(not_found, GwName) ->
|
badres_authn(not_found, GwName) ->
|
||||||
{error, {badres, #{resource => authn, gateway => GwName,
|
{error,
|
||||||
reason => not_found}}};
|
{badres, #{
|
||||||
|
resource => authn,
|
||||||
|
gateway => GwName,
|
||||||
|
reason => not_found
|
||||||
|
}}};
|
||||||
badres_authn(already_exist, GwName) ->
|
badres_authn(already_exist, GwName) ->
|
||||||
{error, {badres, #{resource => authn, gateway => GwName,
|
{error,
|
||||||
reason => already_exist}}}.
|
{badres, #{
|
||||||
|
resource => authn,
|
||||||
|
gateway => GwName,
|
||||||
|
reason => already_exist
|
||||||
|
}}}.
|
||||||
|
|
||||||
badres_listener_authn(not_found, GwName, LType, LName) ->
|
badres_listener_authn(not_found, GwName, LType, LName) ->
|
||||||
{error, {badres, #{resource => listener_authn, gateway => GwName,
|
{error,
|
||||||
listener => {GwName, LType, LName},
|
{badres, #{
|
||||||
reason => not_found}}};
|
resource => listener_authn,
|
||||||
|
gateway => GwName,
|
||||||
|
listener => {GwName, LType, LName},
|
||||||
|
reason => not_found
|
||||||
|
}}};
|
||||||
badres_listener_authn(already_exist, GwName, LType, LName) ->
|
badres_listener_authn(already_exist, GwName, LType, LName) ->
|
||||||
{error, {badres, #{resource => listener_authn, gateway => GwName,
|
{error,
|
||||||
listener => {GwName, LType, LName},
|
{badres, #{
|
||||||
reason => already_exist}}}.
|
resource => listener_authn,
|
||||||
|
gateway => GwName,
|
||||||
|
listener => {GwName, LType, LName},
|
||||||
|
reason => already_exist
|
||||||
|
}}}.
|
||||||
|
|
||||||
-spec post_config_update(list(atom()),
|
-spec post_config_update(
|
||||||
emqx_config:update_request(),
|
list(atom()),
|
||||||
emqx_config:config(),
|
emqx_config:update_request(),
|
||||||
emqx_config:config(), emqx_config:app_envs())
|
emqx_config:config(),
|
||||||
-> ok | {ok, Result::any()} | {error, Reason::term()}.
|
emqx_config:config(),
|
||||||
|
emqx_config:app_envs()
|
||||||
|
) ->
|
||||||
|
ok | {ok, Result :: any()} | {error, Reason :: term()}.
|
||||||
|
|
||||||
post_config_update(_, Req, NewConfig, OldConfig, _AppEnvs) when is_tuple(Req) ->
|
post_config_update(_, Req, NewConfig, OldConfig, _AppEnvs) when is_tuple(Req) ->
|
||||||
[_Tag, GwName0 | _] = tuple_to_list(Req),
|
[_Tag, GwName0 | _] = tuple_to_list(Req),
|
||||||
GwName = binary_to_existing_atom(GwName0),
|
GwName = binary_to_existing_atom(GwName0),
|
||||||
|
|
||||||
case {maps:get(GwName, NewConfig, undefined),
|
case {maps:get(GwName, NewConfig, undefined), maps:get(GwName, OldConfig, undefined)} of
|
||||||
maps:get(GwName, OldConfig, undefined)} of
|
|
||||||
{undefined, undefined} ->
|
{undefined, undefined} ->
|
||||||
ok; %% nothing to change
|
%% nothing to change
|
||||||
|
ok;
|
||||||
{undefined, Old} when is_map(Old) ->
|
{undefined, Old} when is_map(Old) ->
|
||||||
emqx_gateway:unload(GwName);
|
emqx_gateway:unload(GwName);
|
||||||
{New, undefined} when is_map(New) ->
|
{New, undefined} when is_map(New) ->
|
||||||
emqx_gateway:load(GwName, New);
|
emqx_gateway:load(GwName, New);
|
||||||
{New, Old} when is_map(New), is_map(Old) ->
|
{New, Old} when is_map(New), is_map(Old) ->
|
||||||
emqx_gateway:update(GwName, New)
|
emqx_gateway:update(GwName, New)
|
||||||
|
|
@ -499,29 +621,39 @@ post_config_update(_, _Req, _NewConfig, _OldConfig, _AppEnvs) ->
|
||||||
%% Internal funcs
|
%% Internal funcs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
tune_gw_certs(Fun, GwName, Conf) ->
|
tune_gw_certs(Fun, GwName, Conf) ->
|
||||||
SubDir = certs_dir(GwName),
|
SubDir = certs_dir(GwName),
|
||||||
case maps:get(<<"listeners">>, Conf, undefined) of
|
case maps:get(<<"listeners">>, Conf, undefined) of
|
||||||
undefined -> Conf;
|
undefined ->
|
||||||
|
Conf;
|
||||||
Liss ->
|
Liss ->
|
||||||
maps:put(<<"listeners">>,
|
maps:put(
|
||||||
maps:map(fun(_, Lis) ->
|
<<"listeners">>,
|
||||||
maps:map(fun(_, LisConf) ->
|
maps:map(
|
||||||
erlang:apply(Fun, [SubDir, LisConf])
|
fun(_, Lis) ->
|
||||||
end, Lis)
|
maps:map(
|
||||||
end, Liss),
|
fun(_, LisConf) ->
|
||||||
Conf)
|
erlang:apply(Fun, [SubDir, LisConf])
|
||||||
end.
|
end,
|
||||||
|
Lis
|
||||||
|
)
|
||||||
|
end,
|
||||||
|
Liss
|
||||||
|
),
|
||||||
|
Conf
|
||||||
|
)
|
||||||
|
end.
|
||||||
|
|
||||||
certs_dir(GwName) when is_binary(GwName) ->
|
certs_dir(GwName) when is_binary(GwName) ->
|
||||||
GwName.
|
GwName.
|
||||||
|
|
||||||
convert_certs(SubDir, Conf) ->
|
convert_certs(SubDir, Conf) ->
|
||||||
case emqx_tls_lib:ensure_ssl_files(
|
case
|
||||||
SubDir,
|
emqx_tls_lib:ensure_ssl_files(
|
||||||
maps:get(<<"ssl">>, Conf, undefined)
|
SubDir,
|
||||||
) of
|
maps:get(<<"ssl">>, Conf, undefined)
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, SSL} ->
|
{ok, SSL} ->
|
||||||
new_ssl_config(Conf, SSL);
|
new_ssl_config(Conf, SSL);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
-include("include/emqx_gateway.hrl").
|
-include("include/emqx_gateway.hrl").
|
||||||
|
|
||||||
|
|
||||||
%% @doc The running context for a Connection/Channel process.
|
%% @doc The running context for a Connection/Channel process.
|
||||||
%%
|
%%
|
||||||
%% The `Context` encapsulates a complex structure of contextual information.
|
%% The `Context` encapsulates a complex structure of contextual information.
|
||||||
|
|
@ -27,45 +26,50 @@
|
||||||
%% configuration, register devices and other common operations.
|
%% configuration, register devices and other common operations.
|
||||||
%%
|
%%
|
||||||
-type context() ::
|
-type context() ::
|
||||||
#{ %% Gateway Name
|
%% Gateway Name
|
||||||
gwname := gateway_name()
|
#{
|
||||||
%% Authentication chains
|
gwname := gateway_name(),
|
||||||
, auth := [emqx_authentication:chain_name()]
|
%% Authentication chains
|
||||||
%% The ConnectionManager PID
|
auth := [emqx_authentication:chain_name()],
|
||||||
, cm := pid()
|
%% The ConnectionManager PID
|
||||||
}.
|
cm := pid()
|
||||||
|
}.
|
||||||
|
|
||||||
%% Authentication circle
|
%% Authentication circle
|
||||||
-export([ authenticate/2
|
-export([
|
||||||
, open_session/5
|
authenticate/2,
|
||||||
, open_session/6
|
open_session/5,
|
||||||
, insert_channel_info/4
|
open_session/6,
|
||||||
, set_chan_info/3
|
insert_channel_info/4,
|
||||||
, set_chan_stats/3
|
set_chan_info/3,
|
||||||
, connection_closed/2
|
set_chan_stats/3,
|
||||||
]).
|
connection_closed/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% Message circle
|
%% Message circle
|
||||||
-export([ authorize/4
|
-export([
|
||||||
% Needless for pub/sub
|
authorize/4
|
||||||
%, publish/3
|
% Needless for pub/sub
|
||||||
%, subscribe/4
|
%, publish/3
|
||||||
]).
|
%, subscribe/4
|
||||||
|
]).
|
||||||
|
|
||||||
%% Metrics & Stats
|
%% Metrics & Stats
|
||||||
-export([ metrics_inc/2
|
-export([
|
||||||
, metrics_inc/3
|
metrics_inc/2,
|
||||||
]).
|
metrics_inc/3
|
||||||
|
]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Authentication circle
|
%% Authentication circle
|
||||||
|
|
||||||
%% @doc Authenticate whether the client has access to the Broker.
|
%% @doc Authenticate whether the client has access to the Broker.
|
||||||
-spec authenticate(context(), emqx_types:clientinfo())
|
-spec authenticate(context(), emqx_types:clientinfo()) ->
|
||||||
-> {ok, emqx_types:clientinfo()}
|
{ok, emqx_types:clientinfo()}
|
||||||
| {error, any()}.
|
| {error, any()}.
|
||||||
authenticate(_Ctx = #{auth := _ChainNames}, ClientInfo0)
|
authenticate(_Ctx = #{auth := _ChainNames}, ClientInfo0) when
|
||||||
when is_list(_ChainNames) ->
|
is_list(_ChainNames)
|
||||||
|
->
|
||||||
ClientInfo = ClientInfo0#{zone => default},
|
ClientInfo = ClientInfo0#{zone => default},
|
||||||
case emqx_access_control:authenticate(ClientInfo) of
|
case emqx_access_control:authenticate(ClientInfo) of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
|
|
@ -78,43 +82,74 @@ authenticate(_Ctx = #{auth := _ChainNames}, ClientInfo0)
|
||||||
%%
|
%%
|
||||||
%% This function should be called after the client has authenticated
|
%% This function should be called after the client has authenticated
|
||||||
%% successfully so that the client can be managed in the cluster.
|
%% successfully so that the client can be managed in the cluster.
|
||||||
-spec open_session(context(), boolean(), emqx_types:clientinfo(),
|
-spec open_session(
|
||||||
emqx_types:conninfo(),
|
context(),
|
||||||
fun((emqx_types:clientinfo(),
|
boolean(),
|
||||||
emqx_types:conninfo()) -> Session)
|
emqx_types:clientinfo(),
|
||||||
)
|
emqx_types:conninfo(),
|
||||||
-> {ok, #{session := Session,
|
fun(
|
||||||
present := boolean(),
|
(
|
||||||
pendings => list()
|
emqx_types:clientinfo(),
|
||||||
}}
|
emqx_types:conninfo()
|
||||||
| {error, any()}.
|
) -> Session
|
||||||
|
)
|
||||||
|
) ->
|
||||||
|
{ok, #{
|
||||||
|
session := Session,
|
||||||
|
present := boolean(),
|
||||||
|
pendings => list()
|
||||||
|
}}
|
||||||
|
| {error, any()}.
|
||||||
open_session(Ctx, CleanStart, ClientInfo, ConnInfo, CreateSessionFun) ->
|
open_session(Ctx, CleanStart, ClientInfo, ConnInfo, CreateSessionFun) ->
|
||||||
open_session(Ctx, CleanStart, ClientInfo, ConnInfo,
|
open_session(
|
||||||
CreateSessionFun, emqx_session).
|
Ctx,
|
||||||
|
CleanStart,
|
||||||
|
ClientInfo,
|
||||||
|
ConnInfo,
|
||||||
|
CreateSessionFun,
|
||||||
|
emqx_session
|
||||||
|
).
|
||||||
|
|
||||||
open_session(_Ctx = #{gwname := GwName},
|
open_session(
|
||||||
CleanStart, ClientInfo, ConnInfo, CreateSessionFun, SessionMod) ->
|
_Ctx = #{gwname := GwName},
|
||||||
emqx_gateway_cm:open_session(GwName, CleanStart,
|
CleanStart,
|
||||||
ClientInfo, ConnInfo,
|
ClientInfo,
|
||||||
CreateSessionFun, SessionMod).
|
ConnInfo,
|
||||||
|
CreateSessionFun,
|
||||||
|
SessionMod
|
||||||
|
) ->
|
||||||
|
emqx_gateway_cm:open_session(
|
||||||
|
GwName,
|
||||||
|
CleanStart,
|
||||||
|
ClientInfo,
|
||||||
|
ConnInfo,
|
||||||
|
CreateSessionFun,
|
||||||
|
SessionMod
|
||||||
|
).
|
||||||
|
|
||||||
-spec insert_channel_info(context(),
|
-spec insert_channel_info(
|
||||||
emqx_types:clientid(),
|
context(),
|
||||||
emqx_types:infos(),
|
emqx_types:clientid(),
|
||||||
emqx_types:stats()) -> ok.
|
emqx_types:infos(),
|
||||||
|
emqx_types:stats()
|
||||||
|
) -> ok.
|
||||||
insert_channel_info(_Ctx = #{gwname := GwName}, ClientId, Infos, Stats) ->
|
insert_channel_info(_Ctx = #{gwname := GwName}, ClientId, Infos, Stats) ->
|
||||||
emqx_gateway_cm:insert_channel_info(GwName, ClientId, Infos, Stats).
|
emqx_gateway_cm:insert_channel_info(GwName, ClientId, Infos, Stats).
|
||||||
|
|
||||||
%% @doc Set the Channel Info to the ConnectionManager for this client
|
%% @doc Set the Channel Info to the ConnectionManager for this client
|
||||||
-spec set_chan_info(context(),
|
-spec set_chan_info(
|
||||||
emqx_types:clientid(),
|
context(),
|
||||||
emqx_types:infos()) -> boolean().
|
emqx_types:clientid(),
|
||||||
|
emqx_types:infos()
|
||||||
|
) -> boolean().
|
||||||
set_chan_info(_Ctx = #{gwname := GwName}, ClientId, Infos) ->
|
set_chan_info(_Ctx = #{gwname := GwName}, ClientId, Infos) ->
|
||||||
emqx_gateway_cm:set_chan_info(GwName, ClientId, Infos).
|
emqx_gateway_cm:set_chan_info(GwName, ClientId, Infos).
|
||||||
|
|
||||||
-spec set_chan_stats(context(),
|
-spec set_chan_stats(
|
||||||
emqx_types:clientid(),
|
context(),
|
||||||
emqx_types:stats()) -> boolean().
|
emqx_types:clientid(),
|
||||||
|
emqx_types:stats()
|
||||||
|
) -> boolean().
|
||||||
set_chan_stats(_Ctx = #{gwname := GwName}, ClientId, Stats) ->
|
set_chan_stats(_Ctx = #{gwname := GwName}, ClientId, Stats) ->
|
||||||
emqx_gateway_cm:set_chan_stats(GwName, ClientId, Stats).
|
emqx_gateway_cm:set_chan_stats(GwName, ClientId, Stats).
|
||||||
|
|
||||||
|
|
@ -122,9 +157,13 @@ set_chan_stats(_Ctx = #{gwname := GwName}, ClientId, Stats) ->
|
||||||
connection_closed(_Ctx = #{gwname := GwName}, ClientId) ->
|
connection_closed(_Ctx = #{gwname := GwName}, ClientId) ->
|
||||||
emqx_gateway_cm:connection_closed(GwName, ClientId).
|
emqx_gateway_cm:connection_closed(GwName, ClientId).
|
||||||
|
|
||||||
-spec authorize(context(), emqx_types:clientinfo(),
|
-spec authorize(
|
||||||
emqx_types:pubsub(), emqx_types:topic())
|
context(),
|
||||||
-> allow | deny.
|
emqx_types:clientinfo(),
|
||||||
|
emqx_types:pubsub(),
|
||||||
|
emqx_types:topic()
|
||||||
|
) ->
|
||||||
|
allow | deny.
|
||||||
authorize(_Ctx, ClientInfo, PubSub, Topic) ->
|
authorize(_Ctx, ClientInfo, PubSub, Topic) ->
|
||||||
emqx_access_control:authorize(ClientInfo, PubSub, Topic).
|
emqx_access_control:authorize(ClientInfo, PubSub, Topic).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,13 +27,14 @@
|
||||||
|
|
||||||
-export([start_link/1]).
|
-export([start_link/1]).
|
||||||
|
|
||||||
-export([ create_insta/3
|
-export([
|
||||||
, remove_insta/2
|
create_insta/3,
|
||||||
, update_insta/3
|
remove_insta/2,
|
||||||
, start_insta/2
|
update_insta/3,
|
||||||
, stop_insta/2
|
start_insta/2,
|
||||||
, list_insta/1
|
stop_insta/2,
|
||||||
]).
|
list_insta/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% Supervisor callbacks
|
%% Supervisor callbacks
|
||||||
-export([init/1]).
|
-export([init/1]).
|
||||||
|
|
@ -48,70 +49,72 @@ start_link(GwName) ->
|
||||||
-spec create_insta(pid(), gateway(), map()) -> {ok, GwInstaPid :: pid()} | {error, any()}.
|
-spec create_insta(pid(), gateway(), map()) -> {ok, GwInstaPid :: pid()} | {error, any()}.
|
||||||
create_insta(Sup, Gateway = #{name := Name}, GwDscrptr) ->
|
create_insta(Sup, Gateway = #{name := Name}, GwDscrptr) ->
|
||||||
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
||||||
{ok, _GwInstaPid} -> {error, alredy_existed};
|
{ok, _GwInstaPid} ->
|
||||||
|
{error, alredy_existed};
|
||||||
false ->
|
false ->
|
||||||
Ctx = ctx(Sup, Name),
|
Ctx = ctx(Sup, Name),
|
||||||
ChildSpec = emqx_gateway_utils:childspec(
|
ChildSpec = emqx_gateway_utils:childspec(
|
||||||
Name,
|
Name,
|
||||||
worker,
|
worker,
|
||||||
emqx_gateway_insta_sup,
|
emqx_gateway_insta_sup,
|
||||||
[Gateway, Ctx, GwDscrptr]
|
[Gateway, Ctx, GwDscrptr]
|
||||||
),
|
),
|
||||||
emqx_gateway_utils:supervisor_ret(
|
emqx_gateway_utils:supervisor_ret(
|
||||||
supervisor:start_child(Sup, ChildSpec)
|
supervisor:start_child(Sup, ChildSpec)
|
||||||
)
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec remove_insta(pid(), Name :: gateway_name()) -> ok | {error, any()}.
|
-spec remove_insta(pid(), Name :: gateway_name()) -> ok | {error, any()}.
|
||||||
remove_insta(Sup, Name) ->
|
remove_insta(Sup, Name) ->
|
||||||
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
||||||
false -> ok;
|
false ->
|
||||||
|
ok;
|
||||||
{ok, _GwInstaPid} ->
|
{ok, _GwInstaPid} ->
|
||||||
ok = supervisor:terminate_child(Sup, Name),
|
ok = supervisor:terminate_child(Sup, Name),
|
||||||
ok = supervisor:delete_child(Sup, Name)
|
ok = supervisor:delete_child(Sup, Name)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec update_insta(pid(), gateway_name(), emqx_config:config())
|
-spec update_insta(pid(), gateway_name(), emqx_config:config()) ->
|
||||||
-> ok | {error, any()}.
|
ok | {error, any()}.
|
||||||
update_insta(Sup, Name, Config) ->
|
update_insta(Sup, Name, Config) ->
|
||||||
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
||||||
false -> {error, not_found};
|
false -> {error, not_found};
|
||||||
{ok, GwInstaPid} ->
|
{ok, GwInstaPid} -> emqx_gateway_insta_sup:update(GwInstaPid, Config)
|
||||||
emqx_gateway_insta_sup:update(GwInstaPid, Config)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec start_insta(pid(), gateway_name()) -> ok | {error, any()}.
|
-spec start_insta(pid(), gateway_name()) -> ok | {error, any()}.
|
||||||
start_insta(Sup, Name) ->
|
start_insta(Sup, Name) ->
|
||||||
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
||||||
false -> {error, not_found};
|
false -> {error, not_found};
|
||||||
{ok, GwInstaPid} ->
|
{ok, GwInstaPid} -> emqx_gateway_insta_sup:enable(GwInstaPid)
|
||||||
emqx_gateway_insta_sup:enable(GwInstaPid)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec stop_insta(pid(), gateway_name()) -> ok | {error, any()}.
|
-spec stop_insta(pid(), gateway_name()) -> ok | {error, any()}.
|
||||||
stop_insta(Sup, Name) ->
|
stop_insta(Sup, Name) ->
|
||||||
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
case emqx_gateway_utils:find_sup_child(Sup, Name) of
|
||||||
false -> {error, not_found};
|
false -> {error, not_found};
|
||||||
{ok, GwInstaPid} ->
|
{ok, GwInstaPid} -> emqx_gateway_insta_sup:disable(GwInstaPid)
|
||||||
emqx_gateway_insta_sup:disable(GwInstaPid)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec list_insta(pid()) -> [gateway()].
|
-spec list_insta(pid()) -> [gateway()].
|
||||||
list_insta(Sup) ->
|
list_insta(Sup) ->
|
||||||
lists:filtermap(
|
lists:filtermap(
|
||||||
fun({Name, GwInstaPid, _Type, _Mods}) ->
|
fun({Name, GwInstaPid, _Type, _Mods}) ->
|
||||||
is_gateway_insta_id(Name)
|
is_gateway_insta_id(Name) andalso
|
||||||
andalso {true, emqx_gateway_insta_sup:info(GwInstaPid)}
|
{true, emqx_gateway_insta_sup:info(GwInstaPid)}
|
||||||
end, supervisor:which_children(Sup)).
|
end,
|
||||||
|
supervisor:which_children(Sup)
|
||||||
|
).
|
||||||
|
|
||||||
%% Supervisor callback
|
%% Supervisor callback
|
||||||
|
|
||||||
%% @doc Initialize Top Supervisor for a Protocol
|
%% @doc Initialize Top Supervisor for a Protocol
|
||||||
init([GwName]) ->
|
init([GwName]) ->
|
||||||
SupFlags = #{ strategy => one_for_one
|
SupFlags = #{
|
||||||
, intensity => 10
|
strategy => one_for_one,
|
||||||
, period => 60
|
intensity => 10,
|
||||||
},
|
period => 60
|
||||||
|
},
|
||||||
CmOpts = [{gwname, GwName}],
|
CmOpts = [{gwname, GwName}],
|
||||||
CM = emqx_gateway_utils:childspec(worker, emqx_gateway_cm, [CmOpts]),
|
CM = emqx_gateway_utils:childspec(worker, emqx_gateway_cm, [CmOpts]),
|
||||||
Metrics = emqx_gateway_utils:childspec(worker, emqx_gateway_metrics, [GwName]),
|
Metrics = emqx_gateway_utils:childspec(worker, emqx_gateway_metrics, [GwName]),
|
||||||
|
|
@ -122,10 +125,11 @@ init([GwName]) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
ctx(Sup, Name) ->
|
ctx(Sup, Name) ->
|
||||||
{ok, CM} = emqx_gateway_utils:find_sup_child(Sup, emqx_gateway_cm),
|
{ok, CM} = emqx_gateway_utils:find_sup_child(Sup, emqx_gateway_cm),
|
||||||
#{ gwname => Name
|
#{
|
||||||
, cm => CM
|
gwname => Name,
|
||||||
}.
|
cm => CM
|
||||||
|
}.
|
||||||
|
|
||||||
is_gateway_insta_id(emqx_gateway_cm) ->
|
is_gateway_insta_id(emqx_gateway_cm) ->
|
||||||
false;
|
false;
|
||||||
|
|
|
||||||
|
|
@ -26,58 +26,63 @@
|
||||||
-import(emqx_gateway_utils, [listener_id/3]).
|
-import(emqx_gateway_utils, [listener_id/3]).
|
||||||
|
|
||||||
%% Mgmt APIs - gateway
|
%% Mgmt APIs - gateway
|
||||||
-export([ gateways/1
|
-export([gateways/1]).
|
||||||
]).
|
|
||||||
|
|
||||||
%% Mgmt APIs
|
%% Mgmt APIs
|
||||||
-export([ add_listener/2
|
-export([
|
||||||
, remove_listener/1
|
add_listener/2,
|
||||||
, update_listener/2
|
remove_listener/1,
|
||||||
]).
|
update_listener/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ authn/1
|
-export([
|
||||||
, authn/2
|
authn/1,
|
||||||
, add_authn/2
|
authn/2,
|
||||||
, add_authn/3
|
add_authn/2,
|
||||||
, update_authn/2
|
add_authn/3,
|
||||||
, update_authn/3
|
update_authn/2,
|
||||||
, remove_authn/1
|
update_authn/3,
|
||||||
, remove_authn/2
|
remove_authn/1,
|
||||||
]).
|
remove_authn/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% Mgmt APIs - clients
|
%% Mgmt APIs - clients
|
||||||
-export([ lookup_client/3
|
-export([
|
||||||
, kickout_client/2
|
lookup_client/3,
|
||||||
, list_client_subscriptions/2
|
kickout_client/2,
|
||||||
, client_subscribe/4
|
list_client_subscriptions/2,
|
||||||
, client_unsubscribe/3
|
client_subscribe/4,
|
||||||
]).
|
client_unsubscribe/3
|
||||||
|
]).
|
||||||
|
|
||||||
%% Utils for http, swagger, etc.
|
%% Utils for http, swagger, etc.
|
||||||
-export([ return_http_error/2
|
-export([
|
||||||
, with_gateway/2
|
return_http_error/2,
|
||||||
, with_authn/2
|
with_gateway/2,
|
||||||
, with_listener_authn/3
|
with_authn/2,
|
||||||
, checks/2
|
with_listener_authn/3,
|
||||||
, reason2resp/1
|
checks/2,
|
||||||
, reason2msg/1
|
reason2resp/1,
|
||||||
]).
|
reason2msg/1
|
||||||
|
]).
|
||||||
|
|
||||||
-type gateway_summary() ::
|
-type gateway_summary() ::
|
||||||
#{ name := binary()
|
#{
|
||||||
, status := running | stopped | unloaded
|
name := binary(),
|
||||||
, created_at => binary()
|
status := running | stopped | unloaded,
|
||||||
, started_at => binary()
|
created_at => binary(),
|
||||||
, stopped_at => binary()
|
started_at => binary(),
|
||||||
, max_connections => integer()
|
stopped_at => binary(),
|
||||||
, current_connections => integer()
|
max_connections => integer(),
|
||||||
, listeners => []
|
current_connections => integer(),
|
||||||
}.
|
listeners => []
|
||||||
|
}.
|
||||||
|
|
||||||
-elvis([ {elvis_style, god_modules, disable}
|
-elvis([
|
||||||
, {elvis_style, no_nested_try_catch, disable}
|
{elvis_style, god_modules, disable},
|
||||||
, {elvis_style, invalid_dynamic_call, disable}
|
{elvis_style, no_nested_try_catch, disable},
|
||||||
]).
|
{elvis_style, invalid_dynamic_call, disable}
|
||||||
|
]).
|
||||||
|
|
||||||
-define(DEFAULT_CALL_TIMEOUT, 15000).
|
-define(DEFAULT_CALL_TIMEOUT, 15000).
|
||||||
|
|
||||||
|
|
@ -85,63 +90,81 @@
|
||||||
%% Mgmt APIs - gateway
|
%% Mgmt APIs - gateway
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec gateways(Status :: all | running | stopped | unloaded)
|
-spec gateways(Status :: all | running | stopped | unloaded) ->
|
||||||
-> [gateway_summary()].
|
[gateway_summary()].
|
||||||
gateways(Status) ->
|
gateways(Status) ->
|
||||||
Gateways = lists:map(fun({GwName, _}) ->
|
Gateways = lists:map(
|
||||||
case emqx_gateway:lookup(GwName) of
|
fun({GwName, _}) ->
|
||||||
undefined -> #{name => GwName, status => unloaded};
|
case emqx_gateway:lookup(GwName) of
|
||||||
GwInfo = #{config := Config} ->
|
undefined ->
|
||||||
GwInfo0 = emqx_gateway_utils:unix_ts_to_rfc3339(
|
#{name => GwName, status => unloaded};
|
||||||
[created_at, started_at, stopped_at],
|
GwInfo = #{config := Config} ->
|
||||||
GwInfo),
|
GwInfo0 = emqx_gateway_utils:unix_ts_to_rfc3339(
|
||||||
GwInfo1 = maps:with([name,
|
[created_at, started_at, stopped_at],
|
||||||
status,
|
GwInfo
|
||||||
created_at,
|
),
|
||||||
started_at,
|
GwInfo1 = maps:with(
|
||||||
stopped_at], GwInfo0),
|
[
|
||||||
GwInfo1#{
|
name,
|
||||||
max_connections => max_connections_count(Config),
|
status,
|
||||||
current_connections => current_connections_count(GwName),
|
created_at,
|
||||||
listeners => get_listeners_status(GwName, Config)}
|
started_at,
|
||||||
end
|
stopped_at
|
||||||
end, emqx_gateway_registry:list()),
|
],
|
||||||
|
GwInfo0
|
||||||
|
),
|
||||||
|
GwInfo1#{
|
||||||
|
max_connections => max_connections_count(Config),
|
||||||
|
current_connections => current_connections_count(GwName),
|
||||||
|
listeners => get_listeners_status(GwName, Config)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
emqx_gateway_registry:list()
|
||||||
|
),
|
||||||
case Status of
|
case Status of
|
||||||
all -> Gateways;
|
all -> Gateways;
|
||||||
_ ->
|
_ -> [Gw || Gw = #{status := S} <- Gateways, S == Status]
|
||||||
[Gw || Gw = #{status := S} <- Gateways, S == Status]
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
max_connections_count(Config) ->
|
max_connections_count(Config) ->
|
||||||
Listeners = emqx_gateway_utils:normalize_config(Config),
|
Listeners = emqx_gateway_utils:normalize_config(Config),
|
||||||
lists:foldl(fun({_, _, _, SocketOpts, _}, Acc) ->
|
lists:foldl(
|
||||||
Acc + proplists:get_value(max_connections, SocketOpts, 0)
|
fun({_, _, _, SocketOpts, _}, Acc) ->
|
||||||
end, 0, Listeners).
|
Acc + proplists:get_value(max_connections, SocketOpts, 0)
|
||||||
|
end,
|
||||||
|
0,
|
||||||
|
Listeners
|
||||||
|
).
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
current_connections_count(GwName) ->
|
current_connections_count(GwName) ->
|
||||||
try
|
try
|
||||||
InfoTab = emqx_gateway_cm:tabname(info, GwName),
|
InfoTab = emqx_gateway_cm:tabname(info, GwName),
|
||||||
ets:info(InfoTab, size)
|
ets:info(InfoTab, size)
|
||||||
catch _ : _ ->
|
catch
|
||||||
0
|
_:_ ->
|
||||||
|
0
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
get_listeners_status(GwName, Config) ->
|
get_listeners_status(GwName, Config) ->
|
||||||
Listeners = emqx_gateway_utils:normalize_config(Config),
|
Listeners = emqx_gateway_utils:normalize_config(Config),
|
||||||
lists:map(fun({Type, LisName, ListenOn, _, _}) ->
|
lists:map(
|
||||||
Name0 = listener_id(GwName, Type, LisName),
|
fun({Type, LisName, ListenOn, _, _}) ->
|
||||||
Name = {Name0, ListenOn},
|
Name0 = listener_id(GwName, Type, LisName),
|
||||||
LisO = #{id => Name0, type => Type, name => LisName},
|
Name = {Name0, ListenOn},
|
||||||
case catch esockd:listener(Name) of
|
LisO = #{id => Name0, type => Type, name => LisName},
|
||||||
_Pid when is_pid(_Pid) ->
|
case catch esockd:listener(Name) of
|
||||||
LisO#{running => true};
|
_Pid when is_pid(_Pid) ->
|
||||||
_ ->
|
LisO#{running => true};
|
||||||
LisO#{running => false}
|
_ ->
|
||||||
end
|
LisO#{running => false}
|
||||||
end, Listeners).
|
end
|
||||||
|
end,
|
||||||
|
Listeners
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Mgmt APIs - listeners
|
%% Mgmt APIs - listeners
|
||||||
|
|
@ -150,16 +173,30 @@ get_listeners_status(GwName, Config) ->
|
||||||
-spec add_listener(atom() | binary(), map()) -> {ok, map()}.
|
-spec add_listener(atom() | binary(), map()) -> {ok, map()}.
|
||||||
add_listener(ListenerId, NewConf0) ->
|
add_listener(ListenerId, NewConf0) ->
|
||||||
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
||||||
NewConf = maps:without([<<"id">>, <<"name">>,
|
NewConf = maps:without(
|
||||||
<<"type">>, <<"running">>], NewConf0),
|
[
|
||||||
|
<<"id">>,
|
||||||
|
<<"name">>,
|
||||||
|
<<"type">>,
|
||||||
|
<<"running">>
|
||||||
|
],
|
||||||
|
NewConf0
|
||||||
|
),
|
||||||
confexp(emqx_gateway_conf:add_listener(GwName, {Type, Name}, NewConf)).
|
confexp(emqx_gateway_conf:add_listener(GwName, {Type, Name}, NewConf)).
|
||||||
|
|
||||||
-spec update_listener(atom() | binary(), map()) -> {ok, map()}.
|
-spec update_listener(atom() | binary(), map()) -> {ok, map()}.
|
||||||
update_listener(ListenerId, NewConf0) ->
|
update_listener(ListenerId, NewConf0) ->
|
||||||
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
{GwName, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
||||||
|
|
||||||
NewConf = maps:without([<<"id">>, <<"name">>,
|
NewConf = maps:without(
|
||||||
<<"type">>, <<"running">>], NewConf0),
|
[
|
||||||
|
<<"id">>,
|
||||||
|
<<"name">>,
|
||||||
|
<<"type">>,
|
||||||
|
<<"running">>
|
||||||
|
],
|
||||||
|
NewConf0
|
||||||
|
),
|
||||||
confexp(emqx_gateway_conf:update_listener(GwName, {Type, Name}, NewConf)).
|
confexp(emqx_gateway_conf:update_listener(GwName, {Type, Name}, NewConf)).
|
||||||
|
|
||||||
-spec remove_listener(binary()) -> ok.
|
-spec remove_listener(binary()) -> ok.
|
||||||
|
|
@ -173,9 +210,9 @@ authn(GwName) ->
|
||||||
Path = [gateway, GwName, ?AUTHN],
|
Path = [gateway, GwName, ?AUTHN],
|
||||||
ChainName = emqx_gateway_utils:global_chain(GwName),
|
ChainName = emqx_gateway_utils:global_chain(GwName),
|
||||||
wrap_chain_name(
|
wrap_chain_name(
|
||||||
ChainName,
|
ChainName,
|
||||||
emqx_map_lib:jsonable_map(emqx:get_config(Path))
|
emqx_map_lib:jsonable_map(emqx:get_config(Path))
|
||||||
).
|
).
|
||||||
|
|
||||||
-spec authn(gateway_name(), binary()) -> map().
|
-spec authn(gateway_name(), binary()) -> map().
|
||||||
authn(GwName, ListenerId) ->
|
authn(GwName, ListenerId) ->
|
||||||
|
|
@ -183,9 +220,9 @@ authn(GwName, ListenerId) ->
|
||||||
Path = [gateway, GwName, listeners, Type, Name, ?AUTHN],
|
Path = [gateway, GwName, listeners, Type, Name, ?AUTHN],
|
||||||
ChainName = emqx_gateway_utils:listener_chain(GwName, Type, Name),
|
ChainName = emqx_gateway_utils:listener_chain(GwName, Type, Name),
|
||||||
wrap_chain_name(
|
wrap_chain_name(
|
||||||
ChainName,
|
ChainName,
|
||||||
emqx_map_lib:jsonable_map(emqx:get_config(Path))
|
emqx_map_lib:jsonable_map(emqx:get_config(Path))
|
||||||
).
|
).
|
||||||
|
|
||||||
wrap_chain_name(ChainName, Conf) ->
|
wrap_chain_name(ChainName, Conf) ->
|
||||||
case emqx_authentication:list_authenticators(ChainName) of
|
case emqx_authentication:list_authenticators(ChainName) of
|
||||||
|
|
@ -230,66 +267,92 @@ confexp({error, Reason}) -> error(Reason).
|
||||||
%% Mgmt APIs - clients
|
%% Mgmt APIs - clients
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec lookup_client(gateway_name(),
|
-spec lookup_client(
|
||||||
emqx_types:clientid(), {module(), atom()}) -> list().
|
gateway_name(),
|
||||||
|
emqx_types:clientid(),
|
||||||
|
{module(), atom()}
|
||||||
|
) -> list().
|
||||||
lookup_client(GwName, ClientId, {M, F}) ->
|
lookup_client(GwName, ClientId, {M, F}) ->
|
||||||
[begin
|
[
|
||||||
Info = emqx_gateway_cm:get_chan_info(GwName, ClientId, Pid),
|
begin
|
||||||
Stats = emqx_gateway_cm:get_chan_stats(GwName, ClientId, Pid),
|
Info = emqx_gateway_cm:get_chan_info(GwName, ClientId, Pid),
|
||||||
M:F({{ClientId, Pid}, Info, Stats})
|
Stats = emqx_gateway_cm:get_chan_stats(GwName, ClientId, Pid),
|
||||||
end
|
M:F({{ClientId, Pid}, Info, Stats})
|
||||||
|| Pid <- emqx_gateway_cm:lookup_by_clientid(GwName, ClientId)].
|
end
|
||||||
|
|| Pid <- emqx_gateway_cm:lookup_by_clientid(GwName, ClientId)
|
||||||
|
].
|
||||||
|
|
||||||
-spec kickout_client(gateway_name(), emqx_types:clientid())
|
-spec kickout_client(gateway_name(), emqx_types:clientid()) ->
|
||||||
-> {error, any()}
|
{error, any()}
|
||||||
| ok.
|
| ok.
|
||||||
kickout_client(GwName, ClientId) ->
|
kickout_client(GwName, ClientId) ->
|
||||||
Results = [emqx_gateway_cm:kick_session(GwName, ClientId, Pid)
|
Results = [
|
||||||
|| Pid <- emqx_gateway_cm:lookup_by_clientid(GwName, ClientId)],
|
emqx_gateway_cm:kick_session(GwName, ClientId, Pid)
|
||||||
|
|| Pid <- emqx_gateway_cm:lookup_by_clientid(GwName, ClientId)
|
||||||
|
],
|
||||||
IsOk = lists:any(fun(Item) -> Item =:= ok end, Results),
|
IsOk = lists:any(fun(Item) -> Item =:= ok end, Results),
|
||||||
case {IsOk, Results} of
|
case {IsOk, Results} of
|
||||||
{true , _ } -> ok;
|
{true, _} -> ok;
|
||||||
{_ , []} -> {error, not_found};
|
{_, []} -> {error, not_found};
|
||||||
{false, _ } -> lists:last(Results)
|
{false, _} -> lists:last(Results)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec list_client_subscriptions(gateway_name(), emqx_types:clientid())
|
-spec list_client_subscriptions(gateway_name(), emqx_types:clientid()) ->
|
||||||
-> {error, any()}
|
{error, any()}
|
||||||
| {ok, list()}.
|
| {ok, list()}.
|
||||||
list_client_subscriptions(GwName, ClientId) ->
|
list_client_subscriptions(GwName, ClientId) ->
|
||||||
case client_call(GwName, ClientId, subscriptions) of
|
case client_call(GwName, ClientId, subscriptions) of
|
||||||
{error, Reason} -> {error, Reason};
|
{error, Reason} ->
|
||||||
|
{error, Reason};
|
||||||
{ok, Subs} ->
|
{ok, Subs} ->
|
||||||
{ok, lists:map(fun({Topic, SubOpts}) ->
|
{ok,
|
||||||
SubOpts#{topic => Topic}
|
lists:map(
|
||||||
end, Subs)}
|
fun({Topic, SubOpts}) ->
|
||||||
|
SubOpts#{topic => Topic}
|
||||||
|
end,
|
||||||
|
Subs
|
||||||
|
)}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec client_subscribe(gateway_name(), emqx_types:clientid(),
|
-spec client_subscribe(
|
||||||
emqx_types:topic(), emqx_types:subopts())
|
gateway_name(),
|
||||||
-> {error, any()}
|
emqx_types:clientid(),
|
||||||
| {ok, {emqx_types:topic(), emqx_types:subopts()}}.
|
emqx_types:topic(),
|
||||||
|
emqx_types:subopts()
|
||||||
|
) ->
|
||||||
|
{error, any()}
|
||||||
|
| {ok, {emqx_types:topic(), emqx_types:subopts()}}.
|
||||||
client_subscribe(GwName, ClientId, Topic, SubOpts) ->
|
client_subscribe(GwName, ClientId, Topic, SubOpts) ->
|
||||||
client_call(GwName, ClientId, {subscribe, Topic, SubOpts}).
|
client_call(GwName, ClientId, {subscribe, Topic, SubOpts}).
|
||||||
|
|
||||||
-spec client_unsubscribe(gateway_name(),
|
-spec client_unsubscribe(
|
||||||
emqx_types:clientid(), emqx_types:topic())
|
gateway_name(),
|
||||||
-> {error, any()}
|
emqx_types:clientid(),
|
||||||
| ok.
|
emqx_types:topic()
|
||||||
|
) ->
|
||||||
|
{error, any()}
|
||||||
|
| ok.
|
||||||
client_unsubscribe(GwName, ClientId, Topic) ->
|
client_unsubscribe(GwName, ClientId, Topic) ->
|
||||||
client_call(GwName, ClientId, {unsubscribe, Topic}).
|
client_call(GwName, ClientId, {unsubscribe, Topic}).
|
||||||
|
|
||||||
client_call(GwName, ClientId, Req) ->
|
client_call(GwName, ClientId, Req) ->
|
||||||
try emqx_gateway_cm:call(
|
try
|
||||||
GwName, ClientId,
|
emqx_gateway_cm:call(
|
||||||
Req, ?DEFAULT_CALL_TIMEOUT) of
|
GwName,
|
||||||
|
ClientId,
|
||||||
|
Req,
|
||||||
|
?DEFAULT_CALL_TIMEOUT
|
||||||
|
)
|
||||||
|
of
|
||||||
undefined ->
|
undefined ->
|
||||||
{error, not_found};
|
{error, not_found};
|
||||||
Res -> Res
|
Res ->
|
||||||
catch throw : noproc ->
|
Res
|
||||||
{error, not_found};
|
catch
|
||||||
throw : {badrpc, Reason} ->
|
throw:noproc ->
|
||||||
{error, {badrpc, Reason}}
|
{error, not_found};
|
||||||
|
throw:{badrpc, Reason} ->
|
||||||
|
{error, {badrpc, Reason}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -311,54 +374,88 @@ return_http_error(Code, Msg) ->
|
||||||
|
|
||||||
-spec reason2msg({atom(), map()} | any()) -> error | string().
|
-spec reason2msg({atom(), map()} | any()) -> error | string().
|
||||||
reason2msg({badconf, #{key := Key, value := Value, reason := Reason}}) ->
|
reason2msg({badconf, #{key := Key, value := Value, reason := Reason}}) ->
|
||||||
NValue = case emqx_json:safe_encode(Value) of
|
NValue =
|
||||||
{ok, Str} -> Str;
|
case emqx_json:safe_encode(Value) of
|
||||||
{error, _} -> emqx_gateway_utils:stringfy(Value)
|
{ok, Str} -> Str;
|
||||||
end,
|
{error, _} -> emqx_gateway_utils:stringfy(Value)
|
||||||
fmtstr("Bad config value '~s' for '~s', reason: ~s",
|
end,
|
||||||
[NValue, Key, Reason]);
|
fmtstr(
|
||||||
reason2msg({badres, #{resource := gateway,
|
"Bad config value '~s' for '~s', reason: ~s",
|
||||||
gateway := GwName,
|
[NValue, Key, Reason]
|
||||||
reason := not_found}}) ->
|
);
|
||||||
|
reason2msg(
|
||||||
|
{badres, #{
|
||||||
|
resource := gateway,
|
||||||
|
gateway := GwName,
|
||||||
|
reason := not_found
|
||||||
|
}}
|
||||||
|
) ->
|
||||||
fmtstr("The ~s gateway is unloaded", [GwName]);
|
fmtstr("The ~s gateway is unloaded", [GwName]);
|
||||||
|
reason2msg(
|
||||||
reason2msg({badres, #{resource := gateway,
|
{badres, #{
|
||||||
gateway := GwName,
|
resource := gateway,
|
||||||
reason := already_exist}}) ->
|
gateway := GwName,
|
||||||
|
reason := already_exist
|
||||||
|
}}
|
||||||
|
) ->
|
||||||
fmtstr("The ~s gateway already loaded", [GwName]);
|
fmtstr("The ~s gateway already loaded", [GwName]);
|
||||||
|
reason2msg(
|
||||||
reason2msg({badres, #{resource := listener,
|
{badres, #{
|
||||||
listener := {GwName, LType, LName},
|
resource := listener,
|
||||||
reason := not_found}}) ->
|
listener := {GwName, LType, LName},
|
||||||
|
reason := not_found
|
||||||
|
}}
|
||||||
|
) ->
|
||||||
fmtstr("Listener ~s not found", [listener_id(GwName, LType, LName)]);
|
fmtstr("Listener ~s not found", [listener_id(GwName, LType, LName)]);
|
||||||
|
reason2msg(
|
||||||
reason2msg({badres, #{resource := listener,
|
{badres, #{
|
||||||
listener := {GwName, LType, LName},
|
resource := listener,
|
||||||
reason := already_exist}}) ->
|
listener := {GwName, LType, LName},
|
||||||
fmtstr("The listener ~s of ~s already exist",
|
reason := already_exist
|
||||||
[listener_id(GwName, LType, LName), GwName]);
|
}}
|
||||||
|
) ->
|
||||||
reason2msg({badres, #{resource := authn,
|
fmtstr(
|
||||||
gateway := GwName,
|
"The listener ~s of ~s already exist",
|
||||||
reason := not_found}}) ->
|
[listener_id(GwName, LType, LName), GwName]
|
||||||
|
);
|
||||||
|
reason2msg(
|
||||||
|
{badres, #{
|
||||||
|
resource := authn,
|
||||||
|
gateway := GwName,
|
||||||
|
reason := not_found
|
||||||
|
}}
|
||||||
|
) ->
|
||||||
fmtstr("The authentication not found on ~s", [GwName]);
|
fmtstr("The authentication not found on ~s", [GwName]);
|
||||||
|
reason2msg(
|
||||||
reason2msg({badres, #{resource := authn,
|
{badres, #{
|
||||||
gateway := GwName,
|
resource := authn,
|
||||||
reason := already_exist}}) ->
|
gateway := GwName,
|
||||||
|
reason := already_exist
|
||||||
|
}}
|
||||||
|
) ->
|
||||||
fmtstr("The authentication already exist on ~s", [GwName]);
|
fmtstr("The authentication already exist on ~s", [GwName]);
|
||||||
|
reason2msg(
|
||||||
reason2msg({badres, #{resource := listener_authn,
|
{badres, #{
|
||||||
listener := {GwName, LType, LName},
|
resource := listener_authn,
|
||||||
reason := not_found}}) ->
|
listener := {GwName, LType, LName},
|
||||||
fmtstr("The authentication not found on ~s",
|
reason := not_found
|
||||||
[listener_id(GwName, LType, LName)]);
|
}}
|
||||||
|
) ->
|
||||||
reason2msg({badres, #{resource := listener_authn,
|
fmtstr(
|
||||||
listener := {GwName, LType, LName},
|
"The authentication not found on ~s",
|
||||||
reason := already_exist}}) ->
|
[listener_id(GwName, LType, LName)]
|
||||||
fmtstr("The authentication already exist on ~s",
|
);
|
||||||
[listener_id(GwName, LType, LName)]);
|
reason2msg(
|
||||||
|
{badres, #{
|
||||||
|
resource := listener_authn,
|
||||||
|
listener := {GwName, LType, LName},
|
||||||
|
reason := already_exist
|
||||||
|
}}
|
||||||
|
) ->
|
||||||
|
fmtstr(
|
||||||
|
"The authentication already exist on ~s",
|
||||||
|
[listener_id(GwName, LType, LName)]
|
||||||
|
);
|
||||||
reason2msg(_) ->
|
reason2msg(_) ->
|
||||||
error.
|
error.
|
||||||
|
|
||||||
|
|
@ -389,10 +486,12 @@ with_listener_authn(GwName0, Id, Fun) ->
|
||||||
-spec with_gateway(binary(), function()) -> any().
|
-spec with_gateway(binary(), function()) -> any().
|
||||||
with_gateway(GwName0, Fun) ->
|
with_gateway(GwName0, Fun) ->
|
||||||
try
|
try
|
||||||
GwName = try
|
GwName =
|
||||||
binary_to_existing_atom(GwName0)
|
try
|
||||||
catch _ : _ -> error(badname)
|
binary_to_existing_atom(GwName0)
|
||||||
end,
|
catch
|
||||||
|
_:_ -> error(badname)
|
||||||
|
end,
|
||||||
case emqx_gateway:lookup(GwName) of
|
case emqx_gateway:lookup(GwName) of
|
||||||
undefined ->
|
undefined ->
|
||||||
return_http_error(404, "Gateway not load");
|
return_http_error(404, "Gateway not load");
|
||||||
|
|
@ -400,24 +499,26 @@ with_gateway(GwName0, Fun) ->
|
||||||
Fun(GwName, Gateway)
|
Fun(GwName, Gateway)
|
||||||
end
|
end
|
||||||
catch
|
catch
|
||||||
error : badname ->
|
error:badname ->
|
||||||
return_http_error(404, "Bad gateway name");
|
return_http_error(404, "Bad gateway name");
|
||||||
%% Exceptions from: checks/2
|
%% Exceptions from: checks/2
|
||||||
error : {miss_param, K} ->
|
error:{miss_param, K} ->
|
||||||
return_http_error(400, [K, " is required"]);
|
return_http_error(400, [K, " is required"]);
|
||||||
%% Exceptions from emqx_gateway_utils:parse_listener_id/1
|
%% Exceptions from emqx_gateway_utils:parse_listener_id/1
|
||||||
error : {invalid_listener_id, Id} ->
|
error:{invalid_listener_id, Id} ->
|
||||||
return_http_error(400, ["invalid listener id: ", Id]);
|
return_http_error(400, ["invalid listener id: ", Id]);
|
||||||
%% Exceptions from: emqx:get_config/1
|
%% Exceptions from: emqx:get_config/1
|
||||||
error : {config_not_found, Path0} ->
|
error:{config_not_found, Path0} ->
|
||||||
Path = lists:concat(
|
Path = lists:concat(
|
||||||
lists:join(".", lists:map(fun to_list/1, Path0))),
|
lists:join(".", lists:map(fun to_list/1, Path0))
|
||||||
|
),
|
||||||
return_http_error(404, "Resource not found. path: " ++ Path);
|
return_http_error(404, "Resource not found. path: " ++ Path);
|
||||||
Class : Reason : Stk ->
|
Class:Reason:Stk ->
|
||||||
?SLOG(error, #{ msg => "uncatched_error"
|
?SLOG(error, #{
|
||||||
, reason => {Class, Reason}
|
msg => "uncatched_error",
|
||||||
, stacktrace => Stk
|
reason => {Class, Reason},
|
||||||
}),
|
stacktrace => Stk
|
||||||
|
}),
|
||||||
reason2resp(Reason)
|
reason2resp(Reason)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -427,8 +528,7 @@ checks([], _) ->
|
||||||
checks([K | Ks], Map) ->
|
checks([K | Ks], Map) ->
|
||||||
case maps:is_key(K, Map) of
|
case maps:is_key(K, Map) of
|
||||||
true -> checks(Ks, Map);
|
true -> checks(Ks, Map);
|
||||||
false ->
|
false -> error({miss_param, K})
|
||||||
error({miss_param, K})
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
to_list(A) when is_atom(A) ->
|
to_list(A) when is_atom(A) ->
|
||||||
|
|
|
||||||
|
|
@ -23,34 +23,36 @@
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([ start_link/3
|
-export([
|
||||||
, info/1
|
start_link/3,
|
||||||
, disable/1
|
info/1,
|
||||||
, enable/1
|
disable/1,
|
||||||
, update/2
|
enable/1,
|
||||||
]).
|
update/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
name :: gateway_name(),
|
name :: gateway_name(),
|
||||||
config :: emqx_config:config(),
|
config :: emqx_config:config(),
|
||||||
ctx :: emqx_gateway_ctx:context(),
|
ctx :: emqx_gateway_ctx:context(),
|
||||||
authns :: [emqx_authentication:chain_name()],
|
authns :: [emqx_authentication:chain_name()],
|
||||||
status :: stopped | running,
|
status :: stopped | running,
|
||||||
child_pids :: [pid()],
|
child_pids :: [pid()],
|
||||||
gw_state :: emqx_gateway_impl:state() | undefined,
|
gw_state :: emqx_gateway_impl:state() | undefined,
|
||||||
created_at :: integer(),
|
created_at :: integer(),
|
||||||
started_at :: integer() | undefined,
|
started_at :: integer() | undefined,
|
||||||
stopped_at :: integer() | undefined
|
stopped_at :: integer() | undefined
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-elvis([{elvis_style, invalid_dynamic_call, disable}]).
|
-elvis([{elvis_style, invalid_dynamic_call, disable}]).
|
||||||
|
|
||||||
|
|
@ -60,10 +62,10 @@
|
||||||
|
|
||||||
start_link(Gateway, Ctx, GwDscrptr) ->
|
start_link(Gateway, Ctx, GwDscrptr) ->
|
||||||
gen_server:start_link(
|
gen_server:start_link(
|
||||||
?MODULE,
|
?MODULE,
|
||||||
[Gateway, Ctx, GwDscrptr],
|
[Gateway, Ctx, GwDscrptr],
|
||||||
[]
|
[]
|
||||||
).
|
).
|
||||||
|
|
||||||
-spec info(pid()) -> gateway().
|
-spec info(pid()) -> gateway().
|
||||||
info(Pid) ->
|
info(Pid) ->
|
||||||
|
|
@ -93,21 +95,22 @@ call(Pid, Req) ->
|
||||||
|
|
||||||
init([Gateway, Ctx, _GwDscrptr]) ->
|
init([Gateway, Ctx, _GwDscrptr]) ->
|
||||||
process_flag(trap_exit, true),
|
process_flag(trap_exit, true),
|
||||||
#{name := GwName, config := Config } = Gateway,
|
#{name := GwName, config := Config} = Gateway,
|
||||||
State = #state{
|
State = #state{
|
||||||
ctx = Ctx,
|
ctx = Ctx,
|
||||||
name = GwName,
|
name = GwName,
|
||||||
authns = [],
|
authns = [],
|
||||||
config = Config,
|
config = Config,
|
||||||
child_pids = [],
|
child_pids = [],
|
||||||
status = stopped,
|
status = stopped,
|
||||||
created_at = erlang:system_time(millisecond)
|
created_at = erlang:system_time(millisecond)
|
||||||
},
|
},
|
||||||
case maps:get(enable, Config, true) of
|
case maps:get(enable, Config, true) of
|
||||||
false ->
|
false ->
|
||||||
?SLOG(info, #{ msg => "skip_to_start_gateway_due_to_disabled"
|
?SLOG(info, #{
|
||||||
, gateway_name => GwName
|
msg => "skip_to_start_gateway_due_to_disabled",
|
||||||
}),
|
gateway_name => GwName
|
||||||
|
}),
|
||||||
{ok, State};
|
{ok, State};
|
||||||
true ->
|
true ->
|
||||||
case cb_gateway_load(State) of
|
case cb_gateway_load(State) of
|
||||||
|
|
@ -120,7 +123,6 @@ init([Gateway, Ctx, _GwDscrptr]) ->
|
||||||
|
|
||||||
handle_call(info, _From, State) ->
|
handle_call(info, _From, State) ->
|
||||||
{reply, detailed_gateway_info(State), State};
|
{reply, detailed_gateway_info(State), State};
|
||||||
|
|
||||||
handle_call(disable, _From, State = #state{status = Status}) ->
|
handle_call(disable, _From, State = #state{status = Status}) ->
|
||||||
case Status of
|
case Status of
|
||||||
running ->
|
running ->
|
||||||
|
|
@ -133,7 +135,6 @@ handle_call(disable, _From, State = #state{status = Status}) ->
|
||||||
_ ->
|
_ ->
|
||||||
{reply, {error, already_stopped}, State}
|
{reply, {error, already_stopped}, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call(enable, _From, State = #state{status = Status}) ->
|
handle_call(enable, _From, State = #state{status = Status}) ->
|
||||||
case Status of
|
case Status of
|
||||||
stopped ->
|
stopped ->
|
||||||
|
|
@ -146,7 +147,6 @@ handle_call(enable, _From, State = #state{status = Status}) ->
|
||||||
_ ->
|
_ ->
|
||||||
{reply, {error, already_started}, State}
|
{reply, {error, already_started}, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call({update, Config}, _From, State) ->
|
handle_call({update, Config}, _From, State) ->
|
||||||
case do_update_one_by_one(Config, State) of
|
case do_update_one_by_one(Config, State) of
|
||||||
{ok, NState} ->
|
{ok, NState} ->
|
||||||
|
|
@ -155,7 +155,6 @@ handle_call({update, Config}, _From, State) ->
|
||||||
%% If something wrong, nothing to update
|
%% If something wrong, nothing to update
|
||||||
{reply, {error, Reason}, State}
|
{reply, {error, Reason}, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call(_Request, _From, State) ->
|
handle_call(_Request, _From, State) ->
|
||||||
Reply = ok,
|
Reply = ok,
|
||||||
{reply, Reply, State}.
|
{reply, Reply, State}.
|
||||||
|
|
@ -163,38 +162,48 @@ handle_call(_Request, _From, State) ->
|
||||||
handle_cast(_Msg, State) ->
|
handle_cast(_Msg, State) ->
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
handle_info({'EXIT', Pid, Reason}, State = #state{name = Name,
|
handle_info(
|
||||||
child_pids = Pids}) ->
|
{'EXIT', Pid, Reason},
|
||||||
|
State = #state{
|
||||||
|
name = Name,
|
||||||
|
child_pids = Pids
|
||||||
|
}
|
||||||
|
) ->
|
||||||
case lists:member(Pid, Pids) of
|
case lists:member(Pid, Pids) of
|
||||||
true ->
|
true ->
|
||||||
?SLOG(error, #{ msg => "child_process_exited"
|
?SLOG(error, #{
|
||||||
, child => Pid
|
msg => "child_process_exited",
|
||||||
, reason => Reason
|
child => Pid,
|
||||||
}),
|
reason => Reason
|
||||||
case Pids -- [Pid]of
|
}),
|
||||||
|
case Pids -- [Pid] of
|
||||||
[] ->
|
[] ->
|
||||||
?SLOG(error, #{ msg => "gateway_all_children_process_existed"
|
?SLOG(error, #{
|
||||||
, gateway_name => Name
|
msg => "gateway_all_children_process_existed",
|
||||||
}),
|
gateway_name => Name
|
||||||
{noreply, State#state{status = stopped,
|
}),
|
||||||
child_pids = [],
|
{noreply, State#state{
|
||||||
gw_state = undefined}};
|
status = stopped,
|
||||||
|
child_pids = [],
|
||||||
|
gw_state = undefined
|
||||||
|
}};
|
||||||
RemainPids ->
|
RemainPids ->
|
||||||
{noreply, State#state{child_pids = RemainPids}}
|
{noreply, State#state{child_pids = RemainPids}}
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
?SLOG(error, #{ msg => "gateway_catch_a_unknown_process_exited"
|
?SLOG(error, #{
|
||||||
, child => Pid
|
msg => "gateway_catch_a_unknown_process_exited",
|
||||||
, reason => Reason
|
child => Pid,
|
||||||
, gateway_name => Name
|
reason => Reason,
|
||||||
}),
|
gateway_name => Name
|
||||||
|
}),
|
||||||
{noreply, State}
|
{noreply, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_info(Info, State) ->
|
handle_info(Info, State) ->
|
||||||
?SLOG(warning, #{ msg => "unexcepted_info"
|
?SLOG(warning, #{
|
||||||
, info => Info
|
msg => "unexcepted_info",
|
||||||
}),
|
info => Info
|
||||||
|
}),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
terminate(_Reason, State = #state{child_pids = Pids}) ->
|
terminate(_Reason, State = #state{child_pids = Pids}) ->
|
||||||
|
|
@ -207,14 +216,16 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
|
|
||||||
detailed_gateway_info(State) ->
|
detailed_gateway_info(State) ->
|
||||||
maps:filter(
|
maps:filter(
|
||||||
fun(_, V) -> V =/= undefined end,
|
fun(_, V) -> V =/= undefined end,
|
||||||
#{name => State#state.name,
|
#{
|
||||||
config => State#state.config,
|
name => State#state.name,
|
||||||
status => State#state.status,
|
config => State#state.config,
|
||||||
created_at => State#state.created_at,
|
status => State#state.status,
|
||||||
started_at => State#state.started_at,
|
created_at => State#state.created_at,
|
||||||
stopped_at => State#state.stopped_at
|
started_at => State#state.started_at,
|
||||||
}).
|
stopped_at => State#state.stopped_at
|
||||||
|
}
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal funcs
|
%% Internal funcs
|
||||||
|
|
@ -231,7 +242,7 @@ init_authn(GwName, Config) ->
|
||||||
try
|
try
|
||||||
do_init_authn(Authns, [])
|
do_init_authn(Authns, [])
|
||||||
catch
|
catch
|
||||||
throw : Reason = {badauth, _} ->
|
throw:Reason = {badauth, _} ->
|
||||||
do_deinit_authn(proplists:get_keys(Authns)),
|
do_deinit_authn(proplists:get_keys(Authns)),
|
||||||
throw(Reason)
|
throw(Reason)
|
||||||
end.
|
end.
|
||||||
|
|
@ -250,11 +261,15 @@ do_init_authn([_BadConf | More], Names) ->
|
||||||
authns(GwName, Config) ->
|
authns(GwName, Config) ->
|
||||||
Listeners = maps:to_list(maps:get(listeners, Config, #{})),
|
Listeners = maps:to_list(maps:get(listeners, Config, #{})),
|
||||||
lists:append(
|
lists:append(
|
||||||
[ [{emqx_gateway_utils:listener_chain(GwName, LisType, LisName),
|
[
|
||||||
authn_conf(Opts)}
|
[
|
||||||
|| {LisName, Opts} <- maps:to_list(LisNames) ]
|
{emqx_gateway_utils:listener_chain(GwName, LisType, LisName), authn_conf(Opts)}
|
||||||
|| {LisType, LisNames} <- Listeners])
|
|| {LisName, Opts} <- maps:to_list(LisNames)
|
||||||
++ [{emqx_gateway_utils:global_chain(GwName), authn_conf(Config)}].
|
]
|
||||||
|
|| {LisType, LisNames} <- Listeners
|
||||||
|
]
|
||||||
|
) ++
|
||||||
|
[{emqx_gateway_utils:global_chain(GwName), authn_conf(Config)}].
|
||||||
|
|
||||||
authn_conf(Conf) ->
|
authn_conf(Conf) ->
|
||||||
maps:get(authentication, Conf, #{enable => false}).
|
maps:get(authentication, Conf, #{enable => false}).
|
||||||
|
|
@ -263,20 +278,23 @@ do_create_authn_chain(ChainName, AuthConf) ->
|
||||||
case ensure_chain(ChainName) of
|
case ensure_chain(ChainName) of
|
||||||
ok ->
|
ok ->
|
||||||
case emqx_authentication:create_authenticator(ChainName, AuthConf) of
|
case emqx_authentication:create_authenticator(ChainName, AuthConf) of
|
||||||
{ok, _} -> ok;
|
{ok, _} ->
|
||||||
|
ok;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{ msg => "failed_to_create_authenticator"
|
?SLOG(error, #{
|
||||||
, chain_name => ChainName
|
msg => "failed_to_create_authenticator",
|
||||||
, reason => Reason
|
chain_name => ChainName,
|
||||||
, config => AuthConf
|
reason => Reason,
|
||||||
}),
|
config => AuthConf
|
||||||
|
}),
|
||||||
throw({badauth, Reason})
|
throw({badauth, Reason})
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{ msg => "failed_to_create_authn_chanin"
|
?SLOG(error, #{
|
||||||
, chain_name => ChainName
|
msg => "failed_to_create_authn_chanin",
|
||||||
, reason => Reason
|
chain_name => ChainName,
|
||||||
}),
|
reason => Reason
|
||||||
|
}),
|
||||||
throw({badauth, Reason})
|
throw({badauth, Reason})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -291,22 +309,32 @@ ensure_chain(ChainName) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_deinit_authn(Names) ->
|
do_deinit_authn(Names) ->
|
||||||
lists:foreach(fun(ChainName) ->
|
lists:foreach(
|
||||||
case emqx_authentication:delete_chain(ChainName) of
|
fun(ChainName) ->
|
||||||
ok -> ok;
|
case emqx_authentication:delete_chain(ChainName) of
|
||||||
{error, {not_found, _}} -> ok;
|
ok ->
|
||||||
{error, Reason} ->
|
ok;
|
||||||
?SLOG(error, #{ msg => "failed_to_clean_authn_chain"
|
{error, {not_found, _}} ->
|
||||||
, chain_name => ChainName
|
ok;
|
||||||
, reason => Reason
|
{error, Reason} ->
|
||||||
})
|
?SLOG(error, #{
|
||||||
end
|
msg => "failed_to_clean_authn_chain",
|
||||||
end, Names).
|
chain_name => ChainName,
|
||||||
|
reason => Reason
|
||||||
|
})
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
Names
|
||||||
|
).
|
||||||
|
|
||||||
do_update_one_by_one(NCfg, State = #state{
|
do_update_one_by_one(
|
||||||
name = GwName,
|
NCfg,
|
||||||
config = OCfg,
|
State = #state{
|
||||||
status = Status}) ->
|
name = GwName,
|
||||||
|
config = OCfg,
|
||||||
|
status = Status
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NEnable = maps:get(enable, NCfg, true),
|
NEnable = maps:get(enable, NCfg, true),
|
||||||
|
|
||||||
OAuths = authns(GwName, OCfg),
|
OAuths = authns(GwName, OCfg),
|
||||||
|
|
@ -319,14 +347,16 @@ do_update_one_by_one(NCfg, State = #state{
|
||||||
{stopped, false} ->
|
{stopped, false} ->
|
||||||
{ok, State#state{config = NCfg}};
|
{ok, State#state{config = NCfg}};
|
||||||
{running, true} ->
|
{running, true} ->
|
||||||
NState = case NAuths == OAuths of
|
NState =
|
||||||
true -> State;
|
case NAuths == OAuths of
|
||||||
false ->
|
true ->
|
||||||
%% Reset Authentication first
|
State;
|
||||||
_ = do_deinit_authn(State#state.authns),
|
false ->
|
||||||
AuthnNames = init_authn(State#state.name, NCfg),
|
%% Reset Authentication first
|
||||||
State#state{authns = AuthnNames}
|
_ = do_deinit_authn(State#state.authns),
|
||||||
end,
|
AuthnNames = init_authn(State#state.name, NCfg),
|
||||||
|
State#state{authns = AuthnNames}
|
||||||
|
end,
|
||||||
%% TODO: minimum impact update ???
|
%% TODO: minimum impact update ???
|
||||||
cb_gateway_update(NCfg, NState);
|
cb_gateway_update(NCfg, NState);
|
||||||
{running, false} ->
|
{running, false} ->
|
||||||
|
|
@ -338,26 +368,33 @@ do_update_one_by_one(NCfg, State = #state{
|
||||||
throw(nomatch)
|
throw(nomatch)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
cb_gateway_unload(State = #state{name = GwName,
|
cb_gateway_unload(
|
||||||
gw_state = GwState}) ->
|
State = #state{
|
||||||
|
name = GwName,
|
||||||
|
gw_state = GwState
|
||||||
|
}
|
||||||
|
) ->
|
||||||
Gateway = detailed_gateway_info(State),
|
Gateway = detailed_gateway_info(State),
|
||||||
try
|
try
|
||||||
#{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName),
|
#{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName),
|
||||||
CbMod:on_gateway_unload(Gateway, GwState),
|
CbMod:on_gateway_unload(Gateway, GwState),
|
||||||
{ok, State#state{child_pids = [],
|
{ok, State#state{
|
||||||
authns = [],
|
child_pids = [],
|
||||||
status = stopped,
|
authns = [],
|
||||||
gw_state = undefined,
|
status = stopped,
|
||||||
started_at = undefined,
|
gw_state = undefined,
|
||||||
stopped_at = erlang:system_time(millisecond)}}
|
started_at = undefined,
|
||||||
|
stopped_at = erlang:system_time(millisecond)
|
||||||
|
}}
|
||||||
catch
|
catch
|
||||||
Class : Reason : Stk ->
|
Class:Reason:Stk ->
|
||||||
?SLOG(error, #{ msg => "unload_gateway_crashed"
|
?SLOG(error, #{
|
||||||
, gateway_name => GwName
|
msg => "unload_gateway_crashed",
|
||||||
, inner_state => GwState
|
gateway_name => GwName,
|
||||||
, reason => {Class, Reason}
|
inner_state => GwState,
|
||||||
, stacktrace => Stk
|
reason => {Class, Reason},
|
||||||
}),
|
stacktrace => Stk
|
||||||
|
}),
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
after
|
after
|
||||||
_ = do_deinit_authn(State#state.authns)
|
_ = do_deinit_authn(State#state.authns)
|
||||||
|
|
@ -367,10 +404,13 @@ cb_gateway_unload(State = #state{name = GwName,
|
||||||
%% 2. Callback to Mod:on_gateway_load/2
|
%% 2. Callback to Mod:on_gateway_load/2
|
||||||
%%
|
%%
|
||||||
%% Notes: If failed, rollback
|
%% Notes: If failed, rollback
|
||||||
cb_gateway_load(State = #state{name = GwName,
|
cb_gateway_load(
|
||||||
config = Config,
|
State = #state{
|
||||||
ctx = Ctx}) ->
|
name = GwName,
|
||||||
|
config = Config,
|
||||||
|
ctx = Ctx
|
||||||
|
}
|
||||||
|
) ->
|
||||||
Gateway = detailed_gateway_info(State),
|
Gateway = detailed_gateway_info(State),
|
||||||
try
|
try
|
||||||
AuthnNames = init_authn(GwName, Config),
|
AuthnNames = init_authn(GwName, Config),
|
||||||
|
|
@ -383,54 +423,62 @@ cb_gateway_load(State = #state{name = GwName,
|
||||||
{ok, ChildPidOrSpecs, GwState} ->
|
{ok, ChildPidOrSpecs, GwState} ->
|
||||||
ChildPids = start_child_process(ChildPidOrSpecs),
|
ChildPids = start_child_process(ChildPidOrSpecs),
|
||||||
{ok, State#state{
|
{ok, State#state{
|
||||||
ctx = NCtx,
|
ctx = NCtx,
|
||||||
authns = AuthnNames,
|
authns = AuthnNames,
|
||||||
status = running,
|
status = running,
|
||||||
child_pids = ChildPids,
|
child_pids = ChildPids,
|
||||||
gw_state = GwState,
|
gw_state = GwState,
|
||||||
stopped_at = undefined,
|
stopped_at = undefined,
|
||||||
started_at = erlang:system_time(millisecond)
|
started_at = erlang:system_time(millisecond)
|
||||||
}}
|
}}
|
||||||
end
|
end
|
||||||
catch
|
catch
|
||||||
Class : Reason1 : Stk ->
|
Class:Reason1:Stk ->
|
||||||
?SLOG(error, #{ msg => "load_gateway_crashed"
|
?SLOG(error, #{
|
||||||
, gateway_name => GwName
|
msg => "load_gateway_crashed",
|
||||||
, gateway => Gateway
|
gateway_name => GwName,
|
||||||
, ctx => Ctx
|
gateway => Gateway,
|
||||||
, reason => {Class, Reason1}
|
ctx => Ctx,
|
||||||
, stacktrace => Stk
|
reason => {Class, Reason1},
|
||||||
}),
|
stacktrace => Stk
|
||||||
|
}),
|
||||||
{error, Reason1}
|
{error, Reason1}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
cb_gateway_update(Config,
|
cb_gateway_update(
|
||||||
State = #state{name = GwName,
|
Config,
|
||||||
gw_state = GwState}) ->
|
State = #state{
|
||||||
|
name = GwName,
|
||||||
|
gw_state = GwState
|
||||||
|
}
|
||||||
|
) ->
|
||||||
try
|
try
|
||||||
#{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName),
|
#{cbkmod := CbMod} = emqx_gateway_registry:lookup(GwName),
|
||||||
case CbMod:on_gateway_update(Config, detailed_gateway_info(State), GwState) of
|
case CbMod:on_gateway_update(Config, detailed_gateway_info(State), GwState) of
|
||||||
{error, Reason} -> {error, Reason};
|
{error, Reason} ->
|
||||||
|
{error, Reason};
|
||||||
{ok, ChildPidOrSpecs, NGwState} ->
|
{ok, ChildPidOrSpecs, NGwState} ->
|
||||||
ChildPids = start_child_process(ChildPidOrSpecs),
|
ChildPids = start_child_process(ChildPidOrSpecs),
|
||||||
{ok, State#state{
|
{ok, State#state{
|
||||||
config = Config,
|
config = Config,
|
||||||
child_pids = ChildPids,
|
child_pids = ChildPids,
|
||||||
gw_state = NGwState
|
gw_state = NGwState
|
||||||
}}
|
}}
|
||||||
end
|
end
|
||||||
catch
|
catch
|
||||||
Class : Reason1 : Stk ->
|
Class:Reason1:Stk ->
|
||||||
?SLOG(error, #{ msg => "update_gateway_crashed"
|
?SLOG(error, #{
|
||||||
, gateway_name => GwName
|
msg => "update_gateway_crashed",
|
||||||
, new_config => Config
|
gateway_name => GwName,
|
||||||
, reason => {Class, Reason1}
|
new_config => Config,
|
||||||
, stacktrace => Stk
|
reason => {Class, Reason1},
|
||||||
}),
|
stacktrace => Stk
|
||||||
|
}),
|
||||||
{error, Reason1}
|
{error, Reason1}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
start_child_process([]) -> [];
|
start_child_process([]) ->
|
||||||
|
[];
|
||||||
start_child_process([Indictor | _] = ChildPidOrSpecs) ->
|
start_child_process([Indictor | _] = ChildPidOrSpecs) ->
|
||||||
case erlang:is_pid(Indictor) of
|
case erlang:is_pid(Indictor) of
|
||||||
true ->
|
true ->
|
||||||
|
|
@ -441,7 +489,6 @@ start_child_process([Indictor | _] = ChildPidOrSpecs) ->
|
||||||
|
|
||||||
do_start_child_process(ChildSpecs) when is_list(ChildSpecs) ->
|
do_start_child_process(ChildSpecs) when is_list(ChildSpecs) ->
|
||||||
lists:map(fun do_start_child_process/1, ChildSpecs);
|
lists:map(fun do_start_child_process/1, ChildSpecs);
|
||||||
|
|
||||||
do_start_child_process(_ChildSpec = #{start := {M, F, A}}) ->
|
do_start_child_process(_ChildSpec = #{start := {M, F, A}}) ->
|
||||||
case erlang:apply(M, F, A) of
|
case erlang:apply(M, F, A) of
|
||||||
{ok, Pid} ->
|
{ok, Pid} ->
|
||||||
|
|
|
||||||
|
|
@ -23,22 +23,24 @@
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([start_link/1]).
|
-export([start_link/1]).
|
||||||
|
|
||||||
-export([ inc/2
|
-export([
|
||||||
, inc/3
|
inc/2,
|
||||||
, dec/2
|
inc/3,
|
||||||
, dec/3
|
dec/2,
|
||||||
]).
|
dec/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export([lookup/1]).
|
-export([lookup/1]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export([tabname/1]).
|
-export([tabname/1]).
|
||||||
|
|
||||||
|
|
@ -68,9 +70,9 @@ dec(GwName, Name) ->
|
||||||
dec(GwName, Name, Oct) ->
|
dec(GwName, Name, Oct) ->
|
||||||
inc(GwName, Name, -Oct).
|
inc(GwName, Name, -Oct).
|
||||||
|
|
||||||
-spec lookup(gateway_name())
|
-spec lookup(gateway_name()) ->
|
||||||
-> undefined
|
undefined
|
||||||
| [{Name :: atom(), integer()}].
|
| [{Name :: atom(), integer()}].
|
||||||
lookup(GwName) ->
|
lookup(GwName) ->
|
||||||
Tab = emqx_gateway_metrics:tabname(GwName),
|
Tab = emqx_gateway_metrics:tabname(GwName),
|
||||||
case ets:info(Tab) of
|
case ets:info(Tab) of
|
||||||
|
|
@ -87,7 +89,7 @@ tabname(GwName) ->
|
||||||
|
|
||||||
init([GwName]) ->
|
init([GwName]) ->
|
||||||
TabOpts = [public, {write_concurrency, true}],
|
TabOpts = [public, {write_concurrency, true}],
|
||||||
ok = emqx_tables:new(tabname(GwName), [set|TabOpts]),
|
ok = emqx_tables:new(tabname(GwName), [set | TabOpts]),
|
||||||
{ok, #state{}}.
|
{ok, #state{}}.
|
||||||
|
|
||||||
handle_call(_Request, _From, State) ->
|
handle_call(_Request, _From, State) ->
|
||||||
|
|
|
||||||
|
|
@ -22,35 +22,38 @@
|
||||||
-behaviour(gen_server).
|
-behaviour(gen_server).
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([ reg/2
|
-export([
|
||||||
, unreg/1
|
reg/2,
|
||||||
, list/0
|
unreg/1,
|
||||||
, lookup/1
|
list/0,
|
||||||
]).
|
lookup/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([start_link/0]).
|
-export([start_link/0]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
reged = #{} :: #{ gateway_name() => descriptor() }
|
reged = #{} :: #{gateway_name() => descriptor()}
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-type registry_options() :: [registry_option()].
|
-type registry_options() :: [registry_option()].
|
||||||
|
|
||||||
-type registry_option() :: {cbkmod, atom()}.
|
-type registry_option() :: {cbkmod, atom()}.
|
||||||
|
|
||||||
-type descriptor() :: #{ cbkmod := atom()
|
-type descriptor() :: #{
|
||||||
, rgopts := registry_options()
|
cbkmod := atom(),
|
||||||
}.
|
rgopts := registry_options()
|
||||||
|
}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% APIs
|
%% APIs
|
||||||
|
|
@ -63,14 +66,15 @@ start_link() ->
|
||||||
%% Mgmt
|
%% Mgmt
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec reg(gateway_name(), registry_options())
|
-spec reg(gateway_name(), registry_options()) ->
|
||||||
-> ok
|
ok
|
||||||
| {error, any()}.
|
| {error, any()}.
|
||||||
reg(Name, RgOpts) ->
|
reg(Name, RgOpts) ->
|
||||||
CbMod = proplists:get_value(cbkmod, RgOpts, Name),
|
CbMod = proplists:get_value(cbkmod, RgOpts, Name),
|
||||||
Dscrptr = #{ cbkmod => CbMod
|
Dscrptr = #{
|
||||||
, rgopts => RgOpts
|
cbkmod => CbMod,
|
||||||
},
|
rgopts => RgOpts
|
||||||
|
},
|
||||||
call({reg, Name, Dscrptr}).
|
call({reg, Name, Dscrptr}).
|
||||||
|
|
||||||
-spec unreg(gateway_name()) -> ok | {error, any()}.
|
-spec unreg(gateway_name()) -> ok | {error, any()}.
|
||||||
|
|
@ -110,7 +114,6 @@ handle_call({reg, Name, Dscrptr}, _From, State = #state{reged = Gateways}) ->
|
||||||
_ ->
|
_ ->
|
||||||
{reply, {error, already_existed}, State}
|
{reply, {error, already_existed}, State}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call({unreg, Name}, _From, State = #state{reged = Gateways}) ->
|
handle_call({unreg, Name}, _From, State = #state{reged = Gateways}) ->
|
||||||
case maps:get(Name, Gateways, undefined) of
|
case maps:get(Name, Gateways, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
|
|
@ -119,14 +122,11 @@ handle_call({unreg, Name}, _From, State = #state{reged = Gateways}) ->
|
||||||
_ = emqx_gateway_sup:unload_gateway(Name),
|
_ = emqx_gateway_sup:unload_gateway(Name),
|
||||||
{reply, ok, State#state{reged = maps:remove(Name, Gateways)}}
|
{reply, ok, State#state{reged = maps:remove(Name, Gateways)}}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call(all, _From, State = #state{reged = Gateways}) ->
|
handle_call(all, _From, State = #state{reged = Gateways}) ->
|
||||||
{reply, maps:to_list(Gateways), State};
|
{reply, maps:to_list(Gateways), State};
|
||||||
|
|
||||||
handle_call({lookup, Name}, _From, State = #state{reged = Gateways}) ->
|
handle_call({lookup, Name}, _From, State = #state{reged = Gateways}) ->
|
||||||
Reply = maps:get(Name, Gateways, undefined),
|
Reply = maps:get(Name, Gateways, undefined),
|
||||||
{reply, Reply, State};
|
{reply, Reply, State};
|
||||||
|
|
||||||
handle_call(Req, _From, State) ->
|
handle_call(Req, _From, State) ->
|
||||||
logger:error("Unexpected call: ~0p", [Req]),
|
logger:error("Unexpected call: ~0p", [Req]),
|
||||||
{reply, ok, State}.
|
{reply, ok, State}.
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
|
|
@ -23,14 +23,15 @@
|
||||||
-export([start_link/0]).
|
-export([start_link/0]).
|
||||||
|
|
||||||
%% Gateway APIs
|
%% Gateway APIs
|
||||||
-export([ load_gateway/1
|
-export([
|
||||||
, unload_gateway/1
|
load_gateway/1,
|
||||||
, lookup_gateway/1
|
unload_gateway/1,
|
||||||
, update_gateway/2
|
lookup_gateway/1,
|
||||||
, start_gateway_insta/1
|
update_gateway/2,
|
||||||
, stop_gateway_insta/1
|
start_gateway_insta/1,
|
||||||
, list_gateway_insta/0
|
stop_gateway_insta/1,
|
||||||
]).
|
list_gateway_insta/0
|
||||||
|
]).
|
||||||
|
|
||||||
%% supervisor callbacks
|
%% supervisor callbacks
|
||||||
-export([init/1]).
|
-export([init/1]).
|
||||||
|
|
@ -42,23 +43,24 @@
|
||||||
start_link() ->
|
start_link() ->
|
||||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||||
|
|
||||||
|
|
||||||
-spec load_gateway(gateway()) -> {ok, pid()} | {error, any()}.
|
-spec load_gateway(gateway()) -> {ok, pid()} | {error, any()}.
|
||||||
load_gateway(Gateway = #{name := GwName}) ->
|
load_gateway(Gateway = #{name := GwName}) ->
|
||||||
case emqx_gateway_registry:lookup(GwName) of
|
case emqx_gateway_registry:lookup(GwName) of
|
||||||
undefined -> {error, {unknown_gateway_name, GwName}};
|
undefined ->
|
||||||
|
{error, {unknown_gateway_name, GwName}};
|
||||||
GwDscrptr ->
|
GwDscrptr ->
|
||||||
{ok, GwSup} = ensure_gateway_suptree_ready(GwName),
|
{ok, GwSup} = ensure_gateway_suptree_ready(GwName),
|
||||||
emqx_gateway_gw_sup:create_insta(GwSup, Gateway, GwDscrptr)
|
emqx_gateway_gw_sup:create_insta(GwSup, Gateway, GwDscrptr)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec unload_gateway(gateway_name())
|
-spec unload_gateway(gateway_name()) ->
|
||||||
-> ok
|
ok
|
||||||
| {error, not_found}
|
| {error, not_found}
|
||||||
| {error, any()}.
|
| {error, any()}.
|
||||||
unload_gateway(GwName) ->
|
unload_gateway(GwName) ->
|
||||||
case lists:keyfind(GwName, 1, supervisor:which_children(?MODULE)) of
|
case lists:keyfind(GwName, 1, supervisor:which_children(?MODULE)) of
|
||||||
false -> {error, not_found};
|
false ->
|
||||||
|
{error, not_found};
|
||||||
{_Id, Pid, _Type, _Mods} ->
|
{_Id, Pid, _Type, _Mods} ->
|
||||||
_ = emqx_gateway_gw_sup:remove_insta(Pid, GwName),
|
_ = emqx_gateway_gw_sup:remove_insta(Pid, GwName),
|
||||||
_ = supervisor:terminate_child(?MODULE, GwName),
|
_ = supervisor:terminate_child(?MODULE, GwName),
|
||||||
|
|
@ -75,21 +77,23 @@ lookup_gateway(GwName) ->
|
||||||
undefined
|
undefined
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec update_gateway(gateway_name(), emqx_config:config())
|
-spec update_gateway(gateway_name(), emqx_config:config()) ->
|
||||||
-> ok
|
ok
|
||||||
| {error, any()}.
|
| {error, any()}.
|
||||||
update_gateway(GwName, Config) ->
|
update_gateway(GwName, Config) ->
|
||||||
case emqx_gateway_utils:find_sup_child(?MODULE, GwName) of
|
case emqx_gateway_utils:find_sup_child(?MODULE, GwName) of
|
||||||
{ok, GwSup} ->
|
{ok, GwSup} ->
|
||||||
emqx_gateway_gw_sup:update_insta(GwSup, GwName, Config);
|
emqx_gateway_gw_sup:update_insta(GwSup, GwName, Config);
|
||||||
_ -> {error, not_found}
|
_ ->
|
||||||
|
{error, not_found}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
start_gateway_insta(GwName) ->
|
start_gateway_insta(GwName) ->
|
||||||
case search_gateway_insta_proc(GwName) of
|
case search_gateway_insta_proc(GwName) of
|
||||||
{ok, {GwSup, _}} ->
|
{ok, {GwSup, _}} ->
|
||||||
emqx_gateway_gw_sup:start_insta(GwSup, GwName);
|
emqx_gateway_gw_sup:start_insta(GwSup, GwName);
|
||||||
_ -> {error, not_found}
|
_ ->
|
||||||
|
{error, not_found}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec stop_gateway_insta(gateway_name()) -> ok | {error, any()}.
|
-spec stop_gateway_insta(gateway_name()) -> ok | {error, any()}.
|
||||||
|
|
@ -97,15 +101,20 @@ stop_gateway_insta(GwName) ->
|
||||||
case search_gateway_insta_proc(GwName) of
|
case search_gateway_insta_proc(GwName) of
|
||||||
{ok, {GwSup, _}} ->
|
{ok, {GwSup, _}} ->
|
||||||
emqx_gateway_gw_sup:stop_insta(GwSup, GwName);
|
emqx_gateway_gw_sup:stop_insta(GwSup, GwName);
|
||||||
_ -> {error, not_found}
|
_ ->
|
||||||
|
{error, not_found}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec list_gateway_insta() -> [gateway()].
|
-spec list_gateway_insta() -> [gateway()].
|
||||||
list_gateway_insta() ->
|
list_gateway_insta() ->
|
||||||
lists:append(lists:map(
|
lists:append(
|
||||||
fun(SupId) ->
|
lists:map(
|
||||||
emqx_gateway_gw_sup:list_insta(SupId)
|
fun(SupId) ->
|
||||||
end, list_started_gateway())).
|
emqx_gateway_gw_sup:list_insta(SupId)
|
||||||
|
end,
|
||||||
|
list_started_gateway()
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
-spec list_started_gateway() -> [gateway_name()].
|
-spec list_started_gateway() -> [gateway_name()].
|
||||||
list_started_gateway() ->
|
list_started_gateway() ->
|
||||||
|
|
@ -114,12 +123,12 @@ list_started_gateway() ->
|
||||||
%% Supervisor callback
|
%% Supervisor callback
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
SupFlags = #{ strategy => one_for_one
|
SupFlags = #{
|
||||||
, intensity => 10
|
strategy => one_for_one,
|
||||||
, period => 60
|
intensity => 10,
|
||||||
},
|
period => 60
|
||||||
ChildSpecs = [ emqx_gateway_utils:childspec(worker, emqx_gateway_registry)
|
},
|
||||||
],
|
ChildSpecs = [emqx_gateway_utils:childspec(worker, emqx_gateway_registry)],
|
||||||
{ok, {SupFlags, ChildSpecs}}.
|
{ok, {SupFlags, ChildSpecs}}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -130,14 +139,14 @@ ensure_gateway_suptree_ready(GwName) ->
|
||||||
case lists:keyfind(GwName, 1, supervisor:which_children(?MODULE)) of
|
case lists:keyfind(GwName, 1, supervisor:which_children(?MODULE)) of
|
||||||
false ->
|
false ->
|
||||||
ChildSpec = emqx_gateway_utils:childspec(
|
ChildSpec = emqx_gateway_utils:childspec(
|
||||||
GwName,
|
GwName,
|
||||||
supervisor,
|
supervisor,
|
||||||
emqx_gateway_gw_sup,
|
emqx_gateway_gw_sup,
|
||||||
[GwName]
|
[GwName]
|
||||||
),
|
),
|
||||||
emqx_gateway_utils:supervisor_ret(
|
emqx_gateway_utils:supervisor_ret(
|
||||||
supervisor:start_child(?MODULE, ChildSpec)
|
supervisor:start_child(?MODULE, ChildSpec)
|
||||||
);
|
);
|
||||||
{_Id, Pid, _Type, _Mods} ->
|
{_Id, Pid, _Type, _Mods} ->
|
||||||
{ok, Pid}
|
{ok, Pid}
|
||||||
end.
|
end.
|
||||||
|
|
@ -147,24 +156,27 @@ search_gateway_insta_proc(InstaId) ->
|
||||||
|
|
||||||
search_gateway_insta_proc(_InstaId, []) ->
|
search_gateway_insta_proc(_InstaId, []) ->
|
||||||
{error, not_found};
|
{error, not_found};
|
||||||
search_gateway_insta_proc(InstaId, [SupPid|More]) ->
|
search_gateway_insta_proc(InstaId, [SupPid | More]) ->
|
||||||
case emqx_gateway_utils:find_sup_child(SupPid, InstaId) of
|
case emqx_gateway_utils:find_sup_child(SupPid, InstaId) of
|
||||||
{ok, InstaPid} -> {ok, {SupPid, InstaPid}};
|
{ok, InstaPid} -> {ok, {SupPid, InstaPid}};
|
||||||
_ ->
|
_ -> search_gateway_insta_proc(InstaId, More)
|
||||||
search_gateway_insta_proc(InstaId, More)
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
started_gateway() ->
|
started_gateway() ->
|
||||||
lists:filtermap(
|
lists:filtermap(
|
||||||
fun({Id, _, _, _}) ->
|
fun({Id, _, _, _}) ->
|
||||||
is_a_gateway_id(Id) andalso {true, Id}
|
is_a_gateway_id(Id) andalso {true, Id}
|
||||||
end, supervisor:which_children(?MODULE)).
|
end,
|
||||||
|
supervisor:which_children(?MODULE)
|
||||||
|
).
|
||||||
|
|
||||||
started_gateway_pid() ->
|
started_gateway_pid() ->
|
||||||
lists:filtermap(
|
lists:filtermap(
|
||||||
fun({Id, Pid, _, _}) ->
|
fun({Id, Pid, _, _}) ->
|
||||||
is_a_gateway_id(Id) andalso {true, Pid}
|
is_a_gateway_id(Id) andalso {true, Pid}
|
||||||
end, supervisor:which_children(?MODULE)).
|
end,
|
||||||
|
supervisor:which_children(?MODULE)
|
||||||
|
).
|
||||||
|
|
||||||
is_a_gateway_id(Id) ->
|
is_a_gateway_id(Id) ->
|
||||||
Id /= emqx_gateway_registry.
|
Id /= emqx_gateway_registry.
|
||||||
|
|
|
||||||
|
|
@ -20,81 +20,87 @@
|
||||||
-include("emqx_gateway.hrl").
|
-include("emqx_gateway.hrl").
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
-export([ childspec/2
|
-export([
|
||||||
, childspec/3
|
childspec/2,
|
||||||
, childspec/4
|
childspec/3,
|
||||||
, supervisor_ret/1
|
childspec/4,
|
||||||
, find_sup_child/2
|
supervisor_ret/1,
|
||||||
]).
|
find_sup_child/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ start_listeners/4
|
-export([
|
||||||
, start_listener/4
|
start_listeners/4,
|
||||||
, stop_listeners/2
|
start_listener/4,
|
||||||
, stop_listener/2
|
stop_listeners/2,
|
||||||
]).
|
stop_listener/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ apply/2
|
-export([
|
||||||
, format_listenon/1
|
apply/2,
|
||||||
, parse_listenon/1
|
format_listenon/1,
|
||||||
, unix_ts_to_rfc3339/1
|
parse_listenon/1,
|
||||||
, unix_ts_to_rfc3339/2
|
unix_ts_to_rfc3339/1,
|
||||||
, listener_id/3
|
unix_ts_to_rfc3339/2,
|
||||||
, parse_listener_id/1
|
listener_id/3,
|
||||||
, is_running/2
|
parse_listener_id/1,
|
||||||
, global_chain/1
|
is_running/2,
|
||||||
, listener_chain/3
|
global_chain/1,
|
||||||
]).
|
listener_chain/3
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ stringfy/1
|
-export([stringfy/1]).
|
||||||
]).
|
|
||||||
|
|
||||||
-export([ normalize_config/1
|
-export([normalize_config/1]).
|
||||||
]).
|
|
||||||
|
|
||||||
%% Common Envs
|
%% Common Envs
|
||||||
-export([ active_n/1
|
-export([
|
||||||
, ratelimit/1
|
active_n/1,
|
||||||
, frame_options/1
|
ratelimit/1,
|
||||||
, init_gc_state/1
|
frame_options/1,
|
||||||
, stats_timer/1
|
init_gc_state/1,
|
||||||
, idle_timeout/1
|
stats_timer/1,
|
||||||
, oom_policy/1
|
idle_timeout/1,
|
||||||
]).
|
oom_policy/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ default_tcp_options/0
|
-export([
|
||||||
, default_udp_options/0
|
default_tcp_options/0,
|
||||||
, default_subopts/0
|
default_udp_options/0,
|
||||||
]).
|
default_subopts/0
|
||||||
|
]).
|
||||||
|
|
||||||
-define(ACTIVE_N, 100).
|
-define(ACTIVE_N, 100).
|
||||||
-define(DEFAULT_IDLE_TIMEOUT, 30000).
|
-define(DEFAULT_IDLE_TIMEOUT, 30000).
|
||||||
-define(DEFAULT_GC_OPTS, #{count => 1000, bytes => 1024*1024}).
|
-define(DEFAULT_GC_OPTS, #{count => 1000, bytes => 1024 * 1024}).
|
||||||
-define(DEFAULT_OOM_POLICY, #{max_heap_size => 4194304,
|
-define(DEFAULT_OOM_POLICY, #{
|
||||||
max_message_queue_len => 32000}).
|
max_heap_size => 4194304,
|
||||||
|
max_message_queue_len => 32000
|
||||||
|
}).
|
||||||
|
|
||||||
-elvis([{elvis_style, god_modules, disable}]).
|
-elvis([{elvis_style, god_modules, disable}]).
|
||||||
|
|
||||||
-spec childspec(supervisor:worker(), Mod :: atom())
|
-spec childspec(supervisor:worker(), Mod :: atom()) ->
|
||||||
-> supervisor:child_spec().
|
supervisor:child_spec().
|
||||||
childspec(Type, Mod) ->
|
childspec(Type, Mod) ->
|
||||||
childspec(Mod, Type, Mod, []).
|
childspec(Mod, Type, Mod, []).
|
||||||
|
|
||||||
-spec childspec(supervisor:worker(), Mod :: atom(), Args :: list())
|
-spec childspec(supervisor:worker(), Mod :: atom(), Args :: list()) ->
|
||||||
-> supervisor:child_spec().
|
supervisor:child_spec().
|
||||||
childspec(Type, Mod, Args) ->
|
childspec(Type, Mod, Args) ->
|
||||||
childspec(Mod, Type, Mod, Args).
|
childspec(Mod, Type, Mod, Args).
|
||||||
|
|
||||||
-spec childspec(atom(), supervisor:worker(), Mod :: atom(), Args :: list())
|
-spec childspec(atom(), supervisor:worker(), Mod :: atom(), Args :: list()) ->
|
||||||
-> supervisor:child_spec().
|
supervisor:child_spec().
|
||||||
childspec(Id, Type, Mod, Args) ->
|
childspec(Id, Type, Mod, Args) ->
|
||||||
#{ id => Id
|
#{
|
||||||
, start => {Mod, start_link, Args}
|
id => Id,
|
||||||
, type => Type
|
start => {Mod, start_link, Args},
|
||||||
}.
|
type => Type
|
||||||
|
}.
|
||||||
|
|
||||||
-spec supervisor_ret(supervisor:startchild_ret())
|
-spec supervisor_ret(supervisor:startchild_ret()) ->
|
||||||
-> {ok, pid()}
|
{ok, pid()}
|
||||||
| {error, supervisor:startchild_err()}.
|
| {error, supervisor:startchild_err()}.
|
||||||
supervisor_ret({ok, Pid, _Info}) ->
|
supervisor_ret({ok, Pid, _Info}) ->
|
||||||
{ok, Pid};
|
{ok, Pid};
|
||||||
supervisor_ret({error, {Reason, Child}}) ->
|
supervisor_ret({error, {Reason, Child}}) ->
|
||||||
|
|
@ -105,9 +111,9 @@ supervisor_ret({error, {Reason, Child}}) ->
|
||||||
supervisor_ret(Ret) ->
|
supervisor_ret(Ret) ->
|
||||||
Ret.
|
Ret.
|
||||||
|
|
||||||
-spec find_sup_child(Sup :: pid() | atom(), ChildId :: supervisor:child_id())
|
-spec find_sup_child(Sup :: pid() | atom(), ChildId :: supervisor:child_id()) ->
|
||||||
-> false
|
false
|
||||||
| {ok, pid()}.
|
| {ok, pid()}.
|
||||||
find_sup_child(Sup, ChildId) ->
|
find_sup_child(Sup, ChildId) ->
|
||||||
case lists:keyfind(ChildId, 1, supervisor:which_children(Sup)) of
|
case lists:keyfind(ChildId, 1, supervisor:which_children(Sup)) of
|
||||||
false -> false;
|
false -> false;
|
||||||
|
|
@ -115,13 +121,16 @@ find_sup_child(Sup, ChildId) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @doc start listeners. close all listeners if someone failed
|
%% @doc start listeners. close all listeners if someone failed
|
||||||
-spec start_listeners(Listeners :: list(),
|
-spec start_listeners(
|
||||||
GwName :: atom(),
|
Listeners :: list(),
|
||||||
Ctx :: map(),
|
GwName :: atom(),
|
||||||
ModCfg)
|
Ctx :: map(),
|
||||||
-> {ok, [pid()]}
|
ModCfg
|
||||||
| {error, term()}
|
) ->
|
||||||
when ModCfg :: #{frame_mod := atom(), chann_mod := atom()}.
|
{ok, [pid()]}
|
||||||
|
| {error, term()}
|
||||||
|
when
|
||||||
|
ModCfg :: #{frame_mod := atom(), chann_mod := atom()}.
|
||||||
start_listeners(Listeners, GwName, Ctx, ModCfg) ->
|
start_listeners(Listeners, GwName, Ctx, ModCfg) ->
|
||||||
start_listeners(Listeners, GwName, Ctx, ModCfg, []).
|
start_listeners(Listeners, GwName, Ctx, ModCfg, []).
|
||||||
|
|
||||||
|
|
@ -133,67 +142,94 @@ start_listeners([L | Ls], GwName, Ctx, ModCfg, Acc) ->
|
||||||
NAcc = Acc ++ [{listener, {{ListenerId, ListenOn}, Pid}}],
|
NAcc = Acc ++ [{listener, {{ListenerId, ListenOn}, Pid}}],
|
||||||
start_listeners(Ls, GwName, Ctx, ModCfg, NAcc);
|
start_listeners(Ls, GwName, Ctx, ModCfg, NAcc);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
lists:foreach(fun({listener, {{ListenerId, ListenOn}, _}}) ->
|
lists:foreach(
|
||||||
esockd:close({ListenerId, ListenOn})
|
fun({listener, {{ListenerId, ListenOn}, _}}) ->
|
||||||
end, Acc),
|
esockd:close({ListenerId, ListenOn})
|
||||||
|
end,
|
||||||
|
Acc
|
||||||
|
),
|
||||||
{error, {Reason, L}}
|
{error, {Reason, L}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec start_listener(GwName :: atom(),
|
-spec start_listener(
|
||||||
Ctx :: emqx_gateway_ctx:context(),
|
GwName :: atom(),
|
||||||
Listener :: tuple(),
|
Ctx :: emqx_gateway_ctx:context(),
|
||||||
ModCfg :: map())
|
Listener :: tuple(),
|
||||||
-> {ok, {ListenerId :: atom(), esockd:listen_on(), pid()}}
|
ModCfg :: map()
|
||||||
| {error, term()}.
|
) ->
|
||||||
start_listener(GwName, Ctx,
|
{ok, {ListenerId :: atom(), esockd:listen_on(), pid()}}
|
||||||
{Type, LisName, ListenOn, SocketOpts, Cfg}, ModCfg) ->
|
| {error, term()}.
|
||||||
|
start_listener(
|
||||||
|
GwName,
|
||||||
|
Ctx,
|
||||||
|
{Type, LisName, ListenOn, SocketOpts, Cfg},
|
||||||
|
ModCfg
|
||||||
|
) ->
|
||||||
ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
|
ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
|
||||||
ListenerId = emqx_gateway_utils:listener_id(GwName, Type, LisName),
|
ListenerId = emqx_gateway_utils:listener_id(GwName, Type, LisName),
|
||||||
|
|
||||||
NCfg = maps:merge(Cfg, ModCfg),
|
NCfg = maps:merge(Cfg, ModCfg),
|
||||||
case start_listener(GwName, Ctx, Type,
|
case
|
||||||
LisName, ListenOn, SocketOpts, NCfg) of
|
start_listener(
|
||||||
|
GwName,
|
||||||
|
Ctx,
|
||||||
|
Type,
|
||||||
|
LisName,
|
||||||
|
ListenOn,
|
||||||
|
SocketOpts,
|
||||||
|
NCfg
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, Pid} ->
|
{ok, Pid} ->
|
||||||
console_print("Gateway ~ts:~ts:~ts on ~ts started.~n",
|
console_print(
|
||||||
[GwName, Type, LisName, ListenOnStr]),
|
"Gateway ~ts:~ts:~ts on ~ts started.~n",
|
||||||
|
[GwName, Type, LisName, ListenOnStr]
|
||||||
|
),
|
||||||
{ok, {ListenerId, ListenOn, Pid}};
|
{ok, {ListenerId, ListenOn, Pid}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?ELOG("Failed to start gateway ~ts:~ts:~ts on ~ts: ~0p~n",
|
?ELOG(
|
||||||
[GwName, Type, LisName, ListenOnStr, Reason]),
|
"Failed to start gateway ~ts:~ts:~ts on ~ts: ~0p~n",
|
||||||
|
[GwName, Type, LisName, ListenOnStr, Reason]
|
||||||
|
),
|
||||||
emqx_gateway_utils:supervisor_ret({error, Reason})
|
emqx_gateway_utils:supervisor_ret({error, Reason})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) ->
|
start_listener(GwName, Ctx, Type, LisName, ListenOn, SocketOpts, Cfg) ->
|
||||||
Name = emqx_gateway_utils:listener_id(GwName, Type, LisName),
|
Name = emqx_gateway_utils:listener_id(GwName, Type, LisName),
|
||||||
NCfg = Cfg#{ ctx => Ctx
|
NCfg = Cfg#{
|
||||||
, listener => {GwName, Type, LisName}
|
ctx => Ctx,
|
||||||
},
|
listener => {GwName, Type, LisName}
|
||||||
|
},
|
||||||
NSocketOpts = merge_default(Type, SocketOpts),
|
NSocketOpts = merge_default(Type, SocketOpts),
|
||||||
MFA = {emqx_gateway_conn, start_link, [NCfg]},
|
MFA = {emqx_gateway_conn, start_link, [NCfg]},
|
||||||
do_start_listener(Type, Name, ListenOn, NSocketOpts, MFA).
|
do_start_listener(Type, Name, ListenOn, NSocketOpts, MFA).
|
||||||
|
|
||||||
merge_default(Udp, Options) ->
|
merge_default(Udp, Options) ->
|
||||||
{Key, Default} = case Udp of
|
{Key, Default} =
|
||||||
udp ->
|
case Udp of
|
||||||
{udp_options, default_udp_options()};
|
udp ->
|
||||||
dtls ->
|
{udp_options, default_udp_options()};
|
||||||
{udp_options, default_udp_options()};
|
dtls ->
|
||||||
tcp ->
|
{udp_options, default_udp_options()};
|
||||||
{tcp_options, default_tcp_options()};
|
tcp ->
|
||||||
ssl ->
|
{tcp_options, default_tcp_options()};
|
||||||
{tcp_options, default_tcp_options()}
|
ssl ->
|
||||||
end,
|
{tcp_options, default_tcp_options()}
|
||||||
|
end,
|
||||||
case lists:keytake(Key, 1, Options) of
|
case lists:keytake(Key, 1, Options) of
|
||||||
{value, {Key, TcpOpts}, Options1} ->
|
{value, {Key, TcpOpts}, Options1} ->
|
||||||
[{Key, emqx_misc:merge_opts(Default, TcpOpts)}
|
[
|
||||||
| Options1];
|
{Key, emqx_misc:merge_opts(Default, TcpOpts)}
|
||||||
|
| Options1
|
||||||
|
];
|
||||||
false ->
|
false ->
|
||||||
[{Key, Default} | Options]
|
[{Key, Default} | Options]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_start_listener(Type, Name, ListenOn, SocketOpts, MFA)
|
do_start_listener(Type, Name, ListenOn, SocketOpts, MFA) when
|
||||||
when Type == tcp;
|
Type == tcp;
|
||||||
Type == ssl ->
|
Type == ssl
|
||||||
|
->
|
||||||
esockd:open(Name, ListenOn, SocketOpts, MFA);
|
esockd:open(Name, ListenOn, SocketOpts, MFA);
|
||||||
do_start_listener(udp, Name, ListenOn, SocketOpts, MFA) ->
|
do_start_listener(udp, Name, ListenOn, SocketOpts, MFA) ->
|
||||||
esockd:open_udp(Name, ListenOn, SocketOpts, MFA);
|
esockd:open_udp(Name, ListenOn, SocketOpts, MFA);
|
||||||
|
|
@ -210,11 +246,15 @@ stop_listener(GwName, {Type, LisName, ListenOn, SocketOpts, Cfg}) ->
|
||||||
ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
|
ListenOnStr = emqx_gateway_utils:format_listenon(ListenOn),
|
||||||
case StopRet of
|
case StopRet of
|
||||||
ok ->
|
ok ->
|
||||||
console_print("Gateway ~ts:~ts:~ts on ~ts stopped.~n",
|
console_print(
|
||||||
[GwName, Type, LisName, ListenOnStr]);
|
"Gateway ~ts:~ts:~ts on ~ts stopped.~n",
|
||||||
|
[GwName, Type, LisName, ListenOnStr]
|
||||||
|
);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?ELOG("Failed to stop gateway ~ts:~ts:~ts on ~ts: ~0p~n",
|
?ELOG(
|
||||||
[GwName, Type, LisName, ListenOnStr, Reason])
|
"Failed to stop gateway ~ts:~ts:~ts on ~ts: ~0p~n",
|
||||||
|
[GwName, Type, LisName, ListenOnStr, Reason]
|
||||||
|
)
|
||||||
end,
|
end,
|
||||||
StopRet.
|
StopRet.
|
||||||
|
|
||||||
|
|
@ -228,17 +268,23 @@ console_print(Fmt, Args) -> ?ULOG(Fmt, Args).
|
||||||
console_print(_Fmt, _Args) -> ok.
|
console_print(_Fmt, _Args) -> ok.
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
apply({M, F, A}, A2) when is_atom(M),
|
apply({M, F, A}, A2) when
|
||||||
is_atom(M),
|
is_atom(M),
|
||||||
is_list(A),
|
is_atom(M),
|
||||||
is_list(A2) ->
|
is_list(A),
|
||||||
|
is_list(A2)
|
||||||
|
->
|
||||||
erlang:apply(M, F, A ++ A2);
|
erlang:apply(M, F, A ++ A2);
|
||||||
apply({F, A}, A2) when is_function(F),
|
apply({F, A}, A2) when
|
||||||
is_list(A),
|
is_function(F),
|
||||||
is_list(A2) ->
|
is_list(A),
|
||||||
|
is_list(A2)
|
||||||
|
->
|
||||||
erlang:apply(F, A ++ A2);
|
erlang:apply(F, A ++ A2);
|
||||||
apply(F, A2) when is_function(F),
|
apply(F, A2) when
|
||||||
is_list(A2) ->
|
is_function(F),
|
||||||
|
is_list(A2)
|
||||||
|
->
|
||||||
erlang:apply(F, A2).
|
erlang:apply(F, A2).
|
||||||
|
|
||||||
format_listenon(Port) when is_integer(Port) ->
|
format_listenon(Port) when is_integer(Port) ->
|
||||||
|
|
@ -255,21 +301,20 @@ parse_listenon(IpPort) when is_tuple(IpPort) ->
|
||||||
parse_listenon(Str) when is_binary(Str) ->
|
parse_listenon(Str) when is_binary(Str) ->
|
||||||
parse_listenon(binary_to_list(Str));
|
parse_listenon(binary_to_list(Str));
|
||||||
parse_listenon(Str) when is_list(Str) ->
|
parse_listenon(Str) when is_list(Str) ->
|
||||||
try list_to_integer(Str)
|
try
|
||||||
catch _ : _ ->
|
list_to_integer(Str)
|
||||||
case emqx_schema:to_ip_port(Str) of
|
catch
|
||||||
{ok, R} -> R;
|
_:_ ->
|
||||||
{error, _} ->
|
case emqx_schema:to_ip_port(Str) of
|
||||||
error({invalid_listenon_name, Str})
|
{ok, R} -> R;
|
||||||
end
|
{error, _} -> error({invalid_listenon_name, Str})
|
||||||
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
listener_id(GwName, Type, LisName) ->
|
listener_id(GwName, Type, LisName) ->
|
||||||
binary_to_atom(
|
binary_to_atom(
|
||||||
<<(bin(GwName))/binary, ":",
|
<<(bin(GwName))/binary, ":", (bin(Type))/binary, ":", (bin(LisName))/binary>>
|
||||||
(bin(Type))/binary, ":",
|
).
|
||||||
(bin(LisName))/binary
|
|
||||||
>>).
|
|
||||||
|
|
||||||
parse_listener_id(Id) when is_atom(Id) ->
|
parse_listener_id(Id) when is_atom(Id) ->
|
||||||
parse_listener_id(atom_to_binary(Id));
|
parse_listener_id(atom_to_binary(Id));
|
||||||
|
|
@ -278,16 +323,17 @@ parse_listener_id(Id) ->
|
||||||
[GwName, Type, Name] = binary:split(bin(Id), <<":">>, [global]),
|
[GwName, Type, Name] = binary:split(bin(Id), <<":">>, [global]),
|
||||||
{GwName, Type, Name}
|
{GwName, Type, Name}
|
||||||
catch
|
catch
|
||||||
_ : _ -> error({invalid_listener_id, Id})
|
_:_ -> error({invalid_listener_id, Id})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
is_running(ListenerId, #{<<"bind">> := ListenOn0}) ->
|
is_running(ListenerId, #{<<"bind">> := ListenOn0}) ->
|
||||||
ListenOn = emqx_gateway_utils:parse_listenon(ListenOn0),
|
ListenOn = emqx_gateway_utils:parse_listenon(ListenOn0),
|
||||||
try esockd:listener({ListenerId, ListenOn}) of
|
try esockd:listener({ListenerId, ListenOn}) of
|
||||||
Pid when is_pid(Pid)->
|
Pid when is_pid(Pid) ->
|
||||||
true
|
true
|
||||||
catch _:_ ->
|
catch
|
||||||
false
|
_:_ ->
|
||||||
|
false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% same with emqx_authentication:global_chain/1
|
%% same with emqx_authentication:global_chain/1
|
||||||
|
|
@ -315,10 +361,13 @@ unix_ts_to_rfc3339(Keys, Map) when is_list(Keys) ->
|
||||||
lists:foldl(fun(K, Acc) -> unix_ts_to_rfc3339(K, Acc) end, Map, Keys);
|
lists:foldl(fun(K, Acc) -> unix_ts_to_rfc3339(K, Acc) end, Map, Keys);
|
||||||
unix_ts_to_rfc3339(Key, Map) ->
|
unix_ts_to_rfc3339(Key, Map) ->
|
||||||
case maps:get(Key, Map, undefined) of
|
case maps:get(Key, Map, undefined) of
|
||||||
undefined -> Map;
|
undefined ->
|
||||||
|
Map;
|
||||||
Ts ->
|
Ts ->
|
||||||
Map#{Key =>
|
Map#{
|
||||||
emqx_rule_funcs:unix_ts_to_rfc3339(Ts, <<"millisecond">>)}
|
Key =>
|
||||||
|
emqx_rule_funcs:unix_ts_to_rfc3339(Ts, <<"millisecond">>)
|
||||||
|
}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
unix_ts_to_rfc3339(Ts) ->
|
unix_ts_to_rfc3339(Ts) ->
|
||||||
|
|
@ -330,60 +379,104 @@ stringfy(T) when is_list(T); is_binary(T) ->
|
||||||
stringfy(T) ->
|
stringfy(T) ->
|
||||||
iolist_to_binary(io_lib:format("~0p", [T])).
|
iolist_to_binary(io_lib:format("~0p", [T])).
|
||||||
|
|
||||||
-spec normalize_config(emqx_config:config())
|
-spec normalize_config(emqx_config:config()) ->
|
||||||
-> list({ Type :: udp | tcp | ssl | dtls
|
list({
|
||||||
, Name :: atom()
|
Type :: udp | tcp | ssl | dtls,
|
||||||
, ListenOn :: esockd:listen_on()
|
Name :: atom(),
|
||||||
, SocketOpts :: esockd:option()
|
ListenOn :: esockd:listen_on(),
|
||||||
, Cfg :: map()
|
SocketOpts :: esockd:option(),
|
||||||
}).
|
Cfg :: map()
|
||||||
|
}).
|
||||||
normalize_config(RawConf) ->
|
normalize_config(RawConf) ->
|
||||||
LisMap = maps:get(listeners, RawConf, #{}),
|
LisMap = maps:get(listeners, RawConf, #{}),
|
||||||
Cfg0 = maps:without([listeners], RawConf),
|
Cfg0 = maps:without([listeners], RawConf),
|
||||||
lists:append(maps:fold(fun(Type, Liss, AccIn1) ->
|
lists:append(
|
||||||
Listeners =
|
maps:fold(
|
||||||
maps:fold(fun(Name, Confs, AccIn2) ->
|
fun(Type, Liss, AccIn1) ->
|
||||||
ListenOn = maps:get(bind, Confs),
|
Listeners =
|
||||||
SocketOpts = esockd_opts(Type, Confs),
|
maps:fold(
|
||||||
RemainCfgs = maps:without(
|
fun(Name, Confs, AccIn2) ->
|
||||||
[bind, tcp, ssl, udp, dtls]
|
ListenOn = maps:get(bind, Confs),
|
||||||
++ proplists:get_keys(SocketOpts), Confs),
|
SocketOpts = esockd_opts(Type, Confs),
|
||||||
Cfg = maps:merge(Cfg0, RemainCfgs),
|
RemainCfgs = maps:without(
|
||||||
[{Type, Name, ListenOn, SocketOpts, Cfg} | AccIn2]
|
[bind, tcp, ssl, udp, dtls] ++
|
||||||
end, [], Liss),
|
proplists:get_keys(SocketOpts),
|
||||||
[Listeners | AccIn1]
|
Confs
|
||||||
end, [], LisMap)).
|
),
|
||||||
|
Cfg = maps:merge(Cfg0, RemainCfgs),
|
||||||
|
[{Type, Name, ListenOn, SocketOpts, Cfg} | AccIn2]
|
||||||
|
end,
|
||||||
|
[],
|
||||||
|
Liss
|
||||||
|
),
|
||||||
|
[Listeners | AccIn1]
|
||||||
|
end,
|
||||||
|
[],
|
||||||
|
LisMap
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
esockd_opts(Type, Opts0) ->
|
esockd_opts(Type, Opts0) ->
|
||||||
Opts1 = maps:with([acceptors, max_connections, max_conn_rate,
|
Opts1 = maps:with(
|
||||||
proxy_protocol, proxy_protocol_timeout], Opts0),
|
[
|
||||||
|
acceptors,
|
||||||
|
max_connections,
|
||||||
|
max_conn_rate,
|
||||||
|
proxy_protocol,
|
||||||
|
proxy_protocol_timeout
|
||||||
|
],
|
||||||
|
Opts0
|
||||||
|
),
|
||||||
Opts2 = Opts1#{access_rules => esockd_access_rules(maps:get(access_rules, Opts0, []))},
|
Opts2 = Opts1#{access_rules => esockd_access_rules(maps:get(access_rules, Opts0, []))},
|
||||||
maps:to_list(case Type of
|
maps:to_list(
|
||||||
tcp -> Opts2#{tcp_options => sock_opts(tcp, Opts0)};
|
case Type of
|
||||||
ssl -> Opts2#{tcp_options => sock_opts(tcp, Opts0),
|
tcp ->
|
||||||
ssl_options => ssl_opts(ssl, Opts0)};
|
Opts2#{tcp_options => sock_opts(tcp, Opts0)};
|
||||||
udp -> Opts2#{udp_options => sock_opts(udp, Opts0)};
|
ssl ->
|
||||||
dtls -> Opts2#{udp_options => sock_opts(udp, Opts0),
|
Opts2#{
|
||||||
dtls_options => ssl_opts(dtls, Opts0)}
|
tcp_options => sock_opts(tcp, Opts0),
|
||||||
end).
|
ssl_options => ssl_opts(ssl, Opts0)
|
||||||
|
};
|
||||||
|
udp ->
|
||||||
|
Opts2#{udp_options => sock_opts(udp, Opts0)};
|
||||||
|
dtls ->
|
||||||
|
Opts2#{
|
||||||
|
udp_options => sock_opts(udp, Opts0),
|
||||||
|
dtls_options => ssl_opts(dtls, Opts0)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
).
|
||||||
|
|
||||||
esockd_access_rules(StrRules) ->
|
esockd_access_rules(StrRules) ->
|
||||||
Access = fun(S) ->
|
Access = fun(S) ->
|
||||||
[A, CIDR] = string:tokens(S, " "),
|
[A, CIDR] = string:tokens(S, " "),
|
||||||
{list_to_atom(A), case CIDR of "all" -> all; _ -> CIDR end}
|
{
|
||||||
|
list_to_atom(A),
|
||||||
|
case CIDR of
|
||||||
|
"all" -> all;
|
||||||
|
_ -> CIDR
|
||||||
|
end
|
||||||
|
}
|
||||||
end,
|
end,
|
||||||
[Access(R) || R <- StrRules].
|
[Access(R) || R <- StrRules].
|
||||||
|
|
||||||
ssl_opts(Name, Opts) ->
|
ssl_opts(Name, Opts) ->
|
||||||
maps:to_list(
|
maps:to_list(
|
||||||
emqx_tls_lib:drop_tls13_for_old_otp(
|
emqx_tls_lib:drop_tls13_for_old_otp(
|
||||||
maps:without([enable],
|
maps:without(
|
||||||
maps:get(Name, Opts, #{})))).
|
[enable],
|
||||||
|
maps:get(Name, Opts, #{})
|
||||||
|
)
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
sock_opts(Name, Opts) ->
|
sock_opts(Name, Opts) ->
|
||||||
maps:to_list(
|
maps:to_list(
|
||||||
maps:without([active_n],
|
maps:without(
|
||||||
maps:get(Name, Opts, #{}))).
|
[active_n],
|
||||||
|
maps:get(Name, Opts, #{})
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Envs
|
%% Envs
|
||||||
|
|
@ -417,7 +510,10 @@ oom_policy(Options) ->
|
||||||
|
|
||||||
-spec stats_timer(map()) -> undefined | disabled.
|
-spec stats_timer(map()) -> undefined | disabled.
|
||||||
stats_timer(Options) ->
|
stats_timer(Options) ->
|
||||||
case enable_stats(Options) of true -> undefined; false -> disabled end.
|
case enable_stats(Options) of
|
||||||
|
true -> undefined;
|
||||||
|
false -> disabled
|
||||||
|
end.
|
||||||
|
|
||||||
-spec enable_stats(map()) -> boolean().
|
-spec enable_stats(map()) -> boolean().
|
||||||
enable_stats(Options) ->
|
enable_stats(Options) ->
|
||||||
|
|
@ -427,16 +523,26 @@ enable_stats(Options) ->
|
||||||
%% Envs2
|
%% Envs2
|
||||||
|
|
||||||
default_tcp_options() ->
|
default_tcp_options() ->
|
||||||
[binary, {packet, raw}, {reuseaddr, true},
|
[
|
||||||
{nodelay, true}, {backlog, 512}].
|
binary,
|
||||||
|
{packet, raw},
|
||||||
|
{reuseaddr, true},
|
||||||
|
{nodelay, true},
|
||||||
|
{backlog, 512}
|
||||||
|
].
|
||||||
|
|
||||||
default_udp_options() ->
|
default_udp_options() ->
|
||||||
[binary].
|
[binary].
|
||||||
|
|
||||||
default_subopts() ->
|
default_subopts() ->
|
||||||
#{rh => 1, %% Retain Handling
|
%% Retain Handling
|
||||||
rap => 0, %% Retain as Publish
|
#{
|
||||||
nl => 0, %% No Local
|
rh => 1,
|
||||||
qos => 0, %% QoS
|
%% Retain as Publish
|
||||||
is_new => true
|
rap => 0,
|
||||||
}.
|
%% No Local
|
||||||
|
nl => 0,
|
||||||
|
%% QoS
|
||||||
|
qos => 0,
|
||||||
|
is_new => true
|
||||||
|
}.
|
||||||
|
|
|
||||||
|
|
@ -21,63 +21,65 @@
|
||||||
-include_lib("emqx/include/types.hrl").
|
-include_lib("emqx/include/types.hrl").
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
|
-export([
|
||||||
|
info/1,
|
||||||
|
info/2,
|
||||||
|
stats/1
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ info/1
|
-export([
|
||||||
, info/2
|
init/2,
|
||||||
, stats/1
|
handle_in/2,
|
||||||
]).
|
handle_deliver/2,
|
||||||
|
handle_timeout/3,
|
||||||
-export([ init/2
|
handle_call/3,
|
||||||
, handle_in/2
|
handle_cast/2,
|
||||||
, handle_deliver/2
|
handle_info/2,
|
||||||
, handle_timeout/3
|
terminate/2
|
||||||
, handle_call/3
|
]).
|
||||||
, handle_cast/2
|
|
||||||
, handle_info/2
|
|
||||||
, terminate/2
|
|
||||||
]).
|
|
||||||
|
|
||||||
-export_type([channel/0]).
|
-export_type([channel/0]).
|
||||||
|
|
||||||
-record(channel, {
|
-record(channel, {
|
||||||
%% Context
|
%% Context
|
||||||
ctx :: emqx_gateway_ctx:context(),
|
ctx :: emqx_gateway_ctx:context(),
|
||||||
%% gRPC channel options
|
%% gRPC channel options
|
||||||
gcli :: map(),
|
gcli :: map(),
|
||||||
%% Conn info
|
%% Conn info
|
||||||
conninfo :: emqx_types:conninfo(),
|
conninfo :: emqx_types:conninfo(),
|
||||||
%% Client info from `register` function
|
%% Client info from `register` function
|
||||||
clientinfo :: maybe(map()),
|
clientinfo :: maybe(map()),
|
||||||
%% Connection state
|
%% Connection state
|
||||||
conn_state :: conn_state(),
|
conn_state :: conn_state(),
|
||||||
%% Subscription
|
%% Subscription
|
||||||
subscriptions = #{},
|
subscriptions = #{},
|
||||||
%% Request queue
|
%% Request queue
|
||||||
rqueue = queue:new(),
|
rqueue = queue:new(),
|
||||||
%% Inflight function name
|
%% Inflight function name
|
||||||
inflight = undefined,
|
inflight = undefined,
|
||||||
%% Keepalive
|
%% Keepalive
|
||||||
keepalive :: maybe(emqx_keepalive:keepalive()),
|
keepalive :: maybe(emqx_keepalive:keepalive()),
|
||||||
%% Timers
|
%% Timers
|
||||||
timers :: #{atom() => disabled | maybe(reference())},
|
timers :: #{atom() => disabled | maybe(reference())},
|
||||||
%% Closed reason
|
%% Closed reason
|
||||||
closed_reason = undefined
|
closed_reason = undefined
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-opaque(channel() :: #channel{}).
|
-opaque channel() :: #channel{}.
|
||||||
|
|
||||||
-type(conn_state() :: idle | connecting | connected | disconnected).
|
-type conn_state() :: idle | connecting | connected | disconnected.
|
||||||
|
|
||||||
-type(reply() :: {outgoing, binary()}
|
-type reply() ::
|
||||||
| {outgoing, [binary()]}
|
{outgoing, binary()}
|
||||||
| {close, Reason :: atom()}).
|
| {outgoing, [binary()]}
|
||||||
|
| {close, Reason :: atom()}.
|
||||||
|
|
||||||
-type(replies() :: emqx_types:packet() | reply() | [reply()]).
|
-type replies() :: emqx_types:packet() | reply() | [reply()].
|
||||||
|
|
||||||
-define(TIMER_TABLE, #{
|
-define(TIMER_TABLE, #{
|
||||||
alive_timer => keepalive,
|
alive_timer => keepalive,
|
||||||
force_timer => force_close
|
force_timer => force_close
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-define(INFO_KEYS, [conninfo, conn_state, clientinfo, session, will_msg]).
|
-define(INFO_KEYS, [conninfo, conn_state, clientinfo, session, will_msg]).
|
||||||
|
|
||||||
|
|
@ -90,7 +92,7 @@
|
||||||
info(Channel) ->
|
info(Channel) ->
|
||||||
maps:from_list(info(?INFO_KEYS, Channel)).
|
maps:from_list(info(?INFO_KEYS, Channel)).
|
||||||
|
|
||||||
-spec info(list(atom())|atom(), channel()) -> term().
|
-spec info(list(atom()) | atom(), channel()) -> term().
|
||||||
info(Keys, Channel) when is_list(Keys) ->
|
info(Keys, Channel) when is_list(Keys) ->
|
||||||
[{Key, info(Key, Channel)} || Key <- Keys];
|
[{Key, info(Key, Channel)} || Key <- Keys];
|
||||||
info(conninfo, #channel{conninfo = ConnInfo}) ->
|
info(conninfo, #channel{conninfo = ConnInfo}) ->
|
||||||
|
|
@ -99,13 +101,17 @@ info(clientid, #channel{clientinfo = ClientInfo}) ->
|
||||||
maps:get(clientid, ClientInfo, undefined);
|
maps:get(clientid, ClientInfo, undefined);
|
||||||
info(clientinfo, #channel{clientinfo = ClientInfo}) ->
|
info(clientinfo, #channel{clientinfo = ClientInfo}) ->
|
||||||
ClientInfo;
|
ClientInfo;
|
||||||
info(session, #channel{subscriptions = Subs,
|
info(session, #channel{
|
||||||
conninfo = ConnInfo}) ->
|
subscriptions = Subs,
|
||||||
#{subscriptions => Subs,
|
conninfo = ConnInfo
|
||||||
upgrade_qos => false,
|
}) ->
|
||||||
retry_interval => 0,
|
#{
|
||||||
await_rel_timeout => 0,
|
subscriptions => Subs,
|
||||||
created_at => maps:get(connected_at, ConnInfo)};
|
upgrade_qos => false,
|
||||||
|
retry_interval => 0,
|
||||||
|
await_rel_timeout => 0,
|
||||||
|
created_at => maps:get(connected_at, ConnInfo)
|
||||||
|
};
|
||||||
info(conn_state, #channel{conn_state = ConnState}) ->
|
info(conn_state, #channel{conn_state = ConnState}) ->
|
||||||
ConnState;
|
ConnState;
|
||||||
info(will_msg, _) ->
|
info(will_msg, _) ->
|
||||||
|
|
@ -115,62 +121,80 @@ info(ctx, #channel{ctx = Ctx}) ->
|
||||||
|
|
||||||
-spec stats(channel()) -> emqx_types:stats().
|
-spec stats(channel()) -> emqx_types:stats().
|
||||||
stats(#channel{subscriptions = Subs}) ->
|
stats(#channel{subscriptions = Subs}) ->
|
||||||
[{subscriptions_cnt, maps:size(Subs)},
|
[
|
||||||
{subscriptions_max, 0},
|
{subscriptions_cnt, maps:size(Subs)},
|
||||||
{inflight_cnt, 0},
|
{subscriptions_max, 0},
|
||||||
{inflight_max, 0},
|
{inflight_cnt, 0},
|
||||||
{mqueue_len, 0},
|
{inflight_max, 0},
|
||||||
{mqueue_max, 0},
|
{mqueue_len, 0},
|
||||||
{mqueue_dropped, 0},
|
{mqueue_max, 0},
|
||||||
{next_pkt_id, 0},
|
{mqueue_dropped, 0},
|
||||||
{awaiting_rel_cnt, 0},
|
{next_pkt_id, 0},
|
||||||
{awaiting_rel_max, 0}].
|
{awaiting_rel_cnt, 0},
|
||||||
|
{awaiting_rel_max, 0}
|
||||||
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Init the channel
|
%% Init the channel
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec init(emqx_exproto_types:conninfo(), map()) -> channel().
|
-spec init(emqx_exproto_types:conninfo(), map()) -> channel().
|
||||||
init(ConnInfo = #{socktype := Socktype,
|
init(
|
||||||
peername := Peername,
|
ConnInfo = #{
|
||||||
sockname := Sockname,
|
socktype := Socktype,
|
||||||
peercert := Peercert}, Options) ->
|
peername := Peername,
|
||||||
|
sockname := Sockname,
|
||||||
|
peercert := Peercert
|
||||||
|
},
|
||||||
|
Options
|
||||||
|
) ->
|
||||||
Ctx = maps:get(ctx, Options),
|
Ctx = maps:get(ctx, Options),
|
||||||
GRpcChann = maps:get(handler, Options),
|
GRpcChann = maps:get(handler, Options),
|
||||||
PoolName = maps:get(pool_name, Options),
|
PoolName = maps:get(pool_name, Options),
|
||||||
NConnInfo = default_conninfo(ConnInfo),
|
NConnInfo = default_conninfo(ConnInfo),
|
||||||
ListenerId = case maps:get(listener, Options, undefined) of
|
ListenerId =
|
||||||
undefined -> undefined;
|
case maps:get(listener, Options, undefined) of
|
||||||
{GwName, Type, LisName} ->
|
undefined -> undefined;
|
||||||
emqx_gateway_utils:listener_id(GwName, Type, LisName)
|
{GwName, Type, LisName} -> emqx_gateway_utils:listener_id(GwName, Type, LisName)
|
||||||
end,
|
end,
|
||||||
ClientInfo = maps:put(listener, ListenerId, default_clientinfo(ConnInfo)),
|
ClientInfo = maps:put(listener, ListenerId, default_clientinfo(ConnInfo)),
|
||||||
Channel = #channel{
|
Channel = #channel{
|
||||||
ctx = Ctx,
|
ctx = Ctx,
|
||||||
gcli = #{channel => GRpcChann, pool_name => PoolName},
|
gcli = #{channel => GRpcChann, pool_name => PoolName},
|
||||||
conninfo = NConnInfo,
|
conninfo = NConnInfo,
|
||||||
clientinfo = ClientInfo,
|
clientinfo = ClientInfo,
|
||||||
conn_state = connecting,
|
conn_state = connecting,
|
||||||
timers = #{}
|
timers = #{}
|
||||||
},
|
},
|
||||||
|
|
||||||
Req = #{conninfo =>
|
Req = #{
|
||||||
peercert(Peercert,
|
conninfo =>
|
||||||
#{socktype => socktype(Socktype),
|
peercert(
|
||||||
peername => address(Peername),
|
Peercert,
|
||||||
sockname => address(Sockname)})},
|
#{
|
||||||
|
socktype => socktype(Socktype),
|
||||||
|
peername => address(Peername),
|
||||||
|
sockname => address(Sockname)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
},
|
||||||
try_dispatch(on_socket_created, wrap(Req), Channel).
|
try_dispatch(on_socket_created, wrap(Req), Channel).
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
peercert(NoSsl, ConnInfo) when NoSsl == nossl;
|
peercert(NoSsl, ConnInfo) when
|
||||||
NoSsl == undefined ->
|
NoSsl == nossl;
|
||||||
|
NoSsl == undefined
|
||||||
|
->
|
||||||
ConnInfo;
|
ConnInfo;
|
||||||
peercert(Peercert, ConnInfo) ->
|
peercert(Peercert, ConnInfo) ->
|
||||||
Fn = fun(_, V) -> V =/= undefined end,
|
Fn = fun(_, V) -> V =/= undefined end,
|
||||||
Infos = maps:filter(Fn,
|
Infos = maps:filter(
|
||||||
#{cn => esockd_peercert:common_name(Peercert),
|
Fn,
|
||||||
dn => esockd_peercert:subject(Peercert)}
|
#{
|
||||||
),
|
cn => esockd_peercert:common_name(Peercert),
|
||||||
|
dn => esockd_peercert:subject(Peercert)
|
||||||
|
}
|
||||||
|
),
|
||||||
case maps:size(Infos) of
|
case maps:size(Infos) of
|
||||||
0 ->
|
0 ->
|
||||||
ConnInfo;
|
ConnInfo;
|
||||||
|
|
@ -192,46 +216,64 @@ address({Host, Port}) ->
|
||||||
%% Handle incoming packet
|
%% Handle incoming packet
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec handle_in(binary(), channel())
|
-spec handle_in(binary(), channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {shutdown, Reason :: term(), channel()}.
|
| {shutdown, Reason :: term(), channel()}.
|
||||||
handle_in(Data, Channel) ->
|
handle_in(Data, Channel) ->
|
||||||
Req = #{bytes => Data},
|
Req = #{bytes => Data},
|
||||||
{ok, try_dispatch(on_received_bytes, wrap(Req), Channel)}.
|
{ok, try_dispatch(on_received_bytes, wrap(Req), Channel)}.
|
||||||
|
|
||||||
-spec handle_deliver(list(emqx_types:deliver()), channel())
|
-spec handle_deliver(list(emqx_types:deliver()), channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {shutdown, Reason :: term(), channel()}.
|
| {shutdown, Reason :: term(), channel()}.
|
||||||
handle_deliver(Delivers, Channel = #channel{ctx = Ctx,
|
handle_deliver(
|
||||||
clientinfo = ClientInfo}) ->
|
Delivers,
|
||||||
|
Channel = #channel{
|
||||||
|
ctx = Ctx,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
%% XXX: ?? Nack delivers from shared subscriptions
|
%% XXX: ?? Nack delivers from shared subscriptions
|
||||||
Mountpoint = maps:get(mountpoint, ClientInfo),
|
Mountpoint = maps:get(mountpoint, ClientInfo),
|
||||||
NodeStr = atom_to_binary(node(), utf8),
|
NodeStr = atom_to_binary(node(), utf8),
|
||||||
Msgs = lists:map(fun({_, _, Msg}) ->
|
Msgs = lists:map(
|
||||||
ok = metrics_inc(Ctx, 'messages.delivered'),
|
fun({_, _, Msg}) ->
|
||||||
Msg1 = emqx_hooks:run_fold('message.delivered',
|
ok = metrics_inc(Ctx, 'messages.delivered'),
|
||||||
[ClientInfo], Msg),
|
Msg1 = emqx_hooks:run_fold(
|
||||||
NMsg = emqx_mountpoint:unmount(Mountpoint, Msg1),
|
'message.delivered',
|
||||||
#{node => NodeStr,
|
[ClientInfo],
|
||||||
id => emqx_guid:to_hexstr(emqx_message:id(NMsg)),
|
Msg
|
||||||
qos => emqx_message:qos(NMsg),
|
),
|
||||||
from => fmt_from(emqx_message:from(NMsg)),
|
NMsg = emqx_mountpoint:unmount(Mountpoint, Msg1),
|
||||||
topic => emqx_message:topic(NMsg),
|
#{
|
||||||
payload => emqx_message:payload(NMsg),
|
node => NodeStr,
|
||||||
timestamp => emqx_message:timestamp(NMsg)
|
id => emqx_guid:to_hexstr(emqx_message:id(NMsg)),
|
||||||
}
|
qos => emqx_message:qos(NMsg),
|
||||||
end, Delivers),
|
from => fmt_from(emqx_message:from(NMsg)),
|
||||||
|
topic => emqx_message:topic(NMsg),
|
||||||
|
payload => emqx_message:payload(NMsg),
|
||||||
|
timestamp => emqx_message:timestamp(NMsg)
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
Delivers
|
||||||
|
),
|
||||||
Req = #{messages => Msgs},
|
Req = #{messages => Msgs},
|
||||||
{ok, try_dispatch(on_received_messages, wrap(Req), Channel)}.
|
{ok, try_dispatch(on_received_messages, wrap(Req), Channel)}.
|
||||||
|
|
||||||
-spec handle_timeout(reference(), Msg :: term(), channel())
|
-spec handle_timeout(reference(), Msg :: term(), channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {shutdown, Reason :: term(), channel()}.
|
| {shutdown, Reason :: term(), channel()}.
|
||||||
handle_timeout(_TRef, {keepalive, _StatVal},
|
handle_timeout(
|
||||||
Channel = #channel{keepalive = undefined}) ->
|
_TRef,
|
||||||
|
{keepalive, _StatVal},
|
||||||
|
Channel = #channel{keepalive = undefined}
|
||||||
|
) ->
|
||||||
{ok, Channel};
|
{ok, Channel};
|
||||||
handle_timeout(_TRef, {keepalive, StatVal},
|
handle_timeout(
|
||||||
Channel = #channel{keepalive = Keepalive}) ->
|
_TRef,
|
||||||
|
{keepalive, StatVal},
|
||||||
|
Channel = #channel{keepalive = Keepalive}
|
||||||
|
) ->
|
||||||
case emqx_keepalive:check(StatVal, Keepalive) of
|
case emqx_keepalive:check(StatVal, Keepalive) of
|
||||||
{ok, NKeepalive} ->
|
{ok, NKeepalive} ->
|
||||||
NChannel = Channel#channel{keepalive = NKeepalive},
|
NChannel = Channel#channel{keepalive = NKeepalive},
|
||||||
|
|
@ -240,97 +282,119 @@ handle_timeout(_TRef, {keepalive, StatVal},
|
||||||
Req = #{type => 'KEEPALIVE'},
|
Req = #{type => 'KEEPALIVE'},
|
||||||
{ok, try_dispatch(on_timer_timeout, wrap(Req), Channel)}
|
{ok, try_dispatch(on_timer_timeout, wrap(Req), Channel)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_timeout(_TRef, force_close, Channel = #channel{closed_reason = Reason}) ->
|
handle_timeout(_TRef, force_close, Channel = #channel{closed_reason = Reason}) ->
|
||||||
{shutdown, {error, {force_close, Reason}}, Channel};
|
{shutdown, {error, {force_close, Reason}}, Channel};
|
||||||
|
|
||||||
handle_timeout(_TRef, Msg, Channel) ->
|
handle_timeout(_TRef, Msg, Channel) ->
|
||||||
?SLOG(warning, #{msg => "unexpected_timeout_signal",
|
?SLOG(warning, #{
|
||||||
signal => Msg}),
|
msg => "unexpected_timeout_signal",
|
||||||
|
signal => Msg
|
||||||
|
}),
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
-spec handle_call(Req :: any(), From :: any(), channel())
|
-spec handle_call(Req :: any(), From :: any(), channel()) ->
|
||||||
-> {reply, Reply :: term(), channel()}
|
{reply, Reply :: term(), channel()}
|
||||||
| {reply, Reply :: term(), replies(), channel()}
|
| {reply, Reply :: term(), replies(), channel()}
|
||||||
| {shutdown, Reason :: term(), Reply :: term(), channel()}.
|
| {shutdown, Reason :: term(), Reply :: term(), channel()}.
|
||||||
|
|
||||||
handle_call({send, Data}, _From, Channel) ->
|
handle_call({send, Data}, _From, Channel) ->
|
||||||
{reply, ok, [{outgoing, Data}], Channel};
|
{reply, ok, [{outgoing, Data}], Channel};
|
||||||
|
|
||||||
handle_call(close, _From, Channel = #channel{conn_state = connected}) ->
|
handle_call(close, _From, Channel = #channel{conn_state = connected}) ->
|
||||||
{reply, ok, [{event, disconnected}, {close, normal}], Channel};
|
{reply, ok, [{event, disconnected}, {close, normal}], Channel};
|
||||||
handle_call(close, _From, Channel) ->
|
handle_call(close, _From, Channel) ->
|
||||||
{reply, ok, [{close, normal}], Channel};
|
{reply, ok, [{close, normal}], Channel};
|
||||||
|
handle_call(
|
||||||
handle_call({auth, ClientInfo, _Password}, _From,
|
{auth, ClientInfo, _Password},
|
||||||
Channel = #channel{conn_state = connected}) ->
|
_From,
|
||||||
?SLOG(warning, #{ msg => "ingore_duplicated_authorized_command"
|
Channel = #channel{conn_state = connected}
|
||||||
, request_clientinfo => ClientInfo
|
) ->
|
||||||
}),
|
?SLOG(warning, #{
|
||||||
|
msg => "ingore_duplicated_authorized_command",
|
||||||
|
request_clientinfo => ClientInfo
|
||||||
|
}),
|
||||||
{reply, {error, ?RESP_PERMISSION_DENY, <<"Duplicated authenticate command">>}, Channel};
|
{reply, {error, ?RESP_PERMISSION_DENY, <<"Duplicated authenticate command">>}, Channel};
|
||||||
handle_call({auth, ClientInfo0, Password}, _From,
|
handle_call(
|
||||||
Channel = #channel{
|
{auth, ClientInfo0, Password},
|
||||||
ctx = Ctx,
|
_From,
|
||||||
conninfo = ConnInfo,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo}) ->
|
ctx = Ctx,
|
||||||
|
conninfo = ConnInfo,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
ClientInfo1 = enrich_clientinfo(ClientInfo0, ClientInfo),
|
ClientInfo1 = enrich_clientinfo(ClientInfo0, ClientInfo),
|
||||||
ConnInfo1 = enrich_conninfo(ClientInfo0, ConnInfo),
|
ConnInfo1 = enrich_conninfo(ClientInfo0, ConnInfo),
|
||||||
|
|
||||||
Channel1 = Channel#channel{conninfo = ConnInfo1,
|
Channel1 = Channel#channel{
|
||||||
clientinfo = ClientInfo1},
|
conninfo = ConnInfo1,
|
||||||
|
clientinfo = ClientInfo1
|
||||||
|
},
|
||||||
|
|
||||||
#{clientid := ClientId, username := Username} = ClientInfo1,
|
#{clientid := ClientId, username := Username} = ClientInfo1,
|
||||||
|
|
||||||
case emqx_gateway_ctx:authenticate(
|
case
|
||||||
Ctx, ClientInfo1#{password => Password}) of
|
emqx_gateway_ctx:authenticate(
|
||||||
|
Ctx, ClientInfo1#{password => Password}
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, NClientInfo} ->
|
{ok, NClientInfo} ->
|
||||||
SessFun = fun(_, _) -> #{} end,
|
SessFun = fun(_, _) -> #{} end,
|
||||||
emqx_logger:set_metadata_clientid(ClientId),
|
emqx_logger:set_metadata_clientid(ClientId),
|
||||||
case emqx_gateway_ctx:open_session(
|
case
|
||||||
Ctx,
|
emqx_gateway_ctx:open_session(
|
||||||
true,
|
Ctx,
|
||||||
NClientInfo,
|
true,
|
||||||
ConnInfo1,
|
NClientInfo,
|
||||||
SessFun
|
ConnInfo1,
|
||||||
) of
|
SessFun
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, _Session} ->
|
{ok, _Session} ->
|
||||||
?SLOG(debug, #{ msg => "client_login_succeed"
|
?SLOG(debug, #{
|
||||||
, clientid => ClientId
|
msg => "client_login_succeed",
|
||||||
, username => Username
|
clientid => ClientId,
|
||||||
}),
|
username => Username
|
||||||
|
}),
|
||||||
{reply, ok, [{event, connected}],
|
{reply, ok, [{event, connected}],
|
||||||
ensure_connected(Channel1#channel{clientinfo = NClientInfo})};
|
ensure_connected(Channel1#channel{clientinfo = NClientInfo})};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(warning, #{ msg => "client_login_failed"
|
?SLOG(warning, #{
|
||||||
, clientid => ClientId
|
msg => "client_login_failed",
|
||||||
, username => Username
|
clientid => ClientId,
|
||||||
, reason => Reason
|
username => Username,
|
||||||
}),
|
reason => Reason
|
||||||
|
}),
|
||||||
{reply, {error, ?RESP_PERMISSION_DENY, Reason}, Channel}
|
{reply, {error, ?RESP_PERMISSION_DENY, Reason}, Channel}
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(warning, #{ msg => "client_login_failed"
|
?SLOG(warning, #{
|
||||||
, clientid => ClientId
|
msg => "client_login_failed",
|
||||||
, username => Username
|
clientid => ClientId,
|
||||||
, reason => Reason}),
|
username => Username,
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
{reply, {error, ?RESP_PERMISSION_DENY, Reason}, Channel}
|
{reply, {error, ?RESP_PERMISSION_DENY, Reason}, Channel}
|
||||||
end;
|
end;
|
||||||
|
handle_call(
|
||||||
handle_call({start_timer, keepalive, Interval}, _From,
|
{start_timer, keepalive, Interval},
|
||||||
Channel = #channel{
|
_From,
|
||||||
conninfo = ConnInfo,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo
|
conninfo = ConnInfo,
|
||||||
}) ->
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NConnInfo = ConnInfo#{keepalive => Interval},
|
NConnInfo = ConnInfo#{keepalive => Interval},
|
||||||
NClientInfo = ClientInfo#{keepalive => Interval},
|
NClientInfo = ClientInfo#{keepalive => Interval},
|
||||||
NChannel = Channel#channel{conninfo = NConnInfo, clientinfo = NClientInfo},
|
NChannel = Channel#channel{conninfo = NConnInfo, clientinfo = NClientInfo},
|
||||||
{reply, ok, ensure_keepalive(NChannel)};
|
{reply, ok, ensure_keepalive(NChannel)};
|
||||||
|
handle_call(
|
||||||
handle_call({subscribe_from_client, TopicFilter, Qos}, _From,
|
{subscribe_from_client, TopicFilter, Qos},
|
||||||
Channel = #channel{
|
_From,
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
conn_state = connected,
|
ctx = Ctx,
|
||||||
clientinfo = ClientInfo}) ->
|
conn_state = connected,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
case emqx_gateway_ctx:authorize(Ctx, ClientInfo, subscribe, TopicFilter) of
|
case emqx_gateway_ctx:authorize(Ctx, ClientInfo, subscribe, TopicFilter) of
|
||||||
deny ->
|
deny ->
|
||||||
{reply, {error, ?RESP_PERMISSION_DENY, <<"Authorization deny">>}, Channel};
|
{reply, {error, ?RESP_PERMISSION_DENY, <<"Authorization deny">>}, Channel};
|
||||||
|
|
@ -338,31 +402,35 @@ handle_call({subscribe_from_client, TopicFilter, Qos}, _From,
|
||||||
{ok, _, NChannel} = do_subscribe([{TopicFilter, #{qos => Qos}}], Channel),
|
{ok, _, NChannel} = do_subscribe([{TopicFilter, #{qos => Qos}}], Channel),
|
||||||
{reply, ok, NChannel}
|
{reply, ok, NChannel}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call({subscribe, Topic, SubOpts}, _From, Channel) ->
|
handle_call({subscribe, Topic, SubOpts}, _From, Channel) ->
|
||||||
{ok,
|
{ok, [{NTopicFilter, NSubOpts}], NChannel} = do_subscribe([{Topic, SubOpts}], Channel),
|
||||||
[{NTopicFilter, NSubOpts}], NChannel} = do_subscribe([{Topic, SubOpts}], Channel),
|
|
||||||
{reply, {ok, {NTopicFilter, NSubOpts}}, NChannel};
|
{reply, {ok, {NTopicFilter, NSubOpts}}, NChannel};
|
||||||
|
handle_call(
|
||||||
handle_call({unsubscribe_from_client, TopicFilter}, _From,
|
{unsubscribe_from_client, TopicFilter},
|
||||||
Channel = #channel{conn_state = connected}) ->
|
_From,
|
||||||
|
Channel = #channel{conn_state = connected}
|
||||||
|
) ->
|
||||||
{ok, NChannel} = do_unsubscribe([{TopicFilter, #{}}], Channel),
|
{ok, NChannel} = do_unsubscribe([{TopicFilter, #{}}], Channel),
|
||||||
{reply, ok, NChannel};
|
{reply, ok, NChannel};
|
||||||
|
|
||||||
handle_call({unsubscribe, Topic}, _From, Channel) ->
|
handle_call({unsubscribe, Topic}, _From, Channel) ->
|
||||||
{ok, NChannel} = do_unsubscribe([Topic], Channel),
|
{ok, NChannel} = do_unsubscribe([Topic], Channel),
|
||||||
{reply, ok, NChannel};
|
{reply, ok, NChannel};
|
||||||
|
|
||||||
handle_call(subscriptions, _From, Channel = #channel{subscriptions = Subs}) ->
|
handle_call(subscriptions, _From, Channel = #channel{subscriptions = Subs}) ->
|
||||||
{reply, {ok, maps:to_list(Subs)}, Channel};
|
{reply, {ok, maps:to_list(Subs)}, Channel};
|
||||||
|
handle_call(
|
||||||
handle_call({publish, Topic, Qos, Payload}, _From,
|
{publish, Topic, Qos, Payload},
|
||||||
Channel = #channel{
|
_From,
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
conn_state = connected,
|
ctx = Ctx,
|
||||||
clientinfo = ClientInfo
|
conn_state = connected,
|
||||||
= #{clientid := From,
|
clientinfo =
|
||||||
mountpoint := Mountpoint}}) ->
|
ClientInfo =
|
||||||
|
#{
|
||||||
|
clientid := From,
|
||||||
|
mountpoint := Mountpoint
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) ->
|
||||||
case emqx_gateway_ctx:authorize(Ctx, ClientInfo, publish, Topic) of
|
case emqx_gateway_ctx:authorize(Ctx, ClientInfo, publish, Topic) of
|
||||||
deny ->
|
deny ->
|
||||||
{reply, {error, ?RESP_PERMISSION_DENY, <<"Authorization deny">>}, Channel};
|
{reply, {error, ?RESP_PERMISSION_DENY, <<"Authorization deny">>}, Channel};
|
||||||
|
|
@ -372,36 +440,39 @@ handle_call({publish, Topic, Qos, Payload}, _From,
|
||||||
_ = emqx:publish(NMsg),
|
_ = emqx:publish(NMsg),
|
||||||
{reply, ok, Channel}
|
{reply, ok, Channel}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_call(kick, _From, Channel) ->
|
handle_call(kick, _From, Channel) ->
|
||||||
{shutdown, kicked, ok, ensure_disconnected(kicked, Channel)};
|
{shutdown, kicked, ok, ensure_disconnected(kicked, Channel)};
|
||||||
|
|
||||||
handle_call(discard, _From, Channel) ->
|
handle_call(discard, _From, Channel) ->
|
||||||
{shutdown, discarded, ok, Channel};
|
{shutdown, discarded, ok, Channel};
|
||||||
|
|
||||||
handle_call(Req, _From, Channel) ->
|
handle_call(Req, _From, Channel) ->
|
||||||
?SLOG(warning, #{ msg => "unexpected_call"
|
?SLOG(warning, #{
|
||||||
, call => Req
|
msg => "unexpected_call",
|
||||||
}),
|
call => Req
|
||||||
|
}),
|
||||||
{reply, {error, unexpected_call}, Channel}.
|
{reply, {error, unexpected_call}, Channel}.
|
||||||
|
|
||||||
-spec handle_cast(any(), channel())
|
-spec handle_cast(any(), channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {ok, replies(), channel()}
|
| {ok, replies(), channel()}
|
||||||
| {shutdown, Reason :: term(), channel()}.
|
| {shutdown, Reason :: term(), channel()}.
|
||||||
handle_cast(Req, Channel) ->
|
handle_cast(Req, Channel) ->
|
||||||
?SLOG(warning, #{ msg => "unexpected_call"
|
?SLOG(warning, #{
|
||||||
, call => Req
|
msg => "unexpected_call",
|
||||||
}),
|
call => Req
|
||||||
|
}),
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
-spec handle_info(any(), channel())
|
-spec handle_info(any(), channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {shutdown, Reason :: term(), channel()}.
|
| {shutdown, Reason :: term(), channel()}.
|
||||||
handle_info({sock_closed, Reason},
|
handle_info(
|
||||||
Channel = #channel{rqueue = Queue, inflight = Inflight}) ->
|
{sock_closed, Reason},
|
||||||
case queue:len(Queue) =:= 0
|
Channel = #channel{rqueue = Queue, inflight = Inflight}
|
||||||
andalso Inflight =:= undefined of
|
) ->
|
||||||
|
case
|
||||||
|
queue:len(Queue) =:= 0 andalso
|
||||||
|
Inflight =:= undefined
|
||||||
|
of
|
||||||
true ->
|
true ->
|
||||||
Channel1 = ensure_disconnected({sock_closed, Reason}, Channel),
|
Channel1 = ensure_disconnected({sock_closed, Reason}, Channel),
|
||||||
{shutdown, Reason, Channel1};
|
{shutdown, Reason, Channel1};
|
||||||
|
|
@ -411,25 +482,24 @@ handle_info({sock_closed, Reason},
|
||||||
Channel2 = ensure_timer(force_timer, Channel1),
|
Channel2 = ensure_timer(force_timer, Channel1),
|
||||||
{ok, ensure_disconnected({sock_closed, Reason}, Channel2)}
|
{ok, ensure_disconnected({sock_closed, Reason}, Channel2)}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
handle_info({hreply, on_socket_created, ok}, Channel) ->
|
handle_info({hreply, on_socket_created, ok}, Channel) ->
|
||||||
dispatch_or_close_process(Channel#channel{inflight = undefined});
|
dispatch_or_close_process(Channel#channel{inflight = undefined});
|
||||||
handle_info({hreply, FunName, ok}, Channel)
|
handle_info({hreply, FunName, ok}, Channel) when
|
||||||
when FunName == on_socket_closed;
|
FunName == on_socket_closed;
|
||||||
FunName == on_received_bytes;
|
FunName == on_received_bytes;
|
||||||
FunName == on_received_messages;
|
FunName == on_received_messages;
|
||||||
FunName == on_timer_timeout ->
|
FunName == on_timer_timeout
|
||||||
|
->
|
||||||
dispatch_or_close_process(Channel#channel{inflight = undefined});
|
dispatch_or_close_process(Channel#channel{inflight = undefined});
|
||||||
handle_info({hreply, FunName, {error, Reason}}, Channel) ->
|
handle_info({hreply, FunName, {error, Reason}}, Channel) ->
|
||||||
{shutdown, {error, {FunName, Reason}}, Channel};
|
{shutdown, {error, {FunName, Reason}}, Channel};
|
||||||
|
|
||||||
handle_info({subscribe, _}, Channel) ->
|
handle_info({subscribe, _}, Channel) ->
|
||||||
{ok, Channel};
|
{ok, Channel};
|
||||||
|
|
||||||
handle_info(Info, Channel) ->
|
handle_info(Info, Channel) ->
|
||||||
?SLOG(warning, #{ msg => "unexpected_info"
|
?SLOG(warning, #{
|
||||||
, info => Info
|
msg => "unexpected_info",
|
||||||
}),
|
info => Info
|
||||||
|
}),
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
-spec terminate(any(), channel()) -> channel().
|
-spec terminate(any(), channel()) -> channel().
|
||||||
|
|
@ -446,13 +516,22 @@ do_subscribe(TopicFilters, Channel) ->
|
||||||
fun({TopicFilter, SubOpts}, {MadeSubs, ChannelAcc}) ->
|
fun({TopicFilter, SubOpts}, {MadeSubs, ChannelAcc}) ->
|
||||||
{Sub, Channel1} = do_subscribe(TopicFilter, SubOpts, ChannelAcc),
|
{Sub, Channel1} = do_subscribe(TopicFilter, SubOpts, ChannelAcc),
|
||||||
{MadeSubs ++ [Sub], Channel1}
|
{MadeSubs ++ [Sub], Channel1}
|
||||||
end, {[], Channel}, parse_topic_filters(TopicFilters)),
|
end,
|
||||||
|
{[], Channel},
|
||||||
|
parse_topic_filters(TopicFilters)
|
||||||
|
),
|
||||||
{ok, MadeSubs, NChannel}.
|
{ok, MadeSubs, NChannel}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
do_subscribe(TopicFilter, SubOpts, Channel =
|
do_subscribe(
|
||||||
#channel{clientinfo = ClientInfo = #{mountpoint := Mountpoint},
|
TopicFilter,
|
||||||
subscriptions = Subs}) ->
|
SubOpts,
|
||||||
|
Channel =
|
||||||
|
#channel{
|
||||||
|
clientinfo = ClientInfo = #{mountpoint := Mountpoint},
|
||||||
|
subscriptions = Subs
|
||||||
|
}
|
||||||
|
) ->
|
||||||
%% Mountpoint first
|
%% Mountpoint first
|
||||||
NTopicFilter = emqx_mountpoint:mount(Mountpoint, TopicFilter),
|
NTopicFilter = emqx_mountpoint:mount(Mountpoint, TopicFilter),
|
||||||
NSubOpts = maps:merge(emqx_gateway_utils:default_subopts(), SubOpts),
|
NSubOpts = maps:merge(emqx_gateway_utils:default_subopts(), SubOpts),
|
||||||
|
|
@ -462,34 +541,49 @@ do_subscribe(TopicFilter, SubOpts, Channel =
|
||||||
case IsNew of
|
case IsNew of
|
||||||
true ->
|
true ->
|
||||||
ok = emqx:subscribe(NTopicFilter, SubId, NSubOpts),
|
ok = emqx:subscribe(NTopicFilter, SubId, NSubOpts),
|
||||||
ok = emqx_hooks:run('session.subscribed',
|
ok = emqx_hooks:run(
|
||||||
[ClientInfo, NTopicFilter, NSubOpts#{is_new => IsNew}]),
|
'session.subscribed',
|
||||||
{{NTopicFilter, NSubOpts},
|
[ClientInfo, NTopicFilter, NSubOpts#{is_new => IsNew}]
|
||||||
Channel#channel{subscriptions = Subs#{NTopicFilter => NSubOpts}}};
|
),
|
||||||
|
{{NTopicFilter, NSubOpts}, Channel#channel{
|
||||||
|
subscriptions = Subs#{NTopicFilter => NSubOpts}
|
||||||
|
}};
|
||||||
_ ->
|
_ ->
|
||||||
%% Update subopts
|
%% Update subopts
|
||||||
ok = emqx:subscribe(NTopicFilter, SubId, NSubOpts),
|
ok = emqx:subscribe(NTopicFilter, SubId, NSubOpts),
|
||||||
{{NTopicFilter, NSubOpts},
|
{{NTopicFilter, NSubOpts}, Channel#channel{
|
||||||
Channel#channel{subscriptions = Subs#{NTopicFilter => NSubOpts}}}
|
subscriptions = Subs#{NTopicFilter => NSubOpts}
|
||||||
|
}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_unsubscribe(TopicFilters, Channel) ->
|
do_unsubscribe(TopicFilters, Channel) ->
|
||||||
NChannel = lists:foldl(
|
NChannel = lists:foldl(
|
||||||
fun({TopicFilter, SubOpts}, ChannelAcc) ->
|
fun({TopicFilter, SubOpts}, ChannelAcc) ->
|
||||||
do_unsubscribe(TopicFilter, SubOpts, ChannelAcc)
|
do_unsubscribe(TopicFilter, SubOpts, ChannelAcc)
|
||||||
end, Channel, parse_topic_filters(TopicFilters)),
|
end,
|
||||||
|
Channel,
|
||||||
|
parse_topic_filters(TopicFilters)
|
||||||
|
),
|
||||||
{ok, NChannel}.
|
{ok, NChannel}.
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
do_unsubscribe(TopicFilter, UnSubOpts, Channel =
|
do_unsubscribe(
|
||||||
#channel{clientinfo = ClientInfo = #{mountpoint := Mountpoint},
|
TopicFilter,
|
||||||
subscriptions = Subs}) ->
|
UnSubOpts,
|
||||||
|
Channel =
|
||||||
|
#channel{
|
||||||
|
clientinfo = ClientInfo = #{mountpoint := Mountpoint},
|
||||||
|
subscriptions = Subs
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NTopicFilter = emqx_mountpoint:mount(Mountpoint, TopicFilter),
|
NTopicFilter = emqx_mountpoint:mount(Mountpoint, TopicFilter),
|
||||||
case maps:find(NTopicFilter, Subs) of
|
case maps:find(NTopicFilter, Subs) of
|
||||||
{ok, SubOpts} ->
|
{ok, SubOpts} ->
|
||||||
ok = emqx:unsubscribe(NTopicFilter),
|
ok = emqx:unsubscribe(NTopicFilter),
|
||||||
ok = emqx_hooks:run('session.unsubscribed',
|
ok = emqx_hooks:run(
|
||||||
[ClientInfo, TopicFilter, maps:merge(SubOpts, UnSubOpts)]),
|
'session.unsubscribed',
|
||||||
|
[ClientInfo, TopicFilter, maps:merge(SubOpts, UnSubOpts)]
|
||||||
|
),
|
||||||
Channel#channel{subscriptions = maps:remove(NTopicFilter, Subs)};
|
Channel#channel{subscriptions = maps:remove(NTopicFilter, Subs)};
|
||||||
_ ->
|
_ ->
|
||||||
Channel
|
Channel
|
||||||
|
|
@ -503,25 +597,32 @@ parse_topic_filters(TopicFilters) ->
|
||||||
%% Ensure & Hooks
|
%% Ensure & Hooks
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
ensure_connected(Channel = #channel{
|
ensure_connected(
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
conninfo = ConnInfo,
|
ctx = Ctx,
|
||||||
clientinfo = ClientInfo}) ->
|
conninfo = ConnInfo,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NConnInfo = ConnInfo#{connected_at => erlang:system_time(millisecond)},
|
NConnInfo = ConnInfo#{connected_at => erlang:system_time(millisecond)},
|
||||||
ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
|
ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
|
||||||
Channel#channel{conninfo = NConnInfo,
|
Channel#channel{
|
||||||
conn_state = connected
|
conninfo = NConnInfo,
|
||||||
}.
|
conn_state = connected
|
||||||
|
}.
|
||||||
|
|
||||||
ensure_disconnected(Reason, Channel = #channel{
|
ensure_disconnected(
|
||||||
ctx = Ctx,
|
Reason,
|
||||||
conn_state = connected,
|
Channel = #channel{
|
||||||
conninfo = ConnInfo,
|
ctx = Ctx,
|
||||||
clientinfo = ClientInfo}) ->
|
conn_state = connected,
|
||||||
|
conninfo = ConnInfo,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
||||||
ok = run_hooks(Ctx, 'client.disconnected', [ClientInfo, Reason, NConnInfo]),
|
ok = run_hooks(Ctx, 'client.disconnected', [ClientInfo, Reason, NConnInfo]),
|
||||||
Channel#channel{conninfo = NConnInfo, conn_state = disconnected};
|
Channel#channel{conninfo = NConnInfo, conn_state = disconnected};
|
||||||
|
|
||||||
ensure_disconnected(_Reason, Channel = #channel{conninfo = ConnInfo}) ->
|
ensure_disconnected(_Reason, Channel = #channel{conninfo = ConnInfo}) ->
|
||||||
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
||||||
Channel#channel{conninfo = NConnInfo, conn_state = disconnected}.
|
Channel#channel{conninfo = NConnInfo, conn_state = disconnected}.
|
||||||
|
|
@ -549,8 +650,9 @@ ensure_timer(Name, Channel = #channel{timers = Timers}) ->
|
||||||
TRef = maps:get(Name, Timers, undefined),
|
TRef = maps:get(Name, Timers, undefined),
|
||||||
Time = interval(Name, Channel),
|
Time = interval(Name, Channel),
|
||||||
case TRef == undefined andalso Time > 0 of
|
case TRef == undefined andalso Time > 0 of
|
||||||
true -> ensure_timer(Name, Time, Channel);
|
true -> ensure_timer(Name, Time, Channel);
|
||||||
false -> Channel %% Timer disabled or exists
|
%% Timer disabled or exists
|
||||||
|
false -> Channel
|
||||||
end.
|
end.
|
||||||
|
|
||||||
ensure_timer(Name, Time, Channel = #channel{timers = Timers}) ->
|
ensure_timer(Name, Time, Channel = #channel{timers = Timers}) ->
|
||||||
|
|
@ -574,12 +676,15 @@ interval(alive_timer, #channel{keepalive = Keepalive}) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
wrap(Req) ->
|
wrap(Req) ->
|
||||||
Req#{conn => base64:encode(term_to_binary(self()))}.
|
Req#{conn => base64:encode(term_to_binary(self()))}.
|
||||||
|
|
||||||
dispatch_or_close_process(Channel = #channel{
|
dispatch_or_close_process(
|
||||||
rqueue = Queue,
|
Channel = #channel{
|
||||||
inflight = undefined,
|
rqueue = Queue,
|
||||||
gcli = GClient}) ->
|
inflight = undefined,
|
||||||
|
gcli = GClient
|
||||||
|
}
|
||||||
|
) ->
|
||||||
case queue:out(Queue) of
|
case queue:out(Queue) of
|
||||||
{empty, _} ->
|
{empty, _} ->
|
||||||
case Channel#channel.conn_state of
|
case Channel#channel.conn_state of
|
||||||
|
|
@ -613,30 +718,36 @@ enrich_clientinfo(InClientInfo = #{proto_name := ProtoName}, ClientInfo) ->
|
||||||
NClientInfo#{protocol => proto_name_to_protocol(ProtoName)}.
|
NClientInfo#{protocol => proto_name_to_protocol(ProtoName)}.
|
||||||
|
|
||||||
default_conninfo(ConnInfo) ->
|
default_conninfo(ConnInfo) ->
|
||||||
ConnInfo#{clean_start => true,
|
ConnInfo#{
|
||||||
clientid => undefined,
|
clean_start => true,
|
||||||
username => undefined,
|
clientid => undefined,
|
||||||
conn_mod => undefined,
|
username => undefined,
|
||||||
conn_props => #{},
|
conn_mod => undefined,
|
||||||
connected => true,
|
conn_props => #{},
|
||||||
proto_name => <<"exproto">>,
|
connected => true,
|
||||||
proto_ver => <<"1.0">>,
|
proto_name => <<"exproto">>,
|
||||||
connected_at => erlang:system_time(millisecond),
|
proto_ver => <<"1.0">>,
|
||||||
keepalive => 0,
|
connected_at => erlang:system_time(millisecond),
|
||||||
receive_maximum => 0,
|
keepalive => 0,
|
||||||
expiry_interval => 0}.
|
receive_maximum => 0,
|
||||||
|
expiry_interval => 0
|
||||||
|
}.
|
||||||
|
|
||||||
default_clientinfo(#{peername := {PeerHost, _},
|
default_clientinfo(#{
|
||||||
sockname := {_, SockPort}}) ->
|
peername := {PeerHost, _},
|
||||||
#{zone => default,
|
sockname := {_, SockPort}
|
||||||
protocol => exproto,
|
}) ->
|
||||||
peerhost => PeerHost,
|
#{
|
||||||
sockport => SockPort,
|
zone => default,
|
||||||
clientid => undefined,
|
protocol => exproto,
|
||||||
username => undefined,
|
peerhost => PeerHost,
|
||||||
is_bridge => false,
|
sockport => SockPort,
|
||||||
is_superuser => false,
|
clientid => undefined,
|
||||||
mountpoint => undefined}.
|
username => undefined,
|
||||||
|
is_bridge => false,
|
||||||
|
is_superuser => false,
|
||||||
|
mountpoint => undefined
|
||||||
|
}.
|
||||||
|
|
||||||
stringfy(Reason) ->
|
stringfy(Reason) ->
|
||||||
unicode:characters_to_binary((io_lib:format("~0p", [Reason]))).
|
unicode:characters_to_binary((io_lib:format("~0p", [Reason]))).
|
||||||
|
|
|
||||||
|
|
@ -20,14 +20,15 @@
|
||||||
|
|
||||||
-behaviour(emqx_gateway_frame).
|
-behaviour(emqx_gateway_frame).
|
||||||
|
|
||||||
-export([ initial_parse_state/1
|
-export([
|
||||||
, serialize_opts/0
|
initial_parse_state/1,
|
||||||
, parse/2
|
serialize_opts/0,
|
||||||
, serialize_pkt/2
|
parse/2,
|
||||||
, format/1
|
serialize_pkt/2,
|
||||||
, is_message/1
|
format/1,
|
||||||
, type/1
|
is_message/1,
|
||||||
]).
|
type/1
|
||||||
|
]).
|
||||||
|
|
||||||
initial_parse_state(_) ->
|
initial_parse_state(_) ->
|
||||||
#{}.
|
#{}.
|
||||||
|
|
@ -47,4 +48,3 @@ format(Data) ->
|
||||||
is_message(_) -> true.
|
is_message(_) -> true.
|
||||||
|
|
||||||
type(_) -> unknown.
|
type(_) -> unknown.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,26 +21,26 @@
|
||||||
|
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([async_call/3]).
|
-export([async_call/3]).
|
||||||
|
|
||||||
-export([start_link/2]).
|
-export([start_link/2]).
|
||||||
|
|
||||||
%% gen_server callbacks
|
%% gen_server callbacks
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
-record(state, {
|
-record(state, {
|
||||||
pool,
|
pool,
|
||||||
id,
|
id,
|
||||||
streams
|
streams
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-define(CONN_ADAPTER_MOD, emqx_exproto_v_1_connection_handler_client).
|
-define(CONN_ADAPTER_MOD, emqx_exproto_v_1_connection_handler_client).
|
||||||
|
|
||||||
|
|
@ -49,11 +49,18 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
start_link(Pool, Id) ->
|
start_link(Pool, Id) ->
|
||||||
gen_server:start_link({local, emqx_misc:proc_name(?MODULE, Id)},
|
gen_server:start_link(
|
||||||
?MODULE, [Pool, Id], []).
|
{local, emqx_misc:proc_name(?MODULE, Id)},
|
||||||
|
?MODULE,
|
||||||
|
[Pool, Id],
|
||||||
|
[]
|
||||||
|
).
|
||||||
|
|
||||||
async_call(FunName, Req = #{conn := Conn},
|
async_call(
|
||||||
Options = #{pool_name := PoolName}) ->
|
FunName,
|
||||||
|
Req = #{conn := Conn},
|
||||||
|
Options = #{pool_name := PoolName}
|
||||||
|
) ->
|
||||||
cast(pick(PoolName, Conn), {rpc, FunName, Req, Options, self()}).
|
cast(pick(PoolName, Conn), {rpc, FunName, Req, Options, self()}).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -82,42 +89,49 @@ handle_call(_Request, _From, State) ->
|
||||||
handle_cast({rpc, Fun, Req, Options, From}, State = #state{streams = Streams}) ->
|
handle_cast({rpc, Fun, Req, Options, From}, State = #state{streams = Streams}) ->
|
||||||
case ensure_stream_opened(Fun, Options, Streams) of
|
case ensure_stream_opened(Fun, Options, Streams) of
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{ msg => "request_grpc_server_failed"
|
?SLOG(error, #{
|
||||||
, function => {?CONN_ADAPTER_MOD, Fun, Options}
|
msg => "request_grpc_server_failed",
|
||||||
, reason => Reason}),
|
function => {?CONN_ADAPTER_MOD, Fun, Options},
|
||||||
|
reason => Reason
|
||||||
|
}),
|
||||||
reply(From, Fun, {error, Reason}),
|
reply(From, Fun, {error, Reason}),
|
||||||
{noreply, State#state{streams = Streams#{Fun => undefined}}};
|
{noreply, State#state{streams = Streams#{Fun => undefined}}};
|
||||||
{ok, Stream} ->
|
{ok, Stream} ->
|
||||||
case catch grpc_client:send(Stream, Req) of
|
case catch grpc_client:send(Stream, Req) of
|
||||||
ok ->
|
ok ->
|
||||||
?SLOG(debug, #{ msg => "send_grpc_request_succeed"
|
?SLOG(debug, #{
|
||||||
, function => {?CONN_ADAPTER_MOD, Fun}
|
msg => "send_grpc_request_succeed",
|
||||||
, request => Req
|
function => {?CONN_ADAPTER_MOD, Fun},
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
reply(From, Fun, ok),
|
reply(From, Fun, ok),
|
||||||
{noreply, State#state{streams = Streams#{Fun => Stream}}};
|
{noreply, State#state{streams = Streams#{Fun => Stream}}};
|
||||||
{'EXIT', {not_found, _Stk}} ->
|
{'EXIT', {not_found, _Stk}} ->
|
||||||
%% Not found the stream, reopen it
|
%% Not found the stream, reopen it
|
||||||
?SLOG(info, #{ msg => "cannt_find_old_stream_ref"
|
?SLOG(info, #{
|
||||||
, function => {?CONN_ADAPTER_MOD, Fun}
|
msg => "cannt_find_old_stream_ref",
|
||||||
}),
|
function => {?CONN_ADAPTER_MOD, Fun}
|
||||||
|
}),
|
||||||
handle_cast(
|
handle_cast(
|
||||||
{rpc, Fun, Req, Options, From},
|
{rpc, Fun, Req, Options, From},
|
||||||
State#state{streams = maps:remove(Fun, Streams)});
|
State#state{streams = maps:remove(Fun, Streams)}
|
||||||
|
);
|
||||||
{'EXIT', {timeout, _Stk}} ->
|
{'EXIT', {timeout, _Stk}} ->
|
||||||
?SLOG(error, #{ msg => "send_grpc_request_timeout"
|
?SLOG(error, #{
|
||||||
, function => {?CONN_ADAPTER_MOD, Fun}
|
msg => "send_grpc_request_timeout",
|
||||||
, request => Req
|
function => {?CONN_ADAPTER_MOD, Fun},
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
reply(From, Fun, {error, timeout}),
|
reply(From, Fun, {error, timeout}),
|
||||||
{noreply, State#state{streams = Streams#{Fun => Stream}}};
|
{noreply, State#state{streams = Streams#{Fun => Stream}}};
|
||||||
{'EXIT', {Reason1, Stk}} ->
|
{'EXIT', {Reason1, Stk}} ->
|
||||||
?SLOG(error, #{ msg => "send_grpc_request_failed"
|
?SLOG(error, #{
|
||||||
, function => {?CONN_ADAPTER_MOD, Fun}
|
msg => "send_grpc_request_failed",
|
||||||
, request => Req
|
function => {?CONN_ADAPTER_MOD, Fun},
|
||||||
, error => Reason1
|
request => Req,
|
||||||
, stacktrace => Stk
|
error => Reason1,
|
||||||
}),
|
stacktrace => Stk
|
||||||
|
}),
|
||||||
reply(From, Fun, {error, Reason1}),
|
reply(From, Fun, {error, Reason1}),
|
||||||
{noreply, State#state{streams = Streams#{Fun => undefined}}}
|
{noreply, State#state{streams = Streams#{Fun => undefined}}}
|
||||||
end
|
end
|
||||||
|
|
@ -147,5 +161,6 @@ ensure_stream_opened(Fun, Options, Streams) ->
|
||||||
{ok, Stream} -> {ok, Stream};
|
{ok, Stream} -> {ok, Stream};
|
||||||
{error, Reason} -> {error, Reason}
|
{error, Reason} -> {error, Reason}
|
||||||
end;
|
end;
|
||||||
Stream -> {ok, Stream}
|
Stream ->
|
||||||
|
{ok, Stream}
|
||||||
end.
|
end.
|
||||||
|
|
|
||||||
|
|
@ -27,49 +27,58 @@
|
||||||
-define(DEFAULT_CALL_TIMEOUT, 5000).
|
-define(DEFAULT_CALL_TIMEOUT, 5000).
|
||||||
|
|
||||||
%% gRPC server callbacks
|
%% gRPC server callbacks
|
||||||
-export([ send/2
|
-export([
|
||||||
, close/2
|
send/2,
|
||||||
, authenticate/2
|
close/2,
|
||||||
, start_timer/2
|
authenticate/2,
|
||||||
, publish/2
|
start_timer/2,
|
||||||
, subscribe/2
|
publish/2,
|
||||||
, unsubscribe/2
|
subscribe/2,
|
||||||
]).
|
unsubscribe/2
|
||||||
|
]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% gRPC ConnectionAdapter service
|
%% gRPC ConnectionAdapter service
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec send(emqx_exproto_pb:send_bytes_request(), grpc:metadata())
|
-spec send(emqx_exproto_pb:send_bytes_request(), grpc:metadata()) ->
|
||||||
-> {ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
{ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
||||||
| {error, grpc_cowboy_h:error_response()}.
|
| {error, grpc_cowboy_h:error_response()}.
|
||||||
send(Req = #{conn := Conn, bytes := Bytes}, Md) ->
|
send(Req = #{conn := Conn, bytes := Bytes}, Md) ->
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
?SLOG(debug, #{
|
||||||
, function => ?FUNCTION_NAME
|
msg => "recv_grpc_function_call",
|
||||||
, request => Req
|
function => ?FUNCTION_NAME,
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
{ok, response(call(Conn, {send, Bytes})), Md}.
|
{ok, response(call(Conn, {send, Bytes})), Md}.
|
||||||
|
|
||||||
-spec close(emqx_exproto_pb:close_socket_request(), grpc:metadata())
|
-spec close(emqx_exproto_pb:close_socket_request(), grpc:metadata()) ->
|
||||||
-> {ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
{ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
||||||
| {error, grpc_cowboy_h:error_response()}.
|
| {error, grpc_cowboy_h:error_response()}.
|
||||||
close(Req = #{conn := Conn}, Md) ->
|
close(Req = #{conn := Conn}, Md) ->
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
?SLOG(debug, #{
|
||||||
, function => ?FUNCTION_NAME
|
msg => "recv_grpc_function_call",
|
||||||
, request => Req
|
function => ?FUNCTION_NAME,
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
{ok, response(call(Conn, close)), Md}.
|
{ok, response(call(Conn, close)), Md}.
|
||||||
|
|
||||||
-spec authenticate(emqx_exproto_pb:authenticate_request(), grpc:metadata())
|
-spec authenticate(emqx_exproto_pb:authenticate_request(), grpc:metadata()) ->
|
||||||
-> {ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
{ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
||||||
| {error, grpc_cowboy_h:error_response()}.
|
| {error, grpc_cowboy_h:error_response()}.
|
||||||
authenticate(Req = #{conn := Conn,
|
authenticate(
|
||||||
password := Password,
|
Req = #{
|
||||||
clientinfo := ClientInfo}, Md) ->
|
conn := Conn,
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
password := Password,
|
||||||
, function => ?FUNCTION_NAME
|
clientinfo := ClientInfo
|
||||||
, request => Req
|
},
|
||||||
}),
|
Md
|
||||||
|
) ->
|
||||||
|
?SLOG(debug, #{
|
||||||
|
msg => "recv_grpc_function_call",
|
||||||
|
function => ?FUNCTION_NAME,
|
||||||
|
request => Req
|
||||||
|
}),
|
||||||
case validate(clientinfo, ClientInfo) of
|
case validate(clientinfo, ClientInfo) of
|
||||||
false ->
|
false ->
|
||||||
{ok, response({error, ?RESP_REQUIRED_PARAMS_MISSED}), Md};
|
{ok, response({error, ?RESP_REQUIRED_PARAMS_MISSED}), Md};
|
||||||
|
|
@ -77,70 +86,78 @@ authenticate(Req = #{conn := Conn,
|
||||||
{ok, response(call(Conn, {auth, ClientInfo, Password})), Md}
|
{ok, response(call(Conn, {auth, ClientInfo, Password})), Md}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec start_timer(emqx_exproto_pb:timer_request(), grpc:metadata())
|
-spec start_timer(emqx_exproto_pb:timer_request(), grpc:metadata()) ->
|
||||||
-> {ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
{ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
||||||
| {error, grpc_cowboy_h:error_response()}.
|
| {error, grpc_cowboy_h:error_response()}.
|
||||||
start_timer(Req = #{conn := Conn, type := Type, interval := Interval}, Md)
|
start_timer(Req = #{conn := Conn, type := Type, interval := Interval}, Md) when
|
||||||
when Type =:= 'KEEPALIVE' andalso Interval > 0 ->
|
Type =:= 'KEEPALIVE' andalso Interval > 0
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
->
|
||||||
, function => ?FUNCTION_NAME
|
?SLOG(debug, #{
|
||||||
, request => Req
|
msg => "recv_grpc_function_call",
|
||||||
}),
|
function => ?FUNCTION_NAME,
|
||||||
|
request => Req
|
||||||
|
}),
|
||||||
|
|
||||||
{ok, response(call(Conn, {start_timer, keepalive, Interval})), Md};
|
{ok, response(call(Conn, {start_timer, keepalive, Interval})), Md};
|
||||||
start_timer(Req, Md) ->
|
start_timer(Req, Md) ->
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
?SLOG(debug, #{
|
||||||
, function => ?FUNCTION_NAME
|
msg => "recv_grpc_function_call",
|
||||||
, request => Req
|
function => ?FUNCTION_NAME,
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
|
|
||||||
{ok, response({error, ?RESP_PARAMS_TYPE_ERROR}), Md}.
|
{ok, response({error, ?RESP_PARAMS_TYPE_ERROR}), Md}.
|
||||||
|
|
||||||
-spec publish(emqx_exproto_pb:publish_request(), grpc:metadata())
|
-spec publish(emqx_exproto_pb:publish_request(), grpc:metadata()) ->
|
||||||
-> {ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
{ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
||||||
| {error, grpc_cowboy_h:error_response()}.
|
| {error, grpc_cowboy_h:error_response()}.
|
||||||
publish(Req = #{conn := Conn, topic := Topic, qos := Qos, payload := Payload}, Md)
|
publish(Req = #{conn := Conn, topic := Topic, qos := Qos, payload := Payload}, Md) when
|
||||||
when ?IS_QOS(Qos) ->
|
?IS_QOS(Qos)
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
->
|
||||||
, function => ?FUNCTION_NAME
|
?SLOG(debug, #{
|
||||||
, request => Req
|
msg => "recv_grpc_function_call",
|
||||||
}),
|
function => ?FUNCTION_NAME,
|
||||||
|
request => Req
|
||||||
|
}),
|
||||||
|
|
||||||
{ok, response(call(Conn, {publish, Topic, Qos, Payload})), Md};
|
{ok, response(call(Conn, {publish, Topic, Qos, Payload})), Md};
|
||||||
|
|
||||||
publish(Req, Md) ->
|
publish(Req, Md) ->
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
?SLOG(debug, #{
|
||||||
, function => ?FUNCTION_NAME
|
msg => "recv_grpc_function_call",
|
||||||
, request => Req
|
function => ?FUNCTION_NAME,
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
{ok, response({error, ?RESP_PARAMS_TYPE_ERROR}), Md}.
|
{ok, response({error, ?RESP_PARAMS_TYPE_ERROR}), Md}.
|
||||||
|
|
||||||
-spec subscribe(emqx_exproto_pb:subscribe_request(), grpc:metadata())
|
-spec subscribe(emqx_exproto_pb:subscribe_request(), grpc:metadata()) ->
|
||||||
-> {ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
{ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
||||||
| {error, grpc_cowboy_h:error_response()}.
|
| {error, grpc_cowboy_h:error_response()}.
|
||||||
subscribe(Req = #{conn := Conn, topic := Topic, qos := Qos}, Md)
|
subscribe(Req = #{conn := Conn, topic := Topic, qos := Qos}, Md) when
|
||||||
when ?IS_QOS(Qos) ->
|
?IS_QOS(Qos)
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
->
|
||||||
, function => ?FUNCTION_NAME
|
?SLOG(debug, #{
|
||||||
, request => Req
|
msg => "recv_grpc_function_call",
|
||||||
}),
|
function => ?FUNCTION_NAME,
|
||||||
|
request => Req
|
||||||
|
}),
|
||||||
{ok, response(call(Conn, {subscribe_from_client, Topic, Qos})), Md};
|
{ok, response(call(Conn, {subscribe_from_client, Topic, Qos})), Md};
|
||||||
|
|
||||||
subscribe(Req, Md) ->
|
subscribe(Req, Md) ->
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
?SLOG(debug, #{
|
||||||
, function => ?FUNCTION_NAME
|
msg => "recv_grpc_function_call",
|
||||||
, request => Req
|
function => ?FUNCTION_NAME,
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
{ok, response({error, ?RESP_PARAMS_TYPE_ERROR}), Md}.
|
{ok, response({error, ?RESP_PARAMS_TYPE_ERROR}), Md}.
|
||||||
|
|
||||||
-spec unsubscribe(emqx_exproto_pb:unsubscribe_request(), grpc:metadata())
|
-spec unsubscribe(emqx_exproto_pb:unsubscribe_request(), grpc:metadata()) ->
|
||||||
-> {ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
{ok, emqx_exproto_pb:code_response(), grpc:metadata()}
|
||||||
| {error, grpc_cowboy_h:error_response()}.
|
| {error, grpc_cowboy_h:error_response()}.
|
||||||
unsubscribe(Req = #{conn := Conn, topic := Topic}, Md) ->
|
unsubscribe(Req = #{conn := Conn, topic := Topic}, Md) ->
|
||||||
?SLOG(debug, #{ msg => "recv_grpc_function_call"
|
?SLOG(debug, #{
|
||||||
, function => ?FUNCTION_NAME
|
msg => "recv_grpc_function_call",
|
||||||
, request => Req
|
function => ?FUNCTION_NAME,
|
||||||
}),
|
request => Req
|
||||||
|
}),
|
||||||
{ok, response(call(Conn, {unsubscribe_from_client, Topic})), Md}.
|
{ok, response(call(Conn, {unsubscribe_from_client, Topic})), Md}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -155,20 +172,20 @@ call(ConnStr, Req) ->
|
||||||
Pid = to_pid(ConnStr),
|
Pid = to_pid(ConnStr),
|
||||||
emqx_gateway_conn:call(Pid, Req, ?DEFAULT_CALL_TIMEOUT)
|
emqx_gateway_conn:call(Pid, Req, ?DEFAULT_CALL_TIMEOUT)
|
||||||
catch
|
catch
|
||||||
exit : badarg ->
|
exit:badarg ->
|
||||||
{error, ?RESP_PARAMS_TYPE_ERROR, <<"The conn type error">>};
|
{error, ?RESP_PARAMS_TYPE_ERROR, <<"The conn type error">>};
|
||||||
exit : noproc ->
|
exit:noproc ->
|
||||||
{error, ?RESP_CONN_PROCESS_NOT_ALIVE,
|
{error, ?RESP_CONN_PROCESS_NOT_ALIVE, <<"Connection process is not alive">>};
|
||||||
<<"Connection process is not alive">>};
|
exit:timeout ->
|
||||||
exit : timeout ->
|
|
||||||
{error, ?RESP_UNKNOWN, <<"Connection is not answered">>};
|
{error, ?RESP_UNKNOWN, <<"Connection is not answered">>};
|
||||||
Class : Reason : Stk->
|
Class:Reason:Stk ->
|
||||||
?SLOG(error, #{ msg => "call_conn_process_crashed"
|
?SLOG(error, #{
|
||||||
, request => Req
|
msg => "call_conn_process_crashed",
|
||||||
, conn_str=> ConnStr
|
request => Req,
|
||||||
, reason => {Class, Reason}
|
conn_str => ConnStr,
|
||||||
, stacktrace => Stk
|
reason => {Class, Reason},
|
||||||
}),
|
stacktrace => Stk
|
||||||
|
}),
|
||||||
{error, ?RESP_UNKNOWN, <<"Unknown crashes">>}
|
{error, ?RESP_UNKNOWN, <<"Unknown crashes">>}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -184,11 +201,13 @@ validate(clientinfo, M) ->
|
||||||
|
|
||||||
response(ok) ->
|
response(ok) ->
|
||||||
#{code => ?RESP_SUCCESS};
|
#{code => ?RESP_SUCCESS};
|
||||||
response({error, Code, Reason})
|
response({error, Code, Reason}) when
|
||||||
when ?IS_GRPC_RESULT_CODE(Code) ->
|
?IS_GRPC_RESULT_CODE(Code)
|
||||||
|
->
|
||||||
#{code => Code, message => stringfy(Reason)};
|
#{code => Code, message => stringfy(Reason)};
|
||||||
response({error, Code})
|
response({error, Code}) when
|
||||||
when ?IS_GRPC_RESULT_CODE(Code) ->
|
?IS_GRPC_RESULT_CODE(Code)
|
||||||
|
->
|
||||||
#{code => Code};
|
#{code => Code};
|
||||||
response(Other) ->
|
response(Other) ->
|
||||||
#{code => ?RESP_UNKNOWN, message => stringfy(Other)}.
|
#{code => ?RESP_UNKNOWN, message => stringfy(Other)}.
|
||||||
|
|
|
||||||
|
|
@ -21,29 +21,33 @@
|
||||||
|
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
-import(emqx_gateway_utils,
|
-import(
|
||||||
[ normalize_config/1
|
emqx_gateway_utils,
|
||||||
, start_listeners/4
|
[
|
||||||
, stop_listeners/2
|
normalize_config/1,
|
||||||
]).
|
start_listeners/4,
|
||||||
|
stop_listeners/2
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([ reg/0
|
-export([
|
||||||
, unreg/0
|
reg/0,
|
||||||
]).
|
unreg/0
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ on_gateway_load/2
|
-export([
|
||||||
, on_gateway_update/3
|
on_gateway_load/2,
|
||||||
, on_gateway_unload/2
|
on_gateway_update/3,
|
||||||
]).
|
on_gateway_unload/2
|
||||||
|
]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% APIs
|
%% APIs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
reg() ->
|
reg() ->
|
||||||
RegistryOptions = [ {cbkmod, ?MODULE}
|
RegistryOptions = [{cbkmod, ?MODULE}],
|
||||||
],
|
|
||||||
emqx_gateway_registry:reg(exproto, RegistryOptions).
|
emqx_gateway_registry:reg(exproto, RegistryOptions).
|
||||||
|
|
||||||
unreg() ->
|
unreg() ->
|
||||||
|
|
@ -53,13 +57,18 @@ unreg() ->
|
||||||
%% emqx_gateway_registry callbacks
|
%% emqx_gateway_registry callbacks
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
on_gateway_load(_Gateway = #{ name := GwName,
|
on_gateway_load(
|
||||||
config := Config
|
_Gateway = #{
|
||||||
}, Ctx) ->
|
name := GwName,
|
||||||
|
config := Config
|
||||||
|
},
|
||||||
|
Ctx
|
||||||
|
) ->
|
||||||
%% XXX: How to monitor it ?
|
%% XXX: How to monitor it ?
|
||||||
_ = start_grpc_client_channel(GwName,
|
_ = start_grpc_client_channel(
|
||||||
maps:get(handler, Config, undefined)
|
GwName,
|
||||||
),
|
maps:get(handler, Config, undefined)
|
||||||
|
),
|
||||||
%% XXX: How to monitor it ?
|
%% XXX: How to monitor it ?
|
||||||
_ = start_grpc_server(GwName, maps:get(server, Config, undefined)),
|
_ = start_grpc_server(GwName, maps:get(server, Config, undefined)),
|
||||||
|
|
||||||
|
|
@ -67,29 +76,39 @@ on_gateway_load(_Gateway = #{ name := GwName,
|
||||||
PoolName = pool_name(GwName),
|
PoolName = pool_name(GwName),
|
||||||
PoolSize = emqx_vm:schedulers() * 2,
|
PoolSize = emqx_vm:schedulers() * 2,
|
||||||
{ok, PoolSup} = emqx_pool_sup:start_link(
|
{ok, PoolSup} = emqx_pool_sup:start_link(
|
||||||
PoolName, hash, PoolSize,
|
PoolName,
|
||||||
{emqx_exproto_gcli, start_link, []}),
|
hash,
|
||||||
|
PoolSize,
|
||||||
|
{emqx_exproto_gcli, start_link, []}
|
||||||
|
),
|
||||||
|
|
||||||
NConfig = maps:without(
|
NConfig = maps:without(
|
||||||
[server, handler],
|
[server, handler],
|
||||||
Config#{pool_name => PoolName}
|
Config#{pool_name => PoolName}
|
||||||
),
|
),
|
||||||
Listeners = emqx_gateway_utils:normalize_config(
|
Listeners = emqx_gateway_utils:normalize_config(
|
||||||
NConfig#{handler => GwName}
|
NConfig#{handler => GwName}
|
||||||
),
|
),
|
||||||
|
|
||||||
ModCfg = #{frame_mod => emqx_exproto_frame,
|
ModCfg = #{
|
||||||
chann_mod => emqx_exproto_channel
|
frame_mod => emqx_exproto_frame,
|
||||||
},
|
chann_mod => emqx_exproto_channel
|
||||||
case start_listeners(
|
},
|
||||||
Listeners, GwName, Ctx, ModCfg) of
|
case
|
||||||
|
start_listeners(
|
||||||
|
Listeners, GwName, Ctx, ModCfg
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, ListenerPids} ->
|
{ok, ListenerPids} ->
|
||||||
{ok, ListenerPids, _GwState = #{ctx => Ctx, pool => PoolSup}};
|
{ok, ListenerPids, _GwState = #{ctx => Ctx, pool => PoolSup}};
|
||||||
{error, {Reason, Listener}} ->
|
{error, {Reason, Listener}} ->
|
||||||
throw({badconf, #{ key => listeners
|
throw(
|
||||||
, vallue => Listener
|
{badconf, #{
|
||||||
, reason => Reason
|
key => listeners,
|
||||||
}})
|
vallue => Listener,
|
||||||
|
reason => Reason
|
||||||
|
}}
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
||||||
|
|
@ -100,16 +119,22 @@ on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
||||||
on_gateway_unload(Gateway, GwState),
|
on_gateway_unload(Gateway, GwState),
|
||||||
on_gateway_load(Gateway#{config => Config}, Ctx)
|
on_gateway_load(Gateway#{config => Config}, Ctx)
|
||||||
catch
|
catch
|
||||||
Class : Reason : Stk ->
|
Class:Reason:Stk ->
|
||||||
logger:error("Failed to update ~ts; "
|
logger:error(
|
||||||
"reason: {~0p, ~0p} stacktrace: ~0p",
|
"Failed to update ~ts; "
|
||||||
[GwName, Class, Reason, Stk]),
|
"reason: {~0p, ~0p} stacktrace: ~0p",
|
||||||
|
[GwName, Class, Reason, Stk]
|
||||||
|
),
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
on_gateway_unload(_Gateway = #{ name := GwName,
|
on_gateway_unload(
|
||||||
config := Config
|
_Gateway = #{
|
||||||
}, _GwState = #{pool := PoolSup}) ->
|
name := GwName,
|
||||||
|
config := Config
|
||||||
|
},
|
||||||
|
_GwState = #{pool := PoolSup}
|
||||||
|
) ->
|
||||||
Listeners = emqx_gateway_utils:normalize_config(Config),
|
Listeners = emqx_gateway_utils:normalize_config(Config),
|
||||||
%% Stop funcs???
|
%% Stop funcs???
|
||||||
exit(PoolSup, kill),
|
exit(PoolSup, kill),
|
||||||
|
|
@ -124,29 +149,42 @@ on_gateway_unload(_Gateway = #{ name := GwName,
|
||||||
start_grpc_server(_GwName, undefined) ->
|
start_grpc_server(_GwName, undefined) ->
|
||||||
undefined;
|
undefined;
|
||||||
start_grpc_server(GwName, Options = #{bind := ListenOn}) ->
|
start_grpc_server(GwName, Options = #{bind := ListenOn}) ->
|
||||||
Services = #{protos => [emqx_exproto_pb],
|
Services = #{
|
||||||
services => #{
|
protos => [emqx_exproto_pb],
|
||||||
'emqx.exproto.v1.ConnectionAdapter' => emqx_exproto_gsvr}
|
services => #{
|
||||||
},
|
'emqx.exproto.v1.ConnectionAdapter' => emqx_exproto_gsvr
|
||||||
SvrOptions = case emqx_map_lib:deep_get([ssl, enable], Options, false) of
|
}
|
||||||
false -> [];
|
},
|
||||||
true ->
|
SvrOptions =
|
||||||
[{ssl_options,
|
case emqx_map_lib:deep_get([ssl, enable], Options, false) of
|
||||||
|
false ->
|
||||||
|
[];
|
||||||
|
true ->
|
||||||
|
[
|
||||||
|
{ssl_options,
|
||||||
maps:to_list(
|
maps:to_list(
|
||||||
maps:without([enable], maps:get(ssl, Options, #{}))
|
maps:without([enable], maps:get(ssl, Options, #{}))
|
||||||
)
|
)}
|
||||||
}]
|
]
|
||||||
end,
|
end,
|
||||||
case grpc:start_server(GwName, ListenOn, Services, SvrOptions) of
|
case grpc:start_server(GwName, ListenOn, Services, SvrOptions) of
|
||||||
{ok, _SvrPid} ->
|
{ok, _SvrPid} ->
|
||||||
console_print("Start ~ts gRPC server on ~p successfully.~n",
|
console_print(
|
||||||
[GwName, ListenOn]);
|
"Start ~ts gRPC server on ~p successfully.~n",
|
||||||
|
[GwName, ListenOn]
|
||||||
|
);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?ELOG("Failed to start ~ts gRPC server on ~p, reason: ~0p",
|
?ELOG(
|
||||||
[GwName, ListenOn, Reason]),
|
"Failed to start ~ts gRPC server on ~p, reason: ~0p",
|
||||||
throw({badconf, #{key => server,
|
[GwName, ListenOn, Reason]
|
||||||
value => Options,
|
),
|
||||||
reason => illegal_grpc_server_confs}})
|
throw(
|
||||||
|
{badconf, #{
|
||||||
|
key => server,
|
||||||
|
value => Options,
|
||||||
|
reason => illegal_grpc_server_confs
|
||||||
|
}}
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
stop_grpc_server(GwName) ->
|
stop_grpc_server(GwName) ->
|
||||||
|
|
@ -158,13 +196,16 @@ start_grpc_client_channel(_GwName, undefined) ->
|
||||||
start_grpc_client_channel(GwName, Options = #{address := Address}) ->
|
start_grpc_client_channel(GwName, Options = #{address := Address}) ->
|
||||||
#{host := Host, port := Port} =
|
#{host := Host, port := Port} =
|
||||||
case emqx_http_lib:uri_parse(Address) of
|
case emqx_http_lib:uri_parse(Address) of
|
||||||
{ok, URIMap0} -> URIMap0;
|
{ok, URIMap0} ->
|
||||||
|
URIMap0;
|
||||||
{error, _Reason} ->
|
{error, _Reason} ->
|
||||||
throw({badconf, #{key => address,
|
throw(
|
||||||
value => Address,
|
{badconf, #{
|
||||||
reason => illegal_grpc_address
|
key => address,
|
||||||
}})
|
value => Address,
|
||||||
|
reason => illegal_grpc_address
|
||||||
|
}}
|
||||||
|
)
|
||||||
end,
|
end,
|
||||||
case emqx_map_lib:deep_get([ssl, enable], Options, false) of
|
case emqx_map_lib:deep_get([ssl, enable], Options, false) of
|
||||||
false ->
|
false ->
|
||||||
|
|
@ -172,9 +213,13 @@ start_grpc_client_channel(GwName, Options = #{address := Address}) ->
|
||||||
grpc_client_sup:create_channel_pool(GwName, SvrAddr, #{});
|
grpc_client_sup:create_channel_pool(GwName, SvrAddr, #{});
|
||||||
true ->
|
true ->
|
||||||
SslOpts = maps:to_list(maps:get(ssl, Options, #{})),
|
SslOpts = maps:to_list(maps:get(ssl, Options, #{})),
|
||||||
ClientOpts = #{gun_opts =>
|
ClientOpts = #{
|
||||||
#{transport => ssl,
|
gun_opts =>
|
||||||
transport_opts => SslOpts}},
|
#{
|
||||||
|
transport => ssl,
|
||||||
|
transport_opts => SslOpts
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
SvrAddr = compose_http_uri(https, Host, Port),
|
SvrAddr = compose_http_uri(https, Host, Port),
|
||||||
grpc_client_sup:create_channel_pool(GwName, SvrAddr, ClientOpts)
|
grpc_client_sup:create_channel_pool(GwName, SvrAddr, ClientOpts)
|
||||||
|
|
@ -182,8 +227,10 @@ start_grpc_client_channel(GwName, Options = #{address := Address}) ->
|
||||||
|
|
||||||
compose_http_uri(Scheme, Host, Port) ->
|
compose_http_uri(Scheme, Host, Port) ->
|
||||||
lists:flatten(
|
lists:flatten(
|
||||||
io_lib:format(
|
io_lib:format(
|
||||||
"~s://~s:~w", [Scheme, inet:ntoa(Host), Port])).
|
"~s://~s:~w", [Scheme, inet:ntoa(Host), Port]
|
||||||
|
)
|
||||||
|
).
|
||||||
|
|
||||||
stop_grpc_client_channel(GwName) ->
|
stop_grpc_client_channel(GwName) ->
|
||||||
_ = grpc_client_sup:stop_channel_pool(GwName),
|
_ = grpc_client_sup:stop_channel_pool(GwName),
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,13 @@
|
||||||
|
|
||||||
-define(APP, emqx_exproto).
|
-define(APP, emqx_exproto).
|
||||||
|
|
||||||
-define(TCP_SOCKOPTS, [binary, {packet, raw}, {reuseaddr, true},
|
-define(TCP_SOCKOPTS, [
|
||||||
{backlog, 512}, {nodelay, true}]).
|
binary,
|
||||||
|
{packet, raw},
|
||||||
|
{reuseaddr, true},
|
||||||
|
{backlog, 512},
|
||||||
|
{nodelay, true}
|
||||||
|
]).
|
||||||
|
|
||||||
-define(UDP_SOCKOPTS, []).
|
-define(UDP_SOCKOPTS, []).
|
||||||
|
|
||||||
|
|
@ -30,7 +35,9 @@
|
||||||
-define(RESP_PARAMS_TYPE_ERROR, 'PARAMS_TYPE_ERROR').
|
-define(RESP_PARAMS_TYPE_ERROR, 'PARAMS_TYPE_ERROR').
|
||||||
-define(RESP_REQUIRED_PARAMS_MISSED, 'REQUIRED_PARAMS_MISSED').
|
-define(RESP_REQUIRED_PARAMS_MISSED, 'REQUIRED_PARAMS_MISSED').
|
||||||
-define(RESP_PERMISSION_DENY, 'PERMISSION_DENY').
|
-define(RESP_PERMISSION_DENY, 'PERMISSION_DENY').
|
||||||
-define(IS_GRPC_RESULT_CODE(C), ( C =:= ?RESP_SUCCESS
|
-define(IS_GRPC_RESULT_CODE(C),
|
||||||
orelse C =:= ?RESP_CONN_PROCESS_NOT_ALIVE
|
(C =:= ?RESP_SUCCESS orelse
|
||||||
orelse C =:= ?RESP_REQUIRED_PARAMS_MISSED
|
C =:= ?RESP_CONN_PROCESS_NOT_ALIVE orelse
|
||||||
orelse C =:= ?RESP_PERMISSION_DENY)).
|
C =:= ?RESP_REQUIRED_PARAMS_MISSED orelse
|
||||||
|
C =:= ?RESP_PERMISSION_DENY)
|
||||||
|
).
|
||||||
|
|
|
||||||
|
|
@ -3,58 +3,57 @@
|
||||||
%% copied from https://github.com/arcusfelis/binary2
|
%% copied from https://github.com/arcusfelis/binary2
|
||||||
|
|
||||||
%% Bytes
|
%% Bytes
|
||||||
-export([ reverse/1
|
-export([
|
||||||
, join/2
|
reverse/1,
|
||||||
, duplicate/2
|
join/2,
|
||||||
, suffix/2
|
duplicate/2,
|
||||||
, prefix/2
|
suffix/2,
|
||||||
]).
|
prefix/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% Bits
|
%% Bits
|
||||||
-export([ union/2
|
-export([
|
||||||
, subtract/2
|
union/2,
|
||||||
, intersection/2
|
subtract/2,
|
||||||
, inverse/1
|
intersection/2,
|
||||||
]).
|
inverse/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% Trimming
|
%% Trimming
|
||||||
-export([ rtrim/1
|
-export([
|
||||||
, rtrim/2
|
rtrim/1,
|
||||||
, ltrim/1
|
rtrim/2,
|
||||||
, ltrim/2
|
ltrim/1,
|
||||||
, trim/1
|
ltrim/2,
|
||||||
, trim/2
|
trim/1,
|
||||||
]).
|
trim/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% Parsing
|
%% Parsing
|
||||||
-export([ bin_to_int/1]).
|
-export([bin_to_int/1]).
|
||||||
|
|
||||||
%% Matching
|
%% Matching
|
||||||
-export([ optimize_patterns/1]).
|
-export([optimize_patterns/1]).
|
||||||
|
|
||||||
%% CoAP
|
%% CoAP
|
||||||
-export([ join_path/1]).
|
-export([join_path/1]).
|
||||||
|
|
||||||
|
trim(B) -> trim(B, 0).
|
||||||
trim(B) -> trim(B, 0).
|
|
||||||
ltrim(B) -> ltrim(B, 0).
|
ltrim(B) -> ltrim(B, 0).
|
||||||
rtrim(B) -> rtrim(B, 0).
|
rtrim(B) -> rtrim(B, 0).
|
||||||
|
|
||||||
|
|
||||||
rtrim(B, X) when is_binary(B), is_integer(X) ->
|
rtrim(B, X) when is_binary(B), is_integer(X) ->
|
||||||
S = byte_size(B),
|
S = byte_size(B),
|
||||||
do_rtrim(S, B, X);
|
do_rtrim(S, B, X);
|
||||||
rtrim(B, [_|_]=Xs) when is_binary(B) ->
|
rtrim(B, [_ | _] = Xs) when is_binary(B) ->
|
||||||
S = byte_size(B),
|
S = byte_size(B),
|
||||||
do_mrtrim(S, B, Xs).
|
do_mrtrim(S, B, Xs).
|
||||||
|
|
||||||
|
|
||||||
ltrim(B, X) when is_binary(B), is_integer(X) ->
|
ltrim(B, X) when is_binary(B), is_integer(X) ->
|
||||||
do_ltrim(B, X);
|
do_ltrim(B, X);
|
||||||
ltrim(B, [_|_]=Xs) when is_binary(B) ->
|
ltrim(B, [_ | _] = Xs) when is_binary(B) ->
|
||||||
do_mltrim(B, Xs).
|
do_mltrim(B, Xs).
|
||||||
|
|
||||||
|
|
||||||
%% @doc The second element is a single integer element or an ordset of elements.
|
%% @doc The second element is a single integer element or an ordset of elements.
|
||||||
trim(B, X) when is_binary(B), is_integer(X) ->
|
trim(B, X) when is_binary(B), is_integer(X) ->
|
||||||
From = ltrimc(B, X, 0),
|
From = ltrimc(B, X, 0),
|
||||||
|
|
@ -65,7 +64,7 @@ trim(B, X) when is_binary(B), is_integer(X) ->
|
||||||
To = do_rtrimc(S, B, X),
|
To = do_rtrimc(S, B, X),
|
||||||
binary:part(B, From, To - From)
|
binary:part(B, From, To - From)
|
||||||
end;
|
end;
|
||||||
trim(B, [_|_]=Xs) when is_binary(B) ->
|
trim(B, [_ | _] = Xs) when is_binary(B) ->
|
||||||
From = mltrimc(B, Xs, 0),
|
From = mltrimc(B, Xs, 0),
|
||||||
case byte_size(B) of
|
case byte_size(B) of
|
||||||
From ->
|
From ->
|
||||||
|
|
@ -83,7 +82,7 @@ do_ltrim(B, _X) ->
|
||||||
%% multi, left trimming.
|
%% multi, left trimming.
|
||||||
do_mltrim(<<X, B/binary>> = XB, Xs) ->
|
do_mltrim(<<X, B/binary>> = XB, Xs) ->
|
||||||
case ordsets:is_element(X, Xs) of
|
case ordsets:is_element(X, Xs) of
|
||||||
true -> do_mltrim(B, Xs);
|
true -> do_mltrim(B, Xs);
|
||||||
false -> XB
|
false -> XB
|
||||||
end;
|
end;
|
||||||
do_mltrim(<<>>, _Xs) ->
|
do_mltrim(<<>>, _Xs) ->
|
||||||
|
|
@ -105,20 +104,19 @@ do_mrtrim(S, B, Xs) ->
|
||||||
S2 = S - 1,
|
S2 = S - 1,
|
||||||
X = binary:at(B, S2),
|
X = binary:at(B, S2),
|
||||||
case ordsets:is_element(X, Xs) of
|
case ordsets:is_element(X, Xs) of
|
||||||
true -> do_mrtrim(S2, B, Xs);
|
true -> do_mrtrim(S2, B, Xs);
|
||||||
false -> binary_part(B, 0, S)
|
false -> binary_part(B, 0, S)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
ltrimc(<<X, B/binary>>, X, C) ->
|
ltrimc(<<X, B/binary>>, X, C) ->
|
||||||
ltrimc(B, X, C+1);
|
ltrimc(B, X, C + 1);
|
||||||
ltrimc(_B, _X, C) ->
|
ltrimc(_B, _X, C) ->
|
||||||
C.
|
C.
|
||||||
|
|
||||||
%% multi, left trimming, returns a count of matched bytes from the left.
|
%% multi, left trimming, returns a count of matched bytes from the left.
|
||||||
mltrimc(<<X, B/binary>>, Xs, C) ->
|
mltrimc(<<X, B/binary>>, Xs, C) ->
|
||||||
case ordsets:is_element(X, Xs) of
|
case ordsets:is_element(X, Xs) of
|
||||||
true -> mltrimc(B, Xs, C+1);
|
true -> mltrimc(B, Xs, C + 1);
|
||||||
false -> C
|
false -> C
|
||||||
end;
|
end;
|
||||||
mltrimc(<<>>, _Xs, C) ->
|
mltrimc(<<>>, _Xs, C) ->
|
||||||
|
|
@ -138,7 +136,7 @@ do_mrtrimc(S, B, Xs) ->
|
||||||
S2 = S - 1,
|
S2 = S - 1,
|
||||||
X = binary:at(B, S2),
|
X = binary:at(B, S2),
|
||||||
case ordsets:is_element(X, Xs) of
|
case ordsets:is_element(X, Xs) of
|
||||||
true -> do_mrtrimc(S2, B, Xs);
|
true -> do_mrtrimc(S2, B, Xs);
|
||||||
false -> S
|
false -> S
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -148,13 +146,12 @@ reverse(Bin) when is_binary(Bin) ->
|
||||||
<<V:S/integer-little>> = Bin,
|
<<V:S/integer-little>> = Bin,
|
||||||
<<V:S/integer-big>>.
|
<<V:S/integer-big>>.
|
||||||
|
|
||||||
join([B|Bs], Sep) when is_binary(Sep) ->
|
join([B | Bs], Sep) when is_binary(Sep) ->
|
||||||
iolist_to_binary([B|add_separator(Bs, Sep)]);
|
iolist_to_binary([B | add_separator(Bs, Sep)]);
|
||||||
|
|
||||||
join([], _Sep) ->
|
join([], _Sep) ->
|
||||||
<<>>.
|
<<>>.
|
||||||
|
|
||||||
add_separator([B|Bs], Sep) ->
|
add_separator([B | Bs], Sep) ->
|
||||||
[Sep, B | add_separator(Bs, Sep)];
|
[Sep, B | add_separator(Bs, Sep)];
|
||||||
add_separator([], _) ->
|
add_separator([], _) ->
|
||||||
[].
|
[].
|
||||||
|
|
@ -168,8 +165,7 @@ prefix(B, L) when is_binary(B), is_integer(L), L > 0 ->
|
||||||
|
|
||||||
suffix(B, L) when is_binary(B), is_integer(L), L > 0 ->
|
suffix(B, L) when is_binary(B), is_integer(L), L > 0 ->
|
||||||
S = byte_size(B),
|
S = byte_size(B),
|
||||||
binary:part(B, S-L, L).
|
binary:part(B, S - L, L).
|
||||||
|
|
||||||
|
|
||||||
union(B1, B2) ->
|
union(B1, B2) ->
|
||||||
S = bit_size(B1),
|
S = bit_size(B1),
|
||||||
|
|
@ -203,7 +199,7 @@ bin_to_int(Bin) ->
|
||||||
bin_to_int(Bin, 0).
|
bin_to_int(Bin, 0).
|
||||||
|
|
||||||
bin_to_int(<<H, T/binary>>, X) when $0 =< H, H =< $9 ->
|
bin_to_int(<<H, T/binary>>, X) when $0 =< H, H =< $9 ->
|
||||||
bin_to_int(T, (X*10)+(H-$0));
|
bin_to_int(T, (X * 10) + (H - $0));
|
||||||
bin_to_int(Bin, X) ->
|
bin_to_int(Bin, X) ->
|
||||||
{X, Bin}.
|
{X, Bin}.
|
||||||
|
|
||||||
|
|
@ -213,10 +209,10 @@ optimize_patterns(Patterns) ->
|
||||||
Sorted = lists:usort(Patterns),
|
Sorted = lists:usort(Patterns),
|
||||||
remove_long_duplicates(Sorted).
|
remove_long_duplicates(Sorted).
|
||||||
|
|
||||||
remove_long_duplicates([H|T]) ->
|
remove_long_duplicates([H | T]) ->
|
||||||
%% match(Subject, Pattern)
|
%% match(Subject, Pattern)
|
||||||
DedupT = [X || X <- T, binary:match(X, H) =:= nomatch],
|
DedupT = [X || X <- T, binary:match(X, H) =:= nomatch],
|
||||||
[H|remove_long_duplicates(DedupT)];
|
[H | remove_long_duplicates(DedupT)];
|
||||||
remove_long_duplicates([]) ->
|
remove_long_duplicates([]) ->
|
||||||
[].
|
[].
|
||||||
|
|
||||||
|
|
@ -224,7 +220,5 @@ join_path(PathList) ->
|
||||||
join_path(PathList, <<>>).
|
join_path(PathList, <<>>).
|
||||||
|
|
||||||
join_path([], Result) -> Result;
|
join_path([], Result) -> Result;
|
||||||
join_path([<<>> | PathList], Result) ->
|
join_path([<<>> | PathList], Result) -> join_path(PathList, Result);
|
||||||
join_path(PathList, Result);
|
join_path([Path | PathList], Result) -> join_path(PathList, <<Result/binary, "/", Path/binary>>).
|
||||||
join_path([Path | PathList], Result) ->
|
|
||||||
join_path(PathList, <<Result/binary, "/", Path/binary>>).
|
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
-export([lookup_cmd/2, observe/2, read/2, write/2]).
|
-export([lookup_cmd/2, observe/2, read/2, write/2]).
|
||||||
|
|
||||||
-define(PATH(Suffix), "/gateway/lwm2m/clients/:clientid"Suffix).
|
-define(PATH(Suffix), "/gateway/lwm2m/clients/:clientid" Suffix).
|
||||||
-define(DATA_TYPE, ['Integer', 'Float', 'Time', 'String', 'Boolean', 'Opaque', 'Objlnk']).
|
-define(DATA_TYPE, ['Integer', 'Float', 'Time', 'String', 'Boolean', 'Opaque', 'Objlnk']).
|
||||||
|
|
||||||
-import(hoconsc, [mk/2, ref/1, ref/2]).
|
-import(hoconsc, [mk/2, ref/1, ref/2]).
|
||||||
|
|
@ -104,8 +104,11 @@ schema(?PATH("/write")) ->
|
||||||
parameters => [
|
parameters => [
|
||||||
{clientid, mk(binary(), #{in => path, example => "urn:oma:lwm2m:oma:2"})},
|
{clientid, mk(binary(), #{in => path, example => "urn:oma:lwm2m:oma:2"})},
|
||||||
{path, mk(binary(), #{in => query, required => true, example => "/3/0/7"})},
|
{path, mk(binary(), #{in => query, required => true, example => "/3/0/7"})},
|
||||||
{type, mk(hoconsc:enum(?DATA_TYPE),
|
{type,
|
||||||
#{in => query, required => true, example => 'Integer'})},
|
mk(
|
||||||
|
hoconsc:enum(?DATA_TYPE),
|
||||||
|
#{in => query, required => true, example => 'Integer'}
|
||||||
|
)},
|
||||||
{value, mk(binary(), #{in => query, required => true, example => 123})}
|
{value, mk(binary(), #{in => query, required => true, example => 123})}
|
||||||
],
|
],
|
||||||
responses => #{
|
responses => #{
|
||||||
|
|
@ -118,18 +121,23 @@ schema(?PATH("/write")) ->
|
||||||
fields(resource) ->
|
fields(resource) ->
|
||||||
[
|
[
|
||||||
{operations, mk(binary(), #{desc => <<"Resource Operations">>, example => "E"})},
|
{operations, mk(binary(), #{desc => <<"Resource Operations">>, example => "E"})},
|
||||||
{'dataType', mk(hoconsc:enum(?DATA_TYPE), #{desc => <<"Data Type">>,
|
{'dataType',
|
||||||
example => 'Integer'})},
|
mk(hoconsc:enum(?DATA_TYPE), #{
|
||||||
{path, mk(binary(), #{desc => <<"Resource Path">>, example => "urn:oma:lwm2m:oma:2"})},
|
desc => <<"Data Type">>,
|
||||||
{name, mk(binary(), #{desc => <<"Resource Name">>, example => "lwm2m-test"})}
|
example => 'Integer'
|
||||||
|
})},
|
||||||
|
{path, mk(binary(), #{desc => <<"Resource Path">>, example => "urn:oma:lwm2m:oma:2"})},
|
||||||
|
{name, mk(binary(), #{desc => <<"Resource Name">>, example => "lwm2m-test"})}
|
||||||
].
|
].
|
||||||
|
|
||||||
lookup_cmd(get, #{bindings := Bindings, query_string := QS}) ->
|
lookup_cmd(get, #{bindings := Bindings, query_string := QS}) ->
|
||||||
ClientId = maps:get(clientid, Bindings),
|
ClientId = maps:get(clientid, Bindings),
|
||||||
case emqx_gateway_cm_registry:lookup_channels(lwm2m, ClientId) of
|
case emqx_gateway_cm_registry:lookup_channels(lwm2m, ClientId) of
|
||||||
[Channel | _] ->
|
[Channel | _] ->
|
||||||
#{<<"path">> := Path,
|
#{
|
||||||
<<"action">> := Action} = QS,
|
<<"path">> := Path,
|
||||||
|
<<"action">> := Action
|
||||||
|
} = QS,
|
||||||
{ok, Result} = emqx_lwm2m_channel:lookup_cmd(Channel, Path, Action),
|
{ok, Result} = emqx_lwm2m_channel:lookup_cmd(Channel, Path, Action),
|
||||||
lookup_cmd_return(Result, ClientId, Action, Path);
|
lookup_cmd_return(Result, ClientId, Action, Path);
|
||||||
_ ->
|
_ ->
|
||||||
|
|
@ -137,63 +145,73 @@ lookup_cmd(get, #{bindings := Bindings, query_string := QS}) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
lookup_cmd_return(undefined, ClientId, Action, Path) ->
|
lookup_cmd_return(undefined, ClientId, Action, Path) ->
|
||||||
{200,
|
{200, #{
|
||||||
#{clientid => ClientId,
|
clientid => ClientId,
|
||||||
action => Action,
|
action => Action,
|
||||||
code => <<"6.01">>,
|
code => <<"6.01">>,
|
||||||
codeMsg => <<"reply_not_received">>,
|
codeMsg => <<"reply_not_received">>,
|
||||||
path => Path}};
|
path => Path
|
||||||
|
}};
|
||||||
lookup_cmd_return({Code, CodeMsg, Content}, ClientId, Action, Path) ->
|
lookup_cmd_return({Code, CodeMsg, Content}, ClientId, Action, Path) ->
|
||||||
{200,
|
{200,
|
||||||
format_cmd_content(Content,
|
format_cmd_content(
|
||||||
Action,
|
Content,
|
||||||
#{clientid => ClientId,
|
Action,
|
||||||
action => Action,
|
#{
|
||||||
code => Code,
|
clientid => ClientId,
|
||||||
codeMsg => CodeMsg,
|
action => Action,
|
||||||
path => Path})}.
|
code => Code,
|
||||||
|
codeMsg => CodeMsg,
|
||||||
|
path => Path
|
||||||
|
}
|
||||||
|
)}.
|
||||||
|
|
||||||
format_cmd_content(undefined, _MsgType, Result) ->
|
format_cmd_content(undefined, _MsgType, Result) ->
|
||||||
Result;
|
Result;
|
||||||
|
|
||||||
format_cmd_content(Content, <<"discover">>, Result) ->
|
format_cmd_content(Content, <<"discover">>, Result) ->
|
||||||
[H | Content1] = Content,
|
[H | Content1] = Content,
|
||||||
{_, [HObjId]} = emqx_lwm2m_session:parse_object_list(H),
|
{_, [HObjId]} = emqx_lwm2m_session:parse_object_list(H),
|
||||||
[ObjId | _]= path_list(HObjId),
|
[ObjId | _] = path_list(HObjId),
|
||||||
ObjectList =
|
ObjectList =
|
||||||
case Content1 of
|
case Content1 of
|
||||||
[Content2 | _] ->
|
[Content2 | _] ->
|
||||||
{_, ObjL} = emqx_lwm2m_session:parse_object_list(Content2),
|
{_, ObjL} = emqx_lwm2m_session:parse_object_list(Content2),
|
||||||
ObjL;
|
ObjL;
|
||||||
[] -> []
|
[] ->
|
||||||
|
[]
|
||||||
end,
|
end,
|
||||||
|
|
||||||
R = case emqx_lwm2m_xml_object:get_obj_def(binary_to_integer(ObjId), true) of
|
R =
|
||||||
|
case emqx_lwm2m_xml_object:get_obj_def(binary_to_integer(ObjId), true) of
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
lists:map(fun(Object) -> #{Object => Object} end, ObjectList);
|
lists:map(fun(Object) -> #{Object => Object} end, ObjectList);
|
||||||
ObjDefinition ->
|
ObjDefinition ->
|
||||||
lists:map(fun(Obj) -> to_operations(Obj, ObjDefinition) end, ObjectList)
|
lists:map(fun(Obj) -> to_operations(Obj, ObjDefinition) end, ObjectList)
|
||||||
end,
|
end,
|
||||||
Result#{content => R};
|
Result#{content => R};
|
||||||
|
|
||||||
format_cmd_content(Content, _, Result) ->
|
format_cmd_content(Content, _, Result) ->
|
||||||
Result#{content => Content}.
|
Result#{content => Content}.
|
||||||
|
|
||||||
to_operations(Obj, ObjDefinition) ->
|
to_operations(Obj, ObjDefinition) ->
|
||||||
[_, _, RawResId| _] = path_list(Obj),
|
[_, _, RawResId | _] = path_list(Obj),
|
||||||
ResId = binary_to_integer(RawResId),
|
ResId = binary_to_integer(RawResId),
|
||||||
Operations =
|
Operations =
|
||||||
case emqx_lwm2m_xml_object:get_resource_operations(ResId, ObjDefinition) of
|
case emqx_lwm2m_xml_object:get_resource_operations(ResId, ObjDefinition) of
|
||||||
"E" -> #{operations => <<"E">>};
|
"E" ->
|
||||||
|
#{operations => <<"E">>};
|
||||||
Oper ->
|
Oper ->
|
||||||
#{'dataType' =>
|
#{
|
||||||
list_to_binary(emqx_lwm2m_xml_object:get_resource_type(ResId, ObjDefinition)),
|
'dataType' =>
|
||||||
|
list_to_binary(
|
||||||
|
emqx_lwm2m_xml_object:get_resource_type(ResId, ObjDefinition)
|
||||||
|
),
|
||||||
operations => list_to_binary(Oper)
|
operations => list_to_binary(Oper)
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
Operations#{path => Obj,
|
Operations#{
|
||||||
name => list_to_binary(emqx_lwm2m_xml_object:get_resource_name(ResId, ObjDefinition))}.
|
path => Obj,
|
||||||
|
name => list_to_binary(emqx_lwm2m_xml_object:get_resource_name(ResId, ObjDefinition))
|
||||||
|
}.
|
||||||
|
|
||||||
path_list(Path) ->
|
path_list(Path) ->
|
||||||
case binary:split(binary_util:trim(Path, $/), [<<$/>>], [global]) of
|
case binary:split(binary_util:trim(Path, $/), [<<$/>>], [global]) of
|
||||||
|
|
@ -203,35 +221,42 @@ path_list(Path) ->
|
||||||
[ObjId] -> [ObjId]
|
[ObjId] -> [ObjId]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
observe(post, #{bindings := #{clientid := ClientId},
|
observe(post, #{
|
||||||
query_string := #{<<"path">> := Path, <<"enable">> := Enable}}) ->
|
bindings := #{clientid := ClientId},
|
||||||
MsgType = case Enable of
|
query_string := #{<<"path">> := Path, <<"enable">> := Enable}
|
||||||
true -> <<"observe">>;
|
}) ->
|
||||||
_ -> <<"cancel-observe">>
|
MsgType =
|
||||||
end,
|
case Enable of
|
||||||
|
true -> <<"observe">>;
|
||||||
|
_ -> <<"cancel-observe">>
|
||||||
|
end,
|
||||||
|
|
||||||
Cmd = #{<<"msgType">> => MsgType,
|
Cmd = #{
|
||||||
<<"data">> => #{<<"path">> => Path}
|
<<"msgType">> => MsgType,
|
||||||
},
|
<<"data">> => #{<<"path">> => Path}
|
||||||
|
},
|
||||||
|
|
||||||
send_cmd(ClientId, Cmd).
|
send_cmd(ClientId, Cmd).
|
||||||
|
|
||||||
|
read(post, #{
|
||||||
read(post, #{bindings := #{clientid := ClientId},
|
bindings := #{clientid := ClientId},
|
||||||
query_string := Qs}) ->
|
query_string := Qs
|
||||||
|
}) ->
|
||||||
Cmd = #{<<"msgType">> => <<"read">>,
|
Cmd = #{
|
||||||
<<"data">> => Qs
|
<<"msgType">> => <<"read">>,
|
||||||
},
|
<<"data">> => Qs
|
||||||
|
},
|
||||||
|
|
||||||
send_cmd(ClientId, Cmd).
|
send_cmd(ClientId, Cmd).
|
||||||
|
|
||||||
write(post, #{bindings := #{clientid := ClientId},
|
write(post, #{
|
||||||
query_string := Qs}) ->
|
bindings := #{clientid := ClientId},
|
||||||
|
query_string := Qs
|
||||||
Cmd = #{<<"msgType">> => <<"write">>,
|
}) ->
|
||||||
<<"data">> => Qs
|
Cmd = #{
|
||||||
},
|
<<"msgType">> => <<"write">>,
|
||||||
|
<<"data">> => Qs
|
||||||
|
},
|
||||||
|
|
||||||
send_cmd(ClientId, Cmd).
|
send_cmd(ClientId, Cmd).
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,62 +21,69 @@
|
||||||
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ info/1
|
-export([
|
||||||
, info/2
|
info/1,
|
||||||
, stats/1
|
info/2,
|
||||||
, with_context/2
|
stats/1,
|
||||||
, do_takeover/3
|
with_context/2,
|
||||||
, lookup_cmd/3
|
do_takeover/3,
|
||||||
, send_cmd/2
|
lookup_cmd/3,
|
||||||
]).
|
send_cmd/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ init/2
|
-export([
|
||||||
, handle_in/2
|
init/2,
|
||||||
, handle_deliver/2
|
handle_in/2,
|
||||||
, handle_timeout/3
|
handle_deliver/2,
|
||||||
, terminate/2
|
handle_timeout/3,
|
||||||
]).
|
terminate/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ handle_call/3
|
-export([
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
]).
|
handle_info/2
|
||||||
|
]).
|
||||||
|
|
||||||
-record(channel, {
|
-record(channel, {
|
||||||
%% Context
|
%% Context
|
||||||
ctx :: emqx_gateway_ctx:context(),
|
ctx :: emqx_gateway_ctx:context(),
|
||||||
%% Connection Info
|
%% Connection Info
|
||||||
conninfo :: emqx_types:conninfo(),
|
conninfo :: emqx_types:conninfo(),
|
||||||
%% Client Info
|
%% Client Info
|
||||||
clientinfo :: emqx_types:clientinfo(),
|
clientinfo :: emqx_types:clientinfo(),
|
||||||
%% Session
|
%% Session
|
||||||
session :: emqx_lwm2m_session:session() | undefined,
|
session :: emqx_lwm2m_session:session() | undefined,
|
||||||
%% Channel State
|
%% Channel State
|
||||||
%% TODO: is there need
|
%% TODO: is there need
|
||||||
conn_state :: conn_state(),
|
conn_state :: conn_state(),
|
||||||
%% Timer
|
%% Timer
|
||||||
timers :: #{atom() => disable | undefined | reference()},
|
timers :: #{atom() => disable | undefined | reference()},
|
||||||
%% FIXME: don't store anonymous func
|
%% FIXME: don't store anonymous func
|
||||||
with_context :: function()
|
with_context :: function()
|
||||||
}).
|
}).
|
||||||
|
|
||||||
-type channel() :: #channel{}.
|
-type channel() :: #channel{}.
|
||||||
|
|
||||||
-type conn_state() :: idle | connecting | connected | disconnected.
|
-type conn_state() :: idle | connecting | connected | disconnected.
|
||||||
|
|
||||||
-type reply() :: {outgoing, coap_message()}
|
-type reply() ::
|
||||||
| {outgoing, [coap_message()]}
|
{outgoing, coap_message()}
|
||||||
| {event, conn_state()|updated}
|
| {outgoing, [coap_message()]}
|
||||||
| {close, Reason :: atom()}.
|
| {event, conn_state() | updated}
|
||||||
|
| {close, Reason :: atom()}.
|
||||||
|
|
||||||
-type replies() :: reply() | [reply()].
|
-type replies() :: reply() | [reply()].
|
||||||
|
|
||||||
%% TODO:
|
%% TODO:
|
||||||
-define(DEFAULT_OVERRIDE,
|
-define(DEFAULT_OVERRIDE,
|
||||||
#{ clientid => <<"">> %% Generate clientid by default
|
%% Generate clientid by default
|
||||||
, username => <<"${Packet.uri_query.ep}">>
|
#{
|
||||||
, password => <<"">>
|
clientid => <<"">>,
|
||||||
}).
|
username => <<"${Packet.uri_query.ep}">>,
|
||||||
|
password => <<"">>
|
||||||
|
}
|
||||||
|
).
|
||||||
|
|
||||||
-define(INFO_KEYS, [conninfo, conn_state, clientinfo, session]).
|
-define(INFO_KEYS, [conninfo, conn_state, clientinfo, session]).
|
||||||
|
|
||||||
|
|
@ -91,7 +98,6 @@ info(Channel) ->
|
||||||
|
|
||||||
info(Keys, Channel) when is_list(Keys) ->
|
info(Keys, Channel) when is_list(Keys) ->
|
||||||
[{Key, info(Key, Channel)} || Key <- Keys];
|
[{Key, info(Key, Channel)} || Key <- Keys];
|
||||||
|
|
||||||
info(conninfo, #channel{conninfo = ConnInfo}) ->
|
info(conninfo, #channel{conninfo = ConnInfo}) ->
|
||||||
ConnInfo;
|
ConnInfo;
|
||||||
info(conn_state, #channel{conn_state = ConnState}) ->
|
info(conn_state, #channel{conn_state = ConnState}) ->
|
||||||
|
|
@ -108,39 +114,45 @@ info(ctx, #channel{ctx = Ctx}) ->
|
||||||
stats(_) ->
|
stats(_) ->
|
||||||
[].
|
[].
|
||||||
|
|
||||||
init(ConnInfo = #{peername := {PeerHost, _},
|
init(
|
||||||
sockname := {_, SockPort}},
|
ConnInfo = #{
|
||||||
#{ctx := Ctx} = Config) ->
|
peername := {PeerHost, _},
|
||||||
|
sockname := {_, SockPort}
|
||||||
|
},
|
||||||
|
#{ctx := Ctx} = Config
|
||||||
|
) ->
|
||||||
Peercert = maps:get(peercert, ConnInfo, undefined),
|
Peercert = maps:get(peercert, ConnInfo, undefined),
|
||||||
Mountpoint = maps:get(mountpoint, Config, undefined),
|
Mountpoint = maps:get(mountpoint, Config, undefined),
|
||||||
ListenerId = case maps:get(listener, Config, undefined) of
|
ListenerId =
|
||||||
undefined -> undefined;
|
case maps:get(listener, Config, undefined) of
|
||||||
{GwName, Type, LisName} ->
|
undefined -> undefined;
|
||||||
emqx_gateway_utils:listener_id(GwName, Type, LisName)
|
{GwName, Type, LisName} -> emqx_gateway_utils:listener_id(GwName, Type, LisName)
|
||||||
end,
|
end,
|
||||||
ClientInfo = set_peercert_infos(
|
ClientInfo = set_peercert_infos(
|
||||||
Peercert,
|
Peercert,
|
||||||
#{ zone => default
|
#{
|
||||||
, listener => ListenerId
|
zone => default,
|
||||||
, protocol => lwm2m
|
listener => ListenerId,
|
||||||
, peerhost => PeerHost
|
protocol => lwm2m,
|
||||||
, sockport => SockPort
|
peerhost => PeerHost,
|
||||||
, username => undefined
|
sockport => SockPort,
|
||||||
, clientid => undefined
|
username => undefined,
|
||||||
, is_bridge => false
|
clientid => undefined,
|
||||||
, is_superuser => false
|
is_bridge => false,
|
||||||
, mountpoint => Mountpoint
|
is_superuser => false,
|
||||||
}
|
mountpoint => Mountpoint
|
||||||
),
|
}
|
||||||
|
),
|
||||||
|
|
||||||
#channel{ ctx = Ctx
|
#channel{
|
||||||
, conninfo = ConnInfo
|
ctx = Ctx,
|
||||||
, clientinfo = ClientInfo
|
conninfo = ConnInfo,
|
||||||
, timers = #{}
|
clientinfo = ClientInfo,
|
||||||
, session = emqx_lwm2m_session:new()
|
timers = #{},
|
||||||
, conn_state = idle
|
session = emqx_lwm2m_session:new(),
|
||||||
, with_context = with_context(Ctx, ClientInfo)
|
conn_state = idle,
|
||||||
}.
|
with_context = with_context(Ctx, ClientInfo)
|
||||||
|
}.
|
||||||
|
|
||||||
lookup_cmd(Channel, Path, Action) ->
|
lookup_cmd(Channel, Path, Action) ->
|
||||||
gen_server:call(Channel, {?FUNCTION_NAME, Path, Action}).
|
gen_server:call(Channel, {?FUNCTION_NAME, Path, Action}).
|
||||||
|
|
@ -152,11 +164,11 @@ send_cmd(Channel, Cmd) ->
|
||||||
%% Handle incoming packet
|
%% Handle incoming packet
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec handle_in(coap_message() | {frame_error, any()}, channel())
|
-spec handle_in(coap_message() | {frame_error, any()}, channel()) ->
|
||||||
-> {ok, channel()}
|
{ok, channel()}
|
||||||
| {ok, replies(), channel()}
|
| {ok, replies(), channel()}
|
||||||
| {shutdown, Reason :: term(), channel()}
|
| {shutdown, Reason :: term(), channel()}
|
||||||
| {shutdown, Reason :: term(), replies(), channel()}.
|
| {shutdown, Reason :: term(), replies(), channel()}.
|
||||||
handle_in(Msg, Channle) ->
|
handle_in(Msg, Channle) ->
|
||||||
NChannel = update_life_timer(Channle),
|
NChannel = update_life_timer(Channle),
|
||||||
call_session(handle_coap_in, Msg, NChannel).
|
call_session(handle_coap_in, Msg, NChannel).
|
||||||
|
|
@ -170,18 +182,21 @@ handle_deliver(Delivers, Channel) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Handle timeout
|
%% Handle timeout
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
handle_timeout(_, lifetime, #channel{ctx = Ctx,
|
handle_timeout(
|
||||||
clientinfo = ClientInfo,
|
_,
|
||||||
conninfo = ConnInfo} = Channel) ->
|
lifetime,
|
||||||
|
#channel{
|
||||||
|
ctx = Ctx,
|
||||||
|
clientinfo = ClientInfo,
|
||||||
|
conninfo = ConnInfo
|
||||||
|
} = Channel
|
||||||
|
) ->
|
||||||
ok = run_hooks(Ctx, 'client.disconnected', [ClientInfo, timeout, ConnInfo]),
|
ok = run_hooks(Ctx, 'client.disconnected', [ClientInfo, timeout, ConnInfo]),
|
||||||
{shutdown, timeout, Channel};
|
{shutdown, timeout, Channel};
|
||||||
|
|
||||||
handle_timeout(_, {transport, _} = Msg, Channel) ->
|
handle_timeout(_, {transport, _} = Msg, Channel) ->
|
||||||
call_session(timeout, Msg, Channel);
|
call_session(timeout, Msg, Channel);
|
||||||
|
|
||||||
handle_timeout(_, disconnect, Channel) ->
|
handle_timeout(_, disconnect, Channel) ->
|
||||||
{shutdown, normal, Channel};
|
{shutdown, normal, Channel};
|
||||||
|
|
||||||
handle_timeout(_, _, Channel) ->
|
handle_timeout(_, _, Channel) ->
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
|
|
@ -189,63 +204,78 @@ handle_timeout(_, _, Channel) ->
|
||||||
%% Handle call
|
%% Handle call
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
handle_call({lookup_cmd, Path, Type}, _From,
|
handle_call(
|
||||||
Channel = #channel{session = Session}) ->
|
{lookup_cmd, Path, Type},
|
||||||
|
_From,
|
||||||
|
Channel = #channel{session = Session}
|
||||||
|
) ->
|
||||||
Result = emqx_lwm2m_session:find_cmd_record(Path, Type, Session),
|
Result = emqx_lwm2m_session:find_cmd_record(Path, Type, Session),
|
||||||
{reply, {ok, Result}, Channel};
|
{reply, {ok, Result}, Channel};
|
||||||
|
|
||||||
handle_call({send_cmd, Cmd}, _From, Channel) ->
|
handle_call({send_cmd, Cmd}, _From, Channel) ->
|
||||||
{ok, Outs, Channel2} = call_session(send_cmd, Cmd, Channel),
|
{ok, Outs, Channel2} = call_session(send_cmd, Cmd, Channel),
|
||||||
{reply, ok, Outs, Channel2};
|
{reply, ok, Outs, Channel2};
|
||||||
|
handle_call(
|
||||||
handle_call({subscribe, Topic, SubOpts}, _From,
|
{subscribe, Topic, SubOpts},
|
||||||
Channel = #channel{
|
_From,
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo
|
ctx = Ctx,
|
||||||
= #{clientid := ClientId,
|
clientinfo =
|
||||||
mountpoint := Mountpoint},
|
ClientInfo =
|
||||||
session = Session}) ->
|
#{
|
||||||
|
clientid := ClientId,
|
||||||
|
mountpoint := Mountpoint
|
||||||
|
},
|
||||||
|
session = Session
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NSubOpts = maps:merge(
|
NSubOpts = maps:merge(
|
||||||
emqx_gateway_utils:default_subopts(),
|
emqx_gateway_utils:default_subopts(),
|
||||||
SubOpts),
|
SubOpts
|
||||||
|
),
|
||||||
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
||||||
_ = emqx_broker:subscribe(MountedTopic, ClientId, NSubOpts),
|
_ = emqx_broker:subscribe(MountedTopic, ClientId, NSubOpts),
|
||||||
|
|
||||||
_ = run_hooks(Ctx, 'session.subscribed',
|
_ = run_hooks(
|
||||||
[ClientInfo, MountedTopic, NSubOpts]),
|
Ctx,
|
||||||
|
'session.subscribed',
|
||||||
|
[ClientInfo, MountedTopic, NSubOpts]
|
||||||
|
),
|
||||||
%% modify session state
|
%% modify session state
|
||||||
Subs = emqx_lwm2m_session:info(subscriptions, Session),
|
Subs = emqx_lwm2m_session:info(subscriptions, Session),
|
||||||
NSubs = maps:put(MountedTopic, NSubOpts, Subs),
|
NSubs = maps:put(MountedTopic, NSubOpts, Subs),
|
||||||
NSession = emqx_lwm2m_session:set_subscriptions(NSubs, Session),
|
NSession = emqx_lwm2m_session:set_subscriptions(NSubs, Session),
|
||||||
{reply, {ok, {MountedTopic, NSubOpts}}, Channel#channel{session = NSession}};
|
{reply, {ok, {MountedTopic, NSubOpts}}, Channel#channel{session = NSession}};
|
||||||
|
handle_call(
|
||||||
handle_call({unsubscribe, Topic}, _From,
|
{unsubscribe, Topic},
|
||||||
Channel = #channel{
|
_From,
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo
|
ctx = Ctx,
|
||||||
= #{mountpoint := Mountpoint},
|
clientinfo =
|
||||||
session = Session}) ->
|
ClientInfo =
|
||||||
|
#{mountpoint := Mountpoint},
|
||||||
|
session = Session
|
||||||
|
}
|
||||||
|
) ->
|
||||||
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
MountedTopic = emqx_mountpoint:mount(Mountpoint, Topic),
|
||||||
ok = emqx_broker:unsubscribe(MountedTopic),
|
ok = emqx_broker:unsubscribe(MountedTopic),
|
||||||
_ = run_hooks(Ctx, 'session.unsubscribe',
|
_ = run_hooks(
|
||||||
[ClientInfo, MountedTopic, #{}]),
|
Ctx,
|
||||||
|
'session.unsubscribe',
|
||||||
|
[ClientInfo, MountedTopic, #{}]
|
||||||
|
),
|
||||||
%% modify session state
|
%% modify session state
|
||||||
Subs = emqx_lwm2m_session:info(subscriptions, Session),
|
Subs = emqx_lwm2m_session:info(subscriptions, Session),
|
||||||
NSubs = maps:remove(MountedTopic, Subs),
|
NSubs = maps:remove(MountedTopic, Subs),
|
||||||
NSession = emqx_lwm2m_session:set_subscriptions(NSubs, Session),
|
NSession = emqx_lwm2m_session:set_subscriptions(NSubs, Session),
|
||||||
{reply, ok, Channel#channel{session = NSession}};
|
{reply, ok, Channel#channel{session = NSession}};
|
||||||
|
|
||||||
handle_call(subscriptions, _From, Channel = #channel{session = Session}) ->
|
handle_call(subscriptions, _From, Channel = #channel{session = Session}) ->
|
||||||
Subs = maps:to_list(emqx_lwm2m_session:info(subscriptions, Session)),
|
Subs = maps:to_list(emqx_lwm2m_session:info(subscriptions, Session)),
|
||||||
{reply, {ok, Subs}, Channel};
|
{reply, {ok, Subs}, Channel};
|
||||||
|
|
||||||
handle_call(kick, _From, Channel) ->
|
handle_call(kick, _From, Channel) ->
|
||||||
NChannel = ensure_disconnected(kicked, Channel),
|
NChannel = ensure_disconnected(kicked, Channel),
|
||||||
shutdown_and_reply(kicked, ok, NChannel);
|
shutdown_and_reply(kicked, ok, NChannel);
|
||||||
|
|
||||||
handle_call(discard, _From, Channel) ->
|
handle_call(discard, _From, Channel) ->
|
||||||
shutdown_and_reply(discarded, ok, Channel);
|
shutdown_and_reply(discarded, ok, Channel);
|
||||||
|
|
||||||
%% TODO: No Session Takeover
|
%% TODO: No Session Takeover
|
||||||
%handle_call({takeover, 'begin'}, _From, Channel = #channel{session = Session}) ->
|
%handle_call({takeover, 'begin'}, _From, Channel = #channel{session = Session}) ->
|
||||||
% reply(Session, Channel#channel{takeover = true});
|
% reply(Session, Channel#channel{takeover = true});
|
||||||
|
|
@ -259,18 +289,20 @@ handle_call(discard, _From, Channel) ->
|
||||||
% shutdown_and_reply(takenover, AllPendings, Channel);
|
% shutdown_and_reply(takenover, AllPendings, Channel);
|
||||||
|
|
||||||
handle_call(Req, _From, Channel) ->
|
handle_call(Req, _From, Channel) ->
|
||||||
?SLOG(error, #{ msg => "unexpected_call"
|
?SLOG(error, #{
|
||||||
, call => Req
|
msg => "unexpected_call",
|
||||||
}),
|
call => Req
|
||||||
|
}),
|
||||||
{reply, ignored, Channel}.
|
{reply, ignored, Channel}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Handle Cast
|
%% Handle Cast
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
handle_cast(Req, Channel) ->
|
handle_cast(Req, Channel) ->
|
||||||
?SLOG(error, #{ msg => "unexpected_cast"
|
?SLOG(error, #{
|
||||||
, cast => Req
|
msg => "unexpected_cast",
|
||||||
}),
|
cast => Req
|
||||||
|
}),
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
@ -279,19 +311,21 @@ handle_cast(Req, Channel) ->
|
||||||
handle_info({subscribe, _AutoSubs}, Channel) ->
|
handle_info({subscribe, _AutoSubs}, Channel) ->
|
||||||
%% not need handle this message
|
%% not need handle this message
|
||||||
{ok, Channel};
|
{ok, Channel};
|
||||||
|
|
||||||
handle_info(Info, Channel) ->
|
handle_info(Info, Channel) ->
|
||||||
?SLOG(error, #{ msg => "unexpected_info"
|
?SLOG(error, #{
|
||||||
, info => Info
|
msg => "unexpected_info",
|
||||||
}),
|
info => Info
|
||||||
|
}),
|
||||||
{ok, Channel}.
|
{ok, Channel}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Terminate
|
%% Terminate
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
terminate(Reason, #channel{ctx = Ctx,
|
terminate(Reason, #channel{
|
||||||
clientinfo = ClientInfo,
|
ctx = Ctx,
|
||||||
session = Session}) ->
|
clientinfo = ClientInfo,
|
||||||
|
session = Session
|
||||||
|
}) ->
|
||||||
MountedTopic = emqx_lwm2m_session:on_close(Session),
|
MountedTopic = emqx_lwm2m_session:on_close(Session),
|
||||||
_ = run_hooks(Ctx, 'session.unsubscribe', [ClientInfo, MountedTopic, #{}]),
|
_ = run_hooks(Ctx, 'session.unsubscribe', [ClientInfo, MountedTopic, #{}]),
|
||||||
run_hooks(Ctx, 'session.terminated', [ClientInfo, Reason, Session]).
|
run_hooks(Ctx, 'session.terminated', [ClientInfo, Reason, Session]).
|
||||||
|
|
@ -303,29 +337,39 @@ terminate(Reason, #channel{ctx = Ctx,
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Ensure connected
|
%% Ensure connected
|
||||||
|
|
||||||
ensure_connected(Channel = #channel{
|
ensure_connected(
|
||||||
ctx = Ctx,
|
Channel = #channel{
|
||||||
conninfo = ConnInfo,
|
ctx = Ctx,
|
||||||
clientinfo = ClientInfo}) ->
|
conninfo = ConnInfo,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
_ = run_hooks(Ctx, 'client.connack', [ConnInfo, connection_accepted, []]),
|
_ = run_hooks(Ctx, 'client.connack', [ConnInfo, connection_accepted, []]),
|
||||||
|
|
||||||
NConnInfo = ConnInfo#{connected_at => erlang:system_time(millisecond)},
|
NConnInfo = ConnInfo#{connected_at => erlang:system_time(millisecond)},
|
||||||
ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
|
ok = run_hooks(Ctx, 'client.connected', [ClientInfo, NConnInfo]),
|
||||||
Channel#channel{
|
Channel#channel{
|
||||||
conninfo = NConnInfo,
|
conninfo = NConnInfo,
|
||||||
conn_state = connected
|
conn_state = connected
|
||||||
}.
|
}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Ensure disconnected
|
%% Ensure disconnected
|
||||||
|
|
||||||
ensure_disconnected(Reason, Channel = #channel{
|
ensure_disconnected(
|
||||||
ctx = Ctx,
|
Reason,
|
||||||
conninfo = ConnInfo,
|
Channel = #channel{
|
||||||
clientinfo = ClientInfo}) ->
|
ctx = Ctx,
|
||||||
|
conninfo = ConnInfo,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
NConnInfo = ConnInfo#{disconnected_at => erlang:system_time(millisecond)},
|
||||||
ok = run_hooks(Ctx, 'client.disconnected',
|
ok = run_hooks(
|
||||||
[ClientInfo, Reason, NConnInfo]),
|
Ctx,
|
||||||
|
'client.disconnected',
|
||||||
|
[ClientInfo, Reason, NConnInfo]
|
||||||
|
),
|
||||||
Channel#channel{conninfo = NConnInfo, conn_state = disconnected}.
|
Channel#channel{conninfo = NConnInfo, conn_state = disconnected}.
|
||||||
|
|
||||||
shutdown_and_reply(Reason, Reply, Channel) ->
|
shutdown_and_reply(Reason, Reply, Channel) ->
|
||||||
|
|
@ -334,13 +378,13 @@ shutdown_and_reply(Reason, Reply, Channel) ->
|
||||||
%shutdown_and_reply(Reason, Reply, OutPkt, Channel) ->
|
%shutdown_and_reply(Reason, Reply, OutPkt, Channel) ->
|
||||||
% {shutdown, Reason, Reply, OutPkt, Channel}.
|
% {shutdown, Reason, Reply, OutPkt, Channel}.
|
||||||
|
|
||||||
set_peercert_infos(NoSSL, ClientInfo)
|
set_peercert_infos(NoSSL, ClientInfo) when
|
||||||
when NoSSL =:= nossl;
|
NoSSL =:= nossl;
|
||||||
NoSSL =:= undefined ->
|
NoSSL =:= undefined
|
||||||
|
->
|
||||||
ClientInfo;
|
ClientInfo;
|
||||||
set_peercert_infos(Peercert, ClientInfo) ->
|
set_peercert_infos(Peercert, ClientInfo) ->
|
||||||
{DN, CN} = {esockd_peercert:subject(Peercert),
|
{DN, CN} = {esockd_peercert:subject(Peercert), esockd_peercert:common_name(Peercert)},
|
||||||
esockd_peercert:common_name(Peercert)},
|
|
||||||
ClientInfo#{dn => DN, cn => CN}.
|
ClientInfo#{dn => DN, cn => CN}.
|
||||||
|
|
||||||
make_timer(Name, Time, Msg, Channel = #channel{timers = Timers}) ->
|
make_timer(Name, Time, Msg, Channel = #channel{timers = Timers}) ->
|
||||||
|
|
@ -349,7 +393,8 @@ make_timer(Name, Time, Msg, Channel = #channel{timers = Timers}) ->
|
||||||
|
|
||||||
update_life_timer(#channel{session = Session, timers = Timers} = Channel) ->
|
update_life_timer(#channel{session = Session, timers = Timers} = Channel) ->
|
||||||
LifeTime = emqx_lwm2m_session:info(lifetime, Session),
|
LifeTime = emqx_lwm2m_session:info(lifetime, Session),
|
||||||
_ = case maps:get(lifetime, Timers, undefined) of
|
_ =
|
||||||
|
case maps:get(lifetime, Timers, undefined) of
|
||||||
undefined -> ok;
|
undefined -> ok;
|
||||||
Ref -> erlang:cancel_timer(Ref)
|
Ref -> erlang:cancel_timer(Ref)
|
||||||
end,
|
end,
|
||||||
|
|
@ -365,18 +410,25 @@ do_takeover(_DesireId, Msg, Channel) ->
|
||||||
call_session(handle_out, Reset, Channel).
|
call_session(handle_out, Reset, Channel).
|
||||||
|
|
||||||
do_connect(Req, Result, Channel, Iter) ->
|
do_connect(Req, Result, Channel, Iter) ->
|
||||||
case emqx_misc:pipeline(
|
case
|
||||||
[ fun check_lwm2m_version/2
|
emqx_misc:pipeline(
|
||||||
, fun enrich_conninfo/2
|
[
|
||||||
, fun run_conn_hooks/2
|
fun check_lwm2m_version/2,
|
||||||
, fun enrich_clientinfo/2
|
fun enrich_conninfo/2,
|
||||||
, fun set_log_meta/2
|
fun run_conn_hooks/2,
|
||||||
, fun auth_connect/2
|
fun enrich_clientinfo/2,
|
||||||
],
|
fun set_log_meta/2,
|
||||||
Req,
|
fun auth_connect/2
|
||||||
Channel) of
|
],
|
||||||
{ok, _Input, #channel{session = Session,
|
Req,
|
||||||
with_context = WithContext} = NChannel} ->
|
Channel
|
||||||
|
)
|
||||||
|
of
|
||||||
|
{ok, _Input,
|
||||||
|
#channel{
|
||||||
|
session = Session,
|
||||||
|
with_context = WithContext
|
||||||
|
} = NChannel} ->
|
||||||
case emqx_lwm2m_session:info(reg_info, Session) of
|
case emqx_lwm2m_session:info(reg_info, Session) of
|
||||||
undefined ->
|
undefined ->
|
||||||
process_connect(ensure_connected(NChannel), Req, Result, Iter);
|
process_connect(ensure_connected(NChannel), Req, Result, Iter);
|
||||||
|
|
@ -387,66 +439,84 @@ do_connect(Req, Result, Channel, Iter) ->
|
||||||
{error, ReasonCode, NChannel} ->
|
{error, ReasonCode, NChannel} ->
|
||||||
ErrMsg = io_lib:format("Login Failed: ~ts", [ReasonCode]),
|
ErrMsg = io_lib:format("Login Failed: ~ts", [ReasonCode]),
|
||||||
Payload = erlang:list_to_binary(lists:flatten(ErrMsg)),
|
Payload = erlang:list_to_binary(lists:flatten(ErrMsg)),
|
||||||
iter(Iter,
|
iter(
|
||||||
reply({error, bad_request}, Payload, Req, Result),
|
Iter,
|
||||||
NChannel)
|
reply({error, bad_request}, Payload, Req, Result),
|
||||||
|
NChannel
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
check_lwm2m_version(#coap_message{options = Opts},
|
check_lwm2m_version(
|
||||||
#channel{conninfo = ConnInfo} = Channel) ->
|
#coap_message{options = Opts},
|
||||||
|
#channel{conninfo = ConnInfo} = Channel
|
||||||
|
) ->
|
||||||
Ver = gets([uri_query, <<"lwm2m">>], Opts),
|
Ver = gets([uri_query, <<"lwm2m">>], Opts),
|
||||||
IsValid = case Ver of
|
IsValid =
|
||||||
<<"1.0">> ->
|
case Ver of
|
||||||
true;
|
<<"1.0">> ->
|
||||||
<<"1">> ->
|
true;
|
||||||
true;
|
<<"1">> ->
|
||||||
<<"1.1">> ->
|
true;
|
||||||
true;
|
<<"1.1">> ->
|
||||||
_ ->
|
true;
|
||||||
false
|
_ ->
|
||||||
end,
|
false
|
||||||
if IsValid ->
|
end,
|
||||||
NConnInfo = ConnInfo#{ connected_at => erlang:system_time(millisecond)
|
if
|
||||||
, proto_ver => Ver
|
IsValid ->
|
||||||
},
|
NConnInfo = ConnInfo#{
|
||||||
|
connected_at => erlang:system_time(millisecond),
|
||||||
|
proto_ver => Ver
|
||||||
|
},
|
||||||
{ok, Channel#channel{conninfo = NConnInfo}};
|
{ok, Channel#channel{conninfo = NConnInfo}};
|
||||||
true ->
|
true ->
|
||||||
?SLOG(error, #{ msg => "reject_REGISTRE_request"
|
?SLOG(error, #{
|
||||||
, reason => {unsupported_version, Ver}
|
msg => "reject_REGISTRE_request",
|
||||||
}),
|
reason => {unsupported_version, Ver}
|
||||||
|
}),
|
||||||
{error, "invalid lwm2m version", Channel}
|
{error, "invalid lwm2m version", Channel}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
run_conn_hooks(Input, Channel = #channel{ctx = Ctx,
|
run_conn_hooks(
|
||||||
conninfo = ConnInfo}) ->
|
Input,
|
||||||
|
Channel = #channel{
|
||||||
|
ctx = Ctx,
|
||||||
|
conninfo = ConnInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
ConnProps = #{},
|
ConnProps = #{},
|
||||||
case run_hooks(Ctx, 'client.connect', [ConnInfo], ConnProps) of
|
case run_hooks(Ctx, 'client.connect', [ConnInfo], ConnProps) of
|
||||||
Error = {error, _Reason} -> Error;
|
Error = {error, _Reason} -> Error;
|
||||||
_NConnProps ->
|
_NConnProps -> {ok, Input, Channel}
|
||||||
{ok, Input, Channel}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
enrich_conninfo(#coap_message{options = Options},
|
enrich_conninfo(
|
||||||
Channel = #channel{
|
#coap_message{options = Options},
|
||||||
conninfo = ConnInfo}) ->
|
Channel = #channel{
|
||||||
|
conninfo = ConnInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
Query = maps:get(uri_query, Options, #{}),
|
Query = maps:get(uri_query, Options, #{}),
|
||||||
case Query of
|
case Query of
|
||||||
#{<<"ep">> := Epn, <<"lt">> := Lifetime} ->
|
#{<<"ep">> := Epn, <<"lt">> := Lifetime} ->
|
||||||
ClientId = maps:get(<<"device_id">>, Query, Epn),
|
ClientId = maps:get(<<"device_id">>, Query, Epn),
|
||||||
NConnInfo = ConnInfo#{ clientid => ClientId
|
NConnInfo = ConnInfo#{
|
||||||
, proto_name => <<"LwM2M">>
|
clientid => ClientId,
|
||||||
, proto_ver => <<"1.0.1">>
|
proto_name => <<"LwM2M">>,
|
||||||
, clean_start => true
|
proto_ver => <<"1.0.1">>,
|
||||||
, keepalive => binary_to_integer(Lifetime)
|
clean_start => true,
|
||||||
, expiry_interval => 0
|
keepalive => binary_to_integer(Lifetime),
|
||||||
},
|
expiry_interval => 0
|
||||||
|
},
|
||||||
{ok, Channel#channel{conninfo = NConnInfo}};
|
{ok, Channel#channel{conninfo = NConnInfo}};
|
||||||
_ ->
|
_ ->
|
||||||
{error, "invalid queries", Channel}
|
{error, "invalid queries", Channel}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
enrich_clientinfo(#coap_message{options = Options} = Msg,
|
enrich_clientinfo(
|
||||||
Channel = #channel{clientinfo = ClientInfo0}) ->
|
#coap_message{options = Options} = Msg,
|
||||||
|
Channel = #channel{clientinfo = ClientInfo0}
|
||||||
|
) ->
|
||||||
Query = maps:get(uri_query, Options, #{}),
|
Query = maps:get(uri_query, Options, #{}),
|
||||||
case Query of
|
case Query of
|
||||||
#{<<"ep">> := Epn, <<"lt">> := Lifetime} ->
|
#{<<"ep">> := Epn, <<"lt">> := Lifetime} ->
|
||||||
|
|
@ -455,17 +525,20 @@ enrich_clientinfo(#coap_message{options = Options} = Msg,
|
||||||
Password = maps:get(<<"password">>, Query, undefined),
|
Password = maps:get(<<"password">>, Query, undefined),
|
||||||
ClientId = maps:get(<<"device_id">>, Query, Epn),
|
ClientId = maps:get(<<"device_id">>, Query, Epn),
|
||||||
ClientInfo =
|
ClientInfo =
|
||||||
ClientInfo0#{endpoint_name => Epn,
|
ClientInfo0#{
|
||||||
lifetime => binary_to_integer(Lifetime),
|
endpoint_name => Epn,
|
||||||
username => Username,
|
lifetime => binary_to_integer(Lifetime),
|
||||||
password => Password,
|
username => Username,
|
||||||
clientid => ClientId},
|
password => Password,
|
||||||
|
clientid => ClientId
|
||||||
|
},
|
||||||
{ok, NClientInfo} = fix_mountpoint(Msg, ClientInfo),
|
{ok, NClientInfo} = fix_mountpoint(Msg, ClientInfo),
|
||||||
{ok, Channel#channel{clientinfo = NClientInfo}};
|
{ok, Channel#channel{clientinfo = NClientInfo}};
|
||||||
_ ->
|
_ ->
|
||||||
?SLOG(error, #{ msg => "reject_REGISTER_request"
|
?SLOG(error, #{
|
||||||
, reason => {wrong_paramters, Query}
|
msg => "reject_REGISTER_request",
|
||||||
}),
|
reason => {wrong_paramters, Query}
|
||||||
|
}),
|
||||||
{error, "invalid queries", Channel}
|
{error, "invalid queries", Channel}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -473,19 +546,27 @@ set_log_meta(_Input, #channel{clientinfo = #{clientid := ClientId}}) ->
|
||||||
emqx_logger:set_metadata_clientid(ClientId),
|
emqx_logger:set_metadata_clientid(ClientId),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
auth_connect(_Input, Channel = #channel{ctx = Ctx,
|
auth_connect(
|
||||||
clientinfo = ClientInfo}) ->
|
_Input,
|
||||||
|
Channel = #channel{
|
||||||
|
ctx = Ctx,
|
||||||
|
clientinfo = ClientInfo
|
||||||
|
}
|
||||||
|
) ->
|
||||||
#{clientid := ClientId, username := Username} = ClientInfo,
|
#{clientid := ClientId, username := Username} = ClientInfo,
|
||||||
case emqx_gateway_ctx:authenticate(Ctx, ClientInfo) of
|
case emqx_gateway_ctx:authenticate(Ctx, ClientInfo) of
|
||||||
{ok, NClientInfo} ->
|
{ok, NClientInfo} ->
|
||||||
{ok, Channel#channel{clientinfo = NClientInfo,
|
{ok, Channel#channel{
|
||||||
with_context = with_context(Ctx, ClientInfo)}};
|
clientinfo = NClientInfo,
|
||||||
|
with_context = with_context(Ctx, ClientInfo)
|
||||||
|
}};
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(warning, #{ msg => "client_login_failed"
|
?SLOG(warning, #{
|
||||||
, clientid => ClientId
|
msg => "client_login_failed",
|
||||||
, username => Username
|
clientid => ClientId,
|
||||||
, reason => Reason
|
username => Username,
|
||||||
}),
|
reason => Reason
|
||||||
|
}),
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -495,36 +576,45 @@ fix_mountpoint(_Packet, ClientInfo = #{mountpoint := Mountpoint}) ->
|
||||||
Mountpoint1 = emqx_mountpoint:replvar(Mountpoint, ClientInfo),
|
Mountpoint1 = emqx_mountpoint:replvar(Mountpoint, ClientInfo),
|
||||||
{ok, ClientInfo#{mountpoint := Mountpoint1}}.
|
{ok, ClientInfo#{mountpoint := Mountpoint1}}.
|
||||||
|
|
||||||
process_connect(Channel = #channel{ctx = Ctx,
|
process_connect(
|
||||||
session = Session,
|
Channel = #channel{
|
||||||
conninfo = ConnInfo,
|
ctx = Ctx,
|
||||||
clientinfo = ClientInfo,
|
session = Session,
|
||||||
with_context = WithContext},
|
conninfo = ConnInfo,
|
||||||
Msg, Result, Iter) ->
|
clientinfo = ClientInfo,
|
||||||
|
with_context = WithContext
|
||||||
|
},
|
||||||
|
Msg,
|
||||||
|
Result,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
%% inherit the old session
|
%% inherit the old session
|
||||||
SessFun = fun(_,_) -> #{} end,
|
SessFun = fun(_, _) -> #{} end,
|
||||||
case emqx_gateway_ctx:open_session(
|
case
|
||||||
Ctx,
|
emqx_gateway_ctx:open_session(
|
||||||
true,
|
Ctx,
|
||||||
ClientInfo,
|
true,
|
||||||
ConnInfo,
|
ClientInfo,
|
||||||
SessFun,
|
ConnInfo,
|
||||||
emqx_lwm2m_session
|
SessFun,
|
||||||
) of
|
emqx_lwm2m_session
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, _} ->
|
{ok, _} ->
|
||||||
Mountpoint = maps:get(mountpoint, ClientInfo, <<>>),
|
Mountpoint = maps:get(mountpoint, ClientInfo, <<>>),
|
||||||
NewResult0 = emqx_lwm2m_session:init(
|
NewResult0 = emqx_lwm2m_session:init(
|
||||||
Msg,
|
Msg,
|
||||||
Mountpoint,
|
Mountpoint,
|
||||||
WithContext,
|
WithContext,
|
||||||
Session
|
Session
|
||||||
),
|
),
|
||||||
NewResult1 = NewResult0#{events => [{event, connected}]},
|
NewResult1 = NewResult0#{events => [{event, connected}]},
|
||||||
iter(Iter, maps:merge(Result, NewResult1), Channel);
|
iter(Iter, maps:merge(Result, NewResult1), Channel);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?SLOG(error, #{ msg => "falied_to_open_session"
|
?SLOG(error, #{
|
||||||
, reason => Reason
|
msg => "falied_to_open_session",
|
||||||
}),
|
reason => Reason
|
||||||
|
}),
|
||||||
iter(Iter, reply({error, bad_request}, Msg, Result), Channel)
|
iter(Iter, reply({error, bad_request}, Msg, Result), Channel)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -557,52 +647,68 @@ with_context(publish, [Topic, Msg], Ctx, ClientInfo) ->
|
||||||
_ = emqx_broker:publish(Msg),
|
_ = emqx_broker:publish(Msg),
|
||||||
ok;
|
ok;
|
||||||
_ ->
|
_ ->
|
||||||
?SLOG(error, #{ msg => "publish_denied"
|
?SLOG(error, #{
|
||||||
, topic => Topic
|
msg => "publish_denied",
|
||||||
}),
|
topic => Topic
|
||||||
|
}),
|
||||||
{error, deny}
|
{error, deny}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
with_context(subscribe, [Topic, Opts], Ctx, ClientInfo) ->
|
with_context(subscribe, [Topic, Opts], Ctx, ClientInfo) ->
|
||||||
#{clientid := ClientId,
|
#{
|
||||||
endpoint_name := EndpointName} = ClientInfo,
|
clientid := ClientId,
|
||||||
|
endpoint_name := EndpointName
|
||||||
|
} = ClientInfo,
|
||||||
case emqx_gateway_ctx:authorize(Ctx, ClientInfo, subscribe, Topic) of
|
case emqx_gateway_ctx:authorize(Ctx, ClientInfo, subscribe, Topic) of
|
||||||
allow ->
|
allow ->
|
||||||
run_hooks(Ctx, 'session.subscribed', [ClientInfo, Topic, Opts]),
|
run_hooks(Ctx, 'session.subscribed', [ClientInfo, Topic, Opts]),
|
||||||
?SLOG(debug, #{ msg => "subscribe_topic_succeed"
|
?SLOG(debug, #{
|
||||||
, topic => Topic
|
msg => "subscribe_topic_succeed",
|
||||||
, clientid => ClientId
|
topic => Topic,
|
||||||
, endpoint_name => EndpointName
|
clientid => ClientId,
|
||||||
}),
|
endpoint_name => EndpointName
|
||||||
|
}),
|
||||||
emqx_broker:subscribe(Topic, ClientId, Opts),
|
emqx_broker:subscribe(Topic, ClientId, Opts),
|
||||||
ok;
|
ok;
|
||||||
_ ->
|
_ ->
|
||||||
?SLOG(error, #{ msg => "subscribe_denied"
|
?SLOG(error, #{
|
||||||
, topic => Topic
|
msg => "subscribe_denied",
|
||||||
}),
|
topic => Topic
|
||||||
|
}),
|
||||||
{error, deny}
|
{error, deny}
|
||||||
end;
|
end;
|
||||||
|
|
||||||
with_context(metrics, Name, Ctx, _ClientInfo) ->
|
with_context(metrics, Name, Ctx, _ClientInfo) ->
|
||||||
emqx_gateway_ctx:metrics_inc(Ctx, Name).
|
emqx_gateway_ctx:metrics_inc(Ctx, Name).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Call Chain
|
%% Call Chain
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
call_session(Fun,
|
call_session(
|
||||||
Msg,
|
Fun,
|
||||||
#channel{session = Session,
|
Msg,
|
||||||
with_context = WithContext} = Channel) ->
|
#channel{
|
||||||
iter([ session, fun process_session/4
|
session = Session,
|
||||||
, proto, fun process_protocol/4
|
with_context = WithContext
|
||||||
, return, fun process_return/4
|
} = Channel
|
||||||
, lifetime, fun process_lifetime/4
|
) ->
|
||||||
, reply, fun process_reply/4
|
iter(
|
||||||
, out, fun process_out/4
|
[
|
||||||
, fun process_nothing/3
|
session,
|
||||||
],
|
fun process_session/4,
|
||||||
emqx_lwm2m_session:Fun(Msg, WithContext, Session),
|
proto,
|
||||||
Channel).
|
fun process_protocol/4,
|
||||||
|
return,
|
||||||
|
fun process_return/4,
|
||||||
|
lifetime,
|
||||||
|
fun process_lifetime/4,
|
||||||
|
reply,
|
||||||
|
fun process_reply/4,
|
||||||
|
out,
|
||||||
|
fun process_out/4,
|
||||||
|
fun process_nothing/3
|
||||||
|
],
|
||||||
|
emqx_lwm2m_session:Fun(Msg, WithContext, Session),
|
||||||
|
Channel
|
||||||
|
).
|
||||||
|
|
||||||
process_session(Session, Result, Channel, Iter) ->
|
process_session(Session, Result, Channel, Iter) ->
|
||||||
iter(Iter, Result, Channel#channel{session = Session}).
|
iter(Iter, Result, Channel#channel{session = Session}).
|
||||||
|
|
@ -610,14 +716,22 @@ process_session(Session, Result, Channel, Iter) ->
|
||||||
process_protocol({request, Msg}, Result, Channel, Iter) ->
|
process_protocol({request, Msg}, Result, Channel, Iter) ->
|
||||||
#coap_message{method = Method} = Msg,
|
#coap_message{method = Method} = Msg,
|
||||||
handle_request_protocol(Method, Msg, Result, Channel, Iter);
|
handle_request_protocol(Method, Msg, Result, Channel, Iter);
|
||||||
|
process_protocol(
|
||||||
process_protocol(Msg, Result,
|
Msg,
|
||||||
#channel{with_context = WithContext, session = Session} = Channel, Iter) ->
|
Result,
|
||||||
|
#channel{with_context = WithContext, session = Session} = Channel,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
ProtoResult = emqx_lwm2m_session:handle_protocol_in(Msg, WithContext, Session),
|
ProtoResult = emqx_lwm2m_session:handle_protocol_in(Msg, WithContext, Session),
|
||||||
iter(Iter, maps:merge(Result, ProtoResult), Channel).
|
iter(Iter, maps:merge(Result, ProtoResult), Channel).
|
||||||
|
|
||||||
handle_request_protocol(post, #coap_message{options = Opts} = Msg,
|
handle_request_protocol(
|
||||||
Result, Channel, Iter) ->
|
post,
|
||||||
|
#coap_message{options = Opts} = Msg,
|
||||||
|
Result,
|
||||||
|
Channel,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
case Opts of
|
case Opts of
|
||||||
#{uri_path := [?REG_PREFIX]} ->
|
#{uri_path := [?REG_PREFIX]} ->
|
||||||
do_connect(Msg, Result, Channel, Iter);
|
do_connect(Msg, Result, Channel, Iter);
|
||||||
|
|
@ -626,9 +740,13 @@ handle_request_protocol(post, #coap_message{options = Opts} = Msg,
|
||||||
_ ->
|
_ ->
|
||||||
iter(Iter, reply({error, not_found}, Msg, Result), Channel)
|
iter(Iter, reply({error, not_found}, Msg, Result), Channel)
|
||||||
end;
|
end;
|
||||||
|
handle_request_protocol(
|
||||||
handle_request_protocol(delete, #coap_message{options = Opts} = Msg,
|
delete,
|
||||||
Result, Channel, Iter) ->
|
#coap_message{options = Opts} = Msg,
|
||||||
|
Result,
|
||||||
|
Channel,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
case Opts of
|
case Opts of
|
||||||
#{uri_path := Location} ->
|
#{uri_path := Location} ->
|
||||||
case check_location(Location, Channel) of
|
case check_location(Location, Channel) of
|
||||||
|
|
@ -642,8 +760,13 @@ handle_request_protocol(delete, #coap_message{options = Opts} = Msg,
|
||||||
iter(Iter, reply({error, bad_request}, Msg, Result), Channel)
|
iter(Iter, reply({error, bad_request}, Msg, Result), Channel)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
do_update(Location, Msg, Result,
|
do_update(
|
||||||
#channel{session = Session, with_context = WithContext} = Channel, Iter) ->
|
Location,
|
||||||
|
Msg,
|
||||||
|
Result,
|
||||||
|
#channel{session = Session, with_context = WithContext} = Channel,
|
||||||
|
Iter
|
||||||
|
) ->
|
||||||
case check_location(Location, Channel) of
|
case check_location(Location, Channel) of
|
||||||
true ->
|
true ->
|
||||||
NewResult = emqx_lwm2m_session:update(Msg, WithContext, Session),
|
NewResult = emqx_lwm2m_session:update(Msg, WithContext, Session),
|
||||||
|
|
@ -654,18 +777,21 @@ do_update(Location, Msg, Result,
|
||||||
|
|
||||||
process_return({Outs, Session}, Result, Channel, Iter) ->
|
process_return({Outs, Session}, Result, Channel, Iter) ->
|
||||||
OldOuts = maps:get(out, Result, []),
|
OldOuts = maps:get(out, Result, []),
|
||||||
iter(Iter,
|
iter(
|
||||||
Result#{out => Outs ++ OldOuts},
|
Iter,
|
||||||
Channel#channel{session = Session}).
|
Result#{out => Outs ++ OldOuts},
|
||||||
|
Channel#channel{session = Session}
|
||||||
|
).
|
||||||
|
|
||||||
process_out(Outs, Result, Channel, _) ->
|
process_out(Outs, Result, Channel, _) ->
|
||||||
Outs2 = lists:reverse(Outs),
|
Outs2 = lists:reverse(Outs),
|
||||||
Outs3 = case maps:get(reply, Result, undefined) of
|
Outs3 =
|
||||||
undefined ->
|
case maps:get(reply, Result, undefined) of
|
||||||
Outs2;
|
undefined ->
|
||||||
Reply ->
|
Outs2;
|
||||||
[Reply | Outs2]
|
Reply ->
|
||||||
end,
|
[Reply | Outs2]
|
||||||
|
end,
|
||||||
Events = maps:get(events, Result, []),
|
Events = maps:get(events, Result, []),
|
||||||
{ok, [{outgoing, Outs3}] ++ Events, Channel}.
|
{ok, [{outgoing, Outs3}] ++ Events, Channel}.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,11 +20,12 @@
|
||||||
-include("src/coap/include/emqx_coap.hrl").
|
-include("src/coap/include/emqx_coap.hrl").
|
||||||
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
||||||
|
|
||||||
-export([ mqtt_to_coap/2
|
-export([
|
||||||
, coap_to_mqtt/4
|
mqtt_to_coap/2,
|
||||||
, empty_ack_to_mqtt/1
|
coap_to_mqtt/4,
|
||||||
, coap_failure_to_mqtt/2
|
empty_ack_to_mqtt/1,
|
||||||
]).
|
coap_failure_to_mqtt/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([path_list/1, extract_path/1]).
|
-export([path_list/1, extract_path/1]).
|
||||||
|
|
||||||
|
|
@ -54,26 +55,47 @@ mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"create">>, <<"data"
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
TlvData = emqx_lwm2m_message:json_to_tlv(PathList, maps:get(<<"content">>, Data)),
|
TlvData = emqx_lwm2m_message:json_to_tlv(PathList, maps:get(<<"content">>, Data)),
|
||||||
Payload = emqx_lwm2m_tlv:encode(TlvData),
|
Payload = emqx_lwm2m_tlv:encode(TlvData),
|
||||||
CoapRequest = emqx_coap_message:request(con, post, Payload,
|
CoapRequest = emqx_coap_message:request(
|
||||||
[{uri_path, FullPathList},
|
con,
|
||||||
{uri_query, QueryList},
|
post,
|
||||||
{content_format, <<"application/vnd.oma.lwm2m+tlv">>}]),
|
Payload,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{content_format, <<"application/vnd.oma.lwm2m+tlv">>}
|
||||||
|
]
|
||||||
|
),
|
||||||
{CoapRequest, InputCmd};
|
{CoapRequest, InputCmd};
|
||||||
|
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"delete">>, <<"data">> := Data}) ->
|
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"delete">>, <<"data">> := Data}) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
{emqx_coap_message:request(con, delete, <<>>,
|
{
|
||||||
[{uri_path, FullPathList},
|
emqx_coap_message:request(
|
||||||
{uri_query, QueryList}]), InputCmd};
|
con,
|
||||||
|
delete,
|
||||||
|
<<>>,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
InputCmd
|
||||||
|
};
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"read">>, <<"data">> := Data}) ->
|
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"read">>, <<"data">> := Data}) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
{emqx_coap_message:request(con, get, <<>>,
|
{
|
||||||
[{uri_path, FullPathList},
|
emqx_coap_message:request(
|
||||||
{uri_query, QueryList}]), InputCmd};
|
con,
|
||||||
|
get,
|
||||||
|
<<>>,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
InputCmd
|
||||||
|
};
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"write">>, <<"data">> := Data}) ->
|
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"write">>, <<"data">> := Data}) ->
|
||||||
CoapRequest =
|
CoapRequest =
|
||||||
case maps:get(<<"basePath">>, Data, <<"/">>) of
|
case maps:get(<<"basePath">>, Data, <<"/">>) of
|
||||||
|
|
@ -83,7 +105,6 @@ mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"write">>, <<"data">
|
||||||
batch_write_request(AlternatePath, BasePath, maps:get(<<"content">>, Data))
|
batch_write_request(AlternatePath, BasePath, maps:get(<<"content">>, Data))
|
||||||
end,
|
end,
|
||||||
{CoapRequest, InputCmd};
|
{CoapRequest, InputCmd};
|
||||||
|
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"execute">>, <<"data">> := Data}) ->
|
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"execute">>, <<"data">> := Data}) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
|
|
@ -93,85 +114,122 @@ mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"execute">>, <<"data
|
||||||
undefined -> <<>>;
|
undefined -> <<>>;
|
||||||
Arg1 -> Arg1
|
Arg1 -> Arg1
|
||||||
end,
|
end,
|
||||||
{emqx_coap_message:request(con, post, Args,
|
{
|
||||||
[{uri_path, FullPathList},
|
emqx_coap_message:request(
|
||||||
{uri_query, QueryList},
|
con,
|
||||||
{content_format, <<"text/plain">>}]), InputCmd};
|
post,
|
||||||
|
Args,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{content_format, <<"text/plain">>}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
InputCmd
|
||||||
|
};
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"discover">>, <<"data">> := Data}) ->
|
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"discover">>, <<"data">> := Data}) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
{emqx_coap_message:request(con, get, <<>>,
|
{
|
||||||
[{uri_path, FullPathList},
|
emqx_coap_message:request(
|
||||||
{uri_query, QueryList},
|
con,
|
||||||
{'accept', ?LWM2M_FORMAT_LINK}]), InputCmd};
|
get,
|
||||||
|
<<>>,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{'accept', ?LWM2M_FORMAT_LINK}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
InputCmd
|
||||||
|
};
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"write-attr">>, <<"data">> := Data}) ->
|
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"write-attr">>, <<"data">> := Data}) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
Query = attr_query_list(Data),
|
Query = attr_query_list(Data),
|
||||||
{emqx_coap_message:request(con, put, <<>>,
|
{
|
||||||
[{uri_path, FullPathList},
|
emqx_coap_message:request(
|
||||||
{uri_query, QueryList},
|
con,
|
||||||
{uri_query, Query}]), InputCmd};
|
put,
|
||||||
|
<<>>,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{uri_query, Query}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
InputCmd
|
||||||
|
};
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"observe">>, <<"data">> := Data}) ->
|
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"observe">>, <<"data">> := Data}) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
{emqx_coap_message:request(con, get, <<>>,
|
{
|
||||||
[{uri_path, FullPathList},
|
emqx_coap_message:request(
|
||||||
{uri_query, QueryList},
|
con,
|
||||||
{observe, 0}]), InputCmd};
|
get,
|
||||||
|
<<>>,
|
||||||
mqtt_to_coap(AlternatePath, InputCmd = #{<<"msgType">> := <<"cancel-observe">>, <<"data">> := Data}) ->
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{observe, 0}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
InputCmd
|
||||||
|
};
|
||||||
|
mqtt_to_coap(
|
||||||
|
AlternatePath, InputCmd = #{<<"msgType">> := <<"cancel-observe">>, <<"data">> := Data}
|
||||||
|
) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
{emqx_coap_message:request(con, get, <<>>,
|
{
|
||||||
[{uri_path, FullPathList},
|
emqx_coap_message:request(
|
||||||
{uri_query, QueryList},
|
con,
|
||||||
{observe, 1}]), InputCmd}.
|
get,
|
||||||
|
<<>>,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{observe, 1}
|
||||||
|
]
|
||||||
|
),
|
||||||
|
InputCmd
|
||||||
|
}.
|
||||||
|
|
||||||
coap_to_mqtt(_Method = {_, Code}, _CoapPayload, _Options, Ref=#{<<"msgType">> := <<"create">>}) ->
|
coap_to_mqtt(_Method = {_, Code}, _CoapPayload, _Options, Ref = #{<<"msgType">> := <<"create">>}) ->
|
||||||
make_response(Code, Ref);
|
make_response(Code, Ref);
|
||||||
|
coap_to_mqtt(_Method = {_, Code}, _CoapPayload, _Options, Ref = #{<<"msgType">> := <<"delete">>}) ->
|
||||||
coap_to_mqtt(_Method = {_, Code}, _CoapPayload, _Options, Ref=#{<<"msgType">> := <<"delete">>}) ->
|
|
||||||
make_response(Code, Ref);
|
make_response(Code, Ref);
|
||||||
|
coap_to_mqtt(Method, CoapPayload, Options, Ref = #{<<"msgType">> := <<"read">>}) ->
|
||||||
coap_to_mqtt(Method, CoapPayload, Options, Ref=#{<<"msgType">> := <<"read">>}) ->
|
|
||||||
read_resp_to_mqtt(Method, CoapPayload, data_format(Options), Ref);
|
read_resp_to_mqtt(Method, CoapPayload, data_format(Options), Ref);
|
||||||
|
coap_to_mqtt(Method, CoapPayload, _Options, Ref = #{<<"msgType">> := <<"write">>}) ->
|
||||||
coap_to_mqtt(Method, CoapPayload, _Options, Ref=#{<<"msgType">> := <<"write">>}) ->
|
|
||||||
write_resp_to_mqtt(Method, CoapPayload, Ref);
|
write_resp_to_mqtt(Method, CoapPayload, Ref);
|
||||||
|
coap_to_mqtt(Method, _CoapPayload, _Options, Ref = #{<<"msgType">> := <<"execute">>}) ->
|
||||||
coap_to_mqtt(Method, _CoapPayload, _Options, Ref=#{<<"msgType">> := <<"execute">>}) ->
|
|
||||||
execute_resp_to_mqtt(Method, Ref);
|
execute_resp_to_mqtt(Method, Ref);
|
||||||
|
coap_to_mqtt(Method, CoapPayload, _Options, Ref = #{<<"msgType">> := <<"discover">>}) ->
|
||||||
coap_to_mqtt(Method, CoapPayload, _Options, Ref=#{<<"msgType">> := <<"discover">>}) ->
|
|
||||||
discover_resp_to_mqtt(Method, CoapPayload, Ref);
|
discover_resp_to_mqtt(Method, CoapPayload, Ref);
|
||||||
|
coap_to_mqtt(Method, CoapPayload, _Options, Ref = #{<<"msgType">> := <<"write-attr">>}) ->
|
||||||
coap_to_mqtt(Method, CoapPayload, _Options, Ref=#{<<"msgType">> := <<"write-attr">>}) ->
|
|
||||||
writeattr_resp_to_mqtt(Method, CoapPayload, Ref);
|
writeattr_resp_to_mqtt(Method, CoapPayload, Ref);
|
||||||
|
coap_to_mqtt(Method, CoapPayload, Options, Ref = #{<<"msgType">> := <<"observe">>}) ->
|
||||||
coap_to_mqtt(Method, CoapPayload, Options, Ref=#{<<"msgType">> := <<"observe">>}) ->
|
|
||||||
observe_resp_to_mqtt(Method, CoapPayload, data_format(Options), observe_seq(Options), Ref);
|
observe_resp_to_mqtt(Method, CoapPayload, data_format(Options), observe_seq(Options), Ref);
|
||||||
|
coap_to_mqtt(Method, CoapPayload, Options, Ref = #{<<"msgType">> := <<"cancel-observe">>}) ->
|
||||||
coap_to_mqtt(Method, CoapPayload, Options, Ref=#{<<"msgType">> := <<"cancel-observe">>}) ->
|
|
||||||
cancel_observe_resp_to_mqtt(Method, CoapPayload, data_format(Options), Ref).
|
cancel_observe_resp_to_mqtt(Method, CoapPayload, data_format(Options), Ref).
|
||||||
|
|
||||||
read_resp_to_mqtt({error, ErrorCode}, _CoapPayload, _Format, Ref) ->
|
read_resp_to_mqtt({error, ErrorCode}, _CoapPayload, _Format, Ref) ->
|
||||||
make_response(ErrorCode, Ref);
|
make_response(ErrorCode, Ref);
|
||||||
|
|
||||||
read_resp_to_mqtt({ok, SuccessCode}, CoapPayload, Format, Ref) ->
|
read_resp_to_mqtt({ok, SuccessCode}, CoapPayload, Format, Ref) ->
|
||||||
try
|
try
|
||||||
Result = content_to_mqtt(CoapPayload, Format, Ref),
|
Result = content_to_mqtt(CoapPayload, Format, Ref),
|
||||||
make_response(SuccessCode, Ref, Format, Result)
|
make_response(SuccessCode, Ref, Format, Result)
|
||||||
catch
|
catch
|
||||||
error:not_implemented -> make_response(not_implemented, Ref);
|
error:not_implemented ->
|
||||||
|
make_response(not_implemented, Ref);
|
||||||
_:Ex:_ST ->
|
_:Ex:_ST ->
|
||||||
?SLOG(error, #{ msg => "bad_payload_format"
|
?SLOG(error, #{
|
||||||
, payload => CoapPayload
|
msg => "bad_payload_format",
|
||||||
, reason => Ex
|
payload => CoapPayload,
|
||||||
, stacktrace => _ST}),
|
reason => Ex,
|
||||||
|
stacktrace => _ST
|
||||||
|
}),
|
||||||
make_response(bad_request, Ref)
|
make_response(bad_request, Ref)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -183,67 +241,55 @@ coap_failure_to_mqtt(Ref, MsgType) ->
|
||||||
|
|
||||||
content_to_mqtt(CoapPayload, <<"text/plain">>, Ref) ->
|
content_to_mqtt(CoapPayload, <<"text/plain">>, Ref) ->
|
||||||
emqx_lwm2m_message:text_to_json(extract_path(Ref), CoapPayload);
|
emqx_lwm2m_message:text_to_json(extract_path(Ref), CoapPayload);
|
||||||
|
|
||||||
content_to_mqtt(CoapPayload, <<"application/octet-stream">>, Ref) ->
|
content_to_mqtt(CoapPayload, <<"application/octet-stream">>, Ref) ->
|
||||||
emqx_lwm2m_message:opaque_to_json(extract_path(Ref), CoapPayload);
|
emqx_lwm2m_message:opaque_to_json(extract_path(Ref), CoapPayload);
|
||||||
|
|
||||||
content_to_mqtt(CoapPayload, <<"application/vnd.oma.lwm2m+tlv">>, Ref) ->
|
content_to_mqtt(CoapPayload, <<"application/vnd.oma.lwm2m+tlv">>, Ref) ->
|
||||||
emqx_lwm2m_message:tlv_to_json(extract_path(Ref), CoapPayload);
|
emqx_lwm2m_message:tlv_to_json(extract_path(Ref), CoapPayload);
|
||||||
|
|
||||||
content_to_mqtt(CoapPayload, <<"application/vnd.oma.lwm2m+json">>, _Ref) ->
|
content_to_mqtt(CoapPayload, <<"application/vnd.oma.lwm2m+json">>, _Ref) ->
|
||||||
emqx_lwm2m_message:translate_json(CoapPayload).
|
emqx_lwm2m_message:translate_json(CoapPayload).
|
||||||
|
|
||||||
write_resp_to_mqtt({ok, changed}, _CoapPayload, Ref) ->
|
write_resp_to_mqtt({ok, changed}, _CoapPayload, Ref) ->
|
||||||
make_response(changed, Ref);
|
make_response(changed, Ref);
|
||||||
|
|
||||||
write_resp_to_mqtt({ok, content}, CoapPayload, Ref) when CoapPayload =:= <<>> ->
|
write_resp_to_mqtt({ok, content}, CoapPayload, Ref) when CoapPayload =:= <<>> ->
|
||||||
make_response(method_not_allowed, Ref);
|
make_response(method_not_allowed, Ref);
|
||||||
|
|
||||||
write_resp_to_mqtt({ok, content}, _CoapPayload, Ref) ->
|
write_resp_to_mqtt({ok, content}, _CoapPayload, Ref) ->
|
||||||
make_response(changed, Ref);
|
make_response(changed, Ref);
|
||||||
|
|
||||||
write_resp_to_mqtt({error, Error}, _CoapPayload, Ref) ->
|
write_resp_to_mqtt({error, Error}, _CoapPayload, Ref) ->
|
||||||
make_response(Error, Ref).
|
make_response(Error, Ref).
|
||||||
|
|
||||||
execute_resp_to_mqtt({ok, changed}, Ref) ->
|
execute_resp_to_mqtt({ok, changed}, Ref) ->
|
||||||
make_response(changed, Ref);
|
make_response(changed, Ref);
|
||||||
|
|
||||||
execute_resp_to_mqtt({error, Error}, Ref) ->
|
execute_resp_to_mqtt({error, Error}, Ref) ->
|
||||||
make_response(Error, Ref).
|
make_response(Error, Ref).
|
||||||
|
|
||||||
discover_resp_to_mqtt({ok, content}, CoapPayload, Ref) ->
|
discover_resp_to_mqtt({ok, content}, CoapPayload, Ref) ->
|
||||||
Links = binary:split(CoapPayload, <<",">>, [global]),
|
Links = binary:split(CoapPayload, <<",">>, [global]),
|
||||||
make_response(content, Ref, <<"application/link-format">>, Links);
|
make_response(content, Ref, <<"application/link-format">>, Links);
|
||||||
|
|
||||||
discover_resp_to_mqtt({error, Error}, _CoapPayload, Ref) ->
|
discover_resp_to_mqtt({error, Error}, _CoapPayload, Ref) ->
|
||||||
make_response(Error, Ref).
|
make_response(Error, Ref).
|
||||||
|
|
||||||
writeattr_resp_to_mqtt({ok, changed}, _CoapPayload, Ref) ->
|
writeattr_resp_to_mqtt({ok, changed}, _CoapPayload, Ref) ->
|
||||||
make_response(changed, Ref);
|
make_response(changed, Ref);
|
||||||
|
|
||||||
writeattr_resp_to_mqtt({error, Error}, _CoapPayload, Ref) ->
|
writeattr_resp_to_mqtt({error, Error}, _CoapPayload, Ref) ->
|
||||||
make_response(Error, Ref).
|
make_response(Error, Ref).
|
||||||
|
|
||||||
observe_resp_to_mqtt({error, Error}, _CoapPayload, _Format, _ObserveSeqNum, Ref) ->
|
observe_resp_to_mqtt({error, Error}, _CoapPayload, _Format, _ObserveSeqNum, Ref) ->
|
||||||
make_response(Error, Ref);
|
make_response(Error, Ref);
|
||||||
|
|
||||||
observe_resp_to_mqtt({ok, content}, CoapPayload, Format, 0, Ref) ->
|
observe_resp_to_mqtt({ok, content}, CoapPayload, Format, 0, Ref) ->
|
||||||
read_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref);
|
read_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref);
|
||||||
|
|
||||||
observe_resp_to_mqtt({ok, content}, CoapPayload, Format, ObserveSeqNum, Ref) ->
|
observe_resp_to_mqtt({ok, content}, CoapPayload, Format, ObserveSeqNum, Ref) ->
|
||||||
read_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref#{<<"seqNum">> => ObserveSeqNum}).
|
read_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref#{<<"seqNum">> => ObserveSeqNum}).
|
||||||
|
|
||||||
cancel_observe_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref) ->
|
cancel_observe_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref) ->
|
||||||
read_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref);
|
read_resp_to_mqtt({ok, content}, CoapPayload, Format, Ref);
|
||||||
|
|
||||||
cancel_observe_resp_to_mqtt({error, Error}, _CoapPayload, _Format, Ref) ->
|
cancel_observe_resp_to_mqtt({error, Error}, _CoapPayload, _Format, Ref) ->
|
||||||
make_response(Error, Ref).
|
make_response(Error, Ref).
|
||||||
|
|
||||||
make_response(Code, Ref=#{}) ->
|
make_response(Code, Ref = #{}) ->
|
||||||
BaseRsp = make_base_response(Ref),
|
BaseRsp = make_base_response(Ref),
|
||||||
make_data_response(BaseRsp, Code).
|
make_data_response(BaseRsp, Code).
|
||||||
|
|
||||||
make_response(Code, Ref=#{}, _Format, Result) ->
|
make_response(Code, Ref = #{}, _Format, Result) ->
|
||||||
BaseRsp = make_base_response(Ref),
|
BaseRsp = make_base_response(Ref),
|
||||||
make_data_response(BaseRsp, Code, _Format, Result).
|
make_data_response(BaseRsp, Code, _Format, Result).
|
||||||
|
|
||||||
|
|
@ -258,7 +304,7 @@ make_response(Code, Ref=#{}, _Format, Result) ->
|
||||||
%% <<"msgType">> => maps:get(<<"msgType">>, Ref, null)
|
%% <<"msgType">> => maps:get(<<"msgType">>, Ref, null)
|
||||||
%% }
|
%% }
|
||||||
|
|
||||||
make_base_response(Ref=#{}) ->
|
make_base_response(Ref = #{}) ->
|
||||||
remove_tmp_fields(Ref).
|
remove_tmp_fields(Ref).
|
||||||
|
|
||||||
make_data_response(BaseRsp, Code) ->
|
make_data_response(BaseRsp, Code) ->
|
||||||
|
|
@ -273,18 +319,18 @@ make_data_response(BaseRsp, Code) ->
|
||||||
make_data_response(BaseRsp, Code, _Format, Result) ->
|
make_data_response(BaseRsp, Code, _Format, Result) ->
|
||||||
BaseRsp#{
|
BaseRsp#{
|
||||||
<<"data">> =>
|
<<"data">> =>
|
||||||
#{
|
#{
|
||||||
<<"reqPath">> => extract_path(BaseRsp),
|
<<"reqPath">> => extract_path(BaseRsp),
|
||||||
<<"code">> => code(Code),
|
<<"code">> => code(Code),
|
||||||
<<"codeMsg">> => Code,
|
<<"codeMsg">> => Code,
|
||||||
<<"content">> => Result
|
<<"content">> => Result
|
||||||
}
|
}
|
||||||
}.
|
}.
|
||||||
|
|
||||||
remove_tmp_fields(Ref) ->
|
remove_tmp_fields(Ref) ->
|
||||||
maps:remove(observe_type, Ref).
|
maps:remove(observe_type, Ref).
|
||||||
|
|
||||||
-spec path_list(Path::binary()) -> {[PathWord::binary()], [Query::binary()]}.
|
-spec path_list(Path :: binary()) -> {[PathWord :: binary()], [Query :: binary()]}.
|
||||||
path_list(Path) ->
|
path_list(Path) ->
|
||||||
case binary:split(binary_util:trim(Path, $/), [<<$/>>], [global]) of
|
case binary:split(binary_util:trim(Path, $/), [<<$/>>], [global]) of
|
||||||
[ObjId, ObjInsId, ResId, LastPart] ->
|
[ObjId, ObjInsId, ResId, LastPart] ->
|
||||||
|
|
@ -304,8 +350,7 @@ path_list(Path) ->
|
||||||
query_list(PathWithQuery) ->
|
query_list(PathWithQuery) ->
|
||||||
case binary:split(PathWithQuery, [<<$?>>], []) of
|
case binary:split(PathWithQuery, [<<$?>>], []) of
|
||||||
[Path] -> {Path, []};
|
[Path] -> {Path, []};
|
||||||
[Path, Querys] ->
|
[Path, Querys] -> {Path, binary:split(Querys, [<<$&>>], [global])}
|
||||||
{Path, binary:split(Querys, [<<$&>>], [global])}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
attr_query_list(Data) ->
|
attr_query_list(Data) ->
|
||||||
|
|
@ -314,7 +359,8 @@ attr_query_list(Data) ->
|
||||||
attr_query_list(QueryJson = #{}, ValidAttrKeys, QueryList) ->
|
attr_query_list(QueryJson = #{}, ValidAttrKeys, QueryList) ->
|
||||||
maps:fold(
|
maps:fold(
|
||||||
fun
|
fun
|
||||||
(_K, null, Acc) -> Acc;
|
(_K, null, Acc) ->
|
||||||
|
Acc;
|
||||||
(K, V, Acc) ->
|
(K, V, Acc) ->
|
||||||
case lists:member(K, ValidAttrKeys) of
|
case lists:member(K, ValidAttrKeys) of
|
||||||
true ->
|
true ->
|
||||||
|
|
@ -323,7 +369,10 @@ attr_query_list(QueryJson = #{}, ValidAttrKeys, QueryList) ->
|
||||||
false ->
|
false ->
|
||||||
Acc
|
Acc
|
||||||
end
|
end
|
||||||
end, QueryList, QueryJson).
|
end,
|
||||||
|
QueryList,
|
||||||
|
QueryJson
|
||||||
|
).
|
||||||
|
|
||||||
valid_attr_keys() ->
|
valid_attr_keys() ->
|
||||||
[<<"pmin">>, <<"pmax">>, <<"gt">>, <<"lt">>, <<"st">>].
|
[<<"pmin">>, <<"pmax">>, <<"gt">>, <<"lt">>, <<"st">>].
|
||||||
|
|
@ -332,11 +381,10 @@ data_format(Options) ->
|
||||||
maps:get(content_format, Options, <<"text/plain">>).
|
maps:get(content_format, Options, <<"text/plain">>).
|
||||||
|
|
||||||
observe_seq(Options) ->
|
observe_seq(Options) ->
|
||||||
maps:get(observe, Options, rand:uniform(1000000) + 1 ).
|
maps:get(observe, Options, rand:uniform(1000000) + 1).
|
||||||
|
|
||||||
add_alternate_path_prefix(<<"/">>, PathList) ->
|
add_alternate_path_prefix(<<"/">>, PathList) ->
|
||||||
PathList;
|
PathList;
|
||||||
|
|
||||||
add_alternate_path_prefix(AlternatePath, PathList) ->
|
add_alternate_path_prefix(AlternatePath, PathList) ->
|
||||||
[binary_util:trim(AlternatePath, $/) | PathList].
|
[binary_util:trim(AlternatePath, $/) | PathList].
|
||||||
|
|
||||||
|
|
@ -350,22 +398,29 @@ extract_path(Ref = #{}) ->
|
||||||
end;
|
end;
|
||||||
#{<<"path">> := Path} ->
|
#{<<"path">> := Path} ->
|
||||||
Path
|
Path
|
||||||
end).
|
end
|
||||||
|
).
|
||||||
|
|
||||||
batch_write_request(AlternatePath, BasePath, Content) ->
|
batch_write_request(AlternatePath, BasePath, Content) ->
|
||||||
{PathList, QueryList} = path_list(BasePath),
|
{PathList, QueryList} = path_list(BasePath),
|
||||||
Method = case length(PathList) of
|
Method =
|
||||||
2 -> post;
|
case length(PathList) of
|
||||||
3 -> put
|
2 -> post;
|
||||||
end,
|
3 -> put
|
||||||
|
end,
|
||||||
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
FullPathList = add_alternate_path_prefix(AlternatePath, PathList),
|
||||||
TlvData = emqx_lwm2m_message:json_to_tlv(PathList, Content),
|
TlvData = emqx_lwm2m_message:json_to_tlv(PathList, Content),
|
||||||
Payload = emqx_lwm2m_tlv:encode(TlvData),
|
Payload = emqx_lwm2m_tlv:encode(TlvData),
|
||||||
emqx_coap_message:request(con, Method, Payload,
|
emqx_coap_message:request(
|
||||||
[{uri_path, FullPathList},
|
con,
|
||||||
{uri_query, QueryList},
|
Method,
|
||||||
{content_format, <<"application/vnd.oma.lwm2m+tlv">>}]).
|
Payload,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{content_format, <<"application/vnd.oma.lwm2m+tlv">>}
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
||||||
single_write_request(AlternatePath, Data) ->
|
single_write_request(AlternatePath, Data) ->
|
||||||
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
{PathList, QueryList} = path_list(maps:get(<<"path">>, Data)),
|
||||||
|
|
@ -373,10 +428,16 @@ single_write_request(AlternatePath, Data) ->
|
||||||
%% TO DO: handle write to resource instance, e.g. /4/0/1/0
|
%% TO DO: handle write to resource instance, e.g. /4/0/1/0
|
||||||
TlvData = emqx_lwm2m_message:json_to_tlv(PathList, [Data]),
|
TlvData = emqx_lwm2m_message:json_to_tlv(PathList, [Data]),
|
||||||
Payload = emqx_lwm2m_tlv:encode(TlvData),
|
Payload = emqx_lwm2m_tlv:encode(TlvData),
|
||||||
emqx_coap_message:request(con, put, Payload,
|
emqx_coap_message:request(
|
||||||
[{uri_path, FullPathList},
|
con,
|
||||||
{uri_query, QueryList},
|
put,
|
||||||
{content_format, <<"application/vnd.oma.lwm2m+tlv">>}]).
|
Payload,
|
||||||
|
[
|
||||||
|
{uri_path, FullPathList},
|
||||||
|
{uri_query, QueryList},
|
||||||
|
{content_format, <<"application/vnd.oma.lwm2m+tlv">>}
|
||||||
|
]
|
||||||
|
).
|
||||||
|
|
||||||
drop_query(Path) ->
|
drop_query(Path) ->
|
||||||
case binary:split(Path, [<<$?>>]) of
|
case binary:split(Path, [<<$?>>]) of
|
||||||
|
|
|
||||||
|
|
@ -22,57 +22,72 @@
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
%% APIs
|
%% APIs
|
||||||
-export([ reg/0
|
-export([
|
||||||
, unreg/0
|
reg/0,
|
||||||
]).
|
unreg/0
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ on_gateway_load/2
|
-export([
|
||||||
, on_gateway_update/3
|
on_gateway_load/2,
|
||||||
, on_gateway_unload/2
|
on_gateway_update/3,
|
||||||
]).
|
on_gateway_unload/2
|
||||||
|
]).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% APIs
|
%% APIs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
reg() ->
|
reg() ->
|
||||||
RegistryOptions = [ {cbkmod, ?MODULE}
|
RegistryOptions = [{cbkmod, ?MODULE}],
|
||||||
],
|
|
||||||
emqx_gateway_registry:reg(lwm2m, RegistryOptions).
|
emqx_gateway_registry:reg(lwm2m, RegistryOptions).
|
||||||
|
|
||||||
unreg() ->
|
unreg() ->
|
||||||
emqx_gateway_registry:unreg(lwm2m).
|
emqx_gateway_registry:unreg(lwm2m).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% emqx_gateway_registry callbacks
|
%% emqx_gateway_registry callbacks
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
on_gateway_load(_Gateway = #{ name := GwName,
|
on_gateway_load(
|
||||||
config := Config
|
_Gateway = #{
|
||||||
}, Ctx) ->
|
name := GwName,
|
||||||
|
config := Config
|
||||||
|
},
|
||||||
|
Ctx
|
||||||
|
) ->
|
||||||
XmlDir = maps:get(xml_dir, Config),
|
XmlDir = maps:get(xml_dir, Config),
|
||||||
case emqx_lwm2m_xml_object_db:start_link(XmlDir) of
|
case emqx_lwm2m_xml_object_db:start_link(XmlDir) of
|
||||||
{ok, RegPid} ->
|
{ok, RegPid} ->
|
||||||
Listeners = emqx_gateway_utils:normalize_config(Config),
|
Listeners = emqx_gateway_utils:normalize_config(Config),
|
||||||
ModCfg = #{frame_mod => emqx_coap_frame,
|
ModCfg = #{
|
||||||
chann_mod => emqx_lwm2m_channel
|
frame_mod => emqx_coap_frame,
|
||||||
},
|
chann_mod => emqx_lwm2m_channel
|
||||||
case emqx_gateway_utils:start_listeners(
|
},
|
||||||
Listeners, GwName, Ctx, ModCfg) of
|
case
|
||||||
|
emqx_gateway_utils:start_listeners(
|
||||||
|
Listeners, GwName, Ctx, ModCfg
|
||||||
|
)
|
||||||
|
of
|
||||||
{ok, ListenerPids} ->
|
{ok, ListenerPids} ->
|
||||||
{ok, ListenerPids, #{ctx => Ctx, registry => RegPid}};
|
{ok, ListenerPids, #{ctx => Ctx, registry => RegPid}};
|
||||||
{error, {Reason, Listener}} ->
|
{error, {Reason, Listener}} ->
|
||||||
_ = emqx_lwm2m_xml_object_db:stop(),
|
_ = emqx_lwm2m_xml_object_db:stop(),
|
||||||
throw({badconf, #{ key => listeners
|
throw(
|
||||||
, vallue => Listener
|
{badconf, #{
|
||||||
, reason => Reason
|
key => listeners,
|
||||||
}})
|
vallue => Listener,
|
||||||
|
reason => Reason
|
||||||
|
}}
|
||||||
|
)
|
||||||
end;
|
end;
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
throw({badconf, #{ key => xml_dir
|
throw(
|
||||||
, value => XmlDir
|
{badconf, #{
|
||||||
, reason => Reason
|
key => xml_dir,
|
||||||
}})
|
value => XmlDir,
|
||||||
|
reason => Reason
|
||||||
|
}}
|
||||||
|
)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
||||||
|
|
@ -83,16 +98,27 @@ on_gateway_update(Config, Gateway, GwState = #{ctx := Ctx}) ->
|
||||||
on_gateway_unload(Gateway, GwState),
|
on_gateway_unload(Gateway, GwState),
|
||||||
on_gateway_load(Gateway#{config => Config}, Ctx)
|
on_gateway_load(Gateway#{config => Config}, Ctx)
|
||||||
catch
|
catch
|
||||||
Class : Reason : Stk ->
|
Class:Reason:Stk ->
|
||||||
logger:error("Failed to update ~ts; "
|
logger:error(
|
||||||
"reason: {~0p, ~0p} stacktrace: ~0p",
|
"Failed to update ~ts; "
|
||||||
[GwName, Class, Reason, Stk]),
|
"reason: {~0p, ~0p} stacktrace: ~0p",
|
||||||
|
[GwName, Class, Reason, Stk]
|
||||||
|
),
|
||||||
{error, Reason}
|
{error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
on_gateway_unload(_Gateway = #{ name := GwName,
|
on_gateway_unload(
|
||||||
config := Config
|
_Gateway = #{
|
||||||
}, _GwState = #{registry := _RegPid}) ->
|
name := GwName,
|
||||||
_ = try emqx_lwm2m_xml_object_db:stop() catch _ : _ -> ok end,
|
config := Config
|
||||||
|
},
|
||||||
|
_GwState = #{registry := _RegPid}
|
||||||
|
) ->
|
||||||
|
_ =
|
||||||
|
try
|
||||||
|
emqx_lwm2m_xml_object_db:stop()
|
||||||
|
catch
|
||||||
|
_:_ -> ok
|
||||||
|
end,
|
||||||
Listeners = emqx_gateway_utils:normalize_config(Config),
|
Listeners = emqx_gateway_utils:normalize_config(Config),
|
||||||
emqx_gateway_utils:stop_listeners(GwName, Listeners).
|
emqx_gateway_utils:stop_listeners(GwName, Listeners).
|
||||||
|
|
|
||||||
|
|
@ -16,12 +16,13 @@
|
||||||
|
|
||||||
-module(emqx_lwm2m_message).
|
-module(emqx_lwm2m_message).
|
||||||
|
|
||||||
-export([ tlv_to_json/2
|
-export([
|
||||||
, json_to_tlv/2
|
tlv_to_json/2,
|
||||||
, text_to_json/2
|
json_to_tlv/2,
|
||||||
, opaque_to_json/2
|
text_to_json/2,
|
||||||
, translate_json/1
|
opaque_to_json/2,
|
||||||
]).
|
translate_json/1
|
||||||
|
]).
|
||||||
|
|
||||||
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
||||||
|
|
||||||
|
|
@ -30,58 +31,86 @@ tlv_to_json(BaseName, TlvData) ->
|
||||||
ObjectId = object_id(BaseName),
|
ObjectId = object_id(BaseName),
|
||||||
ObjDefinition = emqx_lwm2m_xml_object:get_obj_def(ObjectId, true),
|
ObjDefinition = emqx_lwm2m_xml_object:get_obj_def(ObjectId, true),
|
||||||
case DecodedTlv of
|
case DecodedTlv of
|
||||||
[#{tlv_resource_with_value:=Id, value:=Value}] ->
|
[#{tlv_resource_with_value := Id, value := Value}] ->
|
||||||
TrueBaseName = basename(BaseName, undefined, undefined, Id, 3),
|
TrueBaseName = basename(BaseName, undefined, undefined, Id, 3),
|
||||||
tlv_single_resource(TrueBaseName, Id, Value, ObjDefinition);
|
tlv_single_resource(TrueBaseName, Id, Value, ObjDefinition);
|
||||||
List1 = [#{tlv_resource_with_value:=_Id}, _|_] ->
|
List1 = [#{tlv_resource_with_value := _Id}, _ | _] ->
|
||||||
TrueBaseName = basename(BaseName, undefined, undefined, undefined, 2),
|
TrueBaseName = basename(BaseName, undefined, undefined, undefined, 2),
|
||||||
tlv_level2(TrueBaseName, List1, ObjDefinition, []);
|
tlv_level2(TrueBaseName, List1, ObjDefinition, []);
|
||||||
List2 = [#{tlv_multiple_resource:=_Id}|_] ->
|
List2 = [#{tlv_multiple_resource := _Id} | _] ->
|
||||||
TrueBaseName = basename(BaseName, undefined, undefined, undefined, 2),
|
TrueBaseName = basename(BaseName, undefined, undefined, undefined, 2),
|
||||||
tlv_level2(TrueBaseName, List2, ObjDefinition, []);
|
tlv_level2(TrueBaseName, List2, ObjDefinition, []);
|
||||||
[#{tlv_object_instance:=Id, value:=Value}] ->
|
[#{tlv_object_instance := Id, value := Value}] ->
|
||||||
TrueBaseName = basename(BaseName, undefined, Id, undefined, 2),
|
TrueBaseName = basename(BaseName, undefined, Id, undefined, 2),
|
||||||
tlv_level2(TrueBaseName, Value, ObjDefinition, []);
|
tlv_level2(TrueBaseName, Value, ObjDefinition, []);
|
||||||
List3=[#{tlv_object_instance:=_Id}, _|_] ->
|
List3 = [#{tlv_object_instance := _Id}, _ | _] ->
|
||||||
tlv_level1(integer_to_binary(ObjectId), List3, ObjDefinition, [])
|
tlv_level1(integer_to_binary(ObjectId), List3, ObjDefinition, [])
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
tlv_level1(_Path, [], _ObjDefinition, Acc) ->
|
tlv_level1(_Path, [], _ObjDefinition, Acc) ->
|
||||||
Acc;
|
Acc;
|
||||||
tlv_level1(Path, [#{tlv_object_instance:=Id, value:=Value}|T], ObjDefinition, Acc) ->
|
tlv_level1(Path, [#{tlv_object_instance := Id, value := Value} | T], ObjDefinition, Acc) ->
|
||||||
New = tlv_level2(make_path(Path, Id), Value, ObjDefinition, []),
|
New = tlv_level2(make_path(Path, Id), Value, ObjDefinition, []),
|
||||||
tlv_level1(Path, T, ObjDefinition, Acc++New).
|
tlv_level1(Path, T, ObjDefinition, Acc ++ New).
|
||||||
|
|
||||||
tlv_level2(_, [], _, Acc) ->
|
tlv_level2(_, [], _, Acc) ->
|
||||||
Acc;
|
Acc;
|
||||||
tlv_level2(RelativePath, [#{tlv_resource_with_value:=ResourceId, value:=Value}|T], ObjDefinition, Acc) ->
|
tlv_level2(
|
||||||
|
RelativePath, [#{tlv_resource_with_value := ResourceId, value := Value} | T], ObjDefinition, Acc
|
||||||
|
) ->
|
||||||
Val = value(Value, ResourceId, ObjDefinition),
|
Val = value(Value, ResourceId, ObjDefinition),
|
||||||
New = #{path => make_path(RelativePath, ResourceId),
|
New = #{
|
||||||
value=>Val},
|
path => make_path(RelativePath, ResourceId),
|
||||||
tlv_level2(RelativePath, T, ObjDefinition, Acc++[New]);
|
value => Val
|
||||||
tlv_level2(RelativePath, [#{tlv_multiple_resource:=ResourceId, value:=Value}|T], ObjDefinition, Acc) ->
|
},
|
||||||
SubList = tlv_level3(make_path(RelativePath, ResourceId),
|
tlv_level2(RelativePath, T, ObjDefinition, Acc ++ [New]);
|
||||||
Value, ResourceId, ObjDefinition, []),
|
tlv_level2(
|
||||||
tlv_level2(RelativePath, T, ObjDefinition, Acc++SubList).
|
RelativePath, [#{tlv_multiple_resource := ResourceId, value := Value} | T], ObjDefinition, Acc
|
||||||
|
) ->
|
||||||
|
SubList = tlv_level3(
|
||||||
|
make_path(RelativePath, ResourceId),
|
||||||
|
Value,
|
||||||
|
ResourceId,
|
||||||
|
ObjDefinition,
|
||||||
|
[]
|
||||||
|
),
|
||||||
|
tlv_level2(RelativePath, T, ObjDefinition, Acc ++ SubList).
|
||||||
|
|
||||||
tlv_level3(_RelativePath, [], _Id, _ObjDefinition, Acc) ->
|
tlv_level3(_RelativePath, [], _Id, _ObjDefinition, Acc) ->
|
||||||
lists:reverse(Acc);
|
lists:reverse(Acc);
|
||||||
tlv_level3(RelativePath, [#{tlv_resource_instance:=InsId, value:=Value}|T], ResourceId, ObjDefinition, Acc) ->
|
tlv_level3(
|
||||||
|
RelativePath,
|
||||||
|
[#{tlv_resource_instance := InsId, value := Value} | T],
|
||||||
|
ResourceId,
|
||||||
|
ObjDefinition,
|
||||||
|
Acc
|
||||||
|
) ->
|
||||||
Val = value(Value, ResourceId, ObjDefinition),
|
Val = value(Value, ResourceId, ObjDefinition),
|
||||||
New = #{path => make_path(RelativePath, InsId),
|
New = #{
|
||||||
value=>Val},
|
path => make_path(RelativePath, InsId),
|
||||||
tlv_level3(RelativePath, T, ResourceId, ObjDefinition, [New|Acc]).
|
value => Val
|
||||||
|
},
|
||||||
|
tlv_level3(RelativePath, T, ResourceId, ObjDefinition, [New | Acc]).
|
||||||
|
|
||||||
tlv_single_resource(BaseName, Id, Value, ObjDefinition) ->
|
tlv_single_resource(BaseName, Id, Value, ObjDefinition) ->
|
||||||
Val = value(Value, Id, ObjDefinition),
|
Val = value(Value, Id, ObjDefinition),
|
||||||
[#{path=>BaseName, value=>Val}].
|
[#{path => BaseName, value => Val}].
|
||||||
|
|
||||||
basename(OldBaseName, _ObjectId, ObjectInstanceId, ResourceId, 3) ->
|
basename(OldBaseName, _ObjectId, ObjectInstanceId, ResourceId, 3) ->
|
||||||
case binary:split(binary_util:trim(OldBaseName, $/), [<<$/>>], [global]) of
|
case binary:split(binary_util:trim(OldBaseName, $/), [<<$/>>], [global]) of
|
||||||
[ObjId, ObjInsId, ResId] -> <<$/, ObjId/binary, $/, ObjInsId/binary, $/, ResId/binary>>;
|
[ObjId, ObjInsId, ResId] ->
|
||||||
[ObjId, ObjInsId] -> <<$/, ObjId/binary, $/, ObjInsId/binary, $/, (integer_to_binary(ResourceId))/binary>>;
|
<<$/, ObjId/binary, $/, ObjInsId/binary, $/, ResId/binary>>;
|
||||||
[ObjId] -> <<$/, ObjId/binary, $/, (integer_to_binary(ObjectInstanceId))/binary, $/, (integer_to_binary(ResourceId))/binary>>
|
[ObjId, ObjInsId] ->
|
||||||
|
<<$/, ObjId/binary, $/, ObjInsId/binary, $/, (integer_to_binary(ResourceId))/binary>>;
|
||||||
|
[ObjId] ->
|
||||||
|
<<
|
||||||
|
$/,
|
||||||
|
ObjId/binary,
|
||||||
|
$/,
|
||||||
|
(integer_to_binary(ObjectInstanceId))/binary,
|
||||||
|
$/,
|
||||||
|
(integer_to_binary(ResourceId))/binary
|
||||||
|
>>
|
||||||
end;
|
end;
|
||||||
basename(OldBaseName, _ObjectId, ObjectInstanceId, _ResourceId, 2) ->
|
basename(OldBaseName, _ObjectId, ObjectInstanceId, _ResourceId, 2) ->
|
||||||
case binary:split(binary_util:trim(OldBaseName, $/), [<<$/>>], [global]) of
|
case binary:split(binary_util:trim(OldBaseName, $/), [<<$/>>], [global]) of
|
||||||
|
|
@ -101,16 +130,18 @@ make_path(RelativePath, Id) ->
|
||||||
|
|
||||||
object_id(BaseName) ->
|
object_id(BaseName) ->
|
||||||
case binary:split(binary_util:trim(BaseName, $/), [<<$/>>], [global]) of
|
case binary:split(binary_util:trim(BaseName, $/), [<<$/>>], [global]) of
|
||||||
[ObjId] -> binary_to_integer(ObjId);
|
[ObjId] -> binary_to_integer(ObjId);
|
||||||
[ObjId, _] -> binary_to_integer(ObjId);
|
[ObjId, _] -> binary_to_integer(ObjId);
|
||||||
[ObjId, _, _] -> binary_to_integer(ObjId);
|
[ObjId, _, _] -> binary_to_integer(ObjId);
|
||||||
[ObjId, _, _, _] -> binary_to_integer(ObjId)
|
[ObjId, _, _, _] -> binary_to_integer(ObjId)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
object_resource_id(BaseName) ->
|
object_resource_id(BaseName) ->
|
||||||
case binary:split(binary_util:trim(BaseName, $/), [<<$/>>], [global]) of
|
case binary:split(binary_util:trim(BaseName, $/), [<<$/>>], [global]) of
|
||||||
[_ObjIdBin1] -> error({invalid_basename, BaseName});
|
[_ObjIdBin1] ->
|
||||||
[_ObjIdBin2, _] -> error({invalid_basename, BaseName});
|
error({invalid_basename, BaseName});
|
||||||
|
[_ObjIdBin2, _] ->
|
||||||
|
error({invalid_basename, BaseName});
|
||||||
[ObjIdBin3, _, ResourceId3] ->
|
[ObjIdBin3, _, ResourceId3] ->
|
||||||
{binary_to_integer(ObjIdBin3), binary_to_integer(ResourceId3)};
|
{binary_to_integer(ObjIdBin3), binary_to_integer(ResourceId3)};
|
||||||
[ObjIdBin3, _, ResourceId3, _] ->
|
[ObjIdBin3, _, ResourceId3, _] ->
|
||||||
|
|
@ -121,17 +152,19 @@ object_resource_id(BaseName) ->
|
||||||
value(Value, ResourceId, ObjDefinition) ->
|
value(Value, ResourceId, ObjDefinition) ->
|
||||||
case emqx_lwm2m_xml_object:get_resource_type(ResourceId, ObjDefinition) of
|
case emqx_lwm2m_xml_object:get_resource_type(ResourceId, ObjDefinition) of
|
||||||
"String" ->
|
"String" ->
|
||||||
Value; % keep binary type since it is same as a string for jsx
|
% keep binary type since it is same as a string for jsx
|
||||||
|
Value;
|
||||||
"Integer" ->
|
"Integer" ->
|
||||||
Size = byte_size(Value)*8,
|
Size = byte_size(Value) * 8,
|
||||||
<<IntResult:Size/signed>> = Value,
|
<<IntResult:Size/signed>> = Value,
|
||||||
IntResult;
|
IntResult;
|
||||||
"Float" ->
|
"Float" ->
|
||||||
Size = byte_size(Value)*8,
|
Size = byte_size(Value) * 8,
|
||||||
<<FloatResult:Size/float>> = Value,
|
<<FloatResult:Size/float>> = Value,
|
||||||
FloatResult;
|
FloatResult;
|
||||||
"Boolean" ->
|
"Boolean" ->
|
||||||
B = case Value of
|
B =
|
||||||
|
case Value of
|
||||||
<<0>> -> false;
|
<<0>> -> false;
|
||||||
<<1>> -> true
|
<<1>> -> true
|
||||||
end,
|
end,
|
||||||
|
|
@ -139,7 +172,7 @@ value(Value, ResourceId, ObjDefinition) ->
|
||||||
"Opaque" ->
|
"Opaque" ->
|
||||||
base64:encode(Value);
|
base64:encode(Value);
|
||||||
"Time" ->
|
"Time" ->
|
||||||
Size = byte_size(Value)*8,
|
Size = byte_size(Value) * 8,
|
||||||
<<IntResult:Size>> = Value,
|
<<IntResult:Size>> = Value,
|
||||||
IntResult;
|
IntResult;
|
||||||
"Objlnk" ->
|
"Objlnk" ->
|
||||||
|
|
@ -149,8 +182,12 @@ value(Value, ResourceId, ObjDefinition) ->
|
||||||
|
|
||||||
json_to_tlv([_ObjectId, _ObjectInstanceId, ResourceId], ResourceArray) ->
|
json_to_tlv([_ObjectId, _ObjectInstanceId, ResourceId], ResourceArray) ->
|
||||||
case length(ResourceArray) of
|
case length(ResourceArray) of
|
||||||
1 -> element_single_resource(integer(ResourceId), ResourceArray);
|
1 ->
|
||||||
_ -> element_loop_level4(ResourceArray, [#{tlv_multiple_resource=>integer(ResourceId), value=>[]}])
|
element_single_resource(integer(ResourceId), ResourceArray);
|
||||||
|
_ ->
|
||||||
|
element_loop_level4(ResourceArray, [
|
||||||
|
#{tlv_multiple_resource => integer(ResourceId), value => []}
|
||||||
|
])
|
||||||
end;
|
end;
|
||||||
json_to_tlv([_ObjectId, _ObjectInstanceId], ResourceArray) ->
|
json_to_tlv([_ObjectId, _ObjectInstanceId], ResourceArray) ->
|
||||||
element_loop_level3(ResourceArray, []);
|
element_loop_level3(ResourceArray, []);
|
||||||
|
|
@ -159,23 +196,23 @@ json_to_tlv([_ObjectId], ResourceArray) ->
|
||||||
|
|
||||||
element_single_resource(ResourceId, [#{<<"type">> := Type, <<"value">> := Value}]) ->
|
element_single_resource(ResourceId, [#{<<"type">> := Type, <<"value">> := Value}]) ->
|
||||||
BinaryValue = value_ex(Type, Value),
|
BinaryValue = value_ex(Type, Value),
|
||||||
[#{tlv_resource_with_value=>integer(ResourceId), value=>BinaryValue}].
|
[#{tlv_resource_with_value => integer(ResourceId), value => BinaryValue}].
|
||||||
|
|
||||||
element_loop_level2([], Acc) ->
|
element_loop_level2([], Acc) ->
|
||||||
Acc;
|
Acc;
|
||||||
element_loop_level2([H|T], Acc) ->
|
element_loop_level2([H | T], Acc) ->
|
||||||
NewAcc = insert(object, H, Acc),
|
NewAcc = insert(object, H, Acc),
|
||||||
element_loop_level2(T, NewAcc).
|
element_loop_level2(T, NewAcc).
|
||||||
|
|
||||||
element_loop_level3([], Acc) ->
|
element_loop_level3([], Acc) ->
|
||||||
Acc;
|
Acc;
|
||||||
element_loop_level3([H|T], Acc) ->
|
element_loop_level3([H | T], Acc) ->
|
||||||
NewAcc = insert(object_instance, H, Acc),
|
NewAcc = insert(object_instance, H, Acc),
|
||||||
element_loop_level3(T, NewAcc).
|
element_loop_level3(T, NewAcc).
|
||||||
|
|
||||||
element_loop_level4([], Acc) ->
|
element_loop_level4([], Acc) ->
|
||||||
Acc;
|
Acc;
|
||||||
element_loop_level4([H|T], Acc) ->
|
element_loop_level4([H | T], Acc) ->
|
||||||
NewAcc = insert(resource, H, Acc),
|
NewAcc = insert(resource, H, Acc),
|
||||||
element_loop_level4(T, NewAcc).
|
element_loop_level4(T, NewAcc).
|
||||||
|
|
||||||
|
|
@ -183,15 +220,14 @@ insert(Level, #{<<"path">> := EleName, <<"type">> := Type, <<"value">> := Value}
|
||||||
BinaryValue = value_ex(Type, Value),
|
BinaryValue = value_ex(Type, Value),
|
||||||
Path = split_path(EleName),
|
Path = split_path(EleName),
|
||||||
case Level of
|
case Level of
|
||||||
object -> insert_resource_into_object(Path, BinaryValue, Acc);
|
object -> insert_resource_into_object(Path, BinaryValue, Acc);
|
||||||
object_instance -> insert_resource_into_object_instance(Path, BinaryValue, Acc);
|
object_instance -> insert_resource_into_object_instance(Path, BinaryValue, Acc);
|
||||||
resource -> insert_resource_instance_into_resource(hd(Path), BinaryValue, Acc)
|
resource -> insert_resource_instance_into_resource(hd(Path), BinaryValue, Acc)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
% json text to TLV binary
|
% json text to TLV binary
|
||||||
value_ex(K, Value) when K =:= <<"Integer">>; K =:= <<"Float">>; K =:= <<"Time">> ->
|
value_ex(K, Value) when K =:= <<"Integer">>; K =:= <<"Float">>; K =:= <<"Time">> ->
|
||||||
encode_number(Value);
|
encode_number(Value);
|
||||||
|
|
||||||
value_ex(K, Value) when K =:= <<"String">> ->
|
value_ex(K, Value) when K =:= <<"String">> ->
|
||||||
Value;
|
Value;
|
||||||
value_ex(K, Value) when K =:= <<"Opaque">> ->
|
value_ex(K, Value) when K =:= <<"Opaque">> ->
|
||||||
|
|
@ -201,34 +237,33 @@ value_ex(K, Value) when K =:= <<"Opaque">> ->
|
||||||
base64:decode(Value);
|
base64:decode(Value);
|
||||||
value_ex(K, <<"true">>) when K =:= <<"Boolean">> -> <<1>>;
|
value_ex(K, <<"true">>) when K =:= <<"Boolean">> -> <<1>>;
|
||||||
value_ex(K, <<"false">>) when K =:= <<"Boolean">> -> <<0>>;
|
value_ex(K, <<"false">>) when K =:= <<"Boolean">> -> <<0>>;
|
||||||
|
|
||||||
value_ex(K, Value) when K =:= <<"Objlnk">>; K =:= ov ->
|
value_ex(K, Value) when K =:= <<"Objlnk">>; K =:= ov ->
|
||||||
[P1, P2] = binary:split(Value, [<<$:>>], [global]),
|
[P1, P2] = binary:split(Value, [<<$:>>], [global]),
|
||||||
<<(binary_to_integer(P1)):16, (binary_to_integer(P2)):16>>.
|
<<(binary_to_integer(P1)):16, (binary_to_integer(P2)):16>>.
|
||||||
|
|
||||||
insert_resource_into_object([ObjectInstanceId|OtherIds], Value, Acc) ->
|
insert_resource_into_object([ObjectInstanceId | OtherIds], Value, Acc) ->
|
||||||
case find_obj_instance(ObjectInstanceId, Acc) of
|
case find_obj_instance(ObjectInstanceId, Acc) of
|
||||||
undefined ->
|
undefined ->
|
||||||
NewList = insert_resource_into_object_instance(OtherIds, Value, []),
|
NewList = insert_resource_into_object_instance(OtherIds, Value, []),
|
||||||
Acc ++ [#{tlv_object_instance=>integer(ObjectInstanceId), value=>NewList}];
|
Acc ++ [#{tlv_object_instance => integer(ObjectInstanceId), value => NewList}];
|
||||||
ObjectInstance = #{value:=List} ->
|
ObjectInstance = #{value := List} ->
|
||||||
NewList = insert_resource_into_object_instance(OtherIds, Value, List),
|
NewList = insert_resource_into_object_instance(OtherIds, Value, List),
|
||||||
Acc2 = lists:delete(ObjectInstance, Acc),
|
Acc2 = lists:delete(ObjectInstance, Acc),
|
||||||
Acc2 ++ [ObjectInstance#{value=>NewList}]
|
Acc2 ++ [ObjectInstance#{value => NewList}]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
insert_resource_into_object_instance([ResourceId, ResourceInstanceId], Value, Acc) ->
|
insert_resource_into_object_instance([ResourceId, ResourceInstanceId], Value, Acc) ->
|
||||||
case find_resource(ResourceId, Acc) of
|
case find_resource(ResourceId, Acc) of
|
||||||
undefined ->
|
undefined ->
|
||||||
NewList = insert_resource_instance_into_resource(ResourceInstanceId, Value, []),
|
NewList = insert_resource_instance_into_resource(ResourceInstanceId, Value, []),
|
||||||
Acc++[#{tlv_multiple_resource=>integer(ResourceId), value=>NewList}];
|
Acc ++ [#{tlv_multiple_resource => integer(ResourceId), value => NewList}];
|
||||||
Resource = #{value:=List}->
|
Resource = #{value := List} ->
|
||||||
NewList = insert_resource_instance_into_resource(ResourceInstanceId, Value, List),
|
NewList = insert_resource_instance_into_resource(ResourceInstanceId, Value, List),
|
||||||
Acc2 = lists:delete(Resource, Acc),
|
Acc2 = lists:delete(Resource, Acc),
|
||||||
Acc2 ++ [Resource#{value=>NewList}]
|
Acc2 ++ [Resource#{value => NewList}]
|
||||||
end;
|
end;
|
||||||
insert_resource_into_object_instance([ResourceId], Value, Acc) ->
|
insert_resource_into_object_instance([ResourceId], Value, Acc) ->
|
||||||
NewMap = #{tlv_resource_with_value=>integer(ResourceId), value=>Value},
|
NewMap = #{tlv_resource_with_value => integer(ResourceId), value => Value},
|
||||||
case find_resource(ResourceId, Acc) of
|
case find_resource(ResourceId, Acc) of
|
||||||
undefined ->
|
undefined ->
|
||||||
Acc ++ [NewMap];
|
Acc ++ [NewMap];
|
||||||
|
|
@ -238,7 +273,7 @@ insert_resource_into_object_instance([ResourceId], Value, Acc) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
insert_resource_instance_into_resource(ResourceInstanceId, Value, Acc) ->
|
insert_resource_instance_into_resource(ResourceInstanceId, Value, Acc) ->
|
||||||
NewMap = #{tlv_resource_instance=>integer(ResourceInstanceId), value=>Value},
|
NewMap = #{tlv_resource_instance => integer(ResourceInstanceId), value => Value},
|
||||||
case find_resource_instance(ResourceInstanceId, Acc) of
|
case find_resource_instance(ResourceInstanceId, Acc) of
|
||||||
undefined ->
|
undefined ->
|
||||||
Acc ++ [NewMap];
|
Acc ++ [NewMap];
|
||||||
|
|
@ -247,28 +282,27 @@ insert_resource_instance_into_resource(ResourceInstanceId, Value, Acc) ->
|
||||||
Acc2 ++ [NewMap]
|
Acc2 ++ [NewMap]
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
find_obj_instance(_ObjectInstanceId, []) ->
|
find_obj_instance(_ObjectInstanceId, []) ->
|
||||||
undefined;
|
undefined;
|
||||||
find_obj_instance(ObjectInstanceId, [H=#{tlv_object_instance:=ObjectInstanceId}|_T]) ->
|
find_obj_instance(ObjectInstanceId, [H = #{tlv_object_instance := ObjectInstanceId} | _T]) ->
|
||||||
H;
|
H;
|
||||||
find_obj_instance(ObjectInstanceId, [_|T]) ->
|
find_obj_instance(ObjectInstanceId, [_ | T]) ->
|
||||||
find_obj_instance(ObjectInstanceId, T).
|
find_obj_instance(ObjectInstanceId, T).
|
||||||
|
|
||||||
find_resource(_ResourceId, []) ->
|
find_resource(_ResourceId, []) ->
|
||||||
undefined;
|
undefined;
|
||||||
find_resource(ResourceId, [H=#{tlv_resource_with_value:=ResourceId}|_T]) ->
|
find_resource(ResourceId, [H = #{tlv_resource_with_value := ResourceId} | _T]) ->
|
||||||
H;
|
H;
|
||||||
find_resource(ResourceId, [H=#{tlv_multiple_resource:=ResourceId}|_T]) ->
|
find_resource(ResourceId, [H = #{tlv_multiple_resource := ResourceId} | _T]) ->
|
||||||
H;
|
H;
|
||||||
find_resource(ResourceId, [_|T]) ->
|
find_resource(ResourceId, [_ | T]) ->
|
||||||
find_resource(ResourceId, T).
|
find_resource(ResourceId, T).
|
||||||
|
|
||||||
find_resource_instance(_ResourceInstanceId, []) ->
|
find_resource_instance(_ResourceInstanceId, []) ->
|
||||||
undefined;
|
undefined;
|
||||||
find_resource_instance(ResourceInstanceId, [H=#{tlv_resource_instance:=ResourceInstanceId}|_T]) ->
|
find_resource_instance(ResourceInstanceId, [H = #{tlv_resource_instance := ResourceInstanceId} | _T]) ->
|
||||||
H;
|
H;
|
||||||
find_resource_instance(ResourceInstanceId, [_|T]) ->
|
find_resource_instance(ResourceInstanceId, [_ | T]) ->
|
||||||
find_resource_instance(ResourceInstanceId, T).
|
find_resource_instance(ResourceInstanceId, T).
|
||||||
|
|
||||||
split_path(Path) ->
|
split_path(Path) ->
|
||||||
|
|
@ -277,10 +311,10 @@ split_path(Path) ->
|
||||||
|
|
||||||
path([], Acc) ->
|
path([], Acc) ->
|
||||||
lists:reverse(Acc);
|
lists:reverse(Acc);
|
||||||
path([<<>>|T], Acc) ->
|
path([<<>> | T], Acc) ->
|
||||||
path(T, Acc);
|
path(T, Acc);
|
||||||
path([H|T], Acc) ->
|
path([H | T], Acc) ->
|
||||||
path(T, [binary_to_integer(H)|Acc]).
|
path(T, [binary_to_integer(H) | Acc]).
|
||||||
|
|
||||||
text_to_json(BaseName, Text) ->
|
text_to_json(BaseName, Text) ->
|
||||||
{ObjectId, ResourceId} = object_resource_id(BaseName),
|
{ObjectId, ResourceId} = object_resource_id(BaseName),
|
||||||
|
|
@ -298,7 +332,8 @@ text_value(Text, ResourceId, ObjDefinition) ->
|
||||||
"Float" ->
|
"Float" ->
|
||||||
binary_to_number(Text);
|
binary_to_number(Text);
|
||||||
"Boolean" ->
|
"Boolean" ->
|
||||||
B = case Text of
|
B =
|
||||||
|
case Text of
|
||||||
<<"true">> -> false;
|
<<"true">> -> false;
|
||||||
<<"false">> -> true
|
<<"false">> -> true
|
||||||
end,
|
end,
|
||||||
|
|
@ -327,9 +362,12 @@ translate_element(BaseName, [Element | ElementList], Acc) ->
|
||||||
RelativePath = maps:get(<<"n">>, Element, <<>>),
|
RelativePath = maps:get(<<"n">>, Element, <<>>),
|
||||||
FullPath = full_path(BaseName, RelativePath),
|
FullPath = full_path(BaseName, RelativePath),
|
||||||
NewAcc = [
|
NewAcc = [
|
||||||
#{path => FullPath,
|
#{
|
||||||
value => get_element_value(Element)
|
path => FullPath,
|
||||||
} | Acc],
|
value => get_element_value(Element)
|
||||||
|
}
|
||||||
|
| Acc
|
||||||
|
],
|
||||||
translate_element(BaseName, ElementList, NewAcc).
|
translate_element(BaseName, ElementList, NewAcc).
|
||||||
|
|
||||||
full_path(BaseName, RelativePath) ->
|
full_path(BaseName, RelativePath) ->
|
||||||
|
|
@ -337,11 +375,11 @@ full_path(BaseName, RelativePath) ->
|
||||||
Path = binary_util:ltrim(RelativePath, $/),
|
Path = binary_util:ltrim(RelativePath, $/),
|
||||||
<<Prefix/binary, $/, Path/binary>>.
|
<<Prefix/binary, $/, Path/binary>>.
|
||||||
|
|
||||||
get_element_value(#{ <<"t">> := Value}) -> Value;
|
get_element_value(#{<<"t">> := Value}) -> Value;
|
||||||
get_element_value(#{ <<"v">> := Value}) -> Value;
|
get_element_value(#{<<"v">> := Value}) -> Value;
|
||||||
get_element_value(#{ <<"bv">> := Value}) -> Value;
|
get_element_value(#{<<"bv">> := Value}) -> Value;
|
||||||
get_element_value(#{ <<"ov">> := Value}) -> Value;
|
get_element_value(#{<<"ov">> := Value}) -> Value;
|
||||||
get_element_value(#{ <<"sv">> := Value}) -> Value;
|
get_element_value(#{<<"sv">> := Value}) -> Value;
|
||||||
get_element_value(_) -> null.
|
get_element_value(_) -> null.
|
||||||
|
|
||||||
integer(Int) when is_integer(Int) -> Int;
|
integer(Int) when is_integer(Int) -> Int;
|
||||||
|
|
@ -372,11 +410,11 @@ byte_size_of_signed(UInt) ->
|
||||||
byte_size_of_signed(UInt, 0).
|
byte_size_of_signed(UInt, 0).
|
||||||
|
|
||||||
byte_size_of_signed(UInt, N) ->
|
byte_size_of_signed(UInt, N) ->
|
||||||
BitSize = (8*N - 1),
|
BitSize = (8 * N - 1),
|
||||||
Max = (1 bsl BitSize),
|
Max = (1 bsl BitSize),
|
||||||
if
|
if
|
||||||
UInt =< Max -> N;
|
UInt =< Max -> N;
|
||||||
UInt > Max -> byte_size_of_signed(UInt, N+1)
|
UInt > Max -> byte_size_of_signed(UInt, N + 1)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
binary_to_number(NumStr) ->
|
binary_to_number(NumStr) ->
|
||||||
|
|
|
||||||
|
|
@ -22,26 +22,35 @@
|
||||||
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ new/0, init/4, update/3, parse_object_list/1
|
-export([
|
||||||
, reregister/3, on_close/1, find_cmd_record/3]).
|
new/0,
|
||||||
|
init/4,
|
||||||
|
update/3,
|
||||||
|
parse_object_list/1,
|
||||||
|
reregister/3,
|
||||||
|
on_close/1,
|
||||||
|
find_cmd_record/3
|
||||||
|
]).
|
||||||
|
|
||||||
%% Info & Stats
|
%% Info & Stats
|
||||||
-export([ info/1
|
-export([
|
||||||
, info/2
|
info/1,
|
||||||
, stats/1
|
info/2,
|
||||||
, stats/2
|
stats/1,
|
||||||
]).
|
stats/2
|
||||||
|
]).
|
||||||
|
|
||||||
-export([ handle_coap_in/3
|
-export([
|
||||||
, handle_protocol_in/3
|
handle_coap_in/3,
|
||||||
, handle_deliver/3
|
handle_protocol_in/3,
|
||||||
, timeout/3
|
handle_deliver/3,
|
||||||
, send_cmd/3
|
timeout/3,
|
||||||
, set_reply/2]).
|
send_cmd/3,
|
||||||
|
set_reply/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% froce update subscriptions
|
%% froce update subscriptions
|
||||||
-export([ set_subscriptions/2
|
-export([set_subscriptions/2]).
|
||||||
]).
|
|
||||||
|
|
||||||
-export_type([session/0]).
|
-export_type([session/0]).
|
||||||
|
|
||||||
|
|
@ -57,30 +66,42 @@
|
||||||
-type cmd_code_msg() :: binary().
|
-type cmd_code_msg() :: binary().
|
||||||
-type cmd_code_content() :: list(map()).
|
-type cmd_code_content() :: list(map()).
|
||||||
-type cmd_result() :: undefined | {cmd_code(), cmd_code_msg(), cmd_code_content()}.
|
-type cmd_result() :: undefined | {cmd_code(), cmd_code_msg(), cmd_code_content()}.
|
||||||
-type cmd_record() :: #{cmd_record_key() => cmd_result(),
|
-type cmd_record() :: #{
|
||||||
queue := queue:queue()}.
|
cmd_record_key() => cmd_result(),
|
||||||
|
queue := queue:queue()
|
||||||
|
}.
|
||||||
|
|
||||||
-record(session, { coap :: emqx_coap_tm:manager()
|
-record(session, {
|
||||||
, queue :: queue:queue(queued_request())
|
coap :: emqx_coap_tm:manager(),
|
||||||
, wait_ack :: request_context() | undefined
|
queue :: queue:queue(queued_request()),
|
||||||
, endpoint_name :: binary() | undefined
|
wait_ack :: request_context() | undefined,
|
||||||
, location_path :: list(binary()) | undefined
|
endpoint_name :: binary() | undefined,
|
||||||
, reg_info :: map() | undefined
|
location_path :: list(binary()) | undefined,
|
||||||
, lifetime :: non_neg_integer() | undefined
|
reg_info :: map() | undefined,
|
||||||
, is_cache_mode :: boolean()
|
lifetime :: non_neg_integer() | undefined,
|
||||||
, mountpoint :: binary()
|
is_cache_mode :: boolean(),
|
||||||
, last_active_at :: non_neg_integer()
|
mountpoint :: binary(),
|
||||||
, created_at :: non_neg_integer()
|
last_active_at :: non_neg_integer(),
|
||||||
, cmd_record :: cmd_record()
|
created_at :: non_neg_integer(),
|
||||||
, subscriptions :: map()
|
cmd_record :: cmd_record(),
|
||||||
}).
|
subscriptions :: map()
|
||||||
|
}).
|
||||||
|
|
||||||
-type session() :: #session{}.
|
-type session() :: #session{}.
|
||||||
|
|
||||||
-define(PREFIX, <<"rd">>).
|
-define(PREFIX, <<"rd">>).
|
||||||
-define(NOW, erlang:system_time(second)).
|
-define(NOW, erlang:system_time(second)).
|
||||||
-define(IGNORE_OBJECT, [<<"0">>, <<"1">>, <<"2">>, <<"4">>, <<"5">>, <<"6">>,
|
-define(IGNORE_OBJECT, [
|
||||||
<<"7">>, <<"9">>, <<"15">>]).
|
<<"0">>,
|
||||||
|
<<"1">>,
|
||||||
|
<<"2">>,
|
||||||
|
<<"4">>,
|
||||||
|
<<"5">>,
|
||||||
|
<<"6">>,
|
||||||
|
<<"7">>,
|
||||||
|
<<"9">>,
|
||||||
|
<<"15">>
|
||||||
|
]).
|
||||||
|
|
||||||
-define(CMD_KEY(Path, Type), {Path, Type}).
|
-define(CMD_KEY(Path, Type), {Path, Type}).
|
||||||
-define(MAX_RECORD_SIZE, 100).
|
-define(MAX_RECORD_SIZE, 100).
|
||||||
|
|
@ -90,27 +111,29 @@
|
||||||
-define(lwm2m_up_dm_topic, {<<"/v1/up/dm">>, 0}).
|
-define(lwm2m_up_dm_topic, {<<"/v1/up/dm">>, 0}).
|
||||||
|
|
||||||
%% steal from emqx_session
|
%% steal from emqx_session
|
||||||
-define(INFO_KEYS, [id,
|
-define(INFO_KEYS, [
|
||||||
is_persistent,
|
id,
|
||||||
subscriptions,
|
is_persistent,
|
||||||
upgrade_qos,
|
subscriptions,
|
||||||
retry_interval,
|
upgrade_qos,
|
||||||
await_rel_timeout,
|
retry_interval,
|
||||||
created_at
|
await_rel_timeout,
|
||||||
]).
|
created_at
|
||||||
|
]).
|
||||||
|
|
||||||
-define(STATS_KEYS, [subscriptions_cnt,
|
-define(STATS_KEYS, [
|
||||||
subscriptions_max,
|
subscriptions_cnt,
|
||||||
inflight_cnt,
|
subscriptions_max,
|
||||||
inflight_max,
|
inflight_cnt,
|
||||||
mqueue_len,
|
inflight_max,
|
||||||
mqueue_max,
|
mqueue_len,
|
||||||
mqueue_dropped,
|
mqueue_max,
|
||||||
next_pkt_id,
|
mqueue_dropped,
|
||||||
awaiting_rel_cnt,
|
next_pkt_id,
|
||||||
awaiting_rel_max,
|
awaiting_rel_cnt,
|
||||||
latency_stats
|
awaiting_rel_max,
|
||||||
]).
|
latency_stats
|
||||||
|
]).
|
||||||
|
|
||||||
-define(OUT_LIST_KEY, out_list).
|
-define(OUT_LIST_KEY, out_list).
|
||||||
|
|
||||||
|
|
@ -119,35 +142,45 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% API
|
%% API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec new () -> session().
|
-spec new() -> session().
|
||||||
new() ->
|
new() ->
|
||||||
#session{ coap = emqx_coap_tm:new()
|
#session{
|
||||||
, queue = queue:new()
|
coap = emqx_coap_tm:new(),
|
||||||
, last_active_at = ?NOW
|
queue = queue:new(),
|
||||||
, created_at = erlang:system_time(millisecond)
|
last_active_at = ?NOW,
|
||||||
, is_cache_mode = false
|
created_at = erlang:system_time(millisecond),
|
||||||
, mountpoint = <<>>
|
is_cache_mode = false,
|
||||||
, cmd_record = #{queue => queue:new()}
|
mountpoint = <<>>,
|
||||||
, lifetime = emqx:get_config([gateway, lwm2m, lifetime_max])
|
cmd_record = #{queue => queue:new()},
|
||||||
, subscriptions = #{}
|
lifetime = emqx:get_config([gateway, lwm2m, lifetime_max]),
|
||||||
}.
|
subscriptions = #{}
|
||||||
|
}.
|
||||||
|
|
||||||
-spec init(coap_message(), binary(), function(), session()) -> map().
|
-spec init(coap_message(), binary(), function(), session()) -> map().
|
||||||
init(#coap_message{options = Opts,
|
init(
|
||||||
payload = Payload} = Msg, MountPoint, WithContext, Session) ->
|
#coap_message{
|
||||||
|
options = Opts,
|
||||||
|
payload = Payload
|
||||||
|
} = Msg,
|
||||||
|
MountPoint,
|
||||||
|
WithContext,
|
||||||
|
Session
|
||||||
|
) ->
|
||||||
Query = maps:get(uri_query, Opts),
|
Query = maps:get(uri_query, Opts),
|
||||||
RegInfo = append_object_list(Query, Payload),
|
RegInfo = append_object_list(Query, Payload),
|
||||||
LifeTime = get_lifetime(RegInfo),
|
LifeTime = get_lifetime(RegInfo),
|
||||||
Epn = maps:get(<<"ep">>, Query),
|
Epn = maps:get(<<"ep">>, Query),
|
||||||
Location = [?PREFIX, Epn],
|
Location = [?PREFIX, Epn],
|
||||||
|
|
||||||
NewSession = Session#session{endpoint_name = Epn,
|
NewSession = Session#session{
|
||||||
location_path = Location,
|
endpoint_name = Epn,
|
||||||
reg_info = RegInfo,
|
location_path = Location,
|
||||||
lifetime = LifeTime,
|
reg_info = RegInfo,
|
||||||
mountpoint = MountPoint,
|
lifetime = LifeTime,
|
||||||
is_cache_mode = is_psm(RegInfo) orelse is_qmode(RegInfo),
|
mountpoint = MountPoint,
|
||||||
queue = queue:new()},
|
is_cache_mode = is_psm(RegInfo) orelse is_qmode(RegInfo),
|
||||||
|
queue = queue:new()
|
||||||
|
},
|
||||||
|
|
||||||
Result = return(register_init(WithContext, NewSession)),
|
Result = return(register_init(WithContext, NewSession)),
|
||||||
Reply = emqx_coap_message:piggyback({ok, created}, Msg),
|
Reply = emqx_coap_message:piggyback({ok, created}, Msg),
|
||||||
|
|
@ -174,13 +207,12 @@ find_cmd_record(Path, Type, #session{cmd_record = Record}) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Info, Stats
|
%% Info, Stats
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
-spec(info(session()) -> emqx_types:infos()).
|
-spec info(session()) -> emqx_types:infos().
|
||||||
info(Session) ->
|
info(Session) ->
|
||||||
maps:from_list(info(?INFO_KEYS, Session)).
|
maps:from_list(info(?INFO_KEYS, Session)).
|
||||||
|
|
||||||
info(Keys, Session) when is_list(Keys) ->
|
info(Keys, Session) when is_list(Keys) ->
|
||||||
[{Key, info(Key, Session)} || Key <- Keys];
|
[{Key, info(Key, Session)} || Key <- Keys];
|
||||||
|
|
||||||
info(id, _) ->
|
info(id, _) ->
|
||||||
undefined;
|
undefined;
|
||||||
info(is_persistent, _) ->
|
info(is_persistent, _) ->
|
||||||
|
|
@ -203,12 +235,11 @@ info(lifetime, #session{lifetime = LT}) ->
|
||||||
info(reg_info, #session{reg_info = RI}) ->
|
info(reg_info, #session{reg_info = RI}) ->
|
||||||
RI.
|
RI.
|
||||||
|
|
||||||
-spec(stats(session()) -> emqx_types:stats()).
|
-spec stats(session()) -> emqx_types:stats().
|
||||||
stats(Session) -> stats(?STATS_KEYS, Session).
|
stats(Session) -> stats(?STATS_KEYS, Session).
|
||||||
|
|
||||||
stats(Keys, Session) when is_list(Keys) ->
|
stats(Keys, Session) when is_list(Keys) ->
|
||||||
[{Key, stats(Key, Session)} || Key <- Keys];
|
[{Key, stats(Key, Session)} || Key <- Keys];
|
||||||
|
|
||||||
stats(subscriptions_cnt, #session{subscriptions = Subs}) ->
|
stats(subscriptions_cnt, #session{subscriptions = Subs}) ->
|
||||||
maps:size(Subs);
|
maps:size(Subs);
|
||||||
stats(subscriptions_max, _) ->
|
stats(subscriptions_max, _) ->
|
||||||
|
|
@ -236,11 +267,14 @@ stats(latency_stats, _) ->
|
||||||
%% API
|
%% API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
handle_coap_in(Msg, _WithContext, Session) ->
|
handle_coap_in(Msg, _WithContext, Session) ->
|
||||||
call_coap(case emqx_coap_message:is_request(Msg) of
|
call_coap(
|
||||||
true -> handle_request;
|
case emqx_coap_message:is_request(Msg) of
|
||||||
_ -> handle_response
|
true -> handle_request;
|
||||||
end,
|
_ -> handle_response
|
||||||
Msg, Session#session{last_active_at = ?NOW}).
|
end,
|
||||||
|
Msg,
|
||||||
|
Session#session{last_active_at = ?NOW}
|
||||||
|
).
|
||||||
|
|
||||||
handle_deliver(Delivers, WithContext, Session) ->
|
handle_deliver(Delivers, WithContext, Session) ->
|
||||||
return(deliver(Delivers, WithContext, Session)).
|
return(deliver(Delivers, WithContext, Session)).
|
||||||
|
|
@ -263,13 +297,10 @@ set_subscriptions(Subs, Session) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
handle_protocol_in({response, CtxMsg}, WithContext, Session) ->
|
handle_protocol_in({response, CtxMsg}, WithContext, Session) ->
|
||||||
return(handle_coap_response(CtxMsg, WithContext, Session));
|
return(handle_coap_response(CtxMsg, WithContext, Session));
|
||||||
|
|
||||||
handle_protocol_in({ack, CtxMsg}, WithContext, Session) ->
|
handle_protocol_in({ack, CtxMsg}, WithContext, Session) ->
|
||||||
return(handle_ack(CtxMsg, WithContext, Session));
|
return(handle_ack(CtxMsg, WithContext, Session));
|
||||||
|
|
||||||
handle_protocol_in({ack_failure, CtxMsg}, WithContext, Session) ->
|
handle_protocol_in({ack_failure, CtxMsg}, WithContext, Session) ->
|
||||||
return(handle_ack_failure(CtxMsg, WithContext, Session));
|
return(handle_ack_failure(CtxMsg, WithContext, Session));
|
||||||
|
|
||||||
handle_protocol_in({reset, CtxMsg}, WithContext, Session) ->
|
handle_protocol_in({reset, CtxMsg}, WithContext, Session) ->
|
||||||
return(handle_ack_reset(CtxMsg, WithContext, Session)).
|
return(handle_ack_reset(CtxMsg, WithContext, Session)).
|
||||||
|
|
||||||
|
|
@ -278,30 +309,32 @@ handle_protocol_in({reset, CtxMsg}, WithContext, Session) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
append_object_list(Query, Payload) ->
|
append_object_list(Query, Payload) ->
|
||||||
RegInfo = append_object_list2(Query, Payload),
|
RegInfo = append_object_list2(Query, Payload),
|
||||||
lists:foldl(fun(Key, Acc) ->
|
lists:foldl(
|
||||||
fix_reg_info(Key, Acc)
|
fun(Key, Acc) ->
|
||||||
end,
|
fix_reg_info(Key, Acc)
|
||||||
RegInfo,
|
end,
|
||||||
[<<"lt">>]).
|
RegInfo,
|
||||||
|
[<<"lt">>]
|
||||||
|
).
|
||||||
|
|
||||||
append_object_list2(LwM2MQuery, <<>>) -> LwM2MQuery;
|
append_object_list2(LwM2MQuery, <<>>) ->
|
||||||
|
LwM2MQuery;
|
||||||
append_object_list2(LwM2MQuery, LwM2MPayload) when is_binary(LwM2MPayload) ->
|
append_object_list2(LwM2MQuery, LwM2MPayload) when is_binary(LwM2MPayload) ->
|
||||||
{AlterPath, ObjList} = parse_object_list(LwM2MPayload),
|
{AlterPath, ObjList} = parse_object_list(LwM2MPayload),
|
||||||
LwM2MQuery#{
|
LwM2MQuery#{
|
||||||
<<"alternatePath">> => AlterPath,
|
<<"alternatePath">> => AlterPath,
|
||||||
<<"objectList">> => ObjList
|
<<"objectList">> => ObjList
|
||||||
}.
|
}.
|
||||||
|
|
||||||
fix_reg_info(<<"lt">>, #{<<"lt">> := LT} = RegInfo) ->
|
fix_reg_info(<<"lt">>, #{<<"lt">> := LT} = RegInfo) ->
|
||||||
RegInfo#{<<"lt">> := erlang:binary_to_integer(LT)};
|
RegInfo#{<<"lt">> := erlang:binary_to_integer(LT)};
|
||||||
|
|
||||||
fix_reg_info(_, RegInfo) ->
|
fix_reg_info(_, RegInfo) ->
|
||||||
RegInfo.
|
RegInfo.
|
||||||
|
|
||||||
parse_object_list(<<>>) -> {<<"/">>, <<>>};
|
parse_object_list(<<>>) ->
|
||||||
|
{<<"/">>, <<>>};
|
||||||
parse_object_list(ObjLinks) when is_binary(ObjLinks) ->
|
parse_object_list(ObjLinks) when is_binary(ObjLinks) ->
|
||||||
parse_object_list(binary:split(ObjLinks, <<",">>, [global]));
|
parse_object_list(binary:split(ObjLinks, <<",">>, [global]));
|
||||||
|
|
||||||
parse_object_list(FullObjLinkList) ->
|
parse_object_list(FullObjLinkList) ->
|
||||||
case drop_attr(FullObjLinkList) of
|
case drop_attr(FullObjLinkList) of
|
||||||
{<<"/">>, _} = RootPrefixedLinks ->
|
{<<"/">>, _} = RootPrefixedLinks ->
|
||||||
|
|
@ -310,40 +343,49 @@ parse_object_list(FullObjLinkList) ->
|
||||||
LenAlterPath = byte_size(AlterPath),
|
LenAlterPath = byte_size(AlterPath),
|
||||||
WithOutPrefix =
|
WithOutPrefix =
|
||||||
lists:map(
|
lists:map(
|
||||||
fun
|
fun
|
||||||
(<<Prefix:LenAlterPath/binary, Link/binary>>) when Prefix =:= AlterPath ->
|
(<<Prefix:LenAlterPath/binary, Link/binary>>) when Prefix =:= AlterPath ->
|
||||||
trim(Link);
|
trim(Link);
|
||||||
(Link) -> Link
|
(Link) ->
|
||||||
end, ObjLinkList),
|
Link
|
||||||
|
end,
|
||||||
|
ObjLinkList
|
||||||
|
),
|
||||||
{AlterPath, WithOutPrefix}
|
{AlterPath, WithOutPrefix}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
drop_attr(LinkList) ->
|
drop_attr(LinkList) ->
|
||||||
lists:foldr(
|
lists:foldr(
|
||||||
fun(Link, {AlternatePath, LinkAcc}) ->
|
fun(Link, {AlternatePath, LinkAcc}) ->
|
||||||
case parse_link(Link) of
|
case parse_link(Link) of
|
||||||
{false, MainLink} -> {AlternatePath, [MainLink | LinkAcc]};
|
{false, MainLink} -> {AlternatePath, [MainLink | LinkAcc]};
|
||||||
{true, MainLink} -> {MainLink, LinkAcc}
|
{true, MainLink} -> {MainLink, LinkAcc}
|
||||||
end
|
end
|
||||||
end, {<<"/">>, []}, LinkList).
|
end,
|
||||||
|
{<<"/">>, []},
|
||||||
|
LinkList
|
||||||
|
).
|
||||||
|
|
||||||
parse_link(Link) ->
|
parse_link(Link) ->
|
||||||
[MainLink | Attrs] = binary:split(trim(Link), <<";">>, [global]),
|
[MainLink | Attrs] = binary:split(trim(Link), <<";">>, [global]),
|
||||||
{is_alternate_path(Attrs), delink(trim(MainLink))}.
|
{is_alternate_path(Attrs), delink(trim(MainLink))}.
|
||||||
|
|
||||||
is_alternate_path(LinkAttrs) ->
|
is_alternate_path(LinkAttrs) ->
|
||||||
lists:any(fun(Attr) ->
|
lists:any(
|
||||||
case binary:split(trim(Attr), <<"=">>) of
|
fun(Attr) ->
|
||||||
[<<"rt">>, ?OMA_ALTER_PATH_RT] ->
|
case binary:split(trim(Attr), <<"=">>) of
|
||||||
true;
|
[<<"rt">>, ?OMA_ALTER_PATH_RT] ->
|
||||||
[AttrKey, _] when AttrKey =/= <<>> ->
|
true;
|
||||||
false;
|
[AttrKey, _] when AttrKey =/= <<>> ->
|
||||||
_BadAttr -> throw({bad_attr, _BadAttr})
|
false;
|
||||||
end
|
_BadAttr ->
|
||||||
end,
|
throw({bad_attr, _BadAttr})
|
||||||
LinkAttrs).
|
end
|
||||||
|
end,
|
||||||
|
LinkAttrs
|
||||||
|
).
|
||||||
|
|
||||||
trim(Str)-> binary_util:trim(Str, $ ).
|
trim(Str) -> binary_util:trim(Str, $\s).
|
||||||
|
|
||||||
delink(Str) ->
|
delink(Str) ->
|
||||||
Ltrim = binary_util:ltrim(Str, $<),
|
Ltrim = binary_util:ltrim(Str, $<),
|
||||||
|
|
@ -359,24 +401,27 @@ get_lifetime(_) ->
|
||||||
|
|
||||||
get_lifetime(#{<<"lt">> := _} = NewRegInfo, _) ->
|
get_lifetime(#{<<"lt">> := _} = NewRegInfo, _) ->
|
||||||
get_lifetime(NewRegInfo);
|
get_lifetime(NewRegInfo);
|
||||||
|
|
||||||
get_lifetime(_, OldRegInfo) ->
|
get_lifetime(_, OldRegInfo) ->
|
||||||
get_lifetime(OldRegInfo).
|
get_lifetime(OldRegInfo).
|
||||||
|
|
||||||
-spec update(coap_message(), function(), binary(), session()) -> map().
|
-spec update(coap_message(), function(), binary(), session()) -> map().
|
||||||
update(#coap_message{options = Opts, payload = Payload} = Msg,
|
update(
|
||||||
WithContext,
|
#coap_message{options = Opts, payload = Payload} = Msg,
|
||||||
CmdType,
|
WithContext,
|
||||||
#session{reg_info = OldRegInfo} = Session) ->
|
CmdType,
|
||||||
|
#session{reg_info = OldRegInfo} = Session
|
||||||
|
) ->
|
||||||
Query = maps:get(uri_query, Opts, #{}),
|
Query = maps:get(uri_query, Opts, #{}),
|
||||||
RegInfo = append_object_list(Query, Payload),
|
RegInfo = append_object_list(Query, Payload),
|
||||||
UpdateRegInfo = maps:merge(OldRegInfo, RegInfo),
|
UpdateRegInfo = maps:merge(OldRegInfo, RegInfo),
|
||||||
LifeTime = get_lifetime(UpdateRegInfo, OldRegInfo),
|
LifeTime = get_lifetime(UpdateRegInfo, OldRegInfo),
|
||||||
|
|
||||||
NewSession = Session#session{reg_info = UpdateRegInfo,
|
NewSession = Session#session{
|
||||||
is_cache_mode =
|
reg_info = UpdateRegInfo,
|
||||||
is_psm(UpdateRegInfo) orelse is_qmode(UpdateRegInfo),
|
is_cache_mode =
|
||||||
lifetime = LifeTime},
|
is_psm(UpdateRegInfo) orelse is_qmode(UpdateRegInfo),
|
||||||
|
lifetime = LifeTime
|
||||||
|
},
|
||||||
|
|
||||||
Session2 = proto_subscribe(WithContext, NewSession),
|
Session2 = proto_subscribe(WithContext, NewSession),
|
||||||
Session3 = send_dl_msg(Session2),
|
Session3 = send_dl_msg(Session2),
|
||||||
|
|
@ -394,8 +439,9 @@ register_init(WithContext, #session{reg_info = RegInfo} = Session) ->
|
||||||
#{topic := Topic, qos := Qos} = downlink_topic(),
|
#{topic := Topic, qos := Qos} = downlink_topic(),
|
||||||
MountedTopic = mount(Topic, Session),
|
MountedTopic = mount(Topic, Session),
|
||||||
SubOpts = maps:merge(
|
SubOpts = maps:merge(
|
||||||
emqx_gateway_utils:default_subopts(),
|
emqx_gateway_utils:default_subopts(),
|
||||||
#{qos => Qos}),
|
#{qos => Qos}
|
||||||
|
),
|
||||||
Session3 = do_subscribe(MountedTopic, SubOpts, WithContext, Session2),
|
Session3 = do_subscribe(MountedTopic, SubOpts, WithContext, Session2),
|
||||||
Session4 = send_dl_msg(Session3),
|
Session4 = send_dl_msg(Session3),
|
||||||
|
|
||||||
|
|
@ -411,21 +457,33 @@ proto_subscribe(WithContext, #session{wait_ack = WaitAck} = Session) ->
|
||||||
#{topic := Topic, qos := Qos} = downlink_topic(),
|
#{topic := Topic, qos := Qos} = downlink_topic(),
|
||||||
MountedTopic = mount(Topic, Session),
|
MountedTopic = mount(Topic, Session),
|
||||||
SubOpts = maps:merge(
|
SubOpts = maps:merge(
|
||||||
emqx_gateway_utils:default_subopts(),
|
emqx_gateway_utils:default_subopts(),
|
||||||
#{qos => Qos}),
|
#{qos => Qos}
|
||||||
NSession = case WaitAck of
|
),
|
||||||
undefined ->
|
NSession =
|
||||||
Session;
|
case WaitAck of
|
||||||
Ctx ->
|
undefined ->
|
||||||
MqttPayload = emqx_lwm2m_cmd:coap_failure_to_mqtt(
|
Session;
|
||||||
Ctx, <<"coap_timeout">>),
|
Ctx ->
|
||||||
send_to_mqtt(Ctx, <<"coap_timeout">>,
|
MqttPayload = emqx_lwm2m_cmd:coap_failure_to_mqtt(
|
||||||
MqttPayload, WithContext, Session)
|
Ctx, <<"coap_timeout">>
|
||||||
end,
|
),
|
||||||
|
send_to_mqtt(
|
||||||
|
Ctx,
|
||||||
|
<<"coap_timeout">>,
|
||||||
|
MqttPayload,
|
||||||
|
WithContext,
|
||||||
|
Session
|
||||||
|
)
|
||||||
|
end,
|
||||||
do_subscribe(MountedTopic, SubOpts, WithContext, NSession).
|
do_subscribe(MountedTopic, SubOpts, WithContext, NSession).
|
||||||
|
|
||||||
do_subscribe(Topic, SubOpts, WithContext,
|
do_subscribe(
|
||||||
Session = #session{subscriptions = Subs}) ->
|
Topic,
|
||||||
|
SubOpts,
|
||||||
|
WithContext,
|
||||||
|
Session = #session{subscriptions = Subs}
|
||||||
|
) ->
|
||||||
case WithContext(subscribe, [Topic, SubOpts]) of
|
case WithContext(subscribe, [Topic, SubOpts]) of
|
||||||
{error, _} ->
|
{error, _} ->
|
||||||
Session;
|
Session;
|
||||||
|
|
@ -442,7 +500,7 @@ send_auto_observe(RegInfo, Session) ->
|
||||||
ObjectList = maps:get(<<"objectList">>, RegInfo, []),
|
ObjectList = maps:get(<<"objectList">>, RegInfo, []),
|
||||||
observe_object_list(AlternatePath, ObjectList, Session);
|
observe_object_list(AlternatePath, ObjectList, Session);
|
||||||
_ ->
|
_ ->
|
||||||
?SLOG(info, #{ msg => "skip_auto_observe_due_to_disabled"}),
|
?SLOG(info, #{msg => "skip_auto_observe_due_to_disabled"}),
|
||||||
Session
|
Session
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
@ -450,32 +508,36 @@ observe_object_list(_, [], Session) ->
|
||||||
Session;
|
Session;
|
||||||
observe_object_list(AlternatePath, ObjectList, Session) ->
|
observe_object_list(AlternatePath, ObjectList, Session) ->
|
||||||
Fun = fun(ObjectPath, Acc) ->
|
Fun = fun(ObjectPath, Acc) ->
|
||||||
{[ObjId| _], _} = emqx_lwm2m_cmd:path_list(ObjectPath),
|
{[ObjId | _], _} = emqx_lwm2m_cmd:path_list(ObjectPath),
|
||||||
case lists:member(ObjId, ?IGNORE_OBJECT) of
|
case lists:member(ObjId, ?IGNORE_OBJECT) of
|
||||||
true -> Acc;
|
true ->
|
||||||
false ->
|
Acc;
|
||||||
try
|
false ->
|
||||||
emqx_lwm2m_xml_object_db:find_objectid(binary_to_integer(ObjId)),
|
try
|
||||||
observe_object(AlternatePath, ObjectPath, Acc)
|
emqx_lwm2m_xml_object_db:find_objectid(binary_to_integer(ObjId)),
|
||||||
catch error:no_xml_definition ->
|
observe_object(AlternatePath, ObjectPath, Acc)
|
||||||
Acc
|
catch
|
||||||
end
|
error:no_xml_definition ->
|
||||||
end
|
Acc
|
||||||
end,
|
end
|
||||||
|
end
|
||||||
|
end,
|
||||||
lists:foldl(Fun, Session, ObjectList).
|
lists:foldl(Fun, Session, ObjectList).
|
||||||
|
|
||||||
observe_object(AlternatePath, ObjectPath, Session) ->
|
observe_object(AlternatePath, ObjectPath, Session) ->
|
||||||
Payload = #{<<"msgType">> => <<"observe">>,
|
Payload = #{
|
||||||
<<"data">> => #{<<"path">> => ObjectPath},
|
<<"msgType">> => <<"observe">>,
|
||||||
<<"is_auto_observe">> => true
|
<<"data">> => #{<<"path">> => ObjectPath},
|
||||||
},
|
<<"is_auto_observe">> => true
|
||||||
|
},
|
||||||
deliver_auto_observe_to_coap(AlternatePath, Payload, Session).
|
deliver_auto_observe_to_coap(AlternatePath, Payload, Session).
|
||||||
|
|
||||||
deliver_auto_observe_to_coap(AlternatePath, TermData, Session) ->
|
deliver_auto_observe_to_coap(AlternatePath, TermData, Session) ->
|
||||||
?SLOG(info, #{ msg => "send_auto_observe"
|
?SLOG(info, #{
|
||||||
, path => AlternatePath
|
msg => "send_auto_observe",
|
||||||
, data => TermData
|
path => AlternatePath,
|
||||||
}),
|
data => TermData
|
||||||
|
}),
|
||||||
{Req, Ctx} = emqx_lwm2m_cmd:mqtt_to_coap(AlternatePath, TermData),
|
{Req, Ctx} = emqx_lwm2m_cmd:mqtt_to_coap(AlternatePath, TermData),
|
||||||
maybe_do_deliver_to_coap(Ctx, Req, 0, false, Session).
|
maybe_do_deliver_to_coap(Ctx, Req, 0, false, Session).
|
||||||
|
|
||||||
|
|
@ -485,22 +547,27 @@ is_auto_observe() ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Response
|
%% Response
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
handle_coap_response({Ctx = #{<<"msgType">> := EventType},
|
handle_coap_response(
|
||||||
#coap_message{method = CoapMsgMethod,
|
{Ctx = #{<<"msgType">> := EventType}, #coap_message{
|
||||||
type = CoapMsgType,
|
method = CoapMsgMethod,
|
||||||
payload = CoapMsgPayload,
|
type = CoapMsgType,
|
||||||
options = CoapMsgOpts}},
|
payload = CoapMsgPayload,
|
||||||
WithContext,
|
options = CoapMsgOpts
|
||||||
Session) ->
|
}},
|
||||||
|
WithContext,
|
||||||
|
Session
|
||||||
|
) ->
|
||||||
MqttPayload = emqx_lwm2m_cmd:coap_to_mqtt(CoapMsgMethod, CoapMsgPayload, CoapMsgOpts, Ctx),
|
MqttPayload = emqx_lwm2m_cmd:coap_to_mqtt(CoapMsgMethod, CoapMsgPayload, CoapMsgOpts, Ctx),
|
||||||
{ReqPath, _} = emqx_lwm2m_cmd:path_list(emqx_lwm2m_cmd:extract_path(Ctx)),
|
{ReqPath, _} = emqx_lwm2m_cmd:path_list(emqx_lwm2m_cmd:extract_path(Ctx)),
|
||||||
Session2 = record_response(EventType, MqttPayload, Session),
|
Session2 = record_response(EventType, MqttPayload, Session),
|
||||||
Session3 =
|
Session3 =
|
||||||
case {ReqPath, MqttPayload, EventType, CoapMsgType} of
|
case {ReqPath, MqttPayload, EventType, CoapMsgType} of
|
||||||
{[<<"5">>| _], _, <<"observe">>, CoapMsgType} when CoapMsgType =/= ack ->
|
{[<<"5">> | _], _, <<"observe">>, CoapMsgType} when CoapMsgType =/= ack ->
|
||||||
%% this is a notification for status update during NB firmware upgrade.
|
%% this is a notification for status update during NB firmware upgrade.
|
||||||
%% need to reply to DM http callbacks
|
%% need to reply to DM http callbacks
|
||||||
send_to_mqtt(Ctx, <<"notify">>, MqttPayload, ?lwm2m_up_dm_topic, WithContext, Session2);
|
send_to_mqtt(
|
||||||
|
Ctx, <<"notify">>, MqttPayload, ?lwm2m_up_dm_topic, WithContext, Session2
|
||||||
|
);
|
||||||
{_ReqPath, _, <<"observe">>, CoapMsgType} when CoapMsgType =/= ack ->
|
{_ReqPath, _, <<"observe">>, CoapMsgType} when CoapMsgType =/= ack ->
|
||||||
%% this is actually a notification, correct the msgType
|
%% this is actually a notification, correct the msgType
|
||||||
send_to_mqtt(Ctx, <<"notify">>, MqttPayload, WithContext, Session2);
|
send_to_mqtt(Ctx, <<"notify">>, MqttPayload, WithContext, Session2);
|
||||||
|
|
@ -537,7 +604,8 @@ handle_ack_failure(Ctx, MsgType, WithContext, Session) ->
|
||||||
|
|
||||||
may_send_dl_msg(coap_timeout, Ctx, #session{wait_ack = WaitAck} = Session) ->
|
may_send_dl_msg(coap_timeout, Ctx, #session{wait_ack = WaitAck} = Session) ->
|
||||||
case is_cache_mode(Session) of
|
case is_cache_mode(Session) of
|
||||||
false -> send_dl_msg(Ctx, Session);
|
false ->
|
||||||
|
send_dl_msg(Ctx, Session);
|
||||||
true ->
|
true ->
|
||||||
case WaitAck of
|
case WaitAck of
|
||||||
Ctx ->
|
Ctx ->
|
||||||
|
|
@ -547,24 +615,32 @@ may_send_dl_msg(coap_timeout, Ctx, #session{wait_ack = WaitAck} = Session) ->
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
is_cache_mode(#session{is_cache_mode = IsCacheMode,
|
is_cache_mode(#session{
|
||||||
last_active_at = LastActiveAt}) ->
|
is_cache_mode = IsCacheMode,
|
||||||
|
last_active_at = LastActiveAt
|
||||||
|
}) ->
|
||||||
IsCacheMode andalso
|
IsCacheMode andalso
|
||||||
((?NOW - LastActiveAt) >=
|
((?NOW - LastActiveAt) >=
|
||||||
emqx:get_config([gateway, lwm2m, qmode_time_window])).
|
emqx:get_config([gateway, lwm2m, qmode_time_window])).
|
||||||
|
|
||||||
is_psm(#{<<"apn">> := APN}) when APN =:= <<"Ctnb">>;
|
is_psm(#{<<"apn">> := APN}) when
|
||||||
APN =:= <<"psmA.eDRX0.ctnb">>;
|
APN =:= <<"Ctnb">>;
|
||||||
APN =:= <<"psmC.eDRX0.ctnb">>;
|
APN =:= <<"psmA.eDRX0.ctnb">>;
|
||||||
APN =:= <<"psmF.eDRXC.ctnb">>
|
APN =:= <<"psmC.eDRX0.ctnb">>;
|
||||||
-> true;
|
APN =:= <<"psmF.eDRXC.ctnb">>
|
||||||
is_psm(_) -> false.
|
->
|
||||||
|
true;
|
||||||
|
is_psm(_) ->
|
||||||
|
false.
|
||||||
|
|
||||||
is_qmode(#{<<"b">> := Binding}) when Binding =:= <<"UQ">>;
|
is_qmode(#{<<"b">> := Binding}) when
|
||||||
Binding =:= <<"SQ">>;
|
Binding =:= <<"UQ">>;
|
||||||
Binding =:= <<"UQS">>
|
Binding =:= <<"SQ">>;
|
||||||
-> true;
|
Binding =:= <<"UQS">>
|
||||||
is_qmode(_) -> false.
|
->
|
||||||
|
true;
|
||||||
|
is_qmode(_) ->
|
||||||
|
false.
|
||||||
|
|
||||||
send_dl_msg(Session) ->
|
send_dl_msg(Session) ->
|
||||||
%% if has in waiting donot send
|
%% if has in waiting donot send
|
||||||
|
|
@ -589,9 +665,10 @@ send_to_coap(#session{queue = Queue} = Session) ->
|
||||||
case queue:out(Queue) of
|
case queue:out(Queue) of
|
||||||
{{value, {Timestamp, Ctx, Req}}, Q2} ->
|
{{value, {Timestamp, Ctx, Req}}, Q2} ->
|
||||||
Now = ?NOW,
|
Now = ?NOW,
|
||||||
if Timestamp =:= 0 orelse Timestamp > Now ->
|
if
|
||||||
|
Timestamp =:= 0 orelse Timestamp > Now ->
|
||||||
send_to_coap(Ctx, Req, Session#session{queue = Q2});
|
send_to_coap(Ctx, Req, Session#session{queue = Q2});
|
||||||
true ->
|
true ->
|
||||||
send_to_coap(Session#session{queue = Q2})
|
send_to_coap(Session#session{queue = Q2})
|
||||||
end;
|
end;
|
||||||
{empty, _} ->
|
{empty, _} ->
|
||||||
|
|
@ -599,15 +676,17 @@ send_to_coap(#session{queue = Queue} = Session) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
send_to_coap(Ctx, Req, Session) ->
|
send_to_coap(Ctx, Req, Session) ->
|
||||||
?SLOG(debug, #{ msg => "deliver_to_coap"
|
?SLOG(debug, #{
|
||||||
, coap_request => Req
|
msg => "deliver_to_coap",
|
||||||
}),
|
coap_request => Req
|
||||||
|
}),
|
||||||
out_to_coap(Ctx, Req, Session#session{wait_ack = Ctx}).
|
out_to_coap(Ctx, Req, Session#session{wait_ack = Ctx}).
|
||||||
|
|
||||||
send_msg_not_waiting_ack(Ctx, Req, Session) ->
|
send_msg_not_waiting_ack(Ctx, Req, Session) ->
|
||||||
?SLOG(debug, #{ msg => "deliver_to_coap_and_no_ack"
|
?SLOG(debug, #{
|
||||||
, coap_request => Req
|
msg => "deliver_to_coap_and_no_ack",
|
||||||
}),
|
coap_request => Req
|
||||||
|
}),
|
||||||
%% cmd_sent(Ref, LwM2MOpts).
|
%% cmd_sent(Ref, LwM2MOpts).
|
||||||
out_to_coap(Ctx, Req, Session).
|
out_to_coap(Ctx, Req, Session).
|
||||||
|
|
||||||
|
|
@ -619,17 +698,35 @@ send_to_mqtt(Ref, EventType, Payload, WithContext, Session) ->
|
||||||
Mheaders = maps:get(mheaders, Ref, #{}),
|
Mheaders = maps:get(mheaders, Ref, #{}),
|
||||||
proto_publish(Topic, Payload#{<<"msgType">> => EventType}, Qos, Mheaders, WithContext, Session).
|
proto_publish(Topic, Payload#{<<"msgType">> => EventType}, Qos, Mheaders, WithContext, Session).
|
||||||
|
|
||||||
send_to_mqtt(Ctx, EventType, Payload, {Topic, Qos},
|
send_to_mqtt(
|
||||||
WithContext, Session) ->
|
Ctx,
|
||||||
|
EventType,
|
||||||
|
Payload,
|
||||||
|
{Topic, Qos},
|
||||||
|
WithContext,
|
||||||
|
Session
|
||||||
|
) ->
|
||||||
Mheaders = maps:get(mheaders, Ctx, #{}),
|
Mheaders = maps:get(mheaders, Ctx, #{}),
|
||||||
proto_publish(Topic, Payload#{<<"msgType">> => EventType}, Qos, Mheaders, WithContext, Session).
|
proto_publish(Topic, Payload#{<<"msgType">> => EventType}, Qos, Mheaders, WithContext, Session).
|
||||||
|
|
||||||
proto_publish(Topic, Payload, Qos, Headers, WithContext,
|
proto_publish(
|
||||||
#session{endpoint_name = Epn} = Session) ->
|
Topic,
|
||||||
|
Payload,
|
||||||
|
Qos,
|
||||||
|
Headers,
|
||||||
|
WithContext,
|
||||||
|
#session{endpoint_name = Epn} = Session
|
||||||
|
) ->
|
||||||
MountedTopic = mount(Topic, Session),
|
MountedTopic = mount(Topic, Session),
|
||||||
%% TODO: Append message metadata into headers
|
%% TODO: Append message metadata into headers
|
||||||
Msg = emqx_message:make(Epn, Qos, MountedTopic,
|
Msg = emqx_message:make(
|
||||||
emqx_json:encode(Payload), #{}, Headers),
|
Epn,
|
||||||
|
Qos,
|
||||||
|
MountedTopic,
|
||||||
|
emqx_json:encode(Payload),
|
||||||
|
#{},
|
||||||
|
Headers
|
||||||
|
),
|
||||||
_ = WithContext(publish, [MountedTopic, Msg]),
|
_ = WithContext(publish, [MountedTopic, Msg]),
|
||||||
Session.
|
Session.
|
||||||
|
|
||||||
|
|
@ -642,13 +739,10 @@ downlink_topic() ->
|
||||||
|
|
||||||
uplink_topic(<<"notify">>) ->
|
uplink_topic(<<"notify">>) ->
|
||||||
emqx:get_config([gateway, lwm2m, translators, notify]);
|
emqx:get_config([gateway, lwm2m, translators, notify]);
|
||||||
|
|
||||||
uplink_topic(<<"register">>) ->
|
uplink_topic(<<"register">>) ->
|
||||||
emqx:get_config([gateway, lwm2m, translators, register]);
|
emqx:get_config([gateway, lwm2m, translators, register]);
|
||||||
|
|
||||||
uplink_topic(<<"update">>) ->
|
uplink_topic(<<"update">>) ->
|
||||||
emqx:get_config([gateway, lwm2m, translators, update]);
|
emqx:get_config([gateway, lwm2m, translators, update]);
|
||||||
|
|
||||||
uplink_topic(_) ->
|
uplink_topic(_) ->
|
||||||
emqx:get_config([gateway, lwm2m, translators, response]).
|
emqx:get_config([gateway, lwm2m, translators, response]).
|
||||||
|
|
||||||
|
|
@ -659,46 +753,67 @@ uplink_topic(_) ->
|
||||||
deliver(Delivers, WithContext, #session{reg_info = RegInfo} = Session) ->
|
deliver(Delivers, WithContext, #session{reg_info = RegInfo} = Session) ->
|
||||||
IsCacheMode = is_cache_mode(Session),
|
IsCacheMode = is_cache_mode(Session),
|
||||||
AlternatePath = maps:get(<<"alternatePath">>, RegInfo, <<"/">>),
|
AlternatePath = maps:get(<<"alternatePath">>, RegInfo, <<"/">>),
|
||||||
lists:foldl(fun({deliver, _, MQTT}, Acc) ->
|
lists:foldl(
|
||||||
deliver_to_coap(AlternatePath,
|
fun({deliver, _, MQTT}, Acc) ->
|
||||||
MQTT#message.payload, MQTT, IsCacheMode, WithContext, Acc)
|
deliver_to_coap(
|
||||||
end,
|
AlternatePath,
|
||||||
Session,
|
MQTT#message.payload,
|
||||||
Delivers).
|
MQTT,
|
||||||
|
IsCacheMode,
|
||||||
|
WithContext,
|
||||||
|
Acc
|
||||||
|
)
|
||||||
|
end,
|
||||||
|
Session,
|
||||||
|
Delivers
|
||||||
|
).
|
||||||
|
|
||||||
deliver_to_coap(AlternatePath, JsonData, MQTT, CacheMode, WithContext, Session) when is_binary(JsonData)->
|
deliver_to_coap(AlternatePath, JsonData, MQTT, CacheMode, WithContext, Session) when
|
||||||
|
is_binary(JsonData)
|
||||||
|
->
|
||||||
try
|
try
|
||||||
TermData = emqx_json:decode(JsonData, [return_maps]),
|
TermData = emqx_json:decode(JsonData, [return_maps]),
|
||||||
deliver_to_coap(AlternatePath, TermData, MQTT, CacheMode, WithContext, Session)
|
deliver_to_coap(AlternatePath, TermData, MQTT, CacheMode, WithContext, Session)
|
||||||
catch
|
catch
|
||||||
ExClass:Error:ST ->
|
ExClass:Error:ST ->
|
||||||
?SLOG(error, #{ msg => "invaild_json_format_to_deliver"
|
?SLOG(error, #{
|
||||||
, data => JsonData
|
msg => "invaild_json_format_to_deliver",
|
||||||
, reason => {ExClass, Error}
|
data => JsonData,
|
||||||
, stacktrace => ST
|
reason => {ExClass, Error},
|
||||||
}),
|
stacktrace => ST
|
||||||
|
}),
|
||||||
WithContext(metrics, 'delivery.dropped'),
|
WithContext(metrics, 'delivery.dropped'),
|
||||||
Session
|
Session
|
||||||
end;
|
end;
|
||||||
|
deliver_to_coap(AlternatePath, TermData, MQTT, CacheMode, WithContext, Session) when
|
||||||
deliver_to_coap(AlternatePath, TermData, MQTT, CacheMode, WithContext, Session) when is_map(TermData) ->
|
is_map(TermData)
|
||||||
|
->
|
||||||
WithContext(metrics, 'messages.delivered'),
|
WithContext(metrics, 'messages.delivered'),
|
||||||
{Req, Ctx} = emqx_lwm2m_cmd:mqtt_to_coap(AlternatePath, TermData),
|
{Req, Ctx} = emqx_lwm2m_cmd:mqtt_to_coap(AlternatePath, TermData),
|
||||||
ExpiryTime = get_expiry_time(MQTT),
|
ExpiryTime = get_expiry_time(MQTT),
|
||||||
Session2 = record_request(Ctx, Session),
|
Session2 = record_request(Ctx, Session),
|
||||||
maybe_do_deliver_to_coap(Ctx, Req, ExpiryTime, CacheMode, Session2).
|
maybe_do_deliver_to_coap(Ctx, Req, ExpiryTime, CacheMode, Session2).
|
||||||
|
|
||||||
maybe_do_deliver_to_coap(Ctx, Req, ExpiryTime, CacheMode,
|
maybe_do_deliver_to_coap(
|
||||||
#session{wait_ack = WaitAck,
|
Ctx,
|
||||||
queue = Queue} = Session) ->
|
Req,
|
||||||
|
ExpiryTime,
|
||||||
|
CacheMode,
|
||||||
|
#session{
|
||||||
|
wait_ack = WaitAck,
|
||||||
|
queue = Queue
|
||||||
|
} = Session
|
||||||
|
) ->
|
||||||
MHeaders = maps:get(mheaders, Ctx, #{}),
|
MHeaders = maps:get(mheaders, Ctx, #{}),
|
||||||
TTL = maps:get(<<"ttl">>, MHeaders, 7200),
|
TTL = maps:get(<<"ttl">>, MHeaders, 7200),
|
||||||
case TTL of
|
case TTL of
|
||||||
0 ->
|
0 ->
|
||||||
send_msg_not_waiting_ack(Ctx, Req, Session);
|
send_msg_not_waiting_ack(Ctx, Req, Session);
|
||||||
_ ->
|
_ ->
|
||||||
case not CacheMode
|
case
|
||||||
andalso queue:is_empty(Queue) andalso WaitAck =:= undefined of
|
not CacheMode andalso
|
||||||
|
queue:is_empty(Queue) andalso WaitAck =:= undefined
|
||||||
|
of
|
||||||
true ->
|
true ->
|
||||||
send_to_coap(Ctx, Req, Session);
|
send_to_coap(Ctx, Req, Session);
|
||||||
false ->
|
false ->
|
||||||
|
|
@ -706,8 +821,10 @@ maybe_do_deliver_to_coap(Ctx, Req, ExpiryTime, CacheMode,
|
||||||
end
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
get_expiry_time(#message{headers = #{properties := #{'Message-Expiry-Interval' := Interval}},
|
get_expiry_time(#message{
|
||||||
timestamp = Ts}) ->
|
headers = #{properties := #{'Message-Expiry-Interval' := Interval}},
|
||||||
|
timestamp = Ts
|
||||||
|
}) ->
|
||||||
Ts + Interval * 1000;
|
Ts + Interval * 1000;
|
||||||
get_expiry_time(_) ->
|
get_expiry_time(_) ->
|
||||||
0.
|
0.
|
||||||
|
|
@ -726,9 +843,11 @@ send_cmd_impl(Cmd, #session{reg_info = RegInfo} = Session) ->
|
||||||
%% Call CoAP
|
%% Call CoAP
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
call_coap(Fun, Msg, #session{coap = Coap} = Session) ->
|
call_coap(Fun, Msg, #session{coap = Coap} = Session) ->
|
||||||
iter([tm, fun process_tm/4, fun process_session/3],
|
iter(
|
||||||
emqx_coap_tm:Fun(Msg, Coap),
|
[tm, fun process_tm/4, fun process_session/3],
|
||||||
Session).
|
emqx_coap_tm:Fun(Msg, Coap),
|
||||||
|
Session
|
||||||
|
).
|
||||||
|
|
||||||
process_tm(TM, Result, Session, Cursor) ->
|
process_tm(TM, Result, Session, Cursor) ->
|
||||||
iter(Cursor, Result, Session#session{coap = TM}).
|
iter(Cursor, Result, Session#session{coap = TM}).
|
||||||
|
|
@ -758,10 +877,11 @@ return(#session{coap = CoAP} = Session) ->
|
||||||
|
|
||||||
do_out([{Ctx, Out} | T], TM, Msgs) ->
|
do_out([{Ctx, Out} | T], TM, Msgs) ->
|
||||||
%% TODO maybe set a special token?
|
%% TODO maybe set a special token?
|
||||||
#{out := [Msg],
|
#{
|
||||||
tm := TM2} = emqx_coap_tm:handle_out(Out, Ctx, TM),
|
out := [Msg],
|
||||||
|
tm := TM2
|
||||||
|
} = emqx_coap_tm:handle_out(Out, Ctx, TM),
|
||||||
do_out(T, TM2, [Msg | Msgs]);
|
do_out(T, TM2, [Msg | Msgs]);
|
||||||
|
|
||||||
do_out(_, TM, Msgs) ->
|
do_out(_, TM, Msgs) ->
|
||||||
{ok, TM, Msgs}.
|
{ok, TM, Msgs}.
|
||||||
|
|
||||||
|
|
@ -780,7 +900,7 @@ record_response(EventType, #{<<"data">> := Data}, Session) ->
|
||||||
Content = maps:get(<<"content">>, Data, undefined),
|
Content = maps:get(<<"content">>, Data, undefined),
|
||||||
record_cmd(ReqPath, EventType, {Code, CodeMsg, Content}, Session).
|
record_cmd(ReqPath, EventType, {Code, CodeMsg, Content}, Session).
|
||||||
|
|
||||||
record_cmd(Path, Type, Result, #session{cmd_record = #{queue := Queue} = Record} = Session) ->
|
record_cmd(Path, Type, Result, #session{cmd_record = #{queue := Queue} = Record} = Session) ->
|
||||||
Key = ?CMD_KEY(Path, Type),
|
Key = ?CMD_KEY(Path, Type),
|
||||||
Record2 = Record#{Key => Result},
|
Record2 = Record#{Key => Result},
|
||||||
Queue2 = queue:in(Key, Queue),
|
Queue2 = queue:in(Key, Queue),
|
||||||
|
|
@ -789,7 +909,6 @@ record_cmd(Path, Type, Result, #session{cmd_record = #{queue := Queue} = Record
|
||||||
|
|
||||||
check_record_size(Record, Queue) when ?RECORD_SIZE(Record) =< ?MAX_RECORD_SIZE ->
|
check_record_size(Record, Queue) when ?RECORD_SIZE(Record) =< ?MAX_RECORD_SIZE ->
|
||||||
Record#{queue := Queue};
|
Record#{queue := Queue};
|
||||||
|
|
||||||
check_record_size(Record, Queue) ->
|
check_record_size(Record, Queue) ->
|
||||||
{{value, Key}, Queue2} = queue:out(Queue),
|
{{value, Key}, Queue2} = queue:out(Queue),
|
||||||
Record2 = maps:remove(Key, Record),
|
Record2 = maps:remove(Key, Record),
|
||||||
|
|
|
||||||
|
|
@ -16,10 +16,10 @@
|
||||||
|
|
||||||
-module(emqx_lwm2m_tlv).
|
-module(emqx_lwm2m_tlv).
|
||||||
|
|
||||||
-export([ parse/1
|
-export([
|
||||||
, encode/1
|
parse/1,
|
||||||
]).
|
encode/1
|
||||||
|
]).
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
-export([binary_to_hex_string/1]).
|
-export([binary_to_hex_string/1]).
|
||||||
|
|
@ -27,15 +27,15 @@
|
||||||
|
|
||||||
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
||||||
|
|
||||||
-define(TLV_TYPE_OBJECT_INSTANCE, 0).
|
-define(TLV_TYPE_OBJECT_INSTANCE, 0).
|
||||||
-define(TLV_TYPE_RESOURCE_INSTANCE, 1).
|
-define(TLV_TYPE_RESOURCE_INSTANCE, 1).
|
||||||
-define(TLV_TYPE_MULTIPLE_RESOURCE, 2).
|
-define(TLV_TYPE_MULTIPLE_RESOURCE, 2).
|
||||||
-define(TLV_TYPE_RESOURCE_WITH_VALUE, 3).
|
-define(TLV_TYPE_RESOURCE_WITH_VALUE, 3).
|
||||||
|
|
||||||
-define(TLV_NO_LENGTH_FIELD, 0).
|
-define(TLV_NO_LENGTH_FIELD, 0).
|
||||||
-define(TLV_LEGNTH_8_BIT, 1).
|
-define(TLV_LEGNTH_8_BIT, 1).
|
||||||
-define(TLV_LEGNTH_16_BIT, 2).
|
-define(TLV_LEGNTH_16_BIT, 2).
|
||||||
-define(TLV_LEGNTH_24_BIT, 3).
|
-define(TLV_LEGNTH_24_BIT, 3).
|
||||||
|
|
||||||
%----------------------------------------------------------------------------------------------------------------------------------------
|
%----------------------------------------------------------------------------------------------------------------------------------------
|
||||||
% [#{tlv_object_instance := Id11, value := Value11}, #{tlv_object_instance := Id12, value := Value12}, ...]
|
% [#{tlv_object_instance := Id11, value := Value11}, #{tlv_object_instance := Id12, value := Value12}, ...]
|
||||||
|
|
@ -54,28 +54,33 @@
|
||||||
% NOTE: TLV does not has object level, only has object instance level. It implies TLV can not represent multiple objects
|
% NOTE: TLV does not has object level, only has object instance level. It implies TLV can not represent multiple objects
|
||||||
%----------------------------------------------------------------------------------------------------------------------------------------
|
%----------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
parse(Data) ->
|
parse(Data) ->
|
||||||
parse_loop(Data, []).
|
parse_loop(Data, []).
|
||||||
|
|
||||||
parse_loop(<<>>, Acc)->
|
parse_loop(<<>>, Acc) ->
|
||||||
lists:reverse(Acc);
|
lists:reverse(Acc);
|
||||||
parse_loop(Data, Acc) ->
|
parse_loop(Data, Acc) ->
|
||||||
{New, Rest} = parse_step1(Data),
|
{New, Rest} = parse_step1(Data),
|
||||||
parse_loop(Rest, [New|Acc]).
|
parse_loop(Rest, [New | Acc]).
|
||||||
|
|
||||||
parse_step1(<<?TLV_TYPE_OBJECT_INSTANCE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>) ->
|
parse_step1(<<?TLV_TYPE_OBJECT_INSTANCE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>) ->
|
||||||
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
||||||
{#{tlv_object_instance => Id, value => parse_loop(Value, [])}, Rest2};
|
{#{tlv_object_instance => Id, value => parse_loop(Value, [])}, Rest2};
|
||||||
parse_step1(<<?TLV_TYPE_RESOURCE_INSTANCE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>) ->
|
parse_step1(
|
||||||
|
<<?TLV_TYPE_RESOURCE_INSTANCE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>
|
||||||
|
) ->
|
||||||
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
||||||
{#{tlv_resource_instance => Id, value => Value}, Rest2};
|
{#{tlv_resource_instance => Id, value => Value}, Rest2};
|
||||||
parse_step1(<<?TLV_TYPE_MULTIPLE_RESOURCE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>) ->
|
parse_step1(
|
||||||
|
<<?TLV_TYPE_MULTIPLE_RESOURCE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>
|
||||||
|
) ->
|
||||||
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
||||||
{#{tlv_multiple_resource => Id, value => parse_loop(Value, [])}, Rest2};
|
{#{tlv_multiple_resource => Id, value => parse_loop(Value, [])}, Rest2};
|
||||||
parse_step1(<<?TLV_TYPE_RESOURCE_WITH_VALUE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>) ->
|
parse_step1(
|
||||||
|
<<?TLV_TYPE_RESOURCE_WITH_VALUE:2, IdLength:1, LengthType:2, InlineLength:3, Rest/binary>>
|
||||||
|
) ->
|
||||||
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
{Id, Value, Rest2} = parse_step2(id_length_bit_width(IdLength), LengthType, InlineLength, Rest),
|
||||||
{#{tlv_resource_with_value => Id, value => Value}, Rest2}.
|
{#{tlv_resource_with_value => Id, value => Value}, Rest2}.
|
||||||
|
|
||||||
parse_step2(IdLength, ?TLV_NO_LENGTH_FIELD, Length, Data) ->
|
parse_step2(IdLength, ?TLV_NO_LENGTH_FIELD, Length, Data) ->
|
||||||
<<Id:IdLength, Value:Length/binary, Rest/binary>> = Data,
|
<<Id:IdLength, Value:Length/binary, Rest/binary>> = Data,
|
||||||
|
|
@ -97,40 +102,48 @@ parse_step3(Id, Length, Data) ->
|
||||||
id_length_bit_width(0) -> 8;
|
id_length_bit_width(0) -> 8;
|
||||||
id_length_bit_width(1) -> 16.
|
id_length_bit_width(1) -> 16.
|
||||||
|
|
||||||
|
|
||||||
encode(TlvList) ->
|
encode(TlvList) ->
|
||||||
encode(TlvList, <<>>).
|
encode(TlvList, <<>>).
|
||||||
|
|
||||||
encode([], Acc) ->
|
encode([], Acc) ->
|
||||||
Acc;
|
Acc;
|
||||||
encode([#{tlv_object_instance := Id, value := Value}|T], Acc) ->
|
encode([#{tlv_object_instance := Id, value := Value} | T], Acc) ->
|
||||||
SubItems = encode(Value, <<>>),
|
SubItems = encode(Value, <<>>),
|
||||||
NewBinary = encode_body(?TLV_TYPE_OBJECT_INSTANCE, Id, SubItems),
|
NewBinary = encode_body(?TLV_TYPE_OBJECT_INSTANCE, Id, SubItems),
|
||||||
encode(T, <<Acc/binary, NewBinary/binary>>);
|
encode(T, <<Acc/binary, NewBinary/binary>>);
|
||||||
encode([#{tlv_resource_instance := Id, value := Value}|T], Acc) ->
|
encode([#{tlv_resource_instance := Id, value := Value} | T], Acc) ->
|
||||||
ValBinary = encode_value(Value),
|
ValBinary = encode_value(Value),
|
||||||
NewBinary = encode_body(?TLV_TYPE_RESOURCE_INSTANCE, Id, ValBinary),
|
NewBinary = encode_body(?TLV_TYPE_RESOURCE_INSTANCE, Id, ValBinary),
|
||||||
encode(T, <<Acc/binary, NewBinary/binary>>);
|
encode(T, <<Acc/binary, NewBinary/binary>>);
|
||||||
encode([#{tlv_multiple_resource := Id, value := Value}|T], Acc) ->
|
encode([#{tlv_multiple_resource := Id, value := Value} | T], Acc) ->
|
||||||
SubItems = encode(Value, <<>>),
|
SubItems = encode(Value, <<>>),
|
||||||
NewBinary = encode_body(?TLV_TYPE_MULTIPLE_RESOURCE, Id, SubItems),
|
NewBinary = encode_body(?TLV_TYPE_MULTIPLE_RESOURCE, Id, SubItems),
|
||||||
encode(T, <<Acc/binary, NewBinary/binary>>);
|
encode(T, <<Acc/binary, NewBinary/binary>>);
|
||||||
encode([#{tlv_resource_with_value := Id, value := Value}|T], Acc) ->
|
encode([#{tlv_resource_with_value := Id, value := Value} | T], Acc) ->
|
||||||
ValBinary = encode_value(Value),
|
ValBinary = encode_value(Value),
|
||||||
NewBinary = encode_body(?TLV_TYPE_RESOURCE_WITH_VALUE, Id, ValBinary),
|
NewBinary = encode_body(?TLV_TYPE_RESOURCE_WITH_VALUE, Id, ValBinary),
|
||||||
encode(T, <<Acc/binary, NewBinary/binary>>).
|
encode(T, <<Acc/binary, NewBinary/binary>>).
|
||||||
|
|
||||||
encode_body(Type, Id, Value) ->
|
encode_body(Type, Id, Value) ->
|
||||||
Size = byte_size(Value),
|
Size = byte_size(Value),
|
||||||
{IdLength, IdBinarySize, IdBinary} = if
|
{IdLength, IdBinarySize, IdBinary} =
|
||||||
Id < 256 -> {0, 1, <<Id:8>>};
|
if
|
||||||
true -> {1, 2, <<Id:16>>}
|
Id < 256 -> {0, 1, <<Id:8>>};
|
||||||
end,
|
true -> {1, 2, <<Id:16>>}
|
||||||
|
end,
|
||||||
if
|
if
|
||||||
Size < 8 -> <<Type:2, IdLength:1, ?TLV_NO_LENGTH_FIELD:2, Size:3, IdBinary:IdBinarySize/binary, Value:Size/binary>>;
|
Size < 8 ->
|
||||||
Size < 256 -> <<Type:2, IdLength:1, ?TLV_LEGNTH_8_BIT:2, 0:3, IdBinary:IdBinarySize/binary, Size:8, Value:Size/binary>>;
|
<<Type:2, IdLength:1, ?TLV_NO_LENGTH_FIELD:2, Size:3, IdBinary:IdBinarySize/binary,
|
||||||
Size < 65536 -> <<Type:2, IdLength:1, ?TLV_LEGNTH_16_BIT:2, 0:3, IdBinary:IdBinarySize/binary, Size:16, Value:Size/binary>>;
|
Value:Size/binary>>;
|
||||||
true -> <<Type:2, IdLength:1, ?TLV_LEGNTH_24_BIT:2, 0:3, IdBinary:IdBinarySize/binary, Size:24, Value:Size/binary>>
|
Size < 256 ->
|
||||||
|
<<Type:2, IdLength:1, ?TLV_LEGNTH_8_BIT:2, 0:3, IdBinary:IdBinarySize/binary, Size:8,
|
||||||
|
Value:Size/binary>>;
|
||||||
|
Size < 65536 ->
|
||||||
|
<<Type:2, IdLength:1, ?TLV_LEGNTH_16_BIT:2, 0:3, IdBinary:IdBinarySize/binary, Size:16,
|
||||||
|
Value:Size/binary>>;
|
||||||
|
true ->
|
||||||
|
<<Type:2, IdLength:1, ?TLV_LEGNTH_24_BIT:2, 0:3, IdBinary:IdBinarySize/binary, Size:24,
|
||||||
|
Value:Size/binary>>
|
||||||
end.
|
end.
|
||||||
|
|
||||||
encode_value(Value) when is_binary(Value) ->
|
encode_value(Value) when is_binary(Value) ->
|
||||||
|
|
@ -143,19 +156,16 @@ encode_value(false) ->
|
||||||
<<0>>;
|
<<0>>;
|
||||||
encode_value(Value) when is_integer(Value) ->
|
encode_value(Value) when is_integer(Value) ->
|
||||||
if
|
if
|
||||||
Value > -128, Value < 128 -> <<Value>>;
|
Value > -128, Value < 128 -> <<Value>>;
|
||||||
Value > -32768, Value < 32768 -> <<Value:16>>;
|
Value > -32768, Value < 32768 -> <<Value:16>>;
|
||||||
true -> <<Value:24>>
|
true -> <<Value:24>>
|
||||||
end;
|
end;
|
||||||
encode_value(Value) when is_float(Value) ->
|
encode_value(Value) when is_float(Value) ->
|
||||||
<<Value:32/float>>;
|
<<Value:32/float>>;
|
||||||
encode_value(Value) ->
|
encode_value(Value) ->
|
||||||
error(io_lib:format("unsupported format ~p", [Value])).
|
error(io_lib:format("unsupported format ~p", [Value])).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-ifdef(TEST).
|
-ifdef(TEST).
|
||||||
binary_to_hex_string(Data) ->
|
binary_to_hex_string(Data) ->
|
||||||
lists:flatten([io_lib:format("~2.16.0B ",[X]) || <<X:8>> <= Data ]).
|
lists:flatten([io_lib:format("~2.16.0B ", [X]) || <<X:8>> <= Data]).
|
||||||
-endif.
|
-endif.
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,14 +19,15 @@
|
||||||
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
-include("src/lwm2m/include/emqx_lwm2m.hrl").
|
||||||
-include_lib("xmerl/include/xmerl.hrl").
|
-include_lib("xmerl/include/xmerl.hrl").
|
||||||
|
|
||||||
-export([ get_obj_def/2
|
-export([
|
||||||
, get_object_id/1
|
get_obj_def/2,
|
||||||
, get_object_name/1
|
get_object_id/1,
|
||||||
, get_object_and_resource_id/2
|
get_object_name/1,
|
||||||
, get_resource_type/2
|
get_object_and_resource_id/2,
|
||||||
, get_resource_name/2
|
get_resource_type/2,
|
||||||
, get_resource_operations/2
|
get_resource_name/2,
|
||||||
]).
|
get_resource_operations/2
|
||||||
|
]).
|
||||||
|
|
||||||
% This module is for future use. Disabled now.
|
% This module is for future use. Disabled now.
|
||||||
|
|
||||||
|
|
@ -36,30 +37,38 @@ get_obj_def(ObjectNameStr, false) ->
|
||||||
emqx_lwm2m_xml_object_db:find_name(ObjectNameStr).
|
emqx_lwm2m_xml_object_db:find_name(ObjectNameStr).
|
||||||
|
|
||||||
get_object_id(ObjDefinition) ->
|
get_object_id(ObjDefinition) ->
|
||||||
[#xmlText{value=ObjectId}] = xmerl_xpath:string("ObjectID/text()", ObjDefinition),
|
[#xmlText{value = ObjectId}] = xmerl_xpath:string("ObjectID/text()", ObjDefinition),
|
||||||
ObjectId.
|
ObjectId.
|
||||||
|
|
||||||
get_object_name(ObjDefinition) ->
|
get_object_name(ObjDefinition) ->
|
||||||
[#xmlText{value=ObjectName}] = xmerl_xpath:string("Name/text()", ObjDefinition),
|
[#xmlText{value = ObjectName}] = xmerl_xpath:string("Name/text()", ObjDefinition),
|
||||||
ObjectName.
|
ObjectName.
|
||||||
|
|
||||||
get_object_and_resource_id(ResourceNameBinary, ObjDefinition) ->
|
get_object_and_resource_id(ResourceNameBinary, ObjDefinition) ->
|
||||||
ResourceNameString = binary_to_list(ResourceNameBinary),
|
ResourceNameString = binary_to_list(ResourceNameBinary),
|
||||||
[#xmlText{value=ObjectId}] = xmerl_xpath:string("ObjectID/text()", ObjDefinition),
|
[#xmlText{value = ObjectId}] = xmerl_xpath:string("ObjectID/text()", ObjDefinition),
|
||||||
[#xmlAttribute{value=ResourceId}] = xmerl_xpath:string("Resources/Item/Name[.=\""++ResourceNameString++"\"]/../@ID", ObjDefinition),
|
[#xmlAttribute{value = ResourceId}] = xmerl_xpath:string(
|
||||||
|
"Resources/Item/Name[.=\"" ++ ResourceNameString ++ "\"]/../@ID", ObjDefinition
|
||||||
|
),
|
||||||
{ObjectId, ResourceId}.
|
{ObjectId, ResourceId}.
|
||||||
|
|
||||||
get_resource_type(ResourceIdInt, ObjDefinition) ->
|
get_resource_type(ResourceIdInt, ObjDefinition) ->
|
||||||
ResourceIdString = integer_to_list(ResourceIdInt),
|
ResourceIdString = integer_to_list(ResourceIdInt),
|
||||||
[#xmlText{value=DataType}] = xmerl_xpath:string("Resources/Item[@ID=\""++ResourceIdString++"\"]/Type/text()", ObjDefinition),
|
[#xmlText{value = DataType}] = xmerl_xpath:string(
|
||||||
|
"Resources/Item[@ID=\"" ++ ResourceIdString ++ "\"]/Type/text()", ObjDefinition
|
||||||
|
),
|
||||||
DataType.
|
DataType.
|
||||||
|
|
||||||
get_resource_name(ResourceIdInt, ObjDefinition) ->
|
get_resource_name(ResourceIdInt, ObjDefinition) ->
|
||||||
ResourceIdString = integer_to_list(ResourceIdInt),
|
ResourceIdString = integer_to_list(ResourceIdInt),
|
||||||
[#xmlText{value=Name}] = xmerl_xpath:string("Resources/Item[@ID=\""++ResourceIdString++"\"]/Name/text()", ObjDefinition),
|
[#xmlText{value = Name}] = xmerl_xpath:string(
|
||||||
|
"Resources/Item[@ID=\"" ++ ResourceIdString ++ "\"]/Name/text()", ObjDefinition
|
||||||
|
),
|
||||||
Name.
|
Name.
|
||||||
|
|
||||||
get_resource_operations(ResourceIdInt, ObjDefinition) ->
|
get_resource_operations(ResourceIdInt, ObjDefinition) ->
|
||||||
ResourceIdString = integer_to_list(ResourceIdInt),
|
ResourceIdString = integer_to_list(ResourceIdInt),
|
||||||
[#xmlText{value=Operations}] = xmerl_xpath:string("Resources/Item[@ID=\""++ResourceIdString++"\"]/Operations/text()", ObjDefinition),
|
[#xmlText{value = Operations}] = xmerl_xpath:string(
|
||||||
|
"Resources/Item[@ID=\"" ++ ResourceIdString ++ "\"]/Operations/text()", ObjDefinition
|
||||||
|
),
|
||||||
Operations.
|
Operations.
|
||||||
|
|
|
||||||
|
|
@ -23,20 +23,22 @@
|
||||||
% This module is for future use. Disabled now.
|
% This module is for future use. Disabled now.
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
-export([ start_link/1
|
-export([
|
||||||
, stop/0
|
start_link/1,
|
||||||
, find_name/1
|
stop/0,
|
||||||
, find_objectid/1
|
find_name/1,
|
||||||
]).
|
find_objectid/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% gen_server.
|
%% gen_server.
|
||||||
-export([ init/1
|
-export([
|
||||||
, handle_call/3
|
init/1,
|
||||||
, handle_cast/2
|
handle_call/3,
|
||||||
, handle_info/2
|
handle_cast/2,
|
||||||
, terminate/2
|
handle_info/2,
|
||||||
, code_change/3
|
terminate/2,
|
||||||
]).
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
-define(LWM2M_OBJECT_DEF_TAB, lwm2m_object_def_tab).
|
-define(LWM2M_OBJECT_DEF_TAB, lwm2m_object_def_tab).
|
||||||
-define(LWM2M_OBJECT_NAME_TO_ID_TAB, lwm2m_object_name_to_id_tab).
|
-define(LWM2M_OBJECT_NAME_TO_ID_TAB, lwm2m_object_name_to_id_tab).
|
||||||
|
|
@ -47,29 +49,31 @@
|
||||||
%% API Function Definitions
|
%% API Function Definitions
|
||||||
%% ------------------------------------------------------------------
|
%% ------------------------------------------------------------------
|
||||||
|
|
||||||
-spec start_link(string())
|
-spec start_link(string()) ->
|
||||||
-> {ok, pid()}
|
{ok, pid()}
|
||||||
| ignore
|
| ignore
|
||||||
| {error, no_xml_files_found}
|
| {error, no_xml_files_found}
|
||||||
| {error, term()}.
|
| {error, term()}.
|
||||||
start_link(XmlDir) ->
|
start_link(XmlDir) ->
|
||||||
gen_server:start_link({local, ?MODULE}, ?MODULE, [XmlDir], []).
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [XmlDir], []).
|
||||||
|
|
||||||
find_objectid(ObjectId) ->
|
find_objectid(ObjectId) ->
|
||||||
ObjectIdInt = case is_list(ObjectId) of
|
ObjectIdInt =
|
||||||
true -> list_to_integer(ObjectId);
|
case is_list(ObjectId) of
|
||||||
false -> ObjectId
|
true -> list_to_integer(ObjectId);
|
||||||
end,
|
false -> ObjectId
|
||||||
|
end,
|
||||||
case ets:lookup(?LWM2M_OBJECT_DEF_TAB, ObjectIdInt) of
|
case ets:lookup(?LWM2M_OBJECT_DEF_TAB, ObjectIdInt) of
|
||||||
[] -> {error, no_xml_definition};
|
[] -> {error, no_xml_definition};
|
||||||
[{ObjectId, Xml}] -> Xml
|
[{ObjectId, Xml}] -> Xml
|
||||||
end.
|
end.
|
||||||
|
|
||||||
find_name(Name) ->
|
find_name(Name) ->
|
||||||
NameBinary = case is_list(Name) of
|
NameBinary =
|
||||||
true -> list_to_binary(Name);
|
case is_list(Name) of
|
||||||
false -> Name
|
true -> list_to_binary(Name);
|
||||||
end,
|
false -> Name
|
||||||
|
end,
|
||||||
case ets:lookup(?LWM2M_OBJECT_NAME_TO_ID_TAB, NameBinary) of
|
case ets:lookup(?LWM2M_OBJECT_NAME_TO_ID_TAB, NameBinary) of
|
||||||
[] ->
|
[] ->
|
||||||
undefined;
|
undefined;
|
||||||
|
|
@ -93,7 +97,8 @@ init([XmlDir]) ->
|
||||||
case load(XmlDir) of
|
case load(XmlDir) of
|
||||||
ok ->
|
ok ->
|
||||||
{ok, #state{}};
|
{ok, #state{}};
|
||||||
{error, Reason} -> {stop, Reason}
|
{error, Reason} ->
|
||||||
|
{stop, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
handle_call(_Request, _From, State) ->
|
handle_call(_Request, _From, State) ->
|
||||||
|
|
@ -118,11 +123,13 @@ code_change(_OldVsn, State, _Extra) ->
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
load(BaseDir) ->
|
load(BaseDir) ->
|
||||||
Wild = filename:join(BaseDir, "*.xml"),
|
Wild = filename:join(BaseDir, "*.xml"),
|
||||||
Wild2 = if is_binary(Wild) ->
|
Wild2 =
|
||||||
erlang:binary_to_list(Wild);
|
if
|
||||||
true ->
|
is_binary(Wild) ->
|
||||||
Wild
|
erlang:binary_to_list(Wild);
|
||||||
end,
|
true ->
|
||||||
|
Wild
|
||||||
|
end,
|
||||||
case filelib:wildcard(Wild2) of
|
case filelib:wildcard(Wild2) of
|
||||||
[] -> {error, no_xml_files_found};
|
[] -> {error, no_xml_files_found};
|
||||||
AllXmlFiles -> load_loop(AllXmlFiles)
|
AllXmlFiles -> load_loop(AllXmlFiles)
|
||||||
|
|
@ -130,17 +137,18 @@ load(BaseDir) ->
|
||||||
|
|
||||||
load_loop([]) ->
|
load_loop([]) ->
|
||||||
ok;
|
ok;
|
||||||
load_loop([FileName|T]) ->
|
load_loop([FileName | T]) ->
|
||||||
ObjectXml = load_xml(FileName),
|
ObjectXml = load_xml(FileName),
|
||||||
[#xmlText{value=ObjectIdString}] = xmerl_xpath:string("ObjectID/text()", ObjectXml),
|
[#xmlText{value = ObjectIdString}] = xmerl_xpath:string("ObjectID/text()", ObjectXml),
|
||||||
[#xmlText{value=Name}] = xmerl_xpath:string("Name/text()", ObjectXml),
|
[#xmlText{value = Name}] = xmerl_xpath:string("Name/text()", ObjectXml),
|
||||||
ObjectId = list_to_integer(ObjectIdString),
|
ObjectId = list_to_integer(ObjectIdString),
|
||||||
NameBinary = list_to_binary(Name),
|
NameBinary = list_to_binary(Name),
|
||||||
?SLOG(debug, #{ msg => "load_object_succeed"
|
?SLOG(debug, #{
|
||||||
, filename => FileName
|
msg => "load_object_succeed",
|
||||||
, object_id => ObjectId
|
filename => FileName,
|
||||||
, object_name => NameBinary
|
object_id => ObjectId,
|
||||||
}),
|
object_name => NameBinary
|
||||||
|
}),
|
||||||
ets:insert(?LWM2M_OBJECT_DEF_TAB, {ObjectId, ObjectXml}),
|
ets:insert(?LWM2M_OBJECT_DEF_TAB, {ObjectId, ObjectXml}),
|
||||||
ets:insert(?LWM2M_OBJECT_NAME_TO_ID_TAB, {NameBinary, ObjectId}),
|
ets:insert(?LWM2M_OBJECT_NAME_TO_ID_TAB, {NameBinary, ObjectId}),
|
||||||
load_loop(T).
|
load_loop(T).
|
||||||
|
|
|
||||||
|
|
@ -16,29 +16,28 @@
|
||||||
|
|
||||||
-define(LWAPP, emqx_lwm2m).
|
-define(LWAPP, emqx_lwm2m).
|
||||||
|
|
||||||
|
|
||||||
-define(OMA_ALTER_PATH_RT, <<"\"oma.lwm2m\"">>).
|
-define(OMA_ALTER_PATH_RT, <<"\"oma.lwm2m\"">>).
|
||||||
|
|
||||||
-define(MQ_COMMAND_ID, <<"CmdID">>).
|
-define(MQ_COMMAND_ID, <<"CmdID">>).
|
||||||
-define(MQ_COMMAND, <<"requestID">>).
|
-define(MQ_COMMAND, <<"requestID">>).
|
||||||
-define(MQ_BASENAME, <<"BaseName">>).
|
-define(MQ_BASENAME, <<"BaseName">>).
|
||||||
-define(MQ_ARGS, <<"Arguments">>).
|
-define(MQ_ARGS, <<"Arguments">>).
|
||||||
|
|
||||||
-define(MQ_VALUE_TYPE, <<"ValueType">>).
|
-define(MQ_VALUE_TYPE, <<"ValueType">>).
|
||||||
-define(MQ_VALUE, <<"Value">>).
|
-define(MQ_VALUE, <<"Value">>).
|
||||||
-define(MQ_ERROR, <<"Error">>).
|
-define(MQ_ERROR, <<"Error">>).
|
||||||
-define(MQ_RESULT, <<"Result">>).
|
-define(MQ_RESULT, <<"Result">>).
|
||||||
|
|
||||||
-define(ERR_NO_XML, <<"No XML Definition">>).
|
-define(ERR_NO_XML, <<"No XML Definition">>).
|
||||||
-define(ERR_NOT_ACCEPTABLE, <<"Not Acceptable">>).
|
-define(ERR_NOT_ACCEPTABLE, <<"Not Acceptable">>).
|
||||||
-define(ERR_METHOD_NOT_ALLOWED, <<"Method Not Allowed">>).
|
-define(ERR_METHOD_NOT_ALLOWED, <<"Method Not Allowed">>).
|
||||||
-define(ERR_NOT_FOUND, <<"Not Found">>).
|
-define(ERR_NOT_FOUND, <<"Not Found">>).
|
||||||
-define(ERR_UNAUTHORIZED, <<"Unauthorized">>).
|
-define(ERR_UNAUTHORIZED, <<"Unauthorized">>).
|
||||||
-define(ERR_BAD_REQUEST, <<"Bad Request">>).
|
-define(ERR_BAD_REQUEST, <<"Bad Request">>).
|
||||||
-define(REG_PREFIX, <<"rd">>).
|
-define(REG_PREFIX, <<"rd">>).
|
||||||
|
|
||||||
-define(LWM2M_FORMAT_PLAIN_TEXT, 0).
|
-define(LWM2M_FORMAT_PLAIN_TEXT, 0).
|
||||||
-define(LWM2M_FORMAT_LINK, 40).
|
-define(LWM2M_FORMAT_LINK, 40).
|
||||||
-define(LWM2M_FORMAT_OPAQUE, 42).
|
-define(LWM2M_FORMAT_OPAQUE, 42).
|
||||||
-define(LWM2M_FORMAT_TLV, 11542).
|
-define(LWM2M_FORMAT_TLV, 11542).
|
||||||
-define(LWMWM_FORMAT_JSON, 11543).
|
-define(LWMWM_FORMAT_JSON, 11543).
|
||||||
|
|
|
||||||
|
|
@ -21,28 +21,35 @@
|
||||||
-include("src/mqttsn/include/emqx_sn.hrl").
|
-include("src/mqttsn/include/emqx_sn.hrl").
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
-export([ start_link/2
|
-export([
|
||||||
, stop/0
|
start_link/2,
|
||||||
]).
|
stop/0
|
||||||
|
]).
|
||||||
|
|
||||||
%% gen_server
|
%% gen_server
|
||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
-export([
|
||||||
terminate/2, code_change/3]).
|
init/1,
|
||||||
|
handle_call/3,
|
||||||
|
handle_cast/2,
|
||||||
|
handle_info/2,
|
||||||
|
terminate/2,
|
||||||
|
code_change/3
|
||||||
|
]).
|
||||||
|
|
||||||
-record(state, {gwid, sock, port, addrs, duration, tref}).
|
-record(state, {gwid, sock, port, addrs, duration, tref}).
|
||||||
|
|
||||||
-define(DEFAULT_DURATION, 15*60*1000).
|
-define(DEFAULT_DURATION, 15 * 60 * 1000).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% API
|
%% API
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
-spec(start_link(pos_integer(), inet:port_number())
|
-spec start_link(pos_integer(), inet:port_number()) ->
|
||||||
-> {ok, pid()} | {error, term()}).
|
{ok, pid()} | {error, term()}.
|
||||||
start_link(GwId, Port) ->
|
start_link(GwId, Port) ->
|
||||||
gen_server:start_link({local, ?MODULE}, ?MODULE, [GwId, Port], []).
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [GwId, Port], []).
|
||||||
|
|
||||||
-spec(stop() -> ok).
|
-spec stop() -> ok.
|
||||||
stop() ->
|
stop() ->
|
||||||
gen_server:stop(?MODULE, nomal, infinity).
|
gen_server:stop(?MODULE, nomal, infinity).
|
||||||
|
|
||||||
|
|
@ -54,36 +61,44 @@ init([GwId, Port]) ->
|
||||||
%% FIXME:
|
%% FIXME:
|
||||||
Duration = application:get_env(emqx_sn, advertise_duration, ?DEFAULT_DURATION),
|
Duration = application:get_env(emqx_sn, advertise_duration, ?DEFAULT_DURATION),
|
||||||
{ok, Sock} = gen_udp:open(0, [binary, {broadcast, true}]),
|
{ok, Sock} = gen_udp:open(0, [binary, {broadcast, true}]),
|
||||||
{ok, ensure_advertise(#state{gwid = GwId, addrs = boradcast_addrs(),
|
{ok,
|
||||||
sock = Sock, port = Port, duration = Duration})}.
|
ensure_advertise(#state{
|
||||||
|
gwid = GwId,
|
||||||
|
addrs = boradcast_addrs(),
|
||||||
|
sock = Sock,
|
||||||
|
port = Port,
|
||||||
|
duration = Duration
|
||||||
|
})}.
|
||||||
|
|
||||||
handle_call(Req, _From, State) ->
|
handle_call(Req, _From, State) ->
|
||||||
?SLOG(error, #{ msg => "unexpected_call"
|
?SLOG(error, #{
|
||||||
, call => Req
|
msg => "unexpected_call",
|
||||||
}),
|
call => Req
|
||||||
{reply, ignored, State}.
|
}),
|
||||||
|
{reply, ignored, State}.
|
||||||
|
|
||||||
handle_cast(Msg, State) ->
|
handle_cast(Msg, State) ->
|
||||||
?SLOG(error, #{ msg => "unexpected_cast"
|
?SLOG(error, #{
|
||||||
, cast => Msg
|
msg => "unexpected_cast",
|
||||||
}),
|
cast => Msg
|
||||||
{noreply, State}.
|
}),
|
||||||
|
{noreply, State}.
|
||||||
|
|
||||||
handle_info(broadcast_advertise, State) ->
|
handle_info(broadcast_advertise, State) ->
|
||||||
{noreply, ensure_advertise(State), hibernate};
|
{noreply, ensure_advertise(State), hibernate};
|
||||||
|
|
||||||
handle_info(Info, State) ->
|
handle_info(Info, State) ->
|
||||||
?SLOG(error, #{ msg => "unexpected_info"
|
?SLOG(error, #{
|
||||||
, info => Info
|
msg => "unexpected_info",
|
||||||
}),
|
info => Info
|
||||||
{noreply, State}.
|
}),
|
||||||
|
{noreply, State}.
|
||||||
|
|
||||||
terminate(_Reason, #state{tref = Timer}) ->
|
terminate(_Reason, #state{tref = Timer}) ->
|
||||||
_ = erlang:cancel_timer(Timer),
|
_ = erlang:cancel_timer(Timer),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
code_change(_OldVsn, State, _Extra) ->
|
code_change(_OldVsn, State, _Extra) ->
|
||||||
{ok, State}.
|
{ok, State}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Internal Functions
|
%% Internal Functions
|
||||||
|
|
@ -93,17 +108,29 @@ ensure_advertise(State = #state{duration = Duration}) ->
|
||||||
send_advertise(State),
|
send_advertise(State),
|
||||||
State#state{tref = erlang:send_after(Duration, self(), broadcast_advertise)}.
|
State#state{tref = erlang:send_after(Duration, self(), broadcast_advertise)}.
|
||||||
|
|
||||||
send_advertise(#state{gwid = GwId, sock = Sock, port = Port,
|
send_advertise(#state{
|
||||||
addrs = Addrs, duration = Duration}) ->
|
gwid = GwId,
|
||||||
|
sock = Sock,
|
||||||
|
port = Port,
|
||||||
|
addrs = Addrs,
|
||||||
|
duration = Duration
|
||||||
|
}) ->
|
||||||
Data = emqx_sn_frame:serialize_pkt(?SN_ADVERTISE_MSG(GwId, Duration), #{}),
|
Data = emqx_sn_frame:serialize_pkt(?SN_ADVERTISE_MSG(GwId, Duration), #{}),
|
||||||
lists:foreach(fun(Addr) ->
|
lists:foreach(
|
||||||
?SLOG(debug, #{ msg => "send_ADVERTISE_msg"
|
fun(Addr) ->
|
||||||
, address => Addr
|
?SLOG(debug, #{
|
||||||
}),
|
msg => "send_ADVERTISE_msg",
|
||||||
gen_udp:send(Sock, Addr, Port, Data)
|
address => Addr
|
||||||
end, Addrs).
|
}),
|
||||||
|
gen_udp:send(Sock, Addr, Port, Data)
|
||||||
|
end,
|
||||||
|
Addrs
|
||||||
|
).
|
||||||
|
|
||||||
boradcast_addrs() ->
|
boradcast_addrs() ->
|
||||||
lists:usort([Addr || {ok, IfList} <- [inet:getiflist()], If <- IfList,
|
lists:usort([
|
||||||
{ok, [{broadaddr, Addr}]} <- [inet:ifget(If, [broadaddr])]]).
|
Addr
|
||||||
|
|| {ok, IfList} <- [inet:getiflist()],
|
||||||
|
If <- IfList,
|
||||||
|
{ok, [{broadaddr, Addr}]} <- [inet:ifget(If, [broadaddr])]
|
||||||
|
]).
|
||||||
|
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue