处理交互逻辑

This commit is contained in:
anlicheng 2023-02-18 22:18:12 +08:00
parent c79a6edad4
commit cf90f98a9c
7 changed files with 173 additions and 41 deletions

View File

@ -66,28 +66,8 @@
update_ts = 0 :: integer()
}).
%%
-record(host, {
%% ID
id :: binary(),
%%
name :: binary(),
%%
model :: binary(),
%%
cell_id :: integer(),
%%
activated_ts = 0 :: integer(),
%% 线
update_ts = 0 :: integer(),
%%
status = ?HOST_STATUS_INACTIVE :: integer()
}).
%% ,
-record(host_metric, {
%% ID
host_id :: binary(),
%% cpu相关
cpus = [],
%% cpu温度
@ -100,6 +80,27 @@
interfaces = []
}).
%%
-record(host, {
%% ID
id :: binary(),
%%
name :: binary(),
%%
model :: binary(),
%%
cell_id :: integer(),
%% aes的key,
aes :: binary(),
metric = #host_metric{},
%%
activated_ts = 0 :: integer(),
%% 线
update_ts = 0 :: integer(),
%%
status = ?HOST_STATUS_INACTIVE :: integer()
}).
%%
-record(terminal, {
%% ID

View File

@ -0,0 +1,23 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2018, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 29. 2018 09:30
%%%-------------------------------------------------------------------
-module(iot_cipher_aes).
-author("aresei").
%% API
-export([encrypt/3, decrypt/3]).
%% aes的加密算法
-spec encrypt(binary(), binary(), binary()) -> binary().
encrypt(Key, IVec, PlainText) when is_binary(Key), is_binary(IVec), is_binary(PlainText) ->
crypto:crypto_one_time(aes_128_cfb128, Key, IVec, PlainText, true).
%% aes的解密算法
-spec decrypt(binary(), binary(), binary()) -> binary().
decrypt(Key, IVec, CipherText) when is_binary(Key), is_binary(IVec), is_binary(CipherText) ->
crypto:crypto_one_time(aes_128_cfb128, Key, IVec, CipherText, false).

View File

@ -0,0 +1,22 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2018, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 21. 2018 09:51
%%%-------------------------------------------------------------------
-module(iot_cipher_rsa).
-author("aresei").
%% API
-export([decode/1, encode/2]).
%%
decode(EncBin) when is_binary(EncBin) ->
PrivateKey = private_key(),
public_key:decrypt_private(EncBin, PrivateKey).
%%
encode(Data, PublicKey) when is_binary(Data), is_binary(PublicKey) ->
ok.

View File

@ -0,0 +1,93 @@
%%%-------------------------------------------------------------------
%%% @author aresei
%%% @copyright (C) 2023, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 18. 2 2023 21:39
%%%-------------------------------------------------------------------
-module(iot_message_handler).
-author("aresei").
-include("iot.hrl").
%% API
-export([]).
handle(<<"server.register">>, Msg = #{<<"c_id">> := ClientId, <<"r">> := PubKey,
<<"m">> := #{<<"cpu_core">> := CpuCore, <<"memory">> := Memory, <<"disk">> := Disk, <<"boot_time">> := BootTime, <<"efka_version">> := EfkaVersion, <<"kernel_arch">> := KernelArch, <<"ipv4_1">> := Ip1, <<"ipv4_2">> := Ip2}}) ->
Aes = iot_util:rand_bytes(16),
Host = #host{
id = ClientId,
aes = Aes,
metric = #host_metric{
%% cpu相关
cpus = [
#cpu_metric{
%% cpu编号
num = 1,
%%
load = 0
},
#cpu_metric{
%% cpu编号
num = 2,
%%
load = 0
}
],
%% cpu温度
cpu_temperature = 0,
%%
memory = #memory_metric{
%% 使
used = 0,
%%
total = Memory
},
%%
disk = #disk_metric{
total = Disk
},
%%
interfaces = []
},
activated_ts = iot_util:current_time(),
update_ts = iot_util:current_time(),
status = ?HOST_STATUS_INACTIVE
},
case host_model:add_host(Host) of
ok ->
Reply = #{
<<"a">> => true,
<<"aes">> => Aes,
<<"reply">> => <<"client.reply.", ClientId/binary>>
},
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
lager:debug("enc_reply is: ~p", [EncReply]);
{error, Reason} ->
lager:debug("register error is: ~p", [Reason]),
Reply = #{
<<"a">> => false,
<<"aes">> => <<"">>,
<<"reply">> => <<"client.reply.", ClientId/binary>>
},
EncReply = iot_cipher_rsa:encode(Reply, PubKey),
lager:debug("enc_reply is: ~p", [EncReply])
end;
handle(<<"server.data">>, #{<<"c_id">> := HostId, <<"d">> := Data}) ->
{ok, Host = #host{aes = Aes}} = host_model:get_host(HostId),
PlainData = iot_cipher_aes:decrypt(Aes, Aes, Data),
case jiffy:decode(PlainData, [return_maps]) of
Metrics when is_list(Metrics) ->
lager:debug("the metric is: ~p", [Metrics]),
ok;
_ ->
lager:debug("the metric is invalid json")
end,
ok.

View File

@ -32,14 +32,6 @@ init_database() ->
{type, set}
]),
%%
mnesia:create_table(host_metric, [
{attributes, record_info(fields, host_metric)},
{record_name, host_metric},
{disc_copies, [node()]},
{type, set}
]),
%%
mnesia:create_table(terminal, [
{attributes, record_info(fields, terminal)},
@ -75,7 +67,6 @@ copy_database(MasterNode) when is_atom(MasterNode) ->
%%
mnesia:add_table_copy(host, node(), ram_copies),
mnesia:add_table_copy(host_metric, node(), ram_copies),
mnesia:add_table_copy(terminal, node(), ram_copies),
mnesia:add_table_copy(microservice, node(), ram_copies),
ok.

View File

@ -11,7 +11,7 @@
%% API
-export([timestamp/0, number_format/2, current_time/0]).
-export([step/3, chunks/2, rand_bytes/1]).
-export([step/3, chunks/2, rand_bytes/1, uuid/0]).
-export([json_data/1, json_error/2]).
-export([queue_limited_in/3]).
@ -64,6 +64,9 @@ json_error(ErrCode, ErrMessage) when is_integer(ErrCode), is_binary(ErrMessage)
}
}, [force_utf8]).
uuid() ->
rand_bytes(16).
rand_bytes(Size) when is_integer(Size) ->
Bytes = crypto:strong_rand_bytes(Size),
S = lists:flatten([integer_to_list(E, 16) || <<E:4>> <= Bytes]),

View File

@ -12,7 +12,15 @@
-include_lib("stdlib/include/qlc.hrl").
%% API
-export([get_hosts/2, insert/4, change_status/2, delete/1, table_size/0, filter_hosts/3]).
-export([get_host/1, get_hosts/2, add_host/1, change_status/2, delete/1, table_size/0, filter_hosts/3]).
get_host(HostId) when is_binary(HostId) ->
case mnesia:dirty_read(host, HostId) of
[Host = #host{}] ->
{ok, Host};
_ ->
undefined
end.
%% app信息
get_hosts(Start, Limit) when is_integer(Limit), is_integer(Start), Start >= 0, Limit > 0 ->
@ -57,16 +65,7 @@ filter_hosts(Pred, Start, Limit) when is_function(Pred, 1), is_integer(Limit), i
Error
end.
insert(Id, Name, Model, CellId) ->
Host = #host{
id = Id,
name = Name,
model = Model,
cell_id = CellId,
update_ts = 0,
status = ?HOST_STATUS_INACTIVE
},
add_host(Host = #host{}) ->
case mnesia:transaction(fun() -> mnesia:write(host, Host, write) end) of
{atomic, _} ->
ok;