diff --git a/src/quic/sdlan_quic_channel.erl b/src/quic/sdlan_quic_channel.erl index 858b69a..90ee0d3 100644 --- a/src/quic/sdlan_quic_channel.erl +++ b/src/quic/sdlan_quic_channel.erl @@ -56,7 +56,6 @@ pending_commands = #{}, ping_counter = 0, - ipv6_assist = undefined, %% 离线回调函数 offline_cb :: undefined | fun() @@ -101,8 +100,7 @@ start_link(Conn, Limits) when is_list(Limits) -> init([Conn, Limits]) -> MaxPacketSize = proplists:get_value(max_packet_size, Limits, 16384), HeartbeatSec = proplists:get_value(heartbeat_sec, Limits, 10), - Ipv6Assist = ipv6_assist_info(), - {ok, initializing, #state{conn = Conn, max_packet_size = MaxPacketSize, heartbeat_sec = HeartbeatSec, ipv6_assist = Ipv6Assist}, [{next_event, internal, do_init}]}. + {ok, initializing, #state{conn = Conn, max_packet_size = MaxPacketSize, heartbeat_sec = HeartbeatSec}, [{next_event, internal, do_init}]}. %% @private %% @doc This function is called by a gen_statem when it needs to find out @@ -125,8 +123,17 @@ handle_event(info, {quic, dgram_state_changed, Conn, Opts = #{dgram_send_enabled logger:debug("[sdlan_quic_channel] dgram_state_changed, opts: ~p", [Opts]), {keep_state, State}; -handle_event(info, {quic, new_stream, Stream, Opts}, waiting_stream, State=#state{max_packet_size = MaxPacketSize, heartbeat_sec = HeartbeatSec, ipv6_assist = Ipv6Assist}) -> +handle_event(info, {quic, new_stream, Stream, Opts}, waiting_stream, State=#state{max_packet_size = MaxPacketSize, heartbeat_sec = HeartbeatSec}) -> logger:debug("[sdlan_quic_channel] call new_stream: ~p, opts: ~p", [Stream, Opts]), + Ipv6Assist = case application:get_env(sdlan, ipv6_assist_info) of + {ok, {V6Bytes, Port}} -> + #'SDLV6Info' { + v6 = V6Bytes, + port = Port + }; + _ -> + undefined + end, %% 发送欢迎消息 WelcomePkt = sdlan_pb:encode_msg(#'SDLWelcome'{ version = 1, @@ -432,19 +439,3 @@ get_rules(SrcIdentityId, DstIdentityId) when is_integer(SrcIdentityId), is_integ SrcPolicyIds = identity_policy_ets:get_policies(SrcIdentityId), DstPolicyIds = identity_policy_ets:get_policies(DstIdentityId), rule_ets:get_rules(SrcPolicyIds, DstPolicyIds). - --spec ipv6_assist_info() -> undefined | #'SDLV6Info'{}. -ipv6_assist_info() -> - case application:get_env(sdlan, ipv6_assist) of - {ok, Props} -> - Port = proplists:get_value(port, Props, 0), - GlobalAddr6 = proplists:get_value(global_addr6, Props, <<"">>), - case {Port, sdlan_util:ipv6_to_bytes(GlobalAddr6)} of - {Port0, V6Bytes = <<_:128>>} when is_integer(Port0), Port0 > 0 -> - #'SDLV6Info'{port = Port0, v6 = V6Bytes}; - _ -> - undefined - end; - _ -> - undefined - end. diff --git a/src/sdlan_app.erl b/src/sdlan_app.erl index 537aae1..ba4ec1c 100644 --- a/src/sdlan_app.erl +++ b/src/sdlan_app.erl @@ -14,6 +14,14 @@ start(_StartType, _StartArgs) -> %% 加速内存的回收 erlang:system_flag(fullsweep_after, 16), + %% 设置环境变量 + case sdlan_util:ipv6_assist_info() of + {ok, V6Info} -> + application:set_env(sdlan, ipv6_assist_info, V6Info); + undefined -> + ok + end, + %% 启动注册表 sdlan_hostname_regedit:init(), sdlan_domain_regedit:init(), @@ -55,4 +63,4 @@ start_http_server() -> {max_connections, MaxConnections} ], {ok, Pid} = cowboy:start_clear(http_listener, TransOpts, #{env => #{dispatch => Dispatcher}}), - logger:debug("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]). \ No newline at end of file + logger:debug("[iot_app] the http server start at: ~p, pid is: ~p", [Port, Pid]). diff --git a/src/sdlan_util.erl b/src/sdlan_util.erl index 0d03cc8..b502bc9 100644 --- a/src/sdlan_util.erl +++ b/src/sdlan_util.erl @@ -14,7 +14,7 @@ -export([json_data/1, json_error/2]). -export([is_broadcast_mac/1, is_multicast_mac/1]). -export([ipv4_to_int/1, int_to_ipv4/1, ips/2, format_ip/1]). --export([ipv6_to_bytes/1, ipv6_bytes_to_binary/1]). +-export([ipv6_to_bytes/1, ipv6_bytes_to_binary/1, ipv6_assist_info/0]). -export([hmac/2]). -spec format_mac(Mac :: binary()) -> binary(). @@ -141,6 +141,22 @@ ipv6_bytes_to_binary(<>) -> ipv6_bytes_to_binary(_) -> <<"">>. +-spec ipv6_assist_info() -> undefined | {ok, {binary(), integer()}}. +ipv6_assist_info() -> + case application:get_env(sdlan, ipv6_assist) of + {ok, Props} -> + Port = proplists:get_value(port, Props, 0), + GlobalAddr6 = proplists:get_value(global_addr6, Props, <<"">>), + case {Port, ipv6_to_bytes(GlobalAddr6)} of + {Port0, V6Bytes = <<_:128>>} when is_integer(Port0), Port0 > 0 -> + {ok, {V6Bytes, Port0}}; + _ -> + undefined + end; + _ -> + undefined + end. + -spec hmac(Key :: binary(), Data :: binary()) -> string(). hmac(Key, Data) when is_binary(Key), is_binary(Data) -> Digest = crypto:mac(hmac, md5, Key, Data),