refactor(psk): create the ets table in supervisor process
This commit is contained in:
parent
92356b85f3
commit
c05ce82933
|
@ -0,0 +1,25 @@
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Copyright (c) 2022 EMQ Technologies Co., Ltd. All Rights Reserved.
|
||||||
|
%%
|
||||||
|
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
%% you may not use this file except in compliance with the License.
|
||||||
|
%% You may obtain a copy of the License at
|
||||||
|
%%
|
||||||
|
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
%%
|
||||||
|
%% Unless required by applicable law or agreed to in writing, software
|
||||||
|
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
%% See the License for the specific language governing permissions and
|
||||||
|
%% limitations under the License.
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
|
-ifndef(EMQX_PSK_FILE).
|
||||||
|
-define(EMQX_PSK_FILE, true).
|
||||||
|
|
||||||
|
-define(PSK_FILE_TAB, emqx_psk_file).
|
||||||
|
|
||||||
|
-record(psk_entry, {psk_id :: binary(),
|
||||||
|
psk_str :: binary()}).
|
||||||
|
|
||||||
|
-endif.
|
|
@ -1,6 +1,6 @@
|
||||||
{application, emqx_psk_file,
|
{application, emqx_psk_file,
|
||||||
[{description,"EMQX PSK Plugin from File"},
|
[{description,"EMQX PSK Plugin from File"},
|
||||||
{vsn, "4.3.0"}, % strict semver, bump manually!
|
{vsn, "4.3.1"}, % strict semver, bump manually!
|
||||||
{modules,[]},
|
{modules,[]},
|
||||||
{registered,[emqx_psk_file_sup]},
|
{registered,[emqx_psk_file_sup]},
|
||||||
{applications,[kernel,stdlib]},
|
{applications,[kernel,stdlib]},
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
%% -*- mode: erlang -*-
|
||||||
|
{VSN,
|
||||||
|
[{"4.3.0",
|
||||||
|
[{load_module,emqx_psk_file,brutal_purge,soft_purge,[]},
|
||||||
|
{load_module,emqx_psk_file_sup,brutal_purge,soft_purge,[]}]}
|
||||||
|
],
|
||||||
|
[{"4.3.0",
|
||||||
|
[{load_module,emqx_psk_file,brutal_purge,soft_purge,[]},
|
||||||
|
{load_module,emqx_psk_file_sup,brutal_purge,soft_purge,[]}]}
|
||||||
|
]}.
|
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
-module(emqx_psk_file).
|
-module(emqx_psk_file).
|
||||||
|
|
||||||
|
-include("emqx_psk_file.hrl").
|
||||||
-include_lib("emqx/include/emqx.hrl").
|
-include_lib("emqx/include/emqx.hrl").
|
||||||
-include_lib("emqx/include/logger.hrl").
|
-include_lib("emqx/include/logger.hrl").
|
||||||
|
|
||||||
|
@ -26,15 +27,10 @@
|
||||||
%% Hooks functions
|
%% Hooks functions
|
||||||
-export([on_psk_lookup/2]).
|
-export([on_psk_lookup/2]).
|
||||||
|
|
||||||
-define(TAB, ?MODULE).
|
|
||||||
-define(LF, 10).
|
-define(LF, 10).
|
||||||
|
|
||||||
-record(psk_entry, {psk_id :: binary(),
|
|
||||||
psk_str :: binary()}).
|
|
||||||
|
|
||||||
%% Called when the plugin application start
|
%% Called when the plugin application start
|
||||||
load(Env) ->
|
load(Env) ->
|
||||||
_ = ets:new(?TAB, [set, named_table, {keypos, #psk_entry.psk_id}]),
|
|
||||||
{ok, PskFile} = file:open(get_value(path, Env), [read, raw, binary, read_ahead]),
|
{ok, PskFile} = file:open(get_value(path, Env), [read, raw, binary, read_ahead]),
|
||||||
preload_psks(PskFile, bin(get_value(delimiter, Env))),
|
preload_psks(PskFile, bin(get_value(delimiter, Env))),
|
||||||
_ = file:close(PskFile),
|
_ = file:close(PskFile),
|
||||||
|
@ -45,7 +41,7 @@ unload() ->
|
||||||
emqx:unhook('tls_handshake.psk_lookup', fun ?MODULE:on_psk_lookup/2).
|
emqx:unhook('tls_handshake.psk_lookup', fun ?MODULE:on_psk_lookup/2).
|
||||||
|
|
||||||
on_psk_lookup(ClientPSKID, UserState) ->
|
on_psk_lookup(ClientPSKID, UserState) ->
|
||||||
case ets:lookup(?TAB, ClientPSKID) of
|
case ets:lookup(?PSK_FILE_TAB, ClientPSKID) of
|
||||||
[#psk_entry{psk_str = PskStr}] ->
|
[#psk_entry{psk_str = PskStr}] ->
|
||||||
{stop, PskStr};
|
{stop, PskStr};
|
||||||
[] ->
|
[] ->
|
||||||
|
@ -57,7 +53,9 @@ preload_psks(FileHandler, Delimiter) ->
|
||||||
{ok, Line} ->
|
{ok, Line} ->
|
||||||
case binary:split(Line, Delimiter) of
|
case binary:split(Line, Delimiter) of
|
||||||
[Key, Rem] ->
|
[Key, Rem] ->
|
||||||
ets:insert(?TAB, #psk_entry{psk_id = Key, psk_str = trim_lf(Rem)}),
|
ets:insert(
|
||||||
|
?PSK_FILE_TAB,
|
||||||
|
#psk_entry{psk_id = Key, psk_str = trim_lf(Rem)}),
|
||||||
preload_psks(FileHandler, Delimiter);
|
preload_psks(FileHandler, Delimiter);
|
||||||
[Line] ->
|
[Line] ->
|
||||||
?LOG(warning, "[~p] - Invalid line: ~p, delimiter: ~p", [?MODULE, Line, Delimiter])
|
?LOG(warning, "[~p] - Invalid line: ~p, delimiter: ~p", [?MODULE, Line, Delimiter])
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
|
|
||||||
-module(emqx_psk_file_sup).
|
-module(emqx_psk_file_sup).
|
||||||
|
|
||||||
|
-include("emqx_psk_file.hrl").
|
||||||
|
|
||||||
-behaviour(supervisor).
|
-behaviour(supervisor).
|
||||||
|
|
||||||
%% API
|
%% API
|
||||||
|
@ -25,8 +27,11 @@
|
||||||
-export([init/1]).
|
-export([init/1]).
|
||||||
|
|
||||||
start_link() ->
|
start_link() ->
|
||||||
|
_ = ets:new(
|
||||||
|
?PSK_FILE_TAB,
|
||||||
|
[set, named_table, public, {keypos, #psk_entry.psk_id}]
|
||||||
|
),
|
||||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||||
|
|
||||||
init([]) ->
|
init([]) ->
|
||||||
{ok, { {one_for_one, 0, 1}, []} }.
|
{ok, { {one_for_one, 0, 1}, []} }.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue