简化逻辑
This commit is contained in:
parent
9dd2bd3380
commit
c54a5b35ad
@ -80,8 +80,10 @@ handle_cast(_Request, State) ->
|
||||
{noreply, NewState :: #state{}, timeout() | hibernate} |
|
||||
{stop, Reason :: term(), NewState :: #state{}}.
|
||||
handle_info({udp, Sock, Ip, Port, Body}, State = #state{socket = Sock}) ->
|
||||
case catch sdlan_pb:decode_msg(Body, 'SDLV6AssistProbe') of
|
||||
#'SDLV6AssistProbe'{pkt_id = PktId} ->
|
||||
maybe
|
||||
ThrottleKey = {Ip, Port},
|
||||
ok ?= limit_check(ThrottleKey),
|
||||
#'SDLV6AssistProbe'{pkt_id = PktId} ?= catch sdlan_pb:decode_msg(Body, 'SDLV6AssistProbe'),
|
||||
V6Bytes = sdlan_util:ipv6_to_bytes(Ip),
|
||||
ReplyBin = sdlan_pb:encode_msg(#'SDLV6AssistProbeReply'{
|
||||
pkt_id = PktId,
|
||||
@ -90,9 +92,7 @@ handle_info({udp, Sock, Ip, Port, Body}, State = #state{socket = Sock}) ->
|
||||
port = Port
|
||||
}
|
||||
}),
|
||||
ok = gen_udp:send(Sock, Ip, Port, ReplyBin);
|
||||
_Err ->
|
||||
ok
|
||||
ok ?= gen_udp:send(Sock, Ip, Port, ReplyBin)
|
||||
end,
|
||||
{noreply, State};
|
||||
handle_info(_Info, State) ->
|
||||
@ -108,3 +108,13 @@ terminate(_Reason, _State = #state{}) ->
|
||||
{ok, NewState :: #state{}} | {error, Reason :: term()}.
|
||||
code_change(_OldVsn, State = #state{}, _Extra) ->
|
||||
{ok, State}.
|
||||
|
||||
%% 访问频率限制
|
||||
-spec limit_check(ThrottleKey :: any()) -> ok | limited.
|
||||
limit_check(ThrottleKey) ->
|
||||
case throttle:check(sdlan_ipv6_assist, ThrottleKey) of
|
||||
{ok, _RestCount, _LeftToReset} ->
|
||||
ok;
|
||||
{limit_exceeded, 0, _LeftToReset} ->
|
||||
limited
|
||||
end.
|
||||
@ -22,10 +22,12 @@ start(_StartType, _StartArgs) ->
|
||||
ok
|
||||
end,
|
||||
|
||||
%% 现在ipv6探测工具的使用频率
|
||||
throttle:setup(sdlan_ipv6_assist, 60, per_minute),
|
||||
|
||||
%% 启动注册表
|
||||
sdlan_hostname_regedit:init(),
|
||||
sdlan_domain_regedit:init(),
|
||||
sdlan_token_store:init(),
|
||||
|
||||
%% 权限的数据管理
|
||||
identity_policy_ets:init(),
|
||||
|
||||
@ -1,43 +0,0 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @author anlicheng
|
||||
%%% @copyright (C) 2026, <COMPANY>
|
||||
%%% @doc
|
||||
%%% Token存储
|
||||
%%% @end
|
||||
%%% Created : 16. 4月 2026 12:00
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(sdlan_token_store).
|
||||
-author("anlicheng").
|
||||
|
||||
%% API
|
||||
-export([init/0, store/1, lookup/1, delete/1, generate_token/0]).
|
||||
|
||||
-define(TABLE, sdlan_token_store).
|
||||
|
||||
-spec init() -> ok.
|
||||
init() ->
|
||||
ets:new(?TABLE, [named_table, set, public, {read_concurrency, true}, {write_concurrency, true}]).
|
||||
|
||||
generate_token() ->
|
||||
Token = sdlan_util:rand_byte(128),
|
||||
store(Token),
|
||||
Token.
|
||||
|
||||
-spec store(Token :: binary()) -> ok.
|
||||
store(Token) when is_binary(Token) ->
|
||||
true = ets:insert(?TABLE, {Token}),
|
||||
ok.
|
||||
|
||||
-spec lookup(Token :: binary()) -> boolean().
|
||||
lookup(Token) when is_binary(Token) ->
|
||||
case ets:lookup(?TABLE, Token) of
|
||||
[{Token}] ->
|
||||
true;
|
||||
[] ->
|
||||
false
|
||||
end.
|
||||
|
||||
-spec delete(Token :: binary()) -> ok.
|
||||
delete(Token) when is_binary(Token) ->
|
||||
true = ets:delete(?TABLE, Token),
|
||||
ok.
|
||||
Loading…
x
Reference in New Issue
Block a user